blob: 5e6684db761fe52ea53d101a1c12de1e2e5b2b74 [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 Kolb91902d52011-01-26 17:43:48 -080064 private int mVOffset;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080065 private boolean mNeedsUpdate;
Narayan Kamath5119edd2011-02-23 15:49:17 +000066 private DropdownChangeListener mDropdownListener;
Michael Kolbfe251992010-07-08 15:41:55 -070067
68 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
69 super(context, attrs, defStyle);
70 init(context);
71 }
72
73 public UrlInputView(Context context, AttributeSet attrs) {
74 super(context, attrs);
75 init(context);
76 }
77
78 public UrlInputView(Context context) {
79 super(context);
80 init(context);
81 }
82
83 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070084 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070085 setOnEditorActionListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070086 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070087 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -080088 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -070089 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -080090 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -080091 setOnItemClickListener(this);
Michael Kolb91902d52011-01-26 17:43:48 -080092 mVOffset = 0;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080093 mNeedsUpdate = false;
Narayan Kamath5119edd2011-02-23 15:49:17 +000094 mDropdownListener = null;
95
96 mAdapter.registerDataSetObserver(new DataSetObserver() {
97 @Override
98 public void onChanged() {
99 if (!isPopupShowing()) {
100 return;
101 }
102 dispatchChange();
103 }
104
105 @Override
106 public void onInvalidated() {
107 dispatchChange();
108 }
109 });
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800110 }
111
112 /**
113 * check if focus change requires a title bar update
114 */
115 boolean needsUpdate() {
116 return mNeedsUpdate;
117 }
118
119 /**
120 * clear the focus change needs title bar update flag
121 */
122 void clearNeedsUpdate() {
123 mNeedsUpdate = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700124 }
125
Michael Kolbba99c5d2010-11-29 14:57:41 -0800126 void setController(UiController controller) {
127 UrlSelectionActionMode urlSelectionMode
128 = new UrlSelectionActionMode(controller);
129 setCustomSelectionActionModeCallback(urlSelectionMode);
130 }
131
Michael Kolb91902d52011-01-26 17:43:48 -0800132 void setUseQuickControls(boolean useQuickControls) {
133 mVOffset = (useQuickControls
134 ? (int) getResources().getDimension(R.dimen.dropdown_offset)
135 : 0);
136 mAdapter.setReverseResults(useQuickControls);
137 }
138
Michael Kolb21ce4d22010-09-15 14:55:05 -0700139 void setContainer(View container) {
140 mContainer = container;
141 }
142
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800143 public void setUrlInputListener(UrlInputListener listener) {
144 mListener = listener;
145 }
146
Michael Kolbcfa3af52010-12-14 10:36:11 -0800147 void setVoiceResults(List<String> voiceResults) {
148 mAdapter.setVoiceResults(voiceResults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800149 }
150
Michael Kolb21ce4d22010-09-15 14:55:05 -0700151 @Override
152 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800153 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700154 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800155 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800156 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700157 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800158 setupDropDown();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000159 performFiltering(getUserText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700160 }
161 }
162
163 @Override
164 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800165 setupDropDown();
166 super.showDropDown();
167 }
168
169 @Override
170 public void dismissDropDown() {
171 super.dismissDropDown();
172 mAdapter.clearCache();
173 }
174
175 private void setupDropDown() {
John Reck92026732011-02-15 10:12:30 -0800176 int width = mContainer != null ? mContainer.getWidth() : getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700177 if (width != getDropDownWidth()) {
178 setDropDownWidth(width);
179 }
180 if (getLeft() != -getDropDownHorizontalOffset()) {
181 setDropDownHorizontalOffset(-getLeft());
182 }
Michael Kolb91902d52011-01-26 17:43:48 -0800183 setDropDownVerticalOffset(mVOffset);
Michael Kolb513286f2010-09-09 12:55:12 -0700184 }
185
186 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700187 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000188 if (BrowserSettings.getInstance().useInstant() &&
189 (actionId == EditorInfo.IME_ACTION_NEXT)) {
190 // When instant is turned on AND the user chooses to complete
191 // using the tab key, then use the completion rather than the
192 // text that the user has typed.
193 finishInput(getText().toString(), null, TYPED);
194 } else {
195 finishInput(getUserText(), null, TYPED);
196 }
197
Michael Kolbed217742010-08-10 17:52:34 -0700198 return true;
199 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700200
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800201 void forceFilter() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000202 performForcedFiltering();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800203 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700204 }
205
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800206 void forceIme() {
John Reck92026732011-02-15 10:12:30 -0800207 mInputManager.focusIn(this);
Michael Kolbfe251992010-07-08 15:41:55 -0700208 mInputManager.showSoftInput(this, 0);
209 }
210
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800211 void hideIME() {
212 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
213 }
214
Michael Kolb257cc2c2010-12-09 09:45:52 -0800215 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800216 mNeedsUpdate = true;
217 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700218 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800219 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700220 mListener.onDismiss();
221 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800222 if (mIncognitoMode && isSearch(url)) {
223 // To prevent logging, intercept this request
224 // TODO: This is a quick hack, refactor this
225 SearchEngine searchEngine = BrowserSettings.getInstance()
226 .getSearchEngine();
227 if (searchEngine == null) return;
228 SearchEngineInfo engineInfo = SearchEngines
229 .getSearchEngineInfo(mContext, searchEngine.getName());
230 if (engineInfo == null) return;
231 url = engineInfo.getSearchUriForQuery(url);
232 // mLister.onAction can take it from here without logging
233 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800234 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700235 }
Michael Kolbfe251992010-07-08 15:41:55 -0700236 }
237
John Reckb77bdc42011-01-24 13:24:48 -0800238 boolean isSearch(String inUrl) {
239 String url = UrlUtils.fixUrl(inUrl).trim();
240 if (TextUtils.isEmpty(url)) return false;
241
242 if (Patterns.WEB_URL.matcher(url).matches()
243 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
244 return false;
245 }
246 return true;
247 }
248
Michael Kolb21ce4d22010-09-15 14:55:05 -0700249 // Completion Listener
250
251 @Override
252 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800253 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700254 }
255
256 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800257 public void onSelect(String url, int type, String extra) {
258 finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH)
259 ? VOICE : SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700260 }
261
Michael Kolbfe251992010-07-08 15:41:55 -0700262 @Override
John Reck87369ea2011-01-23 15:20:38 -0800263 public void onItemClick(
264 AdapterView<?> parent, View view, int position, long id) {
265 SuggestItem item = mAdapter.getItem(position);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000266 onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra);
John Reck87369ea2011-01-23 15:20:38 -0800267 }
268
Michael Kolbfe251992010-07-08 15:41:55 -0700269 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700270
Michael Kolbfe251992010-07-08 15:41:55 -0700271 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700272
Michael Kolb257cc2c2010-12-09 09:45:52 -0800273 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700274
Michael Kolb7cdc4902011-02-03 17:54:40 -0800275 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700276
277 }
278
John Reck117f07d2011-01-24 09:39:03 -0800279 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800280 mIncognitoMode = incognito;
281 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800282 }
283
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800284 @Override
285 public boolean onKeyDown(int keyCode, KeyEvent evt) {
286 if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) {
287 finishInput(null, null, null);
288 return true;
289 }
290 return super.onKeyDown(keyCode, evt);
291 }
292
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000293 public SuggestionsAdapter getAdapter() {
294 return mAdapter;
295 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000296
297 private void dispatchChange() {
298 final Rect popupRect = new Rect();
299 getPopupDrawableRect(popupRect);
300
301 if (mDropdownListener != null) {
302 mDropdownListener.onNewDropdownDimensions(popupRect.height());
303 }
304 }
305
306 void registerDropdownChangeListener(DropdownChangeListener d) {
307 mDropdownListener = d;
308 }
Michael Kolbfe251992010-07-08 15:41:55 -0700309}