blob: b7f2bff1fb3a21796b433cbfc2cc2467a201d404 [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 Kolb21ce4d22010-09-15 14:55:05 -070019import com.android.browser.SuggestionsAdapter.CompletionListener;
John Reck87369ea2011-01-23 15:20:38 -080020import com.android.browser.SuggestionsAdapter.SuggestItem;
Narayan Kamath5119edd2011-02-23 15:49:17 +000021import com.android.browser.UI.DropdownChangeListener;
Narayan Kamath80aad8d2011-02-23 12:01:13 +000022import com.android.browser.autocomplete.SuggestiveAutoCompleteTextView;
John Reckb77bdc42011-01-24 13:24:48 -080023import com.android.browser.search.SearchEngine;
24import com.android.browser.search.SearchEngineInfo;
25import com.android.browser.search.SearchEngines;
Michael Kolb21ce4d22010-09-15 14:55:05 -070026
Michael Kolbfe251992010-07-08 15:41:55 -070027import android.content.Context;
Michael Kolb21ce4d22010-09-15 14:55:05 -070028import android.content.res.Configuration;
Narayan Kamath5119edd2011-02-23 15:49:17 +000029import android.database.DataSetObserver;
30import android.graphics.Rect;
John Reckdcda1d52010-11-29 10:05:54 -080031import android.text.TextUtils;
Michael Kolbfe251992010-07-08 15:41:55 -070032import android.util.AttributeSet;
John Reckb77bdc42011-01-24 13:24:48 -080033import android.util.Patterns;
Michael Kolbfe251992010-07-08 15:41:55 -070034import android.view.KeyEvent;
Michael Kolbfe251992010-07-08 15:41:55 -070035import android.view.View;
Narayan Kamath5119edd2011-02-23 15:49:17 +000036import android.view.inputmethod.EditorInfo;
Michael Kolbfe251992010-07-08 15:41:55 -070037import android.view.inputmethod.InputMethodManager;
John Reck87369ea2011-01-23 15:20:38 -080038import android.widget.AdapterView;
39import android.widget.AdapterView.OnItemClickListener;
Michael Kolbfe251992010-07-08 15:41:55 -070040import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070041import android.widget.TextView.OnEditorActionListener;
Michael Kolbfe251992010-07-08 15:41:55 -070042
Michael Kolbcfa3af52010-12-14 10:36:11 -080043import java.util.List;
44
Michael Kolbfe251992010-07-08 15:41:55 -070045/**
46 * url/search input view
47 * handling suggestions
48 */
Narayan Kamath80aad8d2011-02-23 12:01:13 +000049public class UrlInputView extends SuggestiveAutoCompleteTextView
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080050 implements OnEditorActionListener,
John Reck87369ea2011-01-23 15:20:38 -080051 CompletionListener, OnItemClickListener {
Michael Kolbfe251992010-07-08 15:41:55 -070052
Michael Kolb257cc2c2010-12-09 09:45:52 -080053
54 static final String TYPED = "browser-type";
55 static final String SUGGESTED = "browser-suggest";
Michael Kolbbd2dd642011-01-13 13:01:30 -080056 static final String VOICE = "voice-search";
Michael Kolb257cc2c2010-12-09 09:45:52 -080057
Michael Kolbfe251992010-07-08 15:41:55 -070058 private UrlInputListener mListener;
59 private InputMethodManager mInputManager;
60 private SuggestionsAdapter mAdapter;
Michael Kolb21ce4d22010-09-15 14:55:05 -070061 private View mContainer;
62 private boolean mLandscape;
John Reckb77bdc42011-01-24 13:24:48 -080063 private boolean mIncognitoMode;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080064 private boolean mNeedsUpdate;
Narayan Kamath5119edd2011-02-23 15:49:17 +000065 private DropdownChangeListener mDropdownListener;
Michael Kolbfe251992010-07-08 15:41:55 -070066
67 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
68 super(context, attrs, defStyle);
69 init(context);
70 }
71
72 public UrlInputView(Context context, AttributeSet attrs) {
73 super(context, attrs);
74 init(context);
75 }
76
77 public UrlInputView(Context context) {
78 super(context);
79 init(context);
80 }
81
82 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070083 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070084 setOnEditorActionListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070085 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070086 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -080087 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -070088 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -080089 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -080090 setOnItemClickListener(this);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080091 mNeedsUpdate = false;
Narayan Kamath5119edd2011-02-23 15:49:17 +000092 mDropdownListener = null;
93
94 mAdapter.registerDataSetObserver(new DataSetObserver() {
95 @Override
96 public void onChanged() {
97 if (!isPopupShowing()) {
98 return;
99 }
100 dispatchChange();
101 }
102
103 @Override
104 public void onInvalidated() {
105 dispatchChange();
106 }
107 });
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800108 }
109
110 /**
111 * check if focus change requires a title bar update
112 */
113 boolean needsUpdate() {
114 return mNeedsUpdate;
115 }
116
117 /**
118 * clear the focus change needs title bar update flag
119 */
120 void clearNeedsUpdate() {
121 mNeedsUpdate = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700122 }
123
Michael Kolbba99c5d2010-11-29 14:57:41 -0800124 void setController(UiController controller) {
125 UrlSelectionActionMode urlSelectionMode
126 = new UrlSelectionActionMode(controller);
127 setCustomSelectionActionModeCallback(urlSelectionMode);
128 }
129
Michael Kolb91902d52011-01-26 17:43:48 -0800130 void setUseQuickControls(boolean useQuickControls) {
Michael Kolb91902d52011-01-26 17:43:48 -0800131 mAdapter.setReverseResults(useQuickControls);
132 }
133
Michael Kolb21ce4d22010-09-15 14:55:05 -0700134 void setContainer(View container) {
135 mContainer = container;
136 }
137
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800138 public void setUrlInputListener(UrlInputListener listener) {
139 mListener = listener;
140 }
141
Michael Kolbcfa3af52010-12-14 10:36:11 -0800142 void setVoiceResults(List<String> voiceResults) {
143 mAdapter.setVoiceResults(voiceResults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800144 }
145
Michael Kolb21ce4d22010-09-15 14:55:05 -0700146 @Override
147 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800148 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700149 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800150 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800151 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700152 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800153 setupDropDown();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000154 performFiltering(getUserText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700155 }
156 }
157
158 @Override
159 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800160 setupDropDown();
161 super.showDropDown();
162 }
163
164 @Override
165 public void dismissDropDown() {
166 super.dismissDropDown();
167 mAdapter.clearCache();
168 }
169
170 private void setupDropDown() {
John Reck92026732011-02-15 10:12:30 -0800171 int width = mContainer != null ? mContainer.getWidth() : getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700172 if (width != getDropDownWidth()) {
173 setDropDownWidth(width);
174 }
175 if (getLeft() != -getDropDownHorizontalOffset()) {
176 setDropDownHorizontalOffset(-getLeft());
177 }
Michael Kolb513286f2010-09-09 12:55:12 -0700178 }
179
180 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700181 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000182 if (BrowserSettings.getInstance().useInstant() &&
183 (actionId == EditorInfo.IME_ACTION_NEXT)) {
184 // When instant is turned on AND the user chooses to complete
185 // using the tab key, then use the completion rather than the
186 // text that the user has typed.
187 finishInput(getText().toString(), null, TYPED);
188 } else {
189 finishInput(getUserText(), null, TYPED);
190 }
191
Michael Kolbed217742010-08-10 17:52:34 -0700192 return true;
193 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700194
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800195 void forceFilter() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000196 performForcedFiltering();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800197 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700198 }
199
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800200 void forceIme() {
John Reck92026732011-02-15 10:12:30 -0800201 mInputManager.focusIn(this);
Michael Kolbfe251992010-07-08 15:41:55 -0700202 mInputManager.showSoftInput(this, 0);
203 }
204
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800205 void hideIME() {
206 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
207 }
208
Michael Kolb257cc2c2010-12-09 09:45:52 -0800209 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800210 mNeedsUpdate = true;
211 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700212 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800213 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700214 mListener.onDismiss();
215 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800216 if (mIncognitoMode && isSearch(url)) {
217 // To prevent logging, intercept this request
218 // TODO: This is a quick hack, refactor this
219 SearchEngine searchEngine = BrowserSettings.getInstance()
220 .getSearchEngine();
221 if (searchEngine == null) return;
222 SearchEngineInfo engineInfo = SearchEngines
223 .getSearchEngineInfo(mContext, searchEngine.getName());
224 if (engineInfo == null) return;
225 url = engineInfo.getSearchUriForQuery(url);
226 // mLister.onAction can take it from here without logging
227 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800228 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700229 }
Michael Kolbfe251992010-07-08 15:41:55 -0700230 }
231
John Reckb77bdc42011-01-24 13:24:48 -0800232 boolean isSearch(String inUrl) {
233 String url = UrlUtils.fixUrl(inUrl).trim();
234 if (TextUtils.isEmpty(url)) return false;
235
236 if (Patterns.WEB_URL.matcher(url).matches()
237 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
238 return false;
239 }
240 return true;
241 }
242
Michael Kolb21ce4d22010-09-15 14:55:05 -0700243 // Completion Listener
244
245 @Override
246 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800247 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700248 }
249
250 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800251 public void onSelect(String url, int type, String extra) {
252 finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH)
253 ? VOICE : SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700254 }
255
Michael Kolbfe251992010-07-08 15:41:55 -0700256 @Override
John Reck87369ea2011-01-23 15:20:38 -0800257 public void onItemClick(
258 AdapterView<?> parent, View view, int position, long id) {
259 SuggestItem item = mAdapter.getItem(position);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000260 onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra);
John Reck87369ea2011-01-23 15:20:38 -0800261 }
262
Michael Kolbfe251992010-07-08 15:41:55 -0700263 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700264
Michael Kolbfe251992010-07-08 15:41:55 -0700265 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700266
Michael Kolb257cc2c2010-12-09 09:45:52 -0800267 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700268
Michael Kolb7cdc4902011-02-03 17:54:40 -0800269 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700270
271 }
272
John Reck117f07d2011-01-24 09:39:03 -0800273 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800274 mIncognitoMode = incognito;
275 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800276 }
277
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800278 @Override
279 public boolean onKeyDown(int keyCode, KeyEvent evt) {
280 if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) {
281 finishInput(null, null, null);
282 return true;
283 }
284 return super.onKeyDown(keyCode, evt);
285 }
286
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000287 public SuggestionsAdapter getAdapter() {
288 return mAdapter;
289 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000290
291 private void dispatchChange() {
292 final Rect popupRect = new Rect();
293 getPopupDrawableRect(popupRect);
294
295 if (mDropdownListener != null) {
296 mDropdownListener.onNewDropdownDimensions(popupRect.height());
297 }
298 }
299
300 void registerDropdownChangeListener(DropdownChangeListener d) {
301 mDropdownListener = d;
302 }
Michael Kolbfe251992010-07-08 15:41:55 -0700303}