Bug 972351 - Factor URL handling into PanelViewUrlHandler (r=margaret)

This commit is contained in:
Lucas Rocha 2014-02-25 10:41:57 +00:00
parent 26bd62207e
commit f78d688572
4 changed files with 67 additions and 31 deletions

View File

@ -27,20 +27,24 @@ public class PanelGridView extends GridView
private final ViewConfig mViewConfig;
private final PanelViewAdapter mAdapter;
protected OnUrlOpenListener mUrlOpenListener;
private PanelViewUrlHandler mUrlHandler;
public PanelGridView(Context context, ViewConfig viewConfig) {
super(context, null, R.attr.panelGridViewStyle);
mViewConfig = viewConfig;
mUrlHandler = new PanelViewUrlHandler(viewConfig);
mAdapter = new PanelViewAdapter(context, viewConfig.getItemType());
setAdapter(mAdapter);
setOnItemClickListener(new PanelGridItemClickListener());
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
mUrlOpenListener = null;
mUrlHandler.setOnUrlOpenListener(null);
}
@Override
@ -50,26 +54,13 @@ public class PanelGridView extends GridView
@Override
public void setOnUrlOpenListener(OnUrlOpenListener listener) {
mUrlOpenListener = listener;
mUrlHandler.setOnUrlOpenListener(listener);
}
private class PanelGridItemClickListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = mAdapter.getCursor();
if (cursor == null || !cursor.moveToPosition(position)) {
throw new IllegalStateException("Couldn't move cursor to position " + position);
}
int urlIndex = cursor.getColumnIndexOrThrow(HomeItems.URL);
final String url = cursor.getString(urlIndex);
EnumSet<OnUrlOpenListener.Flags> flags = EnumSet.noneOf(OnUrlOpenListener.Flags.class);
if (mViewConfig.getItemHandler() == ItemHandler.INTENT) {
flags.add(OnUrlOpenListener.Flags.OPEN_WITH_INTENT);
}
mUrlOpenListener.onUrlOpen(url, flags);
mUrlHandler.openUrlAtPosition(mAdapter.getCursor(), position);
}
}
}

View File

@ -27,12 +27,17 @@ public class PanelListView extends HomeListView
private final PanelViewAdapter mAdapter;
private final ViewConfig mViewConfig;
private final PanelViewUrlHandler mUrlHandler;
public PanelListView(Context context, ViewConfig viewConfig) {
super(context);
mViewConfig = viewConfig;
mUrlHandler = new PanelViewUrlHandler(viewConfig);
mAdapter = new PanelViewAdapter(context, viewConfig.getItemType());
setAdapter(mAdapter);
setOnItemClickListener(new PanelListItemClickListener());
}
@ -42,23 +47,16 @@ public class PanelListView extends HomeListView
mAdapter.swapCursor(cursor);
}
@Override
public void setOnUrlOpenListener(OnUrlOpenListener listener) {
super.setOnUrlOpenListener(listener);
mUrlHandler.setOnUrlOpenListener(listener);
}
private class PanelListItemClickListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = mAdapter.getCursor();
if (cursor == null || !cursor.moveToPosition(position)) {
throw new IllegalStateException("Couldn't move cursor to position " + position);
}
int urlIndex = cursor.getColumnIndexOrThrow(HomeItems.URL);
final String url = cursor.getString(urlIndex);
EnumSet<OnUrlOpenListener.Flags> flags = EnumSet.noneOf(OnUrlOpenListener.Flags.class);
if (mViewConfig.getItemHandler() == ItemHandler.INTENT) {
flags.add(OnUrlOpenListener.Flags.OPEN_WITH_INTENT);
}
mUrlOpenListener.onUrlOpen(url, flags);
mUrlHandler.openUrlAtPosition(mAdapter.getCursor(), position);
}
}
}

View File

@ -0,0 +1,46 @@
/* -*- 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.db.BrowserContract.HomeItems;
import org.mozilla.gecko.home.HomeConfig.ItemHandler;
import org.mozilla.gecko.home.HomeConfig.ViewConfig;
import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
import android.database.Cursor;
import java.util.EnumSet;
class PanelViewUrlHandler {
private final ViewConfig mViewConfig;
private OnUrlOpenListener mUrlOpenListener;
public PanelViewUrlHandler(ViewConfig viewConfig) {
mViewConfig = viewConfig;
}
public void setOnUrlOpenListener(OnUrlOpenListener listener) {
mUrlOpenListener = listener;
}
public void openUrlAtPosition(Cursor cursor, int position) {
if (cursor == null || !cursor.moveToPosition(position)) {
throw new IllegalStateException("Couldn't move cursor to position " + position);
}
int urlIndex = cursor.getColumnIndexOrThrow(HomeItems.URL);
final String url = cursor.getString(urlIndex);
EnumSet<OnUrlOpenListener.Flags> flags = EnumSet.noneOf(OnUrlOpenListener.Flags.class);
if (mViewConfig.getItemHandler() == ItemHandler.INTENT) {
flags.add(OnUrlOpenListener.Flags.OPEN_WITH_INTENT);
}
if (mUrlOpenListener != null) {
mUrlOpenListener.onUrlOpen(url, flags);
}
}
}

View File

@ -239,6 +239,7 @@ gbjar.sources += [
'home/PanelListView.java',
'home/PanelManager.java',
'home/PanelViewAdapter.java',
'home/PanelViewUrlHandler.java',
'home/PinSiteDialog.java',
'home/ReadingListPanel.java',
'home/SearchEngine.java',