Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 62cce612 authored by Jonathan Klee's avatar Jonathan Klee
Browse files

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.
parent 4e767f76
Loading
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -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);

+1 −1
Original line number Diff line number Diff line
@@ -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
+20 −3
Original line number Diff line number Diff line
@@ -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<Context> mWeakContext;
    private Location mGpsLocation;
@@ -61,13 +63,28 @@ public class WeatherUpdater {
    }

    public void forceWeatherRequest() {
        Log.i(TAG, "Forcing weather request");
        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));