FileDocCategorySizeDatePackage
InputMethodSubtype.javaAPI DocAndroid 5.1 API25726Thu Mar 12 22:22:10 GMT 2015android.view.inputmethod

InputMethodSubtype

public final class InputMethodSubtype extends Object implements android.os.Parcelable
This class is used to specify meta information of a subtype contained in an input method editor (IME). Subtype can describe locale (e.g. en_US, fr_FR...) and mode (e.g. voice, keyboard...), and is used for IME switch and settings. The input method subtype allows the system to bring up the specified subtype of the designated IME directly.

It should be defined in an XML resource file of the input method with the <subtype> element, which resides within an {@code <input-method>} element. For more information, see the guide to Creating an Input Method.

see
InputMethodInfo
attr
ref android.R.styleable#InputMethod_Subtype_label
attr
ref android.R.styleable#InputMethod_Subtype_icon
attr
ref android.R.styleable#InputMethod_Subtype_imeSubtypeLocale
attr
ref android.R.styleable#InputMethod_Subtype_imeSubtypeMode
attr
ref android.R.styleable#InputMethod_Subtype_imeSubtypeExtraValue
attr
ref android.R.styleable#InputMethod_Subtype_isAuxiliary
attr
ref android.R.styleable#InputMethod_Subtype_overridesImplicitlyEnabledSubtype
attr
ref android.R.styleable#InputMethod_Subtype_subtypeId
attr
ref android.R.styleable#InputMethod_Subtype_isAsciiCapable

Fields Summary
private static final String
TAG
private static final String
EXTRA_VALUE_PAIR_SEPARATOR
private static final String
EXTRA_VALUE_KEY_VALUE_SEPARATOR
private static final String
EXTRA_KEY_UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME
private final boolean
mIsAuxiliary
private final boolean
mOverridesImplicitlyEnabledSubtype
private final boolean
mIsAsciiCapable
private final int
mSubtypeHashCode
private final int
mSubtypeIconResId
private final int
mSubtypeNameResId
private final int
mSubtypeId
private final String
mSubtypeLocale
private final String
mSubtypeMode
private final String
mSubtypeExtraValue
private volatile HashMap
mExtraValueHashMapCache
public static final Parcelable.Creator
CREATOR
Constructors Summary
public InputMethodSubtype(int nameId, int iconId, String locale, String mode, String extraValue, boolean isAuxiliary, boolean overridesImplicitlyEnabledSubtype)
Constructor with no subtype ID specified.

deprecated
use {@link InputMethodSubtypeBuilder} instead. Arguments for this constructor have the same meanings as {@link InputMethodSubtype#InputMethodSubtype(int, int, String, String, String, boolean, boolean, int)} except "id".

        this(nameId, iconId, locale, mode, extraValue, isAuxiliary,
                overridesImplicitlyEnabledSubtype, 0);
    
public InputMethodSubtype(int nameId, int iconId, String locale, String mode, String extraValue, boolean isAuxiliary, boolean overridesImplicitlyEnabledSubtype, int id)
Constructor.

deprecated
use {@link InputMethodSubtypeBuilder} instead. "isAsciiCapable" is "false" in this constructor.
param
nameId Resource ID of the subtype name string. The string resource may have exactly one %s in it. If there is, the %s part will be replaced with the locale's display name by the formatter. Please refer to {@link #getDisplayName} for details.
param
iconId Resource ID of the subtype icon drawable.
param
locale The locale supported by the subtype
param
mode The mode supported by the subtype
param
extraValue The extra value of the subtype. This string is free-form, but the API supplies tools to deal with a key-value comma-separated list; see {@link #containsExtraValueKey} and {@link #getExtraValueOf}.
param
isAuxiliary true when this subtype is auxiliary, false otherwise. An auxiliary subtype will not be shown in the list of enabled IMEs for choosing the current IME in the Settings even when this subtype is enabled. Please note that this subtype will still be shown in the list of IMEs in the IME switcher to allow the user to tentatively switch to this subtype while an IME is shown. The framework will never switch the current IME to this subtype by {@link android.view.inputmethod.InputMethodManager#switchToLastInputMethod}. The intent of having this flag is to allow for IMEs that are invoked in a one-shot way as auxiliary input mode, and return to the previous IME once it is finished (e.g. voice input).
param
overridesImplicitlyEnabledSubtype true when this subtype should be enabled by default if no other subtypes in the IME are enabled explicitly. Note that a subtype with this parameter being true will not be shown in the list of subtypes in each IME's subtype enabler. Having an "automatic" subtype is an example use of this flag.
param
id The unique ID for the subtype. The input method framework keeps track of enabled subtypes by ID. When the IME package gets upgraded, enabled IDs will stay enabled even if other attributes are different. If the ID is unspecified or 0, Arrays.hashCode(new Object[] {locale, mode, extraValue, isAuxiliary, overridesImplicitlyEnabledSubtype}) will be used instead.

        this(getBuilder(nameId, iconId, locale, mode, extraValue, isAuxiliary,
                overridesImplicitlyEnabledSubtype, id, false));
    
private InputMethodSubtype(InputMethodSubtypeBuilder builder)
Constructor.

param
builder Builder for InputMethodSubtype

        mSubtypeNameResId = builder.mSubtypeNameResId;
        mSubtypeIconResId = builder.mSubtypeIconResId;
        mSubtypeLocale = builder.mSubtypeLocale;
        mSubtypeMode = builder.mSubtypeMode;
        mSubtypeExtraValue = builder.mSubtypeExtraValue;
        mIsAuxiliary = builder.mIsAuxiliary;
        mOverridesImplicitlyEnabledSubtype = builder.mOverridesImplicitlyEnabledSubtype;
        mSubtypeId = builder.mSubtypeId;
        mIsAsciiCapable = builder.mIsAsciiCapable;
        // If hashCode() of this subtype is 0 and you want to specify it as an id of this subtype,
        // just specify 0 as this subtype's id. Then, this subtype's id is treated as 0.
        mSubtypeHashCode = mSubtypeId != 0 ? mSubtypeId : hashCodeInternal(mSubtypeLocale,
                mSubtypeMode, mSubtypeExtraValue, mIsAuxiliary, mOverridesImplicitlyEnabledSubtype,
                mIsAsciiCapable);
    
InputMethodSubtype(android.os.Parcel source)

        String s;
        mSubtypeNameResId = source.readInt();
        mSubtypeIconResId = source.readInt();
        s = source.readString();
        mSubtypeLocale = s != null ? s : "";
        s = source.readString();
        mSubtypeMode = s != null ? s : "";
        s = source.readString();
        mSubtypeExtraValue = s != null ? s : "";
        mIsAuxiliary = (source.readInt() == 1);
        mOverridesImplicitlyEnabledSubtype = (source.readInt() == 1);
        mSubtypeHashCode = source.readInt();
        mSubtypeId = source.readInt();
        mIsAsciiCapable = (source.readInt() == 1);
    
Methods Summary
private static java.util.LocaleconstructLocaleFromString(java.lang.String localeStr)


         
        if (TextUtils.isEmpty(localeStr))
            return null;
        String[] localeParams = localeStr.split("_", 3);
        // The length of localeStr is guaranteed to always return a 1 <= value <= 3
        // because localeStr is not empty.
        if (localeParams.length == 1) {
            return new Locale(localeParams[0]);
        } else if (localeParams.length == 2) {
            return new Locale(localeParams[0], localeParams[1]);
        } else if (localeParams.length == 3) {
            return new Locale(localeParams[0], localeParams[1], localeParams[2]);
        }
        return null;
    
public booleancontainsExtraValueKey(java.lang.String key)
The string of ExtraValue in subtype should be defined as follows: example: key0,key1=value1,key2,key3,key4=value4

param
key The key of extra value
return
The subtype contains specified the extra value

        return getExtraValueHashMap().containsKey(key);
    
public intdescribeContents()

        return 0;
    
public booleanequals(java.lang.Object o)

        if (o instanceof InputMethodSubtype) {
            InputMethodSubtype subtype = (InputMethodSubtype) o;
            if (subtype.mSubtypeId != 0 || mSubtypeId != 0) {
                return (subtype.hashCode() == hashCode());
            }
            return (subtype.hashCode() == hashCode())
                && (subtype.getLocale().equals(getLocale()))
                && (subtype.getMode().equals(getMode()))
                && (subtype.getExtraValue().equals(getExtraValue()))
                && (subtype.isAuxiliary() == isAuxiliary())
                && (subtype.overridesImplicitlyEnabledSubtype()
                        == overridesImplicitlyEnabledSubtype())
                && (subtype.isAsciiCapable() == isAsciiCapable());
        }
        return false;
    
private static android.view.inputmethod.InputMethodSubtype$InputMethodSubtypeBuildergetBuilder(int nameId, int iconId, java.lang.String locale, java.lang.String mode, java.lang.String extraValue, boolean isAuxiliary, boolean overridesImplicitlyEnabledSubtype, int id, boolean isAsciiCapable)

         final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder();
         builder.mSubtypeNameResId = nameId;
         builder.mSubtypeIconResId = iconId;
         builder.mSubtypeLocale = locale;
         builder.mSubtypeMode = mode;
         builder.mSubtypeExtraValue = extraValue;
         builder.mIsAuxiliary = isAuxiliary;
         builder.mOverridesImplicitlyEnabledSubtype = overridesImplicitlyEnabledSubtype;
         builder.mSubtypeId = id;
         builder.mIsAsciiCapable = isAsciiCapable;
         return builder;
     
public java.lang.CharSequencegetDisplayName(android.content.Context context, java.lang.String packageName, android.content.pm.ApplicationInfo appInfo)

param
context Context will be used for getting Locale and PackageManager.
param
packageName The package name of the IME
param
appInfo The application info of the IME
return
a display name for this subtype. The string resource of the label (mSubtypeNameResId) may have exactly one %s in it. If there is, the %s part will be replaced with the locale's display name by the formatter. If there is not, this method returns the string specified by mSubtypeNameResId. If mSubtypeNameResId is not specified (== 0), it's up to the framework to generate an appropriate display name.

        final Locale locale = constructLocaleFromString(mSubtypeLocale);
        final String localeStr = locale != null ? locale.getDisplayName() : mSubtypeLocale;
        if (mSubtypeNameResId == 0) {
            return localeStr;
        }
        final CharSequence subtypeName = context.getPackageManager().getText(
                packageName, mSubtypeNameResId, appInfo);
        if (!TextUtils.isEmpty(subtypeName)) {
            final String replacementString =
                    containsExtraValueKey(EXTRA_KEY_UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME)
                            ? getExtraValueOf(EXTRA_KEY_UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME)
                            : localeStr;
            try {
                return String.format(
                        subtypeName.toString(), replacementString != null ? replacementString : "");
            } catch (IllegalFormatException e) {
                Slog.w(TAG, "Found illegal format in subtype name("+ subtypeName + "): " + e);
                return "";
            }
        } else {
            return localeStr;
        }
    
public java.lang.StringgetExtraValue()

return
The extra value of the subtype.

        return mSubtypeExtraValue;
    
private java.util.HashMapgetExtraValueHashMap()

        if (mExtraValueHashMapCache == null) {
            synchronized(this) {
                if (mExtraValueHashMapCache == null) {
                    mExtraValueHashMapCache = new HashMap<String, String>();
                    final String[] pairs = mSubtypeExtraValue.split(EXTRA_VALUE_PAIR_SEPARATOR);
                    final int N = pairs.length;
                    for (int i = 0; i < N; ++i) {
                        final String[] pair = pairs[i].split(EXTRA_VALUE_KEY_VALUE_SEPARATOR);
                        if (pair.length == 1) {
                            mExtraValueHashMapCache.put(pair[0], null);
                        } else if (pair.length > 1) {
                            if (pair.length > 2) {
                                Slog.w(TAG, "ExtraValue has two or more '='s");
                            }
                            mExtraValueHashMapCache.put(pair[0], pair[1]);
                        }
                    }
                }
            }
        }
        return mExtraValueHashMapCache;
    
public java.lang.StringgetExtraValueOf(java.lang.String key)
The string of ExtraValue in subtype should be defined as follows: example: key0,key1=value1,key2,key3,key4=value4

param
key The key of extra value
return
The value of the specified key

        return getExtraValueHashMap().get(key);
    
public intgetIconResId()

return
Resource ID of the subtype icon drawable.

        return mSubtypeIconResId;
    
public java.lang.StringgetLocale()

return
The locale of the subtype. This method returns the "locale" string parameter passed to the constructor.

        return mSubtypeLocale;
    
public java.lang.StringgetMode()

return
The mode of the subtype.

        return mSubtypeMode;
    
public intgetNameResId()

return
Resource ID of the subtype name string.

        return mSubtypeNameResId;
    
public inthashCode()

        return mSubtypeHashCode;
    
private static inthashCodeInternal(java.lang.String locale, java.lang.String mode, java.lang.String extraValue, boolean isAuxiliary, boolean overridesImplicitlyEnabledSubtype, boolean isAsciiCapable)

        // CAVEAT: Must revisit how to compute needsToCalculateCompatibleHashCode when a new
        // attribute is added in order to avoid enabled subtypes being unexpectedly disabled.
        final boolean needsToCalculateCompatibleHashCode = !isAsciiCapable;
        if (needsToCalculateCompatibleHashCode) {
            return Arrays.hashCode(new Object[] {locale, mode, extraValue, isAuxiliary,
                    overridesImplicitlyEnabledSubtype});
        }
        return Arrays.hashCode(new Object[] {locale, mode, extraValue, isAuxiliary,
                overridesImplicitlyEnabledSubtype, isAsciiCapable});
    
public booleanisAsciiCapable()

return
true if this subtype is Ascii capable, false otherwise. If the subtype is ASCII capable, it should guarantee that the user can input ASCII characters with this subtype. This is important because many password fields only allow ASCII-characters.

        return mIsAsciiCapable;
    
public booleanisAuxiliary()

return
true if this subtype is auxiliary, false otherwise. An auxiliary subtype will not be shown in the list of enabled IMEs for choosing the current IME in the Settings even when this subtype is enabled. Please note that this subtype will still be shown in the list of IMEs in the IME switcher to allow the user to tentatively switch to this subtype while an IME is shown. The framework will never switch the current IME to this subtype by {@link android.view.inputmethod.InputMethodManager#switchToLastInputMethod}. The intent of having this flag is to allow for IMEs that are invoked in a one-shot way as auxiliary input mode, and return to the previous IME once it is finished (e.g. voice input).

        return mIsAuxiliary;
    
public booleanoverridesImplicitlyEnabledSubtype()

return
true when this subtype will be enabled by default if no other subtypes in the IME are enabled explicitly, false otherwise. Note that a subtype with this method returning true will not be shown in the list of subtypes in each IME's subtype enabler. Having an "automatic" subtype is an example use of this flag.

        return mOverridesImplicitlyEnabledSubtype;
    
public static java.util.Listsort(android.content.Context context, int flags, InputMethodInfo imi, java.util.List subtypeList)
Sort the list of InputMethodSubtype

param
context Context will be used for getting localized strings from IME
param
flags Flags for the sort order
param
imi InputMethodInfo of which subtypes are subject to be sorted
param
subtypeList List of InputMethodSubtype which will be sorted
return
Sorted list of subtypes
hide

        if (imi == null) return subtypeList;
        final HashSet<InputMethodSubtype> inputSubtypesSet = new HashSet<InputMethodSubtype>(
                subtypeList);
        final ArrayList<InputMethodSubtype> sortedList = new ArrayList<InputMethodSubtype>();
        int N = imi.getSubtypeCount();
        for (int i = 0; i < N; ++i) {
            InputMethodSubtype subtype = imi.getSubtypeAt(i);
            if (inputSubtypesSet.contains(subtype)) {
                sortedList.add(subtype);
                inputSubtypesSet.remove(subtype);
            }
        }
        // If subtypes in inputSubtypesSet remain, that means these subtypes are not
        // contained in imi, so the remaining subtypes will be appended.
        for (InputMethodSubtype subtype: inputSubtypesSet) {
            sortedList.add(subtype);
        }
        return sortedList;
    
public voidwriteToParcel(android.os.Parcel dest, int parcelableFlags)

        dest.writeInt(mSubtypeNameResId);
        dest.writeInt(mSubtypeIconResId);
        dest.writeString(mSubtypeLocale);
        dest.writeString(mSubtypeMode);
        dest.writeString(mSubtypeExtraValue);
        dest.writeInt(mIsAuxiliary ? 1 : 0);
        dest.writeInt(mOverridesImplicitlyEnabledSubtype ? 1 : 0);
        dest.writeInt(mSubtypeHashCode);
        dest.writeInt(mSubtypeId);
        dest.writeInt(mIsAsciiCapable ? 1 : 0);