Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 19 | import com.android.browser.SuggestionsAdapter.CompletionListener; |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 20 | import com.android.browser.SuggestionsAdapter.SuggestItem; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 21 | import com.android.browser.UI.DropdownChangeListener; |
Narayan Kamath | 80aad8d | 2011-02-23 12:01:13 +0000 | [diff] [blame] | 22 | import com.android.browser.autocomplete.SuggestiveAutoCompleteTextView; |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 23 | import com.android.browser.search.SearchEngine; |
| 24 | import com.android.browser.search.SearchEngineInfo; |
| 25 | import com.android.browser.search.SearchEngines; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 26 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 27 | import android.content.Context; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 28 | import android.content.res.Configuration; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 29 | import android.database.DataSetObserver; |
| 30 | import android.graphics.Rect; |
John Reck | dcda1d5 | 2010-11-29 10:05:54 -0800 | [diff] [blame] | 31 | import android.text.TextUtils; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 32 | import android.util.AttributeSet; |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 33 | import android.util.Patterns; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 34 | import android.view.KeyEvent; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 35 | import android.view.View; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 36 | import android.view.inputmethod.EditorInfo; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 37 | import android.view.inputmethod.InputMethodManager; |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 38 | import android.widget.AdapterView; |
| 39 | import android.widget.AdapterView.OnItemClickListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 40 | import android.widget.TextView; |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 41 | import android.widget.TextView.OnEditorActionListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 42 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 43 | import java.util.List; |
| 44 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 45 | /** |
| 46 | * url/search input view |
| 47 | * handling suggestions |
| 48 | */ |
Narayan Kamath | 80aad8d | 2011-02-23 12:01:13 +0000 | [diff] [blame] | 49 | public class UrlInputView extends SuggestiveAutoCompleteTextView |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 50 | implements OnEditorActionListener, |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 51 | CompletionListener, OnItemClickListener { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 52 | |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 53 | |
| 54 | static final String TYPED = "browser-type"; |
| 55 | static final String SUGGESTED = "browser-suggest"; |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 56 | static final String VOICE = "voice-search"; |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 57 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 58 | private UrlInputListener mListener; |
| 59 | private InputMethodManager mInputManager; |
| 60 | private SuggestionsAdapter mAdapter; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 61 | private View mContainer; |
| 62 | private boolean mLandscape; |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 63 | private boolean mIncognitoMode; |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 64 | private boolean mNeedsUpdate; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 65 | private DropdownChangeListener mDropdownListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 66 | |
| 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 Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 83 | mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 84 | setOnEditorActionListener(this); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 85 | mAdapter = new SuggestionsAdapter(ctx, this); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 86 | setAdapter(mAdapter); |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 87 | setSelectAllOnFocus(true); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 88 | onConfigurationChanged(ctx.getResources().getConfiguration()); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 89 | setThreshold(1); |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 90 | setOnItemClickListener(this); |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 91 | mNeedsUpdate = false; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 92 | 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 Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 108 | } |
| 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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 124 | void setController(UiController controller) { |
| 125 | UrlSelectionActionMode urlSelectionMode |
| 126 | = new UrlSelectionActionMode(controller); |
| 127 | setCustomSelectionActionModeCallback(urlSelectionMode); |
| 128 | } |
| 129 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 130 | void setContainer(View container) { |
| 131 | mContainer = container; |
| 132 | } |
| 133 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 134 | public void setUrlInputListener(UrlInputListener listener) { |
| 135 | mListener = listener; |
| 136 | } |
| 137 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 138 | void setVoiceResults(List<String> voiceResults) { |
| 139 | mAdapter.setVoiceResults(voiceResults); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 142 | @Override |
| 143 | protected void onConfigurationChanged(Configuration config) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 144 | super.onConfigurationChanged(config); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 145 | mLandscape = (config.orientation & |
Michael Kolb | 31d469b | 2010-12-09 20:49:54 -0800 | [diff] [blame] | 146 | Configuration.ORIENTATION_LANDSCAPE) != 0; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 147 | mAdapter.setLandscapeMode(mLandscape); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 148 | if (isPopupShowing() && (getVisibility() == View.VISIBLE)) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 149 | setupDropDown(); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 150 | performFiltering(getUserText(), 0); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void showDropDown() { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 156 | 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 Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 167 | int width = mContainer != null ? mContainer.getWidth() : getWidth(); |
Michael Kolb | c5998c2 | 2010-10-10 16:04:35 -0700 | [diff] [blame] | 168 | if (width != getDropDownWidth()) { |
| 169 | setDropDownWidth(width); |
| 170 | } |
| 171 | if (getLeft() != -getDropDownHorizontalOffset()) { |
| 172 | setDropDownHorizontalOffset(-getLeft()); |
| 173 | } |
Michael Kolb | 513286f | 2010-09-09 12:55:12 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 177 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 178 | if (BrowserSettings.getInstance().useInstantSearch() && |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 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 Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 188 | return true; |
| 189 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 190 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 191 | void forceFilter() { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 192 | performForcedFiltering(); |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 193 | showDropDown(); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 196 | void forceIme() { |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 197 | mInputManager.focusIn(this); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 198 | mInputManager.showSoftInput(this, 0); |
| 199 | } |
| 200 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 201 | void hideIME() { |
| 202 | mInputManager.hideSoftInputFromWindow(getWindowToken(), 0); |
| 203 | } |
| 204 | |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 205 | private void finishInput(String url, String extra, String source) { |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 206 | mNeedsUpdate = true; |
| 207 | dismissDropDown(); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 208 | mInputManager.hideSoftInputFromWindow(getWindowToken(), 0); |
John Reck | dcda1d5 | 2010-11-29 10:05:54 -0800 | [diff] [blame] | 209 | if (TextUtils.isEmpty(url)) { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 210 | mListener.onDismiss(); |
| 211 | } else { |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 212 | 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 Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 224 | mListener.onAction(url, extra, source); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 225 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 226 | } |
| 227 | |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 228 | 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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 239 | // Completion Listener |
| 240 | |
| 241 | @Override |
| 242 | public void onSearch(String search) { |
Michael Kolb | 7cdc490 | 2011-02-03 17:54:40 -0800 | [diff] [blame] | 243 | mListener.onCopySuggestion(search); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | @Override |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 247 | public void onSelect(String url, int type, String extra) { |
| 248 | finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH) |
| 249 | ? VOICE : SUGGESTED); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 252 | @Override |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 253 | public void onItemClick( |
| 254 | AdapterView<?> parent, View view, int position, long id) { |
| 255 | SuggestItem item = mAdapter.getItem(position); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 256 | onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra); |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 259 | interface UrlInputListener { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 260 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 261 | public void onDismiss(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 262 | |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 263 | public void onAction(String text, String extra, String source); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 264 | |
Michael Kolb | 7cdc490 | 2011-02-03 17:54:40 -0800 | [diff] [blame] | 265 | public void onCopySuggestion(String text); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 266 | |
| 267 | } |
| 268 | |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 269 | public void setIncognitoMode(boolean incognito) { |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 270 | mIncognitoMode = incognito; |
| 271 | mAdapter.setIncognitoMode(mIncognitoMode); |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 274 | @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 Kamath | 80aad8d | 2011-02-23 12:01:13 +0000 | [diff] [blame] | 283 | public SuggestionsAdapter getAdapter() { |
| 284 | return mAdapter; |
| 285 | } |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 286 | |
| 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 Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 299 | } |