blob: 0e4eec4488ee6573d27d81e75aa24ac12ec9d325 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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
17package android.text;
18
19/**
20 * Abstract class for filtering login-related text (user names and passwords)
21 *
Charles Mungerc24469a2019-11-22 15:12:43 -080022 * @deprecated Password requirements should not be hardcoded in clients. This class also does not
23 * handle non-BMP characters.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024 */
Charles Mungerc24469a2019-11-22 15:12:43 -080025@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026public abstract class LoginFilter implements InputFilter {
27 private boolean mAppendInvalid; // whether to append or ignore invalid characters
28 /**
29 * Base constructor for LoginFilter
30 * @param appendInvalid whether or not to append invalid characters.
31 */
32 LoginFilter(boolean appendInvalid) {
33 mAppendInvalid = appendInvalid;
34 }
35
36 /**
37 * Default constructor for LoginFilter doesn't append invalid characters.
38 */
39 LoginFilter() {
40 mAppendInvalid = false;
41 }
42
43 /**
44 * This method is called when the buffer is going to replace the
45 * range <code>dstart &hellip; dend</code> of <code>dest</code>
46 * with the new text from the range <code>start &hellip; end</code>
47 * of <code>source</code>. Returns the CharSequence that we want
48 * placed there instead, including an empty string
49 * if appropriate, or <code>null</code> to accept the original
50 * replacement. Be careful to not to reject 0-length replacements,
51 * as this is what happens when you delete text.
52 */
53 public CharSequence filter(CharSequence source, int start, int end,
54 Spanned dest, int dstart, int dend) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 onStart();
56
57 // Scan through beginning characters in dest, calling onInvalidCharacter()
58 // for each invalid character.
59 for (int i = 0; i < dstart; i++) {
60 char c = dest.charAt(i);
61 if (!isAllowed(c)) onInvalidCharacter(c);
62 }
63
64 // Scan through changed characters rejecting disallowed chars
Daisuke Miyakawac1d27482009-05-25 17:37:41 +090065 SpannableStringBuilder modification = null;
66 int modoff = 0;
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 for (int i = start; i < end; i++) {
69 char c = source.charAt(i);
70 if (isAllowed(c)) {
Daisuke Miyakawac1d27482009-05-25 17:37:41 +090071 // Character allowed.
72 modoff++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 } else {
Daisuke Miyakawac1d27482009-05-25 17:37:41 +090074 if (mAppendInvalid) {
75 modoff++;
76 } else {
77 if (modification == null) {
78 modification = new SpannableStringBuilder(source, start, end);
79 modoff = i - start;
80 }
81
82 modification.delete(modoff, modoff + 1);
83 }
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 onInvalidCharacter(c);
86 }
87 }
88
89 // Scan through remaining characters in dest, calling onInvalidCharacter()
90 // for each invalid character.
91 for (int i = dend; i < dest.length(); i++) {
92 char c = dest.charAt(i);
93 if (!isAllowed(c)) onInvalidCharacter(c);
94 }
95
96 onStop();
97
Daisuke Miyakawac1d27482009-05-25 17:37:41 +090098 // Either returns null if we made no changes,
99 // or what we wanted to change it to if there were changes.
100 return modification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 }
102
103 /**
104 * Called when we start processing filter.
105 */
106 public void onStart() {
107
108 }
109
110 /**
111 * Called whenever we encounter an invalid character.
112 * @param c the invalid character
113 */
114 public void onInvalidCharacter(char c) {
115
116 }
117
118 /**
119 * Called when we're done processing filter
120 */
121 public void onStop() {
122
123 }
124
125 /**
126 * Returns whether or not we allow character c.
127 * Subclasses must override this method.
128 */
129 public abstract boolean isAllowed(char c);
130
131 /**
132 * This filter rejects characters in the user name that are not compatible with GMail
133 * account creation. It prevents the user from entering user names with characters other than
134 * [a-zA-Z0-9.].
135 *
Charles Mungerc24469a2019-11-22 15:12:43 -0800136 * @deprecated Do not encode assumptions about Google account names into client applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 */
Charles Mungerc24469a2019-11-22 15:12:43 -0800138 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 public static class UsernameFilterGMail extends LoginFilter {
140
141 public UsernameFilterGMail() {
142 super(false);
143 }
144
145 public UsernameFilterGMail(boolean appendInvalid) {
146 super(appendInvalid);
147 }
148
149 @Override
150 public boolean isAllowed(char c) {
151 // Allow [a-zA-Z0-9@.]
152 if ('0' <= c && c <= '9')
153 return true;
154 if ('a' <= c && c <= 'z')
155 return true;
156 if ('A' <= c && c <= 'Z')
157 return true;
158 if ('.' == c)
159 return true;
160 return false;
161 }
162 }
163
164 /**
165 * This filter rejects characters in the user name that are not compatible with Google login.
Fred Quintana30d20302010-01-14 12:16:20 -0800166 * It is slightly less restrictive than the above filter in that it allows [a-zA-Z0-9._-+].
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 *
168 */
169 public static class UsernameFilterGeneric extends LoginFilter {
Fred Quintana30d20302010-01-14 12:16:20 -0800170 private static final String mAllowed = "@_-+."; // Additional characters
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
172 public UsernameFilterGeneric() {
173 super(false);
174 }
175
176 public UsernameFilterGeneric(boolean appendInvalid) {
177 super(appendInvalid);
178 }
179
180 @Override
181 public boolean isAllowed(char c) {
182 // Allow [a-zA-Z0-9@.]
183 if ('0' <= c && c <= '9')
184 return true;
185 if ('a' <= c && c <= 'z')
186 return true;
187 if ('A' <= c && c <= 'Z')
188 return true;
189 if (mAllowed.indexOf(c) != -1)
190 return true;
191 return false;
192 }
193 }
194
195 /**
196 * This filter is compatible with GMail passwords which restricts characters to
197 * the Latin-1 (ISO8859-1) char set.
Charles Mungerc24469a2019-11-22 15:12:43 -0800198 *
199 * @deprecated Do not handle a user's Google password. Refer to
200 * <a href="https://support.google.com/accounts/answer/32040">Google Help</a> for
201 * password restriction information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 */
Charles Mungerc24469a2019-11-22 15:12:43 -0800203 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 public static class PasswordFilterGMail extends LoginFilter {
205
206 public PasswordFilterGMail() {
207 super(false);
208 }
209
210 public PasswordFilterGMail(boolean appendInvalid) {
211 super(appendInvalid);
212 }
213
214 // We should reject anything not in the Latin-1 (ISO8859-1) charset
215 @Override
216 public boolean isAllowed(char c) {
217 if (32 <= c && c <= 127)
218 return true; // standard charset
219 // if (128 <= c && c <= 159) return true; // nonstandard (Windows(TM)(R)) charset
220 if (160 <= c && c <= 255)
221 return true; // extended charset
222 return false;
223 }
224 }
225}