Bug 739334: Cleanup bookmarks' AsyncTasks. [r=mfinkle, r=margaret]
authorSriram Ramasubramanian <sriram@mozilla.com>
Mon, 26 Mar 2012 14:43:35 -0700
changeset 92036 585261ed3fc3f011d8dc8ceadb42062f7112f64b
parent 92035 c730b2ecebecc36d3917e14c1e128574299a2208
child 92037 eda41c845ed599c3b2cdee48223061d7deafd1c0
push idunknown
push userunknown
push dateunknown
reviewersmfinkle, margaret
bugs739334
milestone13.0a2
Bug 739334: Cleanup bookmarks' AsyncTasks. [r=mfinkle, r=margaret]
mobile/android/base/AwesomeBarTabs.java
--- a/mobile/android/base/AwesomeBarTabs.java
+++ b/mobile/android/base/AwesomeBarTabs.java
@@ -200,17 +200,16 @@ public class AwesomeBarTabs extends TabH
     private class BookmarksListAdapter extends SimpleCursorAdapter {
         private static final int VIEW_TYPE_ITEM = 0;
         private static final int VIEW_TYPE_FOLDER = 1;
         private static final int VIEW_TYPE_COUNT = 2;
 
         private LayoutInflater mInflater;
         private Resources mResources;
         private LinkedList<Pair<Integer, String>> mParentStack;
-        private RefreshBookmarkCursorTask mRefreshTask = null;
         private LinearLayout mBookmarksTitleView;
 
         public BookmarksListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
             super(context, layout, c, from, to);
 
             mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             mResources = mContext.getResources();
 
@@ -220,22 +219,22 @@ public class AwesomeBarTabs extends TabH
 
             // Add the root folder to the stack
             Pair<Integer, String> rootFolder = new Pair<Integer, String>(Bookmarks.FIXED_ROOT_ID, "");
             mParentStack.addFirst(rootFolder);
         }
 
         public void refreshCurrentFolder() {
             // Cancel any pre-existing async refresh tasks
-            if (mRefreshTask != null)
-                mRefreshTask.cancel(false);
+            if (mBookmarksQueryTask != null)
+                mBookmarksQueryTask.cancel(false);
 
             Pair<Integer, String> folderPair = mParentStack.getFirst();
-            mRefreshTask = new RefreshBookmarkCursorTask(folderPair.first, folderPair.second);
-            mRefreshTask.execute();
+            mBookmarksQueryTask = new BookmarksQueryTask(folderPair.first, folderPair.second);
+            mBookmarksQueryTask.execute();
         }
 
         // Returns false if there is no parent folder to move to
         public boolean moveToParentFolder() {
             // If we're already at the root, we can't move to a parent folder
             if (mParentStack.size() == 1)
                 return false;
 
@@ -303,105 +302,103 @@ public class AwesomeBarTabs extends TabH
                 convertView = mInflater.inflate(R.layout.awesomebar_folder_row, null);
 
             TextView titleView = (TextView) convertView.findViewById(R.id.title);
             titleView.setText(getFolderTitle(position));
 
             return convertView;
         }
 
-        public LinearLayout getBookmarksTitleView() {
+        public LinearLayout getHeaderView() {
             return mBookmarksTitleView;
         }
 
-        public void setBookmarksTitleView(LinearLayout titleView) {
+        public void setHeaderView(LinearLayout titleView) {
             mBookmarksTitleView = titleView;
         }
-
-        private class RefreshBookmarkCursorTask extends AsyncTask<Void, Void, Cursor> {
-            private int mFolderId;
-            private String mFolderTitle;
-
-            public RefreshBookmarkCursorTask(int folderId, String folderTitle) {
-                mFolderId = folderId;
-                mFolderTitle = folderTitle;
-            }
-
-            protected Cursor doInBackground(Void... params) {
-                return BrowserDB.getBookmarksInFolder(mContentResolver, mFolderId);
-            }
-
-            protected void onPostExecute(Cursor cursor) {
-                ListView list = (ListView) findViewById(R.id.bookmarks_list);
-                list.setAdapter(null);
-
-                // Hide the header text if we're at the root folder
-                if (mFolderId == Bookmarks.FIXED_ROOT_ID) {
-                    if (list.getHeaderViewsCount() == 1)
-                        list.removeHeaderView(mBookmarksTitleView);
-                } else {
-                    if (list.getHeaderViewsCount() == 0)
-                        list.addHeaderView(mBookmarksTitleView, null, true);
-
-                    ((TextView) mBookmarksTitleView.findViewById(R.id.title)).setText(mFolderTitle);
-                }
-
-                mBookmarksAdapter.changeCursor(cursor);
-                list.setAdapter(mBookmarksAdapter);
-                mRefreshTask = null;
-            }
-        }
     }
 
     // This method checks to see if we're in a bookmark sub-folder. If we are,
     // it will go up a level and return true. Otherwise it will return false.
     public boolean onBackPressed() {
         // If we're not in the bookmarks tab, we have nothing to do. We should
         // also return false if mBookmarksAdapter hasn't been initialized yet.
         if (!getCurrentTabTag().equals(BOOKMARKS_TAB) ||
                 mBookmarksAdapter == null)
             return false;
 
         return mBookmarksAdapter.moveToParentFolder();
     }
-
+     
     private class BookmarksQueryTask extends AsyncTask<Void, Void, Cursor> {
-        protected Cursor doInBackground(Void... arg0) {
-            return BrowserDB.getBookmarksInFolder(mContentResolver, Bookmarks.FIXED_ROOT_ID);
+        private int mFolderId;
+        private String mFolderTitle;
+
+        public BookmarksQueryTask() {
+            mFolderId = Bookmarks.FIXED_ROOT_ID;
+            mFolderTitle = "";
         }
 
+        public BookmarksQueryTask(int folderId, String folderTitle) {
+            mFolderId = folderId;
+            mFolderTitle = folderTitle;
+        }
+
+        @Override
+        protected Cursor doInBackground(Void... arg0) {
+            return BrowserDB.getBookmarksInFolder(mContentResolver, mFolderId);
+        }
+
+        @Override
         protected void onPostExecute(Cursor cursor) {
-            // Load the list using a custom adapter so we can create the bitmaps
-            mBookmarksAdapter = new BookmarksListAdapter(
-                mContext,
-                R.layout.awesomebar_row,
-                cursor,
-                new String[] { URLColumns.TITLE,
-                               URLColumns.URL,
-                               URLColumns.FAVICON },
-                new int[] { R.id.title, R.id.url, R.id.favicon }
-            );
-
-            mBookmarksAdapter.setViewBinder(new AwesomeCursorViewBinder());
-
-            ListView bookmarksList = (ListView) findViewById(R.id.bookmarks_list);
-            bookmarksList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
+            ListView list = (ListView) findViewById(R.id.bookmarks_list);
+            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                     handleBookmarkItemClick(parent, view, position, id);
                 }
             });
+            
+            // We need to add the header before we set the adapter, hence make it null
+            list.setAdapter(null);
 
-            if (mBookmarksAdapter.getBookmarksTitleView() == null) {
-                // Caching the header view
-                LinearLayout headerView = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.awesomebar_header_row, null);
-                mBookmarksAdapter.setBookmarksTitleView(headerView);
+            if (mBookmarksAdapter == null) {
+                // Load the list using a custom adapter so we can create the bitmaps
+                mBookmarksAdapter = new BookmarksListAdapter(
+                    mContext,
+                    R.layout.awesomebar_row,
+                    cursor,
+                    new String[] { URLColumns.TITLE,
+                                   URLColumns.URL,
+                                   URLColumns.FAVICON },
+                    new int[] { R.id.title, R.id.url, R.id.favicon }
+                );
+
+                mBookmarksAdapter.setViewBinder(new AwesomeCursorViewBinder());
+            } else {
+                mBookmarksAdapter.changeCursor(cursor);
             }
 
-            bookmarksList.setAdapter(mBookmarksAdapter);
+            LinearLayout headerView = mBookmarksAdapter.getHeaderView();
+            if (headerView == null) {
+                headerView = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.awesomebar_header_row, null);
+                mBookmarksAdapter.setHeaderView(headerView);
+            }
 
+            // Add/Remove header based on the root folder
+            if (mFolderId == Bookmarks.FIXED_ROOT_ID) {
+                if (list.getHeaderViewsCount() == 1)
+                    list.removeHeaderView(headerView);
+            } else {
+                if (list.getHeaderViewsCount() == 0)
+                    list.addHeaderView(headerView, null, true);
+
+                ((TextView) headerView.findViewById(R.id.title)).setText(mFolderTitle);
+            }
+
+            list.setAdapter(mBookmarksAdapter);
             mBookmarksQueryTask = null;
         }
     }
 
     private static class GroupList extends LinkedList<Map<String,String>> {
         private static final long serialVersionUID = 0L;
     }