Merge "Filter non-ascii popup charcters from password keyboard"

main
Tadashi G. Takaoka 2011-08-01 01:32:48 -07:00 committed by Android (Google) Code Review
commit 271e55fe53
2 changed files with 60 additions and 34 deletions

View File

@ -33,7 +33,6 @@ import com.android.inputmethod.keyboard.internal.PopupCharactersParser;
import com.android.inputmethod.keyboard.internal.Row;
import com.android.inputmethod.latin.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@ -292,13 +291,18 @@ public class Key {
mY = y;
mWidth = keyWidth - mGap;
final CharSequence[] popupCharacters = style.getTextArray(keyAttr,
R.styleable.Keyboard_Key_popupCharacters);
CharSequence[] popupCharacters = style.getTextArray(
keyAttr, R.styleable.Keyboard_Key_popupCharacters);
if (mKeyboard.mId.mPasswordInput) {
popupCharacters = PopupCharactersParser.filterOut(
res, popupCharacters, PopupCharactersParser.NON_ASCII_FILTER);
}
// In Arabic symbol layouts, we'd like to keep digits in popup characters regardless of
// config_digit_popup_characters_enabled.
if (mKeyboard.mId.isAlphabetKeyboard() && !res.getBoolean(
R.bool.config_digit_popup_characters_enabled)) {
mPopupCharacters = filterOutDigitPopupCharacters(popupCharacters);
mPopupCharacters = PopupCharactersParser.filterOut(
res, popupCharacters, PopupCharactersParser.DIGIT_FILTER);
} else {
mPopupCharacters = popupCharacters;
}
@ -402,36 +406,6 @@ public class Key {
return (mLabelOption & LABEL_OPTION_HAS_HINT_LABEL) != 0;
}
private static boolean isDigitPopupCharacter(CharSequence label) {
return label != null && label.length() == 1 && Character.isDigit(label.charAt(0));
}
private static CharSequence[] filterOutDigitPopupCharacters(CharSequence[] popupCharacters) {
if (popupCharacters == null || popupCharacters.length < 1)
return null;
if (popupCharacters.length == 1 && isDigitPopupCharacter(
PopupCharactersParser.getLabel(popupCharacters[0].toString())))
return null;
ArrayList<CharSequence> filtered = null;
for (int i = 0; i < popupCharacters.length; i++) {
final CharSequence popupSpec = popupCharacters[i];
if (isDigitPopupCharacter(PopupCharactersParser.getLabel(popupSpec.toString()))) {
if (filtered == null) {
filtered = new ArrayList<CharSequence>();
for (int j = 0; j < i; j++)
filtered.add(popupCharacters[j]);
}
} else if (filtered != null) {
filtered.add(popupSpec);
}
}
if (filtered == null)
return popupCharacters;
if (filtered.size() == 0)
return null;
return filtered.toArray(new CharSequence[filtered.size()]);
}
public Drawable getIcon() {
return mIcon;
}

View File

@ -23,6 +23,8 @@ import android.util.Log;
import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.latin.R;
import java.util.ArrayList;
/**
* String parser of popupCharacters attribute of Key.
* The string is comma separated texts each of which represents one popup key.
@ -182,4 +184,54 @@ public class PopupCharactersParser {
super(message);
}
}
public interface CodeFilter {
public boolean shouldFilterOut(int code);
}
public static final CodeFilter DIGIT_FILTER = new CodeFilter() {
@Override
public boolean shouldFilterOut(int code) {
return Character.isDigit(code);
}
};
public static final CodeFilter NON_ASCII_FILTER = new CodeFilter() {
@Override
public boolean shouldFilterOut(int code) {
return code < 0x20 || code > 0x7e;
}
};
public static CharSequence[] filterOut(Resources res, CharSequence[] popupCharacters,
CodeFilter filter) {
if (popupCharacters == null || popupCharacters.length < 1) {
return null;
}
if (popupCharacters.length == 1
&& filter.shouldFilterOut(getCode(res, popupCharacters[0].toString()))) {
return null;
}
ArrayList<CharSequence> filtered = null;
for (int i = 0; i < popupCharacters.length; i++) {
final CharSequence popupSpec = popupCharacters[i];
if (filter.shouldFilterOut(getCode(res, popupSpec.toString()))) {
if (filtered == null) {
filtered = new ArrayList<CharSequence>();
for (int j = 0; j < i; j++) {
filtered.add(popupCharacters[j]);
}
}
} else if (filtered != null) {
filtered.add(popupSpec);
}
}
if (filtered == null) {
return popupCharacters;
}
if (filtered.size() == 0) {
return null;
}
return filtered.toArray(new CharSequence[filtered.size()]);
}
}