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