gecko-dev/mobile/android/base/home/BookmarksPage.java
Sriram Ramasubramanian 8fa109e309 Bug 925068: Bookmarks folders are lost on rotation. [r=lucasr]
--HG--
extra : rebase_source : 0337903f40ba0ead258d66730e9dc10622b5673c
2013-10-18 17:30:10 -07:00

214 lines
7.3 KiB
Java

/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* 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.home;
import org.mozilla.gecko.R;
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.home.BookmarksListAdapter.OnRefreshFolderListener;
import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
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;
// Key for bookmarks folder id.
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;
// Reference to the View to display when there are no results.
private View mEmptyView;
// 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);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
OnUrlOpenListener listener = null;
try {
listener = (OnUrlOpenListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().toString()
+ " must implement HomePager.OnUrlOpenListener");
}
mList.setTag(HomePager.LIST_TAG_BOOKMARKS);
mList.setOnUrlOpenListener(listener);
registerForContextMenu(mList);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final Activity activity = getActivity();
// Setup the list adapter.
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);
}
});
mList.setAdapter(mListAdapter);
// Invalidate the cached value that keeps track of whether or
// not desktop bookmarks (or reading list items) exist.
BrowserDB.invalidateCachedState();
// Create callbacks before the initial loader is started.
mLoaderCallbacks = new CursorLoaderCallbacks();
loadIfVisible();
}
@Override
public void onDestroyView() {
mList = null;
mListAdapter = null;
mEmptyView = null;
super.onDestroyView();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Reattach the fragment, forcing a reinflation of its view.
// 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
protected void load() {
getLoaderManager().initLoader(LOADER_ID_BOOKMARKS_LIST, null, mLoaderCallbacks);
}
private void updateUiFromCursor(Cursor c) {
if ((c == null || c.getCount() == 0) && mEmptyView == null) {
// Set empty page view. We delay this so that the empty view won't flash.
final ViewStub emptyViewStub = (ViewStub) getView().findViewById(R.id.home_empty_view_stub);
mEmptyView = emptyViewStub.inflate();
final ImageView emptyIcon = (ImageView) mEmptyView.findViewById(R.id.home_empty_image);
emptyIcon.setImageResource(R.drawable.icon_bookmarks_empty);
final TextView emptyText = (TextView) mEmptyView.findViewById(R.id.home_empty_text);
emptyText.setText(R.string.home_bookmarks_empty);
mList.setEmptyView(mEmptyView);
}
}
/**
* Loader for the list for bookmarks.
*/
private static class BookmarksLoader extends SimpleCursorLoader {
private final int mFolderId;
public BookmarksLoader(Context context) {
this(context, Bookmarks.FIXED_ROOT_ID);
}
public BookmarksLoader(Context context, int folderId) {
super(context);
mFolderId = folderId;
}
@Override
public Cursor loadCursor() {
return BrowserDB.getBookmarksInFolder(getContext().getContentResolver(), mFolderId);
}
}
/**
* Loader callbacks for the LoaderManager of this fragment.
*/
private class CursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (args == null) {
return new BookmarksLoader(getActivity());
} else {
return new BookmarksLoader(getActivity(), args.getInt(BOOKMARKS_FOLDER_KEY));
}
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
mListAdapter.swapCursor(c);
updateUiFromCursor(c);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
if (mList != null) {
mListAdapter.swapCursor(null);
}
}
}
}