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 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 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; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 21 | import android.graphics.Rect; |
John Reck | 8d021aa | 2011-09-06 15:30:11 -0700 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 23 | import android.text.Editable; |
Axesh R. Ajmera | 9bb763e | 2014-04-04 15:37:34 -0700 | [diff] [blame] | 24 | import android.text.InputFilter; |
| 25 | import android.text.InputFilter.LengthFilter; |
| 26 | import android.text.Spanned; |
John Reck | dcda1d5 | 2010-11-29 10:05:54 -0800 | [diff] [blame] | 27 | import android.text.TextUtils; |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 28 | import android.text.TextWatcher; |
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; |
Axesh R. Ajmera | 9bb763e | 2014-04-04 15:37:34 -0700 | [diff] [blame] | 31 | import android.view.Gravity; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 32 | import android.view.KeyEvent; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 33 | import android.view.MotionEvent; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 34 | import android.view.View; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 35 | import android.view.inputmethod.InputMethodManager; |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 36 | import android.widget.AdapterView; |
| 37 | import android.widget.AdapterView.OnItemClickListener; |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 38 | import android.widget.AutoCompleteTextView; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 39 | import android.widget.TextView; |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 40 | import android.widget.TextView.OnEditorActionListener; |
Axesh R. Ajmera | 9bb763e | 2014-04-04 15:37:34 -0700 | [diff] [blame] | 41 | import android.widget.Toast; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 42 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 43 | import com.android.browser.SuggestionsAdapter.CompletionListener; |
| 44 | import com.android.browser.SuggestionsAdapter.SuggestItem; |
| 45 | import com.android.browser.reflect.ReflectHelper; |
| 46 | import com.android.browser.search.SearchEngine; |
| 47 | import com.android.browser.search.SearchEngineInfo; |
| 48 | import com.android.browser.search.SearchEngines; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 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 | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 54 | public class UrlInputView extends AutoCompleteTextView |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 55 | implements OnEditorActionListener, |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 56 | CompletionListener, OnItemClickListener, TextWatcher { |
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"; |
Tarun Nainani | d530673 | 2014-04-28 19:38:28 -0700 | [diff] [blame] | 60 | static final String LATIN_INPUTMETHOD_PACKAGE_NAME = "com.android.inputmethod.latin"; |
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; |
Axesh R. Ajmera | 9bb763e | 2014-04-04 15:37:34 -0700 | [diff] [blame] | 63 | static final int URL_MAX_LENGTH = 2048; |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 64 | |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 65 | static interface StateListener { |
| 66 | static final int STATE_NORMAL = 0; |
| 67 | static final int STATE_HIGHLIGHTED = 1; |
| 68 | static final int STATE_EDITED = 2; |
| 69 | |
| 70 | public void onStateChanged(int state); |
| 71 | } |
| 72 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 73 | private UrlInputListener mListener; |
| 74 | private InputMethodManager mInputManager; |
| 75 | private SuggestionsAdapter mAdapter; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 76 | private View mContainer; |
| 77 | private boolean mLandscape; |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 78 | private boolean mIncognitoMode; |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 79 | private boolean mNeedsUpdate; |
Axesh R. Ajmera | 9bb763e | 2014-04-04 15:37:34 -0700 | [diff] [blame] | 80 | private Context mContext; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 81 | |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 82 | private int mState; |
| 83 | private StateListener mStateListener; |
| 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); |
| 87 | init(context); |
| 88 | } |
| 89 | |
| 90 | public UrlInputView(Context context, AttributeSet attrs) { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 91 | // SWE_TODO : Needs Fix |
| 92 | //this(context, attrs, R.attr.autoCompleteTextViewStyle); |
| 93 | this(context, attrs, 0); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | public UrlInputView(Context context) { |
John Reck | 8d021aa | 2011-09-06 15:30:11 -0700 | [diff] [blame] | 97 | this(context, null); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | private void init(Context ctx) { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 101 | mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 102 | setOnEditorActionListener(this); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 103 | mAdapter = new SuggestionsAdapter(ctx, this); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 104 | setAdapter(mAdapter); |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 105 | setSelectAllOnFocus(true); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 106 | onConfigurationChanged(ctx.getResources().getConfiguration()); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 107 | setThreshold(1); |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 108 | setOnItemClickListener(this); |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 109 | mNeedsUpdate = false; |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 110 | addTextChangedListener(this); |
Sungmann Cho | 7a9471b | 2014-02-27 20:45:20 +0900 | [diff] [blame^] | 111 | setDropDownAnchor(R.id.taburlbar); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 112 | mState = StateListener.STATE_NORMAL; |
Axesh R. Ajmera | 9bb763e | 2014-04-04 15:37:34 -0700 | [diff] [blame] | 113 | mContext = ctx; |
| 114 | |
| 115 | this.setFilters(new InputFilter[] { |
| 116 | new UrlLengthFilter(URL_MAX_LENGTH) |
| 117 | }); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | protected void onFocusChanged(boolean focused, int direction, Rect prevRect) { |
| 121 | super.onFocusChanged(focused, direction, prevRect); |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 122 | int state = -1; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 123 | if (focused) { |
| 124 | if (hasSelection()) { |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 125 | state = StateListener.STATE_HIGHLIGHTED; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 126 | } else { |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 127 | state = StateListener.STATE_EDITED; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 128 | } |
| 129 | } else { |
| 130 | // reset the selection state |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 131 | state = StateListener.STATE_NORMAL; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 132 | } |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 133 | final int s = state; |
Vivek Sekhar | 8ee3abb | 2014-07-14 12:32:05 -0700 | [diff] [blame] | 134 | post(new Runnable() { |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 135 | public void run() { |
| 136 | changeState(s); |
| 137 | } |
Vivek Sekhar | 8ee3abb | 2014-07-14 12:32:05 -0700 | [diff] [blame] | 138 | }); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public boolean onTouchEvent(MotionEvent evt) { |
| 143 | boolean hasSelection = hasSelection(); |
| 144 | boolean res = super.onTouchEvent(evt); |
| 145 | if ((MotionEvent.ACTION_DOWN == evt.getActionMasked()) |
| 146 | && hasSelection) { |
Michael Kolb | 94ec527 | 2011-08-31 16:23:57 -0700 | [diff] [blame] | 147 | postDelayed(new Runnable() { |
| 148 | public void run() { |
| 149 | changeState(StateListener.STATE_EDITED); |
| 150 | }}, POST_DELAY); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 151 | } |
| 152 | return res; |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
| 156 | * check if focus change requires a title bar update |
| 157 | */ |
| 158 | boolean needsUpdate() { |
| 159 | return mNeedsUpdate; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * clear the focus change needs title bar update flag |
| 164 | */ |
| 165 | void clearNeedsUpdate() { |
| 166 | mNeedsUpdate = false; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 169 | void setController(UiController controller) { |
| 170 | UrlSelectionActionMode urlSelectionMode |
| 171 | = new UrlSelectionActionMode(controller); |
| 172 | setCustomSelectionActionModeCallback(urlSelectionMode); |
| 173 | } |
| 174 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 175 | void setContainer(View container) { |
| 176 | mContainer = container; |
| 177 | } |
| 178 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 179 | public void setUrlInputListener(UrlInputListener listener) { |
| 180 | mListener = listener; |
| 181 | } |
| 182 | |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 183 | public void setStateListener(StateListener listener) { |
| 184 | mStateListener = listener; |
| 185 | // update listener |
| 186 | changeState(mState); |
| 187 | } |
| 188 | |
| 189 | private void changeState(int newState) { |
| 190 | mState = newState; |
| 191 | if (mStateListener != null) { |
| 192 | mStateListener.onStateChanged(mState); |
| 193 | } |
| 194 | } |
| 195 | |
Michael Kolb | 5e8f2b9 | 2011-07-19 13:22:32 -0700 | [diff] [blame] | 196 | int getState() { |
| 197 | return mState; |
| 198 | } |
| 199 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 200 | @Override |
| 201 | protected void onConfigurationChanged(Configuration config) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 202 | super.onConfigurationChanged(config); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 203 | mLandscape = (config.orientation & |
Michael Kolb | 31d469b | 2010-12-09 20:49:54 -0800 | [diff] [blame] | 204 | Configuration.ORIENTATION_LANDSCAPE) != 0; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 205 | mAdapter.setLandscapeMode(mLandscape); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 206 | if (isPopupShowing() && (getVisibility() == View.VISIBLE)) { |
Sungmann Cho | 7a9471b | 2014-02-27 20:45:20 +0900 | [diff] [blame^] | 207 | dismissDropDown(); |
| 208 | showDropDown(); |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 209 | performFiltering(getText(), 0); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | @Override |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 214 | public void dismissDropDown() { |
| 215 | super.dismissDropDown(); |
| 216 | mAdapter.clearCache(); |
| 217 | } |
| 218 | |
Michael Kolb | 513286f | 2010-09-09 12:55:12 -0700 | [diff] [blame] | 219 | @Override |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 220 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 221 | finishInput(getText().toString(), null, TYPED); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 222 | return true; |
| 223 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 224 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 225 | void forceFilter() { |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 226 | showDropDown(); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 229 | void hideIME() { |
| 230 | mInputManager.hideSoftInputFromWindow(getWindowToken(), 0); |
| 231 | } |
| 232 | |
Michael Kolb | 4bb6fcb | 2012-04-13 14:25:27 -0700 | [diff] [blame] | 233 | void showIME() { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 234 | //mInputManager.focusIn(this); |
| 235 | Object[] params = {this}; |
| 236 | Class[] type = new Class[] {View.class}; |
| 237 | ReflectHelper.invokeMethod(mInputManager, "focusIn", type, params); |
Michael Kolb | 4bb6fcb | 2012-04-13 14:25:27 -0700 | [diff] [blame] | 238 | mInputManager.showSoftInput(this, 0); |
| 239 | } |
| 240 | |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 241 | private void finishInput(String url, String extra, String source) { |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 242 | mNeedsUpdate = true; |
| 243 | dismissDropDown(); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 244 | mInputManager.hideSoftInputFromWindow(getWindowToken(), 0); |
John Reck | dcda1d5 | 2010-11-29 10:05:54 -0800 | [diff] [blame] | 245 | if (TextUtils.isEmpty(url)) { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 246 | mListener.onDismiss(); |
| 247 | } else { |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 248 | if (mIncognitoMode && isSearch(url)) { |
| 249 | // To prevent logging, intercept this request |
| 250 | // TODO: This is a quick hack, refactor this |
| 251 | SearchEngine searchEngine = BrowserSettings.getInstance() |
| 252 | .getSearchEngine(); |
| 253 | if (searchEngine == null) return; |
| 254 | SearchEngineInfo engineInfo = SearchEngines |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 255 | .getSearchEngineInfo(getContext(), searchEngine.getName()); |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 256 | if (engineInfo == null) return; |
| 257 | url = engineInfo.getSearchUriForQuery(url); |
| 258 | // mLister.onAction can take it from here without logging |
| 259 | } |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 260 | mListener.onAction(url, extra, source); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 261 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 262 | } |
| 263 | |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 264 | boolean isSearch(String inUrl) { |
| 265 | String url = UrlUtils.fixUrl(inUrl).trim(); |
| 266 | if (TextUtils.isEmpty(url)) return false; |
| 267 | |
| 268 | if (Patterns.WEB_URL.matcher(url).matches() |
| 269 | || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) { |
| 270 | return false; |
| 271 | } |
| 272 | return true; |
| 273 | } |
| 274 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 275 | // Completion Listener |
| 276 | |
| 277 | @Override |
| 278 | public void onSearch(String search) { |
Michael Kolb | 7cdc490 | 2011-02-03 17:54:40 -0800 | [diff] [blame] | 279 | mListener.onCopySuggestion(search); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | @Override |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 283 | public void onSelect(String url, int type, String extra) { |
Michael Kolb | 5ff5c8b | 2012-05-03 11:37:58 -0700 | [diff] [blame] | 284 | finishInput(url, extra, SUGGESTED); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 287 | @Override |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 288 | public void onItemClick( |
| 289 | AdapterView<?> parent, View view, int position, long id) { |
| 290 | SuggestItem item = mAdapter.getItem(position); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 291 | onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra); |
John Reck | 87369ea | 2011-01-23 15:20:38 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 294 | interface UrlInputListener { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 295 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 296 | public void onDismiss(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 297 | |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 298 | public void onAction(String text, String extra, String source); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 299 | |
Michael Kolb | 7cdc490 | 2011-02-03 17:54:40 -0800 | [diff] [blame] | 300 | public void onCopySuggestion(String text); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 301 | |
| 302 | } |
| 303 | |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 304 | public void setIncognitoMode(boolean incognito) { |
John Reck | b77bdc4 | 2011-01-24 13:24:48 -0800 | [diff] [blame] | 305 | mIncognitoMode = incognito; |
| 306 | mAdapter.setIncognitoMode(mIncognitoMode); |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 309 | @Override |
| 310 | public boolean onKeyDown(int keyCode, KeyEvent evt) { |
| 311 | if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) { |
| 312 | finishInput(null, null, null); |
| 313 | return true; |
| 314 | } |
| 315 | return super.onKeyDown(keyCode, evt); |
| 316 | } |
| 317 | |
Narayan Kamath | 80aad8d | 2011-02-23 12:01:13 +0000 | [diff] [blame] | 318 | public SuggestionsAdapter getAdapter() { |
| 319 | return mAdapter; |
| 320 | } |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 321 | |
Michael Kolb | 74e60c2 | 2011-04-30 16:43:47 -0700 | [diff] [blame] | 322 | /* |
| 323 | * no-op to prevent scrolling of webview when embedded titlebar |
| 324 | * gets edited |
| 325 | */ |
| 326 | @Override |
| 327 | public boolean requestRectangleOnScreen(Rect rect, boolean immediate) { |
| 328 | return false; |
| 329 | } |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 330 | |
| 331 | @Override |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 332 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { } |
| 333 | |
| 334 | @Override |
| 335 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 336 | if (StateListener.STATE_HIGHLIGHTED == mState) { |
| 337 | changeState(StateListener.STATE_EDITED); |
| 338 | } |
| 339 | } |
| 340 | |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 341 | @Override |
| 342 | public void afterTextChanged(Editable s) { } |
| 343 | |
Axesh R. Ajmera | 9bb763e | 2014-04-04 15:37:34 -0700 | [diff] [blame] | 344 | /** |
| 345 | * It will prompt the toast if the text length greater than the given length. |
| 346 | */ |
| 347 | private class UrlLengthFilter extends LengthFilter { |
| 348 | public UrlLengthFilter(int max) { |
| 349 | super(max); |
| 350 | } |
| 351 | |
| 352 | @Override |
| 353 | public CharSequence filter(CharSequence source, int start, int end, Spanned dest, |
| 354 | int dstart, int dend) { |
| 355 | CharSequence result = super.filter(source, start, end, dest, dstart, dend); |
| 356 | if (result != null) { |
| 357 | // If the result is not null, it means the text length is greater than |
| 358 | // the given length. We will prompt the toast to alert the user. |
| 359 | CharSequence alert = getResources().getString(R.string.max_url_character_limit_msg); |
| 360 | Toast t = Toast.makeText(mContext , alert, Toast.LENGTH_SHORT); |
| 361 | t.setGravity(Gravity.CENTER, 0, 0); |
| 362 | t.show(); |
| 363 | } |
| 364 | return result; |
| 365 | } |
| 366 | } |
| 367 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 368 | } |