From 7b6fbb801e8c8b798bbc0a52a5c4642b8ed9f2b8 Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Fri, 2 Jun 2023 12:57:15 +0200 Subject: [PATCH 1/2] Initialize properly timestamps at instanciation of WeatherUpdater Indeed, those timestamps are persistent accross boots --- .../e/blisslauncher/features/weather/WeatherUpdater.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java index a9b1f1b595..7807c606a0 100644 --- a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java +++ b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java @@ -61,6 +61,8 @@ public class WeatherUpdater { private WeatherUpdater(@NonNull Context context) { mWeakContext = new WeakReference<>(context); + Preferences.setLastWeatherUpdateTimestamp(context, 0); + Preferences.setForceRequestLastTry(context, 0); } public void checkWeatherRequest() { -- GitLab From e61b705f8a690bd4a07efa6a332d710411d5df71 Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Fri, 2 Jun 2023 16:20:55 +0200 Subject: [PATCH 2/2] Do not use shared pref to maintain timestamp Variables in RAM is enough since we don't need boot persistency. --- .../e/blisslauncher/core/Preferences.java | 16 ---------------- .../features/weather/WeatherUpdater.java | 16 ++++++++-------- 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/foundation/e/blisslauncher/core/Preferences.java b/app/src/main/java/foundation/e/blisslauncher/core/Preferences.java index 70e6db98d6..f6d20b35d3 100644 --- a/app/src/main/java/foundation/e/blisslauncher/core/Preferences.java +++ b/app/src/main/java/foundation/e/blisslauncher/core/Preferences.java @@ -246,22 +246,6 @@ public class Preferences { return getPrefs(context).getString(Constants.CACHED_CITY, fallbackCity); } - public static long lastWeatherUpdateTimestamp(Context context) { - return getPrefs(context).getLong(Constants.WEATHER_LAST_UPDATE, 0); - } - - public static void setLastWeatherUpdateTimestamp(Context context, long timestamp) { - getPrefs(context).edit().putLong(Constants.WEATHER_LAST_UPDATE, timestamp).apply(); - } - - public static void setForceRequestLastTry(Context context, long timestamp) { - getPrefs(context).edit().putLong(Constants.FORCE_WEATHER_LAST_TRY, timestamp).apply(); - } - - public static long getForceRequestLastTry(Context context) { - return getPrefs(context).getLong(Constants.FORCE_WEATHER_LAST_TRY, 0); - } - public static WeatherInfo getCachedWeatherInfo(Context context) { final String cachedInfo = getPrefs(context).getString(Constants.WEATHER_DATA, null); diff --git a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java index 7807c606a0..31266e0af8 100644 --- a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java +++ b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java @@ -49,6 +49,9 @@ public class WeatherUpdater { private final WeakReference mWeakContext; private long mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS; + private long mLastWeatherUpdateTimeStamp = 0; + private long mForceRequestLastTry = 0; + private static WeatherUpdater mInstance = null; public static WeatherUpdater getInstance(@NonNull Context context) { @@ -61,14 +64,12 @@ public class WeatherUpdater { private WeatherUpdater(@NonNull Context context) { mWeakContext = new WeakReference<>(context); - Preferences.setLastWeatherUpdateTimestamp(context, 0); - Preferences.setForceRequestLastTry(context, 0); } public void checkWeatherRequest() { Context context = mWeakContext.get(); long refreshPeriod = Preferences.weatherRefreshIntervalInMs(context); - long elapsedTime = Math.abs(SystemClock.elapsedRealtime() - Preferences.lastWeatherUpdateTimestamp(context)); + long elapsedTime = Math.abs(SystemClock.elapsedRealtime() - mLastWeatherUpdateTimeStamp); boolean isPeriodicRequestAllowed = refreshPeriod != 0 && elapsedTime >= refreshPeriod; if (isPeriodicRequestAllowed) { @@ -84,14 +85,13 @@ public class WeatherUpdater { } private boolean canForceWeatherRequest() { - Context context = mWeakContext.get(); - - long elapsedTime = Math.abs(SystemClock.elapsedRealtime() - Preferences.getForceRequestLastTry(context)); + long systemTime = SystemClock.elapsedRealtime(); + long elapsedTime = Math.abs(systemTime - mForceRequestLastTry); boolean isRequestAllowed = elapsedTime >= mForceRequestPeriodInMs; if (isRequestAllowed) { - Preferences.setForceRequestLastTry(context, SystemClock.elapsedRealtime()); + mForceRequestLastTry = systemTime; } return isRequestAllowed; @@ -190,7 +190,7 @@ public class WeatherUpdater { long now = SystemClock.elapsedRealtime(); Preferences.setCachedWeatherInfo(context, now, weatherInfo); - Preferences.setLastWeatherUpdateTimestamp(context, now); + mLastWeatherUpdateTimeStamp = now; Intent updateIntent = new Intent(WeatherUpdateService.ACTION_UPDATE_FINISHED); LocalBroadcastManager.getInstance(context).sendBroadcast(updateIntent); mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS; -- GitLab