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 409beca40304bbdca5460b5d5dd617bdb84817ad..91c17ebece0937c397bcf419db5acfc742fc5c6b 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 926f8a9bb519db1429fbecfcc67f1b8b67eee825..c0f06554091206724a099972697f20e19f52fd9e 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 cfbe275515753153a3222b87dbfdbaeac6d7686c..ba0e02b6b758a11d0c81da7a169252d1b4859077 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,10 +27,13 @@ import lineageos.weather.WeatherLocation; public class WeatherUpdater { private static final String TAG = "WeatherUpdater"; + 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; @@ -52,33 +55,55 @@ 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 isPeriodicRequestAllowed = refreshPeriod != 0 && elapsedTime >= refreshPeriod; + if (isPeriodicRequestAllowed) { + forceWeatherRequest(); + } + } - boolean isRequestAllowed = refreshPeriod != 0 && elapsedTime >= refreshPeriod; - if (isRequestAllowed) { + public void forceWeatherRequest() { + if (canForceWeatherRequest()) { updateWeather(); + increaseForceRequestPeriod(); } } - public void forceWeatherRequest() { - Log.i(TAG, "Forcing weather request"); - updateWeather(); + private boolean canForceWeatherRequest() { + Context context = mWeakContext.get(); + + long elapsedTime = Math.abs(SystemClock.elapsedRealtime() - Preferences.getForceRequestLastTry(context)); + + boolean isRequestAllowed = elapsedTime >= mForceRequestPeriodInMs; + + if (isRequestAllowed) { + Preferences.setForceRequestLastTry(context, SystemClock.elapsedRealtime()); + } + + 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)); + 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; } @@ -90,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; } @@ -123,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); @@ -139,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() {