blob: 2acc69b05aa921edf73c5b52b47b4e6d223535a2 [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 Kolb5e8f2b92011-07-19 13:22:32 -0700190 int getState() {
191 return mState;
192 }
193
Michael Kolbcfa3af52010-12-14 10:36:11 -0800194 void setVoiceResults(List<String> voiceResults) {
195 mAdapter.setVoiceResults(voiceResults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800196 }
197
Michael Kolb21ce4d22010-09-15 14:55:05 -0700198 @Override
199 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800200 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700201 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800202 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800203 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700204 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800205 setupDropDown();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000206 performFiltering(getUserText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700207 }
208 }
209
210 @Override
211 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800212 setupDropDown();
213 super.showDropDown();
214 }
215
216 @Override
217 public void dismissDropDown() {
218 super.dismissDropDown();
219 mAdapter.clearCache();
220 }
221
222 private void setupDropDown() {
John Reck92026732011-02-15 10:12:30 -0800223 int width = mContainer != null ? mContainer.getWidth() : getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700224 if (width != getDropDownWidth()) {
225 setDropDownWidth(width);
226 }
227 if (getLeft() != -getDropDownHorizontalOffset()) {
228 setDropDownHorizontalOffset(-getLeft());
229 }
Michael Kolb513286f2010-09-09 12:55:12 -0700230 }
231
232 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700233 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
John Reck35e9dd62011-04-25 09:01:54 -0700234 if (BrowserSettings.getInstance().useInstantSearch() &&
Narayan Kamath5119edd2011-02-23 15:49:17 +0000235 (actionId == EditorInfo.IME_ACTION_NEXT)) {
236 // When instant is turned on AND the user chooses to complete
237 // using the tab key, then use the completion rather than the
238 // text that the user has typed.
239 finishInput(getText().toString(), null, TYPED);
240 } else {
241 finishInput(getUserText(), null, TYPED);
242 }
243
Michael Kolbed217742010-08-10 17:52:34 -0700244 return true;
245 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700246
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800247 void forceFilter() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000248 performForcedFiltering();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800249 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700250 }
251
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800252 void forceIme() {
John Reck92026732011-02-15 10:12:30 -0800253 mInputManager.focusIn(this);
Michael Kolbfe251992010-07-08 15:41:55 -0700254 mInputManager.showSoftInput(this, 0);
255 }
256
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800257 void hideIME() {
258 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
259 }
260
Michael Kolb257cc2c2010-12-09 09:45:52 -0800261 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800262 mNeedsUpdate = true;
263 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700264 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800265 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700266 mListener.onDismiss();
267 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800268 if (mIncognitoMode && isSearch(url)) {
269 // To prevent logging, intercept this request
270 // TODO: This is a quick hack, refactor this
271 SearchEngine searchEngine = BrowserSettings.getInstance()
272 .getSearchEngine();
273 if (searchEngine == null) return;
274 SearchEngineInfo engineInfo = SearchEngines
275 .getSearchEngineInfo(mContext, searchEngine.getName());
276 if (engineInfo == null) return;
277 url = engineInfo.getSearchUriForQuery(url);
278 // mLister.onAction can take it from here without logging
279 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800280 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700281 }
Michael Kolbfe251992010-07-08 15:41:55 -0700282 }
283
John Reckb77bdc42011-01-24 13:24:48 -0800284 boolean isSearch(String inUrl) {
285 String url = UrlUtils.fixUrl(inUrl).trim();
286 if (TextUtils.isEmpty(url)) return false;
287
288 if (Patterns.WEB_URL.matcher(url).matches()
289 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
290 return false;
291 }
292 return true;
293 }
294
Michael Kolb21ce4d22010-09-15 14:55:05 -0700295 // Completion Listener
296
297 @Override
298 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800299 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700300 }
301
302 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800303 public void onSelect(String url, int type, String extra) {
304 finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH)
305 ? VOICE : SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700306 }
307
Michael Kolbfe251992010-07-08 15:41:55 -0700308 @Override
John Reck87369ea2011-01-23 15:20:38 -0800309 public void onItemClick(
310 AdapterView<?> parent, View view, int position, long id) {
311 SuggestItem item = mAdapter.getItem(position);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000312 onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra);
John Reck87369ea2011-01-23 15:20:38 -0800313 }
314
Michael Kolbfe251992010-07-08 15:41:55 -0700315 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700316
Michael Kolbfe251992010-07-08 15:41:55 -0700317 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700318
Michael Kolb257cc2c2010-12-09 09:45:52 -0800319 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700320
Michael Kolb7cdc4902011-02-03 17:54:40 -0800321 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700322
323 }
324
John Reck117f07d2011-01-24 09:39:03 -0800325 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800326 mIncognitoMode = incognito;
327 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800328 }
329
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800330 @Override
331 public boolean onKeyDown(int keyCode, KeyEvent evt) {
332 if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) {
333 finishInput(null, null, null);
334 return true;
335 }
336 return super.onKeyDown(keyCode, evt);
337 }
338
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000339 public SuggestionsAdapter getAdapter() {
340 return mAdapter;
341 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000342
343 private void dispatchChange() {
344 final Rect popupRect = new Rect();
345 getPopupDrawableRect(popupRect);
346
347 if (mDropdownListener != null) {
348 mDropdownListener.onNewDropdownDimensions(popupRect.height());
349 }
350 }
351
352 void registerDropdownChangeListener(DropdownChangeListener d) {
353 mDropdownListener = d;
354 }
Michael Kolb74e60c22011-04-30 16:43:47 -0700355
356 /*
357 * no-op to prevent scrolling of webview when embedded titlebar
358 * gets edited
359 */
360 @Override
361 public boolean requestRectangleOnScreen(Rect rect, boolean immediate) {
362 return false;
363 }
Michael Kolb305b1c52011-06-21 16:16:22 -0700364
365 @Override
366 public void onTextChanged(String newText) {
367 if (StateListener.STATE_HIGHLIGHTED == mState) {
368 changeState(StateListener.STATE_EDITED);
369 }
370 }
371
Michael Kolbfe251992010-07-08 15:41:55 -0700372}