InputMethodSubtype.java revision af4bf400abab86baee44dacbcdf13444d06ee46e
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.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.Arrays;
23
24/**
25 * Information given to an {@link InputMethod} about a client connecting
26 * to it.
27 */
28/**
29 * InputMethodSubtype is a subtype contained in the input method. Subtype can describe
30 * locales (e.g. en_US, fr_FR...) and modes (e.g. voice, keyboard...), and is used for
31 * IME switch. The subtype allows the system to call the specified subtype of IME directly.
32 */
33public final class InputMethodSubtype implements Parcelable {
34    private final int mSubtypeNameResId;
35    private final int mSubtypeIconResId;
36    private final String mSubtypeLocale;
37    private final String mSubtypeMode;
38    private final String mSubtypeExtraValue;
39    private final int mSubtypeHashCode;
40
41    /**
42     * Constructor
43     * @param nameId The name of the subtype
44     * @param iconId The icon of the subtype
45     * @param locale The locale supported by the subtype
46     * @param modeId The mode supported by the subtype
47     * @param extraValue The extra value of the subtype
48     */
49    InputMethodSubtype(int nameId, int iconId, String locale, String mode, String extraValue) {
50        mSubtypeNameResId = nameId;
51        mSubtypeIconResId = iconId;
52        mSubtypeLocale = locale != null ? locale : "";
53        mSubtypeMode = mode != null ? mode : "";
54        mSubtypeExtraValue = extraValue != null ? extraValue : "";
55        mSubtypeHashCode = hashCodeInternal(mSubtypeNameResId, mSubtypeIconResId, mSubtypeLocale,
56                mSubtypeMode, mSubtypeExtraValue);
57    }
58
59    InputMethodSubtype(Parcel source) {
60        String s;
61        mSubtypeNameResId = source.readInt();
62        mSubtypeIconResId = source.readInt();
63        s = source.readString();
64        mSubtypeLocale = s != null ? s : "";
65        s = source.readString();
66        mSubtypeMode = s != null ? s : "";
67        s = source.readString();
68        mSubtypeExtraValue = s != null ? s : "";
69        mSubtypeHashCode = hashCodeInternal(mSubtypeNameResId, mSubtypeIconResId, mSubtypeLocale,
70                mSubtypeMode, mSubtypeExtraValue);
71    }
72
73    /**
74     * @return the name of the subtype
75     */
76    public int getNameResId() {
77        return mSubtypeNameResId;
78    }
79
80    /**
81     * @return the icon of the subtype
82     */
83    public int getIconResId() {
84        return mSubtypeIconResId;
85    }
86
87    /**
88     * @return the locale of the subtype
89     */
90    public String getLocale() {
91        return mSubtypeLocale;
92    }
93
94    /**
95     * @return the mode of the subtype
96     */
97    public String getMode() {
98        return mSubtypeMode;
99    }
100
101    /**
102     * @return the extra value of the subtype
103     */
104    public String getExtraValue() {
105        return mSubtypeExtraValue;
106    }
107
108    @Override
109    public int hashCode() {
110        return mSubtypeHashCode;
111    }
112
113    @Override
114    public boolean equals(Object o) {
115        if (o instanceof InputMethodSubtype) {
116            InputMethodSubtype subtype = (InputMethodSubtype) o;
117            return (subtype.hashCode() == hashCode())
118                && (subtype.getNameResId() == getNameResId())
119                && (subtype.getMode().equals(getMode()))
120                && (subtype.getIconResId() == getIconResId())
121                && (subtype.getLocale().equals(getLocale()))
122                && (subtype.getExtraValue().equals(getExtraValue()));
123        }
124        return false;
125    }
126
127    public int describeContents() {
128        return 0;
129    }
130
131    public void writeToParcel(Parcel dest, int parcelableFlags) {
132        dest.writeInt(mSubtypeNameResId);
133        dest.writeInt(mSubtypeIconResId);
134        dest.writeString(mSubtypeLocale);
135        dest.writeString(mSubtypeMode);
136        dest.writeString(mSubtypeExtraValue);
137    }
138
139    public static final Parcelable.Creator<InputMethodSubtype> CREATOR
140            = new Parcelable.Creator<InputMethodSubtype>() {
141        public InputMethodSubtype createFromParcel(Parcel source) {
142            return new InputMethodSubtype(source);
143        }
144
145        public InputMethodSubtype[] newArray(int size) {
146            return new InputMethodSubtype[size];
147        }
148    };
149
150    private static int hashCodeInternal(int nameResId, int iconResId, String locale,
151            String mode, String extraValue) {
152        return Arrays.hashCode(new Object[] {nameResId, iconResId, locale, mode, extraValue});
153    }
154}
155