Bug 922694 - Part 7: use standalone prefs to avoid racing with startup. r=ckitching
authorRichard Newman <rnewman@mozilla.com>
Wed, 16 Oct 2013 19:25:13 -0700
changeset 165825 e9e0dfbe7a898ae1aff31ea65a8bb326232c9912
parent 165824 05ebffcae22e45e3b683d8e463af60f4b9ee1325
child 165826 6da41e59512d1a06f6c593785e9089c18b02a245
child 165991 6d4f3127e050e66c5c14b89dd248a6dd3f95af1c
push id428
push userbbajaj@mozilla.com
push dateTue, 28 Jan 2014 00:16:25 +0000
treeherdermozilla-release@cd72a7ff3a75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersckitching
bugs922694
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 922694 - Part 7: use standalone prefs to avoid racing with startup. r=ckitching
mobile/android/base/BrowserApp.java
mobile/android/base/Distribution.java
mobile/android/base/tests/testDistribution.java.in
--- a/mobile/android/base/BrowserApp.java
+++ b/mobile/android/base/BrowserApp.java
@@ -500,17 +500,17 @@ abstract public class BrowserApp extends
         registerEventListener("Feedback:LastUrl");
         registerEventListener("Feedback:OpenPlayStore");
         registerEventListener("Feedback:MaybeLater");
         registerEventListener("Telemetry:Gather");
         registerEventListener("Settings:Show");
         registerEventListener("Updater:Launch");
         registerEventListener("Reader:GoToReadingList");
 
-        Distribution.init(this, getPackageResourcePath());
+        Distribution.init(this);
         JavaAddonManager.getInstance().init(getApplicationContext());
         mSharedPreferencesHelper = new SharedPreferencesHelper(getApplicationContext());
         mOrderedBroadcastHelper = new OrderedBroadcastHelper(getApplicationContext());
         mBrowserHealthReporter = new BrowserHealthReporter();
 
         if (AppConstants.MOZ_ANDROID_BEAM && Build.VERSION.SDK_INT >= 14) {
             NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);
             if (nfc != null) {
--- a/mobile/android/base/Distribution.java
+++ b/mobile/android/base/Distribution.java
@@ -28,16 +28,18 @@ import java.util.Iterator;
 import java.util.Map;
 import java.util.Scanner;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 
 public final class Distribution {
     private static final String LOGTAG = "GeckoDistribution";
 
+    private static final String DEFAULT_PREFS = GeckoApp.PREFS_NAME;
+
     private static final int STATE_UNKNOWN = 0;
     private static final int STATE_NONE = 1;
     private static final int STATE_SET = 2;
 
     public static class DistributionDescriptor {
         public final boolean valid;
         public final String id;
         public final String version;    // Example uses a float, but that's a crazy idea.
@@ -80,74 +82,76 @@ public final class Distribution {
     }
 
     /**
      * Initializes distribution if it hasn't already been initalized. Sends
      * messages to Gecko as appropriate.
      *
      * @param packagePath where to look for the distribution directory.
      */
-    public static void init(final Context context, final String packagePath) {
+    public static void init(final Context context, final String packagePath, final String prefsPath) {
         // Read/write preferences and files on the background thread.
         ThreadUtils.postToBackgroundThread(new Runnable() {
             @Override
             public void run() {
-                Distribution dist = new Distribution(context, packagePath);
+                Distribution dist = new Distribution(context, packagePath, prefsPath);
                 boolean distributionSet = dist.doInit();
                 if (distributionSet) {
                     GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Distribution:Set", ""));
                 }
             }
         });
     }
 
     /**
      * Use <code>Context.getPackageResourcePath</code> to find an implicit
      * package path.
      */
     public static void init(final Context context) {
-        Distribution.init(context, context.getPackageResourcePath());
+        Distribution.init(context, context.getPackageResourcePath(), DEFAULT_PREFS);
     }
 
     /**
      * Returns parsed contents of bookmarks.json.
      * This method should only be called from a background thread.
      */
     public static JSONArray getBookmarks(final Context context) {
         Distribution dist = new Distribution(context);
         return dist.getBookmarks();
     }
 
+    private final Context context;
     private final String packagePath;
-    private final Context context;
+    private final String prefsBranch;
 
     private int state = STATE_UNKNOWN;
     private File distributionDir = null;
 
     /**
      * @param packagePath where to look for the distribution directory.
      */
-    public Distribution(final Context context, final String packagePath) {
+    public Distribution(final Context context, final String packagePath, final String prefsBranch) {
         this.context = context;
         this.packagePath = packagePath;
+        this.prefsBranch = prefsBranch;
     }
 
     public Distribution(final Context context) {
-        this(context, context.getPackageResourcePath());
+        this(context, context.getPackageResourcePath(), DEFAULT_PREFS);
     }
 
     /**
      * Don't call from the main thread.
      *
      * @return true if we've set a distribution.
      */
     private boolean doInit() {
         // Bail if we've already tried to initialize the distribution, and
         // there wasn't one.
-        SharedPreferences settings = context.getSharedPreferences(GeckoApp.PREFS_NAME, Activity.MODE_PRIVATE);
+        SharedPreferences settings = context.getSharedPreferences(prefsBranch, Activity.MODE_PRIVATE);
         String keyName = context.getPackageName() + ".distribution_state";
         this.state = settings.getInt(keyName, STATE_UNKNOWN);
         if (this.state == STATE_NONE) {
             return false;
         }
 
         // We've done the work once; don't do it again.
         if (this.state == STATE_SET) {
--- a/mobile/android/base/tests/testDistribution.java.in
+++ b/mobile/android/base/tests/testDistribution.java.in
@@ -1,14 +1,15 @@
 #filter substitution
 package @ANDROID_PACKAGE_NAME@.tests;
 
 import @ANDROID_PACKAGE_NAME@.*;
 
 import android.app.Activity;
+import android.content.Context;
 import android.content.SharedPreferences;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.lang.ClassLoader;
 import java.lang.reflect.Method;
 
@@ -98,21 +99,20 @@ public class testDistribution extends Co
     }
 
     // Initialize the distribution from the mock package.
     private void initDistribution(String aPackagePath) {
         try {
             // Call Distribution.init with the mock package.
             ClassLoader classLoader = mActivity.getClassLoader();
             Class distributionClass = classLoader.loadClass("org.mozilla.gecko.Distribution");
-            Class contextClass = classLoader.loadClass("android.content.Context");
-            Method init = distributionClass.getMethod("init", contextClass, String.class);
+            Method init = distributionClass.getMethod("init", Context.class, String.class, String.class);
 
             Actions.EventExpecter distributionSetExpecter = mActions.expectGeckoEvent("Distribution:Set:OK");
-            init.invoke(null, mActivity, aPackagePath);
+            init.invoke(null, mActivity, aPackagePath, "prefs-" + System.currentTimeMillis());
             distributionSetExpecter.blockForEvent();
             distributionSetExpecter.unregisterListener();
         } catch (Exception e) {
             mAsserter.ok(false, "exception initializing distribution", e.toString());
         }
     }
 
     // Test distribution and preferences values stored in preferences.json