diff --git a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherInfoView.java b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherInfoView.java index adf4d32a8fdcab8446b150822ba63e71009a19fe..54d8927ca184c5b6aba2000ba2df407e8344538a 100644 --- a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherInfoView.java +++ b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherInfoView.java @@ -62,7 +62,7 @@ public class WeatherInfoView extends LinearLayout { }); findViewById(R.id.weather_setting_imageview).setOnClickListener(v -> startWeatherPreferences()); findViewById(R.id.weather_refresh_imageview).setOnClickListener(v -> { - WeatherUpdater.getInstance(getContext().getApplicationContext()).forceWeatherRequest(); + WeatherUpdater.getInstance(getContext().getApplicationContext()).updateWeather(); }); } diff --git a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdateService.java b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdateService.java index 500d2f034a5d4a926a2c7cd02aaaeb49c74c917d..5668135c8a5fc2bb88b638b85bbba0ab7b3b5f35 100644 --- a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdateService.java +++ b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdateService.java @@ -42,7 +42,7 @@ public class WeatherUpdateService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null && ACTION_FORCE_UPDATE.equals(intent.getAction())) { - WeatherUpdater.getInstance(this).forceWeatherRequest(); + WeatherUpdater.getInstance(this).updateWeather(); } return START_STICKY; 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 31266e0af88693aed314332ac70eb8bb9100b1b4..1f68471460009226a31de478d173579d60487cd6 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 @@ -44,13 +44,10 @@ import timber.log.Timber; public class WeatherUpdater { private static final String TAG = "WeatherUpdater"; - private static final long DEFAULT_FORCE_REQUEST_PERIOD_IN_MS = 10L * 1000L; private final WeakReference mWeakContext; - private long mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS; - private long mLastWeatherUpdateTimeStamp = 0; - private long mForceRequestLastTry = 0; + private long mLastWeatherUpdateInMs = 0; private static WeatherUpdater mInstance = null; @@ -68,36 +65,18 @@ public class WeatherUpdater { public void checkWeatherRequest() { Context context = mWeakContext.get(); - long refreshPeriod = Preferences.weatherRefreshIntervalInMs(context); - long elapsedTime = Math.abs(SystemClock.elapsedRealtime() - mLastWeatherUpdateTimeStamp); + long refreshPeriodInMs = Preferences.weatherRefreshIntervalInMs(context); + long systemTimeInMs = SystemClock.elapsedRealtime(); + long elapsedTimeInMs = Math.abs(systemTimeInMs - mLastWeatherUpdateInMs); - boolean isPeriodicRequestAllowed = refreshPeriod != 0 && elapsedTime >= refreshPeriod; + boolean isPeriodicRequestAllowed = refreshPeriodInMs != 0 && elapsedTimeInMs >= refreshPeriodInMs; if (isPeriodicRequestAllowed) { - forceWeatherRequest(); - } - } - - public void forceWeatherRequest() { - if (canForceWeatherRequest()) { updateWeather(); - increaseForceRequestPeriod(); + mLastWeatherUpdateInMs = systemTimeInMs; } } - private boolean canForceWeatherRequest() { - - long systemTime = SystemClock.elapsedRealtime(); - long elapsedTime = Math.abs(systemTime - mForceRequestLastTry); - boolean isRequestAllowed = elapsedTime >= mForceRequestPeriodInMs; - - if (isRequestAllowed) { - mForceRequestLastTry = systemTime; - } - - return isRequestAllowed; - } - - private void updateWeather() { + public void updateWeather() { Timber.tag(TAG).i("Updating weather"); Context context = mWeakContext.get(); @@ -108,14 +87,6 @@ public class WeatherUpdater { } } - private void increaseForceRequestPeriod() { - mForceRequestPeriodInMs = mForceRequestPeriodInMs * 2; - - if (mForceRequestPeriodInMs > Preferences.weatherRefreshIntervalInMs(mWeakContext.get())) { - mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS; - } - } - @SuppressLint("MissingPermission") private void fetchNewLocation() { if (hasMissingPermissions()) { @@ -190,10 +161,8 @@ public class WeatherUpdater { long now = SystemClock.elapsedRealtime(); Preferences.setCachedWeatherInfo(context, now, weatherInfo); - mLastWeatherUpdateTimeStamp = now; Intent updateIntent = new Intent(WeatherUpdateService.ACTION_UPDATE_FINISHED); LocalBroadcastManager.getInstance(context).sendBroadcast(updateIntent); - mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS; } private void reverseGeocodeLocation(@NonNull Location location) {