InputMethodSubtype.java revision 7265d9bd6d80c5bedaa6de2b80f6619a301a07c8
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.os.Parcel;
21import android.os.Parcelable;
22
23import java.util.ArrayList;
24import java.util.Arrays;
25import java.util.HashSet;
26import java.util.List;
27
28/**
29 * This class is used to specify meta information of a subtype contained in an input method.
30 * Subtype can describe locale (e.g. en_US, fr_FR...) and mode (e.g. voice, keyboard...), and is
31 * used for IME switch and settings. The input method subtype allows the system to bring up the
32 * specified subtype of the designated input method directly.
33 */
34public final class InputMethodSubtype implements Parcelable {
35    private final int mSubtypeNameResId;
36    private final int mSubtypeIconResId;
37    private final String mSubtypeLocale;
38    private final String mSubtypeMode;
39    private final String mSubtypeExtraValue;
40    private final int mSubtypeHashCode;
41
42    /**
43     * Constructor
44     * @param nameId The name of the subtype
45     * @param iconId The icon of the subtype
46     * @param locale The locale supported by the subtype
47     * @param modeId The mode supported by the subtype
48     * @param extraValue The extra value of the subtype
49     */
50    InputMethodSubtype(int nameId, int iconId, String locale, String mode, String extraValue) {
51        mSubtypeNameResId = nameId;
52        mSubtypeIconResId = iconId;
53        mSubtypeLocale = locale != null ? locale : "";
54        mSubtypeMode = mode != null ? mode : "";
55        mSubtypeExtraValue = extraValue != null ? extraValue : "";
56        mSubtypeHashCode = hashCodeInternal(mSubtypeNameResId, mSubtypeIconResId, mSubtypeLocale,
57                mSubtypeMode, mSubtypeExtraValue);
58    }
59
60    InputMethodSubtype(Parcel source) {
61        String s;
62        mSubtypeNameResId = source.readInt();
63        mSubtypeIconResId = source.readInt();
64        s = source.readString();
65        mSubtypeLocale = s != null ? s : "";
66        s = source.readString();
67        mSubtypeMode = s != null ? s : "";
68        s = source.readString();
69        mSubtypeExtraValue = s != null ? s : "";
70        mSubtypeHashCode = hashCodeInternal(mSubtypeNameResId, mSubtypeIconResId, mSubtypeLocale,
71                mSubtypeMode, mSubtypeExtraValue);
72    }
73
74    /**
75     * @return the name of the subtype
76     */
77    public int getNameResId() {
78        return mSubtypeNameResId;
79    }
80
81    /**
82     * @return the icon of the subtype
83     */
84    public int getIconResId() {
85        return mSubtypeIconResId;
86    }
87
88    /**
89     * @return the locale of the subtype
90     */
91    public String getLocale() {
92        return mSubtypeLocale;
93    }
94
95    /**
96     * @return the mode of the subtype
97     */
98    public String getMode() {
99        return mSubtypeMode;
100    }
101
102    /**
103     * @return the extra value of the subtype
104     */
105    public String getExtraValue() {
106        return mSubtypeExtraValue;
107    }
108
109    @Override
110    public int hashCode() {
111        return mSubtypeHashCode;
112    }
113
114    @Override
115    public boolean equals(Object o) {
116        if (o instanceof InputMethodSubtype) {
117            InputMethodSubtype subtype = (InputMethodSubtype) o;
118            return (subtype.hashCode() == hashCode())
119                && (subtype.getNameResId() == getNameResId())
120                && (subtype.getMode().equals(getMode()))
121                && (subtype.getIconResId() == getIconResId())
122                && (subtype.getLocale().equals(getLocale()))
123                && (subtype.getExtraValue().equals(getExtraValue()));
124        }
125        return false;
126    }
127
128    public int describeContents() {
129        return 0;
130    }
131
132    public void writeToParcel(Parcel dest, int parcelableFlags) {
133        dest.writeInt(mSubtypeNameResId);
134        dest.writeInt(mSubtypeIconResId);
135        dest.writeString(mSubtypeLocale);
136        dest.writeString(mSubtypeMode);
137        dest.writeString(mSubtypeExtraValue);
138    }
139
140    public static final Parcelable.Creator<InputMethodSubtype> CREATOR
141            = new Parcelable.Creator<InputMethodSubtype>() {
142        public InputMethodSubtype createFromParcel(Parcel source) {
143            return new InputMethodSubtype(source);
144        }
145
146        public InputMethodSubtype[] newArray(int size) {
147            return new InputMethodSubtype[size];
148        }
149    };
150
151    private static int hashCodeInternal(int nameResId, int iconResId, String locale,
152            String mode, String extraValue) {
153        return Arrays.hashCode(new Object[] {nameResId, iconResId, locale, mode, extraValue});
154    }
155
156    /**
157     * Sort the list of InputMethodSubtype
158     * @param context Context will be used for getting localized strings from IME
159     * @param flags Flags for the sort order
160     * @param imi InputMethodInfo of which subtypes are subject to be sorted
161     * @param subtypeList List of InputMethodSubtype which will be sorted
162     * @return Sorted list of subtypes
163     * @hide
164     */
165    public static List<InputMethodSubtype> sort(Context context, int flags, InputMethodInfo imi,
166            List<InputMethodSubtype> subtypeList) {
167        if (imi == null) return subtypeList;
168        final HashSet<InputMethodSubtype> inputSubtypesSet = new HashSet<InputMethodSubtype>(
169                subtypeList);
170        final ArrayList<InputMethodSubtype> sortedList = new ArrayList<InputMethodSubtype>();
171        int N = imi.getSubtypeCount();
172        for (int i = 0; i < N; ++i) {
173            InputMethodSubtype subtype = imi.getSubtypeAt(i);
174            if (inputSubtypesSet.contains(subtype)) {
175                sortedList.add(subtype);
176                inputSubtypesSet.remove(subtype);
177            }
178        }
179        // If subtypes in inputSubtypesSet remain, that means these subtypes are not
180        // contained in imi, so the remaining subtypes will be appended.
181        for (InputMethodSubtype subtype: inputSubtypesSet) {
182            sortedList.add(subtype);
183        }
184        return sortedList;
185    }
186}
187