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 | 80aad8d | 2011-02-23 12:01:13 +0000 | [diff] [blame] | 21 | import com.android.browser.autocomplete.SuggestiveAutoCompleteTextView; |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 22 | import com.android.browser.search.SearchEngine; |
| 23 | import com.android.browser.search.SearchEngineInfo; |
| 24 | import com.android.browser.search.SearchEngines; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 25 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 27 | import android.content.res.Configuration; |
John Reck | dcda1d5 | 2010-11-29 10:05:54 -0800 | [diff] [blame] | 28 | import android.text.TextUtils; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 30 | import android.util.Patterns; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 31 | import android.view.KeyEvent; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 32 | import android.view.View; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 33 | import android.view.inputmethod.InputMethodManager; |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 34 | import android.widget.AdapterView; |
| 35 | import android.widget.AdapterView.OnItemClickListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 36 | import android.widget.TextView; |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 37 | import android.widget.TextView.OnEditorActionListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 38 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 39 | import java.util.List; |
| 40 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 41 | /** |
| 42 | * url/search input view |
| 43 | * handling suggestions |
| 44 | */ |
Narayan Kamath | 80aad8d | 2011-02-23 12:01:13 +0000 | [diff] [blame] | 45 | public class UrlInputView extends SuggestiveAutoCompleteTextView |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 46 | implements OnEditorActionListener, |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 47 | CompletionListener, OnItemClickListener { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 48 | |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 49 | |
| 50 | static final String TYPED = "browser-type"; |
| 51 | static final String SUGGESTED = "browser-suggest"; |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 52 | static final String VOICE = "voice-search"; |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 53 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 54 | private UrlInputListener mListener; |
| 55 | private InputMethodManager mInputManager; |
| 56 | private SuggestionsAdapter mAdapter; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 57 | private View mContainer; |
| 58 | private boolean mLandscape; |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 59 | private boolean mIncognitoMode; |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 60 | private boolean mNeedsUpdate; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 61 | |
| 62 | public UrlInputView(Context context, AttributeSet attrs, int defStyle) { |
| 63 | super(context, attrs, defStyle); |
| 64 | init(context); |
| 65 | } |
| 66 | |
| 67 | public UrlInputView(Context context, AttributeSet attrs) { |
| 68 | super(context, attrs); |
| 69 | init(context); |
| 70 | } |
| 71 | |
| 72 | public UrlInputView(Context context) { |
| 73 | super(context); |
| 74 | init(context); |
| 75 | } |
| 76 | |
| 77 | private void init(Context ctx) { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 78 | mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 79 | setOnEditorActionListener(this); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 80 | mAdapter = new SuggestionsAdapter(ctx, this); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 81 | setAdapter(mAdapter); |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 82 | setSelectAllOnFocus(true); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 83 | onConfigurationChanged(ctx.getResources().getConfiguration()); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 84 | setThreshold(1); |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 85 | setOnItemClickListener(this); |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 86 | mNeedsUpdate = false; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * check if focus change requires a title bar update |
| 91 | */ |
| 92 | boolean needsUpdate() { |
| 93 | return mNeedsUpdate; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * clear the focus change needs title bar update flag |
| 98 | */ |
| 99 | void clearNeedsUpdate() { |
| 100 | mNeedsUpdate = false; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 103 | void setController(UiController controller) { |
| 104 | UrlSelectionActionMode urlSelectionMode |
| 105 | = new UrlSelectionActionMode(controller); |
| 106 | setCustomSelectionActionModeCallback(urlSelectionMode); |
| 107 | } |
| 108 | |
Michael Kolb | 91902d5 | 2011-01-26 17:43:48 -0800 | [diff] [blame] | 109 | void setUseQuickControls(boolean useQuickControls) { |
Michael Kolb | 91902d5 | 2011-01-26 17:43:48 -0800 | [diff] [blame] | 110 | mAdapter.setReverseResults(useQuickControls); |
| 111 | } |
| 112 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 113 | void setContainer(View container) { |
| 114 | mContainer = container; |
| 115 | } |
| 116 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 117 | public void setUrlInputListener(UrlInputListener listener) { |
| 118 | mListener = listener; |
| 119 | } |
| 120 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 121 | void setVoiceResults(List<String> voiceResults) { |
| 122 | mAdapter.setVoiceResults(voiceResults); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 125 | @Override |
| 126 | protected void onConfigurationChanged(Configuration config) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 127 | super.onConfigurationChanged(config); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 128 | mLandscape = (config.orientation & |
Michael Kolb | 31d469b | 2010-12-09 20:49:54 -0800 | [diff] [blame] | 129 | Configuration.ORIENTATION_LANDSCAPE) != 0; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 130 | mAdapter.setLandscapeMode(mLandscape); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 131 | if (isPopupShowing() && (getVisibility() == View.VISIBLE)) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 132 | setupDropDown(); |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 133 | performFiltering(getText(), 0); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void showDropDown() { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 139 | setupDropDown(); |
| 140 | super.showDropDown(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void dismissDropDown() { |
| 145 | super.dismissDropDown(); |
| 146 | mAdapter.clearCache(); |
| 147 | } |
| 148 | |
| 149 | private void setupDropDown() { |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 150 | int width = mContainer != null ? mContainer.getWidth() : getWidth(); |
Michael Kolb | c5998c2 | 2010-10-10 16:04:35 -0700 | [diff] [blame] | 151 | if (width != getDropDownWidth()) { |
| 152 | setDropDownWidth(width); |
| 153 | } |
| 154 | if (getLeft() != -getDropDownHorizontalOffset()) { |
| 155 | setDropDownHorizontalOffset(-getLeft()); |
| 156 | } |
Michael Kolb | 513286f | 2010-09-09 12:55:12 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 160 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 161 | finishInput(getText().toString(), null, TYPED); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 162 | return true; |
| 163 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 164 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 165 | void forceFilter() { |
| 166 | performFiltering(getText().toString(), 0); |
| 167 | showDropDown(); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 170 | void forceIme() { |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 171 | mInputManager.focusIn(this); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 172 | mInputManager.showSoftInput(this, 0); |
| 173 | } |
| 174 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 175 | void hideIME() { |
| 176 | mInputManager.hideSoftInputFromWindow(getWindowToken(), 0); |
| 177 | } |
| 178 | |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 179 | private void finishInput(String url, String extra, String source) { |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 180 | mNeedsUpdate = true; |
| 181 | dismissDropDown(); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 182 | mInputManager.hideSoftInputFromWindow(getWindowToken(), 0); |
John Reck | dcda1d5 | 2010-11-29 10:05:54 -0800 | [diff] [blame] | 183 | if (TextUtils.isEmpty(url)) { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 184 | mListener.onDismiss(); |
| 185 | } else { |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 186 | if (mIncognitoMode && isSearch(url)) { |
| 187 | // To prevent logging, intercept this request |
| 188 | // TODO: This is a quick hack, refactor this |
| 189 | SearchEngine searchEngine = BrowserSettings.getInstance() |
| 190 | .getSearchEngine(); |
| 191 | if (searchEngine == null) return; |
| 192 | SearchEngineInfo engineInfo = SearchEngines |
| 193 | .getSearchEngineInfo(mContext, searchEngine.getName()); |
| 194 | if (engineInfo == null) return; |
| 195 | url = engineInfo.getSearchUriForQuery(url); |
| 196 | // mLister.onAction can take it from here without logging |
| 197 | } |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 198 | mListener.onAction(url, extra, source); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 199 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 200 | } |
| 201 | |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 202 | boolean isSearch(String inUrl) { |
| 203 | String url = UrlUtils.fixUrl(inUrl).trim(); |
| 204 | if (TextUtils.isEmpty(url)) return false; |
| 205 | |
| 206 | if (Patterns.WEB_URL.matcher(url).matches() |
| 207 | || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) { |
| 208 | return false; |
| 209 | } |
| 210 | return true; |
| 211 | } |
| 212 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 213 | // Completion Listener |
| 214 | |
| 215 | @Override |
| 216 | public void onSearch(String search) { |
Michael Kolb | 7cdc490 | 2011-02-03 17:54:40 -0800 | [diff] [blame] | 217 | mListener.onCopySuggestion(search); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 221 | public void onSelect(String url, int type, String extra) { |
| 222 | finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH) |
| 223 | ? VOICE : SUGGESTED); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 226 | @Override |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 227 | public void onItemClick( |
| 228 | AdapterView<?> parent, View view, int position, long id) { |
| 229 | SuggestItem item = mAdapter.getItem(position); |
| 230 | onSelect((TextUtils.isEmpty(item.url) ? item.title : item.url), |
| 231 | item.type, item.extra); |
| 232 | } |
| 233 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 234 | interface UrlInputListener { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 235 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 236 | public void onDismiss(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 237 | |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 238 | public void onAction(String text, String extra, String source); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 239 | |
Michael Kolb | 7cdc490 | 2011-02-03 17:54:40 -0800 | [diff] [blame] | 240 | public void onCopySuggestion(String text); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 241 | |
| 242 | } |
| 243 | |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 244 | public void setIncognitoMode(boolean incognito) { |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 245 | mIncognitoMode = incognito; |
| 246 | mAdapter.setIncognitoMode(mIncognitoMode); |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 249 | @Override |
| 250 | public boolean onKeyDown(int keyCode, KeyEvent evt) { |
| 251 | if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) { |
| 252 | finishInput(null, null, null); |
| 253 | return true; |
| 254 | } |
| 255 | return super.onKeyDown(keyCode, evt); |
| 256 | } |
| 257 | |
Narayan Kamath | 80aad8d | 2011-02-23 12:01:13 +0000 | [diff] [blame] | 258 | public SuggestionsAdapter getAdapter() { |
| 259 | return mAdapter; |
| 260 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 261 | } |