Bug 885481: Add the folder name as an item in the BookmarksListView. [r=bnicholson]
authorSriram Ramasubramanian <sriram@mozilla.com>
Mon, 01 Jul 2013 10:59:36 -0700
changeset 143370 db454c5472bfea1d93d85e35e782448a78e8a1b4
parent 143369 ff8eb6acf9e57ad0e4aa4e4cbdf6e74adceac672
child 143371 b3b119e93c0722d32c530071d90074f0853988bf
push id25130
push userlrocha@mozilla.com
push dateWed, 21 Aug 2013 09:41:27 +0000
treeherdermozilla-central@b2486721572e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbnicholson
bugs885481
milestone24.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 885481: Add the folder name as an item in the BookmarksListView. [r=bnicholson]
mobile/android/base/home/BookmarksListView.java
--- a/mobile/android/base/home/BookmarksListView.java
+++ b/mobile/android/base/home/BookmarksListView.java
@@ -32,31 +32,22 @@ import java.util.LinkedList;
 /**
  * A ListView of bookmarks.
  */
 public class BookmarksListView extends HomeListView
                                implements AdapterView.OnItemClickListener{
     
     public static final String LOGTAG = "GeckoBookmarksListView";
 
-    private int mFolderId = Bookmarks.FIXED_ROOT_ID;
-    private String mFolderTitle = "";
-
     // A cursor based adapter.
     private BookmarksListAdapter mCursorAdapter = null;
 
     // A background task to query the db.
     private BookmarksQueryTask mQueryTask = null;
 
-    // Folder title for the currently shown list of bookmarks.
-    private BookmarkFolderView mFolderView;
-
-    // Check for adding a header view, if needed.
-    private boolean mHasFolderHeader = false;
-
     // The last motion event that was intercepted.
     private MotionEvent mMotionEvent;
 
     // The default touch slop.
     private int mTouchSlop;
 
     public BookmarksListView(Context context) {
         this(context, null);
@@ -66,31 +57,25 @@ public class BookmarksListView extends H
         this(context, attrs, android.R.attr.listViewStyle);
     }
 
     public BookmarksListView(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
 
         // Scaled touch slop for this context.
         mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
-
-        // Folder title view, is always in open state.
-        mFolderView = (BookmarkFolderView) LayoutInflater.from(context).inflate(R.layout.bookmark_folder_row, null);
-        mFolderView.open();
     }
 
     @Override
     public void onAttachedToWindow() {
         super.onAttachedToWindow();
 
         // Intialize the adapter.
         mCursorAdapter = new BookmarksListAdapter(getContext(), null);
-
-        // We need to add the header before we set the adapter, hence make it null
-        refreshListWithCursor(null);
+        setAdapter(mCursorAdapter);
 
         setOnItemClickListener(this);
         setOnKeyListener(GamepadUtils.getListItemClickDispatcher());
 
         mQueryTask = new BookmarksQueryTask();
         mQueryTask.execute();
     }
 
@@ -144,36 +129,40 @@ public class BookmarksListView extends H
         return super.onInterceptTouchEvent(event);
     }
 
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         final ListView list = (ListView) parent;
         final int headerCount = list.getHeaderViewsCount();
 
-        if (mHasFolderHeader) {
-            // If we tap on the folder view (last of the header views),
-            // move back to parent folder.
-            if (position == headerCount - 1) {   
+        if (position < headerCount) {
+            // The click is on a header, don't do anything.
+            return;
+        }
+
+        // Absolute position for the adapter.
+        position -= headerCount;
+
+        if (mCursorAdapter.isShowingChildFolder()) {
+            if (position == 0) {
+                // If we tap on an opened folder, move back to parent folder.
                 mCursorAdapter.moveToParentFolder();
                 return;
             }
-        } else if (position < headerCount) {
-            // The click is on a header, don't do anything.
-            return;
+
+            // Accounting for the folder view.
+            position--;
         }
 
         final Cursor cursor = mCursorAdapter.getCursor();
         if (cursor == null) {
             return;
         }
 
-        // Absolute position for the adapter.
-        position -= headerCount;
-
         cursor.moveToPosition(position);
 
         int type = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks.TYPE));
         if (type == Bookmarks.TYPE_FOLDER) {
             // If we're clicking on a folder, update adapter to move to that folder
             final int folderId = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID));
             final String folderTitle = mCursorAdapter.getFolderTitle(cursor);
             mCursorAdapter.moveToChildFolder(folderId, folderTitle);
@@ -183,37 +172,18 @@ public class BookmarksListView extends H
             OnUrlOpenListener listener = getOnUrlOpenListener();
             if (listener != null) {
                 listener.onUrlOpen(url);
             }
         }
     }
 
     private void refreshListWithCursor(Cursor cursor) {
-        // We need to add the header before we set the adapter, hence making it null.
-        setAdapter(null);
-
-        // Add a header view based on the root folder.
-        if (mFolderId == Bookmarks.FIXED_ROOT_ID) {
-            if (mHasFolderHeader) {
-                removeHeaderView(mFolderView);
-                mHasFolderHeader = false;
-            }
-        } else {
-            if (!mHasFolderHeader) {
-                addHeaderView(mFolderView, null, true);
-                mHasFolderHeader = true;
-            }
-
-            mFolderView.setText(mFolderTitle);
-        }
-
         // This will update the cursorAdapter to use the new one if it already exists.
         mCursorAdapter.changeCursor(cursor);
-        setAdapter(mCursorAdapter);
 
         // Reset the task.
         mQueryTask = null;
     }
 
     /**
      * Adapter to back the ListView with a list of bookmarks.
      */
@@ -229,33 +199,30 @@ public class BookmarksListView extends H
 
         public BookmarksListAdapter(Context context, Cursor cursor) {
             // Initializing with a null cursor.
             super(context, cursor);
 
             mParentStack = new LinkedList<Pair<Integer, String>>();
 
             // Add the root folder to the stack
-            Pair<Integer, String> rootFolder = new Pair<Integer, String>(mFolderId, mFolderTitle);
+            Pair<Integer, String> rootFolder = new Pair<Integer, String>(Bookmarks.FIXED_ROOT_ID, "");
             mParentStack.addFirst(rootFolder);
         }
 
         // Refresh the current folder by executing a new task.
         private void refreshCurrentFolder() {
             // Cancel any pre-existing async refresh tasks
             if (mQueryTask != null) {
                 mQueryTask.cancel(false);
             }
 
-            Pair<Integer, String> folderPair = mParentStack.getFirst();
-            mFolderId = folderPair.first;
-            mFolderTitle = folderPair.second;
-
+            final Pair<Integer, String> folderPair = mParentStack.getFirst();
             mQueryTask = new BookmarksQueryTask();
-            mQueryTask.execute(new Integer(mFolderId));
+            mQueryTask.execute(folderPair.first);
         }
 
         /**
          * Moves to parent folder, if one exists.
          */
         public void moveToParentFolder() {
             // If we're already at the root, we can't move to a parent folder
             if (mParentStack.size() != 1) {
@@ -276,16 +243,26 @@ public class BookmarksListView extends H
             refreshCurrentFolder();
         }
 
         /**
          * {@inheritDoc}
          */
         @Override
         public int getItemViewType(int position) {
+            // The position also reflects the opened child folder row.
+            if (isShowingChildFolder()) {
+                if (position == 0) {
+                    return VIEW_TYPE_FOLDER;
+                }
+
+                // Accounting for the folder view.
+                position--;
+            }
+
             Cursor c = getCursor();
 
             if (!c.moveToPosition(position)) {
                 throw new IllegalStateException("Couldn't move cursor to position " + position);
             }
 
             return getItemViewType(c);
         }
@@ -338,26 +315,57 @@ public class BookmarksListView extends H
                 return getResources().getString(R.string.bookmarks_folder_unfiled);
             }
 
             // If for some reason we have a folder with a special GUID, but it's not one of
             // the special folders we expect in the UI, just return the title from the DB.
             return c.getString(c.getColumnIndexOrThrow(Bookmarks.TITLE));
         }
 
+        /**
+         * @return true, if currently showing a child folder, false otherwise.
+         */
+        public boolean isShowingChildFolder() {
+            return (mParentStack.peek().first != Bookmarks.FIXED_ROOT_ID);
+        }
+
+        @Override
+        public int getCount() {
+            return super.getCount() + (isShowingChildFolder() ? 1 : 0);
+        }
+
+        @Override
+        public View getView(int position, View convertView, ViewGroup parent) {
+            // The position also reflects the opened child folder row.
+            if (isShowingChildFolder()) {
+                if (position == 0) {
+                    BookmarkFolderView folder = (BookmarkFolderView) LayoutInflater.from(parent.getContext()).inflate(R.layout.bookmark_folder_row, null);
+                    folder.setText(mParentStack.peek().second);
+                    folder.open();
+                    return folder;
+                }
+
+                // Accounting for the folder view.
+                position--;
+            }
+
+            return super.getView(position, convertView, parent);
+        }
+
         @Override
         public void bindView(View view, Context context, Cursor cursor) {
             final int viewType = getItemViewType(cursor);
 
             if (viewType == VIEW_TYPE_ITEM) {
                 TwoLinePageRow row = (TwoLinePageRow) view;
                 row.updateFromCursor(cursor);
             } else {
                 BookmarkFolderView row = (BookmarkFolderView) view;
                 row.setText(getFolderTitle(cursor));
+                row.close();
             }
         }
 
         @Override
         public View newView(Context context, Cursor cursor, ViewGroup parent) {
             final int viewType = getItemViewType(cursor);
 
             final int resId;