|
| 1 | +package com.fox2code.mmm; |
| 2 | + |
| 3 | +import android.util.Log; |
| 4 | + |
| 5 | +import com.fox2code.mmm.utils.Http; |
| 6 | + |
| 7 | +import org.json.JSONArray; |
| 8 | +import org.json.JSONObject; |
| 9 | + |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | + |
| 12 | +// See https://docs.github.com/en/rest/reference/repos#releases |
| 13 | +public class AppUpdateManager { |
| 14 | + private static final String TAG = "AppUpdateManager"; |
| 15 | + private static final AppUpdateManager INSTANCE = new AppUpdateManager(); |
| 16 | + private static final String RELEASES_API_URL = |
| 17 | + "https://api.github.com/repos/Fox2Code/FoxMagiskModuleManager/releases"; |
| 18 | + |
| 19 | + public static AppUpdateManager getAppUpdateManager() { |
| 20 | + return INSTANCE; |
| 21 | + } |
| 22 | + |
| 23 | + private final Object updateLock = new Object(); |
| 24 | + private String latestRelease; |
| 25 | + private String latestPreRelease; |
| 26 | + private long lastChecked; |
| 27 | + private boolean preReleaseNewer; |
| 28 | + |
| 29 | + private AppUpdateManager() { |
| 30 | + this.latestRelease = MainApplication.getBootSharedPreferences() |
| 31 | + .getString("updater_latest_release", BuildConfig.VERSION_NAME); |
| 32 | + this.latestPreRelease = MainApplication.getBootSharedPreferences() |
| 33 | + .getString("updater_latest_pre_release", BuildConfig.VERSION_NAME); |
| 34 | + this.lastChecked = 0; |
| 35 | + this.preReleaseNewer = true; |
| 36 | + } |
| 37 | + |
| 38 | + // Return true if should show a notification |
| 39 | + public boolean checkUpdate(boolean force) { |
| 40 | + if (this.peekShouldUpdate()) |
| 41 | + return true; |
| 42 | + long lastChecked = this.lastChecked; |
| 43 | + if (!force && lastChecked != 0 && |
| 44 | + // Avoid spam calls by putting a 10 seconds timer |
| 45 | + lastChecked < System.currentTimeMillis() - 10000L) |
| 46 | + return false; |
| 47 | + synchronized (this.updateLock) { |
| 48 | + if (lastChecked != this.lastChecked) |
| 49 | + return this.peekShouldUpdate(); |
| 50 | + boolean preReleaseNewer = true; |
| 51 | + try { |
| 52 | + JSONArray releases = new JSONArray(new String(Http.doHttpGet( |
| 53 | + RELEASES_API_URL, false), StandardCharsets.UTF_8)); |
| 54 | + String latestRelease = null, latestPreRelease = null; |
| 55 | + for (int i = releases.length() - 1; i > 0; i--) { |
| 56 | + JSONObject release = releases.getJSONObject(i); |
| 57 | + // Skip invalid entries |
| 58 | + if (release.getBoolean("draft")) continue; |
| 59 | + boolean preRelease = release.getBoolean("prerelease"); |
| 60 | + String version = release.getString("tag_name"); |
| 61 | + if (version.startsWith("v")) |
| 62 | + version = version.substring(1); |
| 63 | + if (preRelease) { |
| 64 | + latestPreRelease = version; |
| 65 | + } else { |
| 66 | + latestRelease = version; |
| 67 | + if (latestPreRelease == null) |
| 68 | + preReleaseNewer = false; |
| 69 | + } |
| 70 | + if (latestRelease != null && latestPreRelease != null) { |
| 71 | + break; // We read everything we needed to read. |
| 72 | + } |
| 73 | + } |
| 74 | + if (latestRelease != null) |
| 75 | + this.latestRelease = latestRelease; |
| 76 | + if (latestPreRelease != null) { |
| 77 | + this.latestPreRelease = latestPreRelease; |
| 78 | + this.preReleaseNewer = preReleaseNewer; |
| 79 | + } else if (!preReleaseNewer) { |
| 80 | + this.latestPreRelease = ""; |
| 81 | + this.preReleaseNewer = false; |
| 82 | + } |
| 83 | + Log.d(TAG, "Latest release: " + latestRelease); |
| 84 | + Log.d(TAG, "Latest pre-release: " + latestPreRelease); |
| 85 | + Log.d(TAG, "Latest pre-release newer: " + preReleaseNewer); |
| 86 | + this.lastChecked = System.currentTimeMillis(); |
| 87 | + } catch (Exception ioe) { |
| 88 | + Log.e("AppUpdateManager", "Failed to check releases", ioe); |
| 89 | + } |
| 90 | + } |
| 91 | + return this.peekShouldUpdate(); |
| 92 | + } |
| 93 | + |
| 94 | + public boolean peekShouldUpdate() { |
| 95 | + return !(BuildConfig.VERSION_NAME.equals(this.latestRelease) || |
| 96 | + (this.preReleaseNewer && |
| 97 | + BuildConfig.VERSION_NAME.equals(this.latestPreRelease))); |
| 98 | + } |
| 99 | + |
| 100 | + public boolean peekHasUpdate() { |
| 101 | + return !BuildConfig.VERSION_NAME.equals(this.preReleaseNewer ? |
| 102 | + this.latestPreRelease : this.latestRelease); |
| 103 | + } |
| 104 | +} |
0 commit comments