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 | |
Yohei Yukawa | ddad4b9 | 2017-02-02 01:46:13 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.pm.ApplicationInfo; |
| 24 | import android.content.pm.PackageManager; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 25 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.content.pm.ResolveInfo; |
| 27 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 28 | import android.content.res.Resources; |
Yohei Yukawa | fcedfa0 | 2014-09-19 13:22:15 +0900 | [diff] [blame] | 29 | import android.content.res.Resources.NotFoundException; |
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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 41 | import org.xmlpull.v1.XmlPullParser; |
| 42 | import org.xmlpull.v1.XmlPullParserException; |
| 43 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import java.io.IOException; |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 45 | import java.util.ArrayList; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 46 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * This class is used to specify meta information of an input method. |
Scott Main | 5df0631 | 2013-10-18 16:07:47 -0700 | [diff] [blame] | 50 | * |
Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 51 | * <p>It should be defined in an XML resource file with an {@code <input-method>} element. |
Scott Main | 5df0631 | 2013-10-18 16:07:47 -0700 | [diff] [blame] | 52 | * For more information, see the guide to |
| 53 | * <a href="{@docRoot}guide/topics/text/creating-input-method.html"> |
| 54 | * Creating an Input Method</a>.</p> |
| 55 | * |
| 56 | * @see InputMethodSubtype |
| 57 | * |
| 58 | * @attr ref android.R.styleable#InputMethod_settingsActivity |
| 59 | * @attr ref android.R.styleable#InputMethod_isDefault |
| 60 | * @attr ref android.R.styleable#InputMethod_supportsSwitchingToNextInputMethod |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | */ |
| 62 | public final class InputMethodInfo implements Parcelable { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 63 | static final String TAG = "InputMethodInfo"; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | /** |
| 66 | * The Service that implements this input method component. |
| 67 | */ |
| 68 | final ResolveInfo mService; |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 69 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | /** |
Tarandeep Singh | b360b99 | 2017-11-21 14:25:34 -0800 | [diff] [blame] | 71 | * IME only supports VR mode. |
| 72 | */ |
| 73 | final boolean mIsVrOnly; |
| 74 | |
| 75 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | * The unique string Id to identify the input method. This is generated |
| 77 | * from the input method component. |
| 78 | */ |
| 79 | final String mId; |
| 80 | |
| 81 | /** |
| 82 | * The input method setting activity's name, used by the system settings to |
| 83 | * launch the setting activity of this input method. |
| 84 | */ |
| 85 | final String mSettingsActivityName; |
| 86 | |
| 87 | /** |
| 88 | * The resource in the input method's .apk that holds a boolean indicating |
| 89 | * whether it should be considered the default input method for this |
| 90 | * system. This is a resource ID instead of the final value so that it |
| 91 | * can change based on the configuration (in particular locale). |
| 92 | */ |
| 93 | final int mIsDefaultResId; |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 94 | |
| 95 | /** |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 96 | * An array-like container of the subtypes. |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 97 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 98 | @UnsupportedAppUsage |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 99 | private final InputMethodSubtypeArray mSubtypes; |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 100 | |
Satoshi Kataoka | 660e6c9 | 2013-08-01 16:50:48 +0900 | [diff] [blame] | 101 | private final boolean mIsAuxIme; |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 102 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | /** |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 104 | * 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] | 105 | */ |
| 106 | private final boolean mForceDefault; |
| 107 | |
| 108 | /** |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 109 | * The flag whether this IME supports ways to switch to a next input method (e.g. globe key.) |
| 110 | */ |
| 111 | private final boolean mSupportsSwitchingToNextInputMethod; |
| 112 | |
| 113 | /** |
Yohei Yukawa | ddad4b9 | 2017-02-02 01:46:13 -0800 | [diff] [blame] | 114 | * @param service the {@link ResolveInfo} corresponds in which the IME is implemented. |
| 115 | * @return a unique ID to be returned by {@link #getId()}. We have used |
| 116 | * {@link ComponentName#flattenToShortString()} for this purpose (and it is already |
| 117 | * unrealistic to switch to a different scheme as it is already implicitly assumed in |
| 118 | * many places). |
| 119 | * @hide |
| 120 | */ |
| 121 | public static String computeId(@NonNull ResolveInfo service) { |
| 122 | final ServiceInfo si = service.serviceInfo; |
| 123 | return new ComponentName(si.packageName, si.name).flattenToShortString(); |
| 124 | } |
| 125 | |
| 126 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | * Constructor. |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 128 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | * @param context The Context in which we are parsing the input method. |
| 130 | * @param service The ResolveInfo returned from the package manager about |
| 131 | * this input method's component. |
| 132 | */ |
| 133 | public InputMethodInfo(Context context, ResolveInfo service) |
| 134 | throws XmlPullParserException, IOException { |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 135 | this(context, service, null); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Constructor. |
| 140 | * |
| 141 | * @param context The Context in which we are parsing the input method. |
| 142 | * @param service The ResolveInfo returned from the package manager about |
| 143 | * this input method's component. |
Yohei Yukawa | ddad4b9 | 2017-02-02 01:46:13 -0800 | [diff] [blame] | 144 | * @param additionalSubtypes additional subtypes being added to this InputMethodInfo |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 145 | * @hide |
| 146 | */ |
| 147 | public InputMethodInfo(Context context, ResolveInfo service, |
Yohei Yukawa | ddad4b9 | 2017-02-02 01:46:13 -0800 | [diff] [blame] | 148 | List<InputMethodSubtype> additionalSubtypes) |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 149 | throws XmlPullParserException, IOException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | mService = service; |
| 151 | ServiceInfo si = service.serviceInfo; |
Yohei Yukawa | ddad4b9 | 2017-02-02 01:46:13 -0800 | [diff] [blame] | 152 | mId = computeId(service); |
Satoshi Kataoka | 660e6c9 | 2013-08-01 16:50:48 +0900 | [diff] [blame] | 153 | boolean isAuxIme = true; |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 154 | boolean supportsSwitchingToNextInputMethod = false; // false as default |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 155 | mForceDefault = false; |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | PackageManager pm = context.getPackageManager(); |
| 158 | String settingsActivityComponent = null; |
Tarandeep Singh | b360b99 | 2017-11-21 14:25:34 -0800 | [diff] [blame] | 159 | boolean isVrOnly; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | int isDefaultResId = 0; |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 161 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | XmlResourceParser parser = null; |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 163 | final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | try { |
| 165 | parser = si.loadXmlMetaData(pm, InputMethod.SERVICE_META_DATA); |
| 166 | if (parser == null) { |
| 167 | throw new XmlPullParserException("No " |
| 168 | + InputMethod.SERVICE_META_DATA + " meta-data"); |
| 169 | } |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 170 | |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 171 | Resources res = pm.getResourcesForApplication(si.applicationInfo); |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 172 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | AttributeSet attrs = Xml.asAttributeSet(parser); |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 174 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | int type; |
| 176 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 177 | && type != XmlPullParser.START_TAG) { |
| 178 | } |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 179 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | String nodeName = parser.getName(); |
| 181 | if (!"input-method".equals(nodeName)) { |
| 182 | throw new XmlPullParserException( |
| 183 | "Meta-data does not start with input-method tag"); |
| 184 | } |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 185 | |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 186 | TypedArray sa = res.obtainAttributes(attrs, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | com.android.internal.R.styleable.InputMethod); |
| 188 | settingsActivityComponent = sa.getString( |
| 189 | com.android.internal.R.styleable.InputMethod_settingsActivity); |
Tarandeep Singh | b360b99 | 2017-11-21 14:25:34 -0800 | [diff] [blame] | 190 | isVrOnly = sa.getBoolean(com.android.internal.R.styleable.InputMethod_isVrOnly, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | isDefaultResId = sa.getResourceId( |
| 192 | com.android.internal.R.styleable.InputMethod_isDefault, 0); |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 193 | supportsSwitchingToNextInputMethod = sa.getBoolean( |
| 194 | com.android.internal.R.styleable.InputMethod_supportsSwitchingToNextInputMethod, |
| 195 | false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | sa.recycle(); |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 197 | |
| 198 | final int depth = parser.getDepth(); |
| 199 | // Parse all subtypes |
| 200 | while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) |
| 201 | && type != XmlPullParser.END_DOCUMENT) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 202 | if (type == XmlPullParser.START_TAG) { |
| 203 | nodeName = parser.getName(); |
| 204 | if (!"subtype".equals(nodeName)) { |
| 205 | throw new XmlPullParserException( |
| 206 | "Meta-data in input-method does not start with subtype tag"); |
| 207 | } |
| 208 | final TypedArray a = res.obtainAttributes( |
| 209 | attrs, com.android.internal.R.styleable.InputMethod_Subtype); |
Satoshi Kataoka | dc8abf6 | 2013-03-12 14:40:04 +0900 | [diff] [blame] | 210 | final InputMethodSubtype subtype = new InputMethodSubtypeBuilder() |
| 211 | .setSubtypeNameResId(a.getResourceId(com.android.internal.R.styleable |
| 212 | .InputMethod_Subtype_label, 0)) |
| 213 | .setSubtypeIconResId(a.getResourceId(com.android.internal.R.styleable |
| 214 | .InputMethod_Subtype_icon, 0)) |
Yohei Yukawa | b8456a6 | 2016-01-21 22:24:44 -0800 | [diff] [blame] | 215 | .setLanguageTag(a.getString(com.android.internal.R.styleable |
| 216 | .InputMethod_Subtype_languageTag)) |
Satoshi Kataoka | dc8abf6 | 2013-03-12 14:40:04 +0900 | [diff] [blame] | 217 | .setSubtypeLocale(a.getString(com.android.internal.R.styleable |
| 218 | .InputMethod_Subtype_imeSubtypeLocale)) |
| 219 | .setSubtypeMode(a.getString(com.android.internal.R.styleable |
| 220 | .InputMethod_Subtype_imeSubtypeMode)) |
| 221 | .setSubtypeExtraValue(a.getString(com.android.internal.R.styleable |
| 222 | .InputMethod_Subtype_imeSubtypeExtraValue)) |
| 223 | .setIsAuxiliary(a.getBoolean(com.android.internal.R.styleable |
| 224 | .InputMethod_Subtype_isAuxiliary, false)) |
| 225 | .setOverridesImplicitlyEnabledSubtype(a.getBoolean( |
| 226 | com.android.internal.R.styleable |
| 227 | .InputMethod_Subtype_overridesImplicitlyEnabledSubtype, false)) |
| 228 | .setSubtypeId(a.getInt(com.android.internal.R.styleable |
| 229 | .InputMethod_Subtype_subtypeId, 0 /* use Arrays.hashCode */)) |
| 230 | .setIsAsciiCapable(a.getBoolean(com.android.internal.R.styleable |
| 231 | .InputMethod_Subtype_isAsciiCapable, false)).build(); |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 232 | if (!subtype.isAuxiliary()) { |
Satoshi Kataoka | 660e6c9 | 2013-08-01 16:50:48 +0900 | [diff] [blame] | 233 | isAuxIme = false; |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 234 | } |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 235 | subtypes.add(subtype); |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 236 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 237 | } |
Keisuke Kuroyanagi | e65e860 | 2016-08-04 13:58:35 +0900 | [diff] [blame] | 238 | } catch (NameNotFoundException | IndexOutOfBoundsException | NumberFormatException e) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 239 | throw new XmlPullParserException( |
| 240 | "Unable to create context for: " + si.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | } finally { |
| 242 | if (parser != null) parser.close(); |
| 243 | } |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 244 | |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 245 | if (subtypes.size() == 0) { |
Satoshi Kataoka | 660e6c9 | 2013-08-01 16:50:48 +0900 | [diff] [blame] | 246 | isAuxIme = false; |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 247 | } |
| 248 | |
Yohei Yukawa | ddad4b9 | 2017-02-02 01:46:13 -0800 | [diff] [blame] | 249 | if (additionalSubtypes != null) { |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 250 | final int N = additionalSubtypes.size(); |
| 251 | for (int i = 0; i < N; ++i) { |
| 252 | final InputMethodSubtype subtype = additionalSubtypes.get(i); |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 253 | if (!subtypes.contains(subtype)) { |
| 254 | subtypes.add(subtype); |
Satoshi Kataoka | e62e6d8 | 2012-07-02 18:45:43 +0900 | [diff] [blame] | 255 | } else { |
| 256 | Slog.w(TAG, "Duplicated subtype definition found: " |
| 257 | + subtype.getLocale() + ", " + subtype.getMode()); |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | } |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 261 | mSubtypes = new InputMethodSubtypeArray(subtypes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | mSettingsActivityName = settingsActivityComponent; |
| 263 | mIsDefaultResId = isDefaultResId; |
Satoshi Kataoka | 660e6c9 | 2013-08-01 16:50:48 +0900 | [diff] [blame] | 264 | mIsAuxIme = isAuxIme; |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 265 | mSupportsSwitchingToNextInputMethod = supportsSwitchingToNextInputMethod; |
Tarandeep Singh | eda9f31 | 2018-03-08 16:11:37 -0800 | [diff] [blame] | 266 | mIsVrOnly = isVrOnly; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | InputMethodInfo(Parcel source) { |
| 270 | mId = source.readString(); |
| 271 | mSettingsActivityName = source.readString(); |
| 272 | mIsDefaultResId = source.readInt(); |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 273 | mIsAuxIme = source.readInt() == 1; |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 274 | mSupportsSwitchingToNextInputMethod = source.readInt() == 1; |
Tarandeep Singh | b360b99 | 2017-11-21 14:25:34 -0800 | [diff] [blame] | 275 | mIsVrOnly = source.readBoolean(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | mService = ResolveInfo.CREATOR.createFromParcel(source); |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 277 | mSubtypes = new InputMethodSubtypeArray(source); |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 278 | mForceDefault = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 280 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | /** |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 282 | * 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] | 283 | */ |
| 284 | public InputMethodInfo(String packageName, String className, |
| 285 | CharSequence label, String settingsActivity) { |
Tadashi G. Takaoka | e12a6ff | 2017-01-11 15:51:44 +0900 | [diff] [blame] | 286 | this(buildDummyResolveInfo(packageName, className, label), false /* isAuxIme */, |
| 287 | settingsActivity, null /* subtypes */, 0 /* isDefaultResId */, |
Tarandeep Singh | b360b99 | 2017-11-21 14:25:34 -0800 | [diff] [blame] | 288 | false /* forceDefault */, true /* supportsSwitchingToNextInputMethod */, |
| 289 | false /* isVrOnly */); |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Temporary API for creating a built-in input method for test. |
| 294 | * @hide |
| 295 | */ |
| 296 | public InputMethodInfo(ResolveInfo ri, boolean isAuxIme, |
| 297 | String settingsActivity, List<InputMethodSubtype> subtypes, int isDefaultResId, |
| 298 | boolean forceDefault) { |
Tadashi G. Takaoka | e12a6ff | 2017-01-11 15:51:44 +0900 | [diff] [blame] | 299 | this(ri, isAuxIme, settingsActivity, subtypes, isDefaultResId, forceDefault, |
Tarandeep Singh | b360b99 | 2017-11-21 14:25:34 -0800 | [diff] [blame] | 300 | true /* supportsSwitchingToNextInputMethod */, false /* isVrOnly */); |
Yohei Yukawa | d1da115 | 2014-05-01 17:20:05 +0900 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Temporary API for creating a built-in input method for test. |
| 305 | * @hide |
| 306 | */ |
Tadashi G. Takaoka | e12a6ff | 2017-01-11 15:51:44 +0900 | [diff] [blame] | 307 | public InputMethodInfo(ResolveInfo ri, boolean isAuxIme, String settingsActivity, |
| 308 | List<InputMethodSubtype> subtypes, int isDefaultResId, boolean forceDefault, |
Tarandeep Singh | b360b99 | 2017-11-21 14:25:34 -0800 | [diff] [blame] | 309 | boolean supportsSwitchingToNextInputMethod, boolean isVrOnly) { |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 310 | final ServiceInfo si = ri.serviceInfo; |
| 311 | mService = ri; |
| 312 | mId = new ComponentName(si.packageName, si.name).flattenToShortString(); |
| 313 | mSettingsActivityName = settingsActivity; |
| 314 | mIsDefaultResId = isDefaultResId; |
| 315 | mIsAuxIme = isAuxIme; |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 316 | mSubtypes = new InputMethodSubtypeArray(subtypes); |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 317 | mForceDefault = forceDefault; |
Yohei Yukawa | d1da115 | 2014-05-01 17:20:05 +0900 | [diff] [blame] | 318 | mSupportsSwitchingToNextInputMethod = supportsSwitchingToNextInputMethod; |
Tarandeep Singh | b360b99 | 2017-11-21 14:25:34 -0800 | [diff] [blame] | 319 | mIsVrOnly = isVrOnly; |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | private static ResolveInfo buildDummyResolveInfo(String packageName, String className, |
| 323 | CharSequence label) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | ResolveInfo ri = new ResolveInfo(); |
| 325 | ServiceInfo si = new ServiceInfo(); |
| 326 | ApplicationInfo ai = new ApplicationInfo(); |
| 327 | ai.packageName = packageName; |
| 328 | ai.enabled = true; |
| 329 | si.applicationInfo = ai; |
| 330 | si.enabled = true; |
| 331 | si.packageName = packageName; |
| 332 | si.name = className; |
| 333 | si.exported = true; |
| 334 | si.nonLocalizedLabel = label; |
| 335 | ri.serviceInfo = si; |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 336 | return ri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 338 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | /** |
| 340 | * Return a unique ID for this input method. The ID is generated from |
| 341 | * the package and class name implementing the method. |
| 342 | */ |
| 343 | public String getId() { |
| 344 | return mId; |
| 345 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 346 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | /** |
| 348 | * Return the .apk package that implements this input method. |
| 349 | */ |
| 350 | public String getPackageName() { |
| 351 | return mService.serviceInfo.packageName; |
| 352 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 353 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | /** |
| 355 | * Return the class name of the service component that implements |
| 356 | * this input method. |
| 357 | */ |
| 358 | public String getServiceName() { |
| 359 | return mService.serviceInfo.name; |
| 360 | } |
| 361 | |
| 362 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 363 | * Return the raw information about the Service implementing this |
| 364 | * input method. Do not modify the returned object. |
| 365 | */ |
| 366 | public ServiceInfo getServiceInfo() { |
| 367 | return mService.serviceInfo; |
| 368 | } |
| 369 | |
| 370 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | * Return the component of the service that implements this input |
| 372 | * method. |
| 373 | */ |
| 374 | public ComponentName getComponent() { |
| 375 | return new ComponentName(mService.serviceInfo.packageName, |
| 376 | mService.serviceInfo.name); |
| 377 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 378 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | /** |
| 380 | * Load the user-displayed label for this input method. |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 381 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | * @param pm Supply a PackageManager used to load the input method's |
| 383 | * resources. |
| 384 | */ |
| 385 | public CharSequence loadLabel(PackageManager pm) { |
| 386 | return mService.loadLabel(pm); |
| 387 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 388 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | /** |
| 390 | * Load the user-displayed icon for this input method. |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 391 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | * @param pm Supply a PackageManager used to load the input method's |
| 393 | * resources. |
| 394 | */ |
| 395 | public Drawable loadIcon(PackageManager pm) { |
| 396 | return mService.loadIcon(pm); |
| 397 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 398 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | /** |
| 400 | * Return the class name of an activity that provides a settings UI for |
| 401 | * the input method. You can launch this activity be starting it with |
| 402 | * an {@link android.content.Intent} whose action is MAIN and with an |
| 403 | * explicit {@link android.content.ComponentName} |
| 404 | * composed of {@link #getPackageName} and the class name returned here. |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 405 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | * <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] | 407 | * with the input method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | */ |
| 409 | public String getSettingsActivity() { |
| 410 | return mSettingsActivityName; |
| 411 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 412 | |
| 413 | /** |
Tarandeep Singh | b360b99 | 2017-11-21 14:25:34 -0800 | [diff] [blame] | 414 | * Returns true if IME supports VR mode only. |
| 415 | * @hide |
| 416 | */ |
| 417 | public boolean isVrOnly() { |
| 418 | return mIsVrOnly; |
| 419 | } |
| 420 | |
| 421 | /** |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 422 | * Return the count of the subtypes of Input Method. |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 423 | */ |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 424 | public int getSubtypeCount() { |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 425 | return mSubtypes.getCount(); |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Return the Input Method's subtype at the specified index. |
| 430 | * |
| 431 | * @param index the index of the subtype to return. |
| 432 | */ |
| 433 | public InputMethodSubtype getSubtypeAt(int index) { |
| 434 | return mSubtypes.get(index); |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 435 | } |
| 436 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | /** |
| 438 | * Return the resource identifier of a resource inside of this input |
| 439 | * method's .apk that determines whether it should be considered a |
| 440 | * default input method for the system. |
| 441 | */ |
| 442 | public int getIsDefaultResourceId() { |
| 443 | return mIsDefaultResId; |
| 444 | } |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 445 | |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 446 | /** |
| 447 | * Return whether or not this ime is a default ime or not. |
| 448 | * @hide |
| 449 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 450 | @UnsupportedAppUsage |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 451 | public boolean isDefault(Context context) { |
| 452 | if (mForceDefault) { |
| 453 | return true; |
| 454 | } |
| 455 | try { |
Yohei Yukawa | dc48924 | 2014-09-14 12:01:59 +0900 | [diff] [blame] | 456 | if (getIsDefaultResourceId() == 0) { |
| 457 | return false; |
| 458 | } |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 459 | final Resources res = context.createPackageContext(getPackageName(), 0).getResources(); |
| 460 | return res.getBoolean(getIsDefaultResourceId()); |
Yohei Yukawa | fcedfa0 | 2014-09-19 13:22:15 +0900 | [diff] [blame] | 461 | } catch (NameNotFoundException | NotFoundException e) { |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 462 | return false; |
| 463 | } |
| 464 | } |
| 465 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | public void dump(Printer pw, String prefix) { |
| 467 | pw.println(prefix + "mId=" + mId |
Seigo Nonaka | d742bfd | 2015-10-14 19:52:32 +0900 | [diff] [blame] | 468 | + " mSettingsActivityName=" + mSettingsActivityName |
Tarandeep Singh | b360b99 | 2017-11-21 14:25:34 -0800 | [diff] [blame] | 469 | + " mIsVrOnly=" + mIsVrOnly |
Tadashi G. Takaoka | 4669967 | 2017-03-31 12:17:20 +0900 | [diff] [blame] | 470 | + " mSupportsSwitchingToNextInputMethod=" + mSupportsSwitchingToNextInputMethod); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | pw.println(prefix + "mIsDefaultResId=0x" |
| 472 | + Integer.toHexString(mIsDefaultResId)); |
| 473 | pw.println(prefix + "Service:"); |
| 474 | mService.dump(pw, prefix + " "); |
| 475 | } |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 476 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | @Override |
| 478 | public String toString() { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 479 | return "InputMethodInfo{" + mId |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | + ", settings: " |
| 481 | + mSettingsActivityName + "}"; |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Used to test whether the given parameter object is an |
| 486 | * {@link InputMethodInfo} and its Id is the same to this one. |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 487 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | * @return true if the given parameter object is an |
| 489 | * {@link InputMethodInfo} and its Id is the same to this one. |
| 490 | */ |
| 491 | @Override |
| 492 | public boolean equals(Object o) { |
| 493 | if (o == this) return true; |
| 494 | if (o == null) return false; |
| 495 | |
| 496 | if (!(o instanceof InputMethodInfo)) return false; |
| 497 | |
| 498 | InputMethodInfo obj = (InputMethodInfo) o; |
| 499 | return mId.equals(obj.mId); |
| 500 | } |
satok | c50232d | 2011-03-25 13:52:48 +0900 | [diff] [blame] | 501 | |
| 502 | @Override |
| 503 | public int hashCode() { |
| 504 | return mId.hashCode(); |
| 505 | } |
| 506 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | /** |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 508 | * @hide |
Yohei Yukawa | fd70fe8 | 2018-04-08 12:19:56 -0700 | [diff] [blame] | 509 | * @return {@code true} if the IME is a trusted system component (e.g. pre-installed) |
| 510 | */ |
| 511 | public boolean isSystem() { |
| 512 | return (mService.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * @hide |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 517 | */ |
| 518 | public boolean isAuxiliaryIme() { |
| 519 | return mIsAuxIme; |
| 520 | } |
| 521 | |
| 522 | /** |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 523 | * @return true if this input method supports ways to switch to a next input method. |
| 524 | * @hide |
| 525 | */ |
| 526 | public boolean supportsSwitchingToNextInputMethod() { |
| 527 | return mSupportsSwitchingToNextInputMethod; |
| 528 | } |
| 529 | |
| 530 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | * Used to package this object into a {@link Parcel}. |
Yohei Yukawa | 66996da | 2014-02-24 19:50:40 +0900 | [diff] [blame] | 532 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | * @param dest The {@link Parcel} to be written. |
| 534 | * @param flags The flags used for parceling. |
| 535 | */ |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 536 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | public void writeToParcel(Parcel dest, int flags) { |
| 538 | dest.writeString(mId); |
| 539 | dest.writeString(mSettingsActivityName); |
| 540 | dest.writeInt(mIsDefaultResId); |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 541 | dest.writeInt(mIsAuxIme ? 1 : 0); |
Satoshi Kataoka | 69da269 | 2013-08-15 15:36:44 +0900 | [diff] [blame] | 542 | dest.writeInt(mSupportsSwitchingToNextInputMethod ? 1 : 0); |
Tarandeep Singh | b360b99 | 2017-11-21 14:25:34 -0800 | [diff] [blame] | 543 | dest.writeBoolean(mIsVrOnly); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | mService.writeToParcel(dest, flags); |
Yohei Yukawa | f065695 | 2014-03-03 15:38:18 +0900 | [diff] [blame] | 545 | mSubtypes.writeToParcel(dest); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /** |
| 549 | * Used to make this class parcelable. |
| 550 | */ |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 551 | public static final @android.annotation.NonNull Parcelable.Creator<InputMethodInfo> CREATOR |
satok | 7eb8425 | 2010-08-04 18:26:42 +0900 | [diff] [blame] | 552 | = new Parcelable.Creator<InputMethodInfo>() { |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 553 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | public InputMethodInfo createFromParcel(Parcel source) { |
| 555 | return new InputMethodInfo(source); |
| 556 | } |
| 557 | |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 558 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | public InputMethodInfo[] newArray(int size) { |
| 560 | return new InputMethodInfo[size]; |
| 561 | } |
| 562 | }; |
| 563 | |
satok | dbfba85 | 2011-09-05 19:19:12 +0900 | [diff] [blame] | 564 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | public int describeContents() { |
| 566 | return 0; |
| 567 | } |
| 568 | } |