From e22d2ca39bd740b336008b1a52aa99028294ef6b Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Wed, 16 Nov 2022 10:11:26 +0100 Subject: [PATCH 1/2] Establish a period between 2 forcing of weather updates We want to limit the number of weather request forcing because the system or the user could flood the system with weather requests. --- .../e/blisslauncher/core/Preferences.java | 8 +++++++ .../e/blisslauncher/core/utils/Constants.java | 2 +- .../features/weather/WeatherUpdater.java | 22 ++++++++++++++++--- 3 files changed, 28 insertions(+), 4 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 409beca403..91c17ebece 100644 --- a/app/src/main/java/foundation/e/blisslauncher/core/Preferences.java +++ b/app/src/main/java/foundation/e/blisslauncher/core/Preferences.java @@ -244,6 +244,14 @@ public class Preferences { 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/core/utils/Constants.java b/app/src/main/java/foundation/e/blisslauncher/core/utils/Constants.java index 926f8a9bb5..c0f0655409 100755 --- a/app/src/main/java/foundation/e/blisslauncher/core/utils/Constants.java +++ b/app/src/main/java/foundation/e/blisslauncher/core/utils/Constants.java @@ -36,7 +36,7 @@ public class Constants { // other shared pref entries public static final String WEATHER_LAST_UPDATE = "last_weather_update"; - public static final String WEATHER_LAST_TRY = "last_weather_try"; + public static final String FORCE_WEATHER_LAST_TRY = "last_weather_try"; public static final String WEATHER_DATA = "weather_data"; // First run is used to hide the initial no-weather message for a better OOBE 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 cfbe275515..31a0b273d7 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 @@ -27,6 +27,8 @@ import lineageos.weather.WeatherLocation; public class WeatherUpdater { private static final String TAG = "WeatherUpdater"; + private static final long FORCE_REQUEST_PERIOD_IN_MS = 60000L; + private final LocationManager mLocationManager; private final WeakReference mWeakContext; private Location mGpsLocation; @@ -61,13 +63,27 @@ public class WeatherUpdater { } public void forceWeatherRequest() { - Log.i(TAG, "Forcing weather request"); - updateWeather(); + if (canForceWeatherRequest()) { + updateWeather(); + } + } + + private boolean canForceWeatherRequest() { + Context context = mWeakContext.get(); + long elapsedTime = Math.abs(SystemClock.elapsedRealtime() - Preferences.getForceRequestLastTry(context)); + boolean isRequestAllowed = elapsedTime >= FORCE_REQUEST_PERIOD_IN_MS; + if (isRequestAllowed) { + Preferences.setForceRequestLastTry(context, SystemClock.elapsedRealtime()); + } else { + Log.w(TAG, "Cannot force weather update too frequently. Period is " + FORCE_REQUEST_PERIOD_IN_MS + "ms."); + } + + return isRequestAllowed; } private void updateWeather() { + Log.i(TAG, "Updating weather"); Context context = mWeakContext.get(); - Preferences.setLastWeatherUpdateTimestamp(context, SystemClock.elapsedRealtime()); if (Preferences.useCustomWeatherLocation(context)) { requestCustomWeatherUpdate(context, Preferences.getCustomWeatherLocation(context)); -- GitLab From 5673e4a8f675b7ad6705d3e87251626c63aca3e8 Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Wed, 16 Nov 2022 14:20:43 +0100 Subject: [PATCH 2/2] Increase weather force request period linearly --- .../features/weather/WeatherUpdater.java | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) 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 31a0b273d7..ba0e02b6b7 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 @@ -27,12 +27,13 @@ import lineageos.weather.WeatherLocation; public class WeatherUpdater { private static final String TAG = "WeatherUpdater"; - private static final long FORCE_REQUEST_PERIOD_IN_MS = 60000L; + private static final long DEFAULT_FORCE_REQUEST_PERIOD_IN_MS = 60000L; private final LocationManager mLocationManager; private final WeakReference mWeakContext; private Location mGpsLocation; private Location mNetworkLocation; + private long mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS; private static WeatherUpdater mInstance = null; @@ -54,28 +55,28 @@ public class WeatherUpdater { long refreshPeriod = Preferences.weatherRefreshIntervalInMs(context); long elapsedTime = Math.abs(SystemClock.elapsedRealtime() - Preferences.lastWeatherUpdateTimestamp(context)); - Log.i(TAG, "elapsedTime=" + elapsedTime + " vs refreshPeriod=" + refreshPeriod); - - boolean isRequestAllowed = refreshPeriod != 0 && elapsedTime >= refreshPeriod; - if (isRequestAllowed) { - updateWeather(); + boolean isPeriodicRequestAllowed = refreshPeriod != 0 && elapsedTime >= refreshPeriod; + if (isPeriodicRequestAllowed) { + forceWeatherRequest(); } } public void forceWeatherRequest() { if (canForceWeatherRequest()) { updateWeather(); + increaseForceRequestPeriod(); } } private boolean canForceWeatherRequest() { Context context = mWeakContext.get(); + long elapsedTime = Math.abs(SystemClock.elapsedRealtime() - Preferences.getForceRequestLastTry(context)); - boolean isRequestAllowed = elapsedTime >= FORCE_REQUEST_PERIOD_IN_MS; + + boolean isRequestAllowed = elapsedTime >= mForceRequestPeriodInMs; + if (isRequestAllowed) { Preferences.setForceRequestLastTry(context, SystemClock.elapsedRealtime()); - } else { - Log.w(TAG, "Cannot force weather update too frequently. Period is " + FORCE_REQUEST_PERIOD_IN_MS + "ms."); } return isRequestAllowed; @@ -86,15 +87,23 @@ public class WeatherUpdater { Context context = mWeakContext.get(); if (Preferences.useCustomWeatherLocation(context)) { - requestCustomWeatherUpdate(context, Preferences.getCustomWeatherLocation(context)); + requestCustomWeatherUpdate(Preferences.getCustomWeatherLocation(context)); } else { - fetchNewLocation(context); + fetchNewLocation(); + } + } + + private void increaseForceRequestPeriod() { + mForceRequestPeriodInMs = mForceRequestPeriodInMs * 2; + + if (mForceRequestPeriodInMs > Preferences.weatherRefreshIntervalInMs(mWeakContext.get())) { + mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS; } } @SuppressLint("MissingPermission") - private void fetchNewLocation(@NonNull Context context) { - if (hasMissingPermissions(context)) { + private void fetchNewLocation() { + if (hasMissingPermissions()) { Log.e(TAG, "Could not fetch location for missing permission"); return; } @@ -106,28 +115,32 @@ public class WeatherUpdater { Executors.newFixedThreadPool(1), this::onNewLocationFetched); } - protected static boolean hasMissingPermissions(@NonNull Context context) { + protected boolean hasMissingPermissions() { + Context context = mWeakContext.get(); + return ActivityCompat.checkSelfPermission(context, permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED; } - protected static void requestWeatherUpdate(@NonNull Context context, @NonNull Location location) { - + protected void requestWeatherUpdate(@NonNull Location location) { Log.i(TAG, "Requesting weather info for location: " + location); + Context context = mWeakContext.get(); LineageWeatherManager weatherManager = LineageWeatherManager.getInstance(context); weatherManager.requestWeatherUpdate(location, (status, weatherInfo) -> notifyUi(context, weatherInfo, status)); } - protected static void requestCustomWeatherUpdate(@NonNull Context context, @Nullable WeatherLocation location) { + protected void requestCustomWeatherUpdate(@Nullable WeatherLocation location) { Log.i(TAG, "Requesting weather info for location: " + location); + Context context = mWeakContext.get(); LineageWeatherManager weatherManager = LineageWeatherManager.getInstance(context); weatherManager.requestWeatherUpdate(location, (status, weatherInfo) -> notifyUi(context, weatherInfo, status)); } private synchronized void onNewLocationFetched(@Nullable Location location) { if (location == null) { + Log.w(TAG, "Could not fetch any location"); return; } @@ -139,10 +152,10 @@ public class WeatherUpdater { mNetworkLocation = location; } - requestWeatherUpdate(mWeakContext.get(), getMostRecentLocation()); + requestWeatherUpdate(getMostRecentLocation()); } - protected static void notifyUi(@NonNull Context context, @Nullable WeatherInfo weatherInfo, int status) { + private void notifyUi(@NonNull Context context, @Nullable WeatherInfo weatherInfo, int status) { if (weatherInfo == null) { Log.i(TAG, "WeatherInfo is null. Status reported: " + status); @@ -155,6 +168,7 @@ public class WeatherUpdater { Preferences.setLastWeatherUpdateTimestamp(context, now); Intent updateIntent = new Intent(WeatherUpdateService.ACTION_UPDATE_FINISHED); LocalBroadcastManager.getInstance(context).sendBroadcast(updateIntent); + mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS; } private Location getMostRecentLocation() { -- GitLab