| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | import android.widget.Toast; |
| 5 | |
| 6 | import com.google.gson.Gson; |
| 7 | |
| 8 | import org.telegram.messenger.AndroidUtilities; |
| 9 | import org.telegram.messenger.BuildConfig; |
| 10 | import org.telegram.messenger.BuildVars; |
| 11 | import org.telegram.messenger.FileLog; |
| 12 | import org.telegram.messenger.LocaleController; |
| 13 | import org.telegram.messenger.R; |
| 14 | import org.telegram.messenger.browser.Browser; |
| 15 | import org.telegram.ui.ActionBar.AlertDialog; |
| 16 | |
| 17 | import java.text.SimpleDateFormat; |
| 18 | import java.util.Locale; |
| 19 | |
| 20 | import cn.hutool.http.HttpRequest; |
| 21 | import tw.nekomimi.nekogram.utils.FileUtil; |
| 22 | |
| 23 | |
| 24 | public class InternalUpdater { |
| 25 | private static final String API_URL_RELEASE = "https://api.github.com/repos/NekoX-Dev/NekoX/releases?per_page=10"; |
| 26 | |
| 27 | private static class ReleaseMetadata { |
| 28 | String name; |
| 29 | String body; |
| 30 | String published_at; |
| 31 | String html_url; |
| 32 | ApkMetadata[] assets; |
| 33 | } |
| 34 | |
| 35 | private static class ApkMetadata { |
| 36 | String name; |
| 37 | String browser_download_url; |
| 38 | } |
| 39 | |
| 40 | private static ApkMetadata matchBuild(ApkMetadata[] apks) { |
| 41 | String target = BuildConfig.FLAVOR + "-" + FileUtil.getAbi() + "-" + BuildConfig.BUILD_TYPE + ".apk"; |
| 42 | FileLog.e(target); |
| 43 | for (ApkMetadata apk : apks) { |
| 44 | if (apk.name.contains(target)) |
| 45 | return apk; |
| 46 | } |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | public static void checkUpdate(Context ctx, boolean isAutoCheck) { |
| 51 | if (BuildVars.isFdroid) |
| 52 | return; |
| 53 | try { |
| 54 | NekoXConfig.setNextUpdateCheck(System.currentTimeMillis() / 1000 + 24 * 3600); |
| 55 | |
| 56 | String ret = HttpRequest.get(API_URL_RELEASE).header("accept", "application/vnd.github.v3+json").execute().body(); |
| 57 | ReleaseMetadata[] releases = new Gson().fromJson(ret, ReleaseMetadata[].class); |
| 58 | |
| 59 | ReleaseMetadata release = null; |
| 60 | for (ReleaseMetadata rel : releases) { |
| 61 | if (rel.name.contains("rc") && NekoXConfig.autoUpdateReleaseChannel < 2 || rel.name.contains("preview") && NekoXConfig.autoUpdateReleaseChannel < 3) |
| 62 | continue; |
| 63 | if (rel.name.equals("v" + BuildConfig.VERSION_NAME)) |
| 64 | break; |
| 65 | release = rel; |
| 66 | break; |
| 67 | } |
| 68 | if (release == null) { |
| 69 | FileLog.d("no update"); |
| 70 | if (!isAutoCheck) |
| 71 | AndroidUtilities.runOnUIThread(() -> Toast.makeText(ctx, LocaleController.getString("VersionUpdateNoUpdate", R.string.VersionUpdateNoUpdate), Toast.LENGTH_SHORT).show()); |
| 72 | return; |
| 73 | } else if (release.name.equals(NekoXConfig.ignoredUpdateTag) && isAutoCheck) { |
| 74 | FileLog.d("ignored tag " + release.name); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | final ApkMetadata apk = matchBuild(release.assets); |
| 79 | ReleaseMetadata finalRelease = release; |
| 80 | AndroidUtilities.runOnUIThread(() -> { |
| 81 | AlertDialog.Builder builder = new AlertDialog.Builder(ctx); |
| 82 | builder.setTitle(LocaleController.getString("VersionUpdateTitle", R.string.VersionUpdateTitle)); |
| 83 | |
| 84 | String message = null; |
| 85 | try { |
| 86 | 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" |
| 87 | } catch (Exception e) { |
| 88 | FileLog.e(e); |
| 89 | } |
| 90 | if (apk == null) |
| 91 | message += LocaleController.getString("VersionUpdateVariantNotMatch", R.string.VersionUpdateVariantNotMatch); |
| 92 | else |
| 93 | message += apk.name.replace(".apk", ""); |
| 94 | builder.setMessage(message); |
| 95 | |
| 96 | builder.setPositiveButton(LocaleController.getString("VersionUpdateConfirm", R.string.VersionUpdateConfirm), (dialog, which) -> { |
| 97 | if (apk != null) |
| 98 | Browser.openUrl(ctx, apk.browser_download_url); |
| 99 | else |
| 100 | Browser.openUrl(ctx, finalRelease.html_url); |
| 101 | }); |
| 102 | builder.setNeutralButton(LocaleController.getString("VersionUpdateIgnore", R.string.VersionUpdateIgnore), (dialog, which) -> NekoXConfig.setIgnoredUpdateTag(finalRelease.name)); |
| 103 | builder.setNegativeButton(LocaleController.getString("VersionUpdateNotNow", R.string.VersionUpdateNotNow), (dialog, which) -> NekoXConfig.setNextUpdateCheck(System.currentTimeMillis() / 1000 + 3 * 24 * 3600)); |
| 104 | builder.show(); |
| 105 | }); |
| 106 | } catch (Exception e) { |
| 107 | FileLog.e(e); |
| 108 | if (!isAutoCheck) |
| 109 | AndroidUtilities.runOnUIThread(() -> Toast.makeText(ctx, "An exception occurred during checking updates.", Toast.LENGTH_SHORT).show()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | } |
| 114 | |