2013-06-25 09:51:57 -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-09-17 13:24:48 -07:00
|
|
|
import org.mozilla.gecko.favicons.Favicons;
|
2013-06-25 09:51:57 -07:00
|
|
|
import org.mozilla.gecko.R;
|
2013-09-17 13:24:48 -07:00
|
|
|
import org.mozilla.gecko.Tabs;
|
2013-09-17 13:58:12 -07:00
|
|
|
import org.mozilla.gecko.animation.PropertyAnimator;
|
|
|
|
import org.mozilla.gecko.animation.PropertyAnimator.Property;
|
|
|
|
import org.mozilla.gecko.animation.ViewHelper;
|
2013-09-17 13:24:48 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.Thumbnails;
|
2013-06-25 09:51:57 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserDB;
|
|
|
|
import org.mozilla.gecko.db.BrowserDB.URLColumns;
|
2013-09-18 14:30:01 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserDB.TopSitesCursorWrapper;
|
2013-09-17 13:24:48 -07:00
|
|
|
import org.mozilla.gecko.gfx.BitmapUtils;
|
|
|
|
import org.mozilla.gecko.home.HomeListView.HomeContextMenuInfo;
|
2013-06-25 09:51:57 -07:00
|
|
|
import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
|
2013-09-17 13:24:48 -07:00
|
|
|
import org.mozilla.gecko.home.PinSiteDialog.OnSiteSelectedListener;
|
|
|
|
import org.mozilla.gecko.home.TopSitesGridView.OnPinSiteListener;
|
|
|
|
import org.mozilla.gecko.home.TopSitesGridView.TopSitesGridContextMenuInfo;
|
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2013-06-25 09:51:57 -07:00
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
2013-09-17 13:58:12 -07:00
|
|
|
import android.content.res.Configuration;
|
2013-06-25 09:51:57 -07:00
|
|
|
import android.database.Cursor;
|
2013-09-17 13:24:48 -07:00
|
|
|
import android.graphics.Bitmap;
|
2013-06-25 09:51:57 -07:00
|
|
|
import android.os.Bundle;
|
2013-09-17 13:24:48 -07:00
|
|
|
import android.support.v4.app.FragmentManager;
|
2013-07-29 15:25:46 -07:00
|
|
|
import android.support.v4.app.LoaderManager;
|
2013-09-17 13:24:48 -07:00
|
|
|
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
|
|
|
import android.support.v4.content.AsyncTaskLoader;
|
2013-06-25 09:51:57 -07:00
|
|
|
import android.support.v4.content.Loader;
|
2013-07-02 16:27:28 -07:00
|
|
|
import android.support.v4.widget.CursorAdapter;
|
2013-09-17 13:24:48 -07:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.ContextMenu;
|
|
|
|
import android.view.ContextMenu.ContextMenuInfo;
|
2013-07-02 16:27:28 -07:00
|
|
|
import android.view.LayoutInflater;
|
2013-09-17 13:24:48 -07:00
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2013-09-17 13:58:12 -07:00
|
|
|
import android.view.MotionEvent;
|
2013-06-25 09:51:57 -07:00
|
|
|
import android.view.View;
|
2013-09-17 13:58:12 -07:00
|
|
|
import android.view.View.OnTouchListener;
|
2013-06-25 09:51:57 -07:00
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ListView;
|
2013-09-17 13:24:48 -07:00
|
|
|
import android.widget.Toast;
|
2013-06-25 09:51:57 -07:00
|
|
|
|
2013-08-12 11:44:46 -07:00
|
|
|
import java.util.EnumSet;
|
2013-09-17 13:24:48 -07:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2013-08-12 11:44:46 -07:00
|
|
|
|
2013-06-25 09:51:57 -07:00
|
|
|
/**
|
|
|
|
* Fragment that displays frecency search results in a ListView.
|
|
|
|
*/
|
2013-09-17 13:58:12 -07:00
|
|
|
public class TopSitesPage extends HomeFragment {
|
2013-06-25 09:51:57 -07:00
|
|
|
// Logging tag name
|
2013-09-17 13:58:12 -07:00
|
|
|
private static final String LOGTAG = "GeckoTopSitesPage";
|
2013-06-25 09:51:57 -07:00
|
|
|
|
2013-09-18 14:30:01 -07:00
|
|
|
// Cursor loader ID for the top sites
|
|
|
|
private static final int LOADER_ID_TOP_SITES = 0;
|
2013-06-25 09:51:57 -07:00
|
|
|
|
2013-09-17 13:24:48 -07:00
|
|
|
// Loader ID for thumbnails
|
2013-09-18 14:30:01 -07:00
|
|
|
private static final int LOADER_ID_THUMBNAILS = 1;
|
2013-09-17 13:24:48 -07:00
|
|
|
|
|
|
|
// Key for thumbnail urls
|
|
|
|
private static final String THUMBNAILS_URLS_KEY = "urls";
|
|
|
|
|
|
|
|
// Adapter for the list of top sites
|
|
|
|
private VisitedAdapter mListAdapter;
|
|
|
|
|
|
|
|
// Adapter for the grid of top sites
|
|
|
|
private TopSitesGridAdapter mGridAdapter;
|
|
|
|
|
|
|
|
// List of top sites
|
2013-06-25 09:51:57 -07:00
|
|
|
private ListView mList;
|
|
|
|
|
2013-09-17 13:24:48 -07:00
|
|
|
// Grid of top sites
|
|
|
|
private TopSitesGridView mGrid;
|
|
|
|
|
2013-09-17 13:58:12 -07:00
|
|
|
// Banner to show snippets.
|
|
|
|
private HomeBanner mBanner;
|
|
|
|
|
|
|
|
// Raw Y value of the last event that happened on the list view.
|
|
|
|
private float mListTouchY = -1;
|
|
|
|
|
|
|
|
// Scrolling direction of the banner.
|
|
|
|
private boolean mSnapBannerToTop;
|
|
|
|
|
2013-06-25 09:51:57 -07:00
|
|
|
// Callbacks used for the search and favicon cursor loaders
|
|
|
|
private CursorLoaderCallbacks mCursorLoaderCallbacks;
|
|
|
|
|
2013-09-17 13:24:48 -07:00
|
|
|
// Callback for thumbnail loader
|
|
|
|
private ThumbnailsLoaderCallbacks mThumbnailsLoaderCallbacks;
|
|
|
|
|
|
|
|
// Listener for pinning sites
|
|
|
|
private PinSiteListener mPinSiteListener;
|
|
|
|
|
2013-06-25 09:51:57 -07:00
|
|
|
// On URL open listener
|
|
|
|
private OnUrlOpenListener mUrlOpenListener;
|
|
|
|
|
2013-09-18 14:30:01 -07:00
|
|
|
// Max number of entries shown in the grid from the cursor.
|
|
|
|
private int mMaxGridEntries;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to hold the bitmap of cached thumbnails/favicons.
|
|
|
|
*/
|
|
|
|
public static class Thumbnail {
|
|
|
|
// Thumbnail or favicon.
|
|
|
|
private final boolean isThumbnail;
|
|
|
|
|
|
|
|
// Bitmap of thumbnail/favicon.
|
|
|
|
private final Bitmap bitmap;
|
|
|
|
|
|
|
|
public Thumbnail(Bitmap bitmap, boolean isThumbnail) {
|
|
|
|
this.bitmap = bitmap;
|
|
|
|
this.isThumbnail = isThumbnail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 13:58:12 -07:00
|
|
|
public static TopSitesPage newInstance() {
|
|
|
|
return new TopSitesPage();
|
2013-07-25 17:06:02 -07:00
|
|
|
}
|
|
|
|
|
2013-09-17 13:58:12 -07:00
|
|
|
public TopSitesPage() {
|
2013-06-25 09:51:57 -07:00
|
|
|
mUrlOpenListener = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
|
|
|
|
2013-09-18 14:30:01 -07:00
|
|
|
mMaxGridEntries = activity.getResources().getInteger(R.integer.number_of_top_sites);
|
|
|
|
|
2013-06-25 09:51:57 -07:00
|
|
|
try {
|
|
|
|
mUrlOpenListener = (OnUrlOpenListener) activity;
|
|
|
|
} catch (ClassCastException e) {
|
|
|
|
throw new ClassCastException(activity.toString()
|
|
|
|
+ " must implement HomePager.OnUrlOpenListener");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDetach() {
|
|
|
|
super.onDetach();
|
|
|
|
|
|
|
|
mUrlOpenListener = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
2013-09-17 13:24:48 -07:00
|
|
|
final View view = inflater.inflate(R.layout.home_top_sites_page, container, false);
|
|
|
|
|
|
|
|
mList = (HomeListView) view.findViewById(R.id.list);
|
|
|
|
|
|
|
|
mGrid = new TopSitesGridView(getActivity());
|
|
|
|
mList.addHeaderView(mGrid);
|
|
|
|
|
|
|
|
return view;
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
2013-09-17 13:24:48 -07:00
|
|
|
mPinSiteListener = new PinSiteListener();
|
|
|
|
|
2013-09-20 10:35:29 -07:00
|
|
|
mList.setTag(HomePager.LIST_TAG_TOP_SITES);
|
2013-09-19 14:19:43 -07:00
|
|
|
mList.setHeaderDividersEnabled(false);
|
2013-07-08 15:05:51 -07:00
|
|
|
|
2013-06-25 09:51:57 -07:00
|
|
|
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
2013-09-18 14:30:01 -07:00
|
|
|
final ListView list = (ListView) parent;
|
|
|
|
final int headerCount = list.getHeaderViewsCount();
|
|
|
|
if (position < headerCount) {
|
|
|
|
// The click is on a header, don't do anything.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Absolute position for the adapter.
|
|
|
|
position += (mGridAdapter.getCount() - headerCount);
|
|
|
|
|
2013-09-17 13:24:48 -07:00
|
|
|
final Cursor c = mListAdapter.getCursor();
|
2013-06-25 09:51:57 -07:00
|
|
|
if (c == null || !c.moveToPosition(position)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String url = c.getString(c.getColumnIndexOrThrow(URLColumns.URL));
|
2013-08-12 11:44:46 -07:00
|
|
|
|
|
|
|
// This item is a TwoLinePageRow, so we allow switch-to-tab.
|
|
|
|
mUrlOpenListener.onUrlOpen(url, EnumSet.of(OnUrlOpenListener.Flags.ALLOW_SWITCH_TO_TAB));
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-09-17 13:24:48 -07:00
|
|
|
mGrid.setOnUrlOpenListener(mUrlOpenListener);
|
|
|
|
mGrid.setOnPinSiteListener(mPinSiteListener);
|
|
|
|
|
2013-06-25 09:51:57 -07:00
|
|
|
registerForContextMenu(mList);
|
2013-09-17 13:24:48 -07:00
|
|
|
registerForContextMenu(mGrid);
|
2013-09-17 13:58:12 -07:00
|
|
|
|
|
|
|
mBanner = (HomeBanner) view.findViewById(R.id.home_banner);
|
|
|
|
mList.setOnTouchListener(new OnTouchListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
|
|
TopSitesPage.this.handleListTouchEvent(event);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
super.onDestroyView();
|
|
|
|
mList = null;
|
2013-09-17 13:24:48 -07:00
|
|
|
mGrid = null;
|
|
|
|
mListAdapter = null;
|
|
|
|
mGridAdapter = null;
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
|
|
|
|
2013-09-17 13:58:12 -07:00
|
|
|
@Override
|
|
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
|
|
|
super.onConfigurationChanged(newConfig);
|
|
|
|
|
|
|
|
// Detach and reattach the fragment as the layout changes.
|
|
|
|
if (isVisible()) {
|
|
|
|
getFragmentManager().beginTransaction()
|
|
|
|
.detach(this)
|
|
|
|
.attach(this)
|
|
|
|
.commitAllowingStateLoss();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 09:51:57 -07:00
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
2013-07-29 15:25:46 -07:00
|
|
|
final Activity activity = getActivity();
|
|
|
|
|
2013-09-17 13:24:48 -07:00
|
|
|
// Setup the top sites grid adapter.
|
|
|
|
mGridAdapter = new TopSitesGridAdapter(activity, null);
|
|
|
|
mGrid.setAdapter(mGridAdapter);
|
|
|
|
|
|
|
|
// Setup the top sites list adapter.
|
|
|
|
mListAdapter = new VisitedAdapter(activity, null);
|
|
|
|
mList.setAdapter(mListAdapter);
|
2013-06-25 09:51:57 -07:00
|
|
|
|
|
|
|
// Create callbacks before the initial loader is started
|
2013-09-19 10:53:14 -07:00
|
|
|
mCursorLoaderCallbacks = new CursorLoaderCallbacks();
|
2013-09-17 13:24:48 -07:00
|
|
|
mThumbnailsLoaderCallbacks = new ThumbnailsLoaderCallbacks();
|
2013-07-26 04:46:28 -07:00
|
|
|
loadIfVisible();
|
|
|
|
}
|
2013-06-25 09:51:57 -07:00
|
|
|
|
2013-09-17 13:24:48 -07:00
|
|
|
@Override
|
|
|
|
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
|
|
|
|
if (menuInfo == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HomeFragment will handle the default case.
|
|
|
|
if (menuInfo instanceof HomeContextMenuInfo) {
|
|
|
|
super.onCreateContextMenu(menu, view, menuInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(menuInfo instanceof TopSitesGridContextMenuInfo)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MenuInflater inflater = new MenuInflater(view.getContext());
|
|
|
|
inflater.inflate(R.menu.top_sites_contextmenu, menu);
|
|
|
|
|
|
|
|
TopSitesGridContextMenuInfo info = (TopSitesGridContextMenuInfo) menuInfo;
|
2013-09-18 14:30:01 -07:00
|
|
|
menu.setHeaderTitle(info.getDisplayTitle());
|
2013-09-17 13:24:48 -07:00
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(info.url)) {
|
|
|
|
if (info.isPinned) {
|
|
|
|
menu.findItem(R.id.top_sites_pin).setVisible(false);
|
|
|
|
} else {
|
|
|
|
menu.findItem(R.id.top_sites_unpin).setVisible(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
menu.findItem(R.id.top_sites_open_new_tab).setVisible(false);
|
|
|
|
menu.findItem(R.id.top_sites_open_private_tab).setVisible(false);
|
|
|
|
menu.findItem(R.id.top_sites_pin).setVisible(false);
|
|
|
|
menu.findItem(R.id.top_sites_unpin).setVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onContextItemSelected(MenuItem item) {
|
|
|
|
ContextMenuInfo menuInfo = item.getMenuInfo();
|
|
|
|
|
|
|
|
// HomeFragment will handle the default case.
|
|
|
|
if (menuInfo == null || !(menuInfo instanceof TopSitesGridContextMenuInfo)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TopSitesGridContextMenuInfo info = (TopSitesGridContextMenuInfo) menuInfo;
|
|
|
|
final Activity activity = getActivity();
|
|
|
|
|
|
|
|
final int itemId = item.getItemId();
|
|
|
|
if (itemId == R.id.top_sites_open_new_tab || itemId == R.id.top_sites_open_private_tab) {
|
|
|
|
if (info.url == null) {
|
|
|
|
Log.e(LOGTAG, "Can't open in new tab because URL is null");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int flags = Tabs.LOADURL_NEW_TAB | Tabs.LOADURL_BACKGROUND;
|
|
|
|
if (item.getItemId() == R.id.top_sites_open_private_tab)
|
|
|
|
flags |= Tabs.LOADURL_PRIVATE;
|
|
|
|
|
|
|
|
Tabs.getInstance().loadUrl(info.url, flags);
|
|
|
|
Toast.makeText(activity, R.string.new_tab_opened, Toast.LENGTH_SHORT).show();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemId == R.id.top_sites_pin) {
|
|
|
|
final String url = info.url;
|
|
|
|
final String title = info.title;
|
|
|
|
final int position = info.position;
|
|
|
|
final Context context = getActivity().getApplicationContext();
|
|
|
|
|
|
|
|
ThreadUtils.postToBackgroundThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
BrowserDB.pinSite(context.getContentResolver(), url, title, position);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemId == R.id.top_sites_unpin) {
|
|
|
|
final int position = info.position;
|
|
|
|
final Context context = getActivity().getApplicationContext();
|
|
|
|
|
|
|
|
ThreadUtils.postToBackgroundThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
BrowserDB.unpinSite(context.getContentResolver(), position);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemId == R.id.top_sites_edit) {
|
|
|
|
mPinSiteListener.onPinSite(info.position);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-26 04:46:28 -07:00
|
|
|
@Override
|
|
|
|
protected void load() {
|
2013-09-18 14:30:01 -07:00
|
|
|
getLoaderManager().initLoader(LOADER_ID_TOP_SITES, null, mCursorLoaderCallbacks);
|
2013-09-17 13:24:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listener for pinning sites.
|
|
|
|
*/
|
|
|
|
private class PinSiteListener implements OnPinSiteListener,
|
|
|
|
OnSiteSelectedListener {
|
|
|
|
// Tag for the PinSiteDialog fragment.
|
|
|
|
private static final String TAG_PIN_SITE = "pin_site";
|
|
|
|
|
|
|
|
// Position of the pin.
|
|
|
|
private int mPosition;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPinSite(int position) {
|
|
|
|
mPosition = position;
|
|
|
|
|
|
|
|
final FragmentManager manager = getActivity().getSupportFragmentManager();
|
|
|
|
PinSiteDialog dialog = (PinSiteDialog) manager.findFragmentByTag(TAG_PIN_SITE);
|
|
|
|
if (dialog == null) {
|
|
|
|
dialog = PinSiteDialog.newInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog.setOnSiteSelectedListener(this);
|
|
|
|
dialog.show(manager, TAG_PIN_SITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSiteSelected(final String url, final String title) {
|
|
|
|
final int position = mPosition;
|
|
|
|
final Context context = getActivity().getApplicationContext();
|
|
|
|
ThreadUtils.postToBackgroundThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
BrowserDB.pinSite(context.getContentResolver(), url, title, position);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
|
|
|
|
2013-09-17 13:58:12 -07:00
|
|
|
private void handleListTouchEvent(MotionEvent event) {
|
|
|
|
// Ignore the event if the banner is hidden for this session.
|
|
|
|
if (mBanner.isDismissed()) {
|
2013-08-14 15:31:42 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-17 13:58:12 -07:00
|
|
|
switch (event.getActionMasked()) {
|
|
|
|
case MotionEvent.ACTION_DOWN: {
|
|
|
|
mListTouchY = event.getRawY();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MotionEvent.ACTION_MOVE: {
|
|
|
|
// There is a chance that we won't receive ACTION_DOWN, if the touch event
|
|
|
|
// actually started on the Grid instead of the List. Treat this as first event.
|
|
|
|
if (mListTouchY == -1) {
|
|
|
|
mListTouchY = event.getRawY();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final float curY = event.getRawY();
|
|
|
|
final float delta = mListTouchY - curY;
|
|
|
|
mSnapBannerToTop = (delta > 0.0f) ? false : true;
|
|
|
|
|
|
|
|
final float height = mBanner.getHeight();
|
|
|
|
float newTranslationY = ViewHelper.getTranslationY(mBanner) + delta;
|
|
|
|
|
|
|
|
// Clamp the values to be between 0 and height.
|
|
|
|
if (newTranslationY < 0.0f) {
|
|
|
|
newTranslationY = 0.0f;
|
|
|
|
} else if (newTranslationY > height) {
|
|
|
|
newTranslationY = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewHelper.setTranslationY(mBanner, newTranslationY);
|
|
|
|
mListTouchY = curY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
case MotionEvent.ACTION_CANCEL: {
|
|
|
|
mListTouchY = -1;
|
|
|
|
final float y = ViewHelper.getTranslationY(mBanner);
|
|
|
|
final float height = mBanner.getHeight();
|
|
|
|
if (y > 0.0f && y < height) {
|
|
|
|
final PropertyAnimator animator = new PropertyAnimator(100);
|
|
|
|
animator.attach(mBanner, Property.TRANSLATION_Y, mSnapBannerToTop ? 0 : height);
|
|
|
|
animator.start();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateUiFromCursor(Cursor c) {
|
2013-09-19 14:19:43 -07:00
|
|
|
mList.setHeaderDividersEnabled(c != null && c.getCount() > mMaxGridEntries);
|
2013-08-14 15:31:42 -07:00
|
|
|
}
|
|
|
|
|
2013-09-18 14:30:01 -07:00
|
|
|
private static class TopSitesLoader extends SimpleCursorLoader {
|
2013-06-25 09:51:57 -07:00
|
|
|
// Max number of search results
|
2013-09-18 14:30:01 -07:00
|
|
|
private static final int SEARCH_LIMIT = 30;
|
|
|
|
private int mMaxGridEntries;
|
2013-06-25 09:51:57 -07:00
|
|
|
|
2013-09-18 14:30:01 -07:00
|
|
|
public TopSitesLoader(Context context) {
|
2013-06-25 09:51:57 -07:00
|
|
|
super(context);
|
2013-09-18 14:30:01 -07:00
|
|
|
mMaxGridEntries = context.getResources().getInteger(R.integer.number_of_top_sites);
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Cursor loadCursor() {
|
2013-09-30 20:56:09 -07:00
|
|
|
Log.d(LOGTAG, "TopSitesLoader.loadCursor()");
|
2013-09-18 14:30:01 -07:00
|
|
|
return BrowserDB.getTopSites(getContext().getContentResolver(), mMaxGridEntries, SEARCH_LIMIT);
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-18 14:30:01 -07:00
|
|
|
private class VisitedAdapter extends CursorAdapter {
|
|
|
|
public VisitedAdapter(Context context, Cursor cursor) {
|
|
|
|
super(context, cursor);
|
2013-09-17 13:24:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-09-18 14:30:01 -07:00
|
|
|
public int getCount() {
|
|
|
|
return Math.max(0, super.getCount() - mMaxGridEntries);
|
2013-09-17 13:24:48 -07:00
|
|
|
}
|
|
|
|
|
2013-09-18 14:30:01 -07:00
|
|
|
@Override
|
|
|
|
public Object getItem(int position) {
|
|
|
|
return super.getItem(position + mMaxGridEntries);
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-02 16:27:28 -07:00
|
|
|
public void bindView(View view, Context context, Cursor cursor) {
|
2013-09-18 14:30:01 -07:00
|
|
|
final int position = cursor.getPosition();
|
|
|
|
cursor.moveToPosition(position + mMaxGridEntries);
|
|
|
|
|
2013-07-02 16:27:28 -07:00
|
|
|
final TwoLinePageRow row = (TwoLinePageRow) view;
|
|
|
|
row.updateFromCursor(cursor);
|
|
|
|
}
|
2013-06-25 09:51:57 -07:00
|
|
|
|
2013-07-02 16:27:28 -07:00
|
|
|
@Override
|
|
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
2013-09-19 14:19:43 -07:00
|
|
|
return LayoutInflater.from(context).inflate(R.layout.bookmark_item_row, parent, false);
|
2013-09-18 14:30:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class TopSitesGridAdapter extends CursorAdapter {
|
|
|
|
// Cache to store the thumbnails.
|
|
|
|
private Map<String, Thumbnail> mThumbnails;
|
|
|
|
|
|
|
|
public TopSitesGridAdapter(Context context, Cursor cursor) {
|
|
|
|
super(context, cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
return Math.min(mMaxGridEntries, super.getCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onContentChanged() {
|
|
|
|
// Don't do anything. We don't want to regenerate every time
|
|
|
|
// our database is updated.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the thumbnails returned by the db.
|
|
|
|
*
|
|
|
|
* @param thumbnails A map of urls and their thumbnail bitmaps.
|
|
|
|
*/
|
|
|
|
public void updateThumbnails(Map<String, Thumbnail> thumbnails) {
|
|
|
|
mThumbnails = thumbnails;
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void bindView(View bindView, Context context, Cursor cursor) {
|
|
|
|
String url = "";
|
|
|
|
String title = "";
|
|
|
|
boolean pinned = false;
|
|
|
|
|
|
|
|
// Cursor is already moved to required position.
|
|
|
|
if (!cursor.isAfterLast()) {
|
|
|
|
url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL));
|
|
|
|
title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE));
|
|
|
|
pinned = ((TopSitesCursorWrapper) cursor).isPinned();
|
|
|
|
}
|
|
|
|
|
|
|
|
TopSitesGridItemView view = (TopSitesGridItemView) bindView;
|
|
|
|
view.setTitle(title);
|
|
|
|
view.setUrl(url);
|
|
|
|
view.setPinned(pinned);
|
|
|
|
|
|
|
|
// If there is no url, then show "add bookmark".
|
|
|
|
if (TextUtils.isEmpty(url)) {
|
|
|
|
view.displayThumbnail(R.drawable.top_site_add);
|
|
|
|
} else {
|
|
|
|
// Show the thumbnail.
|
|
|
|
Thumbnail thumbnail = (mThumbnails != null ? mThumbnails.get(url) : null);
|
|
|
|
if (thumbnail == null) {
|
|
|
|
view.displayThumbnail(null);
|
|
|
|
} else if (thumbnail.isThumbnail) {
|
|
|
|
view.displayThumbnail(thumbnail.bitmap);
|
|
|
|
} else {
|
|
|
|
view.displayFavicon(thumbnail.bitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
|
|
|
return new TopSitesGridItemView(context);
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-19 10:53:14 -07:00
|
|
|
private class CursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
|
2013-06-25 09:51:57 -07:00
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
2013-09-30 20:56:09 -07:00
|
|
|
Log.d(LOGTAG, "Creating TopSitesLoader: " + id);
|
2013-09-19 10:53:14 -07:00
|
|
|
return new TopSitesLoader(getActivity());
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
|
|
|
|
2013-09-30 20:56:09 -07:00
|
|
|
/**
|
|
|
|
* This method is called *twice* in some circumstances.
|
|
|
|
*
|
|
|
|
* If you try to avoid that through some kind of boolean flag,
|
|
|
|
* sometimes (e.g., returning to the activity) you'll *not* be called
|
|
|
|
* twice, and thus you'll never draw thumbnails.
|
|
|
|
*
|
|
|
|
* The root cause is TopSitesLoader.loadCursor being called twice.
|
|
|
|
* Why that is... dunno.
|
|
|
|
*/
|
2013-06-25 09:51:57 -07:00
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
|
2013-09-30 20:56:09 -07:00
|
|
|
Log.d(LOGTAG, "onLoadFinished: " + c.getCount() + " rows.");
|
|
|
|
|
2013-09-19 10:53:14 -07:00
|
|
|
mListAdapter.swapCursor(c);
|
|
|
|
mGridAdapter.swapCursor(c);
|
|
|
|
updateUiFromCursor(c);
|
|
|
|
|
2013-09-30 20:56:09 -07:00
|
|
|
final int col = c.getColumnIndexOrThrow(URLColumns.URL);
|
|
|
|
|
2013-09-19 10:53:14 -07:00
|
|
|
// Load the thumbnails.
|
2013-09-30 20:56:09 -07:00
|
|
|
// Even though the cursor we're given is supposed to be fresh,
|
|
|
|
// we get a bad first value unless we reset its position.
|
|
|
|
// Using move(-1) and moveToNext() doesn't work correctly under
|
|
|
|
// rotation, so we use moveToFirst.
|
|
|
|
if (!c.moveToFirst()) {
|
|
|
|
return;
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
2013-09-30 20:56:09 -07:00
|
|
|
|
|
|
|
final ArrayList<String> urls = new ArrayList<String>();
|
|
|
|
int i = 1;
|
|
|
|
do {
|
|
|
|
urls.add(c.getString(col));
|
|
|
|
} while (i++ < mMaxGridEntries && c.moveToNext());
|
|
|
|
|
|
|
|
if (urls.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
bundle.putStringArrayList(THUMBNAILS_URLS_KEY, urls);
|
|
|
|
getLoaderManager().restartLoader(LOADER_ID_THUMBNAILS, bundle, mThumbnailsLoaderCallbacks);
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> loader) {
|
2013-09-19 10:53:14 -07:00
|
|
|
if (mListAdapter != null) {
|
|
|
|
mListAdapter.swapCursor(null);
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
2013-07-26 15:44:29 -07:00
|
|
|
|
2013-09-19 10:53:14 -07:00
|
|
|
if (mGridAdapter != null) {
|
|
|
|
mGridAdapter.swapCursor(null);
|
|
|
|
}
|
2013-09-17 13:24:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An AsyncTaskLoader to load the thumbnails from a cursor.
|
|
|
|
*/
|
|
|
|
private static class ThumbnailsLoader extends AsyncTaskLoader<Map<String, Thumbnail>> {
|
|
|
|
private Map<String, Thumbnail> mThumbnails;
|
|
|
|
private ArrayList<String> mUrls;
|
|
|
|
|
|
|
|
public ThumbnailsLoader(Context context, ArrayList<String> urls) {
|
|
|
|
super(context);
|
|
|
|
mUrls = urls;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Map<String, Thumbnail> loadInBackground() {
|
|
|
|
if (mUrls == null || mUrls.size() == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the DB for thumbnails.
|
|
|
|
final ContentResolver cr = getContext().getContentResolver();
|
|
|
|
final Cursor cursor = BrowserDB.getThumbnailsForUrls(cr, mUrls);
|
|
|
|
|
2013-09-30 20:56:09 -07:00
|
|
|
if (cursor == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final Map<String, Thumbnail> thumbnails = new HashMap<String, Thumbnail>();
|
|
|
|
|
2013-09-17 13:24:48 -07:00
|
|
|
try {
|
2013-09-30 20:56:09 -07:00
|
|
|
final int urlIndex = cursor.getColumnIndexOrThrow(Thumbnails.URL);
|
|
|
|
final int dataIndex = cursor.getColumnIndexOrThrow(Thumbnails.DATA);
|
|
|
|
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
String url = cursor.getString(urlIndex);
|
|
|
|
|
|
|
|
// This should never be null, but if it is...
|
|
|
|
final byte[] b = cursor.getBlob(dataIndex);
|
|
|
|
if (b == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
final Bitmap bitmap = BitmapUtils.decodeByteArray(b);
|
|
|
|
|
|
|
|
// Our thumbnails are never null, so if we get a null decoded
|
|
|
|
// bitmap, it's because we hit an OOM or some other disaster.
|
|
|
|
// Give up immediately rather than hammering on.
|
|
|
|
if (bitmap == null) {
|
|
|
|
Log.w(LOGTAG, "Aborting thumbnail load; decode failed.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
thumbnails.put(url, new Thumbnail(bitmap, true));
|
2013-09-17 13:24:48 -07:00
|
|
|
}
|
|
|
|
} finally {
|
2013-09-30 20:56:09 -07:00
|
|
|
cursor.close();
|
2013-09-17 13:24:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query the DB for favicons for the urls without thumbnails.
|
|
|
|
for (String url : mUrls) {
|
|
|
|
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.scaleImage(bitmap), false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return thumbnails;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void deliverResult(Map<String, Thumbnail> thumbnails) {
|
|
|
|
if (isReset()) {
|
|
|
|
mThumbnails = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mThumbnails = thumbnails;
|
|
|
|
|
|
|
|
if (isStarted()) {
|
|
|
|
super.deliverResult(thumbnails);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStartLoading() {
|
|
|
|
if (mThumbnails != null) {
|
|
|
|
deliverResult(mThumbnails);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (takeContentChanged() || mThumbnails == null) {
|
|
|
|
forceLoad();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStopLoading() {
|
|
|
|
cancelLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled(Map<String, Thumbnail> thumbnails) {
|
|
|
|
mThumbnails = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onReset() {
|
|
|
|
super.onReset();
|
|
|
|
|
|
|
|
// Ensure the loader is stopped.
|
|
|
|
onStopLoading();
|
|
|
|
|
|
|
|
mThumbnails = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loader callbacks for the thumbnails on TopSitesGridView.
|
|
|
|
*/
|
|
|
|
private class ThumbnailsLoaderCallbacks implements LoaderCallbacks<Map<String, Thumbnail>> {
|
|
|
|
@Override
|
|
|
|
public Loader<Map<String, Thumbnail>> onCreateLoader(int id, Bundle args) {
|
|
|
|
return new ThumbnailsLoader(getActivity(), args.getStringArrayList(THUMBNAILS_URLS_KEY));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Map<String, Thumbnail>> loader, Map<String, Thumbnail> thumbnails) {
|
|
|
|
if (mGridAdapter != null) {
|
|
|
|
mGridAdapter.updateThumbnails(thumbnails);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Map<String, Thumbnail>> loader) {
|
|
|
|
if (mGridAdapter != null) {
|
|
|
|
mGridAdapter.updateThumbnails(null);
|
|
|
|
}
|
2013-07-26 15:44:29 -07:00
|
|
|
}
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
2013-07-25 17:06:02 -07:00
|
|
|
}
|