Merge "[ML24] Add facilities to read the script of a subtype"

main
Jean Chalard 2014-10-14 11:26:07 +00:00 committed by Android (Google) Code Review
commit 10ac940312
4 changed files with 75 additions and 22 deletions

View File

@ -18,6 +18,7 @@ package com.android.inputmethod.latin.settings;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
@ -48,7 +49,8 @@ public class AdditionalFeaturesSettingUtils {
public static RichInputMethodSubtype createRichInputMethodSubtype( public static RichInputMethodSubtype createRichInputMethodSubtype(
@Nonnull final RichInputMethodManager imm, @Nonnull final RichInputMethodManager imm,
@Nonnull final InputMethodSubtype subtype) { @Nonnull final InputMethodSubtype subtype,
final Resources resources) {
return new RichInputMethodSubtype(subtype); return new RichInputMethodSubtype(subtype);
} }
} }

View File

@ -28,6 +28,7 @@ import android.util.Log;
import android.util.SparseArray; import android.util.SparseArray;
import android.util.Xml; import android.util.Xml;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.compat.EditorInfoCompatUtils; import com.android.inputmethod.compat.EditorInfoCompatUtils;
import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils; import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils;
@ -39,6 +40,7 @@ import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.RichInputMethodSubtype; import com.android.inputmethod.latin.RichInputMethodSubtype;
import com.android.inputmethod.latin.SubtypeSwitcher; import com.android.inputmethod.latin.SubtypeSwitcher;
import com.android.inputmethod.latin.define.DebugFlags; import com.android.inputmethod.latin.define.DebugFlags;
import com.android.inputmethod.latin.utils.DebugLogUtils;
import com.android.inputmethod.latin.utils.InputTypeUtils; import com.android.inputmethod.latin.utils.InputTypeUtils;
import com.android.inputmethod.latin.utils.ScriptUtils; import com.android.inputmethod.latin.utils.ScriptUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
@ -313,23 +315,78 @@ public final class KeyboardLayoutSet {
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.
private static int readScriptId(final Resources resources,
final InputMethodSubtype subtype) {
final String layoutSetName = KEYBOARD_LAYOUT_SET_RESOURCE_PREFIX
+ SubtypeLocaleUtils.getKeyboardLayoutSetName(subtype);
final int xmlId = getXmlId(resources, layoutSetName);
final XmlResourceParser parser = resources.getXml(xmlId);
try {
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
// Bovinate through the XML stupidly searching for TAG_FEATURE, and read
// the script Id from it.
parser.next();
final String tag = parser.getName();
if (TAG_FEATURE.equals(tag)) {
return readScriptIdFromTagFeature(resources, parser);
}
}
} catch (final IOException | XmlPullParserException e) {
throw new RuntimeException(e.getMessage() + " in " + layoutSetName, e);
} finally {
parser.close();
}
// If the tag is not found, then the default script is Latin.
return ScriptUtils.SCRIPT_LATIN;
}
private static int readScriptIdFromTagFeature(final Resources resources,
final XmlPullParser parser) throws IOException, XmlPullParserException {
final TypedArray featureAttr = resources.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.KeyboardLayoutSet_Feature);
try {
final int scriptId =
featureAttr.getInt(R.styleable.KeyboardLayoutSet_Feature_supportedScript,
ScriptUtils.SCRIPT_UNKNOWN);
XmlParseUtils.checkEndTag(TAG_FEATURE, parser);
return scriptId;
} finally {
featureAttr.recycle();
}
}
public KeyboardLayoutSet build() { public KeyboardLayoutSet build() {
if (mParams.mSubtype == null) if (mParams.mSubtype == null)
throw new RuntimeException("KeyboardLayoutSet subtype is not specified"); throw new RuntimeException("KeyboardLayoutSet subtype is not specified");
final String packageName = mResources.getResourcePackageName( final int xmlId = getXmlId(mResources, mParams.mKeyboardLayoutSetName);
R.xml.keyboard_layout_set_qwerty);
final String keyboardLayoutSetName = mParams.mKeyboardLayoutSetName;
final int xmlId = mResources.getIdentifier(keyboardLayoutSetName, "xml", packageName);
try { try {
parseKeyboardLayoutSet(mResources, xmlId); parseKeyboardLayoutSet(mResources, xmlId);
} catch (final IOException e) { } catch (final IOException | XmlPullParserException e) {
throw new RuntimeException(e.getMessage() + " in " + keyboardLayoutSetName, e); throw new RuntimeException(e.getMessage() + " in " + mParams.mKeyboardLayoutSetName,
} catch (final XmlPullParserException e) { e);
throw new RuntimeException(e.getMessage() + " in " + keyboardLayoutSetName, e);
} }
return new KeyboardLayoutSet(mContext, mParams); return new KeyboardLayoutSet(mContext, mParams);
} }
private static int getXmlId(final Resources resources, final String keyboardLayoutSetName) {
final String packageName = resources.getResourcePackageName(
R.xml.keyboard_layout_set_qwerty);
return resources.getIdentifier(keyboardLayoutSetName, "xml", packageName);
}
private void parseKeyboardLayoutSet(final Resources res, final int resId) private void parseKeyboardLayoutSet(final Resources res, final int resId)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
final XmlResourceParser parser = res.getXml(resId); final XmlResourceParser parser = res.getXml(resId);
@ -407,17 +464,8 @@ public final class KeyboardLayoutSet {
private void parseKeyboardLayoutSetFeature(final XmlPullParser parser) private void parseKeyboardLayoutSetFeature(final XmlPullParser parser)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser), final int scriptId = readScriptIdFromTagFeature(mResources, parser);
R.styleable.KeyboardLayoutSet_Feature);
try {
final int scriptId = a.getInt(
R.styleable.KeyboardLayoutSet_Feature_supportedScript,
ScriptUtils.SCRIPT_LATIN);
XmlParseUtils.checkEndTag(TAG_FEATURE, parser);
setScriptId(scriptId); setScriptId(scriptId);
} finally {
a.recycle();
}
} }
private static int getKeyboardMode(final EditorInfo editorInfo) { private static int getKeyboardMode(final EditorInfo editorInfo) {

View File

@ -20,6 +20,7 @@ import static com.android.inputmethod.latin.Constants.Subtype.KEYBOARD_MODE;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -51,6 +52,7 @@ public class RichInputMethodManager {
private static final RichInputMethodManager sInstance = new RichInputMethodManager(); private static final RichInputMethodManager sInstance = new RichInputMethodManager();
private Resources mResources;
private InputMethodManagerCompatWrapper mImmWrapper; private InputMethodManagerCompatWrapper mImmWrapper;
private InputMethodInfoCache mInputMethodInfoCache; private InputMethodInfoCache mInputMethodInfoCache;
final HashMap<InputMethodInfo, List<InputMethodSubtype>> final HashMap<InputMethodInfo, List<InputMethodSubtype>>
@ -84,6 +86,7 @@ public class RichInputMethodManager {
return; return;
} }
mImmWrapper = new InputMethodManagerCompatWrapper(context); mImmWrapper = new InputMethodManagerCompatWrapper(context);
mResources = context.getResources();
mInputMethodInfoCache = new InputMethodInfoCache( mInputMethodInfoCache = new InputMethodInfoCache(
mImmWrapper.mImm, context.getPackageName()); mImmWrapper.mImm, context.getPackageName());
@ -305,7 +308,8 @@ public class RichInputMethodManager {
public RichInputMethodSubtype createCurrentRichInputMethodSubtype( public RichInputMethodSubtype createCurrentRichInputMethodSubtype(
final InputMethodSubtype rawSubtype) { final InputMethodSubtype rawSubtype) {
return AdditionalFeaturesSettingUtils.createRichInputMethodSubtype(this, rawSubtype); return AdditionalFeaturesSettingUtils.createRichInputMethodSubtype(this, rawSubtype,
mResources);
} }
public boolean hasMultipleEnabledIMEsOrSubtypes(final boolean shouldIncludeAuxiliarySubtypes) { public boolean hasMultipleEnabledIMEsOrSubtypes(final boolean shouldIncludeAuxiliarySubtypes) {

View File

@ -36,7 +36,6 @@ import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils;
import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper; import com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper;
import com.android.inputmethod.latin.define.DebugFlags; import com.android.inputmethod.latin.define.DebugFlags;
import com.android.inputmethod.latin.utils.LocaleUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import java.util.HashSet; import java.util.HashSet;