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