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