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

Commit 9480aead authored by Jonathan Klee's avatar Jonathan Klee
Browse files

Force weather update when button is pressed

parent 98b085fa
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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();
        });
    }

+1 −1
Original line number Diff line number Diff line
@@ -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;
+4 −37
Original line number Diff line number Diff line
@@ -47,10 +47,8 @@ public class WeatherUpdater {
    private static final long DEFAULT_FORCE_REQUEST_PERIOD_IN_MS = 10L * 1000L;

    private final WeakReference<Context> mWeakContext;
    private long mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS;

    private long mLastWeatherUpdateTimeStamp = 0;
    private long mForceRequestLastTry = 0;

    private static WeatherUpdater mInstance = null;

@@ -69,38 +67,17 @@ public class WeatherUpdater {
    public void checkWeatherRequest() {
        Context context = mWeakContext.get();
        long refreshPeriod = Preferences.weatherRefreshIntervalInMs(context);
        long elapsedTime = Math.abs(SystemClock.elapsedRealtime() - mLastWeatherUpdateTimeStamp);
        long systemTimeInMs = SystemClock.elapsedRealtime();
        long elapsedTime = Math.abs(systemTimeInMs - mLastWeatherUpdateTimeStamp);

        boolean isPeriodicRequestAllowed = refreshPeriod != 0 && elapsedTime >= refreshPeriod;
        if (isPeriodicRequestAllowed) {
            forceWeatherRequest();
        }
    }

    public void forceWeatherRequest() {
        if (canForceWeatherRequest()) {
            updateWeather();
            increaseForceRequestPeriod();
        }
    }

    private boolean canForceWeatherRequest() {

        long systemTime = SystemClock.elapsedRealtime();
        long elapsedTime = Math.abs(systemTime - mForceRequestLastTry);
        boolean isRequestAllowed = elapsedTime >= mForceRequestPeriodInMs;

        if (isRequestAllowed) {
            mForceRequestLastTry = systemTime;
        } else {
            Timber.tag(TAG).i("Could not force weather request. mForceRequestPeriodInMs=" + mForceRequestPeriodInMs
                    + " elapsedTime=" + elapsedTime);
            mLastWeatherUpdateTimeStamp = systemTimeInMs;
        }

        return isRequestAllowed;
    }

    private void updateWeather() {
    public void updateWeather() {
        Timber.tag(TAG).i("Updating weather");
        Context context = mWeakContext.get();

@@ -111,14 +88,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()) {
@@ -193,10 +162,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) {