blob: fbdf40056b6a57291be5c00c81913eb8d26a2c56 [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;
Narayan Kamath5119edd2011-02-23 15:49:17 +000021import android.database.DataSetObserver;
22import android.graphics.Rect;
John Reckdcda1d52010-11-29 10:05:54 -080023import android.text.TextUtils;
Michael Kolbfe251992010-07-08 15:41:55 -070024import android.util.AttributeSet;
John Reckb77bdc42011-01-24 13:24:48 -080025import android.util.Patterns;
Michael Kolbfe251992010-07-08 15:41:55 -070026import android.view.KeyEvent;
Michael Kolb305b1c52011-06-21 16:16:22 -070027import android.view.MotionEvent;
Michael Kolbfe251992010-07-08 15:41:55 -070028import android.view.View;
Narayan Kamath5119edd2011-02-23 15:49:17 +000029import android.view.inputmethod.EditorInfo;
Michael Kolbfe251992010-07-08 15:41:55 -070030import android.view.inputmethod.InputMethodManager;
John Reck87369ea2011-01-23 15:20:38 -080031import android.widget.AdapterView;
32import android.widget.AdapterView.OnItemClickListener;
Michael Kolbfe251992010-07-08 15:41:55 -070033import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070034import android.widget.TextView.OnEditorActionListener;
Michael Kolbfe251992010-07-08 15:41:55 -070035
Michael Kolb305b1c52011-06-21 16:16:22 -070036import com.android.browser.SuggestionsAdapter.CompletionListener;
37import com.android.browser.SuggestionsAdapter.SuggestItem;
38import com.android.browser.UI.DropdownChangeListener;
39import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
40import com.android.browser.autocomplete.SuggestiveAutoCompleteTextView;
41import com.android.browser.search.SearchEngine;
42import com.android.browser.search.SearchEngineInfo;
43import com.android.browser.search.SearchEngines;
44
Michael Kolbcfa3af52010-12-14 10:36:11 -080045import java.util.List;
46
Michael Kolbfe251992010-07-08 15:41:55 -070047/**
48 * url/search input view
49 * handling suggestions
50 */
Narayan Kamath80aad8d2011-02-23 12:01:13 +000051public class UrlInputView extends SuggestiveAutoCompleteTextView
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080052 implements OnEditorActionListener,
Michael Kolb305b1c52011-06-21 16:16:22 -070053 CompletionListener, OnItemClickListener, TextChangeWatcher {
Michael Kolb257cc2c2010-12-09 09:45:52 -080054
55 static final String TYPED = "browser-type";
56 static final String SUGGESTED = "browser-suggest";
Michael Kolbbd2dd642011-01-13 13:01:30 -080057 static final String VOICE = "voice-search";
Michael Kolb257cc2c2010-12-09 09:45:52 -080058
Michael Kolb94ec5272011-08-31 16:23:57 -070059 static final int POST_DELAY = 100;
60
Michael Kolb305b1c52011-06-21 16:16:22 -070061 static interface StateListener {
62 static final int STATE_NORMAL = 0;
63 static final int STATE_HIGHLIGHTED = 1;
64 static final int STATE_EDITED = 2;
65
66 public void onStateChanged(int state);
67 }
68
Michael Kolbfe251992010-07-08 15:41:55 -070069 private UrlInputListener mListener;
70 private InputMethodManager mInputManager;
71 private SuggestionsAdapter mAdapter;
Michael Kolb21ce4d22010-09-15 14:55:05 -070072 private View mContainer;
73 private boolean mLandscape;
John Reckb77bdc42011-01-24 13:24:48 -080074 private boolean mIncognitoMode;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080075 private boolean mNeedsUpdate;
Narayan Kamath5119edd2011-02-23 15:49:17 +000076 private DropdownChangeListener mDropdownListener;
Michael Kolbfe251992010-07-08 15:41:55 -070077
Michael Kolb305b1c52011-06-21 16:16:22 -070078 private int mState;
79 private StateListener mStateListener;
80
Michael Kolbfe251992010-07-08 15:41:55 -070081 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
82 super(context, attrs, defStyle);
83 init(context);
84 }
85
86 public UrlInputView(Context context, AttributeSet attrs) {
87 super(context, attrs);
88 init(context);
89 }
90
91 public UrlInputView(Context context) {
92 super(context);
93 init(context);
94 }
95
96 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070097 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070098 setOnEditorActionListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070099 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -0700100 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -0800101 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700102 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -0800103 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -0800104 setOnItemClickListener(this);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800105 mNeedsUpdate = false;
Narayan Kamath5119edd2011-02-23 15:49:17 +0000106 mDropdownListener = null;
Michael Kolb305b1c52011-06-21 16:16:22 -0700107 addQueryTextWatcher(this);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000108
109 mAdapter.registerDataSetObserver(new DataSetObserver() {
110 @Override
111 public void onChanged() {
112 if (!isPopupShowing()) {
113 return;
114 }
115 dispatchChange();
116 }
117
118 @Override
119 public void onInvalidated() {
120 dispatchChange();
121 }
122 });
Michael Kolb305b1c52011-06-21 16:16:22 -0700123 mState = StateListener.STATE_NORMAL;
124 }
125
126 protected void onFocusChanged(boolean focused, int direction, Rect prevRect) {
127 super.onFocusChanged(focused, direction, prevRect);
Michael Kolb94ec5272011-08-31 16:23:57 -0700128 int state = -1;
Michael Kolb305b1c52011-06-21 16:16:22 -0700129 if (focused) {
130 if (hasSelection()) {
Michael Kolb94ec5272011-08-31 16:23:57 -0700131 state = StateListener.STATE_HIGHLIGHTED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700132 } else {
Michael Kolb94ec5272011-08-31 16:23:57 -0700133 state = StateListener.STATE_EDITED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700134 }
135 } else {
136 // reset the selection state
Michael Kolb94ec5272011-08-31 16:23:57 -0700137 state = StateListener.STATE_NORMAL;
Michael Kolb305b1c52011-06-21 16:16:22 -0700138 }
Michael Kolb94ec5272011-08-31 16:23:57 -0700139 final int s = state;
140 post(new Runnable() {
141 public void run() {
142 changeState(s);
143 }
144 });
Michael Kolb305b1c52011-06-21 16:16:22 -0700145 }
146
147 @Override
148 public boolean onTouchEvent(MotionEvent evt) {
149 boolean hasSelection = hasSelection();
150 boolean res = super.onTouchEvent(evt);
151 if ((MotionEvent.ACTION_DOWN == evt.getActionMasked())
152 && hasSelection) {
Michael Kolb94ec5272011-08-31 16:23:57 -0700153 postDelayed(new Runnable() {
154 public void run() {
155 changeState(StateListener.STATE_EDITED);
156 }}, POST_DELAY);
Michael Kolb305b1c52011-06-21 16:16:22 -0700157 }
158 return res;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800159 }
160
161 /**
162 * check if focus change requires a title bar update
163 */
164 boolean needsUpdate() {
165 return mNeedsUpdate;
166 }
167
168 /**
169 * clear the focus change needs title bar update flag
170 */
171 void clearNeedsUpdate() {
172 mNeedsUpdate = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700173 }
174
Michael Kolbba99c5d2010-11-29 14:57:41 -0800175 void setController(UiController controller) {
176 UrlSelectionActionMode urlSelectionMode
177 = new UrlSelectionActionMode(controller);
178 setCustomSelectionActionModeCallback(urlSelectionMode);
179 }
180
Michael Kolb21ce4d22010-09-15 14:55:05 -0700181 void setContainer(View container) {
182 mContainer = container;
183 }
184
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800185 public void setUrlInputListener(UrlInputListener listener) {
186 mListener = listener;
187 }
188
Michael Kolb305b1c52011-06-21 16:16:22 -0700189 public void setStateListener(StateListener listener) {
190 mStateListener = listener;
191 // update listener
192 changeState(mState);
193 }
194
195 private void changeState(int newState) {
196 mState = newState;
197 if (mStateListener != null) {
198 mStateListener.onStateChanged(mState);
199 }
200 }
201
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700202 int getState() {
203 return mState;
204 }
205
Michael Kolbcfa3af52010-12-14 10:36:11 -0800206 void setVoiceResults(List<String> voiceResults) {
207 mAdapter.setVoiceResults(voiceResults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800208 }
209
Michael Kolb21ce4d22010-09-15 14:55:05 -0700210 @Override
211 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800212 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700213 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800214 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800215 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700216 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800217 setupDropDown();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000218 performFiltering(getUserText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700219 }
220 }
221
222 @Override
223 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800224 setupDropDown();
225 super.showDropDown();
226 }
227
228 @Override
229 public void dismissDropDown() {
230 super.dismissDropDown();
231 mAdapter.clearCache();
232 }
233
234 private void setupDropDown() {
John Reck92026732011-02-15 10:12:30 -0800235 int width = mContainer != null ? mContainer.getWidth() : getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700236 if (width != getDropDownWidth()) {
237 setDropDownWidth(width);
238 }
239 if (getLeft() != -getDropDownHorizontalOffset()) {
240 setDropDownHorizontalOffset(-getLeft());
241 }
Michael Kolb513286f2010-09-09 12:55:12 -0700242 }
243
244 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700245 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
John Reck35e9dd62011-04-25 09:01:54 -0700246 if (BrowserSettings.getInstance().useInstantSearch() &&
Narayan Kamath5119edd2011-02-23 15:49:17 +0000247 (actionId == EditorInfo.IME_ACTION_NEXT)) {
248 // When instant is turned on AND the user chooses to complete
249 // using the tab key, then use the completion rather than the
250 // text that the user has typed.
251 finishInput(getText().toString(), null, TYPED);
252 } else {
253 finishInput(getUserText(), null, TYPED);
254 }
255
Michael Kolbed217742010-08-10 17:52:34 -0700256 return true;
257 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700258
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800259 void forceFilter() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000260 performForcedFiltering();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800261 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700262 }
263
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800264 void forceIme() {
John Reck92026732011-02-15 10:12:30 -0800265 mInputManager.focusIn(this);
Michael Kolbfe251992010-07-08 15:41:55 -0700266 mInputManager.showSoftInput(this, 0);
267 }
268
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800269 void hideIME() {
270 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
271 }
272
Michael Kolb257cc2c2010-12-09 09:45:52 -0800273 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800274 mNeedsUpdate = true;
275 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700276 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800277 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700278 mListener.onDismiss();
279 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800280 if (mIncognitoMode && isSearch(url)) {
281 // To prevent logging, intercept this request
282 // TODO: This is a quick hack, refactor this
283 SearchEngine searchEngine = BrowserSettings.getInstance()
284 .getSearchEngine();
285 if (searchEngine == null) return;
286 SearchEngineInfo engineInfo = SearchEngines
287 .getSearchEngineInfo(mContext, searchEngine.getName());
288 if (engineInfo == null) return;
289 url = engineInfo.getSearchUriForQuery(url);
290 // mLister.onAction can take it from here without logging
291 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800292 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700293 }
Michael Kolbfe251992010-07-08 15:41:55 -0700294 }
295
John Reckb77bdc42011-01-24 13:24:48 -0800296 boolean isSearch(String inUrl) {
297 String url = UrlUtils.fixUrl(inUrl).trim();
298 if (TextUtils.isEmpty(url)) return false;
299
300 if (Patterns.WEB_URL.matcher(url).matches()
301 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
302 return false;
303 }
304 return true;
305 }
306
Michael Kolb21ce4d22010-09-15 14:55:05 -0700307 // Completion Listener
308
309 @Override
310 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800311 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700312 }
313
314 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800315 public void onSelect(String url, int type, String extra) {
316 finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH)
317 ? VOICE : SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700318 }
319
Michael Kolbfe251992010-07-08 15:41:55 -0700320 @Override
John Reck87369ea2011-01-23 15:20:38 -0800321 public void onItemClick(
322 AdapterView<?> parent, View view, int position, long id) {
323 SuggestItem item = mAdapter.getItem(position);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000324 onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra);
John Reck87369ea2011-01-23 15:20:38 -0800325 }
326
Michael Kolbfe251992010-07-08 15:41:55 -0700327 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700328
Michael Kolbfe251992010-07-08 15:41:55 -0700329 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700330
Michael Kolb257cc2c2010-12-09 09:45:52 -0800331 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700332
Michael Kolb7cdc4902011-02-03 17:54:40 -0800333 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700334
335 }
336
John Reck117f07d2011-01-24 09:39:03 -0800337 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800338 mIncognitoMode = incognito;
339 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800340 }
341
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800342 @Override
343 public boolean onKeyDown(int keyCode, KeyEvent evt) {
344 if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) {
345 finishInput(null, null, null);
346 return true;
347 }
348 return super.onKeyDown(keyCode, evt);
349 }
350
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000351 public SuggestionsAdapter getAdapter() {
352 return mAdapter;
353 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000354
355 private void dispatchChange() {
356 final Rect popupRect = new Rect();
357 getPopupDrawableRect(popupRect);
358
359 if (mDropdownListener != null) {
360 mDropdownListener.onNewDropdownDimensions(popupRect.height());
361 }
362 }
363
364 void registerDropdownChangeListener(DropdownChangeListener d) {
365 mDropdownListener = d;
366 }
Michael Kolb74e60c22011-04-30 16:43:47 -0700367
368 /*
369 * no-op to prevent scrolling of webview when embedded titlebar
370 * gets edited
371 */
372 @Override
373 public boolean requestRectangleOnScreen(Rect rect, boolean immediate) {
374 return false;
375 }
Michael Kolb305b1c52011-06-21 16:16:22 -0700376
377 @Override
378 public void onTextChanged(String newText) {
379 if (StateListener.STATE_HIGHLIGHTED == mState) {
380 changeState(StateListener.STATE_EDITED);
381 }
382 }
383
Michael Kolbfe251992010-07-08 15:41:55 -0700384}