blob: f0037a1223a3ac3224f2e5494d767b4a8b4fcc9d [file] [log] [blame]
Michael Kolbfe251992010-07-08 15:41:55 -07001/*
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 Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolbfe251992010-07-08 15:41:55 -070018
Tarun Nainanid5306732014-04-28 19:38:28 -070019import android.content.ContentResolver;
Michael Kolbfe251992010-07-08 15:41:55 -070020import android.content.Context;
Michael Kolb21ce4d22010-09-15 14:55:05 -070021import android.content.res.Configuration;
Narayan Kamath5119edd2011-02-23 15:49:17 +000022import android.graphics.Rect;
John Reck8d021aa2011-09-06 15:30:11 -070023import android.graphics.drawable.Drawable;
Tarun Nainanid5306732014-04-28 19:38:28 -070024import android.inputmethodservice.InputMethodService;
25import android.provider.Settings.Secure;
26import android.os.Build;
Narayan Kamathf3174a52011-11-17 14:43:32 +000027import android.text.Editable;
Axesh R. Ajmera9bb763e2014-04-04 15:37:34 -070028import android.text.InputFilter;
29import android.text.InputFilter.LengthFilter;
30import android.text.Spanned;
John Reckdcda1d52010-11-29 10:05:54 -080031import android.text.TextUtils;
Narayan Kamathf3174a52011-11-17 14:43:32 +000032import android.text.TextWatcher;
Michael Kolbfe251992010-07-08 15:41:55 -070033import android.util.AttributeSet;
John Reckb77bdc42011-01-24 13:24:48 -080034import android.util.Patterns;
Axesh R. Ajmera9bb763e2014-04-04 15:37:34 -070035import android.view.Gravity;
Michael Kolbfe251992010-07-08 15:41:55 -070036import android.view.KeyEvent;
Michael Kolb305b1c52011-06-21 16:16:22 -070037import android.view.MotionEvent;
Michael Kolbfe251992010-07-08 15:41:55 -070038import android.view.View;
Tarun Nainani244dc462014-04-03 17:39:20 -070039import android.view.inputmethod.EditorInfo;
40import android.view.inputmethod.InputConnection;
Tarun Nainanid5306732014-04-28 19:38:28 -070041import android.view.inputmethod.InputMethodInfo;
Michael Kolbfe251992010-07-08 15:41:55 -070042import android.view.inputmethod.InputMethodManager;
Tarun Nainanid5306732014-04-28 19:38:28 -070043import android.view.inputmethod.InputMethodSubtype;
Tarun Nainani244dc462014-04-03 17:39:20 -070044import android.text.InputType;
John Reck87369ea2011-01-23 15:20:38 -080045import android.widget.AdapterView;
46import android.widget.AdapterView.OnItemClickListener;
Narayan Kamathf3174a52011-11-17 14:43:32 +000047import android.widget.AutoCompleteTextView;
Michael Kolbfe251992010-07-08 15:41:55 -070048import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070049import android.widget.TextView.OnEditorActionListener;
Axesh R. Ajmera9bb763e2014-04-04 15:37:34 -070050import android.widget.Toast;
Michael Kolbfe251992010-07-08 15:41:55 -070051
Bijan Amirzada41242f22014-03-21 12:12:18 -070052import com.android.browser.SuggestionsAdapter.CompletionListener;
53import com.android.browser.SuggestionsAdapter.SuggestItem;
54import com.android.browser.reflect.ReflectHelper;
55import com.android.browser.search.SearchEngine;
56import com.android.browser.search.SearchEngineInfo;
57import com.android.browser.search.SearchEngines;
Michael Kolb305b1c52011-06-21 16:16:22 -070058
Tarun Nainanid5306732014-04-28 19:38:28 -070059import java.util.List;
Michael Kolbfe251992010-07-08 15:41:55 -070060/**
61 * url/search input view
62 * handling suggestions
63 */
Narayan Kamathf3174a52011-11-17 14:43:32 +000064public class UrlInputView extends AutoCompleteTextView
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080065 implements OnEditorActionListener,
Narayan Kamathf3174a52011-11-17 14:43:32 +000066 CompletionListener, OnItemClickListener, TextWatcher {
Michael Kolb257cc2c2010-12-09 09:45:52 -080067
68 static final String TYPED = "browser-type";
69 static final String SUGGESTED = "browser-suggest";
Tarun Nainanid5306732014-04-28 19:38:28 -070070 static final String LATIN_INPUTMETHOD_PACKAGE_NAME = "com.android.inputmethod.latin";
Michael Kolb257cc2c2010-12-09 09:45:52 -080071
Michael Kolb94ec5272011-08-31 16:23:57 -070072 static final int POST_DELAY = 100;
Axesh R. Ajmera9bb763e2014-04-04 15:37:34 -070073 static final int URL_MAX_LENGTH = 2048;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070074 static final int POST_DELAY_FOCUS = 300;
Michael Kolb94ec5272011-08-31 16:23:57 -070075
Michael Kolb305b1c52011-06-21 16:16:22 -070076 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 Kolbfe251992010-07-08 15:41:55 -070084 private UrlInputListener mListener;
85 private InputMethodManager mInputManager;
86 private SuggestionsAdapter mAdapter;
Michael Kolb21ce4d22010-09-15 14:55:05 -070087 private View mContainer;
88 private boolean mLandscape;
John Reckb77bdc42011-01-24 13:24:48 -080089 private boolean mIncognitoMode;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080090 private boolean mNeedsUpdate;
Axesh R. Ajmera9bb763e2014-04-04 15:37:34 -070091 private Context mContext;
Michael Kolbfe251992010-07-08 15:41:55 -070092
Michael Kolb305b1c52011-06-21 16:16:22 -070093 private int mState;
94 private StateListener mStateListener;
John Reck8d021aa2011-09-06 15:30:11 -070095 private Rect mPopupPadding;
Michael Kolb305b1c52011-06-21 16:16:22 -070096
Michael Kolbfe251992010-07-08 15:41:55 -070097 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
98 super(context, attrs, defStyle);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080099 // SWE_TODO : HARDCODED a random background - clean up
100 /*
John Reck8d021aa2011-09-06 15:30:11 -0700101 TypedArray a = context.obtainStyledAttributes(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800102 attrs, R.styleable.PopupWindow,
John Reck8d021aa2011-09-06 15:30:11 -0700103 R.attr.autoCompleteTextViewStyle, 0);
104
105 Drawable popupbg = a.getDrawable(R.styleable.PopupWindow_popupBackground);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800106 a.recycle(); */
107 Drawable popupbg = context.getResources().getDrawable(android.R.drawable.editbox_background);
John Reck8d021aa2011-09-06 15:30:11 -0700108 mPopupPadding = new Rect();
109 popupbg.getPadding(mPopupPadding);
Michael Kolbfe251992010-07-08 15:41:55 -0700110 init(context);
111 }
112
113 public UrlInputView(Context context, AttributeSet attrs) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800114 // SWE_TODO : Needs Fix
115 //this(context, attrs, R.attr.autoCompleteTextViewStyle);
116 this(context, attrs, 0);
Michael Kolbfe251992010-07-08 15:41:55 -0700117 }
118
Tarun Nainanid5306732014-04-28 19:38:28 -0700119 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 Nainani244dc462014-04-03 17:39:20 -0700135 @Override
136 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
Tarun Nainanid5306732014-04-28 19:38:28 -0700137 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 Nainani244dc462014-04-03 17:39:20 -0700145 }
146
Michael Kolbfe251992010-07-08 15:41:55 -0700147 public UrlInputView(Context context) {
John Reck8d021aa2011-09-06 15:30:11 -0700148 this(context, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700149 }
150
151 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -0700152 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -0700153 setOnEditorActionListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700154 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -0700155 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -0800156 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700157 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -0800158 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -0800159 setOnItemClickListener(this);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800160 mNeedsUpdate = false;
Narayan Kamathf3174a52011-11-17 14:43:32 +0000161 addTextChangedListener(this);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000162
Michael Kolb305b1c52011-06-21 16:16:22 -0700163 mState = StateListener.STATE_NORMAL;
Axesh R. Ajmera9bb763e2014-04-04 15:37:34 -0700164 mContext = ctx;
165
166 this.setFilters(new InputFilter[] {
167 new UrlLengthFilter(URL_MAX_LENGTH)
168 });
Michael Kolb305b1c52011-06-21 16:16:22 -0700169 }
170
171 protected void onFocusChanged(boolean focused, int direction, Rect prevRect) {
172 super.onFocusChanged(focused, direction, prevRect);
Michael Kolb94ec5272011-08-31 16:23:57 -0700173 int state = -1;
Michael Kolb305b1c52011-06-21 16:16:22 -0700174 if (focused) {
175 if (hasSelection()) {
Michael Kolb94ec5272011-08-31 16:23:57 -0700176 state = StateListener.STATE_HIGHLIGHTED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700177 } else {
Michael Kolb94ec5272011-08-31 16:23:57 -0700178 state = StateListener.STATE_EDITED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700179 }
180 } else {
181 // reset the selection state
Michael Kolb94ec5272011-08-31 16:23:57 -0700182 state = StateListener.STATE_NORMAL;
Michael Kolb305b1c52011-06-21 16:16:22 -0700183 }
Michael Kolb94ec5272011-08-31 16:23:57 -0700184 final int s = state;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700185 postDelayed(new Runnable() {
Michael Kolb94ec5272011-08-31 16:23:57 -0700186 public void run() {
187 changeState(s);
188 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700189 }, POST_DELAY_FOCUS);
Michael Kolb305b1c52011-06-21 16:16:22 -0700190 }
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 Kolb94ec5272011-08-31 16:23:57 -0700198 postDelayed(new Runnable() {
199 public void run() {
200 changeState(StateListener.STATE_EDITED);
201 }}, POST_DELAY);
Michael Kolb305b1c52011-06-21 16:16:22 -0700202 }
203 return res;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800204 }
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 Kolb21ce4d22010-09-15 14:55:05 -0700218 }
219
Michael Kolbba99c5d2010-11-29 14:57:41 -0800220 void setController(UiController controller) {
221 UrlSelectionActionMode urlSelectionMode
222 = new UrlSelectionActionMode(controller);
223 setCustomSelectionActionModeCallback(urlSelectionMode);
224 }
225
Michael Kolb21ce4d22010-09-15 14:55:05 -0700226 void setContainer(View container) {
227 mContainer = container;
228 }
229
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800230 public void setUrlInputListener(UrlInputListener listener) {
231 mListener = listener;
232 }
233
Michael Kolb305b1c52011-06-21 16:16:22 -0700234 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 Kolb5e8f2b92011-07-19 13:22:32 -0700247 int getState() {
248 return mState;
249 }
250
Michael Kolb21ce4d22010-09-15 14:55:05 -0700251 @Override
252 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800253 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700254 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800255 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800256 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700257 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800258 setupDropDown();
Narayan Kamathf3174a52011-11-17 14:43:32 +0000259 performFiltering(getText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700260 }
261 }
262
263 @Override
264 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800265 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 Reck92026732011-02-15 10:12:30 -0800276 int width = mContainer != null ? mContainer.getWidth() : getWidth();
John Reck8d021aa2011-09-06 15:30:11 -0700277 width += mPopupPadding.left + mPopupPadding.right;
Michael Kolbc5998c22010-10-10 16:04:35 -0700278 if (width != getDropDownWidth()) {
279 setDropDownWidth(width);
280 }
John Reck8d021aa2011-09-06 15:30:11 -0700281 int left = getLeft();
282 left += mPopupPadding.left;
283 if (left != -getDropDownHorizontalOffset()) {
284 setDropDownHorizontalOffset(-left);
Michael Kolbc5998c22010-10-10 16:04:35 -0700285 }
Michael Kolb513286f2010-09-09 12:55:12 -0700286 }
287
288 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700289 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000290 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700291 return true;
292 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700293
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800294 void forceFilter() {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800295 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700296 }
297
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800298 void hideIME() {
299 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
300 }
301
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700302 void showIME() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800303 //mInputManager.focusIn(this);
304 Object[] params = {this};
305 Class[] type = new Class[] {View.class};
306 ReflectHelper.invokeMethod(mInputManager, "focusIn", type, params);
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700307 mInputManager.showSoftInput(this, 0);
308 }
309
Michael Kolb257cc2c2010-12-09 09:45:52 -0800310 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800311 mNeedsUpdate = true;
312 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700313 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800314 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700315 mListener.onDismiss();
316 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800317 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 Amirzada9b1e9882014-02-26 17:15:46 -0800324 .getSearchEngineInfo(getContext(), searchEngine.getName());
John Reckb77bdc42011-01-24 13:24:48 -0800325 if (engineInfo == null) return;
326 url = engineInfo.getSearchUriForQuery(url);
327 // mLister.onAction can take it from here without logging
328 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800329 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700330 }
Michael Kolbfe251992010-07-08 15:41:55 -0700331 }
332
John Reckb77bdc42011-01-24 13:24:48 -0800333 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 Kolb21ce4d22010-09-15 14:55:05 -0700344 // Completion Listener
345
346 @Override
347 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800348 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700349 }
350
351 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800352 public void onSelect(String url, int type, String extra) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700353 finishInput(url, extra, SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700354 }
355
Michael Kolbfe251992010-07-08 15:41:55 -0700356 @Override
John Reck87369ea2011-01-23 15:20:38 -0800357 public void onItemClick(
358 AdapterView<?> parent, View view, int position, long id) {
359 SuggestItem item = mAdapter.getItem(position);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000360 onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra);
John Reck87369ea2011-01-23 15:20:38 -0800361 }
362
Michael Kolbfe251992010-07-08 15:41:55 -0700363 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700364
Michael Kolbfe251992010-07-08 15:41:55 -0700365 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700366
Michael Kolb257cc2c2010-12-09 09:45:52 -0800367 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700368
Michael Kolb7cdc4902011-02-03 17:54:40 -0800369 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700370
371 }
372
John Reck117f07d2011-01-24 09:39:03 -0800373 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800374 mIncognitoMode = incognito;
375 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800376 }
377
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800378 @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 Kamath80aad8d2011-02-23 12:01:13 +0000387 public SuggestionsAdapter getAdapter() {
388 return mAdapter;
389 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000390
Michael Kolb74e60c22011-04-30 16:43:47 -0700391 /*
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 Kolb305b1c52011-06-21 16:16:22 -0700399
400 @Override
Narayan Kamathf3174a52011-11-17 14:43:32 +0000401 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 Kolb305b1c52011-06-21 16:16:22 -0700405 if (StateListener.STATE_HIGHLIGHTED == mState) {
406 changeState(StateListener.STATE_EDITED);
407 }
408 }
409
Narayan Kamathf3174a52011-11-17 14:43:32 +0000410 @Override
411 public void afterTextChanged(Editable s) { }
412
Axesh R. Ajmera9bb763e2014-04-04 15:37:34 -0700413 /**
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 Kolbfe251992010-07-08 15:41:55 -0700437}