Loading app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java +33 −16 Original line number Diff line number Diff line Loading @@ -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<Context> mWeakContext; private Location mGpsLocation; private Location mNetworkLocation; private long mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS; private static WeatherUpdater mInstance = null; Loading @@ -56,26 +57,29 @@ public class WeatherUpdater { 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 || mForceRequestPeriodInMs == Preferences.weatherRefreshIntervalInMs(context); 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."); Log.w(TAG, "Cannot force weather update too frequently. Period is " + mForceRequestPeriodInMs + "ms."); } return isRequestAllowed; Loading @@ -86,15 +90,24 @@ 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; long requestPeriod = Preferences.weatherRefreshIntervalInMs(mWeakContext.get()); if (mForceRequestPeriodInMs > requestPeriod) { mForceRequestPeriodInMs = requestPeriod; } } @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; } Loading @@ -106,22 +119,25 @@ 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)); } Loading @@ -139,10 +155,10 @@ public class WeatherUpdater { mNetworkLocation = location; } requestWeatherUpdate(mWeakContext.get(), getMostRecentLocation()); requestWeatherUpdate(getMostRecentLocation()); } protected static void notifyUi(@NonNull Context context, @Nullable WeatherInfo weatherInfo, int status) { protected void notifyUi(@NonNull Context context, @Nullable WeatherInfo weatherInfo, int status) { if (weatherInfo == null) { Log.i(TAG, "WeatherInfo is null. Status reported: " + status); Loading @@ -155,6 +171,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() { Loading Loading
app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java +33 −16 Original line number Diff line number Diff line Loading @@ -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<Context> mWeakContext; private Location mGpsLocation; private Location mNetworkLocation; private long mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS; private static WeatherUpdater mInstance = null; Loading @@ -56,26 +57,29 @@ public class WeatherUpdater { 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 || mForceRequestPeriodInMs == Preferences.weatherRefreshIntervalInMs(context); 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."); Log.w(TAG, "Cannot force weather update too frequently. Period is " + mForceRequestPeriodInMs + "ms."); } return isRequestAllowed; Loading @@ -86,15 +90,24 @@ 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; long requestPeriod = Preferences.weatherRefreshIntervalInMs(mWeakContext.get()); if (mForceRequestPeriodInMs > requestPeriod) { mForceRequestPeriodInMs = requestPeriod; } } @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; } Loading @@ -106,22 +119,25 @@ 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)); } Loading @@ -139,10 +155,10 @@ public class WeatherUpdater { mNetworkLocation = location; } requestWeatherUpdate(mWeakContext.get(), getMostRecentLocation()); requestWeatherUpdate(getMostRecentLocation()); } protected static void notifyUi(@NonNull Context context, @Nullable WeatherInfo weatherInfo, int status) { protected void notifyUi(@NonNull Context context, @Nullable WeatherInfo weatherInfo, int status) { if (weatherInfo == null) { Log.i(TAG, "WeatherInfo is null. Status reported: " + status); Loading @@ -155,6 +171,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() { Loading