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_INCOMPAT_VERSIONS = false;
private static final boolean DEFAULT_SHOW_ROOT_APPS = true; private static final boolean DEFAULT_SHOW_ROOT_APPS = true;
private static final boolean DEFAULT_SHOW_ANTI_FEATURE_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_PRIVILEGED_INSTALLER = true;
//private static final boolean DEFAULT_LOCAL_REPO_BONJOUR = true; //private static final boolean DEFAULT_LOCAL_REPO_BONJOUR = true;
private static final long DEFAULT_KEEP_CACHE_TIME = TimeUnit.DAYS.toMillis(1); 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"; 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 * 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. * is changed, or c) on startup, in case we get upgraded.
*/ */
public static void schedule(Context ctx) { public static void schedule(Context ctx) {
int interval = Preferences.get().getUpdateInterval();
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(ctx);
String sint = prefs.getString(Preferences.PREF_UPD_INTERVAL, "0");
int interval = Integer.parseInt(sint);
Intent intent = new Intent(ctx, UpdateService.class); Intent intent = new Intent(ctx, UpdateService.class);
PendingIntent pending = PendingIntent.getService(ctx, 0, intent, 0); 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. * @return True if we are due for a scheduled update.
*/ */
private boolean verifyIsTimeForScheduledRun() { private boolean verifyIsTimeForScheduledRun() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); int interval = Preferences.get().getUpdateInterval();
String sint = prefs.getString(Preferences.PREF_UPD_INTERVAL, "0");
int interval = Integer.parseInt(sint);
if (interval == 0) { if (interval == 0) {
Log.i(TAG, "Skipping update - disabled"); Log.i(TAG, "Skipping update - disabled");
return false; return false;
} }
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
long lastUpdate = prefs.getLong(STATE_LAST_UPDATED, 0); long lastUpdate = prefs.getLong(STATE_LAST_UPDATED, 0);
long elapsed = System.currentTimeMillis() - lastUpdate; long elapsed = System.currentTimeMillis() - lastUpdate;
if (elapsed < interval * 60 * 60 * 1000) { if (elapsed < interval * 60 * 60 * 1000) {