InputMethodInfo.java revision c39a6e0c51e182338deb8b63d07933b585134929
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.ResolveInfo; 27import android.content.pm.ServiceInfo; 28import android.content.res.TypedArray; 29import android.content.res.XmlResourceParser; 30import android.graphics.drawable.Drawable; 31import android.os.Parcel; 32import android.os.Parcelable; 33import android.util.AttributeSet; 34import android.util.Printer; 35import android.util.Xml; 36 37import java.io.IOException; 38 39/** 40 * This class is used to specify meta information of an input method. 41 */ 42public final class InputMethodInfo implements Parcelable { 43 static final String TAG = "InputMethodMetaInfo"; 44 45 /** 46 * The Service that implements this input method component. 47 */ 48 final ResolveInfo mService; 49 50 /** 51 * The unique string Id to identify the input method. This is generated 52 * from the input method component. 53 */ 54 final String mId; 55 56 /** 57 * The input method setting activity's name, used by the system settings to 58 * launch the setting activity of this input method. 59 */ 60 final String mSettingsActivityName; 61 62 /** 63 * The resource in the input method's .apk that holds a boolean indicating 64 * whether it should be considered the default input method for this 65 * system. This is a resource ID instead of the final value so that it 66 * can change based on the configuration (in particular locale). 67 */ 68 final int mIsDefaultResId; 69 70 /** 71 * Constructor. 72 * 73 * @param context The Context in which we are parsing the input method. 74 * @param service The ResolveInfo returned from the package manager about 75 * this input method's component. 76 */ 77 public InputMethodInfo(Context context, ResolveInfo service) 78 throws XmlPullParserException, IOException { 79 mService = service; 80 ServiceInfo si = service.serviceInfo; 81 mId = new ComponentName(si.packageName, si.name).flattenToShortString(); 82 83 PackageManager pm = context.getPackageManager(); 84 String settingsActivityComponent = null; 85 int isDefaultResId = 0; 86 87 XmlResourceParser parser = null; 88 try { 89 parser = si.loadXmlMetaData(pm, InputMethod.SERVICE_META_DATA); 90 if (parser == null) { 91 throw new XmlPullParserException("No " 92 + InputMethod.SERVICE_META_DATA + " meta-data"); 93 } 94 95 AttributeSet attrs = Xml.asAttributeSet(parser); 96 97 int type; 98 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 99 && type != XmlPullParser.START_TAG) { 100 } 101 102 String nodeName = parser.getName(); 103 if (!"input-method".equals(nodeName)) { 104 throw new XmlPullParserException( 105 "Meta-data does not start with input-method tag"); 106 } 107 108 TypedArray sa = context.getResources().obtainAttributes(attrs, 109 com.android.internal.R.styleable.InputMethod); 110 settingsActivityComponent = sa.getString( 111 com.android.internal.R.styleable.InputMethod_settingsActivity); 112 isDefaultResId = sa.getResourceId( 113 com.android.internal.R.styleable.InputMethod_isDefault, 0); 114 sa.recycle(); 115 } finally { 116 if (parser != null) parser.close(); 117 } 118 119 mSettingsActivityName = settingsActivityComponent; 120 mIsDefaultResId = isDefaultResId; 121 } 122 123 InputMethodInfo(Parcel source) { 124 mId = source.readString(); 125 mSettingsActivityName = source.readString(); 126 mIsDefaultResId = source.readInt(); 127 mService = ResolveInfo.CREATOR.createFromParcel(source); 128 } 129 130 /** 131 * Temporary API for creating a built-in input method. 132 */ 133 public InputMethodInfo(String packageName, String className, 134 CharSequence label, String settingsActivity) { 135 ResolveInfo ri = new ResolveInfo(); 136 ServiceInfo si = new ServiceInfo(); 137 ApplicationInfo ai = new ApplicationInfo(); 138 ai.packageName = packageName; 139 ai.enabled = true; 140 si.applicationInfo = ai; 141 si.enabled = true; 142 si.packageName = packageName; 143 si.name = className; 144 si.exported = true; 145 si.nonLocalizedLabel = label; 146 ri.serviceInfo = si; 147 mService = ri; 148 mId = new ComponentName(si.packageName, si.name).flattenToShortString(); 149 mSettingsActivityName = settingsActivity; 150 mIsDefaultResId = 0; 151 } 152 153 /** 154 * Return a unique ID for this input method. The ID is generated from 155 * the package and class name implementing the method. 156 */ 157 public String getId() { 158 return mId; 159 } 160 161 /** 162 * Return the .apk package that implements this input method. 163 */ 164 public String getPackageName() { 165 return mService.serviceInfo.packageName; 166 } 167 168 /** 169 * Return the class name of the service component that implements 170 * this input method. 171 */ 172 public String getServiceName() { 173 return mService.serviceInfo.name; 174 } 175 176 /** 177 * Return the raw information about the Service implementing this 178 * input method. Do not modify the returned object. 179 */ 180 public ServiceInfo getServiceInfo() { 181 return mService.serviceInfo; 182 } 183 184 /** 185 * Return the component of the service that implements this input 186 * method. 187 */ 188 public ComponentName getComponent() { 189 return new ComponentName(mService.serviceInfo.packageName, 190 mService.serviceInfo.name); 191 } 192 193 /** 194 * Load the user-displayed label for this input method. 195 * 196 * @param pm Supply a PackageManager used to load the input method's 197 * resources. 198 */ 199 public CharSequence loadLabel(PackageManager pm) { 200 return mService.loadLabel(pm); 201 } 202 203 /** 204 * Load the user-displayed icon for this input method. 205 * 206 * @param pm Supply a PackageManager used to load the input method's 207 * resources. 208 */ 209 public Drawable loadIcon(PackageManager pm) { 210 return mService.loadIcon(pm); 211 } 212 213 /** 214 * Return the class name of an activity that provides a settings UI for 215 * the input method. You can launch this activity be starting it with 216 * an {@link android.content.Intent} whose action is MAIN and with an 217 * explicit {@link android.content.ComponentName} 218 * composed of {@link #getPackageName} and the class name returned here. 219 * 220 * <p>A null will be returned if there is no settings activity associated 221 * with the input method. 222 */ 223 public String getSettingsActivity() { 224 return mSettingsActivityName; 225 } 226 227 /** 228 * Return the resource identifier of a resource inside of this input 229 * method's .apk that determines whether it should be considered a 230 * default input method for the system. 231 */ 232 public int getIsDefaultResourceId() { 233 return mIsDefaultResId; 234 } 235 236 public void dump(Printer pw, String prefix) { 237 pw.println(prefix + "mId=" + mId 238 + " mSettingsActivityName=" + mSettingsActivityName); 239 pw.println(prefix + "mIsDefaultResId=0x" 240 + Integer.toHexString(mIsDefaultResId)); 241 pw.println(prefix + "Service:"); 242 mService.dump(pw, prefix + " "); 243 } 244 245 @Override 246 public String toString() { 247 return "InputMethodMetaInfo{" + mId 248 + ", settings: " 249 + mSettingsActivityName + "}"; 250 } 251 252 /** 253 * Used to test whether the given parameter object is an 254 * {@link InputMethodInfo} and its Id is the same to this one. 255 * 256 * @return true if the given parameter object is an 257 * {@link InputMethodInfo} and its Id is the same to this one. 258 */ 259 @Override 260 public boolean equals(Object o) { 261 if (o == this) return true; 262 if (o == null) return false; 263 264 if (!(o instanceof InputMethodInfo)) return false; 265 266 InputMethodInfo obj = (InputMethodInfo) o; 267 return mId.equals(obj.mId); 268 } 269 270 /** 271 * Used to package this object into a {@link Parcel}. 272 * 273 * @param dest The {@link Parcel} to be written. 274 * @param flags The flags used for parceling. 275 */ 276 public void writeToParcel(Parcel dest, int flags) { 277 dest.writeString(mId); 278 dest.writeString(mSettingsActivityName); 279 dest.writeInt(mIsDefaultResId); 280 mService.writeToParcel(dest, flags); 281 } 282 283 /** 284 * Used to make this class parcelable. 285 */ 286 public static final Parcelable.Creator<InputMethodInfo> CREATOR = new Parcelable.Creator<InputMethodInfo>() { 287 public InputMethodInfo createFromParcel(Parcel source) { 288 return new InputMethodInfo(source); 289 } 290 291 public InputMethodInfo[] newArray(int size) { 292 return new InputMethodInfo[size]; 293 } 294 }; 295 296 public int describeContents() { 297 return 0; 298 } 299} 300