From 430dd6164ce78d44cc17234eafe2d9c00e7424d1 Mon Sep 17 00:00:00 2001 From: sk Date: Mon, 23 Jan 2023 21:27:17 -0300 Subject: [PATCH] implement pre-release toggle --- .../updater/GithubSelfUpdaterImpl.java | 104 ++++++++++-------- .../android/GlobalUserPreferences.java | 7 +- .../android/fragments/SettingsFragment.java | 15 ++- .../main/res/layout/item_settings_switch.xml | 4 +- mastodon/src/main/res/values/strings_sk.xml | 1 + 5 files changed, 78 insertions(+), 53 deletions(-) diff --git a/mastodon/src/github/java/org/joinmastodon/android/updater/GithubSelfUpdaterImpl.java b/mastodon/src/github/java/org/joinmastodon/android/updater/GithubSelfUpdaterImpl.java index 217e09d73..f7e479876 100644 --- a/mastodon/src/github/java/org/joinmastodon/android/updater/GithubSelfUpdaterImpl.java +++ b/mastodon/src/github/java/org/joinmastodon/android/updater/GithubSelfUpdaterImpl.java @@ -14,12 +14,14 @@ import android.os.Build; import android.util.Log; import android.widget.Toast; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.joinmastodon.android.BuildConfig; import org.joinmastodon.android.E; +import org.joinmastodon.android.GlobalUserPreferences; import org.joinmastodon.android.MastodonApp; import org.joinmastodon.android.R; import org.joinmastodon.android.api.MastodonAPIController; @@ -117,60 +119,66 @@ public class GithubSelfUpdaterImpl extends GithubSelfUpdater{ .build(); Call call=MastodonAPIController.getHttpClient().newCall(req); try(Response resp=call.execute()){ - JsonObject obj=JsonParser.parseReader(resp.body().charStream()).getAsJsonObject(); - String changelog=obj.get("body").getAsString(); - String tag=obj.get("tag_name").getAsString(); - Pattern pattern=Pattern.compile("v?(\\d+)\\.(\\d+)\\.(\\d+)\\+fork\\.(\\d+)"); - Matcher matcher=pattern.matcher(tag); - if(!matcher.find()){ - Log.w(TAG, "actuallyCheckForUpdates: release tag has wrong format: "+tag); - return; - } - int newMajor=Integer.parseInt(matcher.group(1)), - newMinor=Integer.parseInt(matcher.group(2)), - newRevision=Integer.parseInt(matcher.group(3)), - newForkNumber=Integer.parseInt(matcher.group(4)); - matcher=pattern.matcher(BuildConfig.VERSION_NAME); - String[] currentParts=BuildConfig.VERSION_NAME.split("[.+]"); - if(!matcher.find()){ - Log.w(TAG, "actuallyCheckForUpdates: current version has wrong format: "+BuildConfig.VERSION_NAME); - return; - } - int curMajor=Integer.parseInt(matcher.group(1)), - curMinor=Integer.parseInt(matcher.group(2)), - curRevision=Integer.parseInt(matcher.group(3)), - curForkNumber=Integer.parseInt(matcher.group(4)); - long newVersion=((long)newMajor << 32) | ((long)newMinor << 16) | newRevision; - long curVersion=((long)curMajor << 32) | ((long)curMinor << 16) | curRevision; - if(newVersion>curVersion || newForkNumber>curForkNumber){ - String version=newMajor+"."+newMinor+"."+newRevision+"+fork."+newForkNumber; - Log.d(TAG, "actuallyCheckForUpdates: new version: "+version); - for(JsonElement el:obj.getAsJsonArray("assets")){ - JsonObject asset=el.getAsJsonObject(); - if("moshidon.apk".equals(asset.get("name").getAsString()) && "application/vnd.android.package-archive".equals(asset.get("content_type").getAsString()) && "uploaded".equals(asset.get("state").getAsString())){ - long size=asset.get("size").getAsLong(); - String url=asset.get("browser_download_url").getAsString(); + JsonArray arr=JsonParser.parseReader(resp.body().charStream()).getAsJsonArray(); + for (JsonElement jsonElement : arr) { + JsonObject obj = jsonElement.getAsJsonObject(); + if (obj.get("prerelease").getAsBoolean() && !GlobalUserPreferences.enablePreReleases) continue; - UpdateInfo info=new UpdateInfo(); - info.size=size; - info.version=version; - info.changelog=changelog; - this.info=info; + String tag=obj.get("tag_name").getAsString(); + String changelog=obj.get("body").getAsString(); + Pattern pattern=Pattern.compile("v?(\\d+)\\.(\\d+)\\.(\\d+)\\+fork\\.(\\d+)"); + Matcher matcher=pattern.matcher(tag); + if(!matcher.find()){ + Log.w(TAG, "actuallyCheckForUpdates: release tag has wrong format: "+tag); + return; + } + int newMajor=Integer.parseInt(matcher.group(1)), + newMinor=Integer.parseInt(matcher.group(2)), + newRevision=Integer.parseInt(matcher.group(3)), + newForkNumber=Integer.parseInt(matcher.group(4)); + matcher=pattern.matcher(BuildConfig.VERSION_NAME); + String[] currentParts=BuildConfig.VERSION_NAME.split("[.+]"); + if(!matcher.find()){ + Log.w(TAG, "actuallyCheckForUpdates: current version has wrong format: "+BuildConfig.VERSION_NAME); + return; + } + int curMajor=Integer.parseInt(matcher.group(1)), + curMinor=Integer.parseInt(matcher.group(2)), + curRevision=Integer.parseInt(matcher.group(3)), + curForkNumber=Integer.parseInt(matcher.group(4)); + long newVersion=((long)newMajor << 32) | ((long)newMinor << 16) | newRevision; + long curVersion=((long)curMajor << 32) | ((long)curMinor << 16) | curRevision; + if(newVersion>curVersion || newForkNumber>curForkNumber){ + String version=newMajor+"."+newMinor+"."+newRevision+"+fork."+newForkNumber; + Log.d(TAG, "actuallyCheckForUpdates: new version: "+version); + for(JsonElement el:obj.getAsJsonArray("assets")){ + JsonObject asset=el.getAsJsonObject(); + if("moshidon.apk".equals(asset.get("name").getAsString()) && "application/vnd.android.package-archive".equals(asset.get("content_type").getAsString()) && "uploaded".equals(asset.get("state").getAsString())){ + long size=asset.get("size").getAsLong(); + String url=asset.get("browser_download_url").getAsString(); - getPrefs().edit() - .putLong("apkSize", size) - .putString("version", version) - .putString("apkURL", url) - .putInt("checkedByBuild", BuildConfig.VERSION_CODE) - .putString("changelog", changelog) - .remove("downloadID") - .apply(); + UpdateInfo info=new UpdateInfo(); + info.size=size; + info.version=version; + info.changelog=changelog; + this.info=info; - break; + getPrefs().edit() + .putLong("apkSize", size) + .putString("version", version) + .putString("apkURL", url) + .putString("changelog", changelog) + .putInt("checkedByBuild", BuildConfig.VERSION_CODE) + .remove("downloadID") + .apply(); + + break; + } } } + getPrefs().edit().putLong("lastCheck", System.currentTimeMillis()).apply(); + break; } - getPrefs().edit().putLong("lastCheck", System.currentTimeMillis()).apply(); }catch(Exception x){ Log.w(TAG, "actuallyCheckForUpdates", x); }finally{ diff --git a/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java b/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java index 27da14c36..e803088d9 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java +++ b/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java @@ -38,6 +38,7 @@ public class GlobalUserPreferences{ public static boolean disableAltTextReminder; public static boolean showAltIndicator; public static boolean showNoAltIndicator; + public static boolean enablePreReleases; public static String publishButtonText; public static ThemePreference theme; public static ColorPreference color; @@ -81,8 +82,9 @@ public class GlobalUserPreferences{ keepOnlyLatestNotification=prefs.getBoolean("keepOnlyLatestNotification", false); enableFabAutoHide=prefs.getBoolean("enableFabAutoHide", true); disableAltTextReminder=prefs.getBoolean("disableAltTextReminder", false); - showAltIndicator =prefs.getBoolean("showAltIndicator", true); - showNoAltIndicator =prefs.getBoolean("showNoAltIndicator", true); + showAltIndicator=prefs.getBoolean("showAltIndicator", true); + showNoAltIndicator=prefs.getBoolean("showNoAltIndicator", true); + enablePreReleases=prefs.getBoolean("enablePreReleases", false); publishButtonText=prefs.getString("publishButtonText", ""); theme=ThemePreference.values()[prefs.getInt("theme", 0)]; recentLanguages=fromJson(prefs.getString("recentLanguages", "{}"), recentLanguagesType, new HashMap<>()); @@ -124,6 +126,7 @@ public class GlobalUserPreferences{ .putBoolean("disableAltTextReminder", disableAltTextReminder) .putBoolean("showAltIndicator", showAltIndicator) .putBoolean("showNoAltIndicator", showNoAltIndicator) + .putBoolean("enablePreReleases", enablePreReleases) .putString("publishButtonText", publishButtonText) .putInt("theme", theme.ordinal()) .putString("color", color.name()) diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java index b50ff931e..01ab5e6e9 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java @@ -298,6 +298,14 @@ public class SettingsFragment extends MastodonToolbarFragment{ GlobalUserPreferences.recentLanguages.remove(accountID); GlobalUserPreferences.save(); }))); + if (GithubSelfUpdater.needSelfUpdating()) { + items.add(new SwitchItem(R.string.sk_updater_enable_pre_releases, 0, GlobalUserPreferences.enablePreReleases, i->{ + GlobalUserPreferences.enablePreReleases=i.checked; + GlobalUserPreferences.save(); + })); + checkForUpdateItem = new TextItem(R.string.sk_check_for_update, GithubSelfUpdater.getInstance()::checkForUpdates); + items.add(checkForUpdateItem); + } items.add(new TextItem(R.string.mo_clear_recent_emoji, ()-> { GlobalUserPreferences.recentEmojis.clear(); GlobalUserPreferences.save(); @@ -806,7 +814,12 @@ public class SettingsFragment extends MastodonToolbarFragment{ @Override public void onBind(SwitchItem item){ text.setText(item.text); - icon.setImageResource(item.icon); + if (item.icon == 0) { + icon.setVisibility(View.GONE); + } else { + icon.setVisibility(View.VISIBLE); + icon.setImageResource(item.icon); + } checkbox.setChecked(item.checked && item.enabled); checkbox.setEnabled(item.enabled); } diff --git a/mastodon/src/main/res/layout/item_settings_switch.xml b/mastodon/src/main/res/layout/item_settings_switch.xml index 6aa91e4dc..20538dad7 100644 --- a/mastodon/src/main/res/layout/item_settings_switch.xml +++ b/mastodon/src/main/res/layout/item_settings_switch.xml @@ -12,8 +12,7 @@ android:id="@+id/icon" android:layout_width="24dp" android:layout_height="24dp" - android:layout_marginStart="16dp" - android:layout_marginEnd="32dp" + android:layout_marginHorizontal="16dp" android:importantForAccessibility="no" android:tint="?android:textColorPrimary" tools:src="@drawable/ic_fluent_star_24_regular"/> @@ -23,6 +22,7 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" + android:layout_marginStart="16dp" android:paddingVertical="8dp" android:textSize="16sp" android:textColor="?android:textColorPrimary" diff --git a/mastodon/src/main/res/values/strings_sk.xml b/mastodon/src/main/res/values/strings_sk.xml index d32da7cd5..a35d21c45 100644 --- a/mastodon/src/main/res/values/strings_sk.xml +++ b/mastodon/src/main/res/values/strings_sk.xml @@ -234,4 +234,5 @@ No alt text available Indicator for alt texts Indicator for missing alt texts + Enable pre-releases \ No newline at end of file