2011-11-18 10:28:17 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Android code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Vladimir Vukicevic <vladimir@pobox.com>
|
|
|
|
* Wes Johnston <wjohnston@mozilla.com>
|
|
|
|
* Mark Finkle <mfinkle@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2011-12-15 11:53:04 -08:00
|
|
|
import android.app.ActionBar;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.content.Intent;
|
2011-12-20 07:28:12 -08:00
|
|
|
import android.content.ContentResolver;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.content.res.Configuration;
|
2011-12-20 07:28:12 -08:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2011-12-15 11:53:04 -08:00
|
|
|
import android.os.Build;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.Log;
|
2011-12-20 07:28:12 -08:00
|
|
|
import android.view.ContextMenu;
|
|
|
|
import android.view.ContextMenu.ContextMenuInfo;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.view.KeyEvent;
|
2011-12-20 07:28:12 -08:00
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2011-12-14 23:34:54 -08:00
|
|
|
import android.view.inputmethod.EditorInfo;
|
2011-12-20 07:28:12 -08:00
|
|
|
import android.widget.AdapterView;
|
2011-11-24 08:54:39 -08:00
|
|
|
import android.widget.Button;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.widget.EditText;
|
2011-12-26 22:07:46 -08:00
|
|
|
import android.widget.ExpandableListView;
|
2011-11-24 08:54:39 -08:00
|
|
|
import android.widget.ImageButton;
|
2011-12-15 11:53:04 -08:00
|
|
|
import android.widget.RelativeLayout;
|
2011-12-20 07:28:12 -08:00
|
|
|
import android.widget.ListView;
|
2012-01-27 11:58:18 -08:00
|
|
|
import android.widget.TabWidget;
|
2012-01-15 13:51:23 -08:00
|
|
|
import android.widget.Toast;
|
2011-12-20 07:28:12 -08:00
|
|
|
|
2012-02-07 18:15:20 -08:00
|
|
|
import java.net.URLEncoder;
|
2011-12-26 22:07:46 -08:00
|
|
|
import java.util.Map;
|
|
|
|
|
2012-02-09 10:01:57 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
|
2011-12-20 07:28:12 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserDB.URLColumns;
|
|
|
|
import org.mozilla.gecko.db.BrowserDB;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-12-07 17:52:07 -08:00
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
public class AwesomeBar extends Activity implements GeckoEventListener {
|
2011-11-18 10:28:17 -08:00
|
|
|
private static final String LOGTAG = "GeckoAwesomeBar";
|
|
|
|
|
|
|
|
static final String URL_KEY = "url";
|
|
|
|
static final String CURRENT_URL_KEY = "currenturl";
|
|
|
|
static final String TYPE_KEY = "type";
|
2011-12-07 17:52:07 -08:00
|
|
|
static final String SEARCH_KEY = "search";
|
2011-11-18 10:28:17 -08:00
|
|
|
static enum Type { ADD, EDIT };
|
|
|
|
|
|
|
|
private String mType;
|
|
|
|
private AwesomeBarTabs mAwesomeTabs;
|
|
|
|
private AwesomeBarEditText mText;
|
2011-11-24 08:54:39 -08:00
|
|
|
private ImageButton mGoButton;
|
2012-02-07 18:15:20 -08:00
|
|
|
private ContentResolver mResolver;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
Log.d(LOGTAG, "creating awesomebar");
|
|
|
|
|
2012-02-07 18:15:20 -08:00
|
|
|
mResolver = Tabs.getInstance().getContentResolver();
|
|
|
|
|
2011-12-21 00:51:56 -08:00
|
|
|
setContentView(R.layout.awesomebar);
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-12-15 11:53:04 -08:00
|
|
|
if (Build.VERSION.SDK_INT >= 11) {
|
2012-02-13 10:23:09 -08:00
|
|
|
RelativeLayout actionBarLayout = (RelativeLayout) GeckoActionBar.getCustomView(this);
|
2011-12-15 11:53:04 -08:00
|
|
|
mGoButton = (ImageButton) actionBarLayout.findViewById(R.id.awesomebar_button);
|
|
|
|
mText = (AwesomeBarEditText) actionBarLayout.findViewById(R.id.awesomebar_text);
|
|
|
|
} else {
|
|
|
|
mGoButton = (ImageButton) findViewById(R.id.awesomebar_button);
|
|
|
|
mText = (AwesomeBarEditText) findViewById(R.id.awesomebar_text);
|
|
|
|
}
|
|
|
|
|
2012-01-27 11:58:18 -08:00
|
|
|
TabWidget tabWidget = (TabWidget) findViewById(android.R.id.tabs);
|
|
|
|
tabWidget.setDividerDrawable(null);
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
mAwesomeTabs = (AwesomeBarTabs) findViewById(R.id.awesomebar_tabs);
|
|
|
|
mAwesomeTabs.setOnUrlOpenListener(new AwesomeBarTabs.OnUrlOpenListener() {
|
2011-12-07 17:52:05 -08:00
|
|
|
public void onUrlOpen(String url) {
|
2012-02-01 16:25:08 -08:00
|
|
|
openUrlAndFinish(url);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
2011-12-07 17:52:07 -08:00
|
|
|
|
|
|
|
public void onSearch(String engine) {
|
|
|
|
openSearchAndFinish(mText.getText().toString(), engine);
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
});
|
|
|
|
|
2011-11-24 08:54:39 -08:00
|
|
|
mGoButton.setOnClickListener(new Button.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
2012-02-01 16:25:08 -08:00
|
|
|
openUrlAndFinish(mText.getText().toString());
|
2011-11-24 08:54:39 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
Resources resources = getResources();
|
|
|
|
|
|
|
|
int padding[] = { mText.getPaddingLeft(),
|
|
|
|
mText.getPaddingTop(),
|
|
|
|
mText.getPaddingRight(),
|
|
|
|
mText.getPaddingBottom() };
|
|
|
|
|
|
|
|
GeckoStateListDrawable states = new GeckoStateListDrawable();
|
|
|
|
states.initializeFilter(GeckoApp.mBrowserToolbar.getHighlightColor());
|
|
|
|
states.addState(new int[] { android.R.attr.state_focused }, resources.getDrawable(R.drawable.address_bar_url_pressed));
|
|
|
|
states.addState(new int[] { android.R.attr.state_pressed }, resources.getDrawable(R.drawable.address_bar_url_pressed));
|
|
|
|
states.addState(new int[] { }, resources.getDrawable(R.drawable.address_bar_url_default));
|
|
|
|
mText.setBackgroundDrawable(states);
|
|
|
|
|
|
|
|
mText.setPadding(padding[0], padding[1], padding[2], padding[3]);
|
|
|
|
|
|
|
|
Intent intent = getIntent();
|
|
|
|
String currentUrl = intent.getStringExtra(CURRENT_URL_KEY);
|
|
|
|
mType = intent.getStringExtra(TYPE_KEY);
|
|
|
|
if (currentUrl != null) {
|
|
|
|
mText.setText(currentUrl);
|
|
|
|
mText.selectAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
mText.setOnKeyPreImeListener(new AwesomeBarEditText.OnKeyPreImeListener() {
|
|
|
|
public boolean onKeyPreIme(View v, int keyCode, KeyEvent event) {
|
|
|
|
InputMethodManager imm =
|
|
|
|
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
|
2011-12-20 19:44:17 -08:00
|
|
|
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
|
2012-02-01 16:25:08 -08:00
|
|
|
openUrlAndFinish(mText.getText().toString());
|
2011-12-20 19:44:17 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
// If input method is in fullscreen mode, we want to dismiss
|
|
|
|
// it instead of closing awesomebar straight away.
|
|
|
|
if (!imm.isFullscreenMode() && keyCode == KeyEvent.KEYCODE_BACK) {
|
|
|
|
cancelAndFinish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mText.addTextChangedListener(new TextWatcher() {
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count,
|
|
|
|
int after) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before,
|
|
|
|
int count) {
|
2011-11-24 08:54:39 -08:00
|
|
|
String text = s.toString();
|
|
|
|
|
|
|
|
mAwesomeTabs.filter(text);
|
|
|
|
updateGoButton(text);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mText.setOnKeyListener(new View.OnKeyListener() {
|
|
|
|
public boolean onKey(View v, int keyCode, KeyEvent event) {
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_ENTER) {
|
|
|
|
if (event.getAction() != KeyEvent.ACTION_DOWN)
|
|
|
|
return true;
|
|
|
|
|
2012-02-01 16:25:08 -08:00
|
|
|
openUrlAndFinish(mText.getText().toString());
|
2011-11-18 10:28:17 -08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
|
|
|
|
public void onFocusChange(View v, boolean hasFocus) {
|
|
|
|
if (!hasFocus) {
|
|
|
|
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-12-07 17:52:07 -08:00
|
|
|
|
2011-12-20 07:28:12 -08:00
|
|
|
registerForContextMenu(mAwesomeTabs.findViewById(R.id.all_pages_list));
|
|
|
|
registerForContextMenu(mAwesomeTabs.findViewById(R.id.bookmarks_list));
|
|
|
|
registerForContextMenu(mAwesomeTabs.findViewById(R.id.history_list));
|
|
|
|
|
2011-12-07 17:52:07 -08:00
|
|
|
GeckoAppShell.registerGeckoEventListener("SearchEngines:Data", this);
|
|
|
|
GeckoAppShell.sendEventToGecko(new GeckoEvent("SearchEngines:Get", null));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void handleMessage(String event, JSONObject message) {
|
|
|
|
try {
|
|
|
|
if (event.equals("SearchEngines:Data")) {
|
|
|
|
mAwesomeTabs.setSearchEngines(message.getJSONArray("searchEngines"));
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
// do nothing
|
|
|
|
Log.i(LOGTAG, "handleMessage throws " + e + " for message: " + event);
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onConfigurationChanged(Configuration newConfiguration) {
|
|
|
|
super.onConfigurationChanged(newConfiguration);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onSearchRequested() {
|
|
|
|
cancelAndFinish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-13 08:31:35 -08:00
|
|
|
/*
|
|
|
|
* This method tries to guess if the given string could be a search query or URL
|
|
|
|
* Search examples:
|
|
|
|
* foo
|
|
|
|
* foo bar.com
|
|
|
|
* foo http://bar.com
|
|
|
|
*
|
|
|
|
* URL examples
|
|
|
|
* foo.com
|
|
|
|
* foo.c
|
|
|
|
* :foo
|
|
|
|
* http://foo.com bar
|
|
|
|
*/
|
|
|
|
private boolean isSearchUrl(String text) {
|
|
|
|
text = text.trim();
|
|
|
|
if (text.length() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int colon = text.indexOf(':');
|
|
|
|
int dot = text.indexOf('.');
|
|
|
|
int space = text.indexOf(' ');
|
|
|
|
|
|
|
|
// If a space is found before any dot or colon, we assume this is a search query
|
|
|
|
boolean spacedOut = space > -1 && (space < colon || space < dot);
|
|
|
|
|
2011-12-13 16:17:58 -08:00
|
|
|
return spacedOut || (dot == -1 && colon == -1);
|
2011-12-13 08:31:35 -08:00
|
|
|
}
|
|
|
|
|
2011-11-24 08:54:39 -08:00
|
|
|
private void updateGoButton(String text) {
|
|
|
|
if (text.length() == 0) {
|
|
|
|
mGoButton.setVisibility(View.GONE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mGoButton.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
int imageResource = R.drawable.ic_awesomebar_go;
|
2011-12-14 23:34:54 -08:00
|
|
|
int imeAction = EditorInfo.IME_ACTION_GO;
|
|
|
|
if (isSearchUrl(text)) {
|
2011-11-24 08:54:39 -08:00
|
|
|
imageResource = R.drawable.ic_awesomebar_search;
|
2011-12-14 23:34:54 -08:00
|
|
|
imeAction = EditorInfo.IME_ACTION_SEARCH;
|
|
|
|
}
|
2011-11-24 08:54:39 -08:00
|
|
|
mGoButton.setImageResource(imageResource);
|
2011-12-14 23:34:54 -08:00
|
|
|
|
|
|
|
if ((mText.getImeOptions() & EditorInfo.IME_MASK_ACTION) != imeAction) {
|
|
|
|
InputMethodManager imm = (InputMethodManager) mText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
mText.setImeOptions(imeAction);
|
|
|
|
imm.restartInput(mText);
|
|
|
|
}
|
2011-11-24 08:54:39 -08:00
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
private void cancelAndFinish() {
|
|
|
|
setResult(Activity.RESULT_CANCELED);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
2011-12-07 17:52:07 -08:00
|
|
|
private void finishWithResult(Intent intent) {
|
|
|
|
setResult(Activity.RESULT_OK, intent);
|
|
|
|
finish();
|
|
|
|
overridePendingTransition(0, 0);
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
private void openUrlAndFinish(String url) {
|
2012-02-07 18:15:20 -08:00
|
|
|
int index = url.indexOf(' ');
|
|
|
|
if (index != -1) {
|
|
|
|
String keywordUrl = BrowserDB.getUrlForKeyword(mResolver, url.substring(0, index));
|
|
|
|
if (keywordUrl != null && keywordUrl.contains("%s")) {
|
|
|
|
String search = URLEncoder.encode(url.substring(index + 1));
|
|
|
|
url = keywordUrl.replace("%s", search);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
Intent resultIntent = new Intent();
|
|
|
|
resultIntent.putExtra(URL_KEY, url);
|
|
|
|
resultIntent.putExtra(TYPE_KEY, mType);
|
2011-12-07 17:52:07 -08:00
|
|
|
finishWithResult(resultIntent);
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-12-07 17:52:07 -08:00
|
|
|
private void openSearchAndFinish(String url, String engine) {
|
|
|
|
Intent resultIntent = new Intent();
|
|
|
|
resultIntent.putExtra(URL_KEY, url);
|
|
|
|
resultIntent.putExtra(TYPE_KEY, mType);
|
|
|
|
resultIntent.putExtra(SEARCH_KEY, engine);
|
|
|
|
finishWithResult(resultIntent);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
|
// This method is called only if the key event was not handled
|
|
|
|
// by any of the views, which usually means the edit box lost focus
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_BACK ||
|
|
|
|
keyCode == KeyEvent.KEYCODE_MENU ||
|
|
|
|
keyCode == KeyEvent.KEYCODE_SEARCH ||
|
|
|
|
keyCode == KeyEvent.KEYCODE_DPAD_UP ||
|
|
|
|
keyCode == KeyEvent.KEYCODE_DPAD_DOWN ||
|
|
|
|
keyCode == KeyEvent.KEYCODE_DPAD_LEFT ||
|
|
|
|
keyCode == KeyEvent.KEYCODE_DPAD_RIGHT ||
|
2011-11-24 18:19:52 -08:00
|
|
|
keyCode == KeyEvent.KEYCODE_DPAD_CENTER ||
|
2011-12-16 13:19:48 -08:00
|
|
|
keyCode == KeyEvent.KEYCODE_DEL ||
|
|
|
|
keyCode == KeyEvent.KEYCODE_VOLUME_UP ||
|
|
|
|
keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
|
2011-11-18 10:28:17 -08:00
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
|
} else {
|
2011-11-23 23:11:06 -08:00
|
|
|
int selStart = -1;
|
|
|
|
int selEnd = -1;
|
|
|
|
if (mText.hasSelection()) {
|
|
|
|
selStart = mText.getSelectionStart();
|
|
|
|
selEnd = mText.getSelectionEnd();
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
// Return focus to the edit box, and dispatch the event to it
|
|
|
|
mText.requestFocusFromTouch();
|
2011-11-23 23:11:06 -08:00
|
|
|
|
|
|
|
if (selStart >= 0) {
|
|
|
|
// Restore the selection, which gets lost due to the focus switch
|
|
|
|
mText.setSelection(selStart, selEnd);
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
mText.dispatchKeyEvent(event);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-23 11:40:17 -08:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
if (mText != null && mText.getText() != null)
|
|
|
|
updateGoButton(mText.getText().toString());
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
mAwesomeTabs.destroy();
|
2011-12-07 17:52:07 -08:00
|
|
|
GeckoAppShell.unregisterGeckoEventListener("SearchEngines:Data", this);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2011-12-26 22:07:46 -08:00
|
|
|
private Object mContextMenuSubject = null;
|
2011-12-20 07:28:12 -08:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
|
|
|
|
super.onCreateContextMenu(menu, view, menuInfo);
|
|
|
|
ListView list = (ListView) view;
|
2011-12-26 22:07:46 -08:00
|
|
|
Object selectedItem = null;
|
|
|
|
String title = "";
|
|
|
|
|
2012-02-07 17:27:25 -08:00
|
|
|
if (list == findViewById(R.id.all_pages_list)) {
|
|
|
|
if (!(menuInfo instanceof AdapterView.AdapterContextMenuInfo)) {
|
|
|
|
Log.e(LOGTAG, "menuInfo is not AdapterContextMenuInfo");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
|
|
|
|
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;
|
|
|
|
title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE));
|
|
|
|
} else {
|
|
|
|
if (!(menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo)) {
|
2012-01-12 09:59:59 -08:00
|
|
|
Log.e(LOGTAG, "menuInfo is not ExpandableListContextMenuInfo");
|
|
|
|
return;
|
|
|
|
}
|
2012-02-07 17:27:25 -08:00
|
|
|
|
2011-12-26 22:07:46 -08:00
|
|
|
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
|
2012-01-24 09:16:48 -08:00
|
|
|
int childPosition = ExpandableListView.getPackedPositionChild(info.packedPosition);
|
|
|
|
int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
|
2012-01-25 08:58:16 -08:00
|
|
|
|
|
|
|
// Check if long tap is on a header row
|
|
|
|
if (groupPosition < 0 || childPosition < 0)
|
|
|
|
return;
|
|
|
|
|
2012-02-07 17:27:25 -08:00
|
|
|
ExpandableListView exList = (ExpandableListView) list;
|
2011-12-26 22:07:46 -08:00
|
|
|
selectedItem = exList.getExpandableListAdapter().getChild(groupPosition, childPosition);
|
|
|
|
|
2012-02-07 17:27:25 -08:00
|
|
|
if (exList == findViewById(R.id.bookmarks_list)) {
|
|
|
|
// The bookmarks list is backed by a SimpleCursorTreeAdapter
|
|
|
|
Cursor cursor = (Cursor) selectedItem;
|
|
|
|
title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE));
|
|
|
|
} else {
|
|
|
|
// The history list is backed by a SimpleExpandableListAdapter
|
|
|
|
Map map = (Map) selectedItem;
|
|
|
|
title = (String) map.get(URLColumns.TITLE);
|
2012-01-12 09:59:59 -08:00
|
|
|
}
|
2011-12-26 22:07:46 -08:00
|
|
|
}
|
2011-12-20 07:28:12 -08:00
|
|
|
|
2011-12-26 22:07:46 -08:00
|
|
|
if (selectedItem == null || !((selectedItem instanceof Cursor) || (selectedItem instanceof Map))) {
|
|
|
|
mContextMenuSubject = null;
|
2011-12-20 07:28:12 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-26 22:07:46 -08:00
|
|
|
mContextMenuSubject = selectedItem;
|
2011-12-20 07:28:12 -08:00
|
|
|
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.awesomebar_contextmenu, menu);
|
2012-01-15 13:51:23 -08:00
|
|
|
|
2012-02-07 17:27:25 -08:00
|
|
|
if (list != findViewById(R.id.bookmarks_list)) {
|
2012-01-15 13:51:23 -08:00
|
|
|
MenuItem removeBookmarkItem = menu.findItem(R.id.remove_bookmark);
|
|
|
|
removeBookmarkItem.setVisible(false);
|
|
|
|
}
|
2011-12-20 07:28:12 -08:00
|
|
|
|
|
|
|
menu.setHeaderTitle(title);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onContextItemSelected(MenuItem item) {
|
2011-12-26 22:07:46 -08:00
|
|
|
if (mContextMenuSubject == null)
|
2011-12-20 07:28:12 -08:00
|
|
|
return false;
|
|
|
|
|
2012-02-09 10:01:57 -08:00
|
|
|
final int id;
|
2012-02-07 06:47:54 -08:00
|
|
|
final String url;
|
2011-12-26 22:07:46 -08:00
|
|
|
byte[] b = null;
|
|
|
|
String title = "";
|
|
|
|
if (mContextMenuSubject instanceof Cursor) {
|
|
|
|
Cursor cursor = (Cursor)mContextMenuSubject;
|
2012-02-09 10:01:57 -08:00
|
|
|
id = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID));
|
2011-12-26 22:07:46 -08:00
|
|
|
url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL));
|
2012-01-06 12:21:49 -08:00
|
|
|
b = cursor.getBlob(cursor.getColumnIndexOrThrow(URLColumns.FAVICON));
|
2011-12-26 22:07:46 -08:00
|
|
|
title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE));
|
|
|
|
} else if (mContextMenuSubject instanceof Map) {
|
2012-01-05 10:49:33 -08:00
|
|
|
Map map = (Map)mContextMenuSubject;
|
2012-02-09 10:01:57 -08:00
|
|
|
id = -1;
|
2011-12-26 22:07:46 -08:00
|
|
|
url = (String)map.get(URLColumns.URL);
|
|
|
|
b = (byte[]) map.get(URLColumns.FAVICON);
|
|
|
|
title = (String)map.get(URLColumns.TITLE);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mContextMenuSubject = null;
|
2011-12-20 07:28:12 -08:00
|
|
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.open_new_tab: {
|
|
|
|
GeckoApp.mAppContext.loadUrl(url, AwesomeBar.Type.ADD);
|
2012-02-02 07:49:46 -08:00
|
|
|
Toast.makeText(this, R.string.new_tab_opened, Toast.LENGTH_SHORT).show();
|
2011-12-20 07:28:12 -08:00
|
|
|
break;
|
|
|
|
}
|
2012-01-15 13:51:23 -08:00
|
|
|
case R.id.remove_bookmark: {
|
2012-02-07 06:47:54 -08:00
|
|
|
GeckoAppShell.getHandler().post(new Runnable() {
|
|
|
|
public void run() {
|
2012-02-09 10:01:57 -08:00
|
|
|
BrowserDB.removeBookmark(mResolver, id);
|
2012-02-07 06:47:54 -08:00
|
|
|
|
|
|
|
GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
2012-02-07 06:47:56 -08:00
|
|
|
mAwesomeTabs.refreshBookmarks();
|
|
|
|
Toast.makeText(AwesomeBar.this, R.string.bookmark_removed,
|
2012-02-07 06:47:54 -08:00
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2012-01-15 13:51:23 -08:00
|
|
|
break;
|
|
|
|
}
|
2011-12-20 07:28:12 -08:00
|
|
|
case R.id.add_to_launcher: {
|
|
|
|
Bitmap bitmap = null;
|
|
|
|
if (b != null)
|
|
|
|
bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
|
|
|
|
|
|
|
|
GeckoAppShell.createShortcut(title, url, bitmap, "");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R.id.share: {
|
|
|
|
GeckoAppShell.openUriExternal(url, "text/plain", "", "",
|
|
|
|
Intent.ACTION_SEND, title);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return super.onContextItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
public static class AwesomeBarEditText extends EditText {
|
|
|
|
OnKeyPreImeListener mOnKeyPreImeListener;
|
|
|
|
|
|
|
|
public interface OnKeyPreImeListener {
|
|
|
|
public boolean onKeyPreIme(View v, int keyCode, KeyEvent event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public AwesomeBarEditText(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
mOnKeyPreImeListener = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
|
|
|
|
if (mOnKeyPreImeListener != null)
|
|
|
|
return mOnKeyPreImeListener.onKeyPreIme(this, keyCode, event);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOnKeyPreImeListener(OnKeyPreImeListener listener) {
|
|
|
|
mOnKeyPreImeListener = listener;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|