Bug 1251362 - Part 14 - Migrate (customised) home panel configurations
MozReview-Commit-ID: GDgUkoGpg6t
--- 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 = 5;
+ private static final int VERSION = 6;
// 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.
@@ -207,16 +207,82 @@ public class HomeConfigPrefsBackend impl
} else {
newArray.put(jsonPanels.get(i));
}
}
return newArray;
}
+ /**
+ * Updates the panels to merge the Recent Tabs panel into the (Combined) History panel.
+ *
+ * Uses the position of the Combined History Panel if it's visible, or falls back to the Recent Tabs
+ * panel if that one is visible. That way, we minimize panel reordering during a migration.
+ * @param context Android context
+ * @param jsonPanels array of original JSON panels
+ * @return new array of updated JSON panels
+ * @throws JSONException
+ */
+ private static JSONArray mergeRecentTabsIntoCombinedHistoryPanel(Context context, JSONArray jsonPanels) throws JSONException {
+ EnumSet<PanelConfig.Flags> historyFlags = null;
+ EnumSet<PanelConfig.Flags> recentTabsFlags = null;
+
+ int historyIndex = -1;
+ int recentTabsIndex = -1;
+
+ // Determine state and location of Combined History and Recent Tabs panels.
+ for (int i = 0; i < jsonPanels.length(); i++) {
+ JSONObject panelObj = jsonPanels.getJSONObject(i);
+ final PanelConfig panelConfig = new PanelConfig(panelObj);
+ final PanelType type = panelConfig.getType();
+ if (type == PanelType.COMBINED_HISTORY) {
+ historyIndex = i;
+ historyFlags = panelConfig.getFlags();
+ } else if (type == PanelType.RECENT_TABS) {
+ recentTabsIndex = i;
+ recentTabsFlags = panelConfig.getFlags();
+ }
+ }
+
+ if (historyIndex == -1 || recentTabsIndex == -1) {
+ throw new IllegalArgumentException("Expected both Combined History and Recent Tabs panels to be present prior to Combined History phase 2.");
+ }
+
+ PanelConfig newPanel;
+ int replaceIndex;
+ int removeIndex;
+
+ if (historyFlags.contains(PanelConfig.Flags.DISABLED_PANEL) && !recentTabsFlags.contains(PanelConfig.Flags.DISABLED_PANEL)) {
+ // Replace the Recent Tabs panel if it's visible and the Combined History panel is disabled.
+ replaceIndex = recentTabsIndex;
+ removeIndex = historyIndex;
+ newPanel = createBuiltinPanelConfig(context, PanelType.COMBINED_HISTORY, recentTabsFlags);
+ } else {
+ // Otherwise, just keep the Combined History panel and remove the Recent Tabs panel.
+ replaceIndex = -1;
+ removeIndex = recentTabsIndex;
+ newPanel = null;
+ }
+
+ // Copy the array with updated panel and removed panel.
+ final JSONArray newArray = new JSONArray();
+ for (int i = 0; i < jsonPanels.length(); i++) {
+ if (i == replaceIndex) {
+ newArray.put(newPanel.toJSON());
+ } else if (i == removeIndex) {
+ continue;
+ } else {
+ newArray.put(jsonPanels.get(i));
+ }
+ }
+
+ return newArray;
+ }
+
private static void ensureDefaultPanelForV5(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;
}
@@ -334,16 +400,20 @@ public class HomeConfigPrefsBackend impl
// 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.
ensureDefaultPanelForV5(context, jsonPanels);
break;
+
+ case 6:
+ // Merge the Recent Tabs panel into the Combined History panel.
+ jsonPanels = mergeRecentTabsIntoCombinedHistoryPanel(context, jsonPanels);
}
}
// 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);