The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2008 The Android Open Source Project |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 3 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 7 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 9 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package android.view.inputmethod; |
| 18 | |
| 19 | import org.xmlpull.v1.XmlPullParser; |
| 20 | import org.xmlpull.v1.XmlPullParserException; |
| 21 | |
| 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
| 24 | import android.content.pm.ApplicationInfo; |
| 25 | import android.content.pm.PackageManager; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 26 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.content.pm.ResolveInfo; |
| 28 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 29 | import android.content.res.Resources; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.content.res.TypedArray; |
| 31 | import android.content.res.XmlResourceParser; |
| 32 | import android.graphics.drawable.Drawable; |
| 33 | import android.os.Parcel; |
| 34 | import android.os.Parcelable; |
| 35 | import android.util.AttributeSet; |
| 36 | import android.util.Printer; |
Satoshi Kataoka | e62e6d8 | 2012-07-02 18:45:43 +0900 | [diff] [blame] | 37 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.util.Xml; |
Satoshi Kataoka | dc8abf6 | 2013-03-12 14:40:04 +0900 | [diff] [blame] | 39 | import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder; |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 40 | import android.view.inputmethod.InputMethodSubtypeArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 42 | import java.io.IOException; |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 43 | import java.util.ArrayList; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 44 | import java.util.List; |
| 45 | import java.util.Map; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * This class is used to specify meta information of an input method. |
Scott Main | 5df0631 | 2013-10-18 16:07:47 -0700 | [diff] [blame] | 49 | * |
| 50 | * <p>It should be defined in an XML resource file with an {@code <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 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | */ |
| 61 | public final class InputMethodInfo implements Parcelable { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 62 | static final String TAG = "InputMethodInfo"; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | /** |
| 65 | * The Service that implements this input method component. |
| 66 | */ |
| 67 | final ResolveInfo mService; |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 68 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 88 | |
| 89 | /** |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 90 | * An array-like container of the subtypes. |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 91 | */ |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 92 | private final InputMethodSubtypeArray mSubtypes; |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 93 | |
Satoshi Kataoka | 660e6c9 | 2013-08-01 16:50:48 +0900 | [diff] [blame] | 94 | private final boolean mIsAuxIme; |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 95 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | /** |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 97 | * Caveat: mForceDefault must be false for production. This flag is only for test. |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 98 | */ |
| 99 | private final boolean mForceDefault; |
| 100 | |
| 101 | /** |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 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 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | * Constructor. |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 108 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 { |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 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 { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | mService = service; |
| 131 | ServiceInfo si = service.serviceInfo; |
| 132 | mId = new ComponentName(si.packageName, si.name).flattenToShortString(); |
Satoshi Kataoka | 660e6c9 | 2013-08-01 16:50:48 +0900 | [diff] [blame] | 133 | boolean isAuxIme = true; |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 134 | boolean supportsSwitchingToNextInputMethod = false; // false as default |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 135 | mForceDefault = false; |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 136 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | PackageManager pm = context.getPackageManager(); |
| 138 | String settingsActivityComponent = null; |
| 139 | int isDefaultResId = 0; |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 140 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | XmlResourceParser parser = null; |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 142 | final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | } |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 149 | |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 150 | Resources res = pm.getResourcesForApplication(si.applicationInfo); |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | AttributeSet attrs = Xml.asAttributeSet(parser); |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 153 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | int type; |
| 155 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 156 | && type != XmlPullParser.START_TAG) { |
| 157 | } |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 158 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | } |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 164 | |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 165 | TypedArray sa = res.obtainAttributes(attrs, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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); |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 171 | supportsSwitchingToNextInputMethod = sa.getBoolean( |
| 172 | com.android.internal.R.styleable.InputMethod_supportsSwitchingToNextInputMethod, |
| 173 | false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | sa.recycle(); |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 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) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 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); |
Satoshi Kataoka | dc8abf6 | 2013-03-12 14:40:04 +0900 | [diff] [blame] | 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(); |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 208 | if (!subtype.isAuxiliary()) { |
Satoshi Kataoka | 660e6c9 | 2013-08-01 16:50:48 +0900 | [diff] [blame] | 209 | isAuxIme = false; |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 210 | } |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 211 | subtypes.add(subtype); |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 212 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 213 | } |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 214 | } catch (NameNotFoundException e) { |
| 215 | throw new XmlPullParserException( |
| 216 | "Unable to create context for: " + si.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | } finally { |
| 218 | if (parser != null) parser.close(); |
| 219 | } |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 220 | |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 221 | if (subtypes.size() == 0) { |
Satoshi Kataoka | 660e6c9 | 2013-08-01 16:50:48 +0900 | [diff] [blame] | 222 | isAuxIme = false; |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 223 | } |
| 224 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 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); |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 230 | if (!subtypes.contains(subtype)) { |
| 231 | subtypes.add(subtype); |
Satoshi Kataoka | e62e6d8 | 2012-07-02 18:45:43 +0900 | [diff] [blame] | 232 | } else { |
| 233 | Slog.w(TAG, "Duplicated subtype definition found: " |
| 234 | + subtype.getLocale() + ", " + subtype.getMode()); |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 238 | mSubtypes = new InputMethodSubtypeArray(subtypes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | mSettingsActivityName = settingsActivityComponent; |
| 240 | mIsDefaultResId = isDefaultResId; |
Satoshi Kataoka | 660e6c9 | 2013-08-01 16:50:48 +0900 | [diff] [blame] | 241 | mIsAuxIme = isAuxIme; |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 242 | mSupportsSwitchingToNextInputMethod = supportsSwitchingToNextInputMethod; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | InputMethodInfo(Parcel source) { |
| 246 | mId = source.readString(); |
| 247 | mSettingsActivityName = source.readString(); |
| 248 | mIsDefaultResId = source.readInt(); |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 249 | mIsAuxIme = source.readInt() == 1; |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 250 | mSupportsSwitchingToNextInputMethod = source.readInt() == 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | mService = ResolveInfo.CREATOR.createFromParcel(source); |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 252 | mSubtypes = new InputMethodSubtypeArray(source); |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 253 | mForceDefault = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | /** |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 257 | * Temporary API for creating a built-in input method for test. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | */ |
| 259 | public InputMethodInfo(String packageName, String className, |
| 260 | CharSequence label, String settingsActivity) { |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 261 | this(buildDummyResolveInfo(packageName, className, label), false, settingsActivity, null, |
Yohei Yukawa | d1da115 | 2014-05-01 17:20:05 +0900 | [diff] [blame] | 262 | 0, false /* forceDefault */, true /* supportsSwitchingToNextInputMethod */); |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 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) { |
Yohei Yukawa | d1da115 | 2014-05-01 17:20:05 +0900 | [diff] [blame] | 272 | this(ri, isAuxIme, settingsActivity, subtypes, isDefaultResId, |
| 273 | forceDefault, true /* supportsSwitchingToNextInputMethod */); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Temporary API for creating a built-in input method for test. |
| 278 | * @hide |
| 279 | */ |
| 280 | public InputMethodInfo(ResolveInfo ri, boolean isAuxIme, |
| 281 | String settingsActivity, List<InputMethodSubtype> subtypes, int isDefaultResId, |
| 282 | boolean forceDefault, boolean supportsSwitchingToNextInputMethod) { |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 283 | final ServiceInfo si = ri.serviceInfo; |
| 284 | mService = ri; |
| 285 | mId = new ComponentName(si.packageName, si.name).flattenToShortString(); |
| 286 | mSettingsActivityName = settingsActivity; |
| 287 | mIsDefaultResId = isDefaultResId; |
| 288 | mIsAuxIme = isAuxIme; |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 289 | mSubtypes = new InputMethodSubtypeArray(subtypes); |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 290 | mForceDefault = forceDefault; |
Yohei Yukawa | d1da115 | 2014-05-01 17:20:05 +0900 | [diff] [blame] | 291 | mSupportsSwitchingToNextInputMethod = supportsSwitchingToNextInputMethod; |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | private static ResolveInfo buildDummyResolveInfo(String packageName, String className, |
| 295 | CharSequence label) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | ResolveInfo ri = new ResolveInfo(); |
| 297 | ServiceInfo si = new ServiceInfo(); |
| 298 | ApplicationInfo ai = new ApplicationInfo(); |
| 299 | ai.packageName = packageName; |
| 300 | ai.enabled = true; |
| 301 | si.applicationInfo = ai; |
| 302 | si.enabled = true; |
| 303 | si.packageName = packageName; |
| 304 | si.name = className; |
| 305 | si.exported = true; |
| 306 | si.nonLocalizedLabel = label; |
| 307 | ri.serviceInfo = si; |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 308 | return ri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | /** |
| 312 | * Return a unique ID for this input method. The ID is generated from |
| 313 | * the package and class name implementing the method. |
| 314 | */ |
| 315 | public String getId() { |
| 316 | return mId; |
| 317 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 318 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | /** |
| 320 | * Return the .apk package that implements this input method. |
| 321 | */ |
| 322 | public String getPackageName() { |
| 323 | return mService.serviceInfo.packageName; |
| 324 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 325 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | /** |
| 327 | * Return the class name of the service component that implements |
| 328 | * this input method. |
| 329 | */ |
| 330 | public String getServiceName() { |
| 331 | return mService.serviceInfo.name; |
| 332 | } |
| 333 | |
| 334 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 335 | * Return the raw information about the Service implementing this |
| 336 | * input method. Do not modify the returned object. |
| 337 | */ |
| 338 | public ServiceInfo getServiceInfo() { |
| 339 | return mService.serviceInfo; |
| 340 | } |
| 341 | |
| 342 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | * Return the component of the service that implements this input |
| 344 | * method. |
| 345 | */ |
| 346 | public ComponentName getComponent() { |
| 347 | return new ComponentName(mService.serviceInfo.packageName, |
| 348 | mService.serviceInfo.name); |
| 349 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 350 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | /** |
| 352 | * Load the user-displayed label for this input method. |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 353 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | * @param pm Supply a PackageManager used to load the input method's |
| 355 | * resources. |
| 356 | */ |
| 357 | public CharSequence loadLabel(PackageManager pm) { |
| 358 | return mService.loadLabel(pm); |
| 359 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 360 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | /** |
| 362 | * Load the user-displayed icon for this input method. |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 363 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | * @param pm Supply a PackageManager used to load the input method's |
| 365 | * resources. |
| 366 | */ |
| 367 | public Drawable loadIcon(PackageManager pm) { |
| 368 | return mService.loadIcon(pm); |
| 369 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 370 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | /** |
| 372 | * Return the class name of an activity that provides a settings UI for |
| 373 | * the input method. You can launch this activity be starting it with |
| 374 | * an {@link android.content.Intent} whose action is MAIN and with an |
| 375 | * explicit {@link android.content.ComponentName} |
| 376 | * composed of {@link #getPackageName} and the class name returned here. |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 377 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | * <p>A null will be returned if there is no settings activity associated |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 379 | * with the input method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | */ |
| 381 | public String getSettingsActivity() { |
| 382 | return mSettingsActivityName; |
| 383 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 384 | |
| 385 | /** |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 386 | * Return the count of the subtypes of Input Method. |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 387 | */ |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 388 | public int getSubtypeCount() { |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 389 | return mSubtypes.getCount(); |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Return the Input Method's subtype at the specified index. |
| 394 | * |
| 395 | * @param index the index of the subtype to return. |
| 396 | */ |
| 397 | public InputMethodSubtype getSubtypeAt(int index) { |
| 398 | return mSubtypes.get(index); |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 399 | } |
| 400 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | /** |
| 402 | * Return the resource identifier of a resource inside of this input |
| 403 | * method's .apk that determines whether it should be considered a |
| 404 | * default input method for the system. |
| 405 | */ |
| 406 | public int getIsDefaultResourceId() { |
| 407 | return mIsDefaultResId; |
| 408 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 409 | |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 410 | /** |
| 411 | * Return whether or not this ime is a default ime or not. |
| 412 | * @hide |
| 413 | */ |
| 414 | public boolean isDefault(Context context) { |
| 415 | if (mForceDefault) { |
| 416 | return true; |
| 417 | } |
| 418 | try { |
Yohei Yukawa | dc48924 | 2014-09-14 12:01:59 +0900 | [diff] [blame] | 419 | if (getIsDefaultResourceId() == 0) { |
| 420 | return false; |
| 421 | } |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 422 | final Resources res = context.createPackageContext(getPackageName(), 0).getResources(); |
| 423 | return res.getBoolean(getIsDefaultResourceId()); |
| 424 | } catch (NameNotFoundException e) { |
| 425 | return false; |
| 426 | } |
| 427 | } |
| 428 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | public void dump(Printer pw, String prefix) { |
| 430 | pw.println(prefix + "mId=" + mId |
| 431 | + " mSettingsActivityName=" + mSettingsActivityName); |
| 432 | pw.println(prefix + "mIsDefaultResId=0x" |
| 433 | + Integer.toHexString(mIsDefaultResId)); |
| 434 | pw.println(prefix + "Service:"); |
| 435 | mService.dump(pw, prefix + " "); |
| 436 | } |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 437 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | @Override |
| 439 | public String toString() { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 440 | return "InputMethodInfo{" + mId |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | + ", settings: " |
| 442 | + mSettingsActivityName + "}"; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Used to test whether the given parameter object is an |
| 447 | * {@link InputMethodInfo} and its Id is the same to this one. |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 448 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | * @return true if the given parameter object is an |
| 450 | * {@link InputMethodInfo} and its Id is the same to this one. |
| 451 | */ |
| 452 | @Override |
| 453 | public boolean equals(Object o) { |
| 454 | if (o == this) return true; |
| 455 | if (o == null) return false; |
| 456 | |
| 457 | if (!(o instanceof InputMethodInfo)) return false; |
| 458 | |
| 459 | InputMethodInfo obj = (InputMethodInfo) o; |
| 460 | return mId.equals(obj.mId); |
| 461 | } |
satok | c50232d | 2011-03-25 13:52:48 +0900 | [diff] [blame] | 462 | |
| 463 | @Override |
| 464 | public int hashCode() { |
| 465 | return mId.hashCode(); |
| 466 | } |
| 467 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | /** |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 469 | * @hide |
| 470 | */ |
| 471 | public boolean isAuxiliaryIme() { |
| 472 | return mIsAuxIme; |
| 473 | } |
| 474 | |
| 475 | /** |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 476 | * @return true if this input method supports ways to switch to a next input method. |
| 477 | * @hide |
| 478 | */ |
| 479 | public boolean supportsSwitchingToNextInputMethod() { |
| 480 | return mSupportsSwitchingToNextInputMethod; |
| 481 | } |
| 482 | |
| 483 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | * Used to package this object into a {@link Parcel}. |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 485 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | * @param dest The {@link Parcel} to be written. |
| 487 | * @param flags The flags used for parceling. |
| 488 | */ |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 489 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | public void writeToParcel(Parcel dest, int flags) { |
| 491 | dest.writeString(mId); |
| 492 | dest.writeString(mSettingsActivityName); |
| 493 | dest.writeInt(mIsDefaultResId); |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 494 | dest.writeInt(mIsAuxIme ? 1 : 0); |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 495 | dest.writeInt(mSupportsSwitchingToNextInputMethod ? 1 : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | mService.writeToParcel(dest, flags); |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 497 | mSubtypes.writeToParcel(dest); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Used to make this class parcelable. |
| 502 | */ |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 503 | public static final Parcelable.Creator<InputMethodInfo> CREATOR |
| 504 | = new Parcelable.Creator<InputMethodInfo>() { |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 505 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | public InputMethodInfo createFromParcel(Parcel source) { |
| 507 | return new InputMethodInfo(source); |
| 508 | } |
| 509 | |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 510 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | public InputMethodInfo[] newArray(int size) { |
| 512 | return new InputMethodInfo[size]; |
| 513 | } |
| 514 | }; |
| 515 | |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 516 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | public int describeContents() { |
| 518 | return 0; |
| 519 | } |
| 520 | } |