зеркало из https://github.com/mozilla/gecko-dev.git
Bug 695444 - Form history autocomplete. r=mfinkle a=android-only
This commit is contained in:
Родитель
bfecd50dfc
Коммит
172c1f9c60
|
@ -0,0 +1,154 @@
|
|||
/* -*- 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):
|
||||
* Margaret Leibovic <margaret.leibovic@gmail.com>
|
||||
* Sriram Ramasubramanian <sriram@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 org.mozilla.gecko.gfx.FloatSize;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.view.animation.Animation;
|
||||
import android.view.animation.AnimationUtils;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.AdapterView.OnItemClickListener;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.RelativeLayout.LayoutParams;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
||||
public class AutoCompletePopup extends ListView {
|
||||
private Context mContext;
|
||||
private RelativeLayout.LayoutParams mLayout;
|
||||
|
||||
private int mWidth;
|
||||
private int mHeight;
|
||||
|
||||
private Animation mAnimation;
|
||||
|
||||
private static final String LOGTAG = "AutoCompletePopup";
|
||||
|
||||
public AutoCompletePopup(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
mContext = context;
|
||||
|
||||
mAnimation = AnimationUtils.loadAnimation(context, R.anim.grow_fade_in);
|
||||
mAnimation.setDuration(75);
|
||||
|
||||
setFocusable(false);
|
||||
|
||||
setOnItemClickListener(new OnItemClickListener() {
|
||||
public void onItemClick(AdapterView<?> parentView, View view, int position, long id) {
|
||||
String value = ((TextView) view).getText().toString();
|
||||
GeckoAppShell.sendEventToGecko(new GeckoEvent("FormAssist:AutoComplete", value));
|
||||
hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void show(JSONArray suggestions, JSONArray rect, double zoom) {
|
||||
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, R.layout.autocomplete_list_item);
|
||||
for (int i = 0; i < suggestions.length(); i++) {
|
||||
try {
|
||||
adapter.add(suggestions.get(i).toString());
|
||||
} catch (JSONException e) {
|
||||
Log.i(LOGTAG, "JSONException: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
setAdapter(adapter);
|
||||
|
||||
if (isShown())
|
||||
return;
|
||||
|
||||
setVisibility(View.VISIBLE);
|
||||
startAnimation(mAnimation);
|
||||
|
||||
if (mLayout == null) {
|
||||
mLayout = (RelativeLayout.LayoutParams) getLayoutParams();
|
||||
mWidth = mLayout.width;
|
||||
mHeight = mLayout.height;
|
||||
}
|
||||
|
||||
int left = 0;
|
||||
int top = 0;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
|
||||
try {
|
||||
left = (int) (rect.getDouble(0) * zoom);
|
||||
top = (int) (rect.getDouble(1) * zoom);
|
||||
width = (int) (rect.getDouble(2) * zoom);
|
||||
height = (int) (rect.getDouble(3) * zoom);
|
||||
} catch (JSONException e) { }
|
||||
|
||||
int listWidth = mWidth;
|
||||
int listHeight = mHeight;
|
||||
int listLeft = left < 0 ? 0 : left;
|
||||
int listTop = top + height;
|
||||
|
||||
FloatSize viewport = GeckoApp.mAppContext.getLayerController().getViewportSize();
|
||||
|
||||
// If the textbox is smaller than the screen-width,
|
||||
// shrink the list's width
|
||||
if ((left + width) < viewport.width)
|
||||
listWidth = left + width;
|
||||
|
||||
// If the list is extending outside of the viewport
|
||||
// try moving above
|
||||
if (((listTop + listHeight) > viewport.height) && (listHeight <= top))
|
||||
listTop = (top - listHeight);
|
||||
|
||||
mLayout = new RelativeLayout.LayoutParams(listWidth, listHeight);
|
||||
mLayout.setMargins(listLeft, listTop, 0, 0);
|
||||
setLayoutParams(mLayout);
|
||||
requestLayout();
|
||||
}
|
||||
|
||||
public void hide() {
|
||||
if (isShown()) {
|
||||
setVisibility(View.GONE);
|
||||
GeckoAppShell.sendEventToGecko(new GeckoEvent("FormAssist:Closed", null));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -121,6 +121,7 @@ abstract public class GeckoApp
|
|||
|
||||
public static BrowserToolbar mBrowserToolbar;
|
||||
public static DoorHangerPopup mDoorHangerPopup;
|
||||
public static AutoCompletePopup mAutoCompletePopup;
|
||||
public Favicons mFavicons;
|
||||
|
||||
private Geocoder mGeocoder;
|
||||
|
@ -925,6 +926,23 @@ abstract public class GeckoApp
|
|||
mBrowserToolbar.setVisibility(View.VISIBLE);
|
||||
}
|
||||
});
|
||||
} else if (event.equals("FormAssist:AutoComplete")) {
|
||||
final JSONArray suggestions = message.getJSONArray("suggestions");
|
||||
if (suggestions.length() == 0) {
|
||||
mMainHandler.post(new Runnable() {
|
||||
public void run() {
|
||||
mAutoCompletePopup.hide();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
final JSONArray rect = message.getJSONArray("rect");
|
||||
final double zoom = message.getDouble("zoom");
|
||||
mMainHandler.post(new Runnable() {
|
||||
public void run() {
|
||||
mAutoCompletePopup.show(suggestions, rect, zoom);
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (event.equals("AgentMode:Changed")) {
|
||||
Tab.AgentMode agentMode = message.getString("agentMode").equals("mobile") ? Tab.AgentMode.MOBILE : Tab.AgentMode.DESKTOP;
|
||||
int tabId = message.getInt("tabId");
|
||||
|
@ -958,6 +976,7 @@ abstract public class GeckoApp
|
|||
}
|
||||
|
||||
public void run() {
|
||||
mAutoCompletePopup.hide();
|
||||
if (mAboutHomeContent == null) {
|
||||
mAboutHomeContent = (AboutHomeContent) findViewById(R.id.abouthome_content);
|
||||
mAboutHomeContent.init(GeckoApp.mAppContext);
|
||||
|
@ -1059,6 +1078,7 @@ abstract public class GeckoApp
|
|||
|
||||
mMainHandler.post(new Runnable() {
|
||||
public void run() {
|
||||
mAutoCompletePopup.hide();
|
||||
if (Tabs.getInstance().isSelectedTab(tab)) {
|
||||
mBrowserToolbar.setTitle(tab.getDisplayTitle());
|
||||
mBrowserToolbar.setFavicon(tab.getFavicon());
|
||||
|
@ -1324,6 +1344,7 @@ abstract public class GeckoApp
|
|||
mMainLayout = (LinearLayout) findViewById(R.id.main_layout);
|
||||
|
||||
mDoorHangerPopup = new DoorHangerPopup(this);
|
||||
mAutoCompletePopup = (AutoCompletePopup) findViewById(R.id.autocomplete_popup);
|
||||
|
||||
Tabs tabs = Tabs.getInstance();
|
||||
Tab tab = tabs.getSelectedTab();
|
||||
|
@ -1415,6 +1436,7 @@ abstract public class GeckoApp
|
|||
GeckoAppShell.registerGeckoEventListener("ToggleChrome:Hide", GeckoApp.mAppContext);
|
||||
GeckoAppShell.registerGeckoEventListener("ToggleChrome:Show", GeckoApp.mAppContext);
|
||||
GeckoAppShell.registerGeckoEventListener("AgentMode:Changed", GeckoApp.mAppContext);
|
||||
GeckoAppShell.registerGeckoEventListener("FormAssist:AutoComplete", GeckoApp.mAppContext);
|
||||
|
||||
mConnectivityFilter = new IntentFilter();
|
||||
mConnectivityFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||
|
@ -1643,6 +1665,7 @@ abstract public class GeckoApp
|
|||
GeckoAppShell.unregisterGeckoEventListener("ToggleChrome:Hide", GeckoApp.mAppContext);
|
||||
GeckoAppShell.unregisterGeckoEventListener("ToggleChrome:Show", GeckoApp.mAppContext);
|
||||
GeckoAppShell.unregisterGeckoEventListener("AgentMode:Changed", GeckoApp.mAppContext);
|
||||
GeckoAppShell.unregisterGeckoEventListener("FormAssist:AutoComplete", GeckoApp.mAppContext);
|
||||
|
||||
mFavicons.close();
|
||||
|
||||
|
@ -1666,7 +1689,10 @@ abstract public class GeckoApp
|
|||
public void onConfigurationChanged(android.content.res.Configuration newConfig)
|
||||
{
|
||||
Log.i(LOGTAG, "configuration changed");
|
||||
// nothing, just ignore
|
||||
|
||||
// hide the autocomplete list on rotation
|
||||
mAutoCompletePopup.hide();
|
||||
|
||||
super.onConfigurationChanged(newConfig);
|
||||
}
|
||||
|
||||
|
|
|
@ -50,6 +50,7 @@ DIST_FILES = package-name.txt
|
|||
JAVAFILES = \
|
||||
AboutHomeContent.java \
|
||||
AlertNotification.java \
|
||||
AutoCompletePopup.java \
|
||||
AwesomeBar.java \
|
||||
AwesomeBarTabs.java \
|
||||
BrowserToolbar.java \
|
||||
|
@ -174,6 +175,7 @@ DEFINES += -DMOZ_ANDROID_SHARED_ID="$(ANDROID_PACKAGE_NAME).sharedID"
|
|||
endif
|
||||
|
||||
RES_LAYOUT = \
|
||||
res/layout/autocomplete_list_item.xml \
|
||||
res/layout/awesomebar_header_row.xml \
|
||||
res/layout/awesomebar_row.xml \
|
||||
res/layout/awesomebar_search.xml \
|
||||
|
@ -369,6 +371,7 @@ MOZ_ANDROID_DRAWABLES += mobile/android/base/resources/drawable/abouthome_bg.png
|
|||
mobile/android/base/resources/drawable/address_bar_bg.xml \
|
||||
mobile/android/base/resources/drawable/address_bar_url_default.xml \
|
||||
mobile/android/base/resources/drawable/address_bar_url_pressed.xml \
|
||||
mobile/android/base/resources/drawable/autocomplete_list_bg.9.png \
|
||||
mobile/android/base/resources/drawable/awesomebar_tab_focus.xml \
|
||||
mobile/android/base/resources/drawable/awesomebar_tab_focus_selected.xml \
|
||||
mobile/android/base/resources/drawable/awesomebar_tab_indicator.xml \
|
||||
|
|
Двоичный файл не отображается.
После Ширина: | Высота: | Размер: 167 B |
|
@ -24,6 +24,15 @@
|
|||
|
||||
</org.mozilla.gecko.AboutHomeContent>
|
||||
|
||||
<org.mozilla.gecko.AutoCompletePopup android:id="@+id/autocomplete_popup"
|
||||
style="@android:style/Widget.ListView.White"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="100dip"
|
||||
android:background="@drawable/autocomplete_list_bg"
|
||||
android:cacheColorHint="#ffffff"
|
||||
android:listSelector="@android:drawable/list_selector_background"
|
||||
android:visibility="gone"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAppearance="?android:attr/textAppearanceMediumInverse"
|
||||
android:textColor="?android:attr/textColorPrimaryInverse"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:paddingLeft="10dp"
|
||||
android:paddingRight="10dp"
|
||||
android:paddingTop="3dp"
|
||||
android:paddingBottom="3dp"/>
|
|
@ -31,6 +31,15 @@
|
|||
|
||||
</org.mozilla.gecko.AboutHomeContent>
|
||||
|
||||
<org.mozilla.gecko.AutoCompletePopup android:id="@+id/autocomplete_popup"
|
||||
style="@android:style/Widget.ListView.White"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="100dip"
|
||||
android:background="@drawable/autocomplete_list_bg"
|
||||
android:cacheColorHint="#ffffff"
|
||||
android:listSelector="@android:drawable/list_selector_background"
|
||||
android:visibility="gone"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
|
|
@ -311,12 +311,14 @@ public class PanZoomController
|
|||
cancelTouch();
|
||||
// fall through
|
||||
case PANNING_HOLD_LOCKED:
|
||||
GeckoApp.mAppContext.mAutoCompletePopup.hide();
|
||||
mState = PanZoomState.PANNING_LOCKED;
|
||||
// fall through
|
||||
case PANNING_LOCKED:
|
||||
track(event);
|
||||
return true;
|
||||
case PANNING_HOLD:
|
||||
GeckoApp.mAppContext.mAutoCompletePopup.hide();
|
||||
mState = PanZoomState.PANNING;
|
||||
// fall through
|
||||
case PANNING:
|
||||
|
@ -990,6 +992,8 @@ public class PanZoomController
|
|||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
GeckoApp.mAppContext.mAutoCompletePopup.hide();
|
||||
|
||||
GeckoEvent e = new GeckoEvent("Gesture:SingleTap", ret.toString());
|
||||
GeckoAppShell.sendEventToGecko(e);
|
||||
return true;
|
||||
|
|
|
@ -201,6 +201,7 @@ var BrowserApp = {
|
|||
|
||||
NativeWindow.init();
|
||||
Downloads.init();
|
||||
FormAssistant.init();
|
||||
OfflineApps.init();
|
||||
IndexedDB.init();
|
||||
XPInstallObserver.init();
|
||||
|
@ -209,6 +210,8 @@ var BrowserApp = {
|
|||
|
||||
// Init LoginManager
|
||||
Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
|
||||
// Init FormHistory
|
||||
Cc["@mozilla.org/satchel/form-history;1"].getService(Ci.nsIFormHistory2);
|
||||
|
||||
let uri = "about:home";
|
||||
if ("arguments" in window && window.arguments[0])
|
||||
|
@ -262,6 +265,7 @@ var BrowserApp = {
|
|||
|
||||
shutdown: function shutdown() {
|
||||
NativeWindow.uninit();
|
||||
FormAssistant.uninit();
|
||||
OfflineApps.uninit();
|
||||
IndexedDB.uninit();
|
||||
ViewportHandler.uninit();
|
||||
|
@ -2172,8 +2176,99 @@ var ErrorPageEventHandler = {
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
var FormAssistant = {
|
||||
// Used to keep track of the element that corresponds to the current
|
||||
// autocomplete suggestions
|
||||
_currentInputElement: null,
|
||||
|
||||
init: function() {
|
||||
Services.obs.addObserver(this, "FormAssist:AutoComplete", false);
|
||||
Services.obs.addObserver(this, "FormAssist:Closed", false);
|
||||
|
||||
BrowserApp.deck.addEventListener("compositionstart", this, false);
|
||||
BrowserApp.deck.addEventListener("compositionupdate", this, false);
|
||||
},
|
||||
|
||||
uninit: function() {
|
||||
Services.obs.removeObserver(this, "FormAssist:AutoComplete");
|
||||
Services.obs.removeObserver(this, "FormAssist:Closed");
|
||||
},
|
||||
|
||||
observe: function(aSubject, aTopic, aData) {
|
||||
switch (aTopic) {
|
||||
case "FormAssist:AutoComplete":
|
||||
if (!this._currentInputElement)
|
||||
break;
|
||||
|
||||
// Remove focus from the textbox to avoid some bad IME interactions
|
||||
this._currentInputElement.blur();
|
||||
this._currentInputElement.value = aData;
|
||||
break;
|
||||
|
||||
case "FormAssist:Closed":
|
||||
this._currentInputElement = null;
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
handleEvent: function(aEvent) {
|
||||
switch (aEvent.type) {
|
||||
case "compositionstart":
|
||||
case "compositionupdate":
|
||||
let currentElement = aEvent.target;
|
||||
if (!this._isAutocomplete(currentElement))
|
||||
break;
|
||||
|
||||
// Keep track of input element so we can fill it in if the user
|
||||
// selects an autocomplete suggestion
|
||||
this._currentInputElement = currentElement;
|
||||
let suggestions = this._getAutocompleteSuggestions(aEvent.data, currentElement);
|
||||
|
||||
let rect = currentElement.getBoundingClientRect();
|
||||
let zoom = BrowserApp.selectedTab.viewport.zoom;
|
||||
|
||||
sendMessageToJava({
|
||||
gecko: {
|
||||
type: "FormAssist:AutoComplete",
|
||||
suggestions: suggestions,
|
||||
rect: [rect.left, rect.top, rect.width, rect.height],
|
||||
zoom: zoom
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
_isAutocomplete: function (aElement) {
|
||||
if (!(aElement instanceof HTMLInputElement) ||
|
||||
(aElement.getAttribute("type") == "password") ||
|
||||
(aElement.hasAttribute("autocomplete") &&
|
||||
aElement.getAttribute("autocomplete").toLowerCase() == "off"))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
},
|
||||
|
||||
/** Retrieve the autocomplete list from the autocomplete service for an element */
|
||||
_getAutocompleteSuggestions: function(aSearchString, aElement) {
|
||||
let results = Cc["@mozilla.org/satchel/form-autocomplete;1"].
|
||||
getService(Ci.nsIFormAutoComplete).
|
||||
autoCompleteSearch(aElement.name || aElement.id, aSearchString, aElement, null);
|
||||
|
||||
let suggestions = [];
|
||||
if (results.matchCount > 0) {
|
||||
for (let i = 0; i < results.matchCount; i++) {
|
||||
let value = results.getValueAt(i);
|
||||
// Do not show the value if it is the current one in the input field
|
||||
if (value == aSearchString)
|
||||
continue;
|
||||
|
||||
suggestions.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
return suggestions;
|
||||
},
|
||||
|
||||
show: function(aList, aElement) {
|
||||
let data = JSON.parse(sendMessageToJava({ gecko: aList }));
|
||||
let selected = data.button;
|
||||
|
|
Загрузка…
Ссылка в новой задаче