| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import static org.telegram.ui.Components.BlockingUpdateView.checkApkInstallPermissions; |
| 4 | |
| 5 | import android.app.Activity; |
| 6 | import android.content.Context; |
| 7 | import android.content.Intent; |
| 8 | import android.net.Uri; |
| 9 | import android.os.Build; |
| 10 | import android.widget.Toast; |
| 11 | |
| 12 | import androidx.core.content.FileProvider; |
| 13 | |
| 14 | import com.google.gson.Gson; |
| 15 | |
| 16 | import org.telegram.messenger.AndroidUtilities; |
| 17 | import org.telegram.messenger.ApplicationLoader; |
| 18 | import org.telegram.messenger.BuildConfig; |
| 19 | import org.telegram.messenger.BuildVars; |
| 20 | import org.telegram.messenger.FileLog; |
| 21 | import org.telegram.messenger.LocaleController; |
| 22 | import org.telegram.messenger.R; |
| 23 | import org.telegram.messenger.browser.Browser; |
| 24 | import org.telegram.ui.ActionBar.AlertDialog; |
| 25 | |
| 26 | import java.io.File; |
| 27 | import java.text.SimpleDateFormat; |
| 28 | import java.util.Locale; |
| 29 | |
| 30 | import cn.hutool.http.HttpRequest; |
| 31 | import tw.nekomimi.nekogram.utils.FileUtil; |
| 32 | |
| 33 | //TODO use UpdateAppAlertDialog / BlockingUpdateView? |
| 34 | |
| 35 | public class InternalUpdater { |
| 36 | private static final String API_URL_RELEASE = "https://api.github.com/repos/NekoX-Dev/NekoX/releases?per_page=10"; |
| 37 | |
| 38 | private static class ReleaseMetadata { |
| 39 | String name; |
| 40 | String body; |
| 41 | String published_at; |
| 42 | String html_url; |
| 43 | ApkMetadata[] assets; |
| 44 | } |
| 45 | |
| 46 | private static class ApkMetadata { |
| 47 | String name; |
| 48 | String browser_download_url; |
| 49 | } |
| 50 | |
| 51 | private static class GithubApiContents { |
| 52 | String content; |
| 53 | } |
| 54 | |
| 55 | // as a base64 encoded json |
| 56 | private static class NekoXReleaseNote { |
| 57 | NekoXAPK[] apks; |
| 58 | } |
| 59 | |
| 60 | private static class NekoXAPK { |
| 61 | String name; |
| 62 | String sha1; |
| 63 | String[] urls; // https://t.me/xxx or bdex://xxx, bdex removed |
| 64 | } |
| 65 | |
| 66 | private static ApkMetadata matchBuild(ApkMetadata[] apks) { |
| 67 | String target = BuildConfig.FLAVOR + "-" + FileUtil.getAbi() + "-" + BuildConfig.BUILD_TYPE + ".apk"; |
| 68 | FileLog.e(target); |
| 69 | for (ApkMetadata apk : apks) { |
| 70 | if (apk.name.contains(target)) |
| 71 | return apk; |
| 72 | } |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | public static void checkUpdate(Context ctx, boolean isAutoCheck) { |
| 77 | if (BuildVars.isFdroid) |
| 78 | return; |
| 79 | |
| 80 | //cleanup |
| 81 | File f = new File(ApplicationLoader.getDataDirFixed(), "cache/new.apk"); |
| 82 | if (f.exists()) f.delete(); |
| 83 | |
| 84 | try { |
| 85 | NekoXConfig.setNextUpdateCheck(System.currentTimeMillis() / 1000 + 24 * 3600); |
| 86 | |
| 87 | //TODO update URL when api.github.com get banned. |
| 88 | String ret = HttpRequest.get(API_URL_RELEASE).header("accept", "application/vnd.github.v3+json").execute().body(); |
| 89 | ReleaseMetadata[] releases = new Gson().fromJson(ret, ReleaseMetadata[].class); |
| 90 | ReleaseMetadata release = null; |
| 91 | |
| 92 | // Not now. |
| 93 | String releaseChannel = "stable"; |
| 94 | switch (NekoXConfig.autoUpdateReleaseChannel) { |
| 95 | case 2: |
| 96 | releaseChannel = "rc"; |
| 97 | break; |
| 98 | case 3: |
| 99 | releaseChannel = "preview"; |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | for (ReleaseMetadata rel : releases) { |
| 104 | if (rel.name.equals("v" + BuildConfig.VERSION_NAME)) |
| 105 | break; |
| 106 | if (rel.name.contains("rc") && NekoXConfig.autoUpdateReleaseChannel < 2 || rel.name.contains("preview") && NekoXConfig.autoUpdateReleaseChannel < 3) |
| 107 | continue; |
| 108 | release = rel; |
| 109 | break; |
| 110 | } |
| 111 | if (release == null) { |
| 112 | FileLog.d("no update"); |
| 113 | if (!isAutoCheck) |
| 114 | AndroidUtilities.runOnUIThread(() -> Toast.makeText(ctx, LocaleController.getString("VersionUpdateNoUpdate", R.string.VersionUpdateNoUpdate), Toast.LENGTH_SHORT).show()); |
| 115 | return; |
| 116 | } else if (release.name.equals(NekoXConfig.ignoredUpdateTag) && isAutoCheck) { |
| 117 | FileLog.d("ignored tag " + release.name); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | // match release apk urls |
| 122 | final ApkMetadata apk = matchBuild(release.assets); |
| 123 | |
| 124 | // match apk urls. these can be empty. |
| 125 | |
| 126 | ReleaseMetadata finalRelease = release; |
| 127 | |
| 128 | AndroidUtilities.runOnUIThread(() -> { |
| 129 | AlertDialog.Builder builder = new AlertDialog.Builder(ctx); |
| 130 | builder.setTitle(LocaleController.getString("VersionUpdateTitle", R.string.VersionUpdateTitle)); |
| 131 | |
| 132 | String message = null; |
| 133 | try { |
| 134 | message = finalRelease.name + " " + LocaleController.formatDateChat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US).parse(finalRelease.published_at).getTime() / 1000) + "\n\n" |
| 135 | } catch (Exception e) { |
| 136 | FileLog.e(e); |
| 137 | } |
| 138 | if (apk == null) |
| 139 | message += LocaleController.getString("VersionUpdateVariantNotMatch", R.string.VersionUpdateVariantNotMatch); |
| 140 | else |
| 141 | message += apk.name.replace(".apk", ""); |
| 142 | builder.setMessage(message); |
| 143 | |
| 144 | builder.setPositiveButton(LocaleController.getString("VersionUpdateConfirm", R.string.VersionUpdateConfirm), (dialog, which) -> { |
| 145 | Browser.openUrl(ctx, apk.browser_download_url); |
| 146 | }); |
| 147 | builder.setNeutralButton(LocaleController.getString("VersionUpdateIgnore", R.string.VersionUpdateIgnore), (dialog, which) -> NekoXConfig.setIgnoredUpdateTag(finalRelease.name)); |
| 148 | builder.setNegativeButton(LocaleController.getString("VersionUpdateNotNow", R.string.VersionUpdateNotNow), (dialog, which) -> NekoXConfig.setNextUpdateCheck(System.currentTimeMillis() / 1000 + 3 * 24 * 3600)); |
| 149 | builder.show(); |
| 150 | }); |
| 151 | } catch (Exception e) { |
| 152 | FileLog.e(e); |
| 153 | if (!isAutoCheck) |
| 154 | AndroidUtilities.runOnUIThread(() -> Toast.makeText(ctx, "An exception occurred during checking updates.", Toast.LENGTH_SHORT).show()); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public static boolean openApkInstall(Activity activity, File f) { |
| 159 | if (!checkApkInstallPermissions(activity)) { |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | boolean exists = false; |
| 164 | try { |
| 165 | if (exists = f.exists()) { |
| 166 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 167 | intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 168 | |
| 169 | if (Build.VERSION.SDK_INT >= 24) { |
| 170 | intent.setDataAndType(FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", f), "application/vnd.android.package-archive"); |
| 171 | } else { |
| 172 | intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive"); |
| 173 | } |
| 174 | try { |
| 175 | activity.startActivityForResult(intent, 500); |
| 176 | } catch (Exception e) { |
| 177 | FileLog.e(e); |
| 178 | } |
| 179 | } |
| 180 | } catch (Exception e) { |
| 181 | FileLog.e(e); |
| 182 | } |
| 183 | return exists; |
| 184 | } |
| 185 | |
| 186 | } |