From 10a6c27ab03189cf5c504d09affedb309d1dafe6 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Mon, 28 Dec 2020 16:08:48 +0100 Subject: check once a week for an app update --- .../main/java/se/leap/bitmaskclient/Constants.java | 1 + .../java/se/leap/bitmaskclient/EipSetupObserver.java | 9 ++++++++- .../se/leap/bitmaskclient/utils/PreferenceHelper.java | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) (limited to 'app/src/main') diff --git a/app/src/main/java/se/leap/bitmaskclient/Constants.java b/app/src/main/java/se/leap/bitmaskclient/Constants.java index f62af638..5fcaba2f 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Constants.java +++ b/app/src/main/java/se/leap/bitmaskclient/Constants.java @@ -37,6 +37,7 @@ public interface Constants { String SHOW_EXPERIMENTAL = "show_experimental"; String USE_IPv6_FIREWALL = "use_ipv6_firewall"; String RESTART_ON_UPDATE = "restart_on_update"; + String LAST_UPDATE_CHECK = "last_update_check"; ////////////////////////////////////////////// diff --git a/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java b/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java index 174e6657..e365c857 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java @@ -77,6 +77,7 @@ class EipSetupObserver extends BroadcastReceiver implements VpnStatus.StateListe //The real timout is 4*2s + 1*4s + 1*8s + 1*16s + 1*32s + 1*64s = 132 s; private static final String TIMEOUT = "4"; + private static final int UPDATE_CHECK_TIMEOUT = 1000*60*60*24*7; private Context context; private VpnProfile setupVpnProfile; private String observedProfileFromVpnStatus; @@ -312,13 +313,19 @@ class EipSetupObserver extends BroadcastReceiver implements VpnStatus.StateListe ProviderAPICommand.execute(context, ProviderAPI.DOWNLOAD_SERVICE_JSON, provider); } - DownloadServiceCommand.execute(context, CHECK_VERSION_FILE); + if (shouldCheckAppUpdate()) { + DownloadServiceCommand.execute(context, CHECK_VERSION_FILE); + } finishGatewaySetup(false); } else if ("TCP_CONNECT".equals(state)) { changingGateway.set(false); } } + private boolean shouldCheckAppUpdate() { + return System.currentTimeMillis() - PreferenceHelper.getLastAppUpdateCheck(context) >= UPDATE_CHECK_TIMEOUT; + } + private void selectNextGateway() { changingGateway.set(true); reconnectTry.set(0); diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java index 5ca2ec75..5b62d0ff 100644 --- a/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java +++ b/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java @@ -22,6 +22,7 @@ import static se.leap.bitmaskclient.Constants.ALLOW_TETHERING_WIFI; import static se.leap.bitmaskclient.Constants.ALWAYS_ON_SHOW_DIALOG; import static se.leap.bitmaskclient.Constants.DEFAULT_SHARED_PREFS_BATTERY_SAVER; import static se.leap.bitmaskclient.Constants.EXCLUDED_APPS; +import static se.leap.bitmaskclient.Constants.LAST_UPDATE_CHECK; import static se.leap.bitmaskclient.Constants.LAST_USED_PROFILE; import static se.leap.bitmaskclient.Constants.PROVIDER_CONFIGURED; import static se.leap.bitmaskclient.Constants.PROVIDER_EIP_DEFINITION; @@ -122,6 +123,14 @@ public class PreferenceHelper { apply(); } + public static void setLastAppUpdateCheck(Context context) { + putLong(context, LAST_UPDATE_CHECK, System.currentTimeMillis()); + } + + public static long getLastAppUpdateCheck(Context context) { + return getLong(context, LAST_UPDATE_CHECK, 0); + } + public static void restartOnUpdate(Context context, boolean isEnabled) { putBoolean(context, RESTART_ON_UPDATE, isEnabled); } @@ -223,6 +232,16 @@ public class PreferenceHelper { return preferences.getStringSet(EXCLUDED_APPS, new HashSet<>()); } + public static long getLong(Context context, String key, long defValue) { + SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); + return preferences.getLong(key, defValue); + } + + public static void putLong(Context context, String key, long value) { + SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); + preferences.edit().putLong(key, value).apply(); + } + public static String getString(Context context, String key, String defValue) { SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); return preferences.getString(key, defValue); -- cgit v1.2.3