Bug 925068: Bookmarks folders are lost on rotation. [r=lucasr] [a=lsblakk]
authorSriram Ramasubramanian <sriram@mozilla.com>
Fri, 18 Oct 2013 17:30:10 -0700
changeset 160794 1cb44a39126e3719e49118fcb0987dc1c054709e
parent 160793 5bd7c307b8a559bade1c141873bb862be153026c
child 160795 b849074cee927af9b1ad6c6333790f9b113886b6
push id2961
push userlsblakk@mozilla.com
push dateMon, 28 Oct 2013 21:59:28 +0000
treeherdermozilla-beta@73ef4f13486f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslucasr, lsblakk
bugs925068
milestone26.0a2
Bug 925068: Bookmarks folders are lost on rotation. [r=lucasr] [a=lsblakk]
mobile/android/base/home/BookmarksListAdapter.java
mobile/android/base/home/BookmarksPage.java
--- a/mobile/android/base/home/BookmarksListAdapter.java
+++ b/mobile/android/base/home/BookmarksListAdapter.java
@@ -9,16 +9,18 @@ import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.BrowserContract.Bookmarks;
 
 import android.content.Context;
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.util.Pair;
 import android.view.View;
 
+import java.util.Collections;
+import java.util.List;
 import java.util.LinkedList;
 
 /**
  * Adapter to back the BookmarksListView with a list of bookmarks.
  */
 class BookmarksListAdapter extends MultiTypeCursorAdapter {
     private static final int VIEW_TYPE_ITEM = 0;
     private static final int VIEW_TYPE_FOLDER = 1;
@@ -35,25 +37,33 @@ class BookmarksListAdapter extends Multi
 
     // mParentStack holds folder id/title pairs that allow us to navigate
     // back up the folder heirarchy.
     private LinkedList<Pair<Integer, String>> mParentStack;
 
     // Refresh folder listener.
     private OnRefreshFolderListener mListener;
 
-    public BookmarksListAdapter(Context context, Cursor cursor) {
+    public BookmarksListAdapter(Context context, Cursor cursor, List<Pair<Integer, String>> parentStack) {
         // Initializing with a null cursor.
         super(context, cursor, VIEW_TYPES, LAYOUT_TYPES);
 
-        mParentStack = new LinkedList<Pair<Integer, String>>();
+        if (parentStack == null) {
+            mParentStack = new LinkedList<Pair<Integer, String>>();
 
-        // Add the root folder to the stack
-        Pair<Integer, String> rootFolder = new Pair<Integer, String>(Bookmarks.FIXED_ROOT_ID, "");
-        mParentStack.addFirst(rootFolder);
+            // Add the root folder to the stack
+            Pair<Integer, String> rootFolder = new Pair<Integer, String>(Bookmarks.FIXED_ROOT_ID, "");
+            mParentStack.addFirst(rootFolder);
+        } else {
+            mParentStack = new LinkedList<Pair<Integer, String>>(parentStack);
+        }
+    }
+
+    public List<Pair<Integer, String>> getParentStack() {
+        return Collections.unmodifiableList(mParentStack);
     }
 
     // Refresh the current folder by executing a new task.
     private void refreshCurrentFolder() {
         if (mListener != null) {
             mListener.onRefreshFolder(mParentStack.peek().first);
         }
     }
--- a/mobile/android/base/home/BookmarksPage.java
+++ b/mobile/android/base/home/BookmarksPage.java
@@ -23,20 +23,23 @@ import android.content.res.Configuration
 import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.os.Bundle;
 import android.support.v4.app.LoaderManager;
 import android.support.v4.app.LoaderManager.LoaderCallbacks;
 import android.support.v4.content.Loader;
 import android.text.TextUtils;
 import android.util.Log;
+import android.util.Pair;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 
+import java.util.List;
+
 /**
  * A page in about:home that displays a ListView of bookmarks.
  */
 public class BookmarksPage extends HomeFragment {
     public static final String LOGTAG = "GeckoBookmarksPage";
 
     // Cursor loader ID for list of bookmarks.
     private static final int LOADER_ID_BOOKMARKS_LIST = 0;
@@ -45,16 +48,19 @@ public class BookmarksPage extends HomeF
     private static final String BOOKMARKS_FOLDER_KEY = "folder_id";
 
     // List of bookmarks.
     private BookmarksListView mList;
 
     // Adapter for list of bookmarks.
     private BookmarksListAdapter mListAdapter;
 
+    // Adapter's parent stack.
+    private List<Pair<Integer, String>> mSavedParentStack;
+
     // Callback for cursor loaders.
     private CursorLoaderCallbacks mLoaderCallbacks;
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         final View view = inflater.inflate(R.layout.home_bookmarks_page, container, false);
 
         mList = (BookmarksListView) view.findViewById(R.id.bookmarks_list);
@@ -82,17 +88,17 @@ public class BookmarksPage extends HomeF
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
         final Activity activity = getActivity();
 
         // Setup the list adapter.
-        mListAdapter = new BookmarksListAdapter(activity, null);
+        mListAdapter = new BookmarksListAdapter(activity, null, mSavedParentStack);
         mListAdapter.setOnRefreshFolderListener(new OnRefreshFolderListener() {
             @Override
             public void onRefreshFolder(int folderId) {
                 // Restart the loader with folder as the argument.
                 Bundle bundle = new Bundle();
                 bundle.putInt(BOOKMARKS_FOLDER_KEY, folderId);
                 getLoaderManager().restartLoader(LOADER_ID_BOOKMARKS_LIST, bundle, mLoaderCallbacks);
             }
@@ -123,16 +129,20 @@ public class BookmarksPage extends HomeF
         // We use commitAllowingStateLoss() instead of commit() here to avoid
         // an IllegalStateException. If the phone is rotated while Fennec
         // is in the background, onConfigurationChanged() is fired.
         // onConfigurationChanged() is called before onResume(), so
         // using commit() would throw an IllegalStateException since it can't
         // be used between the Activity's onSaveInstanceState() and
         // onResume().
         if (isVisible()) {
+            // The parent stack is saved just so that the folder state can be
+            // restored on rotation.
+            mSavedParentStack = mListAdapter.getParentStack();
+
             getFragmentManager().beginTransaction()
                                 .detach(this)
                                 .attach(this)
                                 .commitAllowingStateLoss();
         }
     }
 
     @Override