blob: be16ab17e2f05951d1d947f09e97c87bbd4d64b9 [file] [log] [blame]
luxiaoldbe4a622013-07-19 17:14:06 +08001/*
2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
Bijan Amirzada41242f22014-03-21 12:12:18 -070030package com.android.browser;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080031
Bijan Amirzada41242f22014-03-21 12:12:18 -070032import com.android.browser.R;
luxiaoldbe4a622013-07-19 17:14:06 +080033
34import android.app.AlertDialog;
35import android.content.Context;
36import android.content.DialogInterface;
37import android.text.InputFilter;
38import android.text.Spanned;
39import android.util.Log;
40import android.widget.EditText;
41
42public class BrowserUtils {
43
44 private static final String LOGTAG = "BrowserUtils";
45 public static final int FILENAME_MAX_LENGTH = 32;
46 public static final int ADDRESS_MAX_LENGTH = 2048;
47 private static AlertDialog.Builder mAlertDialog = null;
48
49 public static void maxLengthFilter(final Context context, final EditText editText,
50 final int max_length) {
51 InputFilter[] contentFilters = new InputFilter[1];
52 contentFilters[0] = new InputFilter.LengthFilter(max_length) {
53 public CharSequence filter(CharSequence source, int start, int end,
54 Spanned dest, int dstart, int dend) {
55 int keep = max_length - (dest.length() - (dend - dstart));
56 if (keep <= 0) {
57 showWarningDialog(context, max_length);
58 return "";
59 } else if (keep >= end - start) {
60 return null;
61 } else {
62 if (keep < source.length()) {
63 showWarningDialog(context, max_length);
64 }
65 return source.subSequence(start, start + keep);
66 }
67 }
68 };
69 editText.setFilters(contentFilters);
70 }
71
72 private static void showWarningDialog(final Context context, int max_length) {
73 if (mAlertDialog != null)
74 return;
75
76 mAlertDialog = new AlertDialog.Builder(context);
77 mAlertDialog.setTitle(R.string.browser_max_input_title)
78 .setIcon(android.R.drawable.ic_dialog_info)
79 .setMessage(context.getString(R.string.browser_max_input, max_length))
80 .setPositiveButton(R.string.ok,
81 new DialogInterface.OnClickListener() {
82 public void onClick(DialogInterface dialog, int which) {
83 return;
84 }
85 })
86 .show()
87 .setOnDismissListener(new DialogInterface.OnDismissListener() {
88 public void onDismiss(DialogInterface dialog) {
89 Log.w("BrowserUtils", "onDismiss");
90 mAlertDialog = null;
91 return;
92 }
93 });
94 }
95}