Bug 1068425 - Move TopSitesGridView click listeners to TopSitesPanel. r=lucasr

This commit is contained in:
Brian Nicholson 2014-11-05 12:14:34 -08:00
parent cb11266e0d
commit a4468a5fa9
2 changed files with 66 additions and 117 deletions

View File

@ -5,25 +5,15 @@
package org.mozilla.gecko.home;
import java.util.EnumSet;
import org.mozilla.gecko.R;
import org.mozilla.gecko.Telemetry;
import org.mozilla.gecko.TelemetryContract;
import org.mozilla.gecko.ThumbnailHelper;
import org.mozilla.gecko.db.BrowserContract.TopSites;
import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
import org.mozilla.gecko.util.StringUtils;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
/**
@ -56,12 +46,6 @@ public class TopSitesGridView extends GridView {
// Measured height of this view.
private int mMeasuredHeight;
// On URL open listener.
private OnUrlOpenListener mUrlOpenListener;
// Edit pinned site listener.
private OnEditPinnedSiteListener mEditPinnedSiteListener;
// Context menu info.
private TopSitesGridContextMenuInfo mContextMenuInfo;
@ -90,70 +74,6 @@ public class TopSitesGridView extends GridView {
a.recycle();
}
/**
* {@inheritDoc}
*/
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TopSitesGridItemView item = (TopSitesGridItemView) view;
// Decode "user-entered" URLs before loading them.
String url = StringUtils.decodeUserEnteredUrl(item.getUrl());
int type = item.getType();
// If the url is empty, the user can pin a site.
// If not, navigate to the page given by the url.
if (type != TopSites.TYPE_BLANK) {
if (mUrlOpenListener != null) {
final TelemetryContract.Method method;
if (type == TopSites.TYPE_SUGGESTED) {
method = TelemetryContract.Method.SUGGESTION;
} else {
method = TelemetryContract.Method.GRID_ITEM;
}
Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, method, Integer.toString(position));
mUrlOpenListener.onUrlOpen(url, EnumSet.noneOf(OnUrlOpenListener.Flags.class));
}
} else {
if (mEditPinnedSiteListener != null) {
mEditPinnedSiteListener.onEditPinnedSite(position, "");
}
}
}
});
setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
TopSitesGridItemView item = (TopSitesGridItemView) view;
if (cursor == null || item.getType() == TopSites.TYPE_BLANK) {
mContextMenuInfo = null;
return false;
}
mContextMenuInfo = new TopSitesGridContextMenuInfo(view, position, id);
updateContextMenuFromCursor(mContextMenuInfo, cursor);
return showContextMenuForChild(TopSitesGridView.this);
}
});
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
mUrlOpenListener = null;
mEditPinnedSiteListener = null;
}
@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
mIsHandlingFocusChange = true;
@ -168,9 +88,6 @@ public class TopSitesGridView extends GridView {
}
}
/**
* {@inheritDoc}
*/
@Override
public int getColumnWidth() {
// This method will be called from onMeasure() too.
@ -179,9 +96,6 @@ public class TopSitesGridView extends GridView {
return (getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - totalHorizontalSpacing) / mNumColumns;
}
/**
* {@inheritDoc}
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Sets the padding for this view.
@ -236,35 +150,8 @@ public class TopSitesGridView extends GridView {
return mContextMenuInfo;
}
/*
* Update the fields of a TopSitesGridContextMenuInfo object
* from a cursor.
*
* @param info context menu info object to be updated
* @param cursor used to update the context menu info object
*/
private void updateContextMenuFromCursor(TopSitesGridContextMenuInfo info, Cursor cursor) {
info.url = cursor.getString(cursor.getColumnIndexOrThrow(TopSites.URL));
info.title = cursor.getString(cursor.getColumnIndexOrThrow(TopSites.TITLE));
info.type = cursor.getInt(cursor.getColumnIndexOrThrow(TopSites.TYPE));
info.historyId = cursor.getInt(cursor.getColumnIndexOrThrow(TopSites.HISTORY_ID));
}
/**
* Set an url open listener to be used by this view.
*
* @param listener An url open listener for this view.
*/
public void setOnUrlOpenListener(OnUrlOpenListener listener) {
mUrlOpenListener = listener;
}
/**
* Set an edit pinned site listener to be used by this view.
*
* @param listener An edit pinned site listener for this view.
*/
public void setOnEditPinnedSiteListener(final OnEditPinnedSiteListener listener) {
mEditPinnedSiteListener = listener;
public void setContextMenuInfo(TopSitesGridContextMenuInfo contextMenuInfo) {
mContextMenuInfo = contextMenuInfo;
}
/**

View File

@ -15,6 +15,7 @@ import java.util.List;
import java.util.Map;
import org.mozilla.gecko.GeckoProfile;
import org.mozilla.gecko.GeckoSharedPrefs;
import org.mozilla.gecko.R;
import org.mozilla.gecko.Telemetry;
import org.mozilla.gecko.TelemetryContract;
@ -193,8 +194,69 @@ public class TopSitesPanel extends HomeFragment {
}
});
mGrid.setOnUrlOpenListener(mUrlOpenListener);
mGrid.setOnEditPinnedSiteListener(mEditPinnedSiteListener);
mGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TopSitesGridItemView item = (TopSitesGridItemView) view;
// Decode "user-entered" URLs before loading them.
String url = StringUtils.decodeUserEnteredUrl(item.getUrl());
int type = item.getType();
// If the url is empty, the user can pin a site.
// If not, navigate to the page given by the url.
if (type != TopSites.TYPE_BLANK) {
if (mUrlOpenListener != null) {
final TelemetryContract.Method method;
if (type == TopSites.TYPE_SUGGESTED) {
method = TelemetryContract.Method.SUGGESTION;
} else {
method = TelemetryContract.Method.GRID_ITEM;
}
Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, method, Integer.toString(position));
mUrlOpenListener.onUrlOpen(url, EnumSet.noneOf(OnUrlOpenListener.Flags.class));
}
} else {
if (mEditPinnedSiteListener != null) {
mEditPinnedSiteListener.onEditPinnedSite(position, "");
}
}
}
});
mGrid.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
TopSitesGridItemView item = (TopSitesGridItemView) view;
if (cursor == null || item.getType() == TopSites.TYPE_BLANK) {
mGrid.setContextMenuInfo(null);
return false;
}
TopSitesGridContextMenuInfo contextMenuInfo = new TopSitesGridContextMenuInfo(view, position, id);
updateContextMenuFromCursor(contextMenuInfo, cursor);
mGrid.setContextMenuInfo(contextMenuInfo);
return mGrid.showContextMenuForChild(mGrid);
}
/*
* Update the fields of a TopSitesGridContextMenuInfo object
* from a cursor.
*
* @param info context menu info object to be updated
* @param cursor used to update the context menu info object
*/
private void updateContextMenuFromCursor(TopSitesGridContextMenuInfo info, Cursor cursor) {
info.url = cursor.getString(cursor.getColumnIndexOrThrow(TopSites.URL));
info.title = cursor.getString(cursor.getColumnIndexOrThrow(TopSites.TITLE));
info.type = cursor.getInt(cursor.getColumnIndexOrThrow(TopSites.TYPE));
info.historyId = cursor.getInt(cursor.getColumnIndexOrThrow(TopSites.HISTORY_ID));
}
});
registerForContextMenu(mList);
registerForContextMenu(mGrid);