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 | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 20 | import android.content.res.Configuration; |
John Reck | 8d021aa | 2011-09-06 15:30:11 -0700 | [diff] [blame^] | 21 | import android.content.res.TypedArray; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 22 | import android.database.DataSetObserver; |
| 23 | import android.graphics.Rect; |
John Reck | 8d021aa | 2011-09-06 15:30:11 -0700 | [diff] [blame^] | 24 | import android.graphics.drawable.Drawable; |
John Reck | dcda1d5 | 2010-11-29 10:05:54 -0800 | [diff] [blame] | 25 | import android.text.TextUtils; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 26 | import android.util.AttributeSet; |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 27 | import android.util.Patterns; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 28 | import android.view.KeyEvent; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 29 | import android.view.MotionEvent; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 30 | import android.view.View; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 31 | import android.view.inputmethod.EditorInfo; |
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.TextView; |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 36 | import android.widget.TextView.OnEditorActionListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 37 | |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 38 | import com.android.browser.SuggestionsAdapter.CompletionListener; |
| 39 | import com.android.browser.SuggestionsAdapter.SuggestItem; |
| 40 | import com.android.browser.UI.DropdownChangeListener; |
| 41 | import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher; |
| 42 | import com.android.browser.autocomplete.SuggestiveAutoCompleteTextView; |
| 43 | import com.android.browser.search.SearchEngine; |
| 44 | import com.android.browser.search.SearchEngineInfo; |
| 45 | import com.android.browser.search.SearchEngines; |
John Reck | 8d021aa | 2011-09-06 15:30:11 -0700 | [diff] [blame^] | 46 | import com.android.internal.R; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 47 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 48 | import java.util.List; |
| 49 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 50 | /** |
| 51 | * url/search input view |
| 52 | * handling suggestions |
| 53 | */ |
Narayan Kamath | 80aad8d | 2011-02-23 12:01:13 +0000 | [diff] [blame] | 54 | public class UrlInputView extends SuggestiveAutoCompleteTextView |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 55 | implements OnEditorActionListener, |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 56 | CompletionListener, OnItemClickListener, TextChangeWatcher { |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 57 | |
| 58 | static final String TYPED = "browser-type"; |
| 59 | static final String SUGGESTED = "browser-suggest"; |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 60 | static final String VOICE = "voice-search"; |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 61 | |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 62 | static final int POST_DELAY = 100; |
| 63 | |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 64 | static interface StateListener { |
| 65 | static final int STATE_NORMAL = 0; |
| 66 | static final int STATE_HIGHLIGHTED = 1; |
| 67 | static final int STATE_EDITED = 2; |
| 68 | |
| 69 | public void onStateChanged(int state); |
| 70 | } |
| 71 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 72 | private UrlInputListener mListener; |
| 73 | private InputMethodManager mInputManager; |
| 74 | private SuggestionsAdapter mAdapter; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 75 | private View mContainer; |
| 76 | private boolean mLandscape; |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 77 | private boolean mIncognitoMode; |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 78 | private boolean mNeedsUpdate; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 79 | private DropdownChangeListener mDropdownListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 80 | |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 81 | private int mState; |
| 82 | private StateListener mStateListener; |
John Reck | 8d021aa | 2011-09-06 15:30:11 -0700 | [diff] [blame^] | 83 | private Rect mPopupPadding; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 84 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 85 | public UrlInputView(Context context, AttributeSet attrs, int defStyle) { |
| 86 | super(context, attrs, defStyle); |
John Reck | 8d021aa | 2011-09-06 15:30:11 -0700 | [diff] [blame^] | 87 | TypedArray a = context.obtainStyledAttributes( |
| 88 | attrs, com.android.internal.R.styleable.PopupWindow, |
| 89 | R.attr.autoCompleteTextViewStyle, 0); |
| 90 | |
| 91 | Drawable popupbg = a.getDrawable(R.styleable.PopupWindow_popupBackground); |
| 92 | a.recycle(); |
| 93 | mPopupPadding = new Rect(); |
| 94 | popupbg.getPadding(mPopupPadding); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 95 | init(context); |
| 96 | } |
| 97 | |
| 98 | public UrlInputView(Context context, AttributeSet attrs) { |
John Reck | 8d021aa | 2011-09-06 15:30:11 -0700 | [diff] [blame^] | 99 | this(context, attrs, R.attr.autoCompleteTextViewStyle); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | public UrlInputView(Context context) { |
John Reck | 8d021aa | 2011-09-06 15:30:11 -0700 | [diff] [blame^] | 103 | this(context, null); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | private void init(Context ctx) { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 107 | mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 108 | setOnEditorActionListener(this); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 109 | mAdapter = new SuggestionsAdapter(ctx, this); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 110 | setAdapter(mAdapter); |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 111 | setSelectAllOnFocus(true); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 112 | onConfigurationChanged(ctx.getResources().getConfiguration()); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 113 | setThreshold(1); |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 114 | setOnItemClickListener(this); |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 115 | mNeedsUpdate = false; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 116 | mDropdownListener = null; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 117 | addQueryTextWatcher(this); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 118 | |
| 119 | mAdapter.registerDataSetObserver(new DataSetObserver() { |
| 120 | @Override |
| 121 | public void onChanged() { |
| 122 | if (!isPopupShowing()) { |
| 123 | return; |
| 124 | } |
| 125 | dispatchChange(); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void onInvalidated() { |
| 130 | dispatchChange(); |
| 131 | } |
| 132 | }); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 133 | mState = StateListener.STATE_NORMAL; |
| 134 | } |
| 135 | |
| 136 | protected void onFocusChanged(boolean focused, int direction, Rect prevRect) { |
| 137 | super.onFocusChanged(focused, direction, prevRect); |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 138 | int state = -1; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 139 | if (focused) { |
| 140 | if (hasSelection()) { |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 141 | state = StateListener.STATE_HIGHLIGHTED; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 142 | } else { |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 143 | state = StateListener.STATE_EDITED; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 144 | } |
| 145 | } else { |
| 146 | // reset the selection state |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 147 | state = StateListener.STATE_NORMAL; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 148 | } |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 149 | final int s = state; |
| 150 | post(new Runnable() { |
| 151 | public void run() { |
| 152 | changeState(s); |
| 153 | } |
| 154 | }); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public boolean onTouchEvent(MotionEvent evt) { |
| 159 | boolean hasSelection = hasSelection(); |
| 160 | boolean res = super.onTouchEvent(evt); |
| 161 | if ((MotionEvent.ACTION_DOWN == evt.getActionMasked()) |
| 162 | && hasSelection) { |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 163 | postDelayed(new Runnable() { |
| 164 | public void run() { |
| 165 | changeState(StateListener.STATE_EDITED); |
| 166 | }}, POST_DELAY); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 167 | } |
| 168 | return res; |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * check if focus change requires a title bar update |
| 173 | */ |
| 174 | boolean needsUpdate() { |
| 175 | return mNeedsUpdate; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * clear the focus change needs title bar update flag |
| 180 | */ |
| 181 | void clearNeedsUpdate() { |
| 182 | mNeedsUpdate = false; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 185 | void setController(UiController controller) { |
| 186 | UrlSelectionActionMode urlSelectionMode |
| 187 | = new UrlSelectionActionMode(controller); |
| 188 | setCustomSelectionActionModeCallback(urlSelectionMode); |
| 189 | } |
| 190 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 191 | void setContainer(View container) { |
| 192 | mContainer = container; |
| 193 | } |
| 194 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 195 | public void setUrlInputListener(UrlInputListener listener) { |
| 196 | mListener = listener; |
| 197 | } |
| 198 | |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 199 | public void setStateListener(StateListener listener) { |
| 200 | mStateListener = listener; |
| 201 | // update listener |
| 202 | changeState(mState); |
| 203 | } |
| 204 | |
| 205 | private void changeState(int newState) { |
| 206 | mState = newState; |
| 207 | if (mStateListener != null) { |
| 208 | mStateListener.onStateChanged(mState); |
| 209 | } |
| 210 | } |
| 211 | |
Michael Kolb | 5e8f2b9 | 2011-07-19 13:22:32 -0700 | [diff] [blame] | 212 | int getState() { |
| 213 | return mState; |
| 214 | } |
| 215 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 216 | void setVoiceResults(List<String> voiceResults) { |
| 217 | mAdapter.setVoiceResults(voiceResults); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 220 | @Override |
| 221 | protected void onConfigurationChanged(Configuration config) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 222 | super.onConfigurationChanged(config); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 223 | mLandscape = (config.orientation & |
Michael Kolb | 31d469b | 2010-12-09 20:49:54 -0800 | [diff] [blame] | 224 | Configuration.ORIENTATION_LANDSCAPE) != 0; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 225 | mAdapter.setLandscapeMode(mLandscape); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 226 | if (isPopupShowing() && (getVisibility() == View.VISIBLE)) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 227 | setupDropDown(); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 228 | performFiltering(getUserText(), 0); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public void showDropDown() { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 234 | setupDropDown(); |
| 235 | super.showDropDown(); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void dismissDropDown() { |
| 240 | super.dismissDropDown(); |
| 241 | mAdapter.clearCache(); |
| 242 | } |
| 243 | |
| 244 | private void setupDropDown() { |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 245 | int width = mContainer != null ? mContainer.getWidth() : getWidth(); |
John Reck | 8d021aa | 2011-09-06 15:30:11 -0700 | [diff] [blame^] | 246 | width += mPopupPadding.left + mPopupPadding.right; |
Michael Kolb | c5998c2 | 2010-10-10 16:04:35 -0700 | [diff] [blame] | 247 | if (width != getDropDownWidth()) { |
| 248 | setDropDownWidth(width); |
| 249 | } |
John Reck | 8d021aa | 2011-09-06 15:30:11 -0700 | [diff] [blame^] | 250 | int left = getLeft(); |
| 251 | left += mPopupPadding.left; |
| 252 | if (left != -getDropDownHorizontalOffset()) { |
| 253 | setDropDownHorizontalOffset(-left); |
Michael Kolb | c5998c2 | 2010-10-10 16:04:35 -0700 | [diff] [blame] | 254 | } |
Michael Kolb | 513286f | 2010-09-09 12:55:12 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 258 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 259 | if (BrowserSettings.getInstance().useInstantSearch() && |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 260 | (actionId == EditorInfo.IME_ACTION_NEXT)) { |
| 261 | // When instant is turned on AND the user chooses to complete |
| 262 | // using the tab key, then use the completion rather than the |
| 263 | // text that the user has typed. |
| 264 | finishInput(getText().toString(), null, TYPED); |
| 265 | } else { |
| 266 | finishInput(getUserText(), null, TYPED); |
| 267 | } |
| 268 | |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 269 | return true; |
| 270 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 271 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 272 | void forceFilter() { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 273 | performForcedFiltering(); |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 274 | showDropDown(); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 277 | void forceIme() { |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 278 | mInputManager.focusIn(this); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 279 | mInputManager.showSoftInput(this, 0); |
| 280 | } |
| 281 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 282 | void hideIME() { |
| 283 | mInputManager.hideSoftInputFromWindow(getWindowToken(), 0); |
| 284 | } |
| 285 | |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 286 | private void finishInput(String url, String extra, String source) { |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 287 | mNeedsUpdate = true; |
| 288 | dismissDropDown(); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 289 | mInputManager.hideSoftInputFromWindow(getWindowToken(), 0); |
John Reck | dcda1d5 | 2010-11-29 10:05:54 -0800 | [diff] [blame] | 290 | if (TextUtils.isEmpty(url)) { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 291 | mListener.onDismiss(); |
| 292 | } else { |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 293 | if (mIncognitoMode && isSearch(url)) { |
| 294 | // To prevent logging, intercept this request |
| 295 | // TODO: This is a quick hack, refactor this |
| 296 | SearchEngine searchEngine = BrowserSettings.getInstance() |
| 297 | .getSearchEngine(); |
| 298 | if (searchEngine == null) return; |
| 299 | SearchEngineInfo engineInfo = SearchEngines |
| 300 | .getSearchEngineInfo(mContext, searchEngine.getName()); |
| 301 | if (engineInfo == null) return; |
| 302 | url = engineInfo.getSearchUriForQuery(url); |
| 303 | // mLister.onAction can take it from here without logging |
| 304 | } |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 305 | mListener.onAction(url, extra, source); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 306 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 307 | } |
| 308 | |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 309 | boolean isSearch(String inUrl) { |
| 310 | String url = UrlUtils.fixUrl(inUrl).trim(); |
| 311 | if (TextUtils.isEmpty(url)) return false; |
| 312 | |
| 313 | if (Patterns.WEB_URL.matcher(url).matches() |
| 314 | || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) { |
| 315 | return false; |
| 316 | } |
| 317 | return true; |
| 318 | } |
| 319 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 320 | // Completion Listener |
| 321 | |
| 322 | @Override |
| 323 | public void onSearch(String search) { |
Michael Kolb | 7cdc490 | 2011-02-03 17:54:40 -0800 | [diff] [blame] | 324 | mListener.onCopySuggestion(search); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 328 | public void onSelect(String url, int type, String extra) { |
| 329 | finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH) |
| 330 | ? VOICE : SUGGESTED); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 333 | @Override |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 334 | public void onItemClick( |
| 335 | AdapterView<?> parent, View view, int position, long id) { |
| 336 | SuggestItem item = mAdapter.getItem(position); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 337 | onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra); |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 340 | interface UrlInputListener { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 341 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 342 | public void onDismiss(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 343 | |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 344 | public void onAction(String text, String extra, String source); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 345 | |
Michael Kolb | 7cdc490 | 2011-02-03 17:54:40 -0800 | [diff] [blame] | 346 | public void onCopySuggestion(String text); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 347 | |
| 348 | } |
| 349 | |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 350 | public void setIncognitoMode(boolean incognito) { |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 351 | mIncognitoMode = incognito; |
| 352 | mAdapter.setIncognitoMode(mIncognitoMode); |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 355 | @Override |
| 356 | public boolean onKeyDown(int keyCode, KeyEvent evt) { |
| 357 | if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) { |
| 358 | finishInput(null, null, null); |
| 359 | return true; |
| 360 | } |
| 361 | return super.onKeyDown(keyCode, evt); |
| 362 | } |
| 363 | |
Narayan Kamath | 80aad8d | 2011-02-23 12:01:13 +0000 | [diff] [blame] | 364 | public SuggestionsAdapter getAdapter() { |
| 365 | return mAdapter; |
| 366 | } |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 367 | |
| 368 | private void dispatchChange() { |
| 369 | final Rect popupRect = new Rect(); |
| 370 | getPopupDrawableRect(popupRect); |
| 371 | |
| 372 | if (mDropdownListener != null) { |
| 373 | mDropdownListener.onNewDropdownDimensions(popupRect.height()); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | void registerDropdownChangeListener(DropdownChangeListener d) { |
| 378 | mDropdownListener = d; |
| 379 | } |
Michael Kolb | 74e60c2 | 2011-04-30 16:43:47 -0700 | [diff] [blame] | 380 | |
| 381 | /* |
| 382 | * no-op to prevent scrolling of webview when embedded titlebar |
| 383 | * gets edited |
| 384 | */ |
| 385 | @Override |
| 386 | public boolean requestRectangleOnScreen(Rect rect, boolean immediate) { |
| 387 | return false; |
| 388 | } |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 389 | |
| 390 | @Override |
| 391 | public void onTextChanged(String newText) { |
| 392 | if (StateListener.STATE_HIGHLIGHTED == mState) { |
| 393 | changeState(StateListener.STATE_EDITED); |
| 394 | } |
| 395 | } |
| 396 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 397 | } |