2012-06-18 19:39:13 +00: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;
|
|
|
|
|
2012-07-28 00:53:54 +00:00
|
|
|
import org.mozilla.gecko.AwesomeBar.ContextMenuSubject;
|
|
|
|
import org.mozilla.gecko.db.BrowserDB.URLColumns;
|
2013-04-23 01:03:01 +00:00
|
|
|
import org.mozilla.gecko.gfx.BitmapUtils;
|
2013-05-06 20:13:59 +00:00
|
|
|
import org.mozilla.gecko.widget.FaviconView;
|
2012-07-28 00:53:54 +00:00
|
|
|
|
2012-06-18 19:39:13 +00:00
|
|
|
import android.content.ContentResolver;
|
2012-06-18 19:39:13 +00:00
|
|
|
import android.content.Context;
|
2012-06-18 19:39:13 +00:00
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.text.TextUtils;
|
2012-07-28 00:53:54 +00:00
|
|
|
import android.view.ContextMenu;
|
|
|
|
import android.view.ContextMenu.ContextMenuInfo;
|
2012-06-18 19:39:13 +00:00
|
|
|
import android.view.LayoutInflater;
|
2012-06-18 19:39:13 +00:00
|
|
|
import android.view.View;
|
2012-07-28 00:53:54 +00:00
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2012-06-18 19:39:13 +00:00
|
|
|
import android.widget.ImageView;
|
2012-07-28 00:53:54 +00:00
|
|
|
import android.widget.TextView;
|
2012-06-18 19:39:13 +00:00
|
|
|
|
2013-04-30 17:21:15 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
2012-07-18 00:54:54 +00:00
|
|
|
abstract public class AwesomeBarTab {
|
2012-06-18 19:39:13 +00:00
|
|
|
abstract public String getTag();
|
|
|
|
abstract public int getTitleStringId();
|
2012-06-18 19:39:13 +00:00
|
|
|
abstract public boolean onBackPressed();
|
|
|
|
abstract public ContextMenuSubject getSubject(ContextMenu menu, View view, ContextMenuInfo menuInfo);
|
2012-11-12 18:49:25 +00:00
|
|
|
abstract public View getView();
|
2012-06-18 19:39:13 +00:00
|
|
|
|
2012-07-19 19:08:55 +00:00
|
|
|
protected View mView = null;
|
2012-06-18 19:39:13 +00:00
|
|
|
protected View.OnTouchListener mListListener;
|
|
|
|
private AwesomeBarTabs.OnUrlOpenListener mListener;
|
|
|
|
private LayoutInflater mInflater = null;
|
|
|
|
private ContentResolver mContentResolver = null;
|
|
|
|
private Resources mResources;
|
2012-06-18 19:39:13 +00:00
|
|
|
// FIXME: This value should probably come from a prefs key
|
|
|
|
public static final int MAX_RESULTS = 100;
|
|
|
|
protected Context mContext = null;
|
2013-04-30 17:21:15 +00:00
|
|
|
public static HashMap<String, Integer> sOpenTabs;
|
2012-06-18 19:39:13 +00:00
|
|
|
|
|
|
|
public AwesomeBarTab(Context context) {
|
|
|
|
mContext = context;
|
|
|
|
}
|
|
|
|
|
2013-04-30 17:21:15 +00:00
|
|
|
public void destroy() {
|
|
|
|
sOpenTabs = null;
|
|
|
|
}
|
|
|
|
|
2012-06-18 19:39:13 +00:00
|
|
|
public void setListTouchListener(View.OnTouchListener listener) {
|
|
|
|
mListListener = listener;
|
2012-07-19 19:08:55 +00:00
|
|
|
if (mView != null)
|
|
|
|
mView.setOnTouchListener(mListListener);
|
2012-06-18 19:39:13 +00:00
|
|
|
}
|
|
|
|
|
2012-07-18 00:54:54 +00:00
|
|
|
protected class AwesomeEntryViewHolder {
|
2012-06-18 19:39:13 +00:00
|
|
|
public TextView titleView;
|
|
|
|
public TextView urlView;
|
2013-05-06 20:13:59 +00:00
|
|
|
public FaviconView faviconView;
|
2012-06-18 19:39:13 +00:00
|
|
|
public ImageView bookmarkIconView;
|
|
|
|
}
|
2012-06-18 19:39:13 +00:00
|
|
|
|
|
|
|
protected LayoutInflater getInflater() {
|
|
|
|
if (mInflater == null) {
|
|
|
|
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
}
|
|
|
|
return mInflater;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected AwesomeBarTabs.OnUrlOpenListener getUrlListener() {
|
|
|
|
return mListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void setUrlListener(AwesomeBarTabs.OnUrlOpenListener listener) {
|
|
|
|
mListener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ContentResolver getContentResolver() {
|
|
|
|
if (mContentResolver == null) {
|
|
|
|
mContentResolver = mContext.getContentResolver();
|
|
|
|
}
|
|
|
|
return mContentResolver;
|
|
|
|
}
|
|
|
|
|
2013-04-30 17:21:15 +00:00
|
|
|
private HashMap<String, Integer> getOpenTabs() {
|
|
|
|
if (sOpenTabs == null || sOpenTabs.isEmpty()) {
|
|
|
|
Iterable<Tab> tabs = Tabs.getInstance().getTabsInOrder();
|
|
|
|
sOpenTabs = new HashMap<String, Integer>();
|
|
|
|
for (Tab tab : tabs) {
|
|
|
|
sOpenTabs.put(tab.getURL(), tab.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sOpenTabs;
|
|
|
|
}
|
|
|
|
|
2012-06-18 19:39:13 +00:00
|
|
|
protected Resources getResources() {
|
|
|
|
if (mResources == null) {
|
|
|
|
mResources = mContext.getResources();
|
|
|
|
}
|
|
|
|
return mResources;
|
|
|
|
}
|
|
|
|
|
2013-05-06 20:13:59 +00:00
|
|
|
protected void updateFavicon(FaviconView faviconView, Bitmap bitmap, String key) {
|
|
|
|
faviconView.updateImage(bitmap, key);
|
2012-06-18 19:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void updateTitle(TextView titleView, Cursor cursor) {
|
|
|
|
int titleIndex = cursor.getColumnIndexOrThrow(URLColumns.TITLE);
|
|
|
|
String title = cursor.getString(titleIndex);
|
2013-04-30 17:21:15 +00:00
|
|
|
String url = "";
|
2012-06-18 19:39:13 +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 (TextUtils.isEmpty(title)) {
|
|
|
|
int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL);
|
2013-04-30 17:21:15 +00:00
|
|
|
url = cursor.getString(urlIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTitle(titleView, title, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void updateTitle(TextView titleView, String title, String url) {
|
|
|
|
if (TextUtils.isEmpty(title)) {
|
|
|
|
titleView.setText(url);
|
|
|
|
} else {
|
|
|
|
titleView.setText(title);
|
2012-06-18 19:39:13 +00:00
|
|
|
}
|
2013-04-30 17:21:15 +00:00
|
|
|
}
|
2012-06-18 19:39:13 +00:00
|
|
|
|
2013-04-30 17:21:15 +00:00
|
|
|
public void sendToListener(String url, String title) {
|
|
|
|
AwesomeBarTabs.OnUrlOpenListener listener = getUrlListener();
|
|
|
|
if (listener == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Integer tabId = getOpenTabs().get(url);
|
|
|
|
if (tabId != null) {
|
|
|
|
listener.onSwitchToTab(tabId);
|
|
|
|
} else {
|
|
|
|
listener.onUrlOpen(url, title);
|
|
|
|
}
|
2012-06-18 19:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void updateUrl(TextView urlView, Cursor cursor) {
|
|
|
|
int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL);
|
|
|
|
String url = cursor.getString(urlIndex);
|
2013-04-30 17:21:15 +00:00
|
|
|
updateUrl(urlView, url);
|
|
|
|
}
|
2012-06-18 19:39:13 +00:00
|
|
|
|
2013-04-30 17:21:15 +00:00
|
|
|
protected void updateUrl(TextView urlView, String url) {
|
|
|
|
Integer tabId = getOpenTabs().get(url);
|
|
|
|
if (tabId != null) {
|
|
|
|
urlView.setText(R.string.awesomebar_switch_to_tab);
|
|
|
|
urlView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_awesomebar_tab, 0, 0, 0);
|
|
|
|
} else {
|
|
|
|
urlView.setText(url);
|
|
|
|
urlView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
|
|
|
|
}
|
2012-06-18 19:39:13 +00:00
|
|
|
}
|
2012-06-18 19:39:13 +00:00
|
|
|
|
|
|
|
protected boolean hideSoftInput(View view) {
|
|
|
|
InputMethodManager imm =
|
|
|
|
(InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
|
|
|
|
return imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
|
|
|
|
}
|
2012-06-18 19:39:13 +00:00
|
|
|
}
|