blob: 8b5c29289d9e2879dcf94f26e0a786676b0d688c [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 Kolb305b1c52011-06-21 16:16:22 -070059 static interface StateListener {
60 static final int STATE_NORMAL = 0;
61 static final int STATE_HIGHLIGHTED = 1;
62 static final int STATE_EDITED = 2;
63
64 public void onStateChanged(int state);
65 }
66
Michael Kolbfe251992010-07-08 15:41:55 -070067 private UrlInputListener mListener;
68 private InputMethodManager mInputManager;
69 private SuggestionsAdapter mAdapter;
Michael Kolb21ce4d22010-09-15 14:55:05 -070070 private View mContainer;
71 private boolean mLandscape;
John Reckb77bdc42011-01-24 13:24:48 -080072 private boolean mIncognitoMode;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080073 private boolean mNeedsUpdate;
Narayan Kamath5119edd2011-02-23 15:49:17 +000074 private DropdownChangeListener mDropdownListener;
Michael Kolbfe251992010-07-08 15:41:55 -070075
Michael Kolb305b1c52011-06-21 16:16:22 -070076 private int mState;
77 private StateListener mStateListener;
78
Michael Kolbfe251992010-07-08 15:41:55 -070079 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
80 super(context, attrs, defStyle);
81 init(context);
82 }
83
84 public UrlInputView(Context context, AttributeSet attrs) {
85 super(context, attrs);
86 init(context);
87 }
88
89 public UrlInputView(Context context) {
90 super(context);
91 init(context);
92 }
93
94 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070095 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070096 setOnEditorActionListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070097 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070098 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -080099 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700100 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -0800101 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -0800102 setOnItemClickListener(this);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800103 mNeedsUpdate = false;
Narayan Kamath5119edd2011-02-23 15:49:17 +0000104 mDropdownListener = null;
Michael Kolb305b1c52011-06-21 16:16:22 -0700105 addQueryTextWatcher(this);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000106
107 mAdapter.registerDataSetObserver(new DataSetObserver() {
108 @Override
109 public void onChanged() {
110 if (!isPopupShowing()) {
111 return;
112 }
113 dispatchChange();
114 }
115
116 @Override
117 public void onInvalidated() {
118 dispatchChange();
119 }
120 });
Michael Kolb305b1c52011-06-21 16:16:22 -0700121 mState = StateListener.STATE_NORMAL;
122 }
123
124 protected void onFocusChanged(boolean focused, int direction, Rect prevRect) {
125 super.onFocusChanged(focused, direction, prevRect);
126 if (focused) {
127 if (hasSelection()) {
128 changeState(StateListener.STATE_HIGHLIGHTED);
129 } else {
130 changeState(StateListener.STATE_EDITED);
131 }
132 } else {
133 // reset the selection state
134 changeState(StateListener.STATE_NORMAL);
135 }
136 }
137
138 @Override
139 public boolean onTouchEvent(MotionEvent evt) {
140 boolean hasSelection = hasSelection();
141 boolean res = super.onTouchEvent(evt);
142 if ((MotionEvent.ACTION_DOWN == evt.getActionMasked())
143 && hasSelection) {
144 changeState(StateListener.STATE_EDITED);
145 }
146 return res;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800147 }
148
149 /**
150 * check if focus change requires a title bar update
151 */
152 boolean needsUpdate() {
153 return mNeedsUpdate;
154 }
155
156 /**
157 * clear the focus change needs title bar update flag
158 */
159 void clearNeedsUpdate() {
160 mNeedsUpdate = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700161 }
162
Michael Kolbba99c5d2010-11-29 14:57:41 -0800163 void setController(UiController controller) {
164 UrlSelectionActionMode urlSelectionMode
165 = new UrlSelectionActionMode(controller);
166 setCustomSelectionActionModeCallback(urlSelectionMode);
167 }
168
Michael Kolb21ce4d22010-09-15 14:55:05 -0700169 void setContainer(View container) {
170 mContainer = container;
171 }
172
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800173 public void setUrlInputListener(UrlInputListener listener) {
174 mListener = listener;
175 }
176
Michael Kolb305b1c52011-06-21 16:16:22 -0700177 public void setStateListener(StateListener listener) {
178 mStateListener = listener;
179 // update listener
180 changeState(mState);
181 }
182
183 private void changeState(int newState) {
184 mState = newState;
185 if (mStateListener != null) {
186 mStateListener.onStateChanged(mState);
187 }
188 }
189
Michael Kolbcfa3af52010-12-14 10:36:11 -0800190 void setVoiceResults(List<String> voiceResults) {
191 mAdapter.setVoiceResults(voiceResults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800192 }
193
Michael Kolb21ce4d22010-09-15 14:55:05 -0700194 @Override
195 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800196 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700197 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800198 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800199 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700200 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800201 setupDropDown();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000202 performFiltering(getUserText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700203 }
204 }
205
206 @Override
207 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800208 setupDropDown();
209 super.showDropDown();
210 }
211
212 @Override
213 public void dismissDropDown() {
214 super.dismissDropDown();
215 mAdapter.clearCache();
216 }
217
218 private void setupDropDown() {
John Reck92026732011-02-15 10:12:30 -0800219 int width = mContainer != null ? mContainer.getWidth() : getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700220 if (width != getDropDownWidth()) {
221 setDropDownWidth(width);
222 }
223 if (getLeft() != -getDropDownHorizontalOffset()) {
224 setDropDownHorizontalOffset(-getLeft());
225 }
Michael Kolb513286f2010-09-09 12:55:12 -0700226 }
227
228 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700229 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
John Reck35e9dd62011-04-25 09:01:54 -0700230 if (BrowserSettings.getInstance().useInstantSearch() &&
Narayan Kamath5119edd2011-02-23 15:49:17 +0000231 (actionId == EditorInfo.IME_ACTION_NEXT)) {
232 // When instant is turned on AND the user chooses to complete
233 // using the tab key, then use the completion rather than the
234 // text that the user has typed.
235 finishInput(getText().toString(), null, TYPED);
236 } else {
237 finishInput(getUserText(), null, TYPED);
238 }
239
Michael Kolbed217742010-08-10 17:52:34 -0700240 return true;
241 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700242
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800243 void forceFilter() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000244 performForcedFiltering();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800245 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700246 }
247
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800248 void forceIme() {
John Reck92026732011-02-15 10:12:30 -0800249 mInputManager.focusIn(this);
Michael Kolbfe251992010-07-08 15:41:55 -0700250 mInputManager.showSoftInput(this, 0);
251 }
252
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800253 void hideIME() {
254 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
255 }
256
Michael Kolb257cc2c2010-12-09 09:45:52 -0800257 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800258 mNeedsUpdate = true;
259 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700260 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800261 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700262 mListener.onDismiss();
263 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800264 if (mIncognitoMode && isSearch(url)) {
265 // To prevent logging, intercept this request
266 // TODO: This is a quick hack, refactor this
267 SearchEngine searchEngine = BrowserSettings.getInstance()
268 .getSearchEngine();
269 if (searchEngine == null) return;
270 SearchEngineInfo engineInfo = SearchEngines
271 .getSearchEngineInfo(mContext, searchEngine.getName());
272 if (engineInfo == null) return;
273 url = engineInfo.getSearchUriForQuery(url);
274 // mLister.onAction can take it from here without logging
275 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800276 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700277 }
Michael Kolbfe251992010-07-08 15:41:55 -0700278 }
279
John Reckb77bdc42011-01-24 13:24:48 -0800280 boolean isSearch(String inUrl) {
281 String url = UrlUtils.fixUrl(inUrl).trim();
282 if (TextUtils.isEmpty(url)) return false;
283
284 if (Patterns.WEB_URL.matcher(url).matches()
285 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
286 return false;
287 }
288 return true;
289 }
290
Michael Kolb21ce4d22010-09-15 14:55:05 -0700291 // Completion Listener
292
293 @Override
294 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800295 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700296 }
297
298 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800299 public void onSelect(String url, int type, String extra) {
300 finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH)
301 ? VOICE : SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700302 }
303
Michael Kolbfe251992010-07-08 15:41:55 -0700304 @Override
John Reck87369ea2011-01-23 15:20:38 -0800305 public void onItemClick(
306 AdapterView<?> parent, View view, int position, long id) {
307 SuggestItem item = mAdapter.getItem(position);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000308 onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra);
John Reck87369ea2011-01-23 15:20:38 -0800309 }
310
Michael Kolbfe251992010-07-08 15:41:55 -0700311 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700312
Michael Kolbfe251992010-07-08 15:41:55 -0700313 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700314
Michael Kolb257cc2c2010-12-09 09:45:52 -0800315 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700316
Michael Kolb7cdc4902011-02-03 17:54:40 -0800317 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700318
319 }
320
John Reck117f07d2011-01-24 09:39:03 -0800321 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800322 mIncognitoMode = incognito;
323 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800324 }
325
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800326 @Override
327 public boolean onKeyDown(int keyCode, KeyEvent evt) {
328 if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) {
329 finishInput(null, null, null);
330 return true;
331 }
332 return super.onKeyDown(keyCode, evt);
333 }
334
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000335 public SuggestionsAdapter getAdapter() {
336 return mAdapter;
337 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000338
339 private void dispatchChange() {
340 final Rect popupRect = new Rect();
341 getPopupDrawableRect(popupRect);
342
343 if (mDropdownListener != null) {
344 mDropdownListener.onNewDropdownDimensions(popupRect.height());
345 }
346 }
347
348 void registerDropdownChangeListener(DropdownChangeListener d) {
349 mDropdownListener = d;
350 }
Michael Kolb74e60c22011-04-30 16:43:47 -0700351
352 /*
353 * no-op to prevent scrolling of webview when embedded titlebar
354 * gets edited
355 */
356 @Override
357 public boolean requestRectangleOnScreen(Rect rect, boolean immediate) {
358 return false;
359 }
Michael Kolb305b1c52011-06-21 16:16:22 -0700360
361 @Override
362 public void onTextChanged(String newText) {
363 if (StateListener.STATE_HIGHLIGHTED == mState) {
364 changeState(StateListener.STATE_EDITED);
365 }
366 }
367
Michael Kolbfe251992010-07-08 15:41:55 -0700368}