Bug 799632 - Add a preference for controlling automatic update downloads on Android r=bnicholson

This commit is contained in:
James Willcox 2012-10-15 09:56:42 -04:00
parent 0d6866c6ab
commit 474de51db5
9 changed files with 94 additions and 7 deletions

View File

@ -493,6 +493,10 @@ pref("browser.search.param.yahoo-fr-ja", "mozff");
pref("app.update.timerFirstInterval", 30000); // milliseconds
pref("app.update.timerMinimumDelay", 30); // seconds
// used by update service to decide whether or not to
// automatically download an update
pref("app.update.autodownload", "wifi");
#ifdef MOZ_UPDATER
/* prefs used specifically for updating the app */
pref("app.update.enabled", false);

View File

@ -1732,7 +1732,11 @@ abstract public class GeckoApp
(TextSelectionHandle) findViewById(R.id.end_handle),
GeckoAppShell.getEventDispatcher());
UpdateServiceHelper.registerForUpdates(this);
PrefsHelper.getPref("app.update.autodownload", new PrefsHelper.PrefHandlerBase() {
@Override public void prefValue(String pref, String value) {
UpdateServiceHelper.registerForUpdates(GeckoApp.this, value);
}
});
final GeckoApp self = this;

View File

@ -54,6 +54,7 @@ public class GeckoPreferences
public static String PREFS_MP_ENABLED = "privacy.masterpassword.enabled";
public static String PREFS_MENU_CHAR_ENCODING = "browser.menu.showCharacterEncoding";
public static String PREFS_ANNOUNCEMENTS_ENABLED = NON_PREF_PREFIX + "privacy.announcements.enabled";
public static String PREFS_UPDATER_AUTODOWNLOAD = "app.update.autodownload";
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -210,6 +211,8 @@ public class GeckoPreferences
// Send a broadcast intent to the product announcements service, either to start or
// to stop the repeated background checks.
broadcastAnnouncementsPref(GeckoApp.mAppContext, ((Boolean) newValue).booleanValue());
} else if (prefName != null && prefName.equals(PREFS_UPDATER_AUTODOWNLOAD)) {
org.mozilla.gecko.updater.UpdateServiceHelper.registerForUpdates(GeckoApp.mAppContext, (String)newValue);
}
if (!TextUtils.isEmpty(prefName)) {

View File

@ -76,6 +76,7 @@ public class UpdateService extends IntentService {
private static final String KEY_LAST_HASH_FUNCTION = "UpdateService.lastHashFunction";
private static final String KEY_LAST_HASH_VALUE = "UpdateService.lastHashValue";
private static final String KEY_LAST_ATTEMPT_DATE = "UpdateService.lastAttemptDate";
private static final String KEY_AUTODOWNLOAD_POLICY = "UpdateService.autoDownloadPolicy";
private SharedPreferences mPrefs;
@ -118,6 +119,11 @@ public class UpdateService extends IntentService {
@Override
protected void onHandleIntent (Intent intent) {
if (UpdateServiceHelper.ACTION_REGISTER_FOR_UPDATES.equals(intent.getAction())) {
int policy = intent.getIntExtra(UpdateServiceHelper.EXTRA_AUTODOWNLOAD_NAME, -1);
if (policy >= 0) {
setAutoDownloadPolicy(policy);
}
registerForUpdates(false);
} else if (UpdateServiceHelper.ACTION_CHECK_FOR_UPDATE.equals(intent.getAction())) {
startUpdate(intent.getIntExtra(UpdateServiceHelper.EXTRA_UPDATE_FLAGS_NAME, 0));
@ -202,10 +208,13 @@ public class UpdateService extends IntentService {
Log.i(LOGTAG, "update available, buildID = " + info.buildID);
int connectionType = netInfo.getType();
int autoDownloadPolicy = getAutoDownloadPolicy();
if (!hasFlag(flags, UpdateServiceHelper.FLAG_FORCE_DOWNLOAD) &&
connectionType != ConnectivityManager.TYPE_WIFI &&
connectionType != ConnectivityManager.TYPE_ETHERNET) {
Log.i(LOGTAG, "not connected via wifi or ethernet");
autoDownloadPolicy != UpdateServiceHelper.AUTODOWNLOAD_ENABLED &&
(autoDownloadPolicy == UpdateServiceHelper.AUTODOWNLOAD_WIFI &&
connectionType != ConnectivityManager.TYPE_WIFI &&
connectionType != ConnectivityManager.TYPE_ETHERNET)) {
Log.i(LOGTAG, "not initiating automatic update download due to policy " + autoDownloadPolicy);
// We aren't autodownloading here, so prompt to start the update
Notification notification = new Notification(R.drawable.ic_status_logo, null, System.currentTimeMillis());
@ -522,6 +531,16 @@ public class UpdateService extends IntentService {
editor.commit();
}
private int getAutoDownloadPolicy() {
return mPrefs.getInt(KEY_AUTODOWNLOAD_POLICY, UpdateServiceHelper.AUTODOWNLOAD_WIFI);
}
private void setAutoDownloadPolicy(int policy) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putInt(KEY_AUTODOWNLOAD_POLICY, policy);
editor.commit();
}
private void saveUpdateInfo(UpdateInfo info) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(KEY_LAST_BUILDID, info.buildID);

View File

@ -34,6 +34,14 @@ public class UpdateServiceHelper {
public static final int FLAG_REINSTALL = 1 << 2;
public static final int FLAG_RETRY = 1 << 3;
// Name of the Intent extra for the autodownload policy, used with ACTION_REGISTER_FOR_UPDATES
public static final String EXTRA_AUTODOWNLOAD_NAME = "autodownload";
// Values for EXTRA_AUTODOWNLOAD_NAME
public static final int AUTODOWNLOAD_WIFI = 0;
public static final int AUTODOWNLOAD_DISABLED = 1;
public static final int AUTODOWNLOAD_ENABLED = 2;
// Name of the Intent extra that holds the flags for ACTION_CHECK_FOR_UPDATE
public static final String EXTRA_UPDATE_FLAGS_NAME = "updateFlags";
@ -92,10 +100,33 @@ public class UpdateServiceHelper {
#endif
}
public static void registerForUpdates(Context context) {
public static void registerForUpdates(Context context, String policy) {
if (policy == null)
return;
int intPolicy;
if (policy.equals("wifi")) {
intPolicy = AUTODOWNLOAD_WIFI;
} else if (policy.equals("disabled")) {
intPolicy = AUTODOWNLOAD_DISABLED;
} else if (policy.equals("enabled")) {
intPolicy = AUTODOWNLOAD_ENABLED;
} else {
Log.w(LOGTAG, "Unhandled autoupdate policy: " + policy);
return;
}
registerForUpdates(context, intPolicy);
}
// 'policy' should one of AUTODOWNLOAD_WIFI, AUTODOWNLOAD_DISABLED, AUTODOWNLOAD_ENABLED
public static void registerForUpdates(Context context, int policy) {
if (!isUpdaterEnabled())
return;
context.startService(new Intent(UpdateServiceHelper.ACTION_REGISTER_FOR_UPDATES, null, context, UpdateService.class));
Intent intent = new Intent(UpdateServiceHelper.ACTION_REGISTER_FOR_UPDATES, null, context, UpdateService.class);
intent.putExtra(EXTRA_AUTODOWNLOAD_NAME, policy);
context.startService(intent);
}
}

View File

@ -112,6 +112,11 @@ size. -->
<!ENTITY pref_private_data_offlineApps "Offline website data">
<!ENTITY pref_private_data_siteSettings "Site preferences">
<!ENTITY pref_update_autodownload "Automatic updates">
<!ENTITY pref_update_autodownload_wifi "Only over Wi-Fi">
<!ENTITY pref_update_autodownload_disabled "Disabled">
<!ENTITY pref_update_autodownload_enabled "Enabled">
<!ENTITY quit "Quit">
<!ENTITY addons "Add-ons">

View File

@ -76,4 +76,14 @@
<item>private.data.offlineApps</item>
<item>private.data.siteSettings</item>
</string-array>
<string-array name="pref_update_autodownload_entries">
<item>@string/pref_update_autodownload_wifi</item>
<item>@string/pref_update_autodownload_disabled</item>
<item>@string/pref_update_autodownload_enabled</item>
</string-array>
<string-array name="pref_update_autodownload_values">
<item>wifi</item>
<item>disabled</item>
<item>enabled</item>
</string-array>
</resources>

View File

@ -16,6 +16,14 @@
<org.mozilla.gecko.SyncPreference android:title="@string/pref_sync"
android:persistent="false" />
#ifdef MOZ_UPDATER
<ListPreference android:key="app.update.autodownload"
android:title="@string/pref_update_autodownload"
android:entries="@array/pref_update_autodownload_entries"
android:entryValues="@array/pref_update_autodownload_values"
android:persistent="false" />
#endif
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_content">
@ -109,5 +117,4 @@
android:persistent="false" />
</PreferenceCategory>
</PreferenceScreen>

View File

@ -107,6 +107,10 @@
<string name="pref_private_data_offlineApps">&pref_private_data_offlineApps;</string>
<string name="pref_private_data_siteSettings">&pref_private_data_siteSettings;</string>
<string name="pref_import_android">&pref_import_android;</string>
<string name="pref_update_autodownload">&pref_update_autodownload;</string>
<string name="pref_update_autodownload_wifi">&pref_update_autodownload_wifi;</string>
<string name="pref_update_autodownload_disabled">&pref_update_autodownload_disabled;</string>
<string name="pref_update_autodownload_enabled">&pref_update_autodownload_enabled;</string>
<string name="go">&go;</string>
<string name="search">&search;</string>