| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view.textservice; |
| 18 |
|
| 19 |
import android.content.ComponentName; |
| 20 |
import android.content.Context; |
| 21 |
import android.content.pm.PackageManager; |
| 22 |
import android.content.pm.ResolveInfo; |
| 23 |
import android.content.pm.ServiceInfo; |
| 24 |
import android.content.res.Resources; |
| 25 |
import android.content.res.TypedArray; |
| 26 |
import android.content.res.XmlResourceParser; |
| 27 |
import android.graphics.drawable.Drawable; |
| 28 |
import android.os.Parcel; |
| 29 |
import android.os.Parcelable; |
| 30 |
import android.util.AttributeSet; |
| 31 |
import android.util.PrintWriterPrinter; |
| 32 |
import android.util.Slog; |
| 33 |
import android.util.Xml; |
| 34 |
|
| 35 |
import org.xmlpull.v1.XmlPullParser; |
| 36 |
import org.xmlpull.v1.XmlPullParserException; |
| 37 |
|
| 38 |
import java.io.IOException; |
| 39 |
import java.io.PrintWriter; |
| 40 |
import java.util.ArrayList; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public final class SpellCheckerInfo implements Parcelable { |
| 46 |
private static final String TAG = SpellCheckerInfo.class.getSimpleName(); |
| 47 |
private final ResolveInfo mService; |
| 48 |
private final String mId; |
| 49 |
private final int mLabel; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private final String mSettingsActivityName; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private final ArrayList<SpellCheckerSubtype> mSubtypes = new ArrayList<>(); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public SpellCheckerInfo(Context context, ResolveInfo service) |
| 67 |
throws XmlPullParserException, IOException { |
| 68 |
mService = service; |
| 69 |
ServiceInfo si = service.serviceInfo; |
| 70 |
mId = new ComponentName(si.packageName, si.name).flattenToShortString(); |
| 71 |
|
| 72 |
final PackageManager pm = context.getPackageManager(); |
| 73 |
int label = 0; |
| 74 |
String settingsActivityComponent = null; |
| 75 |
|
| 76 |
XmlResourceParser parser = null; |
| 77 |
try { |
| 78 |
parser = si.loadXmlMetaData(pm, SpellCheckerSession.SERVICE_META_DATA); |
| 79 |
if (parser == null) { |
| 80 |
throw new XmlPullParserException("No " |
| 81 |
+ SpellCheckerSession.SERVICE_META_DATA + " meta-data"); |
| 82 |
} |
| 83 |
|
| 84 |
final Resources res = pm.getResourcesForApplication(si.applicationInfo); |
| 85 |
final AttributeSet attrs = Xml.asAttributeSet(parser); |
| 86 |
int type; |
| 87 |
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 88 |
&& type != XmlPullParser.START_TAG) { |
| 89 |
} |
| 90 |
|
| 91 |
final String nodeName = parser.getName(); |
| 92 |
if (!"spell-checker".equals(nodeName)) { |
| 93 |
throw new XmlPullParserException( |
| 94 |
"Meta-data does not start with spell-checker tag"); |
| 95 |
} |
| 96 |
|
| 97 |
TypedArray sa = res.obtainAttributes(attrs, |
| 98 |
com.android.internal.R.styleable.SpellChecker); |
| 99 |
label = sa.getResourceId(com.android.internal.R.styleable.SpellChecker_label, 0); |
| 100 |
settingsActivityComponent = sa.getString( |
| 101 |
com.android.internal.R.styleable.SpellChecker_settingsActivity); |
| 102 |
sa.recycle(); |
| 103 |
|
| 104 |
final int depth = parser.getDepth(); |
| 105 |
|
| 106 |
while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) |
| 107 |
&& type != XmlPullParser.END_DOCUMENT) { |
| 108 |
if (type == XmlPullParser.START_TAG) { |
| 109 |
final String subtypeNodeName = parser.getName(); |
| 110 |
if (!"subtype".equals(subtypeNodeName)) { |
| 111 |
throw new XmlPullParserException( |
| 112 |
"Meta-data in spell-checker does not start with subtype tag"); |
| 113 |
} |
| 114 |
final TypedArray a = res.obtainAttributes( |
| 115 |
attrs, com.android.internal.R.styleable.SpellChecker_Subtype); |
| 116 |
SpellCheckerSubtype subtype = new SpellCheckerSubtype( |
| 117 |
a.getResourceId(com.android.internal.R.styleable |
| 118 |
.SpellChecker_Subtype_label, 0), |
| 119 |
a.getString(com.android.internal.R.styleable |
| 120 |
.SpellChecker_Subtype_subtypeLocale), |
| 121 |
a.getString(com.android.internal.R.styleable |
| 122 |
.SpellChecker_Subtype_languageTag), |
| 123 |
a.getString(com.android.internal.R.styleable |
| 124 |
.SpellChecker_Subtype_subtypeExtraValue), |
| 125 |
a.getInt(com.android.internal.R.styleable |
| 126 |
.SpellChecker_Subtype_subtypeId, 0)); |
| 127 |
a.recycle(); |
| 128 |
mSubtypes.add(subtype); |
| 129 |
} |
| 130 |
} |
| 131 |
} catch (Exception e) { |
| 132 |
Slog.e(TAG, "Caught exception: " + e); |
| 133 |
throw new XmlPullParserException( |
| 134 |
"Unable to create context for: " + si.packageName); |
| 135 |
} finally { |
| 136 |
if (parser != null) parser.close(); |
| 137 |
} |
| 138 |
mLabel = label; |
| 139 |
mSettingsActivityName = settingsActivityComponent; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public SpellCheckerInfo(Parcel source) { |
| 147 |
mLabel = source.readInt(); |
| 148 |
mId = source.readString(); |
| 149 |
mSettingsActivityName = source.readString(); |
| 150 |
mService = ResolveInfo.CREATOR.createFromParcel(source); |
| 151 |
source.readTypedList(mSubtypes, SpellCheckerSubtype.CREATOR); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public String getId() { |
| 159 |
return mId; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public ComponentName getComponent() { |
| 166 |
return new ComponentName( |
| 167 |
mService.serviceInfo.packageName, mService.serviceInfo.name); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public String getPackageName() { |
| 174 |
return mService.serviceInfo.packageName; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
@Override |
| 184 |
public void writeToParcel(Parcel dest, int flags) { |
| 185 |
dest.writeInt(mLabel); |
| 186 |
dest.writeString(mId); |
| 187 |
dest.writeString(mSettingsActivityName); |
| 188 |
mService.writeToParcel(dest, flags); |
| 189 |
dest.writeTypedList(mSubtypes); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public static final @android.annotation.NonNull Parcelable.Creator<SpellCheckerInfo> CREATOR |
| 197 |
= new Parcelable.Creator<SpellCheckerInfo>() { |
| 198 |
@Override |
| 199 |
public SpellCheckerInfo createFromParcel(Parcel source) { |
| 200 |
return new SpellCheckerInfo(source); |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public SpellCheckerInfo[] newArray(int size) { |
| 205 |
return new SpellCheckerInfo[size]; |
| 206 |
} |
| 207 |
}; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public CharSequence loadLabel(PackageManager pm) { |
| 215 |
if (mLabel == 0 || pm == null) return ""; |
| 216 |
return pm.getText(getPackageName(), mLabel, mService.serviceInfo.applicationInfo); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public Drawable loadIcon(PackageManager pm) { |
| 225 |
return mService.loadIcon(pm); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public ServiceInfo getServiceInfo() { |
| 234 |
return mService.serviceInfo; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public String getSettingsActivity() { |
| 247 |
return mSettingsActivityName; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public int getSubtypeCount() { |
| 254 |
return mSubtypes.size(); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public SpellCheckerSubtype getSubtypeAt(int index) { |
| 263 |
return mSubtypes.get(index); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
@Override |
| 270 |
public int describeContents() { |
| 271 |
return 0; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public void dump(final PrintWriter pw, final String prefix) { |
| 278 |
pw.println(prefix + "mId=" + mId); |
| 279 |
pw.println(prefix + "mSettingsActivityName=" + mSettingsActivityName); |
| 280 |
pw.println(prefix + "Service:"); |
| 281 |
mService.dump(new PrintWriterPrinter(pw), prefix + " "); |
| 282 |
final int N = getSubtypeCount(); |
| 283 |
for (int i = 0; i < N; i++) { |
| 284 |
final SpellCheckerSubtype st = getSubtypeAt(i); |
| 285 |
pw.println(prefix + " " + "Subtype #" + i + ":"); |
| 286 |
pw.println(prefix + " " + "locale=" + st.getLocale() |
| 287 |
+ " languageTag=" + st.getLanguageTag()); |
| 288 |
pw.println(prefix + " " + "extraValue=" + st.getExtraValue()); |
| 289 |
} |
| 290 |
} |
| 291 |
} |