Bug 792992 - Refactor update service. r=snorp, a=lmandel
authorEugen Sawin <esawin@mozilla.com>
Mon, 02 Feb 2015 18:35:11 +0100
changeset 250252 f9f0120c1adf
parent 250251 0e44d113855f
child 250253 ae511f0dda0f
push id4527
push userryanvm@gmail.com
push date2015-03-05 16:09 +0000
treeherdermozilla-beta@00bad6e2ffbc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp, lmandel
bugs792992
milestone37.0
Bug 792992 - Refactor update service. r=snorp, a=lmandel
mobile/android/base/GeckoApp.java
mobile/android/base/preferences/GeckoPreferences.java
mobile/android/base/updater/UpdateService.java
mobile/android/base/updater/UpdateServiceHelper.java
--- a/mobile/android/base/GeckoApp.java
+++ b/mobile/android/base/GeckoApp.java
@@ -41,17 +41,16 @@ import org.mozilla.gecko.menu.GeckoMenu;
 import org.mozilla.gecko.menu.GeckoMenuInflater;
 import org.mozilla.gecko.menu.MenuPanel;
 import org.mozilla.gecko.mozglue.ContextUtils;
 import org.mozilla.gecko.mozglue.ContextUtils.SafeIntent;
 import org.mozilla.gecko.mozglue.GeckoLoader;
 import org.mozilla.gecko.preferences.ClearOnShutdownPref;
 import org.mozilla.gecko.preferences.GeckoPreferences;
 import org.mozilla.gecko.prompts.PromptService;
-import org.mozilla.gecko.updater.UpdateService;
 import org.mozilla.gecko.updater.UpdateServiceHelper;
 import org.mozilla.gecko.util.ActivityResultHandler;
 import org.mozilla.gecko.util.ActivityUtils;
 import org.mozilla.gecko.util.EventCallback;
 import org.mozilla.gecko.util.FileUtils;
 import org.mozilla.gecko.util.GeckoEventListener;
 import org.mozilla.gecko.util.HardwareUtils;
 import org.mozilla.gecko.util.NativeEventListener;
@@ -652,26 +651,21 @@ public abstract class GeckoApp
 
         } else if ("ToggleChrome:Hide".equals(event)) {
             toggleChrome(false);
 
         } else if ("ToggleChrome:Show".equals(event)) {
             toggleChrome(true);
 
         } else if ("Update:Check".equals(event)) {
-            startService(new Intent(
-                    UpdateServiceHelper.ACTION_CHECK_FOR_UPDATE, null, this, UpdateService.class));
-
+            UpdateServiceHelper.checkForUpdate(this);
         } else if ("Update:Download".equals(event)) {
-            startService(new Intent(
-                    UpdateServiceHelper.ACTION_DOWNLOAD_UPDATE, null, this, UpdateService.class));
-
+            UpdateServiceHelper.downloadUpdate(this);
         } else if ("Update:Install".equals(event)) {
-            startService(new Intent(
-                    UpdateServiceHelper.ACTION_APPLY_UPDATE, null, this, UpdateService.class));
+            UpdateServiceHelper.applyUpdate(this);
         }
     }
 
     @Override
     public void handleMessage(String event, JSONObject message) {
         try {
             if (event.equals("Gecko:DelayedStartup")) {
                 ThreadUtils.postToBackgroundThread(new UninstallListener.DelayedStartupTask(this));
@@ -1586,21 +1580,17 @@ public abstract class GeckoApp
         mContactService = new ContactService(EventDispatcher.getInstance(), this);
 
         mPromptService = new PromptService(this);
 
         mTextSelection = new TextSelection((TextSelectionHandle) findViewById(R.id.anchor_handle),
                                            (TextSelectionHandle) findViewById(R.id.caret_handle),
                                            (TextSelectionHandle) findViewById(R.id.focus_handle));
 
-        PrefsHelper.getPref("app.update.autodownload", new PrefsHelper.PrefHandlerBase() {
-            @Override public void prefValue(String pref, String value) {
-                UpdateServiceHelper.registerForUpdates(GeckoApp.this, value);
-            }
-        });
+        UpdateServiceHelper.registerForUpdates(GeckoApp.this);
 
         // Trigger the completion of the telemetry timer that wraps activity startup,
         // then grab the duration to give to FHR.
         mJavaUiStartupTimer.stop();
         final long javaDuration = mJavaUiStartupTimer.getElapsed();
 
         ThreadUtils.getBackgroundHandler().postDelayed(new Runnable() {
             @Override
--- a/mobile/android/base/preferences/GeckoPreferences.java
+++ b/mobile/android/base/preferences/GeckoPreferences.java
@@ -33,16 +33,17 @@ import org.mozilla.gecko.PrefsHelper;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.RestrictedProfiles;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.TelemetryContract.Method;
 import org.mozilla.gecko.background.common.GlobalConstants;
 import org.mozilla.gecko.background.healthreport.HealthReportConstants;
 import org.mozilla.gecko.db.BrowserContract.SuggestedSites;
+import org.mozilla.gecko.updater.UpdateService;
 import org.mozilla.gecko.updater.UpdateServiceHelper;
 import org.mozilla.gecko.util.GeckoEventListener;
 import org.mozilla.gecko.util.HardwareUtils;
 import org.mozilla.gecko.util.ThreadUtils;
 import org.mozilla.gecko.widget.FloatingHintEditText;
 
 import android.app.ActionBar;
 import android.app.Activity;
@@ -1052,17 +1053,17 @@ OnSharedPreferenceChangeListener
             // Even though this is a list preference, we don't want to handle it
             // below, so we return here.
             return onLocaleSelected(Locales.getLanguageTag(lastLocale), (String) newValue);
         }
 
         if (PREFS_MENU_CHAR_ENCODING.equals(prefName)) {
             setCharEncodingState(((String) newValue).equals("true"));
         } else if (PREFS_UPDATER_AUTODOWNLOAD.equals(prefName)) {
-            UpdateServiceHelper.registerForUpdates(this, (String) newValue);
+            UpdateServiceHelper.setAutoDownloadPolicy(this, UpdateService.AutoDownloadPolicy.get((String) newValue));
         } else if (PREFS_HEALTHREPORT_UPLOAD_ENABLED.equals(prefName)) {
             // The healthreport pref only lives in Android, so we do not persist
             // to Gecko, but we do broadcast intent to the health report
             // background uploader service, which will start or stop the
             // repeated background upload attempts.
             broadcastHealthReportUploadPref(this, (Boolean) newValue);
         } else if (PREFS_GEO_REPORTING.equals(prefName)) {
             broadcastStumblerPref(this, (Boolean) newValue);
--- a/mobile/android/base/updater/UpdateService.java
+++ b/mobile/android/base/updater/UpdateService.java
@@ -78,16 +78,58 @@ public class UpdateService extends Inten
     private Builder mBuilder;
 
     private boolean mDownloading;
     private boolean mCancelDownload;
     private boolean mApplyImmediately;
 
     private CrashHandler mCrashHandler;
 
+    public enum AutoDownloadPolicy {
+        NONE(-1),
+        WIFI(0),
+        DISABLED(1),
+        ENABLED(2);
+
+        public final int value;
+
+        private AutoDownloadPolicy(int value) {
+            this.value = value;
+        }
+
+        private final static AutoDownloadPolicy[] sValues = AutoDownloadPolicy.values();
+
+        public static AutoDownloadPolicy get(int value) {
+            for (AutoDownloadPolicy id: sValues) {
+                if (id.value == value) {
+                    return id;
+                }
+            }
+            return NONE;
+        }
+
+        public static AutoDownloadPolicy get(String name) {
+            for (AutoDownloadPolicy id: sValues) {
+                if (name.equalsIgnoreCase(id.toString())) {
+                    return id;
+                }
+            }
+            return NONE;
+        }
+    }
+
+    private enum CheckUpdateResult {
+        // Keep these in sync with mobile/android/chrome/content/about.xhtml
+        NOT_AVAILABLE,
+        AVAILABLE,
+        DOWNLOADING,
+        DOWNLOADED
+    }
+
+
     public UpdateService() {
         super("updater");
     }
 
     @Override
     public void onCreate () {
         mCrashHandler = CrashHandler.createDefaultCrashHandler(getApplicationContext());
 
@@ -127,18 +169,21 @@ public class UpdateService extends Inten
         }
 
         return Service.START_REDELIVER_INTENT;
     }
 
     @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) {
+            AutoDownloadPolicy policy = AutoDownloadPolicy.get(
+                intent.getIntExtra(UpdateServiceHelper.EXTRA_AUTODOWNLOAD_NAME,
+                                   AutoDownloadPolicy.NONE.value));
+
+            if (policy != AutoDownloadPolicy.NONE) {
                 setAutoDownloadPolicy(policy);
             }
 
             registerForUpdates(false);
         } else if (UpdateServiceHelper.ACTION_CHECK_FOR_UPDATE.equals(intent.getAction())) {
             startUpdate(intent.getIntExtra(UpdateServiceHelper.EXTRA_UPDATE_FLAGS_NAME, 0));
             // Use this instead for forcing a download from about:fennec
             // startUpdate(UpdateServiceHelper.FLAG_FORCE_DOWNLOAD | UpdateServiceHelper.FLAG_REINSTALL);
@@ -150,17 +195,17 @@ public class UpdateService extends Inten
             applyUpdate(intent.getStringExtra(UpdateServiceHelper.EXTRA_PACKAGE_PATH_NAME));
         }
     }
 
     private static boolean hasFlag(int flags, int flag) {
         return (flags & flag) == flag;
     }
 
-    private void sendCheckUpdateResult(UpdateServiceHelper.CheckUpdateResult result) {
+    private void sendCheckUpdateResult(CheckUpdateResult result) {
         Intent resultIntent = new Intent(UpdateServiceHelper.ACTION_CHECK_UPDATE_RESULT);
         resultIntent.putExtra("result", result.toString());
         sendBroadcast(resultIntent);
     }
 
     private int getUpdateInterval(boolean isRetry) {
         int interval;
         if (isRetry) {
@@ -203,51 +248,49 @@ public class UpdateService extends Inten
 
     private void startUpdate(int flags) {
         setLastAttemptDate();
 
         NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo();
         if (netInfo == null || !netInfo.isConnected()) {
             Log.i(LOGTAG, "not connected to the network");
             registerForUpdates(true);
-            sendCheckUpdateResult(UpdateServiceHelper.CheckUpdateResult.NOT_AVAILABLE);
+            sendCheckUpdateResult(CheckUpdateResult.NOT_AVAILABLE);
             return;
         }
 
         registerForUpdates(false);
 
         UpdateInfo info = findUpdate(hasFlag(flags, UpdateServiceHelper.FLAG_REINSTALL));
         boolean haveUpdate = (info != null);
 
         if (!haveUpdate) {
             Log.i(LOGTAG, "no update available");
-            sendCheckUpdateResult(UpdateServiceHelper.CheckUpdateResult.NOT_AVAILABLE);
+            sendCheckUpdateResult(CheckUpdateResult.NOT_AVAILABLE);
             return;
         }
 
         Log.i(LOGTAG, "update available, buildID = " + info.buildID);
 
-        int autoDownloadPolicy = getAutoDownloadPolicy();
-
+        AutoDownloadPolicy policy = getAutoDownloadPolicy();
 
-        /**
-         * We only start a download automatically if one of following criteria are met:
-         *
-         * - We have a FORCE_DOWNLOAD flag passed in
-         * - The preference is set to 'always'
-         * - The preference is set to 'wifi' and we are using a non-metered network (i.e. the user
-         *   is OK with large data transfers occurring)
-         */
+        // We only start a download automatically if one of following criteria are met:
+        //
+        // - We have a FORCE_DOWNLOAD flag passed in
+        // - The preference is set to 'always'
+        // - The preference is set to 'wifi' and we are using a non-metered network (i.e. the user
+        //   is OK with large data transfers occurring)
+        //
         boolean shouldStartDownload = hasFlag(flags, UpdateServiceHelper.FLAG_FORCE_DOWNLOAD) ||
-            autoDownloadPolicy == UpdateServiceHelper.AUTODOWNLOAD_ENABLED ||
-            (autoDownloadPolicy == UpdateServiceHelper.AUTODOWNLOAD_WIFI && !ConnectivityManagerCompat.isActiveNetworkMetered(mConnectivityManager));
+            policy == AutoDownloadPolicy.ENABLED ||
+            (policy == AutoDownloadPolicy.WIFI && !ConnectivityManagerCompat.isActiveNetworkMetered(mConnectivityManager));
 
         if (!shouldStartDownload) {
-            Log.i(LOGTAG, "not initiating automatic update download due to policy " + autoDownloadPolicy);
-            sendCheckUpdateResult(UpdateServiceHelper.CheckUpdateResult.AVAILABLE);
+            Log.i(LOGTAG, "not initiating automatic update download due to policy " + policy.toString());
+            sendCheckUpdateResult(CheckUpdateResult.AVAILABLE);
 
             // We aren't autodownloading here, so prompt to start the update
             Notification notification = new Notification(R.drawable.ic_status_logo, null, System.currentTimeMillis());
 
             Intent notificationIntent = new Intent(UpdateServiceHelper.ACTION_DOWNLOAD_UPDATE);
             notificationIntent.setClass(this, UpdateService.class);
 
             PendingIntent contentIntent = PendingIntent.getService(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
@@ -259,24 +302,24 @@ public class UpdateService extends Inten
 
             mNotificationManager.notify(NOTIFICATION_ID, notification);
 
             return;
         }
 
         File pkg = downloadUpdatePackage(info, hasFlag(flags, UpdateServiceHelper.FLAG_OVERWRITE_EXISTING));
         if (pkg == null) {
-            sendCheckUpdateResult(UpdateServiceHelper.CheckUpdateResult.NOT_AVAILABLE);
+            sendCheckUpdateResult(CheckUpdateResult.NOT_AVAILABLE);
             return;
         }
 
         Log.i(LOGTAG, "have update package at " + pkg);
 
         saveUpdateInfo(info, pkg);
-        sendCheckUpdateResult(UpdateServiceHelper.CheckUpdateResult.DOWNLOADED);
+        sendCheckUpdateResult(CheckUpdateResult.DOWNLOADED);
 
         if (mApplyImmediately) {
             applyUpdate(pkg);
         } else {
             // Prompt to apply the update
             Notification notification = new Notification(R.drawable.ic_status_logo, null, System.currentTimeMillis());
 
             Intent notificationIntent = new Intent(UpdateServiceHelper.ACTION_APPLY_UPDATE);
@@ -461,17 +504,17 @@ public class UpdateService extends Inten
                 return downloadFile;
             } else {
                 // Didn't match, so we're going to download a new one.
                 downloadFile.delete();
             }
         }
 
         Log.i(LOGTAG, "downloading update package");
-        sendCheckUpdateResult(UpdateServiceHelper.CheckUpdateResult.DOWNLOADING);
+        sendCheckUpdateResult(CheckUpdateResult.DOWNLOADING);
 
         OutputStream output = null;
         InputStream input = null;
 
         mDownloading = true;
         mCancelDownload = false;
         showDownloadNotification(downloadFile);
 
@@ -620,23 +663,23 @@ public class UpdateService extends Inten
     }
 
     private void setLastAttemptDate() {
         SharedPreferences.Editor editor = mPrefs.edit();
         editor.putLong(KEY_LAST_ATTEMPT_DATE, System.currentTimeMillis());
         editor.commit();
     }
 
-    private int getAutoDownloadPolicy() {
-        return mPrefs.getInt(KEY_AUTODOWNLOAD_POLICY, UpdateServiceHelper.AUTODOWNLOAD_WIFI);
+    private AutoDownloadPolicy getAutoDownloadPolicy() {
+        return AutoDownloadPolicy.get(mPrefs.getInt(KEY_AUTODOWNLOAD_POLICY, AutoDownloadPolicy.WIFI.value));
     }
 
-    private void setAutoDownloadPolicy(int policy) {
+    private void setAutoDownloadPolicy(AutoDownloadPolicy policy) {
         SharedPreferences.Editor editor = mPrefs.edit();
-        editor.putInt(KEY_AUTODOWNLOAD_POLICY, policy);
+        editor.putInt(KEY_AUTODOWNLOAD_POLICY, policy.value);
         editor.commit();
     }
 
     private void saveUpdateInfo(UpdateInfo info, File downloaded) {
         SharedPreferences.Editor editor = mPrefs.edit();
         editor.putString(KEY_LAST_BUILDID, info.buildID);
         editor.putString(KEY_LAST_HASH_FUNCTION, info.hashFunction);
         editor.putString(KEY_LAST_HASH_VALUE, info.hashValue);
--- a/mobile/android/base/updater/UpdateServiceHelper.java
+++ b/mobile/android/base/updater/UpdateServiceHelper.java
@@ -1,88 +1,105 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.updater;
 
 import org.mozilla.gecko.AppConstants;
+import org.mozilla.gecko.PrefsHelper;
 import org.mozilla.gecko.mozglue.RobocopTarget;
 import org.mozilla.gecko.util.GeckoJarReader;
 
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.content.pm.ApplicationInfo;
 import android.os.Build;
 import android.util.Log;
 
 import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
 
 public class UpdateServiceHelper {
     public static final String ACTION_REGISTER_FOR_UPDATES = AppConstants.ANDROID_PACKAGE_NAME + ".REGISTER_FOR_UPDATES";
     public static final String ACTION_UNREGISTER_FOR_UPDATES = AppConstants.ANDROID_PACKAGE_NAME + ".UNREGISTER_FOR_UPDATES";
     public static final String ACTION_CHECK_FOR_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".CHECK_FOR_UPDATE";
     public static final String ACTION_CHECK_UPDATE_RESULT = AppConstants.ANDROID_PACKAGE_NAME + ".CHECK_UPDATE_RESULT";
     public static final String ACTION_DOWNLOAD_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".DOWNLOAD_UPDATE";
     public static final String ACTION_APPLY_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".APPLY_UPDATE";
     public static final String ACTION_CANCEL_DOWNLOAD = AppConstants.ANDROID_PACKAGE_NAME + ".CANCEL_DOWNLOAD";
 
     // Flags for ACTION_CHECK_FOR_UPDATE
-    public static final int FLAG_FORCE_DOWNLOAD = 1;
-    public static final int FLAG_OVERWRITE_EXISTING = 1 << 1;
-    public static final int FLAG_REINSTALL = 1 << 2;
-    public static final int FLAG_RETRY = 1 << 3;
+    protected static final int FLAG_FORCE_DOWNLOAD = 1;
+    protected static final int FLAG_OVERWRITE_EXISTING = 1 << 1;
+    protected static final int FLAG_REINSTALL = 1 << 2;
+    protected 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;
+    protected static final String EXTRA_AUTODOWNLOAD_NAME = "autodownload";
 
     // Name of the Intent extra that holds the flags for ACTION_CHECK_FOR_UPDATE
-    public static final String EXTRA_UPDATE_FLAGS_NAME = "updateFlags";
+    protected static final String EXTRA_UPDATE_FLAGS_NAME = "updateFlags";
 
     // Name of the Intent extra that holds the APK path, used with ACTION_APPLY_UPDATE
-    public static final String EXTRA_PACKAGE_PATH_NAME = "packagePath";
+    protected static final String EXTRA_PACKAGE_PATH_NAME = "packagePath";
 
     private static final String LOGTAG = "UpdateServiceHelper";
     private static final String DEFAULT_UPDATE_LOCALE = "en-US";
 
     private static final String UPDATE_URL;
 
     // So that updates can be disabled by tests.
     private static volatile boolean isEnabled = true;
 
+    private enum Pref {
+        AUTO_DOWNLOAD_POLICY("app.update.autodownload");
+
+        public final String name;
+
+        private Pref(String name) {
+            this.name = name;
+        }
+
+        public final static String[] names;
+
+        @Override
+        public String toString() {
+            return this.name;
+        }
+
+        static {
+            ArrayList<String> nameList = new ArrayList<String>();
+
+            for (Pref id: Pref.values()) {
+                nameList.add(id.toString());
+            }
+
+            names = nameList.toArray(new String[0]);
+        }
+    }
+
     static {
         final String pkgSpecial;
         if (AppConstants.MOZ_PKG_SPECIAL != null) {
             pkgSpecial = "-" + AppConstants.MOZ_PKG_SPECIAL;
         } else {
             pkgSpecial = "";
         }
         UPDATE_URL = "https://aus4.mozilla.org/update/4/" + AppConstants.MOZ_APP_BASENAME + "/" +
                      AppConstants.MOZ_APP_VERSION         +
                      "/%BUILDID%/Android_"                + AppConstants.MOZ_APP_ABI + pkgSpecial +
                      "/%LOCALE%/"                         + AppConstants.MOZ_UPDATE_CHANNEL +
                      "/%OS_VERSION%/default/default/"     + AppConstants.MOZILLA_VERSION +
                      "/update.xml";
     }
 
-    public enum CheckUpdateResult {
-        // Keep these in sync with mobile/android/chrome/content/about.xhtml
-        NOT_AVAILABLE,
-        AVAILABLE,
-        DOWNLOADING,
-        DOWNLOADED
-    }
-
     @RobocopTarget
     public static void setEnabled(final boolean enabled) {
         isEnabled = enabled;
     }
 
     public static URL getUpdateUrl(Context context, boolean force) {
         PackageManager pm = context.getPackageManager();
 
@@ -114,38 +131,68 @@ public class UpdateServiceHelper {
             return null;
         }
     }
 
     public static boolean isUpdaterEnabled() {
         return AppConstants.MOZ_UPDATER && isEnabled;
     }
 
-    public static void registerForUpdates(Context context, String policy) {
-        if (policy == null)
+    public static void setAutoDownloadPolicy(Context context, UpdateService.AutoDownloadPolicy policy) {
+        registerForUpdates(context, policy);
+    }
+
+    public static void checkForUpdate(Context context) {
+        if (context == 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);
+        context.startService(createIntent(context, ACTION_CHECK_FOR_UPDATE));
+    }
+
+    public static void downloadUpdate(Context context) {
+        if (context == null) {
+            return;
+        }
+
+        context.startService(createIntent(context, ACTION_DOWNLOAD_UPDATE));
+    }
+
+    public static void applyUpdate(Context context) {
+        if (context == null) {
             return;
         }
 
-        registerForUpdates(context, intPolicy);
+        context.startService(createIntent(context, ACTION_APPLY_UPDATE));
     }
 
-    // 'policy' should one of AUTODOWNLOAD_WIFI, AUTODOWNLOAD_DISABLED, AUTODOWNLOAD_ENABLED
-    public static void registerForUpdates(Context context, int policy) {
+    public static void registerForUpdates(final Context context) {
+        final HashMap<String, Object> prefs = new HashMap<String, Object>();
+
+        PrefsHelper.getPrefs(Pref.names, new PrefsHelper.PrefHandlerBase() {
+            @Override public void prefValue(String pref, String value) {
+                prefs.put(pref, value);
+            }
+
+            @Override public void finish() {
+                UpdateServiceHelper.registerForUpdates(context,
+                    UpdateService.AutoDownloadPolicy.get(
+                      (String) prefs.get(Pref.AUTO_DOWNLOAD_POLICY.toString())));
+            }
+        });
+    }
+
+    public static void registerForUpdates(Context context, UpdateService.AutoDownloadPolicy policy) {
         if (!isUpdaterEnabled())
             return;
 
-        Intent intent = new Intent(UpdateServiceHelper.ACTION_REGISTER_FOR_UPDATES, null, context, UpdateService.class);
-        intent.putExtra(EXTRA_AUTODOWNLOAD_NAME, policy);
+        Log.i(LOGTAG, "register for updates policy: " + policy.toString());
+
+        Intent intent = createIntent(context, ACTION_REGISTER_FOR_UPDATES);
+        intent.putExtra(EXTRA_AUTODOWNLOAD_NAME, policy.value);
 
         context.startService(intent);
     }
+
+    private static Intent createIntent(Context context, String action) {
+        return new Intent(action, null, context, UpdateService.class);
+    }
 }