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;
|
|
|
|
|
2013-07-02 14:31:34 -07:00
|
|
|
import org.mozilla.gecko.Favicons;
|
2013-05-30 17:53:13 -07:00
|
|
|
import org.mozilla.gecko.R;
|
2013-06-27 14:39:33 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
|
2013-07-02 14:31:34 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.Thumbnails;
|
2013-06-27 14:39:33 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserDB;
|
2013-07-02 14:31:34 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserDB.URLColumns;
|
|
|
|
import org.mozilla.gecko.gfx.BitmapUtils;
|
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-07-02 14:31:34 -07:00
|
|
|
import org.mozilla.gecko.home.TopBookmarksView.Thumbnail;
|
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
|
|
|
import org.mozilla.gecko.util.UiAsyncTask;
|
2013-05-30 17:53:13 -07:00
|
|
|
|
2013-07-02 14:31:34 -07:00
|
|
|
import android.content.ContentResolver;
|
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-07-02 14:31:34 -07:00
|
|
|
import android.graphics.Bitmap;
|
2013-05-30 17:53:13 -07:00
|
|
|
import android.os.Bundle;
|
2013-06-27 14:41:28 -07:00
|
|
|
import android.support.v4.app.LoaderManager;
|
2013-06-27 14:39:33 -07:00
|
|
|
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
|
|
|
import android.support.v4.content.Loader;
|
2013-05-30 17:53:13 -07:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
2013-07-02 14:31:34 -07:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
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.
|
|
|
|
private static final int BOOKMARKS_LIST_LOADER_ID = 0;
|
|
|
|
|
2013-06-27 14:41:28 -07:00
|
|
|
// Cursor loader ID for grid of bookmarks.
|
|
|
|
private static final int TOP_BOOKMARKS_LOADER_ID = 1;
|
|
|
|
|
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-06-24 11:03:28 -07:00
|
|
|
// Grid of top bookmarks.
|
|
|
|
private TopBookmarksView mTopBookmarks;
|
|
|
|
|
2013-07-02 15:03:13 -07:00
|
|
|
// Adapter for list of bookmarks.
|
|
|
|
private BookmarksListAdapter mListAdapter;
|
|
|
|
|
2013-07-02 14:31:34 -07:00
|
|
|
// Adapter for grid of bookmarks.
|
|
|
|
private TopBookmarksAdapter mTopBookmarksAdapter;
|
|
|
|
|
2013-06-27 14:39:33 -07:00
|
|
|
// Callback for loaders.
|
|
|
|
private CursorLoaderCallbacks mLoaderCallbacks;
|
|
|
|
|
2013-05-30 17:53:13 -07:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
2013-06-24 11:03:28 -07:00
|
|
|
BookmarksListView list = (BookmarksListView) inflater.inflate(R.layout.home_bookmarks_page, container, false);
|
|
|
|
|
|
|
|
mTopBookmarks = new TopBookmarksView(getActivity());
|
|
|
|
list.addHeaderView(mTopBookmarks);
|
|
|
|
|
|
|
|
return list;
|
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-06-14 12:00:59 -07:00
|
|
|
mList = (BookmarksListView) view.findViewById(R.id.bookmarks_list);
|
2013-06-14 13:07:18 -07:00
|
|
|
mList.setOnUrlOpenListener(listener);
|
2013-05-30 17:53:13 -07:00
|
|
|
|
2013-06-14 09:35:11 -07:00
|
|
|
registerForContextMenu(mList);
|
2013-06-24 11:03:28 -07:00
|
|
|
|
|
|
|
mTopBookmarks.setOnUrlOpenListener(listener);
|
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-02 14:31:34 -07:00
|
|
|
// Setup the top bookmarks adapter.
|
|
|
|
mTopBookmarksAdapter = new TopBookmarksAdapter(getActivity(), null);
|
|
|
|
mTopBookmarks.setAdapter(mTopBookmarksAdapter);
|
|
|
|
|
2013-07-02 15:03:13 -07:00
|
|
|
// Setup the list adapter.
|
|
|
|
mListAdapter = new BookmarksListAdapter(getActivity(), null);
|
|
|
|
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(BOOKMARKS_LIST_LOADER_ID, bundle, mLoaderCallbacks);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
mList.setAdapter(mListAdapter);
|
|
|
|
|
2013-06-27 14:39:33 -07:00
|
|
|
// Create callbacks before the initial loader is started.
|
|
|
|
mLoaderCallbacks = new CursorLoaderCallbacks();
|
|
|
|
|
|
|
|
// Reconnect to the loader only if present.
|
2013-06-27 14:41:28 -07:00
|
|
|
final LoaderManager manager = getLoaderManager();
|
|
|
|
manager.initLoader(BOOKMARKS_LIST_LOADER_ID, null, mLoaderCallbacks);
|
|
|
|
manager.initLoader(TOP_BOOKMARKS_LOADER_ID, null, mLoaderCallbacks);
|
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-07-02 14:31:34 -07:00
|
|
|
mTopBookmarks = null;
|
|
|
|
mTopBookmarksAdapter = 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()) {
|
|
|
|
getFragmentManager().beginTransaction()
|
|
|
|
.detach(this)
|
|
|
|
.attach(this)
|
|
|
|
.commitAllowingStateLoss();
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 14:41:28 -07:00
|
|
|
/**
|
|
|
|
* Loader for the grid for top bookmarks.
|
|
|
|
*/
|
|
|
|
private static class TopBookmarksLoader extends SimpleCursorLoader {
|
|
|
|
public TopBookmarksLoader(Context context) {
|
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Cursor loadCursor() {
|
|
|
|
final int max = getContext().getResources().getInteger(R.integer.number_of_top_sites);
|
|
|
|
return BrowserDB.getTopSites(getContext().getContentResolver(), max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 14:39:33 -07:00
|
|
|
/**
|
|
|
|
* Loader callbacks for the LoaderManager of this fragment.
|
|
|
|
*/
|
|
|
|
private class CursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
|
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
|
|
|
switch(id) {
|
|
|
|
case BOOKMARKS_LIST_LOADER_ID: {
|
|
|
|
if (args == null) {
|
|
|
|
return new BookmarksLoader(getActivity());
|
|
|
|
} else {
|
|
|
|
return new BookmarksLoader(getActivity(), args.getInt(BOOKMARKS_FOLDER_KEY));
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 14:41:28 -07:00
|
|
|
|
|
|
|
case TOP_BOOKMARKS_LOADER_ID: {
|
|
|
|
return new TopBookmarksLoader(getActivity());
|
|
|
|
}
|
2013-06-27 14:39:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
|
|
|
|
final int loaderId = loader.getId();
|
|
|
|
switch(loaderId) {
|
|
|
|
case BOOKMARKS_LIST_LOADER_ID: {
|
2013-07-02 15:03:13 -07:00
|
|
|
mListAdapter.swapCursor(c);
|
2013-06-27 14:39:33 -07:00
|
|
|
break;
|
|
|
|
}
|
2013-06-27 14:41:28 -07:00
|
|
|
|
|
|
|
case TOP_BOOKMARKS_LOADER_ID: {
|
2013-07-02 14:31:34 -07:00
|
|
|
mTopBookmarksAdapter.swapCursor(c);
|
|
|
|
|
|
|
|
// Load the thumbnails.
|
|
|
|
if (c.getCount() > 0) {
|
|
|
|
new LoadThumbnailsTask(getActivity()).execute(c);
|
|
|
|
}
|
2013-06-27 14:41:28 -07:00
|
|
|
break;
|
|
|
|
}
|
2013-06-27 14:39:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> loader) {
|
|
|
|
final int loaderId = loader.getId();
|
|
|
|
switch(loaderId) {
|
|
|
|
case BOOKMARKS_LIST_LOADER_ID: {
|
|
|
|
if (mList != null) {
|
2013-07-02 15:03:13 -07:00
|
|
|
mListAdapter.swapCursor(null);
|
2013-06-27 14:39:33 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-06-27 14:41:28 -07:00
|
|
|
|
|
|
|
case TOP_BOOKMARKS_LOADER_ID: {
|
|
|
|
if (mTopBookmarks != null) {
|
2013-07-02 14:31:34 -07:00
|
|
|
mTopBookmarksAdapter.swapCursor(null);
|
2013-06-27 14:41:28 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 14:39:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 14:31:34 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An AsyncTask to load the thumbnails from a cursor.
|
|
|
|
*/
|
|
|
|
private class LoadThumbnailsTask extends UiAsyncTask<Cursor, Void, Map<String, Thumbnail>> {
|
|
|
|
private final Context mContext;
|
|
|
|
|
|
|
|
public LoadThumbnailsTask(Context context) {
|
|
|
|
super(ThreadUtils.getBackgroundHandler());
|
|
|
|
mContext = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Map<String, Thumbnail> doInBackground(Cursor... params) {
|
|
|
|
// TopBookmarksAdapter's cursor.
|
|
|
|
final Cursor adapterCursor = params[0];
|
|
|
|
if (adapterCursor == null || !adapterCursor.moveToFirst()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final List<String> urls = new ArrayList<String>();
|
|
|
|
do {
|
|
|
|
final String url = adapterCursor.getString(adapterCursor.getColumnIndexOrThrow(URLColumns.URL));
|
|
|
|
urls.add(url);
|
|
|
|
} while (adapterCursor.moveToNext());
|
|
|
|
|
|
|
|
if (urls.size() == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final Map<String, Thumbnail> thumbnails = new HashMap<String, Thumbnail>();
|
|
|
|
|
|
|
|
// Query the DB for thumbnails.
|
|
|
|
final ContentResolver cr = mContext.getContentResolver();
|
|
|
|
final Cursor cursor = BrowserDB.getThumbnailsForUrls(cr, urls);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
do {
|
|
|
|
// Try to get the thumbnail, if cursor is valid.
|
|
|
|
String url = cursor.getString(cursor.getColumnIndexOrThrow(Thumbnails.URL));
|
|
|
|
final byte[] b = cursor.getBlob(cursor.getColumnIndexOrThrow(Thumbnails.DATA));
|
2013-07-03 17:08:31 -07:00
|
|
|
final Bitmap bitmap = (b == null || b.length == 0 ? null : BitmapUtils.decodeByteArray(b));
|
2013-07-02 14:31:34 -07:00
|
|
|
|
|
|
|
if (bitmap != null) {
|
|
|
|
thumbnails.put(url, new Thumbnail(bitmap, true));
|
|
|
|
}
|
|
|
|
} while (cursor.moveToNext());
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) {
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the DB for favicons for the urls without thumbnails.
|
|
|
|
for (String url : urls) {
|
|
|
|
if (!thumbnails.containsKey(url)) {
|
|
|
|
final Bitmap bitmap = BrowserDB.getFaviconForUrl(cr, url);
|
|
|
|
if (bitmap != null) {
|
|
|
|
// Favicons.scaleImage can return several different size favicons,
|
|
|
|
// but will at least prevent this from being too large.
|
|
|
|
thumbnails.put(url, new Thumbnail(Favicons.getInstance().scaleImage(bitmap), false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return thumbnails;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPostExecute(Map<String, Thumbnail> thumbnails) {
|
|
|
|
mTopBookmarks.updateThumbnails(thumbnails);
|
|
|
|
}
|
|
|
|
}
|
2013-05-30 17:53:13 -07:00
|
|
|
}
|