InputMethodSubtype.java revision a9778d4d442db65344e32318b1fd43ab54898389
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view.inputmethod;
18
19import android.content.Context;
20import android.content.pm.ApplicationInfo;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.text.TextUtils;
24import android.util.Slog;
25
26import java.util.ArrayList;
27import java.util.Arrays;
28import java.util.HashMap;
29import java.util.HashSet;
30import java.util.List;
31import java.util.Locale;
32
33/**
34 * This class is used to specify meta information of a subtype contained in an input method.
35 * Subtype can describe locale (e.g. en_US, fr_FR...) and mode (e.g. voice, keyboard...), and is
36 * used for IME switch and settings. The input method subtype allows the system to bring up the
37 * specified subtype of the designated input method directly.
38 */
39public final class InputMethodSubtype implements Parcelable {
40    private static final String TAG = InputMethodSubtype.class.getSimpleName();
41    private static final String EXTRA_VALUE_PAIR_SEPARATOR = ",";
42    private static final String EXTRA_VALUE_KEY_VALUE_SEPARATOR = "=";
43
44    private final boolean mIsAuxiliary;
45    private final int mSubtypeHashCode;
46    private final int mSubtypeIconResId;
47    private final int mSubtypeNameResId;
48    private final String mSubtypeLocale;
49    private final String mSubtypeMode;
50    private final String mSubtypeExtraValue;
51    private HashMap<String, String> mExtraValueHashMapCache;
52
53    /**
54     * Constructor
55     * @param nameId The name of the subtype
56     * @param iconId The icon of the subtype
57     * @param locale The locale supported by the subtype
58     * @param mode The mode supported by the subtype
59     * @param extraValue The extra value of the subtype
60     */
61    public InputMethodSubtype(
62            int nameId, int iconId, String locale, String mode, String extraValue) {
63        this(nameId, iconId, locale, mode, extraValue, false);
64    }
65
66    /**
67     * Constructor
68     * @param nameId The name of the subtype
69     * @param iconId The icon of the subtype
70     * @param locale The locale supported by the subtype
71     * @param mode The mode supported by the subtype
72     * @param extraValue The extra value of the subtype
73     * @param isAuxiliary true when this subtype is one shot subtype.
74     */
75    public InputMethodSubtype(int nameId, int iconId, String locale, String mode, String extraValue,
76            boolean isAuxiliary) {
77        mSubtypeNameResId = nameId;
78        mSubtypeIconResId = iconId;
79        mSubtypeLocale = locale != null ? locale : "";
80        mSubtypeMode = mode != null ? mode : "";
81        mSubtypeExtraValue = extraValue != null ? extraValue : "";
82        mIsAuxiliary = isAuxiliary;
83        mSubtypeHashCode = hashCodeInternal(mSubtypeLocale, mSubtypeMode, mSubtypeExtraValue,
84                mIsAuxiliary);
85    }
86
87    InputMethodSubtype(Parcel source) {
88        String s;
89        mSubtypeNameResId = source.readInt();
90        mSubtypeIconResId = source.readInt();
91        s = source.readString();
92        mSubtypeLocale = s != null ? s : "";
93        s = source.readString();
94        mSubtypeMode = s != null ? s : "";
95        s = source.readString();
96        mSubtypeExtraValue = s != null ? s : "";
97        mIsAuxiliary = (source.readInt() == 1);
98        mSubtypeHashCode = hashCodeInternal(mSubtypeLocale, mSubtypeMode, mSubtypeExtraValue,
99                mIsAuxiliary);
100    }
101
102    /**
103     * @return the name of the subtype
104     */
105    public int getNameResId() {
106        return mSubtypeNameResId;
107    }
108
109    /**
110     * @return the icon of the subtype
111     */
112    public int getIconResId() {
113        return mSubtypeIconResId;
114    }
115
116    /**
117     * @return the locale of the subtype
118     */
119    public String getLocale() {
120        return mSubtypeLocale;
121    }
122
123    /**
124     * @return the mode of the subtype
125     */
126    public String getMode() {
127        return mSubtypeMode;
128    }
129
130    /**
131     * @return the extra value of the subtype
132     */
133    public String getExtraValue() {
134        return mSubtypeExtraValue;
135    }
136
137    /**
138     * @return true if this subtype is one shot subtype. One shot subtype will not be shown in the
139     * ime switch list when this subtype is implicitly enabled. The framework will never
140     * switch the current ime to this subtype by switchToLastInputMethod in InputMethodManager.
141     */
142    public boolean isAuxiliary() {
143        return mIsAuxiliary;
144    }
145
146    /**
147     * @param context Context will be used for getting Locale and PackageManager.
148     * @param packageName The package name of the IME
149     * @param appInfo The application info of the IME
150     * @return a display name for this subtype. The string resource of the label (mSubtypeNameResId)
151     * can have only one %s in it. If there is, the %s part will be replaced with the locale's
152     * display name by the formatter. If there is not, this method simply returns the string
153     * specified by mSubtypeNameResId. If mSubtypeNameResId is not specified (== 0), it's up to the
154     * framework to generate an appropriate display name.
155     */
156    public CharSequence getDisplayName(
157            Context context, String packageName, ApplicationInfo appInfo) {
158        final Locale locale = constructLocaleFromString(mSubtypeLocale);
159        final String localeStr = locale != null ? locale.getDisplayName() : mSubtypeLocale;
160        if (mSubtypeNameResId == 0) {
161            return localeStr;
162        }
163        final String subtypeName = context.getPackageManager().getText(
164                packageName, mSubtypeNameResId, appInfo).toString();
165        if (!TextUtils.isEmpty(subtypeName)) {
166            return String.format(subtypeName, localeStr);
167        } else {
168            return localeStr;
169        }
170    }
171
172    private HashMap<String, String> getExtraValueHashMap() {
173        if (mExtraValueHashMapCache == null) {
174            mExtraValueHashMapCache = new HashMap<String, String>();
175            final String[] pairs = mSubtypeExtraValue.split(EXTRA_VALUE_PAIR_SEPARATOR);
176            final int N = pairs.length;
177            for (int i = 0; i < N; ++i) {
178                final String[] pair = pairs[i].split(EXTRA_VALUE_KEY_VALUE_SEPARATOR);
179                if (pair.length == 1) {
180                    mExtraValueHashMapCache.put(pair[0], null);
181                } else if (pair.length > 1) {
182                    if (pair.length > 2) {
183                        Slog.w(TAG, "ExtraValue has two or more '='s");
184                    }
185                    mExtraValueHashMapCache.put(pair[0], pair[1]);
186                }
187            }
188        }
189        return mExtraValueHashMapCache;
190    }
191
192    /**
193     * The string of ExtraValue in subtype should be defined as follows:
194     * example: key0,key1=value1,key2,key3,key4=value4
195     * @param key the key of extra value
196     * @return the subtype contains specified the extra value
197     */
198    public boolean containsExtraValueKey(String key) {
199        return getExtraValueHashMap().containsKey(key);
200    }
201
202    /**
203     * The string of ExtraValue in subtype should be defined as follows:
204     * example: key0,key1=value1,key2,key3,key4=value4
205     * @param key the key of extra value
206     * @return the value of the specified key
207     */
208    public String getExtraValueOf(String key) {
209        return getExtraValueHashMap().get(key);
210    }
211
212    @Override
213    public int hashCode() {
214        return mSubtypeHashCode;
215    }
216
217    @Override
218    public boolean equals(Object o) {
219        if (o instanceof InputMethodSubtype) {
220            InputMethodSubtype subtype = (InputMethodSubtype) o;
221            return (subtype.hashCode() == hashCode())
222                && (subtype.getNameResId() == getNameResId())
223                && (subtype.getMode().equals(getMode()))
224                && (subtype.getIconResId() == getIconResId())
225                && (subtype.getLocale().equals(getLocale()))
226                && (subtype.getExtraValue().equals(getExtraValue()))
227                && (subtype.isAuxiliary() == isAuxiliary());
228        }
229        return false;
230    }
231
232    @Override
233    public int describeContents() {
234        return 0;
235    }
236
237    @Override
238    public void writeToParcel(Parcel dest, int parcelableFlags) {
239        dest.writeInt(mSubtypeNameResId);
240        dest.writeInt(mSubtypeIconResId);
241        dest.writeString(mSubtypeLocale);
242        dest.writeString(mSubtypeMode);
243        dest.writeString(mSubtypeExtraValue);
244        dest.writeInt(mIsAuxiliary ? 1 : 0);
245    }
246
247    public static final Parcelable.Creator<InputMethodSubtype> CREATOR
248            = new Parcelable.Creator<InputMethodSubtype>() {
249        @Override
250        public InputMethodSubtype createFromParcel(Parcel source) {
251            return new InputMethodSubtype(source);
252        }
253
254        @Override
255        public InputMethodSubtype[] newArray(int size) {
256            return new InputMethodSubtype[size];
257        }
258    };
259
260    private static Locale constructLocaleFromString(String localeStr) {
261        if (TextUtils.isEmpty(localeStr))
262            return null;
263        String[] localeParams = localeStr.split("_", 3);
264        // The length of localeStr is guaranteed to always return a 1 <= value <= 3
265        // because localeStr is not empty.
266        if (localeParams.length == 1) {
267            return new Locale(localeParams[0]);
268        } else if (localeParams.length == 2) {
269            return new Locale(localeParams[0], localeParams[1]);
270        } else if (localeParams.length == 3) {
271            return new Locale(localeParams[0], localeParams[1], localeParams[2]);
272        }
273        return null;
274    }
275
276    private static int hashCodeInternal(String locale, String mode, String extraValue,
277            boolean isAuxiliary) {
278        return Arrays.hashCode(new Object[] {locale, mode, extraValue, isAuxiliary});
279    }
280
281    /**
282     * Sort the list of InputMethodSubtype
283     * @param context Context will be used for getting localized strings from IME
284     * @param flags Flags for the sort order
285     * @param imi InputMethodInfo of which subtypes are subject to be sorted
286     * @param subtypeList List of InputMethodSubtype which will be sorted
287     * @return Sorted list of subtypes
288     * @hide
289     */
290    public static List<InputMethodSubtype> sort(Context context, int flags, InputMethodInfo imi,
291            List<InputMethodSubtype> subtypeList) {
292        if (imi == null) return subtypeList;
293        final HashSet<InputMethodSubtype> inputSubtypesSet = new HashSet<InputMethodSubtype>(
294                subtypeList);
295        final ArrayList<InputMethodSubtype> sortedList = new ArrayList<InputMethodSubtype>();
296        int N = imi.getSubtypeCount();
297        for (int i = 0; i < N; ++i) {
298            InputMethodSubtype subtype = imi.getSubtypeAt(i);
299            if (inputSubtypesSet.contains(subtype)) {
300                sortedList.add(subtype);
301                inputSubtypesSet.remove(subtype);
302            }
303        }
304        // If subtypes in inputSubtypesSet remain, that means these subtypes are not
305        // contained in imi, so the remaining subtypes will be appended.
306        for (InputMethodSubtype subtype: inputSubtypesSet) {
307            sortedList.add(subtype);
308        }
309        return sortedList;
310    }
311}
312