InputMethodInfo.java revision f06569561fe1c6e898debf8bb9f37331a9f87323
1/*
2 * Copyright (C) 2007-2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package android.view.inputmethod;
18
19import org.xmlpull.v1.XmlPullParser;
20import org.xmlpull.v1.XmlPullParserException;
21
22import android.content.ComponentName;
23import android.content.Context;
24import android.content.pm.ApplicationInfo;
25import android.content.pm.PackageManager;
26import android.content.pm.PackageManager.NameNotFoundException;
27import android.content.pm.ResolveInfo;
28import android.content.pm.ServiceInfo;
29import android.content.res.Resources;
30import android.content.res.TypedArray;
31import android.content.res.XmlResourceParser;
32import android.graphics.drawable.Drawable;
33import android.os.Parcel;
34import android.os.Parcelable;
35import android.util.AttributeSet;
36import android.util.Printer;
37import android.util.Slog;
38import android.util.Xml;
39import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
40import android.view.inputmethod.InputMethodSubtypeArray;
41
42import java.io.IOException;
43import java.util.ArrayList;
44import java.util.List;
45import java.util.Map;
46
47/**
48 * This class is used to specify meta information of an input method.
49 *
50 * <p>It should be defined in an XML resource file with an {@code &lt;input-method>} element.
51 * For more information, see the guide to
52 * <a href="{@docRoot}guide/topics/text/creating-input-method.html">
53 * Creating an Input Method</a>.</p>
54 *
55 * @see InputMethodSubtype
56 *
57 * @attr ref android.R.styleable#InputMethod_settingsActivity
58 * @attr ref android.R.styleable#InputMethod_isDefault
59 * @attr ref android.R.styleable#InputMethod_supportsSwitchingToNextInputMethod
60 */
61public final class InputMethodInfo implements Parcelable {
62    static final String TAG = "InputMethodInfo";
63
64    /**
65     * The Service that implements this input method component.
66     */
67    final ResolveInfo mService;
68
69    /**
70     * The unique string Id to identify the input method.  This is generated
71     * from the input method component.
72     */
73    final String mId;
74
75    /**
76     * The input method setting activity's name, used by the system settings to
77     * launch the setting activity of this input method.
78     */
79    final String mSettingsActivityName;
80
81    /**
82     * The resource in the input method's .apk that holds a boolean indicating
83     * whether it should be considered the default input method for this
84     * system.  This is a resource ID instead of the final value so that it
85     * can change based on the configuration (in particular locale).
86     */
87    final int mIsDefaultResId;
88
89    /**
90     * An array-like container of the subtypes.
91     */
92    private final InputMethodSubtypeArray mSubtypes;
93
94    private final boolean mIsAuxIme;
95
96    /**
97     * Caveat: mForceDefault must be false for production. This flag is only for test.
98     */
99    private final boolean mForceDefault;
100
101    /**
102     * The flag whether this IME supports ways to switch to a next input method (e.g. globe key.)
103     */
104    private final boolean mSupportsSwitchingToNextInputMethod;
105
106    /**
107     * Constructor.
108     *
109     * @param context The Context in which we are parsing the input method.
110     * @param service The ResolveInfo returned from the package manager about
111     * this input method's component.
112     */
113    public InputMethodInfo(Context context, ResolveInfo service)
114            throws XmlPullParserException, IOException {
115        this(context, service, null);
116    }
117
118    /**
119     * Constructor.
120     *
121     * @param context The Context in which we are parsing the input method.
122     * @param service The ResolveInfo returned from the package manager about
123     * this input method's component.
124     * @param additionalSubtypes additional subtypes being added to this InputMethodInfo
125     * @hide
126     */
127    public InputMethodInfo(Context context, ResolveInfo service,
128            Map<String, List<InputMethodSubtype>> additionalSubtypesMap)
129            throws XmlPullParserException, IOException {
130        mService = service;
131        ServiceInfo si = service.serviceInfo;
132        mId = new ComponentName(si.packageName, si.name).flattenToShortString();
133        boolean isAuxIme = true;
134        boolean supportsSwitchingToNextInputMethod = false; // false as default
135        mForceDefault = false;
136
137        PackageManager pm = context.getPackageManager();
138        String settingsActivityComponent = null;
139        int isDefaultResId = 0;
140
141        XmlResourceParser parser = null;
142        final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
143        try {
144            parser = si.loadXmlMetaData(pm, InputMethod.SERVICE_META_DATA);
145            if (parser == null) {
146                throw new XmlPullParserException("No "
147                        + InputMethod.SERVICE_META_DATA + " meta-data");
148            }
149
150            Resources res = pm.getResourcesForApplication(si.applicationInfo);
151
152            AttributeSet attrs = Xml.asAttributeSet(parser);
153
154            int type;
155            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
156                    && type != XmlPullParser.START_TAG) {
157            }
158
159            String nodeName = parser.getName();
160            if (!"input-method".equals(nodeName)) {
161                throw new XmlPullParserException(
162                        "Meta-data does not start with input-method tag");
163            }
164
165            TypedArray sa = res.obtainAttributes(attrs,
166                    com.android.internal.R.styleable.InputMethod);
167            settingsActivityComponent = sa.getString(
168                    com.android.internal.R.styleable.InputMethod_settingsActivity);
169            isDefaultResId = sa.getResourceId(
170                    com.android.internal.R.styleable.InputMethod_isDefault, 0);
171            supportsSwitchingToNextInputMethod = sa.getBoolean(
172                    com.android.internal.R.styleable.InputMethod_supportsSwitchingToNextInputMethod,
173                    false);
174            sa.recycle();
175
176            final int depth = parser.getDepth();
177            // Parse all subtypes
178            while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
179                    && type != XmlPullParser.END_DOCUMENT) {
180                if (type == XmlPullParser.START_TAG) {
181                    nodeName = parser.getName();
182                    if (!"subtype".equals(nodeName)) {
183                        throw new XmlPullParserException(
184                                "Meta-data in input-method does not start with subtype tag");
185                    }
186                    final TypedArray a = res.obtainAttributes(
187                            attrs, com.android.internal.R.styleable.InputMethod_Subtype);
188                    final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
189                            .setSubtypeNameResId(a.getResourceId(com.android.internal.R.styleable
190                                    .InputMethod_Subtype_label, 0))
191                            .setSubtypeIconResId(a.getResourceId(com.android.internal.R.styleable
192                                    .InputMethod_Subtype_icon, 0))
193                            .setSubtypeLocale(a.getString(com.android.internal.R.styleable
194                                    .InputMethod_Subtype_imeSubtypeLocale))
195                            .setSubtypeMode(a.getString(com.android.internal.R.styleable
196                                    .InputMethod_Subtype_imeSubtypeMode))
197                            .setSubtypeExtraValue(a.getString(com.android.internal.R.styleable
198                                    .InputMethod_Subtype_imeSubtypeExtraValue))
199                            .setIsAuxiliary(a.getBoolean(com.android.internal.R.styleable
200                                    .InputMethod_Subtype_isAuxiliary, false))
201                            .setOverridesImplicitlyEnabledSubtype(a.getBoolean(
202                                    com.android.internal.R.styleable
203                                    .InputMethod_Subtype_overridesImplicitlyEnabledSubtype, false))
204                            .setSubtypeId(a.getInt(com.android.internal.R.styleable
205                                    .InputMethod_Subtype_subtypeId, 0 /* use Arrays.hashCode */))
206                            .setIsAsciiCapable(a.getBoolean(com.android.internal.R.styleable
207                                    .InputMethod_Subtype_isAsciiCapable, false)).build();
208                    if (!subtype.isAuxiliary()) {
209                        isAuxIme = false;
210                    }
211                    subtypes.add(subtype);
212                }
213            }
214        } catch (NameNotFoundException e) {
215            throw new XmlPullParserException(
216                    "Unable to create context for: " + si.packageName);
217        } finally {
218            if (parser != null) parser.close();
219        }
220
221        if (subtypes.size() == 0) {
222            isAuxIme = false;
223        }
224
225        if (additionalSubtypesMap != null && additionalSubtypesMap.containsKey(mId)) {
226            final List<InputMethodSubtype> additionalSubtypes = additionalSubtypesMap.get(mId);
227            final int N = additionalSubtypes.size();
228            for (int i = 0; i < N; ++i) {
229                final InputMethodSubtype subtype = additionalSubtypes.get(i);
230                if (!subtypes.contains(subtype)) {
231                    subtypes.add(subtype);
232                } else {
233                    Slog.w(TAG, "Duplicated subtype definition found: "
234                            + subtype.getLocale() + ", " + subtype.getMode());
235                }
236            }
237        }
238        mSubtypes = new InputMethodSubtypeArray(subtypes);
239        mSettingsActivityName = settingsActivityComponent;
240        mIsDefaultResId = isDefaultResId;
241        mIsAuxIme = isAuxIme;
242        mSupportsSwitchingToNextInputMethod = supportsSwitchingToNextInputMethod;
243    }
244
245    InputMethodInfo(Parcel source) {
246        mId = source.readString();
247        mSettingsActivityName = source.readString();
248        mIsDefaultResId = source.readInt();
249        mIsAuxIme = source.readInt() == 1;
250        mSupportsSwitchingToNextInputMethod = source.readInt() == 1;
251        mService = ResolveInfo.CREATOR.createFromParcel(source);
252        mSubtypes = new InputMethodSubtypeArray(source);
253        mForceDefault = false;
254    }
255
256    /**
257     * Temporary API for creating a built-in input method for test.
258     */
259    public InputMethodInfo(String packageName, String className,
260            CharSequence label, String settingsActivity) {
261        this(buildDummyResolveInfo(packageName, className, label), false, settingsActivity, null,
262                0, false);
263    }
264
265    /**
266     * Temporary API for creating a built-in input method for test.
267     * @hide
268     */
269    public InputMethodInfo(ResolveInfo ri, boolean isAuxIme,
270            String settingsActivity, List<InputMethodSubtype> subtypes, int isDefaultResId,
271            boolean forceDefault) {
272        final ServiceInfo si = ri.serviceInfo;
273        mService = ri;
274        mId = new ComponentName(si.packageName, si.name).flattenToShortString();
275        mSettingsActivityName = settingsActivity;
276        mIsDefaultResId = isDefaultResId;
277        mIsAuxIme = isAuxIme;
278        mSubtypes = new InputMethodSubtypeArray(subtypes);
279        mForceDefault = forceDefault;
280        mSupportsSwitchingToNextInputMethod = true;
281    }
282
283    private static ResolveInfo buildDummyResolveInfo(String packageName, String className,
284            CharSequence label) {
285        ResolveInfo ri = new ResolveInfo();
286        ServiceInfo si = new ServiceInfo();
287        ApplicationInfo ai = new ApplicationInfo();
288        ai.packageName = packageName;
289        ai.enabled = true;
290        si.applicationInfo = ai;
291        si.enabled = true;
292        si.packageName = packageName;
293        si.name = className;
294        si.exported = true;
295        si.nonLocalizedLabel = label;
296        ri.serviceInfo = si;
297        return ri;
298    }
299
300    /**
301     * Return a unique ID for this input method.  The ID is generated from
302     * the package and class name implementing the method.
303     */
304    public String getId() {
305        return mId;
306    }
307
308    /**
309     * Return the .apk package that implements this input method.
310     */
311    public String getPackageName() {
312        return mService.serviceInfo.packageName;
313    }
314
315    /**
316     * Return the class name of the service component that implements
317     * this input method.
318     */
319    public String getServiceName() {
320        return mService.serviceInfo.name;
321    }
322
323    /**
324     * Return the raw information about the Service implementing this
325     * input method.  Do not modify the returned object.
326     */
327    public ServiceInfo getServiceInfo() {
328        return mService.serviceInfo;
329    }
330
331    /**
332     * Return the component of the service that implements this input
333     * method.
334     */
335    public ComponentName getComponent() {
336        return new ComponentName(mService.serviceInfo.packageName,
337                mService.serviceInfo.name);
338    }
339
340    /**
341     * Load the user-displayed label for this input method.
342     *
343     * @param pm Supply a PackageManager used to load the input method's
344     * resources.
345     */
346    public CharSequence loadLabel(PackageManager pm) {
347        return mService.loadLabel(pm);
348    }
349
350    /**
351     * Load the user-displayed icon for this input method.
352     *
353     * @param pm Supply a PackageManager used to load the input method's
354     * resources.
355     */
356    public Drawable loadIcon(PackageManager pm) {
357        return mService.loadIcon(pm);
358    }
359
360    /**
361     * Return the class name of an activity that provides a settings UI for
362     * the input method.  You can launch this activity be starting it with
363     * an {@link android.content.Intent} whose action is MAIN and with an
364     * explicit {@link android.content.ComponentName}
365     * composed of {@link #getPackageName} and the class name returned here.
366     *
367     * <p>A null will be returned if there is no settings activity associated
368     * with the input method.</p>
369     */
370    public String getSettingsActivity() {
371        return mSettingsActivityName;
372    }
373
374    /**
375     * Return the count of the subtypes of Input Method.
376     */
377    public int getSubtypeCount() {
378        return mSubtypes.getCount();
379    }
380
381    /**
382     * Return the Input Method's subtype at the specified index.
383     *
384     * @param index the index of the subtype to return.
385     */
386    public InputMethodSubtype getSubtypeAt(int index) {
387        return mSubtypes.get(index);
388    }
389
390    /**
391     * Return the resource identifier of a resource inside of this input
392     * method's .apk that determines whether it should be considered a
393     * default input method for the system.
394     */
395    public int getIsDefaultResourceId() {
396        return mIsDefaultResId;
397    }
398
399    /**
400     * Return whether or not this ime is a default ime or not.
401     * @hide
402     */
403    public boolean isDefault(Context context) {
404        if (mForceDefault) {
405            return true;
406        }
407        try {
408            final Resources res = context.createPackageContext(getPackageName(), 0).getResources();
409            return res.getBoolean(getIsDefaultResourceId());
410        } catch (NameNotFoundException e) {
411            return false;
412        }
413    }
414
415    public void dump(Printer pw, String prefix) {
416        pw.println(prefix + "mId=" + mId
417                + " mSettingsActivityName=" + mSettingsActivityName);
418        pw.println(prefix + "mIsDefaultResId=0x"
419                + Integer.toHexString(mIsDefaultResId));
420        pw.println(prefix + "Service:");
421        mService.dump(pw, prefix + "  ");
422    }
423
424    @Override
425    public String toString() {
426        return "InputMethodInfo{" + mId
427                + ", settings: "
428                + mSettingsActivityName + "}";
429    }
430
431    /**
432     * Used to test whether the given parameter object is an
433     * {@link InputMethodInfo} and its Id is the same to this one.
434     *
435     * @return true if the given parameter object is an
436     *         {@link InputMethodInfo} and its Id is the same to this one.
437     */
438    @Override
439    public boolean equals(Object o) {
440        if (o == this) return true;
441        if (o == null) return false;
442
443        if (!(o instanceof InputMethodInfo)) return false;
444
445        InputMethodInfo obj = (InputMethodInfo) o;
446        return mId.equals(obj.mId);
447    }
448
449    @Override
450    public int hashCode() {
451        return mId.hashCode();
452    }
453
454    /**
455     * @hide
456     */
457    public boolean isAuxiliaryIme() {
458        return mIsAuxIme;
459    }
460
461    /**
462     * @return true if this input method supports ways to switch to a next input method.
463     * @hide
464     */
465    public boolean supportsSwitchingToNextInputMethod() {
466        return mSupportsSwitchingToNextInputMethod;
467    }
468
469    /**
470     * Used to package this object into a {@link Parcel}.
471     *
472     * @param dest The {@link Parcel} to be written.
473     * @param flags The flags used for parceling.
474     */
475    @Override
476    public void writeToParcel(Parcel dest, int flags) {
477        dest.writeString(mId);
478        dest.writeString(mSettingsActivityName);
479        dest.writeInt(mIsDefaultResId);
480        dest.writeInt(mIsAuxIme ? 1 : 0);
481        dest.writeInt(mSupportsSwitchingToNextInputMethod ? 1 : 0);
482        mService.writeToParcel(dest, flags);
483        mSubtypes.writeToParcel(dest);
484    }
485
486    /**
487     * Used to make this class parcelable.
488     */
489    public static final Parcelable.Creator<InputMethodInfo> CREATOR
490            = new Parcelable.Creator<InputMethodInfo>() {
491        @Override
492        public InputMethodInfo createFromParcel(Parcel source) {
493            return new InputMethodInfo(source);
494        }
495
496        @Override
497        public InputMethodInfo[] newArray(int size) {
498            return new InputMethodInfo[size];
499        }
500    };
501
502    @Override
503    public int describeContents() {
504        return 0;
505    }
506}
507