|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2019 PYTHONKOR |
| 5 | +
|
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.ksi.alltv; |
| 26 | + |
| 27 | +import android.app.AlertDialog; |
| 28 | +import android.app.Dialog; |
| 29 | +import android.content.Context; |
| 30 | +import android.content.pm.PackageInfo; |
| 31 | +import android.content.pm.PackageManager; |
| 32 | +import android.os.AsyncTask; |
| 33 | + |
| 34 | +import com.google.gson.JsonObject; |
| 35 | +import com.google.gson.JsonParser; |
| 36 | + |
| 37 | + |
| 38 | +public class VersionChecker { |
| 39 | + |
| 40 | + private Context mContext; |
| 41 | + private String mPackageName, mUpdateJsonUrl, mCurrentVersion, mPatchedVersion; |
| 42 | + private Dialog mDialog; |
| 43 | + private String mOkButtonText; |
| 44 | + private String mDialogMessageText; |
| 45 | + private String mDialogTitleText; |
| 46 | + |
| 47 | + public VersionChecker(Context activity) { |
| 48 | + this.mContext = activity; |
| 49 | + this.getCurrentVersion(); |
| 50 | + } |
| 51 | + |
| 52 | + public void check() { |
| 53 | + new GetLatestVersion().execute(); |
| 54 | + } |
| 55 | + |
| 56 | + public void setokButtonText(String okButtonText) { |
| 57 | + this.mOkButtonText = okButtonText; |
| 58 | + } |
| 59 | + |
| 60 | + public void setmDialogMessageText(String mDialogMessageText) { |
| 61 | + this.mDialogMessageText = mDialogMessageText; |
| 62 | + } |
| 63 | + |
| 64 | + public void setmDialogTitleText(String mDialogTitleText) { |
| 65 | + this.mDialogTitleText = mDialogTitleText; |
| 66 | + } |
| 67 | + |
| 68 | + public void setcheckJsonUrl(String checkUrl) { |
| 69 | + this.mUpdateJsonUrl = checkUrl; |
| 70 | + } |
| 71 | + |
| 72 | + public final String getAppDataString(int resourceId) { |
| 73 | + return mContext.getResources().getString(resourceId); |
| 74 | + } |
| 75 | + |
| 76 | + private void getCurrentVersion() { |
| 77 | + PackageManager pm = mContext.getPackageManager(); |
| 78 | + PackageInfo pInfo; |
| 79 | + |
| 80 | + try { |
| 81 | + mPackageName = mContext.getPackageName(); |
| 82 | + pInfo = pm.getPackageInfo(mContext.getPackageName(), 0); |
| 83 | + mCurrentVersion = pInfo.versionName; |
| 84 | + } catch (PackageManager.NameNotFoundException e1) { |
| 85 | + e1.printStackTrace(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private class GetLatestVersion extends AsyncTask<String, String, Integer> { |
| 90 | + |
| 91 | + @Override |
| 92 | + protected void onPreExecute() { |
| 93 | + super.onPreExecute(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + protected Integer doInBackground(String... params) { |
| 98 | + try { |
| 99 | + |
| 100 | + String resultJson = HttpRequest.get(mUpdateJsonUrl, true).body(); |
| 101 | + |
| 102 | + JsonParser jParser = new JsonParser(); |
| 103 | + JsonObject jObj = jParser.parse(resultJson).getAsJsonObject(); |
| 104 | + |
| 105 | + mPatchedVersion = jObj.get(getAppDataString(R.string.PATCHED_STR)).getAsString(); |
| 106 | + |
| 107 | + } catch (Exception e) { |
| 108 | + e.printStackTrace(); |
| 109 | + } |
| 110 | + |
| 111 | + return 0; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + protected void onPostExecute(Integer jsonObject) { |
| 116 | + if (mPatchedVersion != null && mPatchedVersion.length() > 0) { |
| 117 | + if (!mCurrentVersion.equalsIgnoreCase(mPatchedVersion)) { |
| 118 | + showUpdateDialog(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + super.onPostExecute(jsonObject); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private void showUpdateDialog() { |
| 127 | + final AlertDialog.Builder builder = new AlertDialog.Builder(mContext); |
| 128 | + |
| 129 | + builder.setTitle(mDialogTitleText != null ? |
| 130 | + mDialogTitleText : getAppDataString(R.string.update_title_str)); |
| 131 | + |
| 132 | + builder.setMessage(mDialogMessageText != null ? |
| 133 | + mDialogMessageText : getAppDataString(R.string.update_version_str)); |
| 134 | + |
| 135 | + builder.setPositiveButton(mOkButtonText != null ? |
| 136 | + mOkButtonText : getAppDataString(R.string.ok_str), (dialog, which) -> { |
| 137 | + try { |
| 138 | + dialog.dismiss(); |
| 139 | + } catch (Exception e) { |
| 140 | + e.printStackTrace(); |
| 141 | + dialog.dismiss(); |
| 142 | + } |
| 143 | + }); |
| 144 | + |
| 145 | + mDialog = builder.show(); |
| 146 | + } |
| 147 | +} |
0 commit comments