2011-11-18 18:28:17 +00:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2012-05-21 11:12:37 +00:00
|
|
|
* 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/. */
|
2011-11-18 18:28:17 +00:00
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
2012-05-14 18:44:37 +00:00
|
|
|
import android.database.ContentObserver;
|
2011-11-18 18:28:17 +00:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.LightingColorFilter;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.AsyncTask;
|
2012-01-13 19:08:39 +00:00
|
|
|
import android.os.SystemClock;
|
2012-02-07 19:03:02 +00:00
|
|
|
import android.text.TextUtils;
|
2011-11-18 18:28:17 +00:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Pair;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.inputmethod.InputMethodManager;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ExpandableListView;
|
|
|
|
import android.widget.FilterQueryProvider;
|
|
|
|
import android.widget.ImageView;
|
2012-03-26 21:39:24 +00:00
|
|
|
import android.widget.LinearLayout;
|
2011-11-18 18:28:17 +00:00
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.SimpleCursorAdapter;
|
|
|
|
import android.widget.SimpleExpandableListAdapter;
|
|
|
|
import android.widget.TabHost;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2011-12-08 01:52:07 +00:00
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.IOException;
|
2012-06-05 21:07:14 +00:00
|
|
|
import java.util.ArrayList;
|
2011-11-18 18:28:17 +00:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2011-12-08 01:52:07 +00:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2012-02-23 18:48:47 +00:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
|
2012-04-24 22:59:09 +00:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.Combined;
|
2011-12-02 17:27:56 +00:00
|
|
|
import org.mozilla.gecko.db.BrowserDB;
|
|
|
|
import org.mozilla.gecko.db.BrowserDB.URLColumns;
|
|
|
|
|
2011-11-18 18:28:17 +00:00
|
|
|
public class AwesomeBarTabs extends TabHost {
|
|
|
|
private static final String LOGTAG = "GeckoAwesomeBarTabs";
|
|
|
|
|
|
|
|
private static final String ALL_PAGES_TAB = "all";
|
|
|
|
private static final String BOOKMARKS_TAB = "bookmarks";
|
|
|
|
private static final String HISTORY_TAB = "history";
|
|
|
|
|
|
|
|
private static enum HistorySection { TODAY, YESTERDAY, WEEK, OLDER };
|
|
|
|
|
|
|
|
private Context mContext;
|
2011-12-07 23:12:51 +00:00
|
|
|
private boolean mInflated;
|
2012-03-27 17:05:41 +00:00
|
|
|
private LayoutInflater mInflater;
|
2011-11-18 18:28:17 +00:00
|
|
|
private OnUrlOpenListener mUrlOpenListener;
|
2012-02-15 00:23:06 +00:00
|
|
|
private ContentResolver mContentResolver;
|
2012-05-14 18:44:37 +00:00
|
|
|
private ContentObserver mContentObserver;
|
2012-06-05 21:07:14 +00:00
|
|
|
private SearchEngine mSuggestEngine;
|
|
|
|
private ArrayList<SearchEngine> mSearchEngines;
|
2011-11-18 18:28:17 +00:00
|
|
|
|
2012-02-29 21:26:03 +00:00
|
|
|
private BookmarksQueryTask mBookmarksQueryTask;
|
|
|
|
private HistoryQueryTask mHistoryQueryTask;
|
|
|
|
|
2011-12-08 01:52:07 +00:00
|
|
|
private AwesomeBarCursorAdapter mAllPagesCursorAdapter;
|
2012-02-07 19:03:02 +00:00
|
|
|
private BookmarksListAdapter mBookmarksAdapter;
|
2011-11-18 18:28:17 +00:00
|
|
|
private SimpleExpandableListAdapter mHistoryAdapter;
|
|
|
|
|
2012-06-02 18:23:45 +00:00
|
|
|
private boolean mInReadingList;
|
|
|
|
|
2011-11-18 18:28:17 +00:00
|
|
|
// FIXME: This value should probably come from a
|
|
|
|
// prefs key (just like XUL-based fennec)
|
|
|
|
private static final int MAX_RESULTS = 100;
|
|
|
|
|
|
|
|
public interface OnUrlOpenListener {
|
2011-12-08 01:52:07 +00:00
|
|
|
public void onUrlOpen(String url);
|
2012-06-05 21:07:14 +00:00
|
|
|
public void onSearch(String engine, String text);
|
|
|
|
public void onEditSuggestion(String suggestion);
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
private class AwesomeEntryViewHolder {
|
2012-03-27 17:05:41 +00:00
|
|
|
public TextView titleView;
|
|
|
|
public TextView urlView;
|
|
|
|
public ImageView faviconView;
|
2012-06-07 20:27:39 +00:00
|
|
|
public ImageView bookmarkIconView;
|
2012-03-27 17:05:41 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
private class SearchEntryViewHolder {
|
|
|
|
public FlowLayout suggestionView;
|
|
|
|
public ImageView iconView;
|
|
|
|
public LinearLayout userEnteredView;
|
|
|
|
public TextView userEnteredTextView;
|
|
|
|
}
|
|
|
|
|
2011-11-18 18:28:17 +00:00
|
|
|
private class HistoryListAdapter extends SimpleExpandableListAdapter {
|
|
|
|
public HistoryListAdapter(Context context, List<? extends Map<String, ?>> groupData,
|
|
|
|
int groupLayout, String[] groupFrom, int[] groupTo,
|
2012-03-27 17:05:41 +00:00
|
|
|
List<? extends List<? extends Map<String, ?>>> childData) {
|
2011-11-18 18:28:17 +00:00
|
|
|
|
|
|
|
super(context, groupData, groupLayout, groupFrom, groupTo,
|
2012-03-27 17:05:41 +00:00
|
|
|
childData, -1, new String[] {}, new int[] {});
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
|
|
|
|
View convertView, ViewGroup parent) {
|
2012-06-05 21:07:14 +00:00
|
|
|
AwesomeEntryViewHolder viewHolder = null;
|
2012-03-27 17:05:41 +00:00
|
|
|
|
|
|
|
if (convertView == null) {
|
|
|
|
convertView = mInflater.inflate(R.layout.awesomebar_row, null);
|
2011-11-18 18:28:17 +00:00
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
viewHolder = new AwesomeEntryViewHolder();
|
2012-03-27 17:05:41 +00:00
|
|
|
viewHolder.titleView = (TextView) convertView.findViewById(R.id.title);
|
|
|
|
viewHolder.urlView = (TextView) convertView.findViewById(R.id.url);
|
|
|
|
viewHolder.faviconView = (ImageView) convertView.findViewById(R.id.favicon);
|
2012-06-07 20:27:39 +00:00
|
|
|
viewHolder.bookmarkIconView = (ImageView) convertView.findViewById(R.id.bookmark_icon);
|
2012-03-27 17:05:41 +00:00
|
|
|
|
|
|
|
convertView.setTag(viewHolder);
|
|
|
|
} else {
|
2012-06-05 21:07:14 +00:00
|
|
|
viewHolder = (AwesomeEntryViewHolder) convertView.getTag();
|
2012-03-27 17:05:41 +00:00
|
|
|
}
|
2011-11-18 18:28:17 +00:00
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Map<String,Object> historyItem =
|
|
|
|
(Map<String,Object>) mHistoryAdapter.getChild(groupPosition, childPosition);
|
|
|
|
|
2012-03-27 17:05:41 +00:00
|
|
|
String title = (String) historyItem.get(URLColumns.TITLE);
|
|
|
|
String url = (String) historyItem.get(URLColumns.URL);
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(title))
|
|
|
|
title = url;
|
|
|
|
|
|
|
|
viewHolder.titleView.setText(title);
|
|
|
|
viewHolder.urlView.setText(url);
|
|
|
|
|
2011-12-02 17:27:56 +00:00
|
|
|
byte[] b = (byte[]) historyItem.get(URLColumns.FAVICON);
|
2011-11-18 18:28:17 +00:00
|
|
|
|
|
|
|
if (b == null) {
|
2012-03-27 17:05:41 +00:00
|
|
|
viewHolder.faviconView.setImageDrawable(null);
|
2011-11-18 18:28:17 +00:00
|
|
|
} else {
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
|
2012-03-27 17:05:41 +00:00
|
|
|
viewHolder.faviconView.setImageBitmap(bitmap);
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2012-05-14 18:44:34 +00:00
|
|
|
Integer bookmarkId = (Integer) historyItem.get(Combined.BOOKMARK_ID);
|
2012-06-07 20:27:40 +00:00
|
|
|
Integer display = (Integer) historyItem.get(Combined.DISPLAY);
|
2012-04-24 22:59:30 +00:00
|
|
|
|
|
|
|
// The bookmark id will be 0 (null in database) when the url
|
2012-06-07 20:27:40 +00:00
|
|
|
// is not a bookmark. Reading list items are irrelevant in history
|
|
|
|
// tab. We should never show any sign or them.
|
|
|
|
int visibility = (bookmarkId != 0 && display != Combined.DISPLAY_READER ?
|
|
|
|
View.VISIBLE : View.GONE);
|
|
|
|
|
2012-06-07 20:27:39 +00:00
|
|
|
viewHolder.bookmarkIconView.setVisibility(visibility);
|
2012-06-07 20:27:40 +00:00
|
|
|
viewHolder.bookmarkIconView.setImageResource(R.drawable.ic_awesomebar_star);
|
2012-04-24 22:59:30 +00:00
|
|
|
|
2012-03-27 17:05:41 +00:00
|
|
|
return convertView;
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 18:48:47 +00:00
|
|
|
private class BookmarksListAdapter extends SimpleCursorAdapter {
|
|
|
|
private static final int VIEW_TYPE_ITEM = 0;
|
|
|
|
private static final int VIEW_TYPE_FOLDER = 1;
|
|
|
|
private static final int VIEW_TYPE_COUNT = 2;
|
|
|
|
|
2012-03-07 19:13:03 +00:00
|
|
|
private Resources mResources;
|
2012-02-23 18:48:47 +00:00
|
|
|
private LinkedList<Pair<Integer, String>> mParentStack;
|
2012-03-26 21:39:24 +00:00
|
|
|
private LinearLayout mBookmarksTitleView;
|
2012-02-23 18:48:47 +00:00
|
|
|
|
2012-03-27 17:05:41 +00:00
|
|
|
public BookmarksListAdapter(Context context, Cursor c) {
|
|
|
|
super(context, -1, c, new String[] {}, new int[] {});
|
2012-02-23 18:48:47 +00:00
|
|
|
|
2012-03-07 19:13:03 +00:00
|
|
|
mResources = mContext.getResources();
|
2012-02-07 19:03:02 +00:00
|
|
|
|
2012-02-23 18:48:47 +00:00
|
|
|
// mParentStack holds folder id/title pairs that allow us to navigate
|
|
|
|
// back up the folder heirarchy
|
|
|
|
mParentStack = new LinkedList<Pair<Integer, String>>();
|
|
|
|
|
|
|
|
// Add the root folder to the stack
|
|
|
|
Pair<Integer, String> rootFolder = new Pair<Integer, String>(Bookmarks.FIXED_ROOT_ID, "");
|
2012-02-24 20:12:43 +00:00
|
|
|
mParentStack.addFirst(rootFolder);
|
2012-02-07 19:03:02 +00:00
|
|
|
}
|
|
|
|
|
2012-02-23 18:48:47 +00:00
|
|
|
public void refreshCurrentFolder() {
|
|
|
|
// Cancel any pre-existing async refresh tasks
|
2012-03-26 21:43:35 +00:00
|
|
|
if (mBookmarksQueryTask != null)
|
|
|
|
mBookmarksQueryTask.cancel(false);
|
2012-02-07 19:03:02 +00:00
|
|
|
|
2012-02-23 18:48:47 +00:00
|
|
|
Pair<Integer, String> folderPair = mParentStack.getFirst();
|
2012-06-02 18:23:45 +00:00
|
|
|
mInReadingList = (folderPair.first == Bookmarks.FIXED_READING_LIST_ID);
|
|
|
|
|
2012-03-26 21:43:35 +00:00
|
|
|
mBookmarksQueryTask = new BookmarksQueryTask(folderPair.first, folderPair.second);
|
|
|
|
mBookmarksQueryTask.execute();
|
2012-02-07 19:03:02 +00:00
|
|
|
}
|
|
|
|
|
2012-02-23 18:48:48 +00:00
|
|
|
// Returns false if there is no parent folder to move to
|
|
|
|
public boolean moveToParentFolder() {
|
|
|
|
// If we're already at the root, we can't move to a parent folder
|
|
|
|
if (mParentStack.size() == 1)
|
|
|
|
return false;
|
|
|
|
|
2012-02-24 20:12:43 +00:00
|
|
|
mParentStack.removeFirst();
|
2012-02-23 18:48:47 +00:00
|
|
|
refreshCurrentFolder();
|
2012-02-23 18:48:48 +00:00
|
|
|
return true;
|
2012-02-23 18:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void moveToChildFolder(int folderId, String folderTitle) {
|
|
|
|
Pair<Integer, String> folderPair = new Pair<Integer, String>(folderId, folderTitle);
|
2012-02-24 20:12:43 +00:00
|
|
|
mParentStack.addFirst(folderPair);
|
2012-02-23 18:48:47 +00:00
|
|
|
refreshCurrentFolder();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
Cursor c = getCursor();
|
|
|
|
|
|
|
|
if (c.moveToPosition(position) &&
|
2012-03-20 11:44:32 +00:00
|
|
|
c.getInt(c.getColumnIndexOrThrow(Bookmarks.TYPE)) == Bookmarks.TYPE_FOLDER)
|
2012-02-23 18:48:47 +00:00
|
|
|
return VIEW_TYPE_FOLDER;
|
|
|
|
|
|
|
|
// Default to retuning normal item type
|
|
|
|
return VIEW_TYPE_ITEM;
|
|
|
|
}
|
|
|
|
|
2012-02-07 19:03:02 +00:00
|
|
|
@Override
|
2012-02-23 18:48:47 +00:00
|
|
|
public int getViewTypeCount() {
|
|
|
|
return VIEW_TYPE_COUNT;
|
2012-02-07 19:03:02 +00:00
|
|
|
}
|
|
|
|
|
2012-02-23 18:48:47 +00:00
|
|
|
public String getFolderTitle(int position) {
|
|
|
|
Cursor c = getCursor();
|
|
|
|
if (!c.moveToPosition(position))
|
|
|
|
return "";
|
|
|
|
|
2012-03-07 19:13:03 +00:00
|
|
|
String guid = c.getString(c.getColumnIndexOrThrow(Bookmarks.GUID));
|
|
|
|
|
|
|
|
// If we don't have a special GUID, just return the folder title from the DB.
|
|
|
|
if (guid == null || guid.length() == 12)
|
|
|
|
return c.getString(c.getColumnIndexOrThrow(Bookmarks.TITLE));
|
|
|
|
|
|
|
|
// Use localized strings for special folder names.
|
2012-04-02 18:56:53 +00:00
|
|
|
if (guid.equals(Bookmarks.FAKE_DESKTOP_FOLDER_GUID))
|
|
|
|
return mResources.getString(R.string.bookmarks_folder_desktop);
|
2012-03-07 19:13:03 +00:00
|
|
|
else if (guid.equals(Bookmarks.MENU_FOLDER_GUID))
|
|
|
|
return mResources.getString(R.string.bookmarks_folder_menu);
|
|
|
|
else if (guid.equals(Bookmarks.TOOLBAR_FOLDER_GUID))
|
|
|
|
return mResources.getString(R.string.bookmarks_folder_toolbar);
|
|
|
|
else if (guid.equals(Bookmarks.UNFILED_FOLDER_GUID))
|
|
|
|
return mResources.getString(R.string.bookmarks_folder_unfiled);
|
2012-06-02 18:23:45 +00:00
|
|
|
else if (guid.equals(Bookmarks.READING_LIST_FOLDER_GUID))
|
|
|
|
return mResources.getString(R.string.bookmarks_folder_reading_list);
|
2012-03-07 19:13:03 +00:00
|
|
|
|
|
|
|
// If for some reason we have a folder with a special GUID, but it's not one of
|
|
|
|
// the special folders we expect in the UI, just return the title from the DB.
|
2012-02-23 18:48:47 +00:00
|
|
|
return c.getString(c.getColumnIndexOrThrow(Bookmarks.TITLE));
|
2012-02-07 19:03:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-02-23 18:48:47 +00:00
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
int viewType = getItemViewType(position);
|
2012-06-05 21:07:14 +00:00
|
|
|
AwesomeEntryViewHolder viewHolder = null;
|
2012-02-23 18:48:47 +00:00
|
|
|
|
2012-03-27 17:05:41 +00:00
|
|
|
if (convertView == null) {
|
|
|
|
if (viewType == VIEW_TYPE_ITEM)
|
|
|
|
convertView = mInflater.inflate(R.layout.awesomebar_row, null);
|
|
|
|
else
|
|
|
|
convertView = mInflater.inflate(R.layout.awesomebar_folder_row, null);
|
2012-02-23 18:48:47 +00:00
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
viewHolder = new AwesomeEntryViewHolder();
|
2012-03-27 17:05:41 +00:00
|
|
|
viewHolder.titleView = (TextView) convertView.findViewById(R.id.title);
|
|
|
|
viewHolder.faviconView = (ImageView) convertView.findViewById(R.id.favicon);
|
|
|
|
|
|
|
|
if (viewType == VIEW_TYPE_ITEM)
|
|
|
|
viewHolder.urlView = (TextView) convertView.findViewById(R.id.url);
|
|
|
|
|
|
|
|
convertView.setTag(viewHolder);
|
|
|
|
} else {
|
2012-06-05 21:07:14 +00:00
|
|
|
viewHolder = (AwesomeEntryViewHolder) convertView.getTag();
|
2012-03-27 17:05:41 +00:00
|
|
|
}
|
2012-02-23 18:48:47 +00:00
|
|
|
|
2012-03-27 17:05:41 +00:00
|
|
|
Cursor cursor = getCursor();
|
|
|
|
if (!cursor.moveToPosition(position))
|
|
|
|
throw new IllegalStateException("Couldn't move cursor to position " + position);
|
|
|
|
|
|
|
|
if (viewType == VIEW_TYPE_ITEM) {
|
|
|
|
updateTitle(viewHolder.titleView, cursor);
|
|
|
|
updateUrl(viewHolder.urlView, cursor);
|
2012-03-28 21:14:30 +00:00
|
|
|
updateFavicon(viewHolder.faviconView, cursor);
|
2012-03-27 17:05:41 +00:00
|
|
|
} else {
|
2012-06-02 18:23:45 +00:00
|
|
|
int guidIndex = cursor.getColumnIndexOrThrow(Bookmarks.GUID);
|
|
|
|
String guid = cursor.getString(guidIndex);
|
|
|
|
|
|
|
|
if (guid.equals(Bookmarks.READING_LIST_FOLDER_GUID)) {
|
|
|
|
viewHolder.faviconView.setImageResource(R.drawable.reading_list);
|
|
|
|
} else {
|
|
|
|
viewHolder.faviconView.setImageResource(R.drawable.folder);
|
|
|
|
}
|
|
|
|
|
2012-03-27 17:05:41 +00:00
|
|
|
viewHolder.titleView.setText(getFolderTitle(position));
|
|
|
|
}
|
|
|
|
|
2012-02-23 18:48:47 +00:00
|
|
|
return convertView;
|
|
|
|
}
|
|
|
|
|
2012-03-26 21:43:35 +00:00
|
|
|
public LinearLayout getHeaderView() {
|
2012-03-26 21:39:24 +00:00
|
|
|
return mBookmarksTitleView;
|
|
|
|
}
|
|
|
|
|
2012-03-26 21:43:35 +00:00
|
|
|
public void setHeaderView(LinearLayout titleView) {
|
2012-02-23 18:48:47 +00:00
|
|
|
mBookmarksTitleView = titleView;
|
|
|
|
}
|
2012-02-07 19:03:02 +00:00
|
|
|
}
|
|
|
|
|
2012-02-23 18:48:48 +00:00
|
|
|
// 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() {
|
2012-03-31 00:00:41 +00:00
|
|
|
// 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(BOOKMARKS_TAB) || getCurrentTabTag().equals(HISTORY_TAB)) {
|
|
|
|
View tabView = getCurrentTabView();
|
|
|
|
if (hideSoftInput(tabView))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-29 21:25:41 +00:00
|
|
|
// 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.
|
2012-03-31 00:00:41 +00:00
|
|
|
if (!getCurrentTabTag().equals(BOOKMARKS_TAB) || mBookmarksAdapter == null)
|
2012-02-23 18:48:48 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return mBookmarksAdapter.moveToParentFolder();
|
|
|
|
}
|
2012-03-26 21:43:35 +00:00
|
|
|
|
2011-11-18 18:28:17 +00:00
|
|
|
private class BookmarksQueryTask extends AsyncTask<Void, Void, Cursor> {
|
2012-03-26 21:43:35 +00:00
|
|
|
private int mFolderId;
|
|
|
|
private String mFolderTitle;
|
|
|
|
|
|
|
|
public BookmarksQueryTask() {
|
|
|
|
mFolderId = Bookmarks.FIXED_ROOT_ID;
|
|
|
|
mFolderTitle = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
public BookmarksQueryTask(int folderId, String folderTitle) {
|
|
|
|
mFolderId = folderId;
|
|
|
|
mFolderTitle = folderTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-11-18 18:28:17 +00:00
|
|
|
protected Cursor doInBackground(Void... arg0) {
|
2012-03-26 21:43:35 +00:00
|
|
|
return BrowserDB.getBookmarksInFolder(mContentResolver, mFolderId);
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2012-03-26 21:43:35 +00:00
|
|
|
@Override
|
2012-04-26 20:20:03 +00:00
|
|
|
protected void onPostExecute(final Cursor cursor) {
|
|
|
|
final ListView list = (ListView) findViewById(R.id.bookmarks_list);
|
|
|
|
|
|
|
|
// Hack: force this to the main thread, even though it should already be on it
|
|
|
|
GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
handleBookmarkItemClick(parent, view, position, id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// We need to add the header before we set the adapter, hence make it null
|
|
|
|
list.setAdapter(null);
|
|
|
|
|
|
|
|
if (mBookmarksAdapter == null) {
|
|
|
|
mBookmarksAdapter = new BookmarksListAdapter(mContext, cursor);
|
|
|
|
} else {
|
|
|
|
mBookmarksAdapter.changeCursor(cursor);
|
|
|
|
}
|
2012-03-26 21:43:35 +00:00
|
|
|
|
2012-04-26 20:20:03 +00:00
|
|
|
LinearLayout headerView = mBookmarksAdapter.getHeaderView();
|
|
|
|
if (headerView == null) {
|
|
|
|
headerView = (LinearLayout) mInflater.inflate(R.layout.awesomebar_header_row, null);
|
|
|
|
mBookmarksAdapter.setHeaderView(headerView);
|
|
|
|
}
|
2011-11-18 18:28:17 +00:00
|
|
|
|
2012-04-26 20:20:03 +00:00
|
|
|
// Add/Remove header based on the root folder
|
|
|
|
if (mFolderId == Bookmarks.FIXED_ROOT_ID) {
|
|
|
|
if (list.getHeaderViewsCount() == 1)
|
|
|
|
list.removeHeaderView(headerView);
|
|
|
|
} else {
|
|
|
|
if (list.getHeaderViewsCount() == 0)
|
|
|
|
list.addHeaderView(headerView, null, true);
|
2012-02-23 18:48:47 +00:00
|
|
|
|
2012-04-26 20:20:03 +00:00
|
|
|
((TextView) headerView.findViewById(R.id.title)).setText(mFolderTitle);
|
|
|
|
}
|
2012-03-26 21:43:35 +00:00
|
|
|
|
2012-04-26 20:20:03 +00:00
|
|
|
list.setAdapter(mBookmarksAdapter);
|
|
|
|
}
|
|
|
|
});
|
2012-02-29 21:26:03 +00:00
|
|
|
|
|
|
|
mBookmarksQueryTask = null;
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:28:27 +00:00
|
|
|
private static class GroupList extends LinkedList<Map<String,String>> {
|
|
|
|
private static final long serialVersionUID = 0L;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class ChildrenList extends LinkedList<Map<String,Object>> {
|
|
|
|
private static final long serialVersionUID = 0L;
|
|
|
|
}
|
|
|
|
|
|
|
|
private class HistoryQueryTask extends AsyncTask<Void, Void, Pair<GroupList,List<ChildrenList>>> {
|
2011-11-18 18:28:17 +00:00
|
|
|
private static final long MS_PER_DAY = 86400000;
|
|
|
|
private static final long MS_PER_WEEK = MS_PER_DAY * 7;
|
|
|
|
|
2012-02-14 20:28:27 +00:00
|
|
|
protected Pair<GroupList,List<ChildrenList>> doInBackground(Void... arg0) {
|
2012-02-15 00:23:06 +00:00
|
|
|
Cursor cursor = BrowserDB.getRecentHistory(mContentResolver, MAX_RESULTS);
|
2011-11-18 18:28:17 +00:00
|
|
|
|
|
|
|
Date now = new Date();
|
|
|
|
now.setHours(0);
|
|
|
|
now.setMinutes(0);
|
|
|
|
now.setSeconds(0);
|
|
|
|
|
|
|
|
long today = now.getTime();
|
|
|
|
|
|
|
|
// Split the list of urls into separate date range groups
|
|
|
|
// and show it in an expandable list view.
|
2012-06-05 17:43:22 +00:00
|
|
|
List<ChildrenList> childrenLists = new LinkedList<ChildrenList>();
|
2012-02-14 20:28:27 +00:00
|
|
|
ChildrenList children = null;
|
2012-06-05 17:43:22 +00:00
|
|
|
GroupList groups = new GroupList();
|
2011-11-18 18:28:17 +00:00
|
|
|
HistorySection section = null;
|
|
|
|
|
|
|
|
// Move cursor before the first row in preparation
|
|
|
|
// for the iteration.
|
|
|
|
cursor.moveToPosition(-1);
|
|
|
|
|
|
|
|
// Split the history query results into adapters per time
|
|
|
|
// section (today, yesterday, week, older). Queries on content
|
|
|
|
// Browser content provider don't support limitting the number
|
|
|
|
// of returned rows so we limit it here.
|
|
|
|
while (cursor.moveToNext()) {
|
2011-12-02 17:27:56 +00:00
|
|
|
long time = cursor.getLong(cursor.getColumnIndexOrThrow(URLColumns.DATE_LAST_VISITED));
|
2011-11-18 18:28:17 +00:00
|
|
|
HistorySection itemSection = getSectionForTime(time, today);
|
|
|
|
|
|
|
|
if (section != itemSection) {
|
|
|
|
if (section != null) {
|
|
|
|
groups.add(createGroupItem(section));
|
|
|
|
childrenLists.add(children);
|
|
|
|
}
|
|
|
|
|
|
|
|
section = itemSection;
|
2012-02-14 20:28:27 +00:00
|
|
|
children = new ChildrenList();
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
children.add(createHistoryItem(cursor));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add any remaining section to the list if it hasn't
|
|
|
|
// been added to the list after the loop.
|
|
|
|
if (section != null && children != null) {
|
|
|
|
groups.add(createGroupItem(section));
|
|
|
|
childrenLists.add(children);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the query cursor as we won't use it anymore
|
|
|
|
cursor.close();
|
|
|
|
|
2012-06-05 17:43:22 +00:00
|
|
|
// groups and childrenLists will be empty lists if there's no history
|
|
|
|
return Pair.<GroupList,List<ChildrenList>>create(groups, childrenLists);
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2012-02-14 20:28:27 +00:00
|
|
|
public Map<String,Object> createHistoryItem(Cursor cursor) {
|
2011-11-18 18:28:17 +00:00
|
|
|
Map<String,Object> historyItem = new HashMap<String,Object>();
|
|
|
|
|
2011-12-02 17:27:56 +00:00
|
|
|
String url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL));
|
|
|
|
String title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE));
|
|
|
|
byte[] favicon = cursor.getBlob(cursor.getColumnIndexOrThrow(URLColumns.FAVICON));
|
2012-05-14 18:44:34 +00:00
|
|
|
Integer bookmarkId = cursor.getInt(cursor.getColumnIndexOrThrow(Combined.BOOKMARK_ID));
|
|
|
|
Integer historyId = cursor.getInt(cursor.getColumnIndexOrThrow(Combined.HISTORY_ID));
|
2012-06-07 20:27:40 +00:00
|
|
|
Integer display = cursor.getInt(cursor.getColumnIndexOrThrow(Combined.DISPLAY));
|
2011-11-18 18:28:17 +00:00
|
|
|
|
|
|
|
// Use the URL instead of an empty title for consistency with the normal URL
|
|
|
|
// bar view - this is the equivalent of getDisplayTitle() in Tab.java
|
|
|
|
if (title == null || title.length() == 0)
|
|
|
|
title = url;
|
|
|
|
|
2011-12-02 17:27:56 +00:00
|
|
|
historyItem.put(URLColumns.URL, url);
|
|
|
|
historyItem.put(URLColumns.TITLE, title);
|
2011-11-18 18:28:17 +00:00
|
|
|
|
|
|
|
if (favicon != null)
|
2011-12-02 17:27:56 +00:00
|
|
|
historyItem.put(URLColumns.FAVICON, favicon);
|
2011-11-18 18:28:17 +00:00
|
|
|
|
2012-04-24 22:59:30 +00:00
|
|
|
historyItem.put(Combined.BOOKMARK_ID, bookmarkId);
|
2012-05-14 18:44:34 +00:00
|
|
|
historyItem.put(Combined.HISTORY_ID, historyId);
|
2012-06-07 20:27:40 +00:00
|
|
|
historyItem.put(Combined.DISPLAY, display);
|
2012-04-24 22:59:30 +00:00
|
|
|
|
2011-11-18 18:28:17 +00:00
|
|
|
return historyItem;
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:28:27 +00:00
|
|
|
public Map<String,String> createGroupItem(HistorySection section) {
|
2011-11-18 18:28:17 +00:00
|
|
|
Map<String,String> groupItem = new HashMap<String,String>();
|
|
|
|
|
2011-12-02 17:27:56 +00:00
|
|
|
groupItem.put(URLColumns.TITLE, getSectionName(section));
|
2011-11-18 18:28:17 +00:00
|
|
|
|
|
|
|
return groupItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getSectionName(HistorySection section) {
|
|
|
|
Resources resources = mContext.getResources();
|
|
|
|
|
|
|
|
switch (section) {
|
|
|
|
case TODAY:
|
|
|
|
return resources.getString(R.string.history_today_section);
|
|
|
|
case YESTERDAY:
|
|
|
|
return resources.getString(R.string.history_yesterday_section);
|
|
|
|
case WEEK:
|
|
|
|
return resources.getString(R.string.history_week_section);
|
|
|
|
case OLDER:
|
|
|
|
return resources.getString(R.string.history_older_section);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void expandAllGroups(ExpandableListView historyList) {
|
|
|
|
int groupCount = mHistoryAdapter.getGroupCount();
|
|
|
|
|
|
|
|
for (int i = 0; i < groupCount; i++) {
|
|
|
|
historyList.expandGroup(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private HistorySection getSectionForTime(long time, long today) {
|
|
|
|
long delta = today - time;
|
|
|
|
|
|
|
|
if (delta < 0) {
|
|
|
|
return HistorySection.TODAY;
|
2012-01-13 01:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (delta < MS_PER_DAY) {
|
2011-11-18 18:28:17 +00:00
|
|
|
return HistorySection.YESTERDAY;
|
2012-01-13 01:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (delta < MS_PER_WEEK) {
|
2011-11-18 18:28:17 +00:00
|
|
|
return HistorySection.WEEK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HistorySection.OLDER;
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:28:27 +00:00
|
|
|
protected void onPostExecute(Pair<GroupList,List<ChildrenList>> result) {
|
2011-11-18 18:28:17 +00:00
|
|
|
mHistoryAdapter = new HistoryListAdapter(
|
|
|
|
mContext,
|
|
|
|
result.first,
|
|
|
|
R.layout.awesomebar_header_row,
|
2011-12-02 17:27:56 +00:00
|
|
|
new String[] { URLColumns.TITLE },
|
2011-11-18 18:28:17 +00:00
|
|
|
new int[] { R.id.title },
|
2012-03-27 17:05:41 +00:00
|
|
|
result.second
|
2011-11-18 18:28:17 +00:00
|
|
|
);
|
|
|
|
|
2012-05-14 18:44:37 +00:00
|
|
|
if (mContentObserver == null) {
|
|
|
|
// Register an observer to update the history tab contents if they change.
|
|
|
|
mContentObserver = new ContentObserver(GeckoAppShell.getHandler()) {
|
|
|
|
public void onChange(boolean selfChange) {
|
|
|
|
mHistoryQueryTask = new HistoryQueryTask();
|
|
|
|
mHistoryQueryTask.execute();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
BrowserDB.registerHistoryObserver(mContentResolver, mContentObserver);
|
|
|
|
}
|
|
|
|
|
2011-11-18 18:28:17 +00:00
|
|
|
final ExpandableListView historyList =
|
|
|
|
(ExpandableListView) findViewById(R.id.history_list);
|
|
|
|
|
2012-04-26 20:20:03 +00:00
|
|
|
// Hack: force this to the main thread, even though it should already be on it
|
|
|
|
GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
historyList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
|
|
|
|
public boolean onChildClick(ExpandableListView parent, View view,
|
|
|
|
int groupPosition, int childPosition, long id) {
|
|
|
|
handleHistoryItemClick(groupPosition, childPosition);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// This is to disallow collapsing the expandable groups in the
|
|
|
|
// history expandable list view to mimic simpler sections. We should
|
|
|
|
// Remove this if we decide to allow expanding/collapsing groups.
|
|
|
|
historyList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
|
|
|
|
public boolean onGroupClick(ExpandableListView parent, View v,
|
|
|
|
int groupPosition, long id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
historyList.setAdapter(mHistoryAdapter);
|
|
|
|
expandAllGroups(historyList);
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-02-29 21:26:03 +00:00
|
|
|
mHistoryQueryTask = null;
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-11 22:57:43 +00:00
|
|
|
private interface AwesomeBarItem {
|
|
|
|
public void onClick();
|
|
|
|
}
|
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
private class AwesomeBarCursorAdapter extends SimpleCursorAdapter {
|
|
|
|
private String mSearchTerm;
|
2012-05-11 22:57:43 +00:00
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
private static final int ROW_SEARCH = 0;
|
|
|
|
private static final int ROW_STANDARD = 1;
|
2012-05-11 22:57:43 +00:00
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
private class AwesomeBarCursorItem implements AwesomeBarItem {
|
|
|
|
private Cursor mCursor;
|
2012-05-11 22:57:43 +00:00
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
public AwesomeBarCursorItem(Cursor cursor) {
|
|
|
|
mCursor = cursor;
|
2012-05-11 22:57:43 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
public void onClick() {
|
|
|
|
String url = mCursor.getString(mCursor.getColumnIndexOrThrow(URLColumns.URL));
|
|
|
|
if (mUrlOpenListener != null) {
|
|
|
|
int display = mCursor.getInt(mCursor.getColumnIndexOrThrow(Combined.DISPLAY));
|
|
|
|
if (display == Combined.DISPLAY_READER) {
|
|
|
|
url = getReaderForUrl(url);
|
|
|
|
}
|
2012-05-11 22:57:43 +00:00
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
mUrlOpenListener.onUrlOpen(url);
|
|
|
|
}
|
|
|
|
}
|
2012-05-11 22:57:43 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
private class AwesomeBarSearchEngineItem implements AwesomeBarItem {
|
|
|
|
private String mSearchEngine;
|
2012-05-11 22:57:43 +00:00
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
public AwesomeBarSearchEngineItem(String searchEngine) {
|
|
|
|
mSearchEngine = searchEngine;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onClick() {
|
|
|
|
if (mUrlOpenListener != null)
|
|
|
|
mUrlOpenListener.onSearch(mSearchEngine, mSearchTerm);
|
|
|
|
}
|
|
|
|
}
|
2011-12-08 01:52:07 +00:00
|
|
|
|
2012-03-27 17:05:41 +00:00
|
|
|
public AwesomeBarCursorAdapter(Context context) {
|
|
|
|
super(context, -1, null, new String[] {}, new int[] {});
|
2011-12-08 01:52:07 +00:00
|
|
|
mSearchTerm = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
public void filter(String searchTerm) {
|
|
|
|
mSearchTerm = searchTerm;
|
|
|
|
getFilter().filter(searchTerm);
|
|
|
|
}
|
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
private int getSuggestEngineCount() {
|
|
|
|
return (mSearchTerm.length() == 0 || mSuggestEngine == null) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
2011-12-08 01:52:07 +00:00
|
|
|
// Add the search engines to the number of reported results.
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
final int resultCount = super.getCount();
|
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
// don't show search engines or suggestions if search field is empty
|
2011-12-08 01:52:07 +00:00
|
|
|
if (mSearchTerm.length() == 0)
|
|
|
|
return resultCount;
|
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
return resultCount + mSearchEngines.size() + getSuggestEngineCount();
|
2011-12-08 01:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If an item is part of the cursor result set, return that entry.
|
|
|
|
// Otherwise, return the search engine data.
|
|
|
|
@Override
|
|
|
|
public Object getItem(int position) {
|
2012-06-05 21:07:14 +00:00
|
|
|
int engineIndex = getEngineIndex(position);
|
2011-12-08 01:52:07 +00:00
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
if (engineIndex == -1) {
|
|
|
|
// return awesomebar result
|
|
|
|
position -= getSuggestEngineCount();
|
|
|
|
return new AwesomeBarCursorItem((Cursor) super.getItem(position));
|
2011-12-08 01:52:07 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
// return search engine
|
|
|
|
return new AwesomeBarSearchEngineItem(getEngine(engineIndex).name);
|
|
|
|
}
|
|
|
|
|
|
|
|
private SearchEngine getEngine(int index) {
|
|
|
|
final int suggestEngineCount = getSuggestEngineCount();
|
|
|
|
if (index < suggestEngineCount)
|
|
|
|
return mSuggestEngine;
|
|
|
|
return mSearchEngines.get(index - suggestEngineCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getEngineIndex(int position) {
|
|
|
|
final int resultCount = super.getCount();
|
|
|
|
final int suggestEngineCount = getSuggestEngineCount();
|
|
|
|
|
|
|
|
// return suggest engine index
|
|
|
|
if (position < suggestEngineCount)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// not an engine
|
|
|
|
if (position - suggestEngineCount < resultCount)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// return search engine index
|
|
|
|
return position - resultCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
return getEngineIndex(position) == -1 ? ROW_STANDARD : ROW_SEARCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getViewTypeCount() {
|
|
|
|
// view can be either a standard awesomebar row or a search engine row
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnabled(int position) {
|
|
|
|
// If the suggestion row only contains one item (the user-entered
|
|
|
|
// query), allow the entire row to be clickable; clicking the row
|
|
|
|
// has the same effect as clicking the single suggestion. If the
|
|
|
|
// row contains multiple items, clicking the row will do nothing.
|
|
|
|
int index = getEngineIndex(position);
|
|
|
|
if (index != -1) {
|
|
|
|
return getEngine(index).suggestions.isEmpty();
|
|
|
|
}
|
|
|
|
return true;
|
2011-12-08 01:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
2012-06-05 21:07:14 +00:00
|
|
|
if (getItemViewType(position) == ROW_SEARCH) {
|
|
|
|
SearchEntryViewHolder viewHolder = null;
|
2012-03-27 17:05:41 +00:00
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
if (convertView == null) {
|
|
|
|
convertView = mInflater.inflate(R.layout.awesomebar_suggestion_row, null);
|
2011-12-08 01:52:07 +00:00
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
viewHolder = new SearchEntryViewHolder();
|
|
|
|
viewHolder.suggestionView = (FlowLayout) convertView.findViewById(R.id.suggestion_layout);
|
|
|
|
viewHolder.iconView = (ImageView) convertView.findViewById(R.id.suggestion_icon);
|
|
|
|
viewHolder.userEnteredView = (LinearLayout) convertView.findViewById(R.id.suggestion_user_entered);
|
|
|
|
viewHolder.userEnteredTextView = (TextView) convertView.findViewById(R.id.suggestion_text);
|
2012-03-27 17:05:41 +00:00
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
convertView.setTag(viewHolder);
|
|
|
|
} else {
|
|
|
|
viewHolder = (SearchEntryViewHolder) convertView.getTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
bindSearchEngineView(getEngine(getEngineIndex(position)), viewHolder);
|
2012-03-27 17:05:41 +00:00
|
|
|
} else {
|
2012-06-05 21:07:14 +00:00
|
|
|
AwesomeEntryViewHolder viewHolder = null;
|
2011-12-08 01:52:07 +00:00
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
if (convertView == null) {
|
|
|
|
convertView = mInflater.inflate(R.layout.awesomebar_row, null);
|
|
|
|
|
|
|
|
viewHolder = new AwesomeEntryViewHolder();
|
|
|
|
viewHolder.titleView = (TextView) convertView.findViewById(R.id.title);
|
|
|
|
viewHolder.urlView = (TextView) convertView.findViewById(R.id.url);
|
|
|
|
viewHolder.faviconView = (ImageView) convertView.findViewById(R.id.favicon);
|
2012-06-07 20:27:39 +00:00
|
|
|
viewHolder.bookmarkIconView = (ImageView) convertView.findViewById(R.id.bookmark_icon);
|
2012-06-05 21:07:14 +00:00
|
|
|
|
|
|
|
convertView.setTag(viewHolder);
|
|
|
|
} else {
|
|
|
|
viewHolder = (AwesomeEntryViewHolder) convertView.getTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
position -= getSuggestEngineCount();
|
2012-03-27 17:05:41 +00:00
|
|
|
Cursor cursor = getCursor();
|
|
|
|
if (!cursor.moveToPosition(position))
|
|
|
|
throw new IllegalStateException("Couldn't move cursor to position " + position);
|
|
|
|
|
|
|
|
updateTitle(viewHolder.titleView, cursor);
|
|
|
|
updateUrl(viewHolder.urlView, cursor);
|
|
|
|
updateFavicon(viewHolder.faviconView, cursor);
|
2012-06-07 20:27:39 +00:00
|
|
|
updateBookmarkIcon(viewHolder.bookmarkIconView, cursor);
|
2012-03-27 17:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return convertView;
|
2011-12-08 01:52:07 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
private void bindSearchEngineView(final SearchEngine engine, SearchEntryViewHolder viewHolder) {
|
|
|
|
// when a suggestion is clicked, do a search
|
|
|
|
OnClickListener clickListener = new OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
if (mUrlOpenListener != null) {
|
|
|
|
String suggestion = ((TextView) v.findViewById(R.id.suggestion_text)).getText().toString();
|
|
|
|
mUrlOpenListener.onSearch(engine.name, suggestion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// when a suggestion is long-clicked, copy the suggestion into the URL EditText
|
|
|
|
OnLongClickListener longClickListener = new OnLongClickListener() {
|
|
|
|
public boolean onLongClick(View v) {
|
|
|
|
if (mUrlOpenListener != null) {
|
|
|
|
String suggestion = ((TextView) v.findViewById(R.id.suggestion_text)).getText().toString();
|
|
|
|
mUrlOpenListener.onEditSuggestion(suggestion);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// set the search engine icon (e.g., Google) for the row
|
|
|
|
FlowLayout suggestionView = viewHolder.suggestionView;
|
|
|
|
viewHolder.iconView.setImageDrawable(engine.icon);
|
|
|
|
|
|
|
|
// user-entered search term is first suggestion
|
|
|
|
viewHolder.userEnteredTextView.setText(mSearchTerm);
|
|
|
|
viewHolder.userEnteredView.setOnClickListener(clickListener);
|
|
|
|
|
|
|
|
// add additional suggestions given by this engine
|
|
|
|
int recycledSuggestionCount = suggestionView.getChildCount();
|
|
|
|
int suggestionCount = engine.suggestions.size();
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < suggestionCount; i++) {
|
|
|
|
String suggestion = engine.suggestions.get(i);
|
|
|
|
View suggestionItem = null;
|
|
|
|
|
|
|
|
// reuse suggestion views from recycled view, if possible
|
|
|
|
if (i+1 < recycledSuggestionCount) {
|
|
|
|
suggestionItem = suggestionView.getChildAt(i+1);
|
|
|
|
suggestionItem.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
suggestionItem = mInflater.inflate(R.layout.awesomebar_suggestion_item, null);
|
|
|
|
((ImageView) suggestionItem.findViewById(R.id.suggestion_magnifier)).setVisibility(View.GONE);
|
|
|
|
suggestionView.addView(suggestionItem);
|
|
|
|
}
|
|
|
|
((TextView) suggestionItem.findViewById(R.id.suggestion_text)).setText(suggestion);
|
2011-12-08 01:52:07 +00:00
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
suggestionItem.setOnClickListener(clickListener);
|
|
|
|
suggestionItem.setOnLongClickListener(longClickListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
// hide extra suggestions that have been recycled
|
|
|
|
for (++i; i < recycledSuggestionCount; i++) {
|
|
|
|
suggestionView.getChildAt(i).setVisibility(View.GONE);
|
2011-12-08 01:52:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-18 18:28:17 +00:00
|
|
|
public AwesomeBarTabs(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
|
|
|
|
Log.d(LOGTAG, "Creating AwesomeBarTabs");
|
|
|
|
|
|
|
|
mContext = context;
|
2011-12-07 23:12:51 +00:00
|
|
|
mInflated = false;
|
2012-06-05 21:07:14 +00:00
|
|
|
mSearchEngines = new ArrayList<SearchEngine>();
|
2012-02-15 00:23:06 +00:00
|
|
|
mContentResolver = context.getContentResolver();
|
2012-05-14 18:44:37 +00:00
|
|
|
mContentObserver = null;
|
2012-03-27 17:05:41 +00:00
|
|
|
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2012-06-02 18:23:45 +00:00
|
|
|
|
|
|
|
mInReadingList = false;
|
2011-12-07 23:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
super.onFinishInflate();
|
2011-11-18 18:28:17 +00:00
|
|
|
|
2011-12-07 23:12:51 +00:00
|
|
|
// HACK: Without this, the onFinishInflate is called twice
|
|
|
|
// This issue is due to a bug when Android inflates a layout with a
|
|
|
|
// parent. Fixed in Honeycomb
|
|
|
|
if (mInflated)
|
|
|
|
return;
|
2011-11-18 18:28:17 +00:00
|
|
|
|
2011-12-07 23:12:51 +00:00
|
|
|
mInflated = true;
|
2011-11-18 18:28:17 +00:00
|
|
|
|
|
|
|
// This should be called before adding any tabs
|
|
|
|
// to the TabHost.
|
|
|
|
setup();
|
|
|
|
|
|
|
|
addAllPagesTab();
|
|
|
|
addBookmarksTab();
|
|
|
|
addHistoryTab();
|
|
|
|
|
|
|
|
setOnTabChangedListener(new TabHost.OnTabChangeListener() {
|
|
|
|
public void onTabChanged(String tabId) {
|
2011-11-28 10:34:54 +00:00
|
|
|
boolean hideSoftInput = true;
|
|
|
|
|
2011-11-18 18:28:17 +00:00
|
|
|
// Lazy load bookmarks and history lists. Only query the database
|
|
|
|
// if those lists requested by user.
|
2012-02-29 21:26:03 +00:00
|
|
|
if (tabId.equals(BOOKMARKS_TAB) && mBookmarksAdapter == null
|
|
|
|
&& mBookmarksQueryTask == null) {
|
|
|
|
mBookmarksQueryTask = new BookmarksQueryTask();
|
|
|
|
mBookmarksQueryTask.execute();
|
|
|
|
} else if (tabId.equals(HISTORY_TAB) && mHistoryAdapter == null
|
|
|
|
&& mHistoryQueryTask == null) {
|
|
|
|
mHistoryQueryTask = new HistoryQueryTask();
|
|
|
|
mHistoryQueryTask.execute();
|
2011-11-28 10:34:54 +00:00
|
|
|
} else {
|
|
|
|
hideSoftInput = false;
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2011-11-28 10:34:54 +00:00
|
|
|
// Always dismiss SKB when changing to lazy-loaded tabs
|
|
|
|
if (hideSoftInput) {
|
|
|
|
View tabView = getCurrentTabView();
|
|
|
|
hideSoftInput(tabView);
|
|
|
|
}
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Initialize "App Pages" list with no filter
|
|
|
|
filter("");
|
|
|
|
}
|
|
|
|
|
|
|
|
private TabSpec addAwesomeTab(String id, int titleId, int contentId) {
|
|
|
|
TabSpec tab = newTabSpec(id);
|
|
|
|
|
2012-03-27 17:05:41 +00:00
|
|
|
View indicatorView = mInflater.inflate(R.layout.awesomebar_tab_indicator, null);
|
2011-11-18 18:28:17 +00:00
|
|
|
Drawable background = indicatorView.getBackground();
|
2011-11-30 06:02:55 +00:00
|
|
|
try {
|
2012-05-31 23:11:12 +00:00
|
|
|
background.setColorFilter(new LightingColorFilter(Color.WHITE, 0xFFFF9500));
|
2011-11-30 06:02:55 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.d(LOGTAG, "background.setColorFilter failed " + e);
|
|
|
|
}
|
2011-11-18 18:28:17 +00:00
|
|
|
TextView title = (TextView) indicatorView.findViewById(R.id.title);
|
|
|
|
title.setText(titleId);
|
|
|
|
|
|
|
|
tab.setIndicator(indicatorView);
|
|
|
|
tab.setContent(contentId);
|
|
|
|
|
|
|
|
addTab(tab);
|
|
|
|
|
|
|
|
return tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addAllPagesTab() {
|
|
|
|
Log.d(LOGTAG, "Creating All Pages tab");
|
|
|
|
|
|
|
|
addAwesomeTab(ALL_PAGES_TAB,
|
|
|
|
R.string.awesomebar_all_pages_title,
|
|
|
|
R.id.all_pages_list);
|
|
|
|
|
|
|
|
// Load the list using a custom adapter so we can create the bitmaps
|
2012-03-27 17:05:41 +00:00
|
|
|
mAllPagesCursorAdapter = new AwesomeBarCursorAdapter(mContext);
|
2011-11-18 18:28:17 +00:00
|
|
|
|
2011-12-08 01:52:07 +00:00
|
|
|
mAllPagesCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
|
2011-11-18 18:28:17 +00:00
|
|
|
public Cursor runQuery(CharSequence constraint) {
|
2012-01-13 19:08:39 +00:00
|
|
|
long start = SystemClock.uptimeMillis();
|
2012-01-11 18:53:39 +00:00
|
|
|
|
2012-02-15 00:23:06 +00:00
|
|
|
Cursor c = BrowserDB.filter(mContentResolver, constraint, MAX_RESULTS);
|
2012-01-11 18:53:39 +00:00
|
|
|
c.getCount(); // ensure the query runs at least once
|
|
|
|
|
2012-01-13 19:08:39 +00:00
|
|
|
long end = SystemClock.uptimeMillis();
|
2012-01-11 18:53:39 +00:00
|
|
|
Log.i(LOGTAG, "Got cursor in " + (end - start) + "ms");
|
|
|
|
|
|
|
|
return c;
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
final ListView allPagesList = (ListView) findViewById(R.id.all_pages_list);
|
|
|
|
|
|
|
|
allPagesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
2012-05-11 22:57:43 +00:00
|
|
|
((AwesomeBarItem) allPagesList.getItemAtPosition(position)).onClick();
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-12-08 01:52:07 +00:00
|
|
|
allPagesList.setAdapter(mAllPagesCursorAdapter);
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addBookmarksTab() {
|
|
|
|
Log.d(LOGTAG, "Creating Bookmarks tab");
|
|
|
|
|
|
|
|
addAwesomeTab(BOOKMARKS_TAB,
|
|
|
|
R.string.awesomebar_bookmarks_title,
|
|
|
|
R.id.bookmarks_list);
|
|
|
|
|
|
|
|
ListView bookmarksList = (ListView) findViewById(R.id.bookmarks_list);
|
|
|
|
|
|
|
|
// Only load bookmark list when tab is actually used.
|
|
|
|
// See OnTabChangeListener above.
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addHistoryTab() {
|
|
|
|
Log.d(LOGTAG, "Creating History tab");
|
|
|
|
|
|
|
|
addAwesomeTab(HISTORY_TAB,
|
|
|
|
R.string.awesomebar_history_title,
|
|
|
|
R.id.history_list);
|
|
|
|
|
|
|
|
ListView historyList = (ListView) findViewById(R.id.history_list);
|
|
|
|
|
|
|
|
// Only load history list when tab is actually used.
|
|
|
|
// See OnTabChangeListener above.
|
|
|
|
}
|
|
|
|
|
2012-03-31 00:00:41 +00:00
|
|
|
private boolean hideSoftInput(View view) {
|
2011-11-18 18:28:17 +00:00
|
|
|
InputMethodManager imm =
|
|
|
|
(InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
|
2012-03-31 00:00:41 +00:00
|
|
|
return imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-02 18:23:45 +00:00
|
|
|
private String getReaderForUrl(String url) {
|
|
|
|
// FIXME: still need to define the final way to open items from
|
|
|
|
// reading list. For now, we're using an about:reader page.
|
|
|
|
return "about:reader?url=" + url;
|
|
|
|
}
|
|
|
|
|
2012-03-26 21:39:24 +00:00
|
|
|
private void handleBookmarkItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
int headerCount = ((ListView) parent).getHeaderViewsCount();
|
2012-03-15 17:01:11 +00:00
|
|
|
// If we tap on the header view, there's nothing to do
|
2012-03-26 21:39:24 +00:00
|
|
|
if (headerCount == 1 && position == 0)
|
2012-02-23 18:48:47 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
Cursor cursor = mBookmarksAdapter.getCursor();
|
|
|
|
// The header view takes up a spot in the list
|
2012-03-26 21:39:24 +00:00
|
|
|
if (headerCount == 1)
|
|
|
|
position--;
|
|
|
|
|
|
|
|
cursor.moveToPosition(position);
|
2012-02-07 19:03:02 +00:00
|
|
|
|
2012-03-20 11:44:32 +00:00
|
|
|
int type = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks.TYPE));
|
|
|
|
if (type == Bookmarks.TYPE_FOLDER) {
|
2012-02-23 18:48:47 +00:00
|
|
|
// If we're clicking on a folder, update mBookmarksAdapter to move to that folder
|
|
|
|
int folderId = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID));
|
2012-03-26 21:39:24 +00:00
|
|
|
String folderTitle = mBookmarksAdapter.getFolderTitle(position);
|
2012-02-23 18:48:47 +00:00
|
|
|
|
|
|
|
mBookmarksAdapter.moveToChildFolder(folderId, folderTitle);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, just open the URL
|
|
|
|
String url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL));
|
2012-06-02 18:23:45 +00:00
|
|
|
if (mUrlOpenListener != null) {
|
|
|
|
if (mInReadingList) {
|
|
|
|
url = getReaderForUrl(url);
|
|
|
|
}
|
|
|
|
|
2012-02-07 19:03:02 +00:00
|
|
|
mUrlOpenListener.onUrlOpen(url);
|
2012-06-02 18:23:45 +00:00
|
|
|
}
|
2012-02-07 19:03:02 +00:00
|
|
|
}
|
|
|
|
|
2011-11-18 18:28:17 +00:00
|
|
|
private void handleHistoryItemClick(int groupPosition, int childPosition) {
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Map<String,Object> historyItem =
|
|
|
|
(Map<String,Object>) mHistoryAdapter.getChild(groupPosition, childPosition);
|
|
|
|
|
2011-12-02 17:27:56 +00:00
|
|
|
String url = (String) historyItem.get(URLColumns.URL);
|
2011-11-18 18:28:17 +00:00
|
|
|
|
|
|
|
if (mUrlOpenListener != null)
|
2011-12-08 01:52:05 +00:00
|
|
|
mUrlOpenListener.onUrlOpen(url);
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2012-03-27 17:05:41 +00:00
|
|
|
private void updateFavicon(ImageView faviconView, Cursor cursor) {
|
|
|
|
byte[] b = cursor.getBlob(cursor.getColumnIndexOrThrow(URLColumns.FAVICON));
|
|
|
|
if (b == null) {
|
|
|
|
faviconView.setImageDrawable(null);
|
|
|
|
} else {
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
|
|
|
|
faviconView.setImageBitmap(bitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateTitle(TextView titleView, Cursor cursor) {
|
|
|
|
int titleIndex = cursor.getColumnIndexOrThrow(URLColumns.TITLE);
|
|
|
|
String title = cursor.getString(titleIndex);
|
|
|
|
|
|
|
|
// Use the URL instead of an empty title for consistency with the normal URL
|
|
|
|
// bar view - this is the equivalent of getDisplayTitle() in Tab.java
|
|
|
|
if (TextUtils.isEmpty(title)) {
|
|
|
|
int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL);
|
|
|
|
title = cursor.getString(urlIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
titleView.setText(title);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateUrl(TextView urlView, Cursor cursor) {
|
|
|
|
int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL);
|
|
|
|
String url = cursor.getString(urlIndex);
|
|
|
|
|
|
|
|
urlView.setText(url);
|
|
|
|
}
|
|
|
|
|
2012-06-07 20:27:39 +00:00
|
|
|
private void updateBookmarkIcon(ImageView bookmarkIconView, Cursor cursor) {
|
2012-04-24 22:59:09 +00:00
|
|
|
int bookmarkIdIndex = cursor.getColumnIndexOrThrow(Combined.BOOKMARK_ID);
|
|
|
|
long id = cursor.getLong(bookmarkIdIndex);
|
|
|
|
|
2012-06-07 20:27:40 +00:00
|
|
|
int displayIndex = cursor.getColumnIndexOrThrow(Combined.DISPLAY);
|
|
|
|
int display = cursor.getInt(displayIndex);
|
|
|
|
|
2012-04-24 22:59:09 +00:00
|
|
|
// The bookmark id will be 0 (null in database) when the url
|
|
|
|
// is not a bookmark.
|
|
|
|
int visibility = (id == 0 ? View.GONE : View.VISIBLE);
|
2012-06-07 20:27:39 +00:00
|
|
|
bookmarkIconView.setVisibility(visibility);
|
2012-06-07 20:27:40 +00:00
|
|
|
|
|
|
|
if (display == Combined.DISPLAY_READER) {
|
|
|
|
bookmarkIconView.setImageResource(R.drawable.ic_awesomebar_reader);
|
|
|
|
} else {
|
|
|
|
bookmarkIconView.setImageResource(R.drawable.ic_awesomebar_star);
|
|
|
|
}
|
2012-04-24 22:59:09 +00:00
|
|
|
}
|
|
|
|
|
2011-11-18 18:28:17 +00:00
|
|
|
public void setOnUrlOpenListener(OnUrlOpenListener listener) {
|
|
|
|
mUrlOpenListener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void destroy() {
|
2011-12-08 01:52:07 +00:00
|
|
|
Cursor allPagesCursor = mAllPagesCursorAdapter.getCursor();
|
2011-11-18 18:28:17 +00:00
|
|
|
if (allPagesCursor != null)
|
|
|
|
allPagesCursor.close();
|
|
|
|
|
|
|
|
if (mBookmarksAdapter != null) {
|
|
|
|
Cursor bookmarksCursor = mBookmarksAdapter.getCursor();
|
|
|
|
if (bookmarksCursor != null)
|
|
|
|
bookmarksCursor.close();
|
|
|
|
}
|
2012-05-14 18:44:37 +00:00
|
|
|
|
|
|
|
if (mContentObserver != null)
|
|
|
|
BrowserDB.unregisterContentObserver(mContentResolver, mContentObserver);
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void filter(String searchTerm) {
|
|
|
|
// Don't let the tab's content steal focus on tab switch
|
|
|
|
setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
|
|
|
|
|
|
|
|
// Ensure the 'All Pages' tab is selected
|
|
|
|
setCurrentTabByTag(ALL_PAGES_TAB);
|
|
|
|
|
|
|
|
// Restore normal focus behavior on tab host
|
|
|
|
setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
|
|
|
|
|
|
|
|
// The tabs should only be visible if there's no on-going search
|
|
|
|
int tabsVisibility = (searchTerm.length() == 0 ? View.VISIBLE : View.GONE);
|
|
|
|
getTabWidget().setVisibility(tabsVisibility);
|
|
|
|
|
|
|
|
// Perform the actual search
|
2011-12-08 01:52:07 +00:00
|
|
|
mAllPagesCursorAdapter.filter(searchTerm);
|
|
|
|
}
|
|
|
|
|
2012-06-05 21:07:14 +00:00
|
|
|
private Drawable getDrawableFromDataURI(String dataURI) {
|
|
|
|
String base64 = dataURI.substring(dataURI.indexOf(',') + 1);
|
|
|
|
Drawable drawable = null;
|
|
|
|
try {
|
|
|
|
byte[] bytes = GeckoAppShell.decodeBase64(base64, GeckoAppShell.BASE64_DEFAULT);
|
|
|
|
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
|
|
|
|
drawable = Drawable.createFromStream(stream, "src");
|
|
|
|
stream.close();
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
Log.i(LOGTAG, "exception while decoding drawable: " + base64, e);
|
|
|
|
} catch (IOException e) { }
|
|
|
|
return drawable;
|
|
|
|
}
|
|
|
|
|
|
|
|
private class SearchEngine {
|
|
|
|
public String name;
|
|
|
|
public Drawable icon;
|
|
|
|
public ArrayList<String> suggestions;
|
|
|
|
|
|
|
|
public SearchEngine(String name, Drawable icon) {
|
|
|
|
this.name = name;
|
|
|
|
this.icon = icon;
|
|
|
|
this.suggestions = new ArrayList<String>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets suggestions associated with the current suggest engine.
|
|
|
|
* If there is no suggest engine, this does nothing.
|
|
|
|
*/
|
|
|
|
public void setSuggestions(final ArrayList<String> suggestions) {
|
|
|
|
GeckoAppShell.getMainHandler().post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
if (mSuggestEngine != null) {
|
|
|
|
mSuggestEngine.suggestions = suggestions;
|
|
|
|
mAllPagesCursorAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets search engines to be shown for user-entered queries.
|
|
|
|
*/
|
2012-06-12 17:03:14 +00:00
|
|
|
public void setSearchEngines(String suggestEngineName, JSONArray engines) {
|
2012-06-05 21:07:14 +00:00
|
|
|
final ArrayList<SearchEngine> searchEngines = new ArrayList<SearchEngine>();
|
2012-06-12 17:03:14 +00:00
|
|
|
SearchEngine suggestEngine = null;
|
2012-06-05 21:07:14 +00:00
|
|
|
for (int i = 0; i < engines.length(); i++) {
|
|
|
|
try {
|
|
|
|
JSONObject engineJSON = engines.getJSONObject(i);
|
|
|
|
String name = engineJSON.getString("name");
|
|
|
|
String iconURI = engineJSON.getString("iconURI");
|
|
|
|
Drawable icon = getDrawableFromDataURI(iconURI);
|
2012-06-12 17:03:14 +00:00
|
|
|
if (name.equals(suggestEngineName)) {
|
|
|
|
suggestEngine = new SearchEngine(name, icon);
|
2012-06-05 21:07:14 +00:00
|
|
|
} else {
|
|
|
|
searchEngines.add(new SearchEngine(name, icon));
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(LOGTAG, "Error getting search engine JSON", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-12 17:03:14 +00:00
|
|
|
final SearchEngine suggestEngineArg = suggestEngine;
|
2011-12-08 01:52:07 +00:00
|
|
|
GeckoAppShell.getMainHandler().post(new Runnable() {
|
|
|
|
public void run() {
|
2012-06-12 17:03:14 +00:00
|
|
|
mSuggestEngine = suggestEngineArg;
|
2012-06-05 21:07:14 +00:00
|
|
|
mSearchEngines = searchEngines;
|
2011-12-08 01:52:07 +00:00
|
|
|
mAllPagesCursorAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
});
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
2012-05-25 21:03:53 +00:00
|
|
|
|
2012-06-02 18:23:45 +00:00
|
|
|
public boolean isInReadingList() {
|
|
|
|
return mInReadingList;
|
|
|
|
}
|
|
|
|
|
2012-05-25 21:03:53 +00:00
|
|
|
@Override
|
|
|
|
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
2012-06-05 21:07:14 +00:00
|
|
|
// we should only have to hide the soft keyboard once - when the user
|
|
|
|
// initially touches the screen
|
|
|
|
if (ev.getAction() == MotionEvent.ACTION_DOWN)
|
|
|
|
hideSoftInput(this);
|
2012-05-25 21:03:53 +00:00
|
|
|
|
|
|
|
// the android docs make no sense, but returning false will cause this and other
|
|
|
|
// motion events to be sent to the view the user tapped on
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|