Bug 1288127 - Allows disabling smartfolders mode in CombinedHistoryAdapter r?sebastian draft
authorAndrzej Hunt <ahunt@mozilla.com>
Thu, 28 Jul 2016 10:06:18 -0700
changeset 399342 141ccc00ae58c9192e44e226476a13e7c68a231b
parent 399341 b7d69c24507d86cd7e64af0feff35221d221bb60
child 399343 158a8c8ad6241382310711dbacc07bf5342f9fee
push id25805
push userahunt@mozilla.com
push dateWed, 10 Aug 2016 22:18:08 +0000
reviewerssebastian
bugs1288127
milestone51.0a1
Bug 1288127 - Allows disabling smartfolders mode in CombinedHistoryAdapter r?sebastian We want to generalise the CombinedHistoryPanel to allow showing a list of chronoligically sorted and separated bookmarks. The smartfolders aren't relevant to bookmarks, hence we need to be able to disable them. MozReview-Commit-ID: 5iLabqiC198
mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryAdapter.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryAdapter.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryAdapter.java
@@ -38,21 +38,32 @@ public class CombinedHistoryAdapter exte
     }
 
     private Cursor historyCursor;
     private DevicesUpdateHandler devicesUpdateHandler;
     private int deviceCount = 0;
     private RecentTabsUpdateHandler recentTabsUpdateHandler;
     private int recentTabsCount = 0;
 
+    private final boolean disableSmartfolders;
+
     // We use a sparse array to store each section header's position in the panel [more cheaply than a HashMap].
     private final SparseArray<SectionHeader> sectionHeaders;
 
-    public CombinedHistoryAdapter(Resources resources) {
+    private int getNumSmartFolders() {
+        if (disableSmartfolders) {
+            return 0;
+        } else {
+            return CombinedHistoryPanel.NUM_SMART_FOLDERS;
+        }
+    }
+
+    public CombinedHistoryAdapter(Resources resources, boolean disableSmartfolders) {
         super();
+        this.disableSmartfolders = disableSmartfolders;
         sectionHeaders = new SparseArray<>();
         HistorySectionsHelper.updateRecentSectionOffset(resources, sectionDateRangeArray);
         this.setHasStableIds(true);
     }
 
     public void setHistory(Cursor history) {
         historyCursor = history;
         populateSectionHeaders(historyCursor, sectionHeaders);
@@ -155,45 +166,47 @@ public class CombinedHistoryAdapter exte
      * @param type ItemType of the item
      * @param position position in the adapter
      * @return position of the item in the data structure
      */
     private int transformAdapterPositionForDataStructure(CombinedHistoryItem.ItemType type, int position) {
         if (type == CombinedHistoryItem.ItemType.SECTION_HEADER) {
             return position;
         } else if (type == CombinedHistoryItem.ItemType.HISTORY) {
-            return position - getHeadersBefore(position) - CombinedHistoryPanel.NUM_SMART_FOLDERS;
+            return position - getHeadersBefore(position) - getNumSmartFolders();
         } else {
             return position;
         }
     }
 
     private CombinedHistoryItem.ItemType getItemTypeForPosition(int position) {
-        if (position == RECENT_TABS_SMARTFOLDER_INDEX) {
-            return CombinedHistoryItem.ItemType.RECENT_TABS;
-        }
-        if (position == SYNCED_DEVICES_SMARTFOLDER_INDEX) {
-            return CombinedHistoryItem.ItemType.SYNCED_DEVICES;
+        if (!disableSmartfolders) {
+            if (position == RECENT_TABS_SMARTFOLDER_INDEX) {
+                return CombinedHistoryItem.ItemType.RECENT_TABS;
+            }
+            if (position == SYNCED_DEVICES_SMARTFOLDER_INDEX) {
+                return CombinedHistoryItem.ItemType.SYNCED_DEVICES;
+            }
         }
         final int sectionPosition = transformAdapterPositionForDataStructure(CombinedHistoryItem.ItemType.SECTION_HEADER, position);
         if (sectionHeaders.get(sectionPosition) != null) {
             return CombinedHistoryItem.ItemType.SECTION_HEADER;
         }
         return CombinedHistoryItem.ItemType.HISTORY;
     }
 
     @Override
     public int getItemViewType(int position) {
         return CombinedHistoryItem.ItemType.itemTypeToViewType(getItemTypeForPosition(position));
     }
 
     @Override
     public int getItemCount() {
         final int historySize = historyCursor == null ? 0 : historyCursor.getCount();
-        return historySize + sectionHeaders.size() + CombinedHistoryPanel.NUM_SMART_FOLDERS;
+        return historySize + sectionHeaders.size() + getNumSmartFolders();
     }
 
     /**
      * Returns stable ID for each position. Data behind historyCursor is a sorted Combined view.
      *
      * @param position view item position for which to generate a stable ID
      * @return stable ID for given position
      */
@@ -238,33 +251,33 @@ public class CombinedHistoryAdapter exte
     }
 
     /**
      * Add only the SectionHeaders that have history items within their range to a SparseArray, where the
      * array index is the position of the header in the history-only (no clients) ordering.
      * @param c data Cursor
      * @param sparseArray SparseArray to populate
      */
-    private static void populateSectionHeaders(Cursor c, SparseArray<SectionHeader> sparseArray) {
+    private void populateSectionHeaders(Cursor c, SparseArray<SectionHeader> sparseArray) {
         sparseArray.clear();
 
         if (c == null || !c.moveToFirst()) {
             return;
         }
 
         SectionHeader section = null;
 
         do {
             final int historyPosition = c.getPosition();
             final long visitTime = c.getLong(c.getColumnIndexOrThrow(BrowserContract.History.DATE_LAST_VISITED));
             final SectionHeader itemSection = getSectionFromTime(visitTime);
 
             if (section != itemSection) {
                 section = itemSection;
-                sparseArray.append(historyPosition + sparseArray.size() + CombinedHistoryPanel.NUM_SMART_FOLDERS, section);
+                sparseArray.append(historyPosition + sparseArray.size() + getNumSmartFolders(), section);
             }
 
             if (section == SectionHeader.OLDER_THAN_SIX_MONTHS) {
                 break;
             }
         } while (c.moveToNext());
     }