blob: 1ba26febf475fb8784fc720c6372c92ee9133a1d [file] [log] [blame]
Michael Kolbfe251992010-07-08 15:41:55 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolbfe251992010-07-08 15:41:55 -070019import android.content.Context;
Michael Kolb21ce4d22010-09-15 14:55:05 -070020import android.content.res.Configuration;
John Reck8d021aa2011-09-06 15:30:11 -070021import android.content.res.TypedArray;
Narayan Kamath5119edd2011-02-23 15:49:17 +000022import android.graphics.Rect;
John Reck8d021aa2011-09-06 15:30:11 -070023import android.graphics.drawable.Drawable;
Narayan Kamathf3174a52011-11-17 14:43:32 +000024import android.text.Editable;
John Reckdcda1d52010-11-29 10:05:54 -080025import android.text.TextUtils;
Narayan Kamathf3174a52011-11-17 14:43:32 +000026import android.text.TextWatcher;
Michael Kolbfe251992010-07-08 15:41:55 -070027import android.util.AttributeSet;
John Reckb77bdc42011-01-24 13:24:48 -080028import android.util.Patterns;
Michael Kolbfe251992010-07-08 15:41:55 -070029import android.view.KeyEvent;
Michael Kolb305b1c52011-06-21 16:16:22 -070030import android.view.MotionEvent;
Michael Kolbfe251992010-07-08 15:41:55 -070031import android.view.View;
Michael Kolbfe251992010-07-08 15:41:55 -070032import android.view.inputmethod.InputMethodManager;
John Reck87369ea2011-01-23 15:20:38 -080033import android.widget.AdapterView;
34import android.widget.AdapterView.OnItemClickListener;
Narayan Kamathf3174a52011-11-17 14:43:32 +000035import android.widget.AutoCompleteTextView;
Michael Kolbfe251992010-07-08 15:41:55 -070036import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070037import android.widget.TextView.OnEditorActionListener;
Michael Kolbfe251992010-07-08 15:41:55 -070038
Michael Kolb305b1c52011-06-21 16:16:22 -070039import com.android.browser.SuggestionsAdapter.CompletionListener;
40import com.android.browser.SuggestionsAdapter.SuggestItem;
Michael Kolb305b1c52011-06-21 16:16:22 -070041import com.android.browser.search.SearchEngine;
42import com.android.browser.search.SearchEngineInfo;
43import com.android.browser.search.SearchEngines;
John Reck8d021aa2011-09-06 15:30:11 -070044import com.android.internal.R;
Michael Kolb305b1c52011-06-21 16:16:22 -070045
Michael Kolbcfa3af52010-12-14 10:36:11 -080046import java.util.List;
47
Michael Kolbfe251992010-07-08 15:41:55 -070048/**
49 * url/search input view
50 * handling suggestions
51 */
Narayan Kamathf3174a52011-11-17 14:43:32 +000052public class UrlInputView extends AutoCompleteTextView
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080053 implements OnEditorActionListener,
Narayan Kamathf3174a52011-11-17 14:43:32 +000054 CompletionListener, OnItemClickListener, TextWatcher {
Michael Kolb257cc2c2010-12-09 09:45:52 -080055
56 static final String TYPED = "browser-type";
57 static final String SUGGESTED = "browser-suggest";
Michael Kolbbd2dd642011-01-13 13:01:30 -080058 static final String VOICE = "voice-search";
Michael Kolb257cc2c2010-12-09 09:45:52 -080059
Michael Kolb94ec5272011-08-31 16:23:57 -070060 static final int POST_DELAY = 100;
61
Michael Kolb305b1c52011-06-21 16:16:22 -070062 static interface StateListener {
63 static final int STATE_NORMAL = 0;
64 static final int STATE_HIGHLIGHTED = 1;
65 static final int STATE_EDITED = 2;
66
67 public void onStateChanged(int state);
68 }
69
Michael Kolbfe251992010-07-08 15:41:55 -070070 private UrlInputListener mListener;
71 private InputMethodManager mInputManager;
72 private SuggestionsAdapter mAdapter;
Michael Kolb21ce4d22010-09-15 14:55:05 -070073 private View mContainer;
74 private boolean mLandscape;
John Reckb77bdc42011-01-24 13:24:48 -080075 private boolean mIncognitoMode;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080076 private boolean mNeedsUpdate;
Michael Kolbfe251992010-07-08 15:41:55 -070077
Michael Kolb305b1c52011-06-21 16:16:22 -070078 private int mState;
79 private StateListener mStateListener;
John Reck8d021aa2011-09-06 15:30:11 -070080 private Rect mPopupPadding;
Michael Kolb305b1c52011-06-21 16:16:22 -070081
Michael Kolbfe251992010-07-08 15:41:55 -070082 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
83 super(context, attrs, defStyle);
John Reck8d021aa2011-09-06 15:30:11 -070084 TypedArray a = context.obtainStyledAttributes(
85 attrs, com.android.internal.R.styleable.PopupWindow,
86 R.attr.autoCompleteTextViewStyle, 0);
87
88 Drawable popupbg = a.getDrawable(R.styleable.PopupWindow_popupBackground);
89 a.recycle();
90 mPopupPadding = new Rect();
91 popupbg.getPadding(mPopupPadding);
Michael Kolbfe251992010-07-08 15:41:55 -070092 init(context);
93 }
94
95 public UrlInputView(Context context, AttributeSet attrs) {
John Reck8d021aa2011-09-06 15:30:11 -070096 this(context, attrs, R.attr.autoCompleteTextViewStyle);
Michael Kolbfe251992010-07-08 15:41:55 -070097 }
98
99 public UrlInputView(Context context) {
John Reck8d021aa2011-09-06 15:30:11 -0700100 this(context, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700101 }
102
103 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -0700104 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -0700105 setOnEditorActionListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700106 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -0700107 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -0800108 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700109 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -0800110 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -0800111 setOnItemClickListener(this);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800112 mNeedsUpdate = false;
Narayan Kamathf3174a52011-11-17 14:43:32 +0000113 addTextChangedListener(this);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000114
Michael Kolb305b1c52011-06-21 16:16:22 -0700115 mState = StateListener.STATE_NORMAL;
116 }
117
118 protected void onFocusChanged(boolean focused, int direction, Rect prevRect) {
119 super.onFocusChanged(focused, direction, prevRect);
Michael Kolb94ec5272011-08-31 16:23:57 -0700120 int state = -1;
Michael Kolb305b1c52011-06-21 16:16:22 -0700121 if (focused) {
122 if (hasSelection()) {
Michael Kolb94ec5272011-08-31 16:23:57 -0700123 state = StateListener.STATE_HIGHLIGHTED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700124 } else {
Michael Kolb94ec5272011-08-31 16:23:57 -0700125 state = StateListener.STATE_EDITED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700126 }
127 } else {
128 // reset the selection state
Michael Kolb94ec5272011-08-31 16:23:57 -0700129 state = StateListener.STATE_NORMAL;
Michael Kolb305b1c52011-06-21 16:16:22 -0700130 }
Michael Kolb94ec5272011-08-31 16:23:57 -0700131 final int s = state;
132 post(new Runnable() {
133 public void run() {
134 changeState(s);
135 }
136 });
Michael Kolb305b1c52011-06-21 16:16:22 -0700137 }
138
139 @Override
140 public boolean onTouchEvent(MotionEvent evt) {
141 boolean hasSelection = hasSelection();
142 boolean res = super.onTouchEvent(evt);
143 if ((MotionEvent.ACTION_DOWN == evt.getActionMasked())
144 && hasSelection) {
Michael Kolb94ec5272011-08-31 16:23:57 -0700145 postDelayed(new Runnable() {
146 public void run() {
147 changeState(StateListener.STATE_EDITED);
148 }}, POST_DELAY);
Michael Kolb305b1c52011-06-21 16:16:22 -0700149 }
150 return res;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800151 }
152
153 /**
154 * check if focus change requires a title bar update
155 */
156 boolean needsUpdate() {
157 return mNeedsUpdate;
158 }
159
160 /**
161 * clear the focus change needs title bar update flag
162 */
163 void clearNeedsUpdate() {
164 mNeedsUpdate = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700165 }
166
Michael Kolbba99c5d2010-11-29 14:57:41 -0800167 void setController(UiController controller) {
168 UrlSelectionActionMode urlSelectionMode
169 = new UrlSelectionActionMode(controller);
170 setCustomSelectionActionModeCallback(urlSelectionMode);
171 }
172
Michael Kolb21ce4d22010-09-15 14:55:05 -0700173 void setContainer(View container) {
174 mContainer = container;
175 }
176
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800177 public void setUrlInputListener(UrlInputListener listener) {
178 mListener = listener;
179 }
180
Michael Kolb305b1c52011-06-21 16:16:22 -0700181 public void setStateListener(StateListener listener) {
182 mStateListener = listener;
183 // update listener
184 changeState(mState);
185 }
186
187 private void changeState(int newState) {
188 mState = newState;
189 if (mStateListener != null) {
190 mStateListener.onStateChanged(mState);
191 }
192 }
193
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700194 int getState() {
195 return mState;
196 }
197
Michael Kolbcfa3af52010-12-14 10:36:11 -0800198 void setVoiceResults(List<String> voiceResults) {
199 mAdapter.setVoiceResults(voiceResults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800200 }
201
Michael Kolb21ce4d22010-09-15 14:55:05 -0700202 @Override
203 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800204 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700205 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800206 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800207 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700208 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800209 setupDropDown();
Narayan Kamathf3174a52011-11-17 14:43:32 +0000210 performFiltering(getText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700211 }
212 }
213
214 @Override
215 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800216 setupDropDown();
217 super.showDropDown();
218 }
219
220 @Override
221 public void dismissDropDown() {
222 super.dismissDropDown();
223 mAdapter.clearCache();
224 }
225
226 private void setupDropDown() {
John Reck92026732011-02-15 10:12:30 -0800227 int width = mContainer != null ? mContainer.getWidth() : getWidth();
John Reck8d021aa2011-09-06 15:30:11 -0700228 width += mPopupPadding.left + mPopupPadding.right;
Michael Kolbc5998c22010-10-10 16:04:35 -0700229 if (width != getDropDownWidth()) {
230 setDropDownWidth(width);
231 }
John Reck8d021aa2011-09-06 15:30:11 -0700232 int left = getLeft();
233 left += mPopupPadding.left;
234 if (left != -getDropDownHorizontalOffset()) {
235 setDropDownHorizontalOffset(-left);
Michael Kolbc5998c22010-10-10 16:04:35 -0700236 }
Michael Kolb513286f2010-09-09 12:55:12 -0700237 }
238
239 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700240 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000241 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700242 return true;
243 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700244
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800245 void forceFilter() {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800246 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700247 }
248
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800249 void hideIME() {
250 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
251 }
252
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700253 void showIME() {
254 mInputManager.focusIn(this);
255 mInputManager.showSoftInput(this, 0);
256 }
257
Michael Kolb257cc2c2010-12-09 09:45:52 -0800258 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800259 mNeedsUpdate = true;
260 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700261 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800262 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700263 mListener.onDismiss();
264 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800265 if (mIncognitoMode && isSearch(url)) {
266 // To prevent logging, intercept this request
267 // TODO: This is a quick hack, refactor this
268 SearchEngine searchEngine = BrowserSettings.getInstance()
269 .getSearchEngine();
270 if (searchEngine == null) return;
271 SearchEngineInfo engineInfo = SearchEngines
272 .getSearchEngineInfo(mContext, searchEngine.getName());
273 if (engineInfo == null) return;
274 url = engineInfo.getSearchUriForQuery(url);
275 // mLister.onAction can take it from here without logging
276 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800277 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700278 }
Michael Kolbfe251992010-07-08 15:41:55 -0700279 }
280
John Reckb77bdc42011-01-24 13:24:48 -0800281 boolean isSearch(String inUrl) {
282 String url = UrlUtils.fixUrl(inUrl).trim();
283 if (TextUtils.isEmpty(url)) return false;
284
285 if (Patterns.WEB_URL.matcher(url).matches()
286 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
287 return false;
288 }
289 return true;
290 }
291
Michael Kolb21ce4d22010-09-15 14:55:05 -0700292 // Completion Listener
293
294 @Override
295 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800296 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700297 }
298
299 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800300 public void onSelect(String url, int type, String extra) {
301 finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH)
302 ? VOICE : SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700303 }
304
Michael Kolbfe251992010-07-08 15:41:55 -0700305 @Override
John Reck87369ea2011-01-23 15:20:38 -0800306 public void onItemClick(
307 AdapterView<?> parent, View view, int position, long id) {
308 SuggestItem item = mAdapter.getItem(position);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000309 onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra);
John Reck87369ea2011-01-23 15:20:38 -0800310 }
311
Michael Kolbfe251992010-07-08 15:41:55 -0700312 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700313
Michael Kolbfe251992010-07-08 15:41:55 -0700314 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700315
Michael Kolb257cc2c2010-12-09 09:45:52 -0800316 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700317
Michael Kolb7cdc4902011-02-03 17:54:40 -0800318 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700319
320 }
321
John Reck117f07d2011-01-24 09:39:03 -0800322 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800323 mIncognitoMode = incognito;
324 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800325 }
326
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800327 @Override
328 public boolean onKeyDown(int keyCode, KeyEvent evt) {
329 if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) {
330 finishInput(null, null, null);
331 return true;
332 }
333 return super.onKeyDown(keyCode, evt);
334 }
335
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000336 public SuggestionsAdapter getAdapter() {
337 return mAdapter;
338 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000339
Michael Kolb74e60c22011-04-30 16:43:47 -0700340 /*
341 * no-op to prevent scrolling of webview when embedded titlebar
342 * gets edited
343 */
344 @Override
345 public boolean requestRectangleOnScreen(Rect rect, boolean immediate) {
346 return false;
347 }
Michael Kolb305b1c52011-06-21 16:16:22 -0700348
349 @Override
Narayan Kamathf3174a52011-11-17 14:43:32 +0000350 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
351
352 @Override
353 public void onTextChanged(CharSequence s, int start, int before, int count) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700354 if (StateListener.STATE_HIGHLIGHTED == mState) {
355 changeState(StateListener.STATE_EDITED);
356 }
357 }
358
Narayan Kamathf3174a52011-11-17 14:43:32 +0000359 @Override
360 public void afterTextChanged(Editable s) { }
361
Michael Kolbfe251992010-07-08 15:41:55 -0700362}