Bug 759041 - Move back and context menu actions to interface. r=lucasr

This commit is contained in:
Wes Johnston 2012-06-18 12:39:13 -07:00
Родитель 2159939d40
Коммит 82ec5654cc
6 изменённых файлов: 257 добавлений и 166 удалений

Просмотреть файл

@ -433,7 +433,7 @@ public class AwesomeBar extends GeckoActivity implements GeckoEventListener {
cancelAndFinish();
}
private class ContextMenuSubject {
static public class ContextMenuSubject {
public int id;
public String url;
public byte[] favicon;
@ -453,87 +453,8 @@ public class AwesomeBar extends GeckoActivity implements GeckoEventListener {
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
ListView list = (ListView) view;
boolean isBookmarksList = (list.getTag() == mAwesomeTabs.mBookmarksTab.getTag());
mContextMenuSubject = null;
if (list.getTag() == mAwesomeTabs.mHistoryTab.getTag()) {
if (!(menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo)) {
Log.e(LOGTAG, "menuInfo is not ExpandableListContextMenuInfo");
return;
}
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int childPosition = ExpandableListView.getPackedPositionChild(info.packedPosition);
int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
// Check if long tap is on a header row
if (groupPosition < 0 || childPosition < 0)
return;
ExpandableListView exList = (ExpandableListView) list;
// The history list is backed by a SimpleExpandableListAdapter
@SuppressWarnings("rawtypes")
Map map = (Map) exList.getExpandableListAdapter().getChild(groupPosition, childPosition);
mContextMenuSubject = new ContextMenuSubject((Integer) map.get(Combined.HISTORY_ID),
(String) map.get(URLColumns.URL),
(byte[]) map.get(URLColumns.FAVICON),
(String) map.get(URLColumns.TITLE),
null);
} else {
if (!(menuInfo instanceof AdapterView.AdapterContextMenuInfo)) {
Log.e(LOGTAG, "menuInfo is not AdapterContextMenuInfo");
return;
}
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
Object selectedItem = list.getItemAtPosition(info.position);
if (!(selectedItem instanceof Cursor)) {
Log.e(LOGTAG, "item at " + info.position + " is not a Cursor");
return;
}
Cursor cursor = (Cursor) selectedItem;
// Don't show the context menu for folders
if (!(isBookmarksList &&
cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks.TYPE)) == Bookmarks.TYPE_FOLDER)) {
String keyword = null;
int keywordCol = cursor.getColumnIndex(URLColumns.KEYWORD);
if (keywordCol != -1)
keyword = cursor.getString(keywordCol);
// Use the bookmark id for the Bookmarks tab and the history id for the Top Sites tab
int id = (isBookmarksList) ? cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID)) :
cursor.getInt(cursor.getColumnIndexOrThrow(Combined.HISTORY_ID));
mContextMenuSubject = new ContextMenuSubject(id,
cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL)),
cursor.getBlob(cursor.getColumnIndexOrThrow(URLColumns.FAVICON)),
cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE)),
keyword);
}
}
if (mContextMenuSubject == null)
return;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.awesomebar_contextmenu, menu);
if (!isBookmarksList) {
menu.findItem(R.id.remove_bookmark).setVisible(false);
menu.findItem(R.id.edit_bookmark).setVisible(false);
// Hide "Remove" item if there isn't a valid history ID
if (mContextMenuSubject.id < 0)
menu.findItem(R.id.remove_history).setVisible(false);
} else {
menu.findItem(R.id.remove_history).setVisible(false);
}
menu.setHeaderTitle(mContextMenuSubject.title);
AwesomeBarTab tab = mAwesomeTabs.getAwesomeBarTabForView(view);
mContextMenuSubject = tab.getSubject(menu, view, menuInfo);
}
@Override

Просмотреть файл

@ -61,9 +61,7 @@ public class AwesomeBarTabs extends TabHost {
private OnUrlOpenListener mUrlOpenListener;
private View.OnTouchListener mListTouchListener;
private AllPagesTab mAllPagesTab;
public BookmarksTab mBookmarksTab;
public HistoryTab mHistoryTab;
private AwesomeBarTab mTabs[];
// FIXME: This value should probably come from a
// prefs key (just like XUL-based fennec)
@ -75,24 +73,30 @@ public class AwesomeBarTabs extends TabHost {
public void onEditSuggestion(String suggestion);
}
// This method checks to see if we're in a bookmark sub-folder. If we are,
// it will go up a level and return true. Otherwise it will return false.
public boolean onBackPressed() {
// If the soft keyboard is visible in the bookmarks or history tab, the user
// must have explictly brought it up, so we should try hiding it instead of
// exiting the activity or going up a bookmarks folder level.
if (getCurrentTabTag().equals(mBookmarksTab.getTag()) || getCurrentTabTag().equals(mHistoryTab.getTag())) {
View tabView = getCurrentTabView();
if (hideSoftInput(tabView))
return true;
private AwesomeBarTab getCurrentAwesomeBarTab() {
String tag = getCurrentTabTag();
return getAwesomeBarTabForTag(tag);
}
public AwesomeBarTab getAwesomeBarTabForView(View view) {
String tag = (String)view.getTag();
return getAwesomeBarTabForTag(tag);
}
public AwesomeBarTab getAwesomeBarTabForTag(String tag) {
for (AwesomeBarTab tab : mTabs) {
if (tag == tab.getTag()) {
return tab;
}
}
return null;
}
// If we're not in the bookmarks tab, we have nothing to do. We should
// also return false if mBookmarksAdapter hasn't been initialized yet.
if (!getCurrentTabTag().equals(mBookmarksTab.getTag()))
return false;
return mBookmarksTab.moveToParentFolder();
public boolean onBackPressed() {
AwesomeBarTab tab = getCurrentAwesomeBarTab();
if (tab == null)
return false;
return tab.onBackPressed();
}
public AwesomeBarTabs(Context context, AttributeSet attrs) {
@ -129,28 +133,27 @@ public class AwesomeBarTabs extends TabHost {
}
};
addAllPagesTab();
addBookmarksTab();
addHistoryTab();
mTabs = new AwesomeBarTab[] {
new AllPagesTab(mContext),
new BookmarksTab(mContext),
new HistoryTab(mContext)
};
for (AwesomeBarTab tab : mTabs) {
addAwesomeTab(tab);
}
// Initialize "App Pages" list with no filter
filter("");
}
private TabSpec addAwesomeTab(String id, int titleId, TabHost.TabContentFactory factory) {
TabSpec tab = getTabSpec(id, titleId);
tab.setContent(factory);
addTab(tab);
return tab;
}
private TabSpec addAwesomeTab(String id, int titleId, int contentId) {
TabSpec tab = getTabSpec(id, titleId);
tab.setContent(contentId);
addTab(tab);
return tab;
private void addAwesomeTab(AwesomeBarTab tab) {
TabSpec tabspec = getTabSpec(tab.getTag(), tab.getTitleStringId());
tabspec.setContent(tab.getFactory());
addTab(tabspec);
tab.setListTouchListener(mListTouchListener);
return;
}
private TabSpec getTabSpec(String id, int titleId) {
@ -170,42 +173,6 @@ public class AwesomeBarTabs extends TabHost {
return tab;
}
private void addAllPagesTab() {
Log.d(LOGTAG, "Creating All Pages tab");
mAllPagesTab = new AllPagesTab(mContext);
addAwesomeTab(mAllPagesTab.getTag(),
mAllPagesTab.getTitleStringId(),
mAllPagesTab.getFactory());
mAllPagesTab.setListTouchListener(mListTouchListener);
}
private void addBookmarksTab() {
Log.d(LOGTAG, "Creating Bookmarks tab");
mBookmarksTab = new BookmarksTab(mContext);
addAwesomeTab(mBookmarksTab.getTag(),
mBookmarksTab.getTitleStringId(),
mBookmarksTab.getFactory());
mBookmarksTab.setListTouchListener(mListTouchListener);
// Only load bookmark list when tab is actually used.
// See OnTabChangeListener above.
}
private void addHistoryTab() {
Log.d(LOGTAG, "Creating History tab");
mHistoryTab = new HistoryTab(mContext);
addAwesomeTab(mHistoryTab.getTag(),
mHistoryTab.getTitleStringId(),
mHistoryTab.getFactory());
mHistoryTab.setListTouchListener(mListTouchListener);
// Only load history list when tab is actually used.
// See OnTabChangeListener above.
}
private boolean hideSoftInput(View view) {
InputMethodManager imm =
(InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
@ -221,15 +188,27 @@ public class AwesomeBarTabs extends TabHost {
public void setOnUrlOpenListener(OnUrlOpenListener listener) {
mUrlOpenListener = listener;
mAllPagesTab.setUrlListener(listener);
mBookmarksTab.setUrlListener(listener);
mHistoryTab.setUrlListener(listener);
for (AwesomeBarTab tab : mTabs) {
tab.setUrlListener(listener);
}
}
public void destroy() {
mAllPagesTab.destroy();
mBookmarksTab.destroy();
mHistoryTab.destroy();
for (AwesomeBarTab tab : mTabs) {
tab.destroy();
}
}
public AllPagesTab getAllPagesTab() {
return (AllPagesTab)getAwesomeBarTabForTag("allPages");
}
public BookmarksTab getBookmarksTab() {
return (BookmarksTab)getAwesomeBarTabForTag("bookmarks");
}
public HistoryTab getHistoryTab() {
return (HistoryTab)getAwesomeBarTabForTag("history");
}
public void filter(String searchTerm) {
@ -237,7 +216,8 @@ public class AwesomeBarTabs extends TabHost {
setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
// Ensure the 'All Pages' tab is selected
setCurrentTabByTag(mAllPagesTab.getTag());
AllPagesTab allPages = getAllPagesTab();
setCurrentTabByTag(allPages.getTag());
// Restore normal focus behavior on tab host
setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
@ -247,7 +227,7 @@ public class AwesomeBarTabs extends TabHost {
getTabWidget().setVisibility(tabsVisibility);
// Perform the actual search
mAllPagesTab.filter(searchTerm);
allPages.filter(searchTerm);
}
/**
@ -257,7 +237,7 @@ public class AwesomeBarTabs extends TabHost {
public void setSuggestions(final ArrayList<String> suggestions) {
GeckoAppShell.getMainHandler().post(new Runnable() {
public void run() {
mAllPagesTab.setSuggestions(suggestions);
getAllPagesTab().setSuggestions(suggestions);
}
});
}
@ -268,12 +248,12 @@ public class AwesomeBarTabs extends TabHost {
public void setSearchEngines(final String suggestEngineName, final JSONArray engines) {
GeckoAppShell.getMainHandler().post(new Runnable() {
public void run() {
mAllPagesTab.setSearchEngines(suggestEngineName, engines);
getAllPagesTab().setSearchEngines(suggestEngineName, engines);
}
});
}
public boolean isInReadingList() {
return mBookmarksTab.isInReadingList();
return getBookmarksTab().isInReadingList();
}
}

Просмотреть файл

@ -33,6 +33,10 @@ import android.graphics.BitmapFactory;
import android.content.Intent;
import android.widget.FilterQueryProvider;
import android.os.SystemClock;
import android.view.MenuInflater;
import android.widget.TabHost;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@ -42,6 +46,7 @@ import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
import org.mozilla.gecko.AwesomeBar.ContextMenuSubject;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.BrowserDB.URLColumns;
import org.mozilla.gecko.db.BrowserContract.Combined;
@ -66,6 +71,10 @@ public class AllPagesTab extends AwesomeBarTab {
mSearchEngines = new ArrayList<SearchEngine>();
}
public boolean onBackPressed() {
return false;
}
public TabContentFactory getFactory() {
return new TabContentFactory() {
public View createTabContent(String tag) {
@ -498,4 +507,54 @@ public class AllPagesTab extends AwesomeBarTab {
bookmarkIconView.setImageResource(R.drawable.ic_awesomebar_star);
}
}
public ContextMenuSubject getSubject(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
ContextMenuSubject subject = null;
if (!(menuInfo instanceof AdapterView.AdapterContextMenuInfo)) {
Log.e(LOGTAG, "menuInfo is not AdapterContextMenuInfo");
return subject;
}
ListView list = (ListView)view;
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
Object selectedItem = list.getItemAtPosition(info.position);
if (!(selectedItem instanceof Cursor)) {
Log.e(LOGTAG, "item at " + info.position + " is not a Cursor");
return subject;
}
Cursor cursor = (Cursor) selectedItem;
// Don't show the context menu for folders
String keyword = null;
int keywordCol = cursor.getColumnIndex(URLColumns.KEYWORD);
if (keywordCol != -1)
keyword = cursor.getString(keywordCol);
// Use the bookmark id for the Bookmarks tab and the history id for the Top Sites tab
int id = cursor.getInt(cursor.getColumnIndexOrThrow(Combined._ID));
subject = new ContextMenuSubject(id,
cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL)),
cursor.getBlob(cursor.getColumnIndexOrThrow(URLColumns.FAVICON)),
cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE)),
keyword);
if (subject == null)
return subject;
MenuInflater inflater = new MenuInflater(mContext);
inflater.inflate(R.menu.awesomebar_contextmenu, menu);
menu.findItem(R.id.remove_bookmark).setVisible(false);
menu.findItem(R.id.edit_bookmark).setVisible(false);
// Hide "Remove" item if there isn't a valid history ID
if (subject.id < 0)
menu.findItem(R.id.remove_history).setVisible(false);
menu.setHeaderTitle(subject.title);
return subject;
}
}

Просмотреть файл

@ -17,15 +17,23 @@ import android.view.View;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.TabHost.TabContentFactory;
import android.view.inputmethod.InputMethodManager;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import org.json.JSONArray;
import org.mozilla.gecko.db.BrowserContract.Combined;
import org.mozilla.gecko.db.BrowserDB.URLColumns;
import org.mozilla.gecko.AwesomeBar.ContextMenuSubject;
abstract public class AwesomeBarTab {
abstract public String getTag();
abstract public int getTitleStringId();
abstract public void destroy();
abstract public TabContentFactory getFactory();
abstract public boolean onBackPressed();
abstract public ContextMenuSubject getSubject(ContextMenu menu, View view, ContextMenuInfo menuInfo);
protected View.OnTouchListener mListListener;
private AwesomeBarTabs.OnUrlOpenListener mListener;
@ -116,4 +124,11 @@ abstract public class AwesomeBarTab {
urlView.setText(url);
}
protected boolean hideSoftInput(View view) {
InputMethodManager imm =
(InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
return imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}

Просмотреть файл

@ -23,9 +23,16 @@ import android.widget.LinearLayout;
import android.os.SystemClock;
import android.util.Pair;
import android.widget.TabHost.TabContentFactory;
import android.view.MenuInflater;
import android.widget.TabHost;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import org.json.JSONArray;
import java.util.LinkedList;
import org.mozilla.gecko.AwesomeBar.ContextMenuSubject;
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.BrowserDB.URLColumns;
@ -95,7 +102,14 @@ public class BookmarksTab extends AwesomeBarTab {
}
public boolean onBackPressed() {
return false;
// If the soft keyboard is visible in the bookmarks or history tab, the user
// must have explictly brought it up, so we should try hiding it instead of
// exiting the activity or going up a bookmarks folder level.
ListView view = getListView();
if (hideSoftInput(view))
return true;
return moveToParentFolder();
}
protected BookmarksListAdapter getCursorAdapter() {
@ -369,4 +383,52 @@ public class BookmarksTab extends AwesomeBarTab {
public boolean isInReadingList() {
return mInReadingList;
}
public ContextMenuSubject getSubject(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
ContextMenuSubject subject = null;
if (!(menuInfo instanceof AdapterView.AdapterContextMenuInfo)) {
Log.e(LOGTAG, "menuInfo is not AdapterContextMenuInfo");
return subject;
}
ListView list = (ListView)view;
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
Object selectedItem = list.getItemAtPosition(info.position);
if (!(selectedItem instanceof Cursor)) {
Log.e(LOGTAG, "item at " + info.position + " is not a Cursor");
return subject;
}
Cursor cursor = (Cursor) selectedItem;
// Don't show the context menu for folders
if (!(cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks.TYPE)) == Bookmarks.TYPE_FOLDER)) {
String keyword = null;
int keywordCol = cursor.getColumnIndex(URLColumns.KEYWORD);
if (keywordCol != -1)
keyword = cursor.getString(keywordCol);
// Use the bookmark id for the Bookmarks tab and the history id for the Top Sites tab
int id = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID));
subject = new ContextMenuSubject(id,
cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL)),
cursor.getBlob(cursor.getColumnIndexOrThrow(URLColumns.FAVICON)),
cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE)),
keyword);
}
if (subject == null)
return subject;
MenuInflater inflater = new MenuInflater(mContext);
inflater.inflate(R.menu.awesomebar_contextmenu, menu);
menu.findItem(R.id.remove_history).setVisible(false);
menu.setHeaderTitle(subject.title);
return subject;
}
}

Просмотреть файл

@ -17,6 +17,7 @@ import android.widget.TextView;
import android.view.View;
import android.widget.ListView;
import android.database.Cursor;
import android.view.MenuInflater;
import android.util.Log;
import android.view.ViewGroup;
import android.graphics.Bitmap;
@ -25,6 +26,8 @@ import android.widget.TabHost.TabContentFactory;
import android.util.Pair;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import java.util.LinkedList;
import java.util.HashMap;
@ -32,6 +35,9 @@ import java.util.Map;
import java.util.List;
import java.util.Date;
import org.json.JSONArray;
import org.mozilla.gecko.AwesomeBar.ContextMenuSubject;
import org.mozilla.gecko.db.BrowserContract.Combined;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.BrowserDB.URLColumns;
@ -104,6 +110,13 @@ public class HistoryTab extends AwesomeBarTab {
}
public boolean onBackPressed() {
// If the soft keyboard is visible in the bookmarks or history tab, the user
// must have explictly brought it up, so we should try hiding it instead of
// exiting the activity or going up a bookmarks folder level.
ListView view = getListView();
if (hideSoftInput(view))
return true;
return false;
}
@ -394,4 +407,45 @@ public class HistoryTab extends AwesomeBarTab {
return true;
}
public ContextMenuSubject getSubject(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
ContextMenuSubject subject = null;
if (!(menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo)) {
Log.e(LOGTAG, "menuInfo is not ExpandableListContextMenuInfo");
return subject;
}
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int childPosition = ExpandableListView.getPackedPositionChild(info.packedPosition);
int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
// Check if long tap is on a header row
if (groupPosition < 0 || childPosition < 0)
return subject;
ExpandableListView exList = (ExpandableListView) view;
// The history list is backed by a SimpleExpandableListAdapter
@SuppressWarnings("rawtypes")
Map map = (Map) exList.getExpandableListAdapter().getChild(groupPosition, childPosition);
subject = new AwesomeBar.ContextMenuSubject((Integer) map.get(Combined.HISTORY_ID),
(String) map.get(URLColumns.URL),
(byte[]) map.get(URLColumns.FAVICON),
(String) map.get(URLColumns.TITLE),
null);
MenuInflater inflater = new MenuInflater(mContext);
inflater.inflate(R.menu.awesomebar_contextmenu, menu);
menu.findItem(R.id.remove_bookmark).setVisible(false);
menu.findItem(R.id.edit_bookmark).setVisible(false);
// Hide "Remove" item if there isn't a valid history ID
if (subject.id < 0)
menu.findItem(R.id.remove_history).setVisible(false);
menu.setHeaderTitle(subject.title);
return subject;
}
}