put stray preference handling into Preferences

This moves a few stray preference handling instances into Preferences, and
move the non-preference "lastUpdateCheck" state to local only to
UpdateService.  This will still work with existing installs since the
String constant value is the same.
This commit is contained in:
Hans-Christoph Steiner 2016-04-06 12:07:57 +02:00
parent 721d4a300a
commit 77e041d640
2 changed files with 15 additions and 9 deletions

View File

@ -54,7 +54,6 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
public static final String PREF_CACHE_APK = "cacheDownloaded"; public static final String PREF_CACHE_APK = "cacheDownloaded";
public static final String PREF_UNSTABLE_UPDATES = "unstableUpdates"; public static final String PREF_UNSTABLE_UPDATES = "unstableUpdates";
public static final String PREF_EXPERT = "expert"; public static final String PREF_EXPERT = "expert";
public static final String PREF_UPD_LAST = "lastUpdateCheck";
public static final String PREF_PRIVILEGED_INSTALLER = "privilegedInstaller"; public static final String PREF_PRIVILEGED_INSTALLER = "privilegedInstaller";
public static final String PREF_UNINSTALL_PRIVILEGED_APP = "uninstallPrivilegedApp"; public static final String PREF_UNINSTALL_PRIVILEGED_APP = "uninstallPrivilegedApp";
public static final String PREF_LOCAL_REPO_NAME = "localRepoName"; public static final String PREF_LOCAL_REPO_NAME = "localRepoName";
@ -179,6 +178,14 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
return preferences.getString(PREF_LOCAL_REPO_NAME, getDefaultLocalRepoName()); return preferences.getString(PREF_LOCAL_REPO_NAME, getDefaultLocalRepoName());
} }
public boolean isUpdateNotificationEnabled() {
return preferences.getBoolean(PREF_UPD_NOTIFY, true);
}
public boolean isUpdateOnlyOnWifi() {
return preferences.getBoolean(PREF_UPD_WIFI_ONLY, false);
}
/** /**
* This preference's default is set dynamically based on whether Orbot is * This preference's default is set dynamically based on whether Orbot is
* installed. If Orbot is installed, default to using Tor, the user can still override * installed. If Orbot is installed, default to using Tor, the user can still override

View File

@ -73,6 +73,8 @@ public class UpdateService extends IntentService implements ProgressListener {
public static final int STATUS_ERROR_LOCAL_SMALL = 4; public static final int STATUS_ERROR_LOCAL_SMALL = 4;
public static final int STATUS_INFO = 5; public static final int STATUS_INFO = 5;
private static final String STATE_LAST_UPDATED = "lastUpdateCheck";
private LocalBroadcastManager localBroadcastManager; private LocalBroadcastManager localBroadcastManager;
private static final int NOTIFY_ID_UPDATING = 0; private static final int NOTIFY_ID_UPDATING = 0;
@ -295,7 +297,7 @@ public class UpdateService extends IntentService implements ProgressListener {
Log.i(TAG, "Skipping update - disabled"); Log.i(TAG, "Skipping update - disabled");
return false; return false;
} }
long lastUpdate = prefs.getLong(Preferences.PREF_UPD_LAST, 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) {
Log.i(TAG, "Skipping update - done " + elapsed Log.i(TAG, "Skipping update - done " + elapsed
@ -318,9 +320,7 @@ public class UpdateService extends IntentService implements ProgressListener {
return false; return false;
} }
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); if (activeNetwork.getType() != ConnectivityManager.TYPE_WIFI && Preferences.get().isUpdateOnlyOnWifi()) {
if (activeNetwork.getType() != ConnectivityManager.TYPE_WIFI
&& prefs.getBoolean(Preferences.PREF_UPD_WIFI_ONLY, false)) {
Log.i(TAG, "Skipping update - wifi not available"); Log.i(TAG, "Skipping update - wifi not available");
return false; return false;
} }
@ -343,8 +343,6 @@ public class UpdateService extends IntentService implements ProgressListener {
return; return;
} }
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
// Grab some preliminary information, then we can release the // Grab some preliminary information, then we can release the
// database while we do all the downloading, etc... // database while we do all the downloading, etc...
List<Repo> repos = RepoProvider.Helper.all(this); List<Repo> repos = RepoProvider.Helper.all(this);
@ -395,13 +393,14 @@ public class UpdateService extends IntentService implements ProgressListener {
} else { } else {
notifyContentProviders(); notifyContentProviders();
if (prefs.getBoolean(Preferences.PREF_UPD_NOTIFY, true)) { if (Preferences.get().isUpdateNotificationEnabled()) {
performUpdateNotification(); performUpdateNotification();
} }
} }
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor e = prefs.edit(); SharedPreferences.Editor e = prefs.edit();
e.putLong(Preferences.PREF_UPD_LAST, System.currentTimeMillis()); e.putLong(STATE_LAST_UPDATED, System.currentTimeMillis());
PreferencesCompat.apply(e); PreferencesCompat.apply(e);
if (errorRepos == 0) { if (errorRepos == 0) {