move "Update Interval" pref handling to Preferences class

Basically all of the settings are handled in the Preferences class now,
this was an outlier.
This commit is contained in:
Hans-Christoph Steiner 2016-11-10 16:04:46 +01:00
parent 552da24d30
commit 5f3e952958
2 changed files with 14 additions and 8 deletions

View File

@ -75,6 +75,7 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
private static final boolean DEFAULT_SHOW_INCOMPAT_VERSIONS = false;
private static final boolean DEFAULT_SHOW_ROOT_APPS = true;
private static final boolean DEFAULT_SHOW_ANTI_FEATURE_APPS = true;
private static final int DEFAULT_UPD_INTERVAL = 24;
private static final boolean DEFAULT_PRIVILEGED_INSTALLER = true;
//private static final boolean DEFAULT_LOCAL_REPO_BONJOUR = true;
private static final long DEFAULT_KEEP_CACHE_TIME = TimeUnit.DAYS.toMillis(1);
@ -157,6 +158,16 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
*/
private static final String PREF_CACHE_APK = "cacheDownloaded";
public int getUpdateInterval() {
try {
String value = preferences.getString(PREF_UPD_INTERVAL,
String.valueOf(DEFAULT_UPD_INTERVAL));
return Integer.parseInt(value);
} catch (NumberFormatException e) {
return DEFAULT_UPD_INTERVAL;
}
}
/**
* Time in millis to keep cached files. Anything that has been around longer will be deleted
*/

View File

@ -121,11 +121,7 @@ public class UpdateService extends IntentService {
* is changed, or c) on startup, in case we get upgraded.
*/
public static void schedule(Context ctx) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(ctx);
String sint = prefs.getString(Preferences.PREF_UPD_INTERVAL, "0");
int interval = Integer.parseInt(sint);
int interval = Preferences.get().getUpdateInterval();
Intent intent = new Intent(ctx, UpdateService.class);
PendingIntent pending = PendingIntent.getService(ctx, 0, intent, 0);
@ -290,13 +286,12 @@ public class UpdateService extends IntentService {
* @return True if we are due for a scheduled update.
*/
private boolean verifyIsTimeForScheduledRun() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String sint = prefs.getString(Preferences.PREF_UPD_INTERVAL, "0");
int interval = Integer.parseInt(sint);
int interval = Preferences.get().getUpdateInterval();
if (interval == 0) {
Log.i(TAG, "Skipping update - disabled");
return false;
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
long lastUpdate = prefs.getLong(STATE_LAST_UPDATED, 0);
long elapsed = System.currentTimeMillis() - lastUpdate;
if (elapsed < interval * 60 * 60 * 1000) {