blob: 96ab864d0e06206ccdfd906245caa17963d05984 [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.database.DataSetObserver;
23import android.graphics.Rect;
John Reck8d021aa2011-09-06 15:30:11 -070024import android.graphics.drawable.Drawable;
John Reckdcda1d52010-11-29 10:05:54 -080025import android.text.TextUtils;
Michael Kolbfe251992010-07-08 15:41:55 -070026import android.util.AttributeSet;
John Reckb77bdc42011-01-24 13:24:48 -080027import android.util.Patterns;
Michael Kolbfe251992010-07-08 15:41:55 -070028import android.view.KeyEvent;
Michael Kolb305b1c52011-06-21 16:16:22 -070029import android.view.MotionEvent;
Michael Kolbfe251992010-07-08 15:41:55 -070030import android.view.View;
Narayan Kamath5119edd2011-02-23 15:49:17 +000031import android.view.inputmethod.EditorInfo;
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;
Michael Kolbfe251992010-07-08 15:41:55 -070035import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070036import android.widget.TextView.OnEditorActionListener;
Michael Kolbfe251992010-07-08 15:41:55 -070037
Michael Kolb305b1c52011-06-21 16:16:22 -070038import com.android.browser.SuggestionsAdapter.CompletionListener;
39import com.android.browser.SuggestionsAdapter.SuggestItem;
40import com.android.browser.UI.DropdownChangeListener;
41import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
42import com.android.browser.autocomplete.SuggestiveAutoCompleteTextView;
43import com.android.browser.search.SearchEngine;
44import com.android.browser.search.SearchEngineInfo;
45import com.android.browser.search.SearchEngines;
John Reck8d021aa2011-09-06 15:30:11 -070046import com.android.internal.R;
Michael Kolb305b1c52011-06-21 16:16:22 -070047
Michael Kolbcfa3af52010-12-14 10:36:11 -080048import java.util.List;
49
Michael Kolbfe251992010-07-08 15:41:55 -070050/**
51 * url/search input view
52 * handling suggestions
53 */
Narayan Kamath80aad8d2011-02-23 12:01:13 +000054public class UrlInputView extends SuggestiveAutoCompleteTextView
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080055 implements OnEditorActionListener,
Michael Kolb305b1c52011-06-21 16:16:22 -070056 CompletionListener, OnItemClickListener, TextChangeWatcher {
Michael Kolb257cc2c2010-12-09 09:45:52 -080057
58 static final String TYPED = "browser-type";
59 static final String SUGGESTED = "browser-suggest";
Michael Kolbbd2dd642011-01-13 13:01:30 -080060 static final String VOICE = "voice-search";
Michael Kolb257cc2c2010-12-09 09:45:52 -080061
Michael Kolb94ec5272011-08-31 16:23:57 -070062 static final int POST_DELAY = 100;
63
Michael Kolb305b1c52011-06-21 16:16:22 -070064 static interface StateListener {
65 static final int STATE_NORMAL = 0;
66 static final int STATE_HIGHLIGHTED = 1;
67 static final int STATE_EDITED = 2;
68
69 public void onStateChanged(int state);
70 }
71
Michael Kolbfe251992010-07-08 15:41:55 -070072 private UrlInputListener mListener;
73 private InputMethodManager mInputManager;
74 private SuggestionsAdapter mAdapter;
Michael Kolb21ce4d22010-09-15 14:55:05 -070075 private View mContainer;
76 private boolean mLandscape;
John Reckb77bdc42011-01-24 13:24:48 -080077 private boolean mIncognitoMode;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080078 private boolean mNeedsUpdate;
Narayan Kamath5119edd2011-02-23 15:49:17 +000079 private DropdownChangeListener mDropdownListener;
Michael Kolbfe251992010-07-08 15:41:55 -070080
Michael Kolb305b1c52011-06-21 16:16:22 -070081 private int mState;
82 private StateListener mStateListener;
John Reck8d021aa2011-09-06 15:30:11 -070083 private Rect mPopupPadding;
Michael Kolb305b1c52011-06-21 16:16:22 -070084
Michael Kolbfe251992010-07-08 15:41:55 -070085 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
86 super(context, attrs, defStyle);
John Reck8d021aa2011-09-06 15:30:11 -070087 TypedArray a = context.obtainStyledAttributes(
88 attrs, com.android.internal.R.styleable.PopupWindow,
89 R.attr.autoCompleteTextViewStyle, 0);
90
91 Drawable popupbg = a.getDrawable(R.styleable.PopupWindow_popupBackground);
92 a.recycle();
93 mPopupPadding = new Rect();
94 popupbg.getPadding(mPopupPadding);
Michael Kolbfe251992010-07-08 15:41:55 -070095 init(context);
96 }
97
98 public UrlInputView(Context context, AttributeSet attrs) {
John Reck8d021aa2011-09-06 15:30:11 -070099 this(context, attrs, R.attr.autoCompleteTextViewStyle);
Michael Kolbfe251992010-07-08 15:41:55 -0700100 }
101
102 public UrlInputView(Context context) {
John Reck8d021aa2011-09-06 15:30:11 -0700103 this(context, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700104 }
105
106 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -0700107 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -0700108 setOnEditorActionListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700109 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -0700110 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -0800111 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700112 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -0800113 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -0800114 setOnItemClickListener(this);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800115 mNeedsUpdate = false;
Narayan Kamath5119edd2011-02-23 15:49:17 +0000116 mDropdownListener = null;
Michael Kolb305b1c52011-06-21 16:16:22 -0700117 addQueryTextWatcher(this);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000118
119 mAdapter.registerDataSetObserver(new DataSetObserver() {
120 @Override
121 public void onChanged() {
122 if (!isPopupShowing()) {
123 return;
124 }
125 dispatchChange();
126 }
127
128 @Override
129 public void onInvalidated() {
130 dispatchChange();
131 }
132 });
Michael Kolb305b1c52011-06-21 16:16:22 -0700133 mState = StateListener.STATE_NORMAL;
134 }
135
136 protected void onFocusChanged(boolean focused, int direction, Rect prevRect) {
137 super.onFocusChanged(focused, direction, prevRect);
Michael Kolb94ec5272011-08-31 16:23:57 -0700138 int state = -1;
Michael Kolb305b1c52011-06-21 16:16:22 -0700139 if (focused) {
140 if (hasSelection()) {
Michael Kolb94ec5272011-08-31 16:23:57 -0700141 state = StateListener.STATE_HIGHLIGHTED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700142 } else {
Michael Kolb94ec5272011-08-31 16:23:57 -0700143 state = StateListener.STATE_EDITED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700144 }
145 } else {
146 // reset the selection state
Michael Kolb94ec5272011-08-31 16:23:57 -0700147 state = StateListener.STATE_NORMAL;
Michael Kolb305b1c52011-06-21 16:16:22 -0700148 }
Michael Kolb94ec5272011-08-31 16:23:57 -0700149 final int s = state;
150 post(new Runnable() {
151 public void run() {
152 changeState(s);
153 }
154 });
Michael Kolb305b1c52011-06-21 16:16:22 -0700155 }
156
157 @Override
158 public boolean onTouchEvent(MotionEvent evt) {
159 boolean hasSelection = hasSelection();
160 boolean res = super.onTouchEvent(evt);
161 if ((MotionEvent.ACTION_DOWN == evt.getActionMasked())
162 && hasSelection) {
Michael Kolb94ec5272011-08-31 16:23:57 -0700163 postDelayed(new Runnable() {
164 public void run() {
165 changeState(StateListener.STATE_EDITED);
166 }}, POST_DELAY);
Michael Kolb305b1c52011-06-21 16:16:22 -0700167 }
168 return res;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800169 }
170
171 /**
172 * check if focus change requires a title bar update
173 */
174 boolean needsUpdate() {
175 return mNeedsUpdate;
176 }
177
178 /**
179 * clear the focus change needs title bar update flag
180 */
181 void clearNeedsUpdate() {
182 mNeedsUpdate = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700183 }
184
Michael Kolbba99c5d2010-11-29 14:57:41 -0800185 void setController(UiController controller) {
186 UrlSelectionActionMode urlSelectionMode
187 = new UrlSelectionActionMode(controller);
188 setCustomSelectionActionModeCallback(urlSelectionMode);
189 }
190
Michael Kolb21ce4d22010-09-15 14:55:05 -0700191 void setContainer(View container) {
192 mContainer = container;
193 }
194
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800195 public void setUrlInputListener(UrlInputListener listener) {
196 mListener = listener;
197 }
198
Michael Kolb305b1c52011-06-21 16:16:22 -0700199 public void setStateListener(StateListener listener) {
200 mStateListener = listener;
201 // update listener
202 changeState(mState);
203 }
204
205 private void changeState(int newState) {
206 mState = newState;
207 if (mStateListener != null) {
208 mStateListener.onStateChanged(mState);
209 }
210 }
211
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700212 int getState() {
213 return mState;
214 }
215
Michael Kolbcfa3af52010-12-14 10:36:11 -0800216 void setVoiceResults(List<String> voiceResults) {
217 mAdapter.setVoiceResults(voiceResults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800218 }
219
Michael Kolb21ce4d22010-09-15 14:55:05 -0700220 @Override
221 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800222 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700223 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800224 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800225 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700226 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800227 setupDropDown();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000228 performFiltering(getUserText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700229 }
230 }
231
232 @Override
233 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800234 setupDropDown();
235 super.showDropDown();
236 }
237
238 @Override
239 public void dismissDropDown() {
240 super.dismissDropDown();
241 mAdapter.clearCache();
242 }
243
244 private void setupDropDown() {
John Reck92026732011-02-15 10:12:30 -0800245 int width = mContainer != null ? mContainer.getWidth() : getWidth();
John Reck8d021aa2011-09-06 15:30:11 -0700246 width += mPopupPadding.left + mPopupPadding.right;
Michael Kolbc5998c22010-10-10 16:04:35 -0700247 if (width != getDropDownWidth()) {
248 setDropDownWidth(width);
249 }
John Reck8d021aa2011-09-06 15:30:11 -0700250 int left = getLeft();
251 left += mPopupPadding.left;
252 if (left != -getDropDownHorizontalOffset()) {
253 setDropDownHorizontalOffset(-left);
Michael Kolbc5998c22010-10-10 16:04:35 -0700254 }
Michael Kolb513286f2010-09-09 12:55:12 -0700255 }
256
257 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700258 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
John Reck35e9dd62011-04-25 09:01:54 -0700259 if (BrowserSettings.getInstance().useInstantSearch() &&
Narayan Kamath5119edd2011-02-23 15:49:17 +0000260 (actionId == EditorInfo.IME_ACTION_NEXT)) {
261 // When instant is turned on AND the user chooses to complete
262 // using the tab key, then use the completion rather than the
263 // text that the user has typed.
264 finishInput(getText().toString(), null, TYPED);
265 } else {
266 finishInput(getUserText(), null, TYPED);
267 }
268
Michael Kolbed217742010-08-10 17:52:34 -0700269 return true;
270 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700271
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800272 void forceFilter() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000273 performForcedFiltering();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800274 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700275 }
276
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800277 void forceIme() {
John Reck92026732011-02-15 10:12:30 -0800278 mInputManager.focusIn(this);
Michael Kolbfe251992010-07-08 15:41:55 -0700279 mInputManager.showSoftInput(this, 0);
280 }
281
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800282 void hideIME() {
283 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
284 }
285
Michael Kolb257cc2c2010-12-09 09:45:52 -0800286 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800287 mNeedsUpdate = true;
288 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700289 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800290 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700291 mListener.onDismiss();
292 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800293 if (mIncognitoMode && isSearch(url)) {
294 // To prevent logging, intercept this request
295 // TODO: This is a quick hack, refactor this
296 SearchEngine searchEngine = BrowserSettings.getInstance()
297 .getSearchEngine();
298 if (searchEngine == null) return;
299 SearchEngineInfo engineInfo = SearchEngines
300 .getSearchEngineInfo(mContext, searchEngine.getName());
301 if (engineInfo == null) return;
302 url = engineInfo.getSearchUriForQuery(url);
303 // mLister.onAction can take it from here without logging
304 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800305 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700306 }
Michael Kolbfe251992010-07-08 15:41:55 -0700307 }
308
John Reckb77bdc42011-01-24 13:24:48 -0800309 boolean isSearch(String inUrl) {
310 String url = UrlUtils.fixUrl(inUrl).trim();
311 if (TextUtils.isEmpty(url)) return false;
312
313 if (Patterns.WEB_URL.matcher(url).matches()
314 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
315 return false;
316 }
317 return true;
318 }
319
Michael Kolb21ce4d22010-09-15 14:55:05 -0700320 // Completion Listener
321
322 @Override
323 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800324 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700325 }
326
327 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800328 public void onSelect(String url, int type, String extra) {
329 finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH)
330 ? VOICE : SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700331 }
332
Michael Kolbfe251992010-07-08 15:41:55 -0700333 @Override
John Reck87369ea2011-01-23 15:20:38 -0800334 public void onItemClick(
335 AdapterView<?> parent, View view, int position, long id) {
336 SuggestItem item = mAdapter.getItem(position);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000337 onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra);
John Reck87369ea2011-01-23 15:20:38 -0800338 }
339
Michael Kolbfe251992010-07-08 15:41:55 -0700340 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700341
Michael Kolbfe251992010-07-08 15:41:55 -0700342 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700343
Michael Kolb257cc2c2010-12-09 09:45:52 -0800344 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700345
Michael Kolb7cdc4902011-02-03 17:54:40 -0800346 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700347
348 }
349
John Reck117f07d2011-01-24 09:39:03 -0800350 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800351 mIncognitoMode = incognito;
352 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800353 }
354
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800355 @Override
356 public boolean onKeyDown(int keyCode, KeyEvent evt) {
357 if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) {
358 finishInput(null, null, null);
359 return true;
360 }
361 return super.onKeyDown(keyCode, evt);
362 }
363
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000364 public SuggestionsAdapter getAdapter() {
365 return mAdapter;
366 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000367
368 private void dispatchChange() {
369 final Rect popupRect = new Rect();
370 getPopupDrawableRect(popupRect);
371
372 if (mDropdownListener != null) {
373 mDropdownListener.onNewDropdownDimensions(popupRect.height());
374 }
375 }
376
377 void registerDropdownChangeListener(DropdownChangeListener d) {
378 mDropdownListener = d;
379 }
Michael Kolb74e60c22011-04-30 16:43:47 -0700380
381 /*
382 * no-op to prevent scrolling of webview when embedded titlebar
383 * gets edited
384 */
385 @Override
386 public boolean requestRectangleOnScreen(Rect rect, boolean immediate) {
387 return false;
388 }
Michael Kolb305b1c52011-06-21 16:16:22 -0700389
390 @Override
391 public void onTextChanged(String newText) {
392 if (StateListener.STATE_HIGHLIGHTED == mState) {
393 changeState(StateListener.STATE_EDITED);
394 }
395 }
396
Michael Kolbfe251992010-07-08 15:41:55 -0700397}