blob: 0e2deed4d5e7e616626a4efe3da0469060ffae3b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content.pm;
18
19import org.xmlpull.v1.XmlPullParser;
20import org.xmlpull.v1.XmlPullParserException;
21
22import android.content.ComponentName;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.content.res.AssetManager;
26import android.content.res.Configuration;
27import android.content.res.Resources;
28import android.content.res.TypedArray;
29import android.content.res.XmlResourceParser;
30import android.os.Bundle;
31import android.os.PatternMatcher;
32import android.util.AttributeSet;
33import android.util.Config;
34import android.util.DisplayMetrics;
35import android.util.Log;
36import android.util.TypedValue;
37import com.android.internal.util.XmlUtils;
38
39import java.io.File;
40import java.io.IOException;
41import java.io.InputStream;
42import java.lang.ref.WeakReference;
43import java.security.cert.Certificate;
44import java.security.cert.CertificateEncodingException;
45import java.util.ArrayList;
46import java.util.Enumeration;
47import java.util.Iterator;
Mitsuru Oshima8d112672009-04-27 12:01:23 -070048import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import java.util.jar.JarEntry;
50import java.util.jar.JarFile;
51
52/**
53 * Package archive parsing
54 *
55 * {@hide}
56 */
57public class PackageParser {
Dianne Hackborna96cbb42009-05-13 15:06:13 -070058 /** @hide */
59 public static class NewPermissionInfo {
60 public final String name;
61 public final int sdkVersion;
62 public final int fileVersion;
63
64 public NewPermissionInfo(String name, int sdkVersion, int fileVersion) {
65 this.name = name;
66 this.sdkVersion = sdkVersion;
67 this.fileVersion = fileVersion;
68 }
69 }
70
71 /**
72 * List of new permissions that have been added since 1.0.
73 * NOTE: These must be declared in SDK version order, with permissions
74 * added to older SDKs appearing before those added to newer SDKs.
75 * @hide
76 */
Jaikumar Ganesh45515652009-04-23 15:20:21 -070077 public static final PackageParser.NewPermissionInfo NEW_PERMISSIONS[] =
78 new PackageParser.NewPermissionInfo[] {
San Mehat5a3a77d2009-06-01 09:25:28 -070079 new PackageParser.NewPermissionInfo(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
Jaikumar Ganesh45515652009-04-23 15:20:21 -070080 android.os.Build.VERSION_CODES.DONUT, 0),
81 new PackageParser.NewPermissionInfo(android.Manifest.permission.READ_PHONE_STATE,
82 android.os.Build.VERSION_CODES.DONUT, 0)
Dianne Hackborna96cbb42009-05-13 15:06:13 -070083 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
85 private String mArchiveSourcePath;
86 private String[] mSeparateProcesses;
87 private int mSdkVersion;
Dianne Hackborn851a5412009-05-08 12:06:44 -070088 private String mSdkCodename;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90 private int mParseError = PackageManager.INSTALL_SUCCEEDED;
91
92 private static final Object mSync = new Object();
93 private static WeakReference<byte[]> mReadBuffer;
94
Dianne Hackborn1d442e02009-04-20 18:14:05 -070095 static class ParsePackageItemArgs {
96 final Package owner;
97 final String[] outError;
98 final int nameRes;
99 final int labelRes;
100 final int iconRes;
101
102 String tag;
103 TypedArray sa;
104
105 ParsePackageItemArgs(Package _owner, String[] _outError,
106 int _nameRes, int _labelRes, int _iconRes) {
107 owner = _owner;
108 outError = _outError;
109 nameRes = _nameRes;
110 labelRes = _labelRes;
111 iconRes = _iconRes;
112 }
113 }
114
115 static class ParseComponentArgs extends ParsePackageItemArgs {
116 final String[] sepProcesses;
117 final int processRes;
118 final int enabledRes;
119 int flags;
120
121 ParseComponentArgs(Package _owner, String[] _outError,
122 int _nameRes, int _labelRes, int _iconRes,
123 String[] _sepProcesses, int _processRes,int _enabledRes) {
124 super(_owner, _outError, _nameRes, _labelRes, _iconRes);
125 sepProcesses = _sepProcesses;
126 processRes = _processRes;
127 enabledRes = _enabledRes;
128 }
129 }
130
131 private ParsePackageItemArgs mParseInstrumentationArgs;
132 private ParseComponentArgs mParseActivityArgs;
133 private ParseComponentArgs mParseActivityAliasArgs;
134 private ParseComponentArgs mParseServiceArgs;
135 private ParseComponentArgs mParseProviderArgs;
136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 /** If set to true, we will only allow package files that exactly match
138 * the DTD. Otherwise, we try to get as much from the package as we
139 * can without failing. This should normally be set to false, to
140 * support extensions to the DTD in future versions. */
141 private static final boolean RIGID_PARSER = false;
142
143 private static final String TAG = "PackageParser";
144
145 public PackageParser(String archiveSourcePath) {
146 mArchiveSourcePath = archiveSourcePath;
147 }
148
149 public void setSeparateProcesses(String[] procs) {
150 mSeparateProcesses = procs;
151 }
152
Dianne Hackborn851a5412009-05-08 12:06:44 -0700153 public void setSdkVersion(int sdkVersion, String codename) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 mSdkVersion = sdkVersion;
Dianne Hackborn851a5412009-05-08 12:06:44 -0700155 mSdkCodename = codename;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 }
157
158 private static final boolean isPackageFilename(String name) {
159 return name.endsWith(".apk");
160 }
161
162 /**
163 * Generate and return the {@link PackageInfo} for a parsed package.
164 *
165 * @param p the parsed package.
166 * @param flags indicating which optional information is included.
167 */
168 public static PackageInfo generatePackageInfo(PackageParser.Package p,
169 int gids[], int flags) {
170
171 PackageInfo pi = new PackageInfo();
172 pi.packageName = p.packageName;
173 pi.versionCode = p.mVersionCode;
174 pi.versionName = p.mVersionName;
175 pi.sharedUserId = p.mSharedUserId;
176 pi.sharedUserLabel = p.mSharedUserLabel;
177 pi.applicationInfo = p.applicationInfo;
178 if ((flags&PackageManager.GET_GIDS) != 0) {
179 pi.gids = gids;
180 }
181 if ((flags&PackageManager.GET_CONFIGURATIONS) != 0) {
182 int N = p.configPreferences.size();
183 if (N > 0) {
184 pi.configPreferences = new ConfigurationInfo[N];
185 for (int i=0; i<N; i++) {
186 pi.configPreferences[i] = p.configPreferences.get(i);
187 }
188 }
189 }
190 if ((flags&PackageManager.GET_ACTIVITIES) != 0) {
191 int N = p.activities.size();
192 if (N > 0) {
193 pi.activities = new ActivityInfo[N];
194 for (int i=0; i<N; i++) {
195 final Activity activity = p.activities.get(i);
196 if (activity.info.enabled
197 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
198 pi.activities[i] = generateActivityInfo(p.activities.get(i), flags);
199 }
200 }
201 }
202 }
203 if ((flags&PackageManager.GET_RECEIVERS) != 0) {
204 int N = p.receivers.size();
205 if (N > 0) {
206 pi.receivers = new ActivityInfo[N];
207 for (int i=0; i<N; i++) {
208 final Activity activity = p.receivers.get(i);
209 if (activity.info.enabled
210 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
211 pi.receivers[i] = generateActivityInfo(p.receivers.get(i), flags);
212 }
213 }
214 }
215 }
216 if ((flags&PackageManager.GET_SERVICES) != 0) {
217 int N = p.services.size();
218 if (N > 0) {
219 pi.services = new ServiceInfo[N];
220 for (int i=0; i<N; i++) {
221 final Service service = p.services.get(i);
222 if (service.info.enabled
223 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
224 pi.services[i] = generateServiceInfo(p.services.get(i), flags);
225 }
226 }
227 }
228 }
229 if ((flags&PackageManager.GET_PROVIDERS) != 0) {
230 int N = p.providers.size();
231 if (N > 0) {
232 pi.providers = new ProviderInfo[N];
233 for (int i=0; i<N; i++) {
234 final Provider provider = p.providers.get(i);
235 if (provider.info.enabled
236 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
237 pi.providers[i] = generateProviderInfo(p.providers.get(i), flags);
238 }
239 }
240 }
241 }
242 if ((flags&PackageManager.GET_INSTRUMENTATION) != 0) {
243 int N = p.instrumentation.size();
244 if (N > 0) {
245 pi.instrumentation = new InstrumentationInfo[N];
246 for (int i=0; i<N; i++) {
247 pi.instrumentation[i] = generateInstrumentationInfo(
248 p.instrumentation.get(i), flags);
249 }
250 }
251 }
252 if ((flags&PackageManager.GET_PERMISSIONS) != 0) {
253 int N = p.permissions.size();
254 if (N > 0) {
255 pi.permissions = new PermissionInfo[N];
256 for (int i=0; i<N; i++) {
257 pi.permissions[i] = generatePermissionInfo(p.permissions.get(i), flags);
258 }
259 }
260 N = p.requestedPermissions.size();
261 if (N > 0) {
262 pi.requestedPermissions = new String[N];
263 for (int i=0; i<N; i++) {
264 pi.requestedPermissions[i] = p.requestedPermissions.get(i);
265 }
266 }
267 }
268 if ((flags&PackageManager.GET_SIGNATURES) != 0) {
269 int N = p.mSignatures.length;
270 if (N > 0) {
271 pi.signatures = new Signature[N];
272 System.arraycopy(p.mSignatures, 0, pi.signatures, 0, N);
273 }
274 }
275 return pi;
276 }
277
278 private Certificate[] loadCertificates(JarFile jarFile, JarEntry je,
279 byte[] readBuffer) {
280 try {
281 // We must read the stream for the JarEntry to retrieve
282 // its certificates.
283 InputStream is = jarFile.getInputStream(je);
284 while (is.read(readBuffer, 0, readBuffer.length) != -1) {
285 // not using
286 }
287 is.close();
288 return je != null ? je.getCertificates() : null;
289 } catch (IOException e) {
290 Log.w(TAG, "Exception reading " + je.getName() + " in "
291 + jarFile.getName(), e);
292 }
293 return null;
294 }
295
296 public final static int PARSE_IS_SYSTEM = 0x0001;
297 public final static int PARSE_CHATTY = 0x0002;
298 public final static int PARSE_MUST_BE_APK = 0x0004;
299 public final static int PARSE_IGNORE_PROCESSES = 0x0008;
300
301 public int getParseError() {
302 return mParseError;
303 }
304
305 public Package parsePackage(File sourceFile, String destFileName,
306 DisplayMetrics metrics, int flags) {
307 mParseError = PackageManager.INSTALL_SUCCEEDED;
308
309 mArchiveSourcePath = sourceFile.getPath();
310 if (!sourceFile.isFile()) {
311 Log.w(TAG, "Skipping dir: " + mArchiveSourcePath);
312 mParseError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK;
313 return null;
314 }
315 if (!isPackageFilename(sourceFile.getName())
316 && (flags&PARSE_MUST_BE_APK) != 0) {
317 if ((flags&PARSE_IS_SYSTEM) == 0) {
318 // We expect to have non-.apk files in the system dir,
319 // so don't warn about them.
320 Log.w(TAG, "Skipping non-package file: " + mArchiveSourcePath);
321 }
322 mParseError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK;
323 return null;
324 }
325
326 if ((flags&PARSE_CHATTY) != 0 && Config.LOGD) Log.d(
327 TAG, "Scanning package: " + mArchiveSourcePath);
328
329 XmlResourceParser parser = null;
330 AssetManager assmgr = null;
331 boolean assetError = true;
332 try {
333 assmgr = new AssetManager();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700334 int cookie = assmgr.addAssetPath(mArchiveSourcePath);
335 if(cookie != 0) {
336 parser = assmgr.openXmlResourceParser(cookie, "AndroidManifest.xml");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 assetError = false;
338 } else {
339 Log.w(TAG, "Failed adding asset path:"+mArchiveSourcePath);
340 }
341 } catch (Exception e) {
342 Log.w(TAG, "Unable to read AndroidManifest.xml of "
343 + mArchiveSourcePath, e);
344 }
345 if(assetError) {
346 if (assmgr != null) assmgr.close();
347 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST;
348 return null;
349 }
350 String[] errorText = new String[1];
351 Package pkg = null;
352 Exception errorException = null;
353 try {
354 // XXXX todo: need to figure out correct configuration.
355 Resources res = new Resources(assmgr, metrics, null);
356 pkg = parsePackage(res, parser, flags, errorText);
357 } catch (Exception e) {
358 errorException = e;
359 mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
360 }
361
362
363 if (pkg == null) {
364 if (errorException != null) {
365 Log.w(TAG, mArchiveSourcePath, errorException);
366 } else {
367 Log.w(TAG, mArchiveSourcePath + " (at "
368 + parser.getPositionDescription()
369 + "): " + errorText[0]);
370 }
371 parser.close();
372 assmgr.close();
373 if (mParseError == PackageManager.INSTALL_SUCCEEDED) {
374 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
375 }
376 return null;
377 }
378
379 parser.close();
380 assmgr.close();
381
382 pkg.applicationInfo.sourceDir = destFileName;
383 pkg.applicationInfo.publicSourceDir = destFileName;
384 pkg.mSignatures = null;
385
386 return pkg;
387 }
388
389 public boolean collectCertificates(Package pkg, int flags) {
390 pkg.mSignatures = null;
391
392 WeakReference<byte[]> readBufferRef;
393 byte[] readBuffer = null;
394 synchronized (mSync) {
395 readBufferRef = mReadBuffer;
396 if (readBufferRef != null) {
397 mReadBuffer = null;
398 readBuffer = readBufferRef.get();
399 }
400 if (readBuffer == null) {
401 readBuffer = new byte[8192];
402 readBufferRef = new WeakReference<byte[]>(readBuffer);
403 }
404 }
405
406 try {
407 JarFile jarFile = new JarFile(mArchiveSourcePath);
408
409 Certificate[] certs = null;
410
411 if ((flags&PARSE_IS_SYSTEM) != 0) {
412 // If this package comes from the system image, then we
413 // can trust it... we'll just use the AndroidManifest.xml
414 // to retrieve its signatures, not validating all of the
415 // files.
416 JarEntry jarEntry = jarFile.getJarEntry("AndroidManifest.xml");
417 certs = loadCertificates(jarFile, jarEntry, readBuffer);
418 if (certs == null) {
419 Log.e(TAG, "Package " + pkg.packageName
420 + " has no certificates at entry "
421 + jarEntry.getName() + "; ignoring!");
422 jarFile.close();
423 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
424 return false;
425 }
426 if (false) {
427 Log.i(TAG, "File " + mArchiveSourcePath + ": entry=" + jarEntry
428 + " certs=" + (certs != null ? certs.length : 0));
429 if (certs != null) {
430 final int N = certs.length;
431 for (int i=0; i<N; i++) {
432 Log.i(TAG, " Public key: "
433 + certs[i].getPublicKey().getEncoded()
434 + " " + certs[i].getPublicKey());
435 }
436 }
437 }
438
439 } else {
440 Enumeration entries = jarFile.entries();
441 while (entries.hasMoreElements()) {
442 JarEntry je = (JarEntry)entries.nextElement();
443 if (je.isDirectory()) continue;
444 if (je.getName().startsWith("META-INF/")) continue;
445 Certificate[] localCerts = loadCertificates(jarFile, je,
446 readBuffer);
447 if (false) {
448 Log.i(TAG, "File " + mArchiveSourcePath + " entry " + je.getName()
449 + ": certs=" + certs + " ("
450 + (certs != null ? certs.length : 0) + ")");
451 }
452 if (localCerts == null) {
453 Log.e(TAG, "Package " + pkg.packageName
454 + " has no certificates at entry "
455 + je.getName() + "; ignoring!");
456 jarFile.close();
457 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
458 return false;
459 } else if (certs == null) {
460 certs = localCerts;
461 } else {
462 // Ensure all certificates match.
463 for (int i=0; i<certs.length; i++) {
464 boolean found = false;
465 for (int j=0; j<localCerts.length; j++) {
466 if (certs[i] != null &&
467 certs[i].equals(localCerts[j])) {
468 found = true;
469 break;
470 }
471 }
472 if (!found || certs.length != localCerts.length) {
473 Log.e(TAG, "Package " + pkg.packageName
474 + " has mismatched certificates at entry "
475 + je.getName() + "; ignoring!");
476 jarFile.close();
477 mParseError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
478 return false;
479 }
480 }
481 }
482 }
483 }
484 jarFile.close();
485
486 synchronized (mSync) {
487 mReadBuffer = readBufferRef;
488 }
489
490 if (certs != null && certs.length > 0) {
491 final int N = certs.length;
492 pkg.mSignatures = new Signature[certs.length];
493 for (int i=0; i<N; i++) {
494 pkg.mSignatures[i] = new Signature(
495 certs[i].getEncoded());
496 }
497 } else {
498 Log.e(TAG, "Package " + pkg.packageName
499 + " has no certificates; ignoring!");
500 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
501 return false;
502 }
503 } catch (CertificateEncodingException e) {
504 Log.w(TAG, "Exception reading " + mArchiveSourcePath, e);
505 mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
506 return false;
507 } catch (IOException e) {
508 Log.w(TAG, "Exception reading " + mArchiveSourcePath, e);
509 mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
510 return false;
511 } catch (RuntimeException e) {
512 Log.w(TAG, "Exception reading " + mArchiveSourcePath, e);
513 mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
514 return false;
515 }
516
517 return true;
518 }
519
520 public static String parsePackageName(String packageFilePath, int flags) {
521 XmlResourceParser parser = null;
522 AssetManager assmgr = null;
523 try {
524 assmgr = new AssetManager();
525 int cookie = assmgr.addAssetPath(packageFilePath);
526 parser = assmgr.openXmlResourceParser(cookie, "AndroidManifest.xml");
527 } catch (Exception e) {
528 if (assmgr != null) assmgr.close();
529 Log.w(TAG, "Unable to read AndroidManifest.xml of "
530 + packageFilePath, e);
531 return null;
532 }
533 AttributeSet attrs = parser;
534 String errors[] = new String[1];
535 String packageName = null;
536 try {
537 packageName = parsePackageName(parser, attrs, flags, errors);
538 } catch (IOException e) {
539 Log.w(TAG, packageFilePath, e);
540 } catch (XmlPullParserException e) {
541 Log.w(TAG, packageFilePath, e);
542 } finally {
543 if (parser != null) parser.close();
544 if (assmgr != null) assmgr.close();
545 }
546 if (packageName == null) {
547 Log.e(TAG, "parsePackageName error: " + errors[0]);
548 return null;
549 }
550 return packageName;
551 }
552
553 private static String validateName(String name, boolean requiresSeparator) {
554 final int N = name.length();
555 boolean hasSep = false;
556 boolean front = true;
557 for (int i=0; i<N; i++) {
558 final char c = name.charAt(i);
559 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
560 front = false;
561 continue;
562 }
563 if (!front) {
564 if ((c >= '0' && c <= '9') || c == '_') {
565 continue;
566 }
567 }
568 if (c == '.') {
569 hasSep = true;
570 front = true;
571 continue;
572 }
573 return "bad character '" + c + "'";
574 }
575 return hasSep || !requiresSeparator
576 ? null : "must have at least one '.' separator";
577 }
578
579 private static String parsePackageName(XmlPullParser parser,
580 AttributeSet attrs, int flags, String[] outError)
581 throws IOException, XmlPullParserException {
582
583 int type;
584 while ((type=parser.next()) != parser.START_TAG
585 && type != parser.END_DOCUMENT) {
586 ;
587 }
588
589 if (type != parser.START_TAG) {
590 outError[0] = "No start tag found";
591 return null;
592 }
593 if ((flags&PARSE_CHATTY) != 0 && Config.LOGV) Log.v(
594 TAG, "Root element name: '" + parser.getName() + "'");
595 if (!parser.getName().equals("manifest")) {
596 outError[0] = "No <manifest> tag";
597 return null;
598 }
599 String pkgName = attrs.getAttributeValue(null, "package");
600 if (pkgName == null || pkgName.length() == 0) {
601 outError[0] = "<manifest> does not specify package";
602 return null;
603 }
604 String nameError = validateName(pkgName, true);
605 if (nameError != null && !"android".equals(pkgName)) {
606 outError[0] = "<manifest> specifies bad package name \""
607 + pkgName + "\": " + nameError;
608 return null;
609 }
610
611 return pkgName.intern();
612 }
613
614 /**
615 * Temporary.
616 */
617 static public Signature stringToSignature(String str) {
618 final int N = str.length();
619 byte[] sig = new byte[N];
620 for (int i=0; i<N; i++) {
621 sig[i] = (byte)str.charAt(i);
622 }
623 return new Signature(sig);
624 }
625
626 private Package parsePackage(
627 Resources res, XmlResourceParser parser, int flags, String[] outError)
628 throws XmlPullParserException, IOException {
629 AttributeSet attrs = parser;
630
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700631 mParseInstrumentationArgs = null;
632 mParseActivityArgs = null;
633 mParseServiceArgs = null;
634 mParseProviderArgs = null;
635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 String pkgName = parsePackageName(parser, attrs, flags, outError);
637 if (pkgName == null) {
638 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME;
639 return null;
640 }
641 int type;
642
643 final Package pkg = new Package(pkgName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 boolean foundApp = false;
Dianne Hackborn851a5412009-05-08 12:06:44 -0700645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 TypedArray sa = res.obtainAttributes(attrs,
647 com.android.internal.R.styleable.AndroidManifest);
648 pkg.mVersionCode = sa.getInteger(
649 com.android.internal.R.styleable.AndroidManifest_versionCode, 0);
650 pkg.mVersionName = sa.getNonResourceString(
651 com.android.internal.R.styleable.AndroidManifest_versionName);
652 if (pkg.mVersionName != null) {
653 pkg.mVersionName = pkg.mVersionName.intern();
654 }
655 String str = sa.getNonResourceString(
656 com.android.internal.R.styleable.AndroidManifest_sharedUserId);
657 if (str != null) {
658 String nameError = validateName(str, true);
659 if (nameError != null && !"android".equals(pkgName)) {
660 outError[0] = "<manifest> specifies bad sharedUserId name \""
661 + str + "\": " + nameError;
662 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID;
663 return null;
664 }
665 pkg.mSharedUserId = str.intern();
666 pkg.mSharedUserLabel = sa.getResourceId(
667 com.android.internal.R.styleable.AndroidManifest_sharedUserLabel, 0);
668 }
669 sa.recycle();
670
Dianne Hackborn723738c2009-06-25 19:48:04 -0700671 // Resource boolean are -1, so 1 means we don't know the value.
672 int supportsSmallScreens = 1;
673 int supportsNormalScreens = 1;
674 int supportsLargeScreens = 1;
675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 int outerDepth = parser.getDepth();
677 while ((type=parser.next()) != parser.END_DOCUMENT
678 && (type != parser.END_TAG || parser.getDepth() > outerDepth)) {
679 if (type == parser.END_TAG || type == parser.TEXT) {
680 continue;
681 }
682
683 String tagName = parser.getName();
684 if (tagName.equals("application")) {
685 if (foundApp) {
686 if (RIGID_PARSER) {
687 outError[0] = "<manifest> has more than one <application>";
688 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
689 return null;
690 } else {
691 Log.w(TAG, "<manifest> has more than one <application>");
692 XmlUtils.skipCurrentTag(parser);
693 continue;
694 }
695 }
696
697 foundApp = true;
698 if (!parseApplication(pkg, res, parser, attrs, flags, outError)) {
699 return null;
700 }
701 } else if (tagName.equals("permission-group")) {
702 if (parsePermissionGroup(pkg, res, parser, attrs, outError) == null) {
703 return null;
704 }
705 } else if (tagName.equals("permission")) {
706 if (parsePermission(pkg, res, parser, attrs, outError) == null) {
707 return null;
708 }
709 } else if (tagName.equals("permission-tree")) {
710 if (parsePermissionTree(pkg, res, parser, attrs, outError) == null) {
711 return null;
712 }
713 } else if (tagName.equals("uses-permission")) {
714 sa = res.obtainAttributes(attrs,
715 com.android.internal.R.styleable.AndroidManifestUsesPermission);
716
717 String name = sa.getNonResourceString(
718 com.android.internal.R.styleable.AndroidManifestUsesPermission_name);
719
720 sa.recycle();
721
722 if (name != null && !pkg.requestedPermissions.contains(name)) {
723 pkg.requestedPermissions.add(name);
724 }
725
726 XmlUtils.skipCurrentTag(parser);
727
728 } else if (tagName.equals("uses-configuration")) {
729 ConfigurationInfo cPref = new ConfigurationInfo();
730 sa = res.obtainAttributes(attrs,
731 com.android.internal.R.styleable.AndroidManifestUsesConfiguration);
732 cPref.reqTouchScreen = sa.getInt(
733 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqTouchScreen,
734 Configuration.TOUCHSCREEN_UNDEFINED);
735 cPref.reqKeyboardType = sa.getInt(
736 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqKeyboardType,
737 Configuration.KEYBOARD_UNDEFINED);
738 if (sa.getBoolean(
739 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqHardKeyboard,
740 false)) {
741 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
742 }
743 cPref.reqNavigation = sa.getInt(
744 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqNavigation,
745 Configuration.NAVIGATION_UNDEFINED);
746 if (sa.getBoolean(
747 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqFiveWayNav,
748 false)) {
749 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
750 }
751 sa.recycle();
752 pkg.configPreferences.add(cPref);
753
754 XmlUtils.skipCurrentTag(parser);
755
Suchi Amalapurapud299b812009-06-05 10:26:19 -0700756 } else if (tagName.equals("uses-feature")) {
757 ConfigurationInfo cPref = new ConfigurationInfo();
758 sa = res.obtainAttributes(attrs,
759 com.android.internal.R.styleable.AndroidManifestUsesFeature);
760 cPref.reqGlEsVersion = sa.getInt(
761 com.android.internal.R.styleable.AndroidManifestUsesFeature_glEsVersion,
762 ConfigurationInfo.GL_ES_VERSION_UNDEFINED);
763 sa.recycle();
764 pkg.configPreferences.add(cPref);
765
766 XmlUtils.skipCurrentTag(parser);
767
Dianne Hackborn851a5412009-05-08 12:06:44 -0700768 } else if (tagName.equals("uses-sdk")) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 if (mSdkVersion > 0) {
770 sa = res.obtainAttributes(attrs,
771 com.android.internal.R.styleable.AndroidManifestUsesSdk);
772
Dianne Hackborn851a5412009-05-08 12:06:44 -0700773 int minVers = 0;
774 String minCode = null;
775 int targetVers = 0;
776 String targetCode = null;
777
778 TypedValue val = sa.peekValue(
779 com.android.internal.R.styleable.AndroidManifestUsesSdk_minSdkVersion);
780 if (val != null) {
781 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
782 targetCode = minCode = val.string.toString();
783 } else {
784 // If it's not a string, it's an integer.
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700785 targetVers = minVers = val.data;
Dianne Hackborn851a5412009-05-08 12:06:44 -0700786 }
787 }
788
789 val = sa.peekValue(
790 com.android.internal.R.styleable.AndroidManifestUsesSdk_targetSdkVersion);
791 if (val != null) {
792 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
793 targetCode = minCode = val.string.toString();
794 } else {
795 // If it's not a string, it's an integer.
796 targetVers = val.data;
797 }
798 }
799
800 int maxVers = sa.getInt(
801 com.android.internal.R.styleable.AndroidManifestUsesSdk_maxSdkVersion,
802 mSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803
804 sa.recycle();
805
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700806 if (minCode != null) {
807 if (!minCode.equals(mSdkCodename)) {
808 if (mSdkCodename != null) {
809 outError[0] = "Requires development platform " + minCode
810 + " (current platform is " + mSdkCodename + ")";
811 } else {
812 outError[0] = "Requires development platform " + minCode
813 + " but this is a release platform.";
814 }
815 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
816 return null;
817 }
818 } else if (minVers > mSdkVersion) {
819 outError[0] = "Requires newer sdk version #" + minVers
820 + " (current version is #" + mSdkVersion + ")";
821 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
822 return null;
823 }
824
Dianne Hackborn851a5412009-05-08 12:06:44 -0700825 if (targetCode != null) {
826 if (!targetCode.equals(mSdkCodename)) {
827 if (mSdkCodename != null) {
828 outError[0] = "Requires development platform " + targetCode
829 + " (current platform is " + mSdkCodename + ")";
830 } else {
831 outError[0] = "Requires development platform " + targetCode
832 + " but this is a release platform.";
833 }
834 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
835 return null;
836 }
837 // If the code matches, it definitely targets this SDK.
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700838 pkg.applicationInfo.targetSdkVersion
839 = android.os.Build.VERSION_CODES.CUR_DEVELOPMENT;
840 } else {
841 pkg.applicationInfo.targetSdkVersion = targetVers;
Dianne Hackborn851a5412009-05-08 12:06:44 -0700842 }
843
Dianne Hackborn851a5412009-05-08 12:06:44 -0700844 if (maxVers < mSdkVersion) {
845 outError[0] = "Requires older sdk version #" + maxVers
846 + " (current version is #" + mSdkVersion + ")";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
848 return null;
849 }
850 }
851
852 XmlUtils.skipCurrentTag(parser);
853
854 } else if (tagName.equals("instrumentation")) {
855 if (parseInstrumentation(pkg, res, parser, attrs, outError) == null) {
856 return null;
857 }
858 } else if (tagName.equals("eat-comment")) {
859 // Just skip this tag
860 XmlUtils.skipCurrentTag(parser);
861 continue;
862 } else if (RIGID_PARSER) {
863 outError[0] = "Bad element under <manifest>: "
864 + parser.getName();
865 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
866 return null;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700867
868
869 } else if (tagName.equals("supports-density")) {
870 sa = res.obtainAttributes(attrs,
871 com.android.internal.R.styleable.AndroidManifestSupportsDensity);
872
873 int density = sa.getInteger(
874 com.android.internal.R.styleable.AndroidManifestSupportsDensity_density, -1);
875
876 sa.recycle();
877
878 if (density != -1 && !pkg.supportsDensityList.contains(density)) {
879 pkg.supportsDensityList.add(density);
880 }
881
882 XmlUtils.skipCurrentTag(parser);
883
Dianne Hackborn723738c2009-06-25 19:48:04 -0700884 } else if (tagName.equals("supports-screens")) {
885 sa = res.obtainAttributes(attrs,
886 com.android.internal.R.styleable.AndroidManifestSupportsScreens);
887
888 // This is a trick to get a boolean and still able to detect
889 // if a value was actually set.
890 supportsSmallScreens = sa.getInteger(
891 com.android.internal.R.styleable.AndroidManifestSupportsScreens_smallScreens,
892 supportsSmallScreens);
893 supportsNormalScreens = sa.getInteger(
894 com.android.internal.R.styleable.AndroidManifestSupportsScreens_normalScreens,
895 supportsNormalScreens);
896 supportsLargeScreens = sa.getInteger(
897 com.android.internal.R.styleable.AndroidManifestSupportsScreens_largeScreens,
898 supportsLargeScreens);
899
900 sa.recycle();
901
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700902 XmlUtils.skipCurrentTag(parser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 } else {
904 Log.w(TAG, "Bad element under <manifest>: "
905 + parser.getName());
906 XmlUtils.skipCurrentTag(parser);
907 continue;
908 }
909 }
910
911 if (!foundApp && pkg.instrumentation.size() == 0) {
912 outError[0] = "<manifest> does not contain an <application> or <instrumentation>";
913 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_EMPTY;
914 }
915
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700916 final int NP = PackageParser.NEW_PERMISSIONS.length;
917 for (int ip=0; ip<NP; ip++) {
918 final PackageParser.NewPermissionInfo npi
919 = PackageParser.NEW_PERMISSIONS[ip];
920 if (pkg.applicationInfo.targetSdkVersion >= npi.sdkVersion) {
921 break;
922 }
923 if (!pkg.requestedPermissions.contains(npi.name)) {
924 Log.i(TAG, "Impliciting adding " + npi.name + " to old pkg "
925 + pkg.packageName);
926 pkg.requestedPermissions.add(npi.name);
927 }
Dianne Hackborn851a5412009-05-08 12:06:44 -0700928 }
929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 if (pkg.usesLibraries.size() > 0) {
931 pkg.usesLibraryFiles = new String[pkg.usesLibraries.size()];
932 pkg.usesLibraries.toArray(pkg.usesLibraryFiles);
933 }
Dianne Hackborn723738c2009-06-25 19:48:04 -0700934
935 if (supportsSmallScreens < 0 || (supportsSmallScreens > 0
936 && pkg.applicationInfo.targetSdkVersion
937 >= android.os.Build.VERSION_CODES.CUR_DEVELOPMENT)) {
938 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS;
939 }
940 if (supportsNormalScreens != 0) {
941 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS;
942 }
943 if (supportsLargeScreens < 0 || (supportsLargeScreens > 0
944 && pkg.applicationInfo.targetSdkVersion
945 >= android.os.Build.VERSION_CODES.CUR_DEVELOPMENT)) {
946 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS;
947 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700948 int densities[] = null;
Mitsuru Oshima8d112672009-04-27 12:01:23 -0700949 int size = pkg.supportsDensityList.size();
950 if (size > 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700951 densities = pkg.supportsDensities = new int[size];
Mitsuru Oshima8d112672009-04-27 12:01:23 -0700952 List<Integer> densityList = pkg.supportsDensityList;
953 for (int i = 0; i < size; i++) {
954 densities[i] = densityList.get(i);
955 }
956 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700957 /**
958 * TODO: enable this before code freeze. b/1967935
959 * *
960 if ((densities == null || densities.length == 0)
961 && (pkg.applicationInfo.targetSdkVersion
962 >= android.os.Build.VERSION_CODES.CUR_DEVELOPMENT)) {
963 pkg.supportsDensities = ApplicationInfo.ANY_DENSITIES_ARRAY;
964 }
965 */
966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 return pkg;
968 }
969
970 private static String buildClassName(String pkg, CharSequence clsSeq,
971 String[] outError) {
972 if (clsSeq == null || clsSeq.length() <= 0) {
973 outError[0] = "Empty class name in package " + pkg;
974 return null;
975 }
976 String cls = clsSeq.toString();
977 char c = cls.charAt(0);
978 if (c == '.') {
979 return (pkg + cls).intern();
980 }
981 if (cls.indexOf('.') < 0) {
982 StringBuilder b = new StringBuilder(pkg);
983 b.append('.');
984 b.append(cls);
985 return b.toString().intern();
986 }
987 if (c >= 'a' && c <= 'z') {
988 return cls.intern();
989 }
990 outError[0] = "Bad class name " + cls + " in package " + pkg;
991 return null;
992 }
993
994 private static String buildCompoundName(String pkg,
995 CharSequence procSeq, String type, String[] outError) {
996 String proc = procSeq.toString();
997 char c = proc.charAt(0);
998 if (pkg != null && c == ':') {
999 if (proc.length() < 2) {
1000 outError[0] = "Bad " + type + " name " + proc + " in package " + pkg
1001 + ": must be at least two characters";
1002 return null;
1003 }
1004 String subName = proc.substring(1);
1005 String nameError = validateName(subName, false);
1006 if (nameError != null) {
1007 outError[0] = "Invalid " + type + " name " + proc + " in package "
1008 + pkg + ": " + nameError;
1009 return null;
1010 }
1011 return (pkg + proc).intern();
1012 }
1013 String nameError = validateName(proc, true);
1014 if (nameError != null && !"system".equals(proc)) {
1015 outError[0] = "Invalid " + type + " name " + proc + " in package "
1016 + pkg + ": " + nameError;
1017 return null;
1018 }
1019 return proc.intern();
1020 }
1021
1022 private static String buildProcessName(String pkg, String defProc,
1023 CharSequence procSeq, int flags, String[] separateProcesses,
1024 String[] outError) {
1025 if ((flags&PARSE_IGNORE_PROCESSES) != 0 && !"system".equals(procSeq)) {
1026 return defProc != null ? defProc : pkg;
1027 }
1028 if (separateProcesses != null) {
1029 for (int i=separateProcesses.length-1; i>=0; i--) {
1030 String sp = separateProcesses[i];
1031 if (sp.equals(pkg) || sp.equals(defProc) || sp.equals(procSeq)) {
1032 return pkg;
1033 }
1034 }
1035 }
1036 if (procSeq == null || procSeq.length() <= 0) {
1037 return defProc;
1038 }
1039 return buildCompoundName(pkg, procSeq, "package", outError);
1040 }
1041
1042 private static String buildTaskAffinityName(String pkg, String defProc,
1043 CharSequence procSeq, String[] outError) {
1044 if (procSeq == null) {
1045 return defProc;
1046 }
1047 if (procSeq.length() <= 0) {
1048 return null;
1049 }
1050 return buildCompoundName(pkg, procSeq, "taskAffinity", outError);
1051 }
1052
1053 private PermissionGroup parsePermissionGroup(Package owner, Resources res,
1054 XmlPullParser parser, AttributeSet attrs, String[] outError)
1055 throws XmlPullParserException, IOException {
1056 PermissionGroup perm = new PermissionGroup(owner);
1057
1058 TypedArray sa = res.obtainAttributes(attrs,
1059 com.android.internal.R.styleable.AndroidManifestPermissionGroup);
1060
1061 if (!parsePackageItemInfo(owner, perm.info, outError,
1062 "<permission-group>", sa,
1063 com.android.internal.R.styleable.AndroidManifestPermissionGroup_name,
1064 com.android.internal.R.styleable.AndroidManifestPermissionGroup_label,
1065 com.android.internal.R.styleable.AndroidManifestPermissionGroup_icon)) {
1066 sa.recycle();
1067 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1068 return null;
1069 }
1070
1071 perm.info.descriptionRes = sa.getResourceId(
1072 com.android.internal.R.styleable.AndroidManifestPermissionGroup_description,
1073 0);
1074
1075 sa.recycle();
1076
1077 if (!parseAllMetaData(res, parser, attrs, "<permission-group>", perm,
1078 outError)) {
1079 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1080 return null;
1081 }
1082
1083 owner.permissionGroups.add(perm);
1084
1085 return perm;
1086 }
1087
1088 private Permission parsePermission(Package owner, Resources res,
1089 XmlPullParser parser, AttributeSet attrs, String[] outError)
1090 throws XmlPullParserException, IOException {
1091 Permission perm = new Permission(owner);
1092
1093 TypedArray sa = res.obtainAttributes(attrs,
1094 com.android.internal.R.styleable.AndroidManifestPermission);
1095
1096 if (!parsePackageItemInfo(owner, perm.info, outError,
1097 "<permission>", sa,
1098 com.android.internal.R.styleable.AndroidManifestPermission_name,
1099 com.android.internal.R.styleable.AndroidManifestPermission_label,
1100 com.android.internal.R.styleable.AndroidManifestPermission_icon)) {
1101 sa.recycle();
1102 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1103 return null;
1104 }
1105
1106 perm.info.group = sa.getNonResourceString(
1107 com.android.internal.R.styleable.AndroidManifestPermission_permissionGroup);
1108 if (perm.info.group != null) {
1109 perm.info.group = perm.info.group.intern();
1110 }
1111
1112 perm.info.descriptionRes = sa.getResourceId(
1113 com.android.internal.R.styleable.AndroidManifestPermission_description,
1114 0);
1115
1116 perm.info.protectionLevel = sa.getInt(
1117 com.android.internal.R.styleable.AndroidManifestPermission_protectionLevel,
1118 PermissionInfo.PROTECTION_NORMAL);
1119
1120 sa.recycle();
1121
1122 if (perm.info.protectionLevel == -1) {
1123 outError[0] = "<permission> does not specify protectionLevel";
1124 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1125 return null;
1126 }
1127
1128 if (!parseAllMetaData(res, parser, attrs, "<permission>", perm,
1129 outError)) {
1130 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1131 return null;
1132 }
1133
1134 owner.permissions.add(perm);
1135
1136 return perm;
1137 }
1138
1139 private Permission parsePermissionTree(Package owner, Resources res,
1140 XmlPullParser parser, AttributeSet attrs, String[] outError)
1141 throws XmlPullParserException, IOException {
1142 Permission perm = new Permission(owner);
1143
1144 TypedArray sa = res.obtainAttributes(attrs,
1145 com.android.internal.R.styleable.AndroidManifestPermissionTree);
1146
1147 if (!parsePackageItemInfo(owner, perm.info, outError,
1148 "<permission-tree>", sa,
1149 com.android.internal.R.styleable.AndroidManifestPermissionTree_name,
1150 com.android.internal.R.styleable.AndroidManifestPermissionTree_label,
1151 com.android.internal.R.styleable.AndroidManifestPermissionTree_icon)) {
1152 sa.recycle();
1153 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1154 return null;
1155 }
1156
1157 sa.recycle();
1158
1159 int index = perm.info.name.indexOf('.');
1160 if (index > 0) {
1161 index = perm.info.name.indexOf('.', index+1);
1162 }
1163 if (index < 0) {
1164 outError[0] = "<permission-tree> name has less than three segments: "
1165 + perm.info.name;
1166 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1167 return null;
1168 }
1169
1170 perm.info.descriptionRes = 0;
1171 perm.info.protectionLevel = PermissionInfo.PROTECTION_NORMAL;
1172 perm.tree = true;
1173
1174 if (!parseAllMetaData(res, parser, attrs, "<permission-tree>", perm,
1175 outError)) {
1176 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1177 return null;
1178 }
1179
1180 owner.permissions.add(perm);
1181
1182 return perm;
1183 }
1184
1185 private Instrumentation parseInstrumentation(Package owner, Resources res,
1186 XmlPullParser parser, AttributeSet attrs, String[] outError)
1187 throws XmlPullParserException, IOException {
1188 TypedArray sa = res.obtainAttributes(attrs,
1189 com.android.internal.R.styleable.AndroidManifestInstrumentation);
1190
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001191 if (mParseInstrumentationArgs == null) {
1192 mParseInstrumentationArgs = new ParsePackageItemArgs(owner, outError,
1193 com.android.internal.R.styleable.AndroidManifestInstrumentation_name,
1194 com.android.internal.R.styleable.AndroidManifestInstrumentation_label,
1195 com.android.internal.R.styleable.AndroidManifestInstrumentation_icon);
1196 mParseInstrumentationArgs.tag = "<instrumentation>";
1197 }
1198
1199 mParseInstrumentationArgs.sa = sa;
1200
1201 Instrumentation a = new Instrumentation(mParseInstrumentationArgs,
1202 new InstrumentationInfo());
1203 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 sa.recycle();
1205 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1206 return null;
1207 }
1208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 String str;
1210 str = sa.getNonResourceString(
1211 com.android.internal.R.styleable.AndroidManifestInstrumentation_targetPackage);
1212 a.info.targetPackage = str != null ? str.intern() : null;
1213
1214 a.info.handleProfiling = sa.getBoolean(
1215 com.android.internal.R.styleable.AndroidManifestInstrumentation_handleProfiling,
1216 false);
1217
1218 a.info.functionalTest = sa.getBoolean(
1219 com.android.internal.R.styleable.AndroidManifestInstrumentation_functionalTest,
1220 false);
1221
1222 sa.recycle();
1223
1224 if (a.info.targetPackage == null) {
1225 outError[0] = "<instrumentation> does not specify targetPackage";
1226 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1227 return null;
1228 }
1229
1230 if (!parseAllMetaData(res, parser, attrs, "<instrumentation>", a,
1231 outError)) {
1232 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1233 return null;
1234 }
1235
1236 owner.instrumentation.add(a);
1237
1238 return a;
1239 }
1240
1241 private boolean parseApplication(Package owner, Resources res,
1242 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
1243 throws XmlPullParserException, IOException {
1244 final ApplicationInfo ai = owner.applicationInfo;
1245 final String pkgName = owner.applicationInfo.packageName;
1246
1247 TypedArray sa = res.obtainAttributes(attrs,
1248 com.android.internal.R.styleable.AndroidManifestApplication);
1249
1250 String name = sa.getNonResourceString(
1251 com.android.internal.R.styleable.AndroidManifestApplication_name);
1252 if (name != null) {
1253 ai.className = buildClassName(pkgName, name, outError);
1254 if (ai.className == null) {
1255 sa.recycle();
1256 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1257 return false;
1258 }
1259 }
1260
1261 String manageSpaceActivity = sa.getNonResourceString(
1262 com.android.internal.R.styleable.AndroidManifestApplication_manageSpaceActivity);
1263 if (manageSpaceActivity != null) {
1264 ai.manageSpaceActivityName = buildClassName(pkgName, manageSpaceActivity,
1265 outError);
1266 }
1267
Christopher Tate181fafa2009-05-14 11:12:14 -07001268 boolean allowBackup = sa.getBoolean(
1269 com.android.internal.R.styleable.AndroidManifestApplication_allowBackup, true);
1270 if (allowBackup) {
1271 ai.flags |= ApplicationInfo.FLAG_ALLOW_BACKUP;
1272 String backupAgent = sa.getNonResourceString(
1273 com.android.internal.R.styleable.AndroidManifestApplication_backupAgent);
1274 if (backupAgent != null) {
1275 ai.backupAgentName = buildClassName(pkgName, backupAgent, outError);
1276 Log.v(TAG, "android:backupAgent = " + ai.backupAgentName
1277 + " from " + pkgName + "+" + backupAgent);
1278 }
1279 }
1280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 TypedValue v = sa.peekValue(
1282 com.android.internal.R.styleable.AndroidManifestApplication_label);
1283 if (v != null && (ai.labelRes=v.resourceId) == 0) {
1284 ai.nonLocalizedLabel = v.coerceToString();
1285 }
1286
1287 ai.icon = sa.getResourceId(
1288 com.android.internal.R.styleable.AndroidManifestApplication_icon, 0);
1289 ai.theme = sa.getResourceId(
1290 com.android.internal.R.styleable.AndroidManifestApplication_theme, 0);
1291 ai.descriptionRes = sa.getResourceId(
1292 com.android.internal.R.styleable.AndroidManifestApplication_description, 0);
1293
1294 if ((flags&PARSE_IS_SYSTEM) != 0) {
1295 if (sa.getBoolean(
1296 com.android.internal.R.styleable.AndroidManifestApplication_persistent,
1297 false)) {
1298 ai.flags |= ApplicationInfo.FLAG_PERSISTENT;
1299 }
1300 }
1301
1302 if (sa.getBoolean(
1303 com.android.internal.R.styleable.AndroidManifestApplication_debuggable,
1304 false)) {
1305 ai.flags |= ApplicationInfo.FLAG_DEBUGGABLE;
1306 }
1307
1308 if (sa.getBoolean(
1309 com.android.internal.R.styleable.AndroidManifestApplication_hasCode,
1310 true)) {
1311 ai.flags |= ApplicationInfo.FLAG_HAS_CODE;
1312 }
1313
1314 if (sa.getBoolean(
1315 com.android.internal.R.styleable.AndroidManifestApplication_allowTaskReparenting,
1316 false)) {
1317 ai.flags |= ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING;
1318 }
1319
1320 if (sa.getBoolean(
1321 com.android.internal.R.styleable.AndroidManifestApplication_allowClearUserData,
1322 true)) {
1323 ai.flags |= ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA;
1324 }
1325
Dianne Hackbornade3eca2009-05-11 18:54:45 -07001326 if (sa.getBoolean(
1327 com.android.internal.R.styleable.AndroidManifestApplication_testOnly,
Dianne Hackborne7fe35b2009-05-13 10:53:41 -07001328 false)) {
Dianne Hackbornade3eca2009-05-11 18:54:45 -07001329 ai.flags |= ApplicationInfo.FLAG_TEST_ONLY;
1330 }
1331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 String str;
1333 str = sa.getNonResourceString(
1334 com.android.internal.R.styleable.AndroidManifestApplication_permission);
1335 ai.permission = (str != null && str.length() > 0) ? str.intern() : null;
1336
1337 str = sa.getNonResourceString(
1338 com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity);
1339 ai.taskAffinity = buildTaskAffinityName(ai.packageName, ai.packageName,
1340 str, outError);
1341
1342 if (outError[0] == null) {
1343 ai.processName = buildProcessName(ai.packageName, null, sa.getNonResourceString(
1344 com.android.internal.R.styleable.AndroidManifestApplication_process),
1345 flags, mSeparateProcesses, outError);
1346
1347 ai.enabled = sa.getBoolean(com.android.internal.R.styleable.AndroidManifestApplication_enabled, true);
1348 }
1349
1350 sa.recycle();
1351
1352 if (outError[0] != null) {
1353 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1354 return false;
1355 }
1356
1357 final int innerDepth = parser.getDepth();
1358
1359 int type;
1360 while ((type=parser.next()) != parser.END_DOCUMENT
1361 && (type != parser.END_TAG || parser.getDepth() > innerDepth)) {
1362 if (type == parser.END_TAG || type == parser.TEXT) {
1363 continue;
1364 }
1365
1366 String tagName = parser.getName();
1367 if (tagName.equals("activity")) {
1368 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, false);
1369 if (a == null) {
1370 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1371 return false;
1372 }
1373
1374 owner.activities.add(a);
1375
1376 } else if (tagName.equals("receiver")) {
1377 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, true);
1378 if (a == null) {
1379 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1380 return false;
1381 }
1382
1383 owner.receivers.add(a);
1384
1385 } else if (tagName.equals("service")) {
1386 Service s = parseService(owner, res, parser, attrs, flags, outError);
1387 if (s == null) {
1388 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1389 return false;
1390 }
1391
1392 owner.services.add(s);
1393
1394 } else if (tagName.equals("provider")) {
1395 Provider p = parseProvider(owner, res, parser, attrs, flags, outError);
1396 if (p == null) {
1397 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1398 return false;
1399 }
1400
1401 owner.providers.add(p);
1402
1403 } else if (tagName.equals("activity-alias")) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001404 Activity a = parseActivityAlias(owner, res, parser, attrs, flags, outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 if (a == null) {
1406 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1407 return false;
1408 }
1409
1410 owner.activities.add(a);
1411
1412 } else if (parser.getName().equals("meta-data")) {
1413 // note: application meta-data is stored off to the side, so it can
1414 // remain null in the primary copy (we like to avoid extra copies because
1415 // it can be large)
1416 if ((owner.mAppMetaData = parseMetaData(res, parser, attrs, owner.mAppMetaData,
1417 outError)) == null) {
1418 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1419 return false;
1420 }
1421
1422 } else if (tagName.equals("uses-library")) {
1423 sa = res.obtainAttributes(attrs,
1424 com.android.internal.R.styleable.AndroidManifestUsesLibrary);
1425
1426 String lname = sa.getNonResourceString(
1427 com.android.internal.R.styleable.AndroidManifestUsesLibrary_name);
1428
1429 sa.recycle();
1430
1431 if (lname != null && !owner.usesLibraries.contains(lname)) {
1432 owner.usesLibraries.add(lname);
1433 }
1434
1435 XmlUtils.skipCurrentTag(parser);
1436
1437 } else {
1438 if (!RIGID_PARSER) {
1439 Log.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
1440 Log.w(TAG, "Unknown element under <application>: " + tagName);
1441 XmlUtils.skipCurrentTag(parser);
1442 continue;
1443 } else {
1444 outError[0] = "Bad element under <application>: " + tagName;
1445 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1446 return false;
1447 }
1448 }
1449 }
1450
1451 return true;
1452 }
1453
1454 private boolean parsePackageItemInfo(Package owner, PackageItemInfo outInfo,
1455 String[] outError, String tag, TypedArray sa,
1456 int nameRes, int labelRes, int iconRes) {
1457 String name = sa.getNonResourceString(nameRes);
1458 if (name == null) {
1459 outError[0] = tag + " does not specify android:name";
1460 return false;
1461 }
1462
1463 outInfo.name
1464 = buildClassName(owner.applicationInfo.packageName, name, outError);
1465 if (outInfo.name == null) {
1466 return false;
1467 }
1468
1469 int iconVal = sa.getResourceId(iconRes, 0);
1470 if (iconVal != 0) {
1471 outInfo.icon = iconVal;
1472 outInfo.nonLocalizedLabel = null;
1473 }
1474
1475 TypedValue v = sa.peekValue(labelRes);
1476 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
1477 outInfo.nonLocalizedLabel = v.coerceToString();
1478 }
1479
1480 outInfo.packageName = owner.packageName;
1481
1482 return true;
1483 }
1484
1485 private boolean parseComponentInfo(Package owner, int flags,
1486 ComponentInfo outInfo, String[] outError, String tag, TypedArray sa,
1487 int nameRes, int labelRes, int iconRes, int processRes,
1488 int enabledRes) {
1489 if (!parsePackageItemInfo(owner, outInfo, outError, tag, sa,
1490 nameRes, labelRes, iconRes)) {
1491 return false;
1492 }
1493
1494 if (processRes != 0) {
1495 outInfo.processName = buildProcessName(owner.applicationInfo.packageName,
1496 owner.applicationInfo.processName, sa.getNonResourceString(processRes),
1497 flags, mSeparateProcesses, outError);
1498 }
1499 outInfo.enabled = sa.getBoolean(enabledRes, true);
1500
1501 return outError[0] == null;
1502 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 private Activity parseActivity(Package owner, Resources res,
1505 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError,
1506 boolean receiver) throws XmlPullParserException, IOException {
1507 TypedArray sa = res.obtainAttributes(attrs,
1508 com.android.internal.R.styleable.AndroidManifestActivity);
1509
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001510 if (mParseActivityArgs == null) {
1511 mParseActivityArgs = new ParseComponentArgs(owner, outError,
1512 com.android.internal.R.styleable.AndroidManifestActivity_name,
1513 com.android.internal.R.styleable.AndroidManifestActivity_label,
1514 com.android.internal.R.styleable.AndroidManifestActivity_icon,
1515 mSeparateProcesses,
1516 com.android.internal.R.styleable.AndroidManifestActivity_process,
1517 com.android.internal.R.styleable.AndroidManifestActivity_enabled);
1518 }
1519
1520 mParseActivityArgs.tag = receiver ? "<receiver>" : "<activity>";
1521 mParseActivityArgs.sa = sa;
1522 mParseActivityArgs.flags = flags;
1523
1524 Activity a = new Activity(mParseActivityArgs, new ActivityInfo());
1525 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 sa.recycle();
1527 return null;
1528 }
1529
1530 final boolean setExported = sa.hasValue(
1531 com.android.internal.R.styleable.AndroidManifestActivity_exported);
1532 if (setExported) {
1533 a.info.exported = sa.getBoolean(
1534 com.android.internal.R.styleable.AndroidManifestActivity_exported, false);
1535 }
1536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 a.info.theme = sa.getResourceId(
1538 com.android.internal.R.styleable.AndroidManifestActivity_theme, 0);
1539
1540 String str;
1541 str = sa.getNonResourceString(
1542 com.android.internal.R.styleable.AndroidManifestActivity_permission);
1543 if (str == null) {
1544 a.info.permission = owner.applicationInfo.permission;
1545 } else {
1546 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
1547 }
1548
1549 str = sa.getNonResourceString(
1550 com.android.internal.R.styleable.AndroidManifestActivity_taskAffinity);
1551 a.info.taskAffinity = buildTaskAffinityName(owner.applicationInfo.packageName,
1552 owner.applicationInfo.taskAffinity, str, outError);
1553
1554 a.info.flags = 0;
1555 if (sa.getBoolean(
1556 com.android.internal.R.styleable.AndroidManifestActivity_multiprocess,
1557 false)) {
1558 a.info.flags |= ActivityInfo.FLAG_MULTIPROCESS;
1559 }
1560
1561 if (sa.getBoolean(
1562 com.android.internal.R.styleable.AndroidManifestActivity_finishOnTaskLaunch,
1563 false)) {
1564 a.info.flags |= ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH;
1565 }
1566
1567 if (sa.getBoolean(
1568 com.android.internal.R.styleable.AndroidManifestActivity_clearTaskOnLaunch,
1569 false)) {
1570 a.info.flags |= ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH;
1571 }
1572
1573 if (sa.getBoolean(
1574 com.android.internal.R.styleable.AndroidManifestActivity_noHistory,
1575 false)) {
1576 a.info.flags |= ActivityInfo.FLAG_NO_HISTORY;
1577 }
1578
1579 if (sa.getBoolean(
1580 com.android.internal.R.styleable.AndroidManifestActivity_alwaysRetainTaskState,
1581 false)) {
1582 a.info.flags |= ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE;
1583 }
1584
1585 if (sa.getBoolean(
1586 com.android.internal.R.styleable.AndroidManifestActivity_stateNotNeeded,
1587 false)) {
1588 a.info.flags |= ActivityInfo.FLAG_STATE_NOT_NEEDED;
1589 }
1590
1591 if (sa.getBoolean(
1592 com.android.internal.R.styleable.AndroidManifestActivity_excludeFromRecents,
1593 false)) {
1594 a.info.flags |= ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
1595 }
1596
1597 if (sa.getBoolean(
1598 com.android.internal.R.styleable.AndroidManifestActivity_allowTaskReparenting,
1599 (owner.applicationInfo.flags&ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING) != 0)) {
1600 a.info.flags |= ActivityInfo.FLAG_ALLOW_TASK_REPARENTING;
1601 }
1602
1603 if (!receiver) {
1604 a.info.launchMode = sa.getInt(
1605 com.android.internal.R.styleable.AndroidManifestActivity_launchMode,
1606 ActivityInfo.LAUNCH_MULTIPLE);
1607 a.info.screenOrientation = sa.getInt(
1608 com.android.internal.R.styleable.AndroidManifestActivity_screenOrientation,
1609 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
1610 a.info.configChanges = sa.getInt(
1611 com.android.internal.R.styleable.AndroidManifestActivity_configChanges,
1612 0);
1613 a.info.softInputMode = sa.getInt(
1614 com.android.internal.R.styleable.AndroidManifestActivity_windowSoftInputMode,
1615 0);
1616 } else {
1617 a.info.launchMode = ActivityInfo.LAUNCH_MULTIPLE;
1618 a.info.configChanges = 0;
1619 }
1620
1621 sa.recycle();
1622
1623 if (outError[0] != null) {
1624 return null;
1625 }
1626
1627 int outerDepth = parser.getDepth();
1628 int type;
1629 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1630 && (type != XmlPullParser.END_TAG
1631 || parser.getDepth() > outerDepth)) {
1632 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1633 continue;
1634 }
1635
1636 if (parser.getName().equals("intent-filter")) {
1637 ActivityIntentInfo intent = new ActivityIntentInfo(a);
1638 if (!parseIntent(res, parser, attrs, flags, intent, outError, !receiver)) {
1639 return null;
1640 }
1641 if (intent.countActions() == 0) {
1642 Log.w(TAG, "Intent filter for activity " + intent
1643 + " defines no actions");
1644 } else {
1645 a.intents.add(intent);
1646 }
1647 } else if (parser.getName().equals("meta-data")) {
1648 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
1649 outError)) == null) {
1650 return null;
1651 }
1652 } else {
1653 if (!RIGID_PARSER) {
1654 Log.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
1655 if (receiver) {
1656 Log.w(TAG, "Unknown element under <receiver>: " + parser.getName());
1657 } else {
1658 Log.w(TAG, "Unknown element under <activity>: " + parser.getName());
1659 }
1660 XmlUtils.skipCurrentTag(parser);
1661 continue;
1662 }
1663 if (receiver) {
1664 outError[0] = "Bad element under <receiver>: " + parser.getName();
1665 } else {
1666 outError[0] = "Bad element under <activity>: " + parser.getName();
1667 }
1668 return null;
1669 }
1670 }
1671
1672 if (!setExported) {
1673 a.info.exported = a.intents.size() > 0;
1674 }
1675
1676 return a;
1677 }
1678
1679 private Activity parseActivityAlias(Package owner, Resources res,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001680 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
1681 throws XmlPullParserException, IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 TypedArray sa = res.obtainAttributes(attrs,
1683 com.android.internal.R.styleable.AndroidManifestActivityAlias);
1684
1685 String targetActivity = sa.getNonResourceString(
1686 com.android.internal.R.styleable.AndroidManifestActivityAlias_targetActivity);
1687 if (targetActivity == null) {
1688 outError[0] = "<activity-alias> does not specify android:targetActivity";
1689 sa.recycle();
1690 return null;
1691 }
1692
1693 targetActivity = buildClassName(owner.applicationInfo.packageName,
1694 targetActivity, outError);
1695 if (targetActivity == null) {
1696 sa.recycle();
1697 return null;
1698 }
1699
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001700 if (mParseActivityAliasArgs == null) {
1701 mParseActivityAliasArgs = new ParseComponentArgs(owner, outError,
1702 com.android.internal.R.styleable.AndroidManifestActivityAlias_name,
1703 com.android.internal.R.styleable.AndroidManifestActivityAlias_label,
1704 com.android.internal.R.styleable.AndroidManifestActivityAlias_icon,
1705 mSeparateProcesses,
1706 0,
1707 com.android.internal.R.styleable.AndroidManifestActivityAlias_enabled);
1708 mParseActivityAliasArgs.tag = "<activity-alias>";
1709 }
1710
1711 mParseActivityAliasArgs.sa = sa;
1712 mParseActivityAliasArgs.flags = flags;
1713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 Activity target = null;
1715
1716 final int NA = owner.activities.size();
1717 for (int i=0; i<NA; i++) {
1718 Activity t = owner.activities.get(i);
1719 if (targetActivity.equals(t.info.name)) {
1720 target = t;
1721 break;
1722 }
1723 }
1724
1725 if (target == null) {
1726 outError[0] = "<activity-alias> target activity " + targetActivity
1727 + " not found in manifest";
1728 sa.recycle();
1729 return null;
1730 }
1731
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001732 ActivityInfo info = new ActivityInfo();
1733 info.targetActivity = targetActivity;
1734 info.configChanges = target.info.configChanges;
1735 info.flags = target.info.flags;
1736 info.icon = target.info.icon;
1737 info.labelRes = target.info.labelRes;
1738 info.nonLocalizedLabel = target.info.nonLocalizedLabel;
1739 info.launchMode = target.info.launchMode;
1740 info.processName = target.info.processName;
1741 info.screenOrientation = target.info.screenOrientation;
1742 info.taskAffinity = target.info.taskAffinity;
1743 info.theme = target.info.theme;
1744
1745 Activity a = new Activity(mParseActivityAliasArgs, info);
1746 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 sa.recycle();
1748 return null;
1749 }
1750
1751 final boolean setExported = sa.hasValue(
1752 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported);
1753 if (setExported) {
1754 a.info.exported = sa.getBoolean(
1755 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported, false);
1756 }
1757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 String str;
1759 str = sa.getNonResourceString(
1760 com.android.internal.R.styleable.AndroidManifestActivityAlias_permission);
1761 if (str != null) {
1762 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
1763 }
1764
1765 sa.recycle();
1766
1767 if (outError[0] != null) {
1768 return null;
1769 }
1770
1771 int outerDepth = parser.getDepth();
1772 int type;
1773 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1774 && (type != XmlPullParser.END_TAG
1775 || parser.getDepth() > outerDepth)) {
1776 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1777 continue;
1778 }
1779
1780 if (parser.getName().equals("intent-filter")) {
1781 ActivityIntentInfo intent = new ActivityIntentInfo(a);
1782 if (!parseIntent(res, parser, attrs, flags, intent, outError, true)) {
1783 return null;
1784 }
1785 if (intent.countActions() == 0) {
1786 Log.w(TAG, "Intent filter for activity alias " + intent
1787 + " defines no actions");
1788 } else {
1789 a.intents.add(intent);
1790 }
1791 } else if (parser.getName().equals("meta-data")) {
1792 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
1793 outError)) == null) {
1794 return null;
1795 }
1796 } else {
1797 if (!RIGID_PARSER) {
1798 Log.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
1799 Log.w(TAG, "Unknown element under <activity-alias>: " + parser.getName());
1800 XmlUtils.skipCurrentTag(parser);
1801 continue;
1802 }
1803 outError[0] = "Bad element under <activity-alias>: " + parser.getName();
1804 return null;
1805 }
1806 }
1807
1808 if (!setExported) {
1809 a.info.exported = a.intents.size() > 0;
1810 }
1811
1812 return a;
1813 }
1814
1815 private Provider parseProvider(Package owner, Resources res,
1816 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
1817 throws XmlPullParserException, IOException {
1818 TypedArray sa = res.obtainAttributes(attrs,
1819 com.android.internal.R.styleable.AndroidManifestProvider);
1820
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001821 if (mParseProviderArgs == null) {
1822 mParseProviderArgs = new ParseComponentArgs(owner, outError,
1823 com.android.internal.R.styleable.AndroidManifestProvider_name,
1824 com.android.internal.R.styleable.AndroidManifestProvider_label,
1825 com.android.internal.R.styleable.AndroidManifestProvider_icon,
1826 mSeparateProcesses,
1827 com.android.internal.R.styleable.AndroidManifestProvider_process,
1828 com.android.internal.R.styleable.AndroidManifestProvider_enabled);
1829 mParseProviderArgs.tag = "<provider>";
1830 }
1831
1832 mParseProviderArgs.sa = sa;
1833 mParseProviderArgs.flags = flags;
1834
1835 Provider p = new Provider(mParseProviderArgs, new ProviderInfo());
1836 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 sa.recycle();
1838 return null;
1839 }
1840
1841 p.info.exported = sa.getBoolean(
1842 com.android.internal.R.styleable.AndroidManifestProvider_exported, true);
1843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 String cpname = sa.getNonResourceString(
1845 com.android.internal.R.styleable.AndroidManifestProvider_authorities);
1846
1847 p.info.isSyncable = sa.getBoolean(
1848 com.android.internal.R.styleable.AndroidManifestProvider_syncable,
1849 false);
1850
1851 String permission = sa.getNonResourceString(
1852 com.android.internal.R.styleable.AndroidManifestProvider_permission);
1853 String str = sa.getNonResourceString(
1854 com.android.internal.R.styleable.AndroidManifestProvider_readPermission);
1855 if (str == null) {
1856 str = permission;
1857 }
1858 if (str == null) {
1859 p.info.readPermission = owner.applicationInfo.permission;
1860 } else {
1861 p.info.readPermission =
1862 str.length() > 0 ? str.toString().intern() : null;
1863 }
1864 str = sa.getNonResourceString(
1865 com.android.internal.R.styleable.AndroidManifestProvider_writePermission);
1866 if (str == null) {
1867 str = permission;
1868 }
1869 if (str == null) {
1870 p.info.writePermission = owner.applicationInfo.permission;
1871 } else {
1872 p.info.writePermission =
1873 str.length() > 0 ? str.toString().intern() : null;
1874 }
1875
1876 p.info.grantUriPermissions = sa.getBoolean(
1877 com.android.internal.R.styleable.AndroidManifestProvider_grantUriPermissions,
1878 false);
1879
1880 p.info.multiprocess = sa.getBoolean(
1881 com.android.internal.R.styleable.AndroidManifestProvider_multiprocess,
1882 false);
1883
1884 p.info.initOrder = sa.getInt(
1885 com.android.internal.R.styleable.AndroidManifestProvider_initOrder,
1886 0);
1887
1888 sa.recycle();
1889
1890 if (cpname == null) {
1891 outError[0] = "<provider> does not incude authorities attribute";
1892 return null;
1893 }
1894 p.info.authority = cpname.intern();
1895
1896 if (!parseProviderTags(res, parser, attrs, p, outError)) {
1897 return null;
1898 }
1899
1900 return p;
1901 }
1902
1903 private boolean parseProviderTags(Resources res,
1904 XmlPullParser parser, AttributeSet attrs,
1905 Provider outInfo, String[] outError)
1906 throws XmlPullParserException, IOException {
1907 int outerDepth = parser.getDepth();
1908 int type;
1909 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1910 && (type != XmlPullParser.END_TAG
1911 || parser.getDepth() > outerDepth)) {
1912 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1913 continue;
1914 }
1915
1916 if (parser.getName().equals("meta-data")) {
1917 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
1918 outInfo.metaData, outError)) == null) {
1919 return false;
1920 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07001921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 } else if (parser.getName().equals("grant-uri-permission")) {
1923 TypedArray sa = res.obtainAttributes(attrs,
1924 com.android.internal.R.styleable.AndroidManifestGrantUriPermission);
1925
1926 PatternMatcher pa = null;
1927
1928 String str = sa.getNonResourceString(
1929 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_path);
1930 if (str != null) {
1931 pa = new PatternMatcher(str, PatternMatcher.PATTERN_LITERAL);
1932 }
1933
1934 str = sa.getNonResourceString(
1935 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPrefix);
1936 if (str != null) {
1937 pa = new PatternMatcher(str, PatternMatcher.PATTERN_PREFIX);
1938 }
1939
1940 str = sa.getNonResourceString(
1941 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPattern);
1942 if (str != null) {
1943 pa = new PatternMatcher(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
1944 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07001945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 sa.recycle();
1947
1948 if (pa != null) {
1949 if (outInfo.info.uriPermissionPatterns == null) {
1950 outInfo.info.uriPermissionPatterns = new PatternMatcher[1];
1951 outInfo.info.uriPermissionPatterns[0] = pa;
1952 } else {
1953 final int N = outInfo.info.uriPermissionPatterns.length;
1954 PatternMatcher[] newp = new PatternMatcher[N+1];
1955 System.arraycopy(outInfo.info.uriPermissionPatterns, 0, newp, 0, N);
1956 newp[N] = pa;
1957 outInfo.info.uriPermissionPatterns = newp;
1958 }
1959 outInfo.info.grantUriPermissions = true;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07001960 } else {
1961 if (!RIGID_PARSER) {
1962 Log.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
1963 Log.w(TAG, "No path, pathPrefix, or pathPattern for <path-permission>");
1964 XmlUtils.skipCurrentTag(parser);
1965 continue;
1966 }
1967 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
1968 return false;
1969 }
1970 XmlUtils.skipCurrentTag(parser);
1971
1972 } else if (parser.getName().equals("path-permission")) {
1973 TypedArray sa = res.obtainAttributes(attrs,
1974 com.android.internal.R.styleable.AndroidManifestPathPermission);
1975
1976 PathPermission pa = null;
1977
1978 String permission = sa.getNonResourceString(
1979 com.android.internal.R.styleable.AndroidManifestPathPermission_permission);
1980 String readPermission = sa.getNonResourceString(
1981 com.android.internal.R.styleable.AndroidManifestPathPermission_readPermission);
1982 if (readPermission == null) {
1983 readPermission = permission;
1984 }
1985 String writePermission = sa.getNonResourceString(
1986 com.android.internal.R.styleable.AndroidManifestPathPermission_writePermission);
1987 if (writePermission == null) {
1988 writePermission = permission;
1989 }
1990
1991 boolean havePerm = false;
1992 if (readPermission != null) {
1993 readPermission = readPermission.intern();
1994 havePerm = true;
1995 }
1996 if (writePermission != null) {
1997 writePermission = readPermission.intern();
1998 havePerm = true;
1999 }
2000
2001 if (!havePerm) {
2002 if (!RIGID_PARSER) {
2003 Log.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
2004 Log.w(TAG, "No readPermission or writePermssion for <path-permission>");
2005 XmlUtils.skipCurrentTag(parser);
2006 continue;
2007 }
2008 outError[0] = "No readPermission or writePermssion for <path-permission>";
2009 return false;
2010 }
2011
2012 String path = sa.getNonResourceString(
2013 com.android.internal.R.styleable.AndroidManifestPathPermission_path);
2014 if (path != null) {
2015 pa = new PathPermission(path,
2016 PatternMatcher.PATTERN_LITERAL, readPermission, writePermission);
2017 }
2018
2019 path = sa.getNonResourceString(
2020 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPrefix);
2021 if (path != null) {
2022 pa = new PathPermission(path,
2023 PatternMatcher.PATTERN_PREFIX, readPermission, writePermission);
2024 }
2025
2026 path = sa.getNonResourceString(
2027 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPattern);
2028 if (path != null) {
2029 pa = new PathPermission(path,
2030 PatternMatcher.PATTERN_SIMPLE_GLOB, readPermission, writePermission);
2031 }
2032
2033 sa.recycle();
2034
2035 if (pa != null) {
2036 if (outInfo.info.pathPermissions == null) {
2037 outInfo.info.pathPermissions = new PathPermission[1];
2038 outInfo.info.pathPermissions[0] = pa;
2039 } else {
2040 final int N = outInfo.info.pathPermissions.length;
2041 PathPermission[] newp = new PathPermission[N+1];
2042 System.arraycopy(outInfo.info.pathPermissions, 0, newp, 0, N);
2043 newp[N] = pa;
2044 outInfo.info.pathPermissions = newp;
2045 }
2046 } else {
2047 if (!RIGID_PARSER) {
2048 Log.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
2049 Log.w(TAG, "No path, pathPrefix, or pathPattern for <path-permission>");
2050 XmlUtils.skipCurrentTag(parser);
2051 continue;
2052 }
2053 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
2054 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 }
2056 XmlUtils.skipCurrentTag(parser);
2057
2058 } else {
2059 if (!RIGID_PARSER) {
2060 Log.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
2061 Log.w(TAG, "Unknown element under <provider>: "
2062 + parser.getName());
2063 XmlUtils.skipCurrentTag(parser);
2064 continue;
2065 }
2066 outError[0] = "Bad element under <provider>: "
2067 + parser.getName();
2068 return false;
2069 }
2070 }
2071 return true;
2072 }
2073
2074 private Service parseService(Package owner, Resources res,
2075 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2076 throws XmlPullParserException, IOException {
2077 TypedArray sa = res.obtainAttributes(attrs,
2078 com.android.internal.R.styleable.AndroidManifestService);
2079
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002080 if (mParseServiceArgs == null) {
2081 mParseServiceArgs = new ParseComponentArgs(owner, outError,
2082 com.android.internal.R.styleable.AndroidManifestService_name,
2083 com.android.internal.R.styleable.AndroidManifestService_label,
2084 com.android.internal.R.styleable.AndroidManifestService_icon,
2085 mSeparateProcesses,
2086 com.android.internal.R.styleable.AndroidManifestService_process,
2087 com.android.internal.R.styleable.AndroidManifestService_enabled);
2088 mParseServiceArgs.tag = "<service>";
2089 }
2090
2091 mParseServiceArgs.sa = sa;
2092 mParseServiceArgs.flags = flags;
2093
2094 Service s = new Service(mParseServiceArgs, new ServiceInfo());
2095 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 sa.recycle();
2097 return null;
2098 }
2099
2100 final boolean setExported = sa.hasValue(
2101 com.android.internal.R.styleable.AndroidManifestService_exported);
2102 if (setExported) {
2103 s.info.exported = sa.getBoolean(
2104 com.android.internal.R.styleable.AndroidManifestService_exported, false);
2105 }
2106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 String str = sa.getNonResourceString(
2108 com.android.internal.R.styleable.AndroidManifestService_permission);
2109 if (str == null) {
2110 s.info.permission = owner.applicationInfo.permission;
2111 } else {
2112 s.info.permission = str.length() > 0 ? str.toString().intern() : null;
2113 }
2114
2115 sa.recycle();
2116
2117 int outerDepth = parser.getDepth();
2118 int type;
2119 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2120 && (type != XmlPullParser.END_TAG
2121 || parser.getDepth() > outerDepth)) {
2122 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2123 continue;
2124 }
2125
2126 if (parser.getName().equals("intent-filter")) {
2127 ServiceIntentInfo intent = new ServiceIntentInfo(s);
2128 if (!parseIntent(res, parser, attrs, flags, intent, outError, false)) {
2129 return null;
2130 }
2131
2132 s.intents.add(intent);
2133 } else if (parser.getName().equals("meta-data")) {
2134 if ((s.metaData=parseMetaData(res, parser, attrs, s.metaData,
2135 outError)) == null) {
2136 return null;
2137 }
2138 } else {
2139 if (!RIGID_PARSER) {
2140 Log.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
2141 Log.w(TAG, "Unknown element under <service>: "
2142 + parser.getName());
2143 XmlUtils.skipCurrentTag(parser);
2144 continue;
2145 }
2146 outError[0] = "Bad element under <service>: "
2147 + parser.getName();
2148 return null;
2149 }
2150 }
2151
2152 if (!setExported) {
2153 s.info.exported = s.intents.size() > 0;
2154 }
2155
2156 return s;
2157 }
2158
2159 private boolean parseAllMetaData(Resources res,
2160 XmlPullParser parser, AttributeSet attrs, String tag,
2161 Component outInfo, String[] outError)
2162 throws XmlPullParserException, IOException {
2163 int outerDepth = parser.getDepth();
2164 int type;
2165 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2166 && (type != XmlPullParser.END_TAG
2167 || parser.getDepth() > outerDepth)) {
2168 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2169 continue;
2170 }
2171
2172 if (parser.getName().equals("meta-data")) {
2173 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
2174 outInfo.metaData, outError)) == null) {
2175 return false;
2176 }
2177 } else {
2178 if (!RIGID_PARSER) {
2179 Log.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
2180 Log.w(TAG, "Unknown element under " + tag + ": "
2181 + parser.getName());
2182 XmlUtils.skipCurrentTag(parser);
2183 continue;
2184 }
2185 outError[0] = "Bad element under " + tag + ": "
2186 + parser.getName();
2187 return false;
2188 }
2189 }
2190 return true;
2191 }
2192
2193 private Bundle parseMetaData(Resources res,
2194 XmlPullParser parser, AttributeSet attrs,
2195 Bundle data, String[] outError)
2196 throws XmlPullParserException, IOException {
2197
2198 TypedArray sa = res.obtainAttributes(attrs,
2199 com.android.internal.R.styleable.AndroidManifestMetaData);
2200
2201 if (data == null) {
2202 data = new Bundle();
2203 }
2204
2205 String name = sa.getNonResourceString(
2206 com.android.internal.R.styleable.AndroidManifestMetaData_name);
2207 if (name == null) {
2208 outError[0] = "<meta-data> requires an android:name attribute";
2209 sa.recycle();
2210 return null;
2211 }
2212
2213 boolean success = true;
2214
2215 TypedValue v = sa.peekValue(
2216 com.android.internal.R.styleable.AndroidManifestMetaData_resource);
2217 if (v != null && v.resourceId != 0) {
2218 //Log.i(TAG, "Meta data ref " + name + ": " + v);
2219 data.putInt(name, v.resourceId);
2220 } else {
2221 v = sa.peekValue(
2222 com.android.internal.R.styleable.AndroidManifestMetaData_value);
2223 //Log.i(TAG, "Meta data " + name + ": " + v);
2224 if (v != null) {
2225 if (v.type == TypedValue.TYPE_STRING) {
2226 CharSequence cs = v.coerceToString();
2227 data.putString(name, cs != null ? cs.toString() : null);
2228 } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
2229 data.putBoolean(name, v.data != 0);
2230 } else if (v.type >= TypedValue.TYPE_FIRST_INT
2231 && v.type <= TypedValue.TYPE_LAST_INT) {
2232 data.putInt(name, v.data);
2233 } else if (v.type == TypedValue.TYPE_FLOAT) {
2234 data.putFloat(name, v.getFloat());
2235 } else {
2236 if (!RIGID_PARSER) {
2237 Log.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
2238 Log.w(TAG, "<meta-data> only supports string, integer, float, color, boolean, and resource reference types");
2239 } else {
2240 outError[0] = "<meta-data> only supports string, integer, float, color, boolean, and resource reference types";
2241 data = null;
2242 }
2243 }
2244 } else {
2245 outError[0] = "<meta-data> requires an android:value or android:resource attribute";
2246 data = null;
2247 }
2248 }
2249
2250 sa.recycle();
2251
2252 XmlUtils.skipCurrentTag(parser);
2253
2254 return data;
2255 }
2256
2257 private static final String ANDROID_RESOURCES
2258 = "http://schemas.android.com/apk/res/android";
2259
2260 private boolean parseIntent(Resources res,
2261 XmlPullParser parser, AttributeSet attrs, int flags,
2262 IntentInfo outInfo, String[] outError, boolean isActivity)
2263 throws XmlPullParserException, IOException {
2264
2265 TypedArray sa = res.obtainAttributes(attrs,
2266 com.android.internal.R.styleable.AndroidManifestIntentFilter);
2267
2268 int priority = sa.getInt(
2269 com.android.internal.R.styleable.AndroidManifestIntentFilter_priority, 0);
2270 if (priority > 0 && isActivity && (flags&PARSE_IS_SYSTEM) == 0) {
2271 Log.w(TAG, "Activity with priority > 0, forcing to 0 at "
2272 + parser.getPositionDescription());
2273 priority = 0;
2274 }
2275 outInfo.setPriority(priority);
2276
2277 TypedValue v = sa.peekValue(
2278 com.android.internal.R.styleable.AndroidManifestIntentFilter_label);
2279 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
2280 outInfo.nonLocalizedLabel = v.coerceToString();
2281 }
2282
2283 outInfo.icon = sa.getResourceId(
2284 com.android.internal.R.styleable.AndroidManifestIntentFilter_icon, 0);
2285
2286 sa.recycle();
2287
2288 int outerDepth = parser.getDepth();
2289 int type;
2290 while ((type=parser.next()) != parser.END_DOCUMENT
2291 && (type != parser.END_TAG || parser.getDepth() > outerDepth)) {
2292 if (type == parser.END_TAG || type == parser.TEXT) {
2293 continue;
2294 }
2295
2296 String nodeName = parser.getName();
2297 if (nodeName.equals("action")) {
2298 String value = attrs.getAttributeValue(
2299 ANDROID_RESOURCES, "name");
2300 if (value == null || value == "") {
2301 outError[0] = "No value supplied for <android:name>";
2302 return false;
2303 }
2304 XmlUtils.skipCurrentTag(parser);
2305
2306 outInfo.addAction(value);
2307 } else if (nodeName.equals("category")) {
2308 String value = attrs.getAttributeValue(
2309 ANDROID_RESOURCES, "name");
2310 if (value == null || value == "") {
2311 outError[0] = "No value supplied for <android:name>";
2312 return false;
2313 }
2314 XmlUtils.skipCurrentTag(parser);
2315
2316 outInfo.addCategory(value);
2317
2318 } else if (nodeName.equals("data")) {
2319 sa = res.obtainAttributes(attrs,
2320 com.android.internal.R.styleable.AndroidManifestData);
2321
2322 String str = sa.getNonResourceString(
2323 com.android.internal.R.styleable.AndroidManifestData_mimeType);
2324 if (str != null) {
2325 try {
2326 outInfo.addDataType(str);
2327 } catch (IntentFilter.MalformedMimeTypeException e) {
2328 outError[0] = e.toString();
2329 sa.recycle();
2330 return false;
2331 }
2332 }
2333
2334 str = sa.getNonResourceString(
2335 com.android.internal.R.styleable.AndroidManifestData_scheme);
2336 if (str != null) {
2337 outInfo.addDataScheme(str);
2338 }
2339
2340 String host = sa.getNonResourceString(
2341 com.android.internal.R.styleable.AndroidManifestData_host);
2342 String port = sa.getNonResourceString(
2343 com.android.internal.R.styleable.AndroidManifestData_port);
2344 if (host != null) {
2345 outInfo.addDataAuthority(host, port);
2346 }
2347
2348 str = sa.getNonResourceString(
2349 com.android.internal.R.styleable.AndroidManifestData_path);
2350 if (str != null) {
2351 outInfo.addDataPath(str, PatternMatcher.PATTERN_LITERAL);
2352 }
2353
2354 str = sa.getNonResourceString(
2355 com.android.internal.R.styleable.AndroidManifestData_pathPrefix);
2356 if (str != null) {
2357 outInfo.addDataPath(str, PatternMatcher.PATTERN_PREFIX);
2358 }
2359
2360 str = sa.getNonResourceString(
2361 com.android.internal.R.styleable.AndroidManifestData_pathPattern);
2362 if (str != null) {
2363 outInfo.addDataPath(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
2364 }
2365
2366 sa.recycle();
2367 XmlUtils.skipCurrentTag(parser);
2368 } else if (!RIGID_PARSER) {
2369 Log.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
2370 Log.w(TAG, "Unknown element under <intent-filter>: " + parser.getName());
2371 XmlUtils.skipCurrentTag(parser);
2372 } else {
2373 outError[0] = "Bad element under <intent-filter>: " + parser.getName();
2374 return false;
2375 }
2376 }
2377
2378 outInfo.hasDefault = outInfo.hasCategory(Intent.CATEGORY_DEFAULT);
2379 if (false) {
2380 String cats = "";
2381 Iterator<String> it = outInfo.categoriesIterator();
2382 while (it != null && it.hasNext()) {
2383 cats += " " + it.next();
2384 }
2385 System.out.println("Intent d=" +
2386 outInfo.hasDefault + ", cat=" + cats);
2387 }
2388
2389 return true;
2390 }
2391
2392 public final static class Package {
2393 public final String packageName;
2394
2395 // For now we only support one application per package.
2396 public final ApplicationInfo applicationInfo = new ApplicationInfo();
2397
2398 public final ArrayList<Permission> permissions = new ArrayList<Permission>(0);
2399 public final ArrayList<PermissionGroup> permissionGroups = new ArrayList<PermissionGroup>(0);
2400 public final ArrayList<Activity> activities = new ArrayList<Activity>(0);
2401 public final ArrayList<Activity> receivers = new ArrayList<Activity>(0);
2402 public final ArrayList<Provider> providers = new ArrayList<Provider>(0);
2403 public final ArrayList<Service> services = new ArrayList<Service>(0);
2404 public final ArrayList<Instrumentation> instrumentation = new ArrayList<Instrumentation>(0);
2405
2406 public final ArrayList<String> requestedPermissions = new ArrayList<String>();
2407
2408 public final ArrayList<String> usesLibraries = new ArrayList<String>();
2409 public String[] usesLibraryFiles = null;
2410
2411 // We store the application meta-data independently to avoid multiple unwanted references
2412 public Bundle mAppMetaData = null;
2413
Mitsuru Oshima8d112672009-04-27 12:01:23 -07002414 public final ArrayList<Integer> supportsDensityList = new ArrayList<Integer>();
2415 public int[] supportsDensities = null;
2416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 // If this is a 3rd party app, this is the path of the zip file.
2418 public String mPath;
2419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 // The version code declared for this package.
2421 public int mVersionCode;
2422
2423 // The version name declared for this package.
2424 public String mVersionName;
2425
2426 // The shared user id that this package wants to use.
2427 public String mSharedUserId;
2428
2429 // The shared user label that this package wants to use.
2430 public int mSharedUserLabel;
2431
2432 // Signatures that were read from the package.
2433 public Signature mSignatures[];
2434
2435 // For use by package manager service for quick lookup of
2436 // preferred up order.
2437 public int mPreferredOrder = 0;
2438
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07002439 // For use by package manager service to keep track of which apps
2440 // have been installed with forward locking.
2441 public boolean mForwardLocked;
2442
2443 // For use by the package manager to keep track of the path to the
2444 // file an app came from.
2445 public String mScanPath;
2446
2447 // For use by package manager to keep track of where it has done dexopt.
2448 public boolean mDidDexOpt;
2449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 // Additional data supplied by callers.
2451 public Object mExtras;
2452
2453 /*
2454 * Applications hardware preferences
2455 */
2456 public final ArrayList<ConfigurationInfo> configPreferences =
2457 new ArrayList<ConfigurationInfo>();
2458
2459 public Package(String _name) {
2460 packageName = _name;
2461 applicationInfo.packageName = _name;
2462 applicationInfo.uid = -1;
2463 }
2464
2465 public String toString() {
2466 return "Package{"
2467 + Integer.toHexString(System.identityHashCode(this))
2468 + " " + packageName + "}";
2469 }
2470 }
2471
2472 public static class Component<II extends IntentInfo> {
2473 public final Package owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002474 public final ArrayList<II> intents;
2475 public final ComponentName component;
2476 public final String componentShortName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 public Bundle metaData;
2478
2479 public Component(Package _owner) {
2480 owner = _owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002481 intents = null;
2482 component = null;
2483 componentShortName = null;
2484 }
2485
2486 public Component(final ParsePackageItemArgs args, final PackageItemInfo outInfo) {
2487 owner = args.owner;
2488 intents = new ArrayList<II>(0);
2489 String name = args.sa.getNonResourceString(args.nameRes);
2490 if (name == null) {
2491 component = null;
2492 componentShortName = null;
2493 args.outError[0] = args.tag + " does not specify android:name";
2494 return;
2495 }
2496
2497 outInfo.name
2498 = buildClassName(owner.applicationInfo.packageName, name, args.outError);
2499 if (outInfo.name == null) {
2500 component = null;
2501 componentShortName = null;
2502 args.outError[0] = args.tag + " does not have valid android:name";
2503 return;
2504 }
2505
2506 component = new ComponentName(owner.applicationInfo.packageName,
2507 outInfo.name);
2508 componentShortName = component.flattenToShortString();
2509
2510 int iconVal = args.sa.getResourceId(args.iconRes, 0);
2511 if (iconVal != 0) {
2512 outInfo.icon = iconVal;
2513 outInfo.nonLocalizedLabel = null;
2514 }
2515
2516 TypedValue v = args.sa.peekValue(args.labelRes);
2517 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
2518 outInfo.nonLocalizedLabel = v.coerceToString();
2519 }
2520
2521 outInfo.packageName = owner.packageName;
2522 }
2523
2524 public Component(final ParseComponentArgs args, final ComponentInfo outInfo) {
2525 this(args, (PackageItemInfo)outInfo);
2526 if (args.outError[0] != null) {
2527 return;
2528 }
2529
2530 if (args.processRes != 0) {
2531 outInfo.processName = buildProcessName(owner.applicationInfo.packageName,
2532 owner.applicationInfo.processName, args.sa.getNonResourceString(args.processRes),
2533 args.flags, args.sepProcesses, args.outError);
2534 }
2535 outInfo.enabled = args.sa.getBoolean(args.enabledRes, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 }
2537
2538 public Component(Component<II> clone) {
2539 owner = clone.owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002540 intents = clone.intents;
2541 component = clone.component;
2542 componentShortName = clone.componentShortName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 metaData = clone.metaData;
2544 }
2545 }
2546
2547 public final static class Permission extends Component<IntentInfo> {
2548 public final PermissionInfo info;
2549 public boolean tree;
2550 public PermissionGroup group;
2551
2552 public Permission(Package _owner) {
2553 super(_owner);
2554 info = new PermissionInfo();
2555 }
2556
2557 public Permission(Package _owner, PermissionInfo _info) {
2558 super(_owner);
2559 info = _info;
2560 }
2561
2562 public String toString() {
2563 return "Permission{"
2564 + Integer.toHexString(System.identityHashCode(this))
2565 + " " + info.name + "}";
2566 }
2567 }
2568
2569 public final static class PermissionGroup extends Component<IntentInfo> {
2570 public final PermissionGroupInfo info;
2571
2572 public PermissionGroup(Package _owner) {
2573 super(_owner);
2574 info = new PermissionGroupInfo();
2575 }
2576
2577 public PermissionGroup(Package _owner, PermissionGroupInfo _info) {
2578 super(_owner);
2579 info = _info;
2580 }
2581
2582 public String toString() {
2583 return "PermissionGroup{"
2584 + Integer.toHexString(System.identityHashCode(this))
2585 + " " + info.name + "}";
2586 }
2587 }
2588
2589 private static boolean copyNeeded(int flags, Package p, Bundle metaData) {
2590 if ((flags & PackageManager.GET_META_DATA) != 0
2591 && (metaData != null || p.mAppMetaData != null)) {
2592 return true;
2593 }
2594 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0
2595 && p.usesLibraryFiles != null) {
2596 return true;
2597 }
Mitsuru Oshima8d112672009-04-27 12:01:23 -07002598 if ((flags & PackageManager.GET_SUPPORTS_DENSITIES) != 0
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002599 && p.supportsDensities != null) {
2600 return true;
2601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 return false;
2603 }
2604
2605 public static ApplicationInfo generateApplicationInfo(Package p, int flags) {
2606 if (p == null) return null;
2607 if (!copyNeeded(flags, p, null)) {
2608 return p.applicationInfo;
2609 }
2610
2611 // Make shallow copy so we can store the metadata/libraries safely
2612 ApplicationInfo ai = new ApplicationInfo(p.applicationInfo);
2613 if ((flags & PackageManager.GET_META_DATA) != 0) {
2614 ai.metaData = p.mAppMetaData;
2615 }
2616 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0) {
2617 ai.sharedLibraryFiles = p.usesLibraryFiles;
2618 }
Mitsuru Oshima8d112672009-04-27 12:01:23 -07002619 if ((flags & PackageManager.GET_SUPPORTS_DENSITIES) != 0) {
2620 ai.supportsDensities = p.supportsDensities;
2621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 return ai;
2623 }
2624
2625 public static final PermissionInfo generatePermissionInfo(
2626 Permission p, int flags) {
2627 if (p == null) return null;
2628 if ((flags&PackageManager.GET_META_DATA) == 0) {
2629 return p.info;
2630 }
2631 PermissionInfo pi = new PermissionInfo(p.info);
2632 pi.metaData = p.metaData;
2633 return pi;
2634 }
2635
2636 public static final PermissionGroupInfo generatePermissionGroupInfo(
2637 PermissionGroup pg, int flags) {
2638 if (pg == null) return null;
2639 if ((flags&PackageManager.GET_META_DATA) == 0) {
2640 return pg.info;
2641 }
2642 PermissionGroupInfo pgi = new PermissionGroupInfo(pg.info);
2643 pgi.metaData = pg.metaData;
2644 return pgi;
2645 }
2646
2647 public final static class Activity extends Component<ActivityIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002648 public final ActivityInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002650 public Activity(final ParseComponentArgs args, final ActivityInfo _info) {
2651 super(args, _info);
2652 info = _info;
2653 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 public String toString() {
2657 return "Activity{"
2658 + Integer.toHexString(System.identityHashCode(this))
2659 + " " + component.flattenToString() + "}";
2660 }
2661 }
2662
2663 public static final ActivityInfo generateActivityInfo(Activity a,
2664 int flags) {
2665 if (a == null) return null;
2666 if (!copyNeeded(flags, a.owner, a.metaData)) {
2667 return a.info;
2668 }
2669 // Make shallow copies so we can store the metadata safely
2670 ActivityInfo ai = new ActivityInfo(a.info);
2671 ai.metaData = a.metaData;
2672 ai.applicationInfo = generateApplicationInfo(a.owner, flags);
2673 return ai;
2674 }
2675
2676 public final static class Service extends Component<ServiceIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002677 public final ServiceInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002679 public Service(final ParseComponentArgs args, final ServiceInfo _info) {
2680 super(args, _info);
2681 info = _info;
2682 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 public String toString() {
2686 return "Service{"
2687 + Integer.toHexString(System.identityHashCode(this))
2688 + " " + component.flattenToString() + "}";
2689 }
2690 }
2691
2692 public static final ServiceInfo generateServiceInfo(Service s, int flags) {
2693 if (s == null) return null;
2694 if (!copyNeeded(flags, s.owner, s.metaData)) {
2695 return s.info;
2696 }
2697 // Make shallow copies so we can store the metadata safely
2698 ServiceInfo si = new ServiceInfo(s.info);
2699 si.metaData = s.metaData;
2700 si.applicationInfo = generateApplicationInfo(s.owner, flags);
2701 return si;
2702 }
2703
2704 public final static class Provider extends Component {
2705 public final ProviderInfo info;
2706 public boolean syncable;
2707
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002708 public Provider(final ParseComponentArgs args, final ProviderInfo _info) {
2709 super(args, _info);
2710 info = _info;
2711 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 syncable = false;
2713 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 public Provider(Provider existingProvider) {
2716 super(existingProvider);
2717 this.info = existingProvider.info;
2718 this.syncable = existingProvider.syncable;
2719 }
2720
2721 public String toString() {
2722 return "Provider{"
2723 + Integer.toHexString(System.identityHashCode(this))
2724 + " " + info.name + "}";
2725 }
2726 }
2727
2728 public static final ProviderInfo generateProviderInfo(Provider p,
2729 int flags) {
2730 if (p == null) return null;
2731 if (!copyNeeded(flags, p.owner, p.metaData)
2732 && ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) != 0
2733 || p.info.uriPermissionPatterns == null)) {
2734 return p.info;
2735 }
2736 // Make shallow copies so we can store the metadata safely
2737 ProviderInfo pi = new ProviderInfo(p.info);
2738 pi.metaData = p.metaData;
2739 if ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) == 0) {
2740 pi.uriPermissionPatterns = null;
2741 }
2742 pi.applicationInfo = generateApplicationInfo(p.owner, flags);
2743 return pi;
2744 }
2745
2746 public final static class Instrumentation extends Component {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002747 public final InstrumentationInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002749 public Instrumentation(final ParsePackageItemArgs args, final InstrumentationInfo _info) {
2750 super(args, _info);
2751 info = _info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 public String toString() {
2755 return "Instrumentation{"
2756 + Integer.toHexString(System.identityHashCode(this))
2757 + " " + component.flattenToString() + "}";
2758 }
2759 }
2760
2761 public static final InstrumentationInfo generateInstrumentationInfo(
2762 Instrumentation i, int flags) {
2763 if (i == null) return null;
2764 if ((flags&PackageManager.GET_META_DATA) == 0) {
2765 return i.info;
2766 }
2767 InstrumentationInfo ii = new InstrumentationInfo(i.info);
2768 ii.metaData = i.metaData;
2769 return ii;
2770 }
2771
2772 public static class IntentInfo extends IntentFilter {
2773 public boolean hasDefault;
2774 public int labelRes;
2775 public CharSequence nonLocalizedLabel;
2776 public int icon;
2777 }
2778
2779 public final static class ActivityIntentInfo extends IntentInfo {
2780 public final Activity activity;
2781
2782 public ActivityIntentInfo(Activity _activity) {
2783 activity = _activity;
2784 }
2785
2786 public String toString() {
2787 return "ActivityIntentInfo{"
2788 + Integer.toHexString(System.identityHashCode(this))
2789 + " " + activity.info.name + "}";
2790 }
2791 }
2792
2793 public final static class ServiceIntentInfo extends IntentInfo {
2794 public final Service service;
2795
2796 public ServiceIntentInfo(Service _service) {
2797 service = _service;
2798 }
2799
2800 public String toString() {
2801 return "ServiceIntentInfo{"
2802 + Integer.toHexString(System.identityHashCode(this))
2803 + " " + service.info.name + "}";
2804 }
2805 }
2806}