blob: c8f2d401cca2010ffd0c75e0ba928e427fc3bbc8 [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;
Michael Kolb94ec5272011-08-31 16:23:57 -070074
Michael Kolb305b1c52011-06-21 16:16:22 -070075 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 Kolbfe251992010-07-08 15:41:55 -070083 private UrlInputListener mListener;
84 private InputMethodManager mInputManager;
85 private SuggestionsAdapter mAdapter;
Michael Kolb21ce4d22010-09-15 14:55:05 -070086 private View mContainer;
87 private boolean mLandscape;
John Reckb77bdc42011-01-24 13:24:48 -080088 private boolean mIncognitoMode;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080089 private boolean mNeedsUpdate;
Axesh R. Ajmera9bb763e2014-04-04 15:37:34 -070090 private Context mContext;
Michael Kolbfe251992010-07-08 15:41:55 -070091
Michael Kolb305b1c52011-06-21 16:16:22 -070092 private int mState;
93 private StateListener mStateListener;
John Reck8d021aa2011-09-06 15:30:11 -070094 private Rect mPopupPadding;
Michael Kolb305b1c52011-06-21 16:16:22 -070095
Michael Kolbfe251992010-07-08 15:41:55 -070096 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
97 super(context, attrs, defStyle);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080098 // SWE_TODO : HARDCODED a random background - clean up
99 /*
John Reck8d021aa2011-09-06 15:30:11 -0700100 TypedArray a = context.obtainStyledAttributes(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800101 attrs, R.styleable.PopupWindow,
John Reck8d021aa2011-09-06 15:30:11 -0700102 R.attr.autoCompleteTextViewStyle, 0);
103
104 Drawable popupbg = a.getDrawable(R.styleable.PopupWindow_popupBackground);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800105 a.recycle(); */
106 Drawable popupbg = context.getResources().getDrawable(android.R.drawable.editbox_background);
John Reck8d021aa2011-09-06 15:30:11 -0700107 mPopupPadding = new Rect();
108 popupbg.getPadding(mPopupPadding);
Michael Kolbfe251992010-07-08 15:41:55 -0700109 init(context);
110 }
111
112 public UrlInputView(Context context, AttributeSet attrs) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800113 // SWE_TODO : Needs Fix
114 //this(context, attrs, R.attr.autoCompleteTextViewStyle);
115 this(context, attrs, 0);
Michael Kolbfe251992010-07-08 15:41:55 -0700116 }
117
Tarun Nainanid5306732014-04-28 19:38:28 -0700118 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 Nainani244dc462014-04-03 17:39:20 -0700134 @Override
135 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
Tarun Nainanid5306732014-04-28 19:38:28 -0700136 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 Nainani244dc462014-04-03 17:39:20 -0700144 }
145
Michael Kolbfe251992010-07-08 15:41:55 -0700146 public UrlInputView(Context context) {
John Reck8d021aa2011-09-06 15:30:11 -0700147 this(context, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700148 }
149
150 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -0700151 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -0700152 setOnEditorActionListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700153 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -0700154 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -0800155 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700156 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -0800157 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -0800158 setOnItemClickListener(this);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800159 mNeedsUpdate = false;
Narayan Kamathf3174a52011-11-17 14:43:32 +0000160 addTextChangedListener(this);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000161
Michael Kolb305b1c52011-06-21 16:16:22 -0700162 mState = StateListener.STATE_NORMAL;
Axesh R. Ajmera9bb763e2014-04-04 15:37:34 -0700163 mContext = ctx;
164
165 this.setFilters(new InputFilter[] {
166 new UrlLengthFilter(URL_MAX_LENGTH)
167 });
Michael Kolb305b1c52011-06-21 16:16:22 -0700168 }
169
170 protected void onFocusChanged(boolean focused, int direction, Rect prevRect) {
171 super.onFocusChanged(focused, direction, prevRect);
Michael Kolb94ec5272011-08-31 16:23:57 -0700172 int state = -1;
Michael Kolb305b1c52011-06-21 16:16:22 -0700173 if (focused) {
174 if (hasSelection()) {
Michael Kolb94ec5272011-08-31 16:23:57 -0700175 state = StateListener.STATE_HIGHLIGHTED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700176 } else {
Michael Kolb94ec5272011-08-31 16:23:57 -0700177 state = StateListener.STATE_EDITED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700178 }
179 } else {
180 // reset the selection state
Michael Kolb94ec5272011-08-31 16:23:57 -0700181 state = StateListener.STATE_NORMAL;
Michael Kolb305b1c52011-06-21 16:16:22 -0700182 }
Michael Kolb94ec5272011-08-31 16:23:57 -0700183 final int s = state;
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700184 post(new Runnable() {
Michael Kolb94ec5272011-08-31 16:23:57 -0700185 public void run() {
186 changeState(s);
187 }
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700188 });
Michael Kolb305b1c52011-06-21 16:16:22 -0700189 }
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 Kolb94ec5272011-08-31 16:23:57 -0700197 postDelayed(new Runnable() {
198 public void run() {
199 changeState(StateListener.STATE_EDITED);
200 }}, POST_DELAY);
Michael Kolb305b1c52011-06-21 16:16:22 -0700201 }
202 return res;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800203 }
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 Kolb21ce4d22010-09-15 14:55:05 -0700217 }
218
Michael Kolbba99c5d2010-11-29 14:57:41 -0800219 void setController(UiController controller) {
220 UrlSelectionActionMode urlSelectionMode
221 = new UrlSelectionActionMode(controller);
222 setCustomSelectionActionModeCallback(urlSelectionMode);
223 }
224
Michael Kolb21ce4d22010-09-15 14:55:05 -0700225 void setContainer(View container) {
226 mContainer = container;
227 }
228
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800229 public void setUrlInputListener(UrlInputListener listener) {
230 mListener = listener;
231 }
232
Michael Kolb305b1c52011-06-21 16:16:22 -0700233 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 Kolb5e8f2b92011-07-19 13:22:32 -0700246 int getState() {
247 return mState;
248 }
249
Michael Kolb21ce4d22010-09-15 14:55:05 -0700250 @Override
251 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800252 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700253 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800254 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800255 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700256 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800257 setupDropDown();
Narayan Kamathf3174a52011-11-17 14:43:32 +0000258 performFiltering(getText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700259 }
260 }
261
262 @Override
263 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800264 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 Reck92026732011-02-15 10:12:30 -0800275 int width = mContainer != null ? mContainer.getWidth() : getWidth();
John Reck8d021aa2011-09-06 15:30:11 -0700276 width += mPopupPadding.left + mPopupPadding.right;
Michael Kolbc5998c22010-10-10 16:04:35 -0700277 if (width != getDropDownWidth()) {
278 setDropDownWidth(width);
279 }
John Reck8d021aa2011-09-06 15:30:11 -0700280 int left = getLeft();
281 left += mPopupPadding.left;
282 if (left != -getDropDownHorizontalOffset()) {
283 setDropDownHorizontalOffset(-left);
Michael Kolbc5998c22010-10-10 16:04:35 -0700284 }
Michael Kolb513286f2010-09-09 12:55:12 -0700285 }
286
287 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700288 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000289 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700290 return true;
291 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700292
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800293 void forceFilter() {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800294 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700295 }
296
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800297 void hideIME() {
298 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
299 }
300
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700301 void showIME() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800302 //mInputManager.focusIn(this);
303 Object[] params = {this};
304 Class[] type = new Class[] {View.class};
305 ReflectHelper.invokeMethod(mInputManager, "focusIn", type, params);
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700306 mInputManager.showSoftInput(this, 0);
307 }
308
Michael Kolb257cc2c2010-12-09 09:45:52 -0800309 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800310 mNeedsUpdate = true;
311 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700312 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800313 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700314 mListener.onDismiss();
315 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800316 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 Amirzada9b1e9882014-02-26 17:15:46 -0800323 .getSearchEngineInfo(getContext(), searchEngine.getName());
John Reckb77bdc42011-01-24 13:24:48 -0800324 if (engineInfo == null) return;
325 url = engineInfo.getSearchUriForQuery(url);
326 // mLister.onAction can take it from here without logging
327 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800328 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700329 }
Michael Kolbfe251992010-07-08 15:41:55 -0700330 }
331
John Reckb77bdc42011-01-24 13:24:48 -0800332 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 Kolb21ce4d22010-09-15 14:55:05 -0700343 // Completion Listener
344
345 @Override
346 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800347 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700348 }
349
350 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800351 public void onSelect(String url, int type, String extra) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700352 finishInput(url, extra, SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700353 }
354
Michael Kolbfe251992010-07-08 15:41:55 -0700355 @Override
John Reck87369ea2011-01-23 15:20:38 -0800356 public void onItemClick(
357 AdapterView<?> parent, View view, int position, long id) {
358 SuggestItem item = mAdapter.getItem(position);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000359 onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra);
John Reck87369ea2011-01-23 15:20:38 -0800360 }
361
Michael Kolbfe251992010-07-08 15:41:55 -0700362 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700363
Michael Kolbfe251992010-07-08 15:41:55 -0700364 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700365
Michael Kolb257cc2c2010-12-09 09:45:52 -0800366 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700367
Michael Kolb7cdc4902011-02-03 17:54:40 -0800368 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700369
370 }
371
John Reck117f07d2011-01-24 09:39:03 -0800372 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800373 mIncognitoMode = incognito;
374 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800375 }
376
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800377 @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 Kamath80aad8d2011-02-23 12:01:13 +0000386 public SuggestionsAdapter getAdapter() {
387 return mAdapter;
388 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000389
Michael Kolb74e60c22011-04-30 16:43:47 -0700390 /*
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 Kolb305b1c52011-06-21 16:16:22 -0700398
399 @Override
Narayan Kamathf3174a52011-11-17 14:43:32 +0000400 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 Kolb305b1c52011-06-21 16:16:22 -0700404 if (StateListener.STATE_HIGHLIGHTED == mState) {
405 changeState(StateListener.STATE_EDITED);
406 }
407 }
408
Narayan Kamathf3174a52011-11-17 14:43:32 +0000409 @Override
410 public void afterTextChanged(Editable s) { }
411
Axesh R. Ajmera9bb763e2014-04-04 15:37:34 -0700412 /**
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 Kolbfe251992010-07-08 15:41:55 -0700436}