Bug 1259078 - Remove reading list panel from HomePanels r?liuche draft
authorAndrzej Hunt <ahunt@mozilla.com>
Wed, 04 May 2016 15:35:50 -0700
changeset 363512 61f2295b8df4c2ad0c50ab933e811fd07819c611
parent 363437 2f4723efdf3aa222b2cdcd76daf17b5816c2f406
child 363513 b4d801ba18213bb10a5e426dc228f30db4c0b362
push id17225
push userahunt@mozilla.com
push dateWed, 04 May 2016 22:36:49 +0000
reviewersliuche
bugs1259078
milestone49.0a1
Bug 1259078 - Remove reading list panel from HomePanels r?liuche MozReview-Commit-ID: C09O3sOmpxq
mobile/android/base/java/org/mozilla/gecko/home/HomeConfig.java
mobile/android/base/java/org/mozilla/gecko/home/HomeConfigPrefsBackend.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/AboutHomeTest.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/components/AboutHomeComponent.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testAboutHomePageNavigation.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/HomeConfig.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/HomeConfig.java
@@ -38,23 +38,23 @@ public final class HomeConfig {
      * to a default set of built-in panels. The DYNAMIC panel type is used by
      * third-party services to create panels with varying types of content.
      */
     @RobocopTarget
     public static enum PanelType implements Parcelable {
         TOP_SITES("top_sites", TopSitesPanel.class),
         BOOKMARKS("bookmarks", BookmarksPanel.class),
         COMBINED_HISTORY("combined_history", CombinedHistoryPanel.class),
-        READING_LIST("reading_list", ReadingListPanel.class),
         RECENT_TABS("recent_tabs", RecentTabsPanel.class),
         DYNAMIC("dynamic", DynamicPanel.class),
         // Deprecated panels that should no longer exist but are kept around for
         // migration code. Class references have been replaced with new version of the panel.
         DEPRECATED_REMOTE_TABS("remote_tabs", CombinedHistoryPanel.class),
-        DEPRECATED_HISTORY("history", CombinedHistoryPanel.class);
+        DEPRECATED_HISTORY("history", CombinedHistoryPanel.class),
+        DEPRECATED_READING_LIST("reading_list", BookmarksPanel.class);
 
         private final String mId;
         private final Class<?> mPanelClass;
 
         PanelType(String id, Class<?> panelClass) {
             mId = id;
             mPanelClass = panelClass;
         }
@@ -1593,21 +1593,21 @@ public final class HomeConfig {
     // UUIDs used to create PanelConfigs for default built-in panels. These are
     // public because they can be used in "about:home?panel=UUID" query strings
     // to open specific panels without querying the active Home Panel
     // configuration. Because they don't consider the active configuration, it
     // is only sensible to do this for built-in panels (and not for dynamic
     // panels).
     private static final String TOP_SITES_PANEL_ID = "4becc86b-41eb-429a-a042-88fe8b5a094e";
     private static final String BOOKMARKS_PANEL_ID = "7f6d419a-cd6c-4e34-b26f-f68b1b551907";
-    private static final String READING_LIST_PANEL_ID = "20f4549a-64ad-4c32-93e4-1dcef792733b";
     private static final String HISTORY_PANEL_ID = "f134bf20-11f7-4867-ab8b-e8e705d7fbe8";
     private static final String COMBINED_HISTORY_PANEL_ID = "4d716ce2-e063-486d-9e7c-b190d7b04dc6";
     private static final String RECENT_TABS_PANEL_ID = "5c2601a5-eedc-4477-b297-ce4cef52adf8";
     private static final String REMOTE_TABS_PANEL_ID = "72429afd-8d8b-43d8-9189-14b779c563d0";
+    private static final String DEPRECATED_READING_LIST_PANEL_ID = "20f4549a-64ad-4c32-93e4-1dcef792733b";
 
     private final HomeConfigBackend mBackend;
 
     public HomeConfig(HomeConfigBackend backend) {
         mBackend = backend;
     }
 
     public State load() {
@@ -1634,26 +1634,24 @@ public final class HomeConfig {
     }
 
     public static int getTitleResourceIdForBuiltinPanelType(PanelType panelType) {
         switch (panelType) {
         case TOP_SITES:
             return R.string.home_top_sites_title;
 
         case BOOKMARKS:
+        case DEPRECATED_READING_LIST:
             return R.string.bookmarks_title;
 
         case DEPRECATED_HISTORY:
         case DEPRECATED_REMOTE_TABS:
         case COMBINED_HISTORY:
             return R.string.home_history_title;
 
-        case READING_LIST:
-            return R.string.reading_list_title;
-
         case RECENT_TABS:
             return R.string.recent_tabs_title;
 
         default:
             throw new IllegalArgumentException("Only for built-in panel types: " + panelType);
         }
     }
 
@@ -1669,18 +1667,18 @@ public final class HomeConfig {
             return HISTORY_PANEL_ID;
 
         case COMBINED_HISTORY:
             return COMBINED_HISTORY_PANEL_ID;
 
         case DEPRECATED_REMOTE_TABS:
             return REMOTE_TABS_PANEL_ID;
 
-        case READING_LIST:
-            return READING_LIST_PANEL_ID;
+        case DEPRECATED_READING_LIST:
+            return DEPRECATED_READING_LIST_PANEL_ID;
 
         case RECENT_TABS:
             return RECENT_TABS_PANEL_ID;
 
         default:
             throw new IllegalArgumentException("Only for built-in panel types: " + panelType);
         }
     }
--- 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.
@@ -71,17 +71,16 @@ public class HomeConfigPrefsBackend impl
         panelConfigs.add(createBuiltinPanelConfig(mContext, PanelType.TOP_SITES,
                                                   EnumSet.of(PanelConfig.Flags.DEFAULT_PANEL)));
 
         panelConfigs.add(createBuiltinPanelConfig(mContext, PanelType.BOOKMARKS));
         panelConfigs.add(createBuiltinPanelConfig(mContext, PanelType.COMBINED_HISTORY));
 
 
         panelConfigs.add(createBuiltinPanelConfig(mContext, PanelType.RECENT_TABS));
-        panelConfigs.add(createBuiltinPanelConfig(mContext, PanelType.READING_LIST));
 
         return new State(panelConfigs, true);
     }
 
     /**
      * Iterate through the panels to check if they are all disabled.
      */
     private static boolean allPanelsAreDisabled(JSONArray jsonPanels) throws JSONException {
@@ -229,29 +228,71 @@ public class HomeConfigPrefsBackend impl
         }
 
         // 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());
     }
 
     /**
+     * Remove the reading list panel.
+     * If the reading list panel used to be the default panel, we make bookmarks the new default.
+     */
+    private static JSONArray removeReadingListPanel(Context context, JSONArray jsonPanels) throws JSONException {
+        boolean wasDefault = false;
+        int bookmarksIndex = -1;
+
+        // JSONArrary doesn't provide remove() for API < 19, therefore we need to manually copy all
+        // the items we don't want deleted into a new array.
+        final JSONArray newJSONPanels = new JSONArray();
+
+        for (int i = 0; i < jsonPanels.length(); i++) {
+            final JSONObject panelJSON = jsonPanels.getJSONObject(i);
+            final PanelConfig panelConfig = new PanelConfig(panelJSON);
+
+            if (panelConfig.getType() == PanelType.DEPRECATED_READING_LIST) {
+                // If this panel was the default we'll need to assign a new default:
+                wasDefault = panelConfig.isDefault();
+            } else {
+                if (panelConfig.getType() == PanelType.BOOKMARKS) {
+                    bookmarksIndex = newJSONPanels.length();
+                }
+
+                newJSONPanels.put(panelJSON);
+            }
+        }
+
+        if (wasDefault) {
+            // This will make the bookmarks panel visible if it was previously hidden - this is desired
+            // since this will make the new equivalent of the reading list visible by default.
+            final JSONObject bookmarksPanelConfig = createBuiltinPanelConfig(context, PanelType.BOOKMARKS, EnumSet.of(PanelConfig.Flags.DEFAULT_PANEL)).toJSON();
+            if (bookmarksIndex != -1) {
+                newJSONPanels.put(bookmarksIndex, bookmarksPanelConfig);
+            } else {
+                newJSONPanels.put(bookmarksPanelConfig);
+            }
+        }
+
+        return newJSONPanels;
+    }
+
+    /**
      * 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) {
         final int count = jsonPanels.length();
         for (int i = 0; i < count; i++) {
             try {
                 final JSONObject jsonPanelConfig = jsonPanels.getJSONObject(i);
                 final PanelConfig panelConfig = new PanelConfig(jsonPanelConfig);
-                if (panelConfig.getType() == PanelType.READING_LIST) {
+                if (panelConfig.getType() == PanelType.DEPRECATED_READING_LIST) {
                     return true;
                 }
             } catch (Exception e) {
                 // It's okay to ignore this exception, since an invalid reading list
                 // panel config is equivalent to no reading list panel.
                 Log.e(LOGTAG, "Exception loading PanelConfig from JSON", e);
             }
         }
@@ -318,34 +359,38 @@ public class HomeConfigPrefsBackend impl
                     break;
 
                 case 3:
                     // Add the "Reading List" panel if it does not exist. At one time,
                     // the Reading List panel was shown only to devices that were not
                     // considered "low memory". Now, we expose the panel to all devices.
                     // This migration should only occur for "low memory" devices.
                     // Note: This will not agree with the default configuration, which
-                    // has DEPRECATED_REMOTE_TABS after READING_LIST on some devices.
+                    // has DEPRECATED_REMOTE_TABS after DEPRECATED_READING_LIST on some devices.
                     if (!readingListPanelExists(jsonPanels)) {
                         addBuiltinPanelConfig(context, jsonPanels,
-                                PanelType.READING_LIST, Position.BACK, Position.BACK);
+                                PanelType.DEPRECATED_READING_LIST, Position.BACK, Position.BACK);
                     }
                     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.
                     ensureDefaultPanelForV5(context, jsonPanels);
                     break;
+
+                case 6:
+                    jsonPanels = removeReadingListPanel(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);
 
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/AboutHomeTest.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/AboutHomeTest.java
@@ -28,23 +28,21 @@ import com.robotium.solo.Condition;
  * To use any of these methods in your test make sure it extends AboutHomeTest instead of BaseTest
  */
 abstract class AboutHomeTest extends PixelTest {
     protected enum AboutHomeTabs {
         RECENT_TABS,
         HISTORY,
         TOP_SITES,
         BOOKMARKS,
-        READING_LIST
     };
 
     private final ArrayList<String> aboutHomeTabs = new ArrayList<String>() {{
                   add("TOP_SITES");
                   add("BOOKMARKS");
-                  add("READING_LIST");
               }};
 
 
     @Override
     public void setUp() throws Exception {
         super.setUp();
 
         if (aboutHomeTabs.size() < 4) {
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/components/AboutHomeComponent.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/components/AboutHomeComponent.java
@@ -31,17 +31,16 @@ import com.robotium.solo.Solo;
 public class AboutHomeComponent extends BaseComponent {
     private static final String LOGTAG = AboutHomeComponent.class.getSimpleName();
 
     private static final List<PanelType> PANEL_ORDERING = Arrays.asList(
             PanelType.TOP_SITES,
             PanelType.BOOKMARKS,
             PanelType.COMBINED_HISTORY,
             PanelType.RECENT_TABS,
-            PanelType.READING_LIST
     );
 
     // The percentage of the panel to swipe between 0 and 1. This value was set through
     // testing: 0.55f was tested on try and fails on armv6 devices.
     private static final float SWIPE_PERCENTAGE = 0.70f;
 
     public AboutHomeComponent(final UITestContext testContext) {
         super(testContext);
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testAboutHomePageNavigation.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testAboutHomePageNavigation.java
@@ -1,15 +1,14 @@
 /* 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.tests;
 
-import org.mozilla.gecko.home.HomeConfig;
 import org.mozilla.gecko.home.HomeConfig.PanelType;
 import org.mozilla.gecko.tests.helpers.DeviceHelper;
 import org.mozilla.gecko.tests.helpers.GeckoHelper;
 
 /**
  * Tests functionality related to navigating between the various about:home panels.
  *
  * TODO: Update this test to account for recent tabs panel (bug 1028727).
@@ -22,19 +21,16 @@ public class testAboutHomePageNavigation
         GeckoHelper.blockForDelayedStartup();
 
         mAboutHome.assertVisible()
                   .assertCurrentPanel(PanelType.TOP_SITES);
 
         mAboutHome.swipeToPanelOnRight();
         mAboutHome.assertCurrentPanel(PanelType.BOOKMARKS);
 
-        mAboutHome.swipeToPanelOnRight();
-        mAboutHome.assertCurrentPanel(PanelType.READING_LIST);
-
         // Ideally these helpers would just be their own tests. However, by keeping this within
         // one method, we're saving test setUp and tearDown resources.
         if (DeviceHelper.isTablet()) {
             helperTestTablet();
         } else {
             helperTestPhone();
         }
     }
@@ -43,34 +39,28 @@ public class testAboutHomePageNavigation
         mAboutHome.swipeToPanelOnRight();
         mAboutHome.assertCurrentPanel(PanelType.COMBINED_HISTORY);
 
         // Edge case.
         mAboutHome.swipeToPanelOnRight();
         mAboutHome.assertCurrentPanel(PanelType.COMBINED_HISTORY);
 
         mAboutHome.swipeToPanelOnLeft();
-        mAboutHome.assertCurrentPanel(PanelType.READING_LIST);
-
-        mAboutHome.swipeToPanelOnLeft();
         mAboutHome.assertCurrentPanel(PanelType.BOOKMARKS);
 
         mAboutHome.swipeToPanelOnLeft();
         mAboutHome.assertCurrentPanel(PanelType.TOP_SITES);
 
         // Edge case.
         mAboutHome.swipeToPanelOnLeft();
         mAboutHome.assertCurrentPanel(PanelType.TOP_SITES);
     }
 
     private void helperTestPhone() {
         // Edge case.
-        mAboutHome.swipeToPanelOnRight();
-        mAboutHome.assertCurrentPanel(PanelType.READING_LIST);
-
         mAboutHome.swipeToPanelOnLeft();
         mAboutHome.assertCurrentPanel(PanelType.BOOKMARKS);
 
         mAboutHome.swipeToPanelOnLeft();
         mAboutHome.assertCurrentPanel(PanelType.TOP_SITES);
 
         mAboutHome.swipeToPanelOnLeft();
         mAboutHome.assertCurrentPanel(PanelType.COMBINED_HISTORY);