2013-05-30 17:53:13 -07:00
|
|
|
/* -*- 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;
|
2013-06-27 14:39:33 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
|
|
|
|
import org.mozilla.gecko.db.BrowserDB;
|
2013-07-02 15:03:13 -07:00
|
|
|
import org.mozilla.gecko.home.BookmarksListAdapter.OnRefreshFolderListener;
|
2013-06-11 09:57:45 -07:00
|
|
|
import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
|
2013-05-30 17:53:13 -07:00
|
|
|
|
2013-07-22 12:49:36 -07:00
|
|
|
import android.app.Activity;
|
2013-06-27 14:39:33 -07:00
|
|
|
import android.content.Context;
|
2013-05-30 17:53:13 -07:00
|
|
|
import android.content.res.Configuration;
|
2013-06-27 14:39:33 -07:00
|
|
|
import android.database.Cursor;
|
2013-05-30 17:53:13 -07:00
|
|
|
import android.os.Bundle;
|
2013-06-27 14:39:33 -07:00
|
|
|
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
|
|
|
import android.support.v4.content.Loader;
|
2013-10-18 17:30:10 -07:00
|
|
|
import android.util.Pair;
|
2013-05-30 17:53:13 -07:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
2013-10-16 09:42:33 -07:00
|
|
|
import android.view.ViewStub;
|
2013-05-30 17:53:13 -07:00
|
|
|
import android.view.ViewGroup;
|
2013-10-16 09:42:33 -07:00
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
2013-07-02 14:31:34 -07:00
|
|
|
|
2013-10-18 17:30:10 -07:00
|
|
|
import java.util.List;
|
|
|
|
|
2013-05-30 17:53:13 -07:00
|
|
|
/**
|
|
|
|
* A page in about:home that displays a ListView of bookmarks.
|
|
|
|
*/
|
2013-06-14 12:00:59 -07:00
|
|
|
public class BookmarksPage extends HomeFragment {
|
2013-05-30 17:53:13 -07:00
|
|
|
public static final String LOGTAG = "GeckoBookmarksPage";
|
|
|
|
|
2013-06-27 14:39:33 -07:00
|
|
|
// Cursor loader ID for list of bookmarks.
|
2013-07-29 15:05:18 -07:00
|
|
|
private static final int LOADER_ID_BOOKMARKS_LIST = 0;
|
2013-06-27 14:39:33 -07:00
|
|
|
|
|
|
|
// Key for bookmarks folder id.
|
|
|
|
private static final String BOOKMARKS_FOLDER_KEY = "folder_id";
|
|
|
|
|
2013-06-24 11:03:28 -07:00
|
|
|
// List of bookmarks.
|
2013-06-14 12:00:59 -07:00
|
|
|
private BookmarksListView mList;
|
2013-05-30 17:53:13 -07:00
|
|
|
|
2013-07-02 15:03:13 -07:00
|
|
|
// Adapter for list of bookmarks.
|
|
|
|
private BookmarksListAdapter mListAdapter;
|
|
|
|
|
2013-10-18 17:30:10 -07:00
|
|
|
// Adapter's parent stack.
|
|
|
|
private List<Pair<Integer, String>> mSavedParentStack;
|
|
|
|
|
2013-10-16 09:42:33 -07:00
|
|
|
// Reference to the View to display when there are no results.
|
|
|
|
private View mEmptyView;
|
|
|
|
|
2013-07-09 16:24:38 -07:00
|
|
|
// Callback for cursor loaders.
|
2013-06-27 14:39:33 -07:00
|
|
|
private CursorLoaderCallbacks mLoaderCallbacks;
|
|
|
|
|
2013-05-30 17:53:13 -07:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
2013-08-08 15:15:13 -07:00
|
|
|
final View view = inflater.inflate(R.layout.home_bookmarks_page, container, false);
|
|
|
|
|
|
|
|
mList = (BookmarksListView) view.findViewById(R.id.bookmarks_list);
|
2013-06-24 11:03:28 -07:00
|
|
|
|
2013-08-08 15:15:13 -07:00
|
|
|
return view;
|
2013-05-30 17:53:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
|
|
|
super.onViewCreated(view, savedInstanceState);
|
|
|
|
|
2013-06-14 13:07:18 -07:00
|
|
|
OnUrlOpenListener listener = null;
|
|
|
|
try {
|
|
|
|
listener = (OnUrlOpenListener) getActivity();
|
|
|
|
} catch (ClassCastException e) {
|
|
|
|
throw new ClassCastException(getActivity().toString()
|
|
|
|
+ " must implement HomePager.OnUrlOpenListener");
|
|
|
|
}
|
|
|
|
|
2013-08-17 06:11:41 -07:00
|
|
|
mList.setTag(HomePager.LIST_TAG_BOOKMARKS);
|
2013-06-14 13:07:18 -07:00
|
|
|
mList.setOnUrlOpenListener(listener);
|
2013-05-30 17:53:13 -07:00
|
|
|
|
2013-07-22 13:08:38 -07:00
|
|
|
registerForContextMenu(mList);
|
2013-05-30 17:53:13 -07:00
|
|
|
}
|
|
|
|
|
2013-06-27 14:39:33 -07:00
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
2013-07-29 15:25:46 -07:00
|
|
|
final Activity activity = getActivity();
|
|
|
|
|
2013-07-02 15:03:13 -07:00
|
|
|
// Setup the list adapter.
|
2013-10-18 17:30:10 -07:00
|
|
|
mListAdapter = new BookmarksListAdapter(activity, null, mSavedParentStack);
|
2013-07-02 15:03:13 -07:00
|
|
|
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);
|
2013-07-29 15:05:18 -07:00
|
|
|
getLoaderManager().restartLoader(LOADER_ID_BOOKMARKS_LIST, bundle, mLoaderCallbacks);
|
2013-07-02 15:03:13 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
mList.setAdapter(mListAdapter);
|
|
|
|
|
2013-08-21 11:34:16 -07:00
|
|
|
// Invalidate the cached value that keeps track of whether or
|
|
|
|
// not desktop bookmarks (or reading list items) exist.
|
|
|
|
BrowserDB.invalidateCachedState();
|
|
|
|
|
2013-06-27 14:39:33 -07:00
|
|
|
// Create callbacks before the initial loader is started.
|
2013-09-18 09:58:25 -07:00
|
|
|
mLoaderCallbacks = new CursorLoaderCallbacks();
|
2013-07-26 04:46:28 -07:00
|
|
|
loadIfVisible();
|
2013-06-27 14:39:33 -07:00
|
|
|
}
|
|
|
|
|
2013-05-30 17:53:13 -07:00
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
mList = null;
|
2013-07-02 15:03:13 -07:00
|
|
|
mListAdapter = null;
|
2013-10-16 09:42:33 -07:00
|
|
|
mEmptyView = null;
|
2013-05-30 17:53:13 -07:00
|
|
|
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()) {
|
2013-10-18 17:30:10 -07:00
|
|
|
// The parent stack is saved just so that the folder state can be
|
|
|
|
// restored on rotation.
|
|
|
|
mSavedParentStack = mListAdapter.getParentStack();
|
|
|
|
|
2013-05-30 17:53:13 -07:00
|
|
|
getFragmentManager().beginTransaction()
|
|
|
|
.detach(this)
|
|
|
|
.attach(this)
|
|
|
|
.commitAllowingStateLoss();
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 14:39:33 -07:00
|
|
|
|
2013-07-26 04:46:28 -07:00
|
|
|
@Override
|
|
|
|
protected void load() {
|
2013-09-17 13:58:12 -07:00
|
|
|
getLoaderManager().initLoader(LOADER_ID_BOOKMARKS_LIST, null, mLoaderCallbacks);
|
2013-07-22 12:49:36 -07:00
|
|
|
}
|
|
|
|
|
2013-10-16 09:42:33 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 14:39:33 -07:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-09-18 09:58:25 -07:00
|
|
|
private class CursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
|
2013-06-27 14:39:33 -07:00
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
2013-09-19 10:53:14 -07:00
|
|
|
if (args == null) {
|
|
|
|
return new BookmarksLoader(getActivity());
|
|
|
|
} else {
|
|
|
|
return new BookmarksLoader(getActivity(), args.getInt(BOOKMARKS_FOLDER_KEY));
|
2013-06-27 14:39:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
|
2013-09-19 10:53:14 -07:00
|
|
|
mListAdapter.swapCursor(c);
|
2013-10-16 09:42:33 -07:00
|
|
|
updateUiFromCursor(c);
|
2013-06-27 14:39:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> loader) {
|
2013-09-19 10:53:14 -07:00
|
|
|
if (mList != null) {
|
|
|
|
mListAdapter.swapCursor(null);
|
2013-07-08 14:16:19 -07:00
|
|
|
}
|
2013-07-02 14:31:34 -07:00
|
|
|
}
|
|
|
|
}
|
2013-05-30 17:53:13 -07:00
|
|
|
}
|