Clean up KeyboardLayoutSet a bit

This CL moves getScriptId() from KeyboardLayoutSet.Builder to
KeyboardLayoutSet.

Change-Id: I13588467e3d03abb92120acb367f021ee3490a50
main
Tadashi G. Takaoka 2014-11-06 12:22:01 +09:00
parent ab68143ad0
commit b68f90ac1a
1 changed files with 19 additions and 28 deletions

View File

@ -52,6 +52,9 @@ import java.io.IOException;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.util.HashMap; import java.util.HashMap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/** /**
* This class represents a set of keyboard layouts. Each of them represents a different keyboard * This class represents a set of keyboard layouts. Each of them represents a different keyboard
* specific to a keyboard state, such as alphabet, symbols, and so on. Layouts in the same * specific to a keyboard state, such as alphabet, symbols, and so on. Layouts in the same
@ -83,6 +86,8 @@ public final class KeyboardLayoutSet {
private static final HashMap<KeyboardId, SoftReference<Keyboard>> sKeyboardCache = private static final HashMap<KeyboardId, SoftReference<Keyboard>> sKeyboardCache =
new HashMap<>(); new HashMap<>();
private static final KeysCache sKeysCache = new KeysCache(); private static final KeysCache sKeysCache = new KeysCache();
private final static HashMap<InputMethodSubtype, Integer> sScriptIdsForSubtypes =
new HashMap<>();
@SuppressWarnings("serial") @SuppressWarnings("serial")
public static final class KeyboardLayoutSetException extends RuntimeException { public static final class KeyboardLayoutSetException extends RuntimeException {
@ -141,6 +146,16 @@ public final class KeyboardLayoutSet {
sKeysCache.clear(); sKeysCache.clear();
} }
public static int getScriptId(final Resources resources, final InputMethodSubtype subtype) {
final Integer value = sScriptIdsForSubtypes.get(subtype);
if (null == value) {
final int scriptId = Builder.readScriptId(resources, subtype);
sScriptIdsForSubtypes.put(subtype, scriptId);
return scriptId;
}
return value;
}
KeyboardLayoutSet(final Context context, final Params params) { KeyboardLayoutSet(final Context context, final Params params) {
mContext = context; mContext = context;
mParams = params; mParams = params;
@ -245,7 +260,7 @@ public final class KeyboardLayoutSet {
private static final EditorInfo EMPTY_EDITOR_INFO = new EditorInfo(); private static final EditorInfo EMPTY_EDITOR_INFO = new EditorInfo();
public Builder(final Context context, final EditorInfo ei) { public Builder(final Context context, @Nullable final EditorInfo ei) {
mContext = context; mContext = context;
mPackageName = context.getPackageName(); mPackageName = context.getPackageName();
mResources = context.getResources(); mResources = context.getResources();
@ -266,7 +281,7 @@ public final class KeyboardLayoutSet {
return this; return this;
} }
public Builder setSubtype(final RichInputMethodSubtype subtype) { public Builder setSubtype(@Nonnull final RichInputMethodSubtype subtype) {
final boolean asciiCapable = InputMethodSubtypeCompatUtils.isAsciiCapable(subtype); final boolean asciiCapable = InputMethodSubtypeCompatUtils.isAsciiCapable(subtype);
// TODO: Consolidate with {@link InputAttributes}. // TODO: Consolidate with {@link InputAttributes}.
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@ -304,31 +319,13 @@ public final class KeyboardLayoutSet {
return this; return this;
} }
public Builder setScriptId(final int scriptId) {
mParams.mScriptId = scriptId;
return this;
}
public Builder setSplitLayoutEnabledByUser(final boolean enabled) { public Builder setSplitLayoutEnabledByUser(final boolean enabled) {
mParams.mIsSplitLayoutEnabledByUser = enabled; mParams.mIsSplitLayoutEnabledByUser = enabled;
return this; return this;
} }
private final static HashMap<InputMethodSubtype, Integer> sScriptIdsForSubtypes =
new HashMap<>();
public static int getScriptId(final Resources resources, final InputMethodSubtype subtype) {
final Integer value = sScriptIdsForSubtypes.get(subtype);
if (null == value) {
final int scriptId = readScriptId(resources, subtype);
sScriptIdsForSubtypes.put(subtype, scriptId);
return scriptId;
}
return value;
}
// Super redux version of reading the script ID for some subtype from Xml. // Super redux version of reading the script ID for some subtype from Xml.
private static int readScriptId(final Resources resources, static int readScriptId(final Resources resources, final InputMethodSubtype subtype) {
final InputMethodSubtype subtype) {
final String layoutSetName = KEYBOARD_LAYOUT_SET_RESOURCE_PREFIX final String layoutSetName = KEYBOARD_LAYOUT_SET_RESOURCE_PREFIX
+ SubtypeLocaleUtils.getKeyboardLayoutSetName(subtype); + SubtypeLocaleUtils.getKeyboardLayoutSetName(subtype);
final int xmlId = getXmlId(resources, layoutSetName); final int xmlId = getXmlId(resources, layoutSetName);
@ -415,7 +412,7 @@ public final class KeyboardLayoutSet {
if (TAG_ELEMENT.equals(tag)) { if (TAG_ELEMENT.equals(tag)) {
parseKeyboardLayoutSetElement(parser); parseKeyboardLayoutSetElement(parser);
} else if (TAG_FEATURE.equals(tag)) { } else if (TAG_FEATURE.equals(tag)) {
parseKeyboardLayoutSetFeature(parser); mParams.mScriptId = readScriptIdFromTagFeature(mResources, parser);
} else { } else {
throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_KEYBOARD_SET); throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_KEYBOARD_SET);
} }
@ -460,12 +457,6 @@ public final class KeyboardLayoutSet {
} }
} }
private void parseKeyboardLayoutSetFeature(final XmlPullParser parser)
throws XmlPullParserException, IOException {
final int scriptId = readScriptIdFromTagFeature(mResources, parser);
setScriptId(scriptId);
}
private static int getKeyboardMode(final EditorInfo editorInfo) { private static int getKeyboardMode(final EditorInfo editorInfo) {
final int inputType = editorInfo.inputType; final int inputType = editorInfo.inputType;
final int variation = inputType & InputType.TYPE_MASK_VARIATION; final int variation = inputType & InputType.TYPE_MASK_VARIATION;