Bug 1264136 - NPE in TabMenuStripLayout:onPageSelected while closing last tab or opening new tab. r=sebastian draft
authorChenxia Liu <liuche@mozilla.com>
Wed, 13 Apr 2016 11:29:15 -0700
changeset 350587 52bd81ad7e8be03f889ca6bbbbbc024ed33c7d26
parent 350586 f3ff455f8a397e85691cb5c8aa3f7f88ac5317f3
child 518368 b698b961bd4850b5dbb2dccf3adb50aee6fc4662
push id15378
push usercliu@mozilla.com
push dateThu, 14 Apr 2016 01:48:47 +0000
reviewerssebastian
bugs1264136
milestone48.0a1
Bug 1264136 - NPE in TabMenuStripLayout:onPageSelected while closing last tab or opening new tab. r=sebastian MozReview-Commit-ID: 59gB7bGi6oh
mobile/android/base/java/org/mozilla/gecko/home/HomeConfigPrefsBackend.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/HomeConfigPrefsBackend.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/HomeConfigPrefsBackend.java
@@ -30,17 +30,17 @@ import android.content.SharedPreferences
 import android.support.v4.content.LocalBroadcastManager;
 import android.text.TextUtils;
 import android.util.Log;
 
 public class HomeConfigPrefsBackend implements HomeConfigBackend {
     private static final String LOGTAG = "GeckoHomeConfigBackend";
 
     // Increment this to trigger a migration.
-    private static final int VERSION = 4;
+    private static final int VERSION = 5;
 
     // This key was originally used to store only an array of panel configs.
     public static final String PREFS_CONFIG_KEY_OLD = "home_panels";
 
     // This key is now used to store a version number with the array of panel configs.
     public static final String PREFS_CONFIG_KEY = "home_panels_with_version";
 
     // Keys used with JSON object stored in prefs.
@@ -181,16 +181,17 @@ public class HomeConfigPrefsBackend impl
 
         if (historyIndex == -1 || syncIndex == -1) {
             throw new IllegalArgumentException("Expected both History and Sync panels to be present prior to Combined History.");
         }
 
         PanelConfig newPanel;
         int replaceIndex;
         int removeIndex;
+
         if (historyFlags.contains(PanelConfig.Flags.DISABLED_PANEL) && !syncFlags.contains(PanelConfig.Flags.DISABLED_PANEL)) {
             // Replace the Sync panel if it's visible and the History panel is disabled.
             replaceIndex = syncIndex;
             removeIndex = historyIndex;
             newPanel = createBuiltinPanelConfig(context, PanelType.COMBINED_HISTORY, syncFlags);
         } else {
             // Otherwise, just replace the History panel.
             replaceIndex = historyIndex;
@@ -208,16 +209,36 @@ public class HomeConfigPrefsBackend impl
             } else {
                 newArray.put(jsonPanels.get(i));
             }
         }
 
         return newArray;
     }
 
+    private static JSONArray ensureDefaultPanel(Context context, JSONArray jsonPanels) throws JSONException {
+        int historyIndex = -1;
+
+        for (int i = 0; i < jsonPanels.length(); i++) {
+            final PanelConfig panelConfig = new PanelConfig(jsonPanels.getJSONObject(i));
+            if (panelConfig.isDefault()) {
+                return jsonPanels;
+            }
+
+            if (panelConfig.getType() == PanelType.COMBINED_HISTORY) {
+                historyIndex = i;
+            }
+        }
+
+        // Make the History panel default. We can't modify existing PanelConfigs, so make a new one.
+        final PanelConfig historyPanelConfig = createBuiltinPanelConfig(context, PanelType.COMBINED_HISTORY, EnumSet.of(PanelConfig.Flags.DEFAULT_PANEL));
+        jsonPanels.put(historyIndex, historyPanelConfig.toJSON());
+        return jsonPanels;
+    }
+
     /**
      * Checks to see if the reading list panel already exists.
      *
      * @param jsonPanels JSONArray array representing the curent set of panel configs.
      *
      * @return boolean Whether or not the reading list panel exists.
      */
     private static boolean readingListPanelExists(JSONArray jsonPanels) {
@@ -311,16 +332,21 @@ public class HomeConfigPrefsBackend impl
                     break;
 
                 case 4:
                     // Combine the History and Sync panels. In order to minimize an unexpected reordering
                     // of panels, we try to replace the History panel if it's visible, and fall back to
                     // the Sync panel if that's visible.
                     jsonPanels = combineHistoryAndSyncPanels(context, jsonPanels);
                     break;
+
+                case 5:
+                    // This is the fix for bug 1264136 where we lost track of the default panel during some migrations.
+                    jsonPanels = ensureDefaultPanel(context, jsonPanels);
+                    break;
             }
         }
 
         // Save the new panel config and the new version number.
         final JSONObject newJson = new JSONObject();
         newJson.put(JSON_KEY_PANELS, jsonPanels);
         newJson.put(JSON_KEY_VERSION, VERSION);