WorkManager.getInstance() is deprecated

Pass context where available
This commit is contained in:
Hamidreza Bayat 2020-04-10 20:42:55 +04:30
parent 4959319d58
commit fff0dce83e
7 changed files with 26 additions and 24 deletions

View File

@ -148,7 +148,7 @@ public class NetworkPreferencesFragment extends PreferenceFragmentCompat {
timePickerDialog.show();
});
builder.setNeutralButton(R.string.pref_autoUpdateIntervallOrTime_Disable, (dialog, which) -> {
UserPreferences.disableAutoUpdate();
UserPreferences.disableAutoUpdate(context);
setUpdateIntervalText();
});
builder.show();

View File

@ -24,7 +24,7 @@ public class PreferenceUpgrader {
int newVersion = BuildConfig.VERSION_CODE;
if (oldVersion != newVersion) {
AutoUpdateManager.restartUpdateAlarm();
AutoUpdateManager.restartUpdateAlarm(context);
upgrade(oldVersion);
upgraderPrefs.edit().putInt(PREF_CONFIGURED_VERSION, newVersion).apply();

View File

@ -695,7 +695,7 @@ public class UserPreferences {
.apply();
// when updating with an interval, we assume the user wants
// to update *now* and then every 'hours' interval thereafter.
AutoUpdateManager.restartUpdateAlarm();
AutoUpdateManager.restartUpdateAlarm(context);
}
/**
@ -705,14 +705,14 @@ public class UserPreferences {
prefs.edit()
.putString(PREF_UPDATE_INTERVAL, hourOfDay + ":" + minute)
.apply();
AutoUpdateManager.restartUpdateAlarm();
AutoUpdateManager.restartUpdateAlarm(context);
}
public static void disableAutoUpdate() {
public static void disableAutoUpdate(Context context) {
prefs.edit()
.putString(PREF_UPDATE_INTERVAL, "0")
.apply();
AutoUpdateManager.disableAutoUpdate();
AutoUpdateManager.disableAutoUpdate(context);
}
public static boolean gpodnetNotificationsEnabled() {

View File

@ -20,7 +20,7 @@ public class FeedUpdateReceiver extends BroadcastReceiver {
Log.d(TAG, "Received intent");
ClientConfig.initialize(context);
AutoUpdateManager.runOnce();
AutoUpdateManager.runOnce(context);
}
}

View File

@ -39,7 +39,7 @@ public class FeedUpdateWorker extends Worker {
if (!isRunOnce && UserPreferences.isAutoUpdateTimeOfDay()) {
// WorkManager does not allow to set specific time for repeated tasks.
// We repeatedly schedule a OneTimeWorkRequest instead.
AutoUpdateManager.restartUpdateAlarm();
AutoUpdateManager.restartUpdateAlarm(getApplicationContext());
}
return Result.success();

View File

@ -168,7 +168,7 @@ public class SyncService extends Worker {
public static void sync(Context context) {
OneTimeWorkRequest workRequest = getWorkRequest().build();
WorkManager.getInstance().enqueueUniqueWork(WORK_ID_SYNC, ExistingWorkPolicy.REPLACE, workRequest);
WorkManager.getInstance(context).enqueueUniqueWork(WORK_ID_SYNC, ExistingWorkPolicy.REPLACE, workRequest);
EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_started));
}
@ -176,7 +176,7 @@ public class SyncService extends Worker {
OneTimeWorkRequest workRequest = getWorkRequest()
.setInitialDelay(0L, TimeUnit.SECONDS)
.build();
WorkManager.getInstance().enqueueUniqueWork(WORK_ID_SYNC, ExistingWorkPolicy.REPLACE, workRequest);
WorkManager.getInstance(context).enqueueUniqueWork(WORK_ID_SYNC, ExistingWorkPolicy.REPLACE, workRequest);
EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_started));
}
@ -192,7 +192,7 @@ public class SyncService extends Worker {
.setInitialDelay(0L, TimeUnit.SECONDS)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.MINUTES)
.build();
WorkManager.getInstance().enqueueUniqueWork(WORK_ID_SYNC, ExistingWorkPolicy.REPLACE, workRequest);
WorkManager.getInstance(context).enqueueUniqueWork(WORK_ID_SYNC, ExistingWorkPolicy.REPLACE, workRequest);
EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_started));
}

View File

@ -32,24 +32,25 @@ public class AutoUpdateManager {
/**
* Start / restart periodic auto feed refresh
* @param context
*/
public static void restartUpdateAlarm() {
public static void restartUpdateAlarm(Context context) {
if (UserPreferences.isAutoUpdateDisabled()) {
disableAutoUpdate();
disableAutoUpdate(context);
} else if (UserPreferences.isAutoUpdateTimeOfDay()) {
int[] timeOfDay = UserPreferences.getUpdateTimeOfDay();
Log.d(TAG, "timeOfDay: " + Arrays.toString(timeOfDay));
restartUpdateTimeOfDayAlarm(timeOfDay[0], timeOfDay[1]);
restartUpdateTimeOfDayAlarm(timeOfDay[0], timeOfDay[1], context);
} else {
long milliseconds = UserPreferences.getUpdateInterval();
restartUpdateIntervalAlarm(milliseconds);
restartUpdateIntervalAlarm(milliseconds, context);
}
}
/**
* Sets the interval in which the feeds are refreshed automatically
*/
private static void restartUpdateIntervalAlarm(long intervalMillis) {
private static void restartUpdateIntervalAlarm(long intervalMillis, Context context) {
Log.d(TAG, "Restarting update alarm.");
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(FeedUpdateWorker.class,
@ -57,14 +58,14 @@ public class AutoUpdateManager {
.setConstraints(getConstraints())
.build();
WorkManager.getInstance().enqueueUniquePeriodicWork(
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
WORK_ID_FEED_UPDATE, ExistingPeriodicWorkPolicy.REPLACE, workRequest);
}
/**
* Sets time of day the feeds are refreshed automatically
*/
private static void restartUpdateTimeOfDayAlarm(int hoursOfDay, int minute) {
private static void restartUpdateTimeOfDayAlarm(int hoursOfDay, int minute, Context context) {
Log.d(TAG, "Restarting update alarm.");
Calendar now = Calendar.getInstance();
@ -81,7 +82,7 @@ public class AutoUpdateManager {
.setInitialDelay(triggerAtMillis, TimeUnit.MILLISECONDS)
.build();
WorkManager.getInstance().enqueueUniqueWork(WORK_ID_FEED_UPDATE, ExistingWorkPolicy.REPLACE, workRequest);
WorkManager.getInstance(context).enqueueUniqueWork(WORK_ID_FEED_UPDATE, ExistingWorkPolicy.REPLACE, workRequest);
}
/**
@ -89,8 +90,9 @@ public class AutoUpdateManager {
*
* Callers from UI should use {@link #runImmediate(Context)}, as it will guarantee
* the refresh be run immediately.
* @param context
*/
public static void runOnce() {
public static void runOnce(Context context) {
Log.d(TAG, "Run auto update once, as soon as OS allows.");
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(FeedUpdateWorker.class)
@ -102,7 +104,7 @@ public class AutoUpdateManager {
)
.build();
WorkManager.getInstance().enqueueUniqueWork(WORK_ID_FEED_UPDATE_ONCE, ExistingWorkPolicy.REPLACE, workRequest);
WorkManager.getInstance(context).enqueueUniqueWork(WORK_ID_FEED_UPDATE_ONCE, ExistingWorkPolicy.REPLACE, workRequest);
}
@ -110,7 +112,7 @@ public class AutoUpdateManager {
/**
* Run auto feed refresh once in background immediately, using its own thread.
*
* Callers where the additional threads is not suitable should use {@link #runOnce()}
* Callers where the additional threads is not suitable should use {@link #runOnce(Context)}
*/
public static void runImmediate(@NonNull Context context) {
Log.d(TAG, "Run auto update immediately in background.");
@ -119,8 +121,8 @@ public class AutoUpdateManager {
}, "ManualRefreshAllFeeds").start();
}
public static void disableAutoUpdate() {
WorkManager.getInstance().cancelUniqueWork(WORK_ID_FEED_UPDATE);
public static void disableAutoUpdate(Context context) {
WorkManager.getInstance(context).cancelUniqueWork(WORK_ID_FEED_UPDATE);
}
private static Constraints getConstraints() {