Use Params instead of Builder as an argument of KeyboardSet constructor

Change-Id: I4c576a0b0b632cdf413f2ccd5c69dbd37747fbd3
This commit is contained in:
Tadashi G. Takaoka 2011-12-16 16:22:20 +09:00
parent 702f9fc194
commit 289544d6e4
2 changed files with 53 additions and 50 deletions

View file

@ -73,15 +73,6 @@ public class KeyboardId {
private final int mHashCode; private final int mHashCode;
public KeyboardId(int xmlId, int elementState, Locale locale, int orientation, int width, public KeyboardId(int xmlId, int elementState, Locale locale, int orientation, int width,
int mode, EditorInfo editorInfo, boolean settingsKeyEnabled,
boolean clobberSettingsKey, boolean shortcutKeyEnabled, boolean hasShortcutKey) {
this(xmlId, elementState, locale, orientation, width, mode,
(editorInfo != null ? editorInfo.inputType : 0),
(editorInfo != null ? editorInfo.imeOptions : 0),
settingsKeyEnabled, clobberSettingsKey, shortcutKeyEnabled, hasShortcutKey);
}
private KeyboardId(int xmlId, int elementState, Locale locale, int orientation, int width,
int mode, int inputType, int imeOptions, boolean settingsKeyEnabled, int mode, int inputType, int imeOptions, boolean settingsKeyEnabled,
boolean clobberSettingsKey, boolean shortcutKeyEnabled, boolean hasShortcutKey) { boolean clobberSettingsKey, boolean shortcutKeyEnabled, boolean hasShortcutKey) {
this.mLocale = locale; this.mLocale = locale;

View file

@ -17,11 +17,9 @@
package com.android.inputmethod.keyboard; package com.android.inputmethod.keyboard;
import android.content.Context; import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.content.res.XmlResourceParser; import android.content.res.XmlResourceParser;
import android.util.DisplayMetrics;
import android.util.Xml; import android.util.Xml;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
@ -43,8 +41,8 @@ import java.util.Locale;
/** /**
* This class has a set of {@link KeyboardId}s. Each of them represents a different keyboard * This class has a set of {@link KeyboardId}s. 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
* {@link KeyboardSet} are related to each other. * {@link KeyboardSet} are related to each other. A {@link KeyboardSet} needs to be created for each
* A {@link KeyboardSet} needs to be created for each {@link android.view.inputmethod.EditorInfo}. * {@link android.view.inputmethod.EditorInfo}.
*/ */
public class KeyboardSet { public class KeyboardSet {
private static final String TAG_KEYBOARD_SET = "KeyboardSet"; private static final String TAG_KEYBOARD_SET = "KeyboardSet";
@ -55,56 +53,67 @@ public class KeyboardSet {
public final KeyboardId mSymbolsId; public final KeyboardId mSymbolsId;
public final KeyboardId mSymbolsShiftedId; public final KeyboardId mSymbolsShiftedId;
KeyboardSet(Builder builder) { KeyboardSet(Params params) {
mAlphabetId = builder.getKeyboardId(false, false); mAlphabetId = Builder.getKeyboardId(false, false, params);
mSymbolsId = builder.getKeyboardId(true, false); mSymbolsId = Builder.getKeyboardId(true, false, params);
mSymbolsShiftedId = builder.getKeyboardId(true, true); mSymbolsShiftedId = Builder.getKeyboardId(true, true, params);
}
private static class Params {
int mMode;
int mInputTypes;
int mImeOptions;
boolean mSettingsKeyEnabled;
boolean mVoiceKeyEnabled;
boolean mVoiceKeyOnMain;
boolean mNoSettingsKey;
Locale mLocale;
int mOrientation;
int mWidth;
final HashMap<Integer, Integer> mElementKeyboards =
new HashMap<Integer, Integer>();
Params() {}
} }
public static class Builder { public static class Builder {
private final Resources mResources; private final Resources mResources;
private final EditorInfo mEditorInfo;
private final HashMap<Integer, Integer> mElementKeyboards = private final Params mParams = new Params();
new HashMap<Integer, Integer>();
private final int mMode;
private final boolean mSettingsKeyEnabled;
private final boolean mVoiceKeyEnabled;
private final boolean mVoiceKeyOnMain;
private final boolean mNoSettingsKey;
private final Locale mLocale;
private final Configuration mConf;
private final DisplayMetrics mMetrics;
public Builder(Context context, EditorInfo editorInfo, SettingsValues settingsValues) { public Builder(Context context, EditorInfo editorInfo, SettingsValues settingsValues) {
mResources = context.getResources(); mResources = context.getResources();
mEditorInfo = editorInfo;
final SubtypeSwitcher subtypeSwitcher = SubtypeSwitcher.getInstance(); final SubtypeSwitcher subtypeSwitcher = SubtypeSwitcher.getInstance();
final String packageName = context.getPackageName(); final String packageName = context.getPackageName();
final Params params = mParams;
mMode = Utils.getKeyboardMode(mEditorInfo); params.mMode = Utils.getKeyboardMode(editorInfo);
mSettingsKeyEnabled = settingsValues.isSettingsKeyEnabled(); if (editorInfo != null) {
params.mInputTypes = editorInfo.inputType;
params.mImeOptions = editorInfo.imeOptions;
}
params.mSettingsKeyEnabled = settingsValues.isSettingsKeyEnabled();
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
final boolean noMicrophone = Utils.inPrivateImeOptions( final boolean noMicrophone = Utils.inPrivateImeOptions(
packageName, LatinIME.IME_OPTION_NO_MICROPHONE, editorInfo) packageName, LatinIME.IME_OPTION_NO_MICROPHONE, editorInfo)
|| Utils.inPrivateImeOptions( || Utils.inPrivateImeOptions(
null, LatinIME.IME_OPTION_NO_MICROPHONE_COMPAT, editorInfo); null, LatinIME.IME_OPTION_NO_MICROPHONE_COMPAT, editorInfo);
mVoiceKeyEnabled = settingsValues.isVoiceKeyEnabled(editorInfo) && !noMicrophone; params.mVoiceKeyEnabled = settingsValues.isVoiceKeyEnabled(editorInfo) && !noMicrophone;
mVoiceKeyOnMain = settingsValues.isVoiceKeyOnMain(); params.mVoiceKeyOnMain = settingsValues.isVoiceKeyOnMain();
mNoSettingsKey = Utils.inPrivateImeOptions( params.mNoSettingsKey = Utils.inPrivateImeOptions(
packageName, LatinIME.IME_OPTION_NO_SETTINGS_KEY, editorInfo); packageName, LatinIME.IME_OPTION_NO_SETTINGS_KEY, editorInfo);
final boolean forceAscii = Utils.inPrivateImeOptions( final boolean forceAscii = Utils.inPrivateImeOptions(
packageName, LatinIME.IME_OPTION_FORCE_ASCII, editorInfo); packageName, LatinIME.IME_OPTION_FORCE_ASCII, editorInfo);
final boolean asciiCapable = subtypeSwitcher.currentSubtypeContainsExtraValueKey( final boolean asciiCapable = subtypeSwitcher.currentSubtypeContainsExtraValueKey(
LatinIME.SUBTYPE_EXTRA_VALUE_ASCII_CAPABLE); LatinIME.SUBTYPE_EXTRA_VALUE_ASCII_CAPABLE);
mLocale = (forceAscii && !asciiCapable) ? Locale.US : subtypeSwitcher.getInputLocale(); params.mLocale = (forceAscii && !asciiCapable)
mConf = mResources.getConfiguration(); ? Locale.US : subtypeSwitcher.getInputLocale();
mMetrics = mResources.getDisplayMetrics(); params.mOrientation = mResources.getConfiguration().orientation;
params.mWidth = mResources.getDisplayMetrics().widthPixels;
} }
public KeyboardSet build() { public KeyboardSet build() {
final Locale savedLocale = LocaleUtils.setSystemLocale(mResources, mLocale); final Locale savedLocale = LocaleUtils.setSystemLocale(mResources, mParams.mLocale);
try { try {
parseKeyboardSet(mResources, R.xml.keyboard_set); parseKeyboardSet(mResources, R.xml.keyboard_set);
} catch (Exception e) { } catch (Exception e) {
@ -112,16 +121,18 @@ public class KeyboardSet {
} finally { } finally {
LocaleUtils.setSystemLocale(mResources, savedLocale); LocaleUtils.setSystemLocale(mResources, savedLocale);
} }
return new KeyboardSet(this); return new KeyboardSet(mParams);
} }
KeyboardId getKeyboardId(boolean isSymbols, boolean isShift) { static KeyboardId getKeyboardId(boolean isSymbols, boolean isShift, Params params) {
final int elementState = getElementState(mMode, isSymbols, isShift); final int elementState = getElementState(params.mMode, isSymbols, isShift);
final int xmlId = mElementKeyboards.get(elementState); final int xmlId = params.mElementKeyboards.get(elementState);
final boolean hasShortcutKey = mVoiceKeyEnabled && (isSymbols != mVoiceKeyOnMain); final boolean hasShortcutKey = params.mVoiceKeyEnabled
return new KeyboardId(xmlId, elementState, mLocale, mConf.orientation, && (isSymbols != params.mVoiceKeyOnMain);
mMetrics.widthPixels, mMode, mEditorInfo, mSettingsKeyEnabled, mNoSettingsKey, return new KeyboardId(xmlId, elementState, params.mLocale, params.mOrientation,
mVoiceKeyEnabled, hasShortcutKey); params.mWidth, params.mMode, params.mInputTypes, params.mImeOptions,
params.mSettingsKeyEnabled, params.mNoSettingsKey, params.mVoiceKeyEnabled,
hasShortcutKey);
} }
private static int getElementState(int mode, boolean isSymbols, boolean isShift) { private static int getElementState(int mode, boolean isSymbols, boolean isShift) {
@ -198,7 +209,7 @@ public class KeyboardSet {
R.styleable.KeyboardSet_Element_elementName, 0); R.styleable.KeyboardSet_Element_elementName, 0);
final int elementKeyboard = a.getResourceId( final int elementKeyboard = a.getResourceId(
R.styleable.KeyboardSet_Element_elementKeyboard, 0); R.styleable.KeyboardSet_Element_elementKeyboard, 0);
mElementKeyboards.put(elementName, elementKeyboard); mParams.mElementKeyboards.put(elementName, elementKeyboard);
} finally { } finally {
a.recycle(); a.recycle();
} }
@ -208,7 +219,8 @@ public class KeyboardSet {
public static String parseKeyboardLocale(Resources res, int resId) public static String parseKeyboardLocale(Resources res, int resId)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
final XmlPullParser parser = res.getXml(resId); final XmlPullParser parser = res.getXml(resId);
if (parser == null) return ""; if (parser == null)
return "";
int event; int event;
while ((event = parser.next()) != XmlPullParser.END_DOCUMENT) { while ((event = parser.next()) != XmlPullParser.END_DOCUMENT) {
if (event == XmlPullParser.START_TAG) { if (event == XmlPullParser.START_TAG) {