blob: 9c5d33814ceb860b9be2a990215cc370eb5d8798 [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 Kolb21ce4d22010-09-15 14:55:05 -0700130 void setContainer(View container) {
131 mContainer = container;
132 }
133
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800134 public void setUrlInputListener(UrlInputListener listener) {
135 mListener = listener;
136 }
137
Michael Kolbcfa3af52010-12-14 10:36:11 -0800138 void setVoiceResults(List<String> voiceResults) {
139 mAdapter.setVoiceResults(voiceResults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800140 }
141
Michael Kolb21ce4d22010-09-15 14:55:05 -0700142 @Override
143 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800144 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700145 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800146 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800147 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700148 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800149 setupDropDown();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000150 performFiltering(getUserText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700151 }
152 }
153
154 @Override
155 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800156 setupDropDown();
157 super.showDropDown();
158 }
159
160 @Override
161 public void dismissDropDown() {
162 super.dismissDropDown();
163 mAdapter.clearCache();
164 }
165
166 private void setupDropDown() {
John Reck92026732011-02-15 10:12:30 -0800167 int width = mContainer != null ? mContainer.getWidth() : getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700168 if (width != getDropDownWidth()) {
169 setDropDownWidth(width);
170 }
171 if (getLeft() != -getDropDownHorizontalOffset()) {
172 setDropDownHorizontalOffset(-getLeft());
173 }
Michael Kolb513286f2010-09-09 12:55:12 -0700174 }
175
176 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700177 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000178 if (BrowserSettings.getInstance().useInstant() &&
179 (actionId == EditorInfo.IME_ACTION_NEXT)) {
180 // When instant is turned on AND the user chooses to complete
181 // using the tab key, then use the completion rather than the
182 // text that the user has typed.
183 finishInput(getText().toString(), null, TYPED);
184 } else {
185 finishInput(getUserText(), null, TYPED);
186 }
187
Michael Kolbed217742010-08-10 17:52:34 -0700188 return true;
189 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700190
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800191 void forceFilter() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000192 performForcedFiltering();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800193 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700194 }
195
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800196 void forceIme() {
John Reck92026732011-02-15 10:12:30 -0800197 mInputManager.focusIn(this);
Michael Kolbfe251992010-07-08 15:41:55 -0700198 mInputManager.showSoftInput(this, 0);
199 }
200
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800201 void hideIME() {
202 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
203 }
204
Michael Kolb257cc2c2010-12-09 09:45:52 -0800205 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800206 mNeedsUpdate = true;
207 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700208 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800209 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700210 mListener.onDismiss();
211 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800212 if (mIncognitoMode && isSearch(url)) {
213 // To prevent logging, intercept this request
214 // TODO: This is a quick hack, refactor this
215 SearchEngine searchEngine = BrowserSettings.getInstance()
216 .getSearchEngine();
217 if (searchEngine == null) return;
218 SearchEngineInfo engineInfo = SearchEngines
219 .getSearchEngineInfo(mContext, searchEngine.getName());
220 if (engineInfo == null) return;
221 url = engineInfo.getSearchUriForQuery(url);
222 // mLister.onAction can take it from here without logging
223 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800224 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700225 }
Michael Kolbfe251992010-07-08 15:41:55 -0700226 }
227
John Reckb77bdc42011-01-24 13:24:48 -0800228 boolean isSearch(String inUrl) {
229 String url = UrlUtils.fixUrl(inUrl).trim();
230 if (TextUtils.isEmpty(url)) return false;
231
232 if (Patterns.WEB_URL.matcher(url).matches()
233 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
234 return false;
235 }
236 return true;
237 }
238
Michael Kolb21ce4d22010-09-15 14:55:05 -0700239 // Completion Listener
240
241 @Override
242 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800243 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700244 }
245
246 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800247 public void onSelect(String url, int type, String extra) {
248 finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH)
249 ? VOICE : SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700250 }
251
Michael Kolbfe251992010-07-08 15:41:55 -0700252 @Override
John Reck87369ea2011-01-23 15:20:38 -0800253 public void onItemClick(
254 AdapterView<?> parent, View view, int position, long id) {
255 SuggestItem item = mAdapter.getItem(position);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000256 onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra);
John Reck87369ea2011-01-23 15:20:38 -0800257 }
258
Michael Kolbfe251992010-07-08 15:41:55 -0700259 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700260
Michael Kolbfe251992010-07-08 15:41:55 -0700261 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700262
Michael Kolb257cc2c2010-12-09 09:45:52 -0800263 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700264
Michael Kolb7cdc4902011-02-03 17:54:40 -0800265 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700266
267 }
268
John Reck117f07d2011-01-24 09:39:03 -0800269 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800270 mIncognitoMode = incognito;
271 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800272 }
273
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800274 @Override
275 public boolean onKeyDown(int keyCode, KeyEvent evt) {
276 if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) {
277 finishInput(null, null, null);
278 return true;
279 }
280 return super.onKeyDown(keyCode, evt);
281 }
282
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000283 public SuggestionsAdapter getAdapter() {
284 return mAdapter;
285 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000286
287 private void dispatchChange() {
288 final Rect popupRect = new Rect();
289 getPopupDrawableRect(popupRect);
290
291 if (mDropdownListener != null) {
292 mDropdownListener.onNewDropdownDimensions(popupRect.height());
293 }
294 }
295
296 void registerDropdownChangeListener(DropdownChangeListener d) {
297 mDropdownListener = d;
298 }
Michael Kolb1214af32011-05-05 15:26:37 -0700299
300 /*
301 * no-op to prevent scrolling of webview when embedded titlebar is edited
302 */
303 @Override
304 public boolean requestRectangleOnScreen(Rect rect, boolean immediate) {
305 return false;
306 }
307
Michael Kolbfe251992010-07-08 15:41:55 -0700308}