2011-12-06 16:44:08 -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):
|
|
|
|
* 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;
|
2012-01-27 12:10:45 -08:00
|
|
|
import android.util.DisplayMetrics;
|
2012-03-06 13:56:16 -08:00
|
|
|
import android.util.Pair;
|
|
|
|
import android.view.LayoutInflater;
|
2011-12-06 16:44:08 -08:00
|
|
|
import android.view.View;
|
2012-03-06 13:56:16 -08:00
|
|
|
import android.view.ViewGroup;
|
2011-12-06 16:44:08 -08:00
|
|
|
import android.view.animation.Animation;
|
|
|
|
import android.view.animation.AnimationUtils;
|
2012-03-06 11:56:42 -08:00
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2011-12-06 16:44:08 -08:00
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.RelativeLayout;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
2012-03-06 11:56:42 -08:00
|
|
|
import org.json.JSONObject;
|
2011-12-06 16:44:08 -08:00
|
|
|
|
2012-03-06 11:56:42 -08:00
|
|
|
public class FormAssistPopup extends ListView implements GeckoEventListener {
|
2011-12-06 16:44:08 -08:00
|
|
|
private Context mContext;
|
|
|
|
private RelativeLayout.LayoutParams mLayout;
|
|
|
|
|
|
|
|
private int mWidth;
|
|
|
|
private int mHeight;
|
|
|
|
|
|
|
|
private Animation mAnimation;
|
|
|
|
|
2012-03-06 11:56:41 -08:00
|
|
|
private static final String LOGTAG = "FormAssistPopup";
|
2011-12-06 16:44:08 -08:00
|
|
|
|
2012-01-27 12:10:45 -08:00
|
|
|
private static int sMinWidth = 0;
|
2012-02-03 14:10:49 -08:00
|
|
|
private static int sRowHeight = 0;
|
2012-03-06 11:56:41 -08:00
|
|
|
private static final int POPUP_MIN_WIDTH_IN_DPI = 200;
|
|
|
|
private static final int POPUP_ROW_HEIGHT_IN_DPI = 32;
|
2012-01-27 12:10:45 -08:00
|
|
|
|
2012-03-06 11:56:44 -08:00
|
|
|
private static enum PopupType { NONE, AUTOCOMPLETE, VALIDATION };
|
|
|
|
|
|
|
|
// Keep track of the type of popup we're currently showing
|
|
|
|
private PopupType mTypeShowing = PopupType.NONE;
|
|
|
|
|
2012-03-06 11:56:41 -08:00
|
|
|
public FormAssistPopup(Context context, AttributeSet attrs) {
|
2011-12-06 16:44:08 -08:00
|
|
|
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) {
|
2012-03-06 11:56:44 -08:00
|
|
|
if (mTypeShowing.equals(PopupType.AUTOCOMPLETE)) {
|
2012-03-06 13:56:16 -08:00
|
|
|
// Use the value stored with the autocomplete view, not the label text,
|
|
|
|
// since they can be different.
|
2012-03-06 11:56:44 -08:00
|
|
|
TextView textView = (TextView) view;
|
2012-03-06 13:56:16 -08:00
|
|
|
String value = (String) textView.getTag();
|
2012-03-06 11:56:44 -08:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FormAssist:AutoComplete", value));
|
|
|
|
hide();
|
|
|
|
}
|
2011-12-06 16:44:08 -08:00
|
|
|
}
|
|
|
|
});
|
2012-03-06 11:56:42 -08:00
|
|
|
|
|
|
|
GeckoAppShell.registerGeckoEventListener("FormAssist:AutoComplete", this);
|
2012-03-06 11:56:44 -08:00
|
|
|
GeckoAppShell.registerGeckoEventListener("FormAssist:ValidationMessage", this);
|
2012-03-06 11:56:43 -08:00
|
|
|
GeckoAppShell.registerGeckoEventListener("FormAssist:Hide", this);
|
2012-03-06 11:56:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void handleMessage(String event, JSONObject message) {
|
|
|
|
try {
|
|
|
|
if (event.equals("FormAssist:AutoComplete")) {
|
2012-03-06 11:56:43 -08:00
|
|
|
handleAutoCompleteMessage(message);
|
2012-03-06 11:56:44 -08:00
|
|
|
} else if (event.equals("FormAssist:ValidationMessage")) {
|
|
|
|
handleValidationMessage(message);
|
2012-03-06 11:56:43 -08:00
|
|
|
} else if (event.equals("FormAssist:Hide")) {
|
2012-03-06 11:56:43 -08:00
|
|
|
handleHideMessage(message);
|
2012-03-06 11:56:42 -08:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
|
|
|
|
}
|
2011-12-06 16:44:08 -08:00
|
|
|
}
|
|
|
|
|
2012-03-06 11:56:43 -08:00
|
|
|
private void handleAutoCompleteMessage(JSONObject message) throws JSONException {
|
|
|
|
final JSONArray suggestions = message.getJSONArray("suggestions");
|
|
|
|
final JSONArray rect = message.getJSONArray("rect");
|
|
|
|
final double zoom = message.getDouble("zoom");
|
|
|
|
GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
2012-03-06 11:56:44 -08:00
|
|
|
showAutoCompleteSuggestions(suggestions, rect, zoom);
|
2012-03-06 11:56:43 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-03-06 11:56:44 -08:00
|
|
|
private void handleValidationMessage(JSONObject message) throws JSONException {
|
|
|
|
final String validationMessage = message.getString("validationMessage");
|
|
|
|
final JSONArray rect = message.getJSONArray("rect");
|
|
|
|
final double zoom = message.getDouble("zoom");
|
|
|
|
GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
showValidationMessage(validationMessage, rect, zoom);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-03-06 11:56:43 -08:00
|
|
|
private void handleHideMessage(JSONObject message) {
|
|
|
|
GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-03-06 11:56:44 -08:00
|
|
|
private void showAutoCompleteSuggestions(JSONArray suggestions, JSONArray rect, double zoom) {
|
2012-03-06 13:56:16 -08:00
|
|
|
AutoCompleteListAdapter adapter = new AutoCompleteListAdapter(mContext, R.layout.autocomplete_list_item);
|
|
|
|
adapter.populateSuggestionsList(suggestions);
|
2012-03-06 11:56:44 -08:00
|
|
|
setAdapter(adapter);
|
|
|
|
|
|
|
|
if (positionAndShowPopup(rect, zoom))
|
|
|
|
mTypeShowing = PopupType.AUTOCOMPLETE;
|
|
|
|
}
|
2011-12-06 16:44:08 -08:00
|
|
|
|
2012-03-06 11:56:44 -08:00
|
|
|
// TODO: style the validation message popup differently (bug 731654)
|
|
|
|
private void showValidationMessage(String validationMessage, JSONArray rect, double zoom) {
|
|
|
|
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, R.layout.autocomplete_list_item);
|
|
|
|
adapter.add(validationMessage);
|
2011-12-06 16:44:08 -08:00
|
|
|
setAdapter(adapter);
|
|
|
|
|
2012-03-06 11:56:44 -08:00
|
|
|
if (positionAndShowPopup(rect, zoom))
|
|
|
|
mTypeShowing = PopupType.VALIDATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the popup is successfully shown, false otherwise
|
|
|
|
public boolean positionAndShowPopup(JSONArray rect, double zoom) {
|
|
|
|
// Don't show the form assist popup when using fullscreen VKB
|
|
|
|
InputMethodManager imm =
|
|
|
|
(InputMethodManager) GeckoApp.mAppContext.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
if (imm.isFullscreenMode())
|
|
|
|
return false;
|
|
|
|
|
2012-02-03 14:10:49 -08:00
|
|
|
if (!isShown()) {
|
|
|
|
setVisibility(View.VISIBLE);
|
|
|
|
startAnimation(mAnimation);
|
|
|
|
}
|
2011-12-06 16:44:08 -08:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2012-01-27 12:10:45 -08:00
|
|
|
// Late initializing variable to allow DisplayMetrics not to be null and avoid NPE
|
|
|
|
if (sMinWidth == 0) {
|
|
|
|
DisplayMetrics metrics = new DisplayMetrics();
|
|
|
|
GeckoApp.mAppContext.getWindowManager().getDefaultDisplay().getMetrics(metrics);
|
2012-03-06 11:56:41 -08:00
|
|
|
sMinWidth = (int) (POPUP_MIN_WIDTH_IN_DPI * metrics.density);
|
|
|
|
sRowHeight = (int) (POPUP_ROW_HEIGHT_IN_DPI * metrics.density);
|
2012-01-27 12:10:45 -08:00
|
|
|
}
|
|
|
|
|
2011-12-06 16:44:08 -08:00
|
|
|
// If the textbox is smaller than the screen-width,
|
|
|
|
// shrink the list's width
|
|
|
|
if ((left + width) < viewport.width)
|
2012-01-27 12:10:45 -08:00
|
|
|
listWidth = left < 0 ? left + width : width;
|
|
|
|
|
|
|
|
// listWidth can be negative if it is a constant - FILL_PARENT or MATCH_PARENT
|
|
|
|
if (listWidth >= 0 && listWidth < sMinWidth) {
|
|
|
|
listWidth = sMinWidth;
|
|
|
|
|
|
|
|
if ((listLeft + listWidth) > viewport.width)
|
|
|
|
listLeft = (int) (viewport.width - listWidth);
|
|
|
|
}
|
2011-12-06 16:44:08 -08:00
|
|
|
|
2012-03-06 11:56:44 -08:00
|
|
|
listHeight = sRowHeight * getAdapter().getCount();
|
2012-02-03 14:10:49 -08:00
|
|
|
|
|
|
|
// The text box doesnt fit below
|
|
|
|
if ((listTop + listHeight) > viewport.height) {
|
|
|
|
// Find where the maximum space is, and fit it there
|
|
|
|
if ((viewport.height - listTop) > top) {
|
|
|
|
// Shrink the height to fit it below the text-box
|
|
|
|
listHeight = (int) (viewport.height - listTop);
|
|
|
|
} else {
|
|
|
|
if (listHeight < top) {
|
|
|
|
// No shrinking needed to fit on top
|
|
|
|
listTop = (top - listHeight);
|
|
|
|
} else {
|
|
|
|
// Shrink to available space on top
|
|
|
|
listTop = 0;
|
|
|
|
listHeight = top;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-06 16:44:08 -08:00
|
|
|
|
|
|
|
mLayout = new RelativeLayout.LayoutParams(listWidth, listHeight);
|
|
|
|
mLayout.setMargins(listLeft, listTop, 0, 0);
|
|
|
|
setLayoutParams(mLayout);
|
|
|
|
requestLayout();
|
2012-03-06 11:56:44 -08:00
|
|
|
|
|
|
|
return true;
|
2011-12-06 16:44:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void hide() {
|
|
|
|
if (isShown()) {
|
|
|
|
setVisibility(View.GONE);
|
2012-03-06 11:56:44 -08:00
|
|
|
mTypeShowing = PopupType.NONE;
|
2012-03-06 11:56:43 -08:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FormAssist:Hidden", null));
|
2011-12-06 16:44:08 -08:00
|
|
|
}
|
|
|
|
}
|
2012-03-06 13:56:16 -08:00
|
|
|
|
|
|
|
private class AutoCompleteListAdapter extends ArrayAdapter<Pair<String, String>> {
|
|
|
|
private LayoutInflater mInflater;
|
|
|
|
private int mTextViewResourceId;
|
|
|
|
|
|
|
|
public AutoCompleteListAdapter(Context context, int textViewResourceId) {
|
|
|
|
super(context, textViewResourceId);
|
|
|
|
|
|
|
|
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
mTextViewResourceId = textViewResourceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method takes an array of autocomplete suggestions with label/value properties
|
|
|
|
// and adds label/value Pair objects to the array that backs the adapter.
|
|
|
|
public void populateSuggestionsList(JSONArray suggestions) {
|
|
|
|
try {
|
|
|
|
for (int i = 0; i < suggestions.length(); i++) {
|
|
|
|
JSONObject suggestion = (JSONObject) suggestions.get(i);
|
|
|
|
String label = suggestion.getString("label");
|
|
|
|
String value = suggestion.getString("value");
|
|
|
|
add(new Pair<String, String>(label, value));
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(LOGTAG, "JSONException: " + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
if (convertView == null)
|
|
|
|
convertView = mInflater.inflate(mTextViewResourceId, null);
|
|
|
|
|
|
|
|
Pair<String, String> item = getItem(position);
|
|
|
|
TextView itemView = (TextView) convertView;
|
|
|
|
|
|
|
|
// Set the text with the suggestion label
|
|
|
|
itemView.setText(item.first);
|
|
|
|
|
|
|
|
// Set a tag with the suggestion value
|
|
|
|
itemView.setTag(item.second);
|
|
|
|
|
|
|
|
return convertView;
|
|
|
|
}
|
|
|
|
}
|
2011-12-06 16:44:08 -08:00
|
|
|
}
|