From 3c3e22d48640639a7e009a323744e185a4dc7064 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Wed, 14 Jul 2021 16:16:29 -0400 Subject: [PATCH] v0.9.4: added an opt-in for betas in the update checker --- .../messages/windows/windows.properties | 1 + .../shatteredpixeldungeon/SPDSettings.java | 10 ++++++++++ .../services/updates/Updates.java | 11 ++++++++++- .../windows/WndSettings.java | 19 +++++++++++++++++++ .../services/updates/UpdateService.java | 5 ++++- .../services/updates/DebugUpdates.java | 7 ++++++- .../services/updates/GitHubUpdates.java | 11 +++++++---- 7 files changed, 57 insertions(+), 7 deletions(-) diff --git a/core/src/main/assets/messages/windows/windows.properties b/core/src/main/assets/messages/windows/windows.properties index 7d1dbce33..303b571bf 100644 --- a/core/src/main/assets/messages/windows/windows.properties +++ b/core/src/main/assets/messages/windows/windows.properties @@ -186,6 +186,7 @@ windows.wndsettings$uitab.key_bindings=Key Bindings windows.wndsettings$datatab.title=Connectivity Settings windows.wndsettings$datatab.news=Check for news windows.wndsettings$datatab.updates=Check for updates +windows.wndsettings$datatab.betas=Include beta updates windows.wndsettings$datatab.wifi=Only check on WiFi windows.wndsettings$audiotab.title=Audio Settings windows.wndsettings$audiotab.music_vol=Music Volume diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/SPDSettings.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/SPDSettings.java index f252574d0..2dc5a4ce2 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/SPDSettings.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/SPDSettings.java @@ -24,6 +24,7 @@ package com.shatteredpixel.shatteredpixeldungeon; import com.shatteredpixel.shatteredpixeldungeon.messages.Languages; import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene; import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene; +import com.watabou.noosa.Game; import com.watabou.noosa.audio.Music; import com.watabou.noosa.audio.Sample; import com.watabou.utils.GameSettings; @@ -268,6 +269,7 @@ public class SPDSettings extends GameSettings { public static final String KEY_NEWS = "news"; public static final String KEY_UPDATES = "updates"; + public static final String KEY_BETAS = "betas"; public static final String KEY_WIFI = "wifi"; public static final String KEY_NEWS_LAST_READ = "news_last_read"; @@ -288,6 +290,14 @@ public class SPDSettings extends GameSettings { return getBoolean(KEY_UPDATES, true); } + public static void betas(boolean value){ + put(KEY_BETAS, value); + } + + public static boolean betas(){ + return getBoolean(KEY_BETAS, Game.version.contains("BETA") || Game.version.contains("RC")); + } + public static void WiFi(boolean value){ put(KEY_WIFI, value); } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/Updates.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/Updates.java index 1cad81931..6175ee770 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/Updates.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/Updates.java @@ -41,11 +41,20 @@ public class Updates { return supportsUpdates() && service.isUpdateable(); } + public static boolean supportsBetaChannel(){ + return supportsUpdates() && service.supportsBetaChannel(); + } + public static void checkForUpdate(){ if (!isUpdateable()) return; if (lastCheck != null && (new Date().getTime() - lastCheck.getTime()) < CHECK_DELAY) return; - service.checkForUpdate(!SPDSettings.WiFi(), new UpdateService.UpdateResultCallback() { + //We do this so that automatically enabled beta checking (for users who DLed a beta) persists afterward + if (SPDSettings.betas()){ + SPDSettings.betas(true); + } + + service.checkForUpdate(!SPDSettings.WiFi(), SPDSettings.betas(), new UpdateService.UpdateResultCallback() { @Override public void onUpdateAvailable(AvailableUpdateData update) { lastCheck = new Date(); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndSettings.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndSettings.java index a2afadd8b..3649ca375 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndSettings.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndSettings.java @@ -528,6 +528,7 @@ public class WndSettings extends WndTabbed { ColorBlock sep1; CheckBox chkNews; CheckBox chkUpdates; + CheckBox chkBetas; CheckBox chkWifi; @Override @@ -561,6 +562,19 @@ public class WndSettings extends WndTabbed { }; chkUpdates.checked(SPDSettings.updates()); add(chkUpdates); + + if (Updates.supportsBetaChannel()){ + chkBetas = new CheckBox(Messages.get(this, "betas")) { + @Override + protected void onClick() { + super.onClick(); + SPDSettings.updates(checked()); + Updates.clearUpdate(); + } + }; + chkBetas.checked(SPDSettings.betas()); + add(chkBetas); + } } if (!DeviceCompat.isDesktop()){ @@ -596,6 +610,11 @@ public class WndSettings extends WndTabbed { } } + if (chkBetas != null){ + chkBetas.setRect(0, pos + GAP, width, BTN_HEIGHT); + pos = chkBetas.bottom(); + } + if (chkWifi != null){ chkWifi.setRect(0, pos + GAP, width, BTN_HEIGHT); pos = chkWifi.bottom(); diff --git a/services/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/UpdateService.java b/services/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/UpdateService.java index 1df7d2a8f..d656e0f61 100644 --- a/services/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/UpdateService.java +++ b/services/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/UpdateService.java @@ -34,7 +34,10 @@ public abstract class UpdateService { //whether the app is updateable via an ingame prompt (e.g. not a demo or an android instant app) public abstract boolean isUpdateable(); - public abstract void checkForUpdate( boolean useMetered, UpdateResultCallback callback ); + //whether the service supports an opt-in channel for betas + public abstract boolean supportsBetaChannel(); + + public abstract void checkForUpdate( boolean useMetered, boolean includeBetas, UpdateResultCallback callback ); public abstract void initializeUpdate( AvailableUpdateData update ); diff --git a/services/updates/debugUpdates/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/DebugUpdates.java b/services/updates/debugUpdates/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/DebugUpdates.java index 8c1bfdb73..120e9e9ab 100644 --- a/services/updates/debugUpdates/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/DebugUpdates.java +++ b/services/updates/debugUpdates/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/DebugUpdates.java @@ -35,7 +35,12 @@ public class DebugUpdates extends UpdateService { } @Override - public void checkForUpdate(boolean useMetered, UpdateResultCallback callback) { + public boolean supportsBetaChannel() { + return true; + } + + @Override + public void checkForUpdate(boolean useMetered, boolean includeBetas, UpdateResultCallback callback) { if (!useMetered && !Game.platform.connectedToUnmeteredNetwork()){ callback.onConnectionFailed(); diff --git a/services/updates/githubUpdates/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/GitHubUpdates.java b/services/updates/githubUpdates/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/GitHubUpdates.java index e0806cf84..a59d02d79 100644 --- a/services/updates/githubUpdates/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/GitHubUpdates.java +++ b/services/updates/githubUpdates/src/main/java/com/shatteredpixel/shatteredpixeldungeon/services/updates/GitHubUpdates.java @@ -44,7 +44,12 @@ public class GitHubUpdates extends UpdateService { } @Override - public void checkForUpdate(boolean useMetered, UpdateResultCallback callback) { + public boolean supportsBetaChannel() { + return true; + } + + @Override + public void checkForUpdate(boolean useMetered, boolean includeBetas, UpdateResultCallback callback) { if (!useMetered && !Game.platform.connectedToUnmeteredNetwork()){ callback.onConnectionFailed(); @@ -62,15 +67,13 @@ public class GitHubUpdates extends UpdateService { Bundle latestRelease = null; int latestVersionCode = Game.versionCode; - boolean includePrereleases = Game.version.contains("-BETA-") || Game.version.contains("-RC-"); - for (Bundle b : Bundle.read( httpResponse.getResultAsStream() ).getBundleArray()){ Matcher m = versionCodePattern.matcher(b.getString("body")); if (m.find()){ int releaseVersion = Integer.parseInt(m.group(1)); if (releaseVersion > latestVersionCode - && (includePrereleases || !b.getBoolean("prerelease"))){ + && (includeBetas || !b.getBoolean("prerelease"))){ latestRelease = b; latestVersionCode = releaseVersion; }