blob: b07bafcff2e9797074eee07d80ed0e9108ec6ca9 [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;
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -070030import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.os.Bundle;
32import android.os.PatternMatcher;
Suchi Amalapurapuaaec7792010-02-25 11:49:43 -080033import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.util.AttributeSet;
35import android.util.Config;
36import android.util.DisplayMetrics;
37import android.util.Log;
38import android.util.TypedValue;
Dianne Hackborn2269d152010-02-24 19:54:22 -080039
40import com.android.internal.util.XmlUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42import java.io.File;
43import java.io.IOException;
44import java.io.InputStream;
45import java.lang.ref.WeakReference;
46import java.security.cert.Certificate;
47import java.security.cert.CertificateEncodingException;
48import java.util.ArrayList;
49import java.util.Enumeration;
50import java.util.Iterator;
Mitsuru Oshima8d112672009-04-27 12:01:23 -070051import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import java.util.jar.JarEntry;
53import java.util.jar.JarFile;
54
55/**
56 * Package archive parsing
57 *
58 * {@hide}
59 */
60public class PackageParser {
Dianne Hackborna96cbb42009-05-13 15:06:13 -070061 /** @hide */
62 public static class NewPermissionInfo {
63 public final String name;
64 public final int sdkVersion;
65 public final int fileVersion;
66
67 public NewPermissionInfo(String name, int sdkVersion, int fileVersion) {
68 this.name = name;
69 this.sdkVersion = sdkVersion;
70 this.fileVersion = fileVersion;
71 }
72 }
73
74 /**
75 * List of new permissions that have been added since 1.0.
76 * NOTE: These must be declared in SDK version order, with permissions
77 * added to older SDKs appearing before those added to newer SDKs.
78 * @hide
79 */
Jaikumar Ganesh45515652009-04-23 15:20:21 -070080 public static final PackageParser.NewPermissionInfo NEW_PERMISSIONS[] =
81 new PackageParser.NewPermissionInfo[] {
San Mehat5a3a77d2009-06-01 09:25:28 -070082 new PackageParser.NewPermissionInfo(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
Jaikumar Ganesh45515652009-04-23 15:20:21 -070083 android.os.Build.VERSION_CODES.DONUT, 0),
84 new PackageParser.NewPermissionInfo(android.Manifest.permission.READ_PHONE_STATE,
85 android.os.Build.VERSION_CODES.DONUT, 0)
Dianne Hackborna96cbb42009-05-13 15:06:13 -070086 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88 private String mArchiveSourcePath;
89 private String[] mSeparateProcesses;
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -070090 private static final int SDK_VERSION = Build.VERSION.SDK_INT;
91 private static final String SDK_CODENAME = "REL".equals(Build.VERSION.CODENAME)
92 ? null : Build.VERSION.CODENAME;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94 private int mParseError = PackageManager.INSTALL_SUCCEEDED;
95
96 private static final Object mSync = new Object();
97 private static WeakReference<byte[]> mReadBuffer;
98
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -070099 private static boolean sCompatibilityModeEnabled = true;
100
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700101 static class ParsePackageItemArgs {
102 final Package owner;
103 final String[] outError;
104 final int nameRes;
105 final int labelRes;
106 final int iconRes;
107
108 String tag;
109 TypedArray sa;
110
111 ParsePackageItemArgs(Package _owner, String[] _outError,
112 int _nameRes, int _labelRes, int _iconRes) {
113 owner = _owner;
114 outError = _outError;
115 nameRes = _nameRes;
116 labelRes = _labelRes;
117 iconRes = _iconRes;
118 }
119 }
120
121 static class ParseComponentArgs extends ParsePackageItemArgs {
122 final String[] sepProcesses;
123 final int processRes;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800124 final int descriptionRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700125 final int enabledRes;
126 int flags;
127
128 ParseComponentArgs(Package _owner, String[] _outError,
129 int _nameRes, int _labelRes, int _iconRes,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800130 String[] _sepProcesses, int _processRes,
131 int _descriptionRes, int _enabledRes) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700132 super(_owner, _outError, _nameRes, _labelRes, _iconRes);
133 sepProcesses = _sepProcesses;
134 processRes = _processRes;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800135 descriptionRes = _descriptionRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700136 enabledRes = _enabledRes;
137 }
138 }
139
140 private ParsePackageItemArgs mParseInstrumentationArgs;
141 private ParseComponentArgs mParseActivityArgs;
142 private ParseComponentArgs mParseActivityAliasArgs;
143 private ParseComponentArgs mParseServiceArgs;
144 private ParseComponentArgs mParseProviderArgs;
145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 /** If set to true, we will only allow package files that exactly match
147 * the DTD. Otherwise, we try to get as much from the package as we
148 * can without failing. This should normally be set to false, to
149 * support extensions to the DTD in future versions. */
150 private static final boolean RIGID_PARSER = false;
151
152 private static final String TAG = "PackageParser";
153
154 public PackageParser(String archiveSourcePath) {
155 mArchiveSourcePath = archiveSourcePath;
156 }
157
158 public void setSeparateProcesses(String[] procs) {
159 mSeparateProcesses = procs;
160 }
161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 private static final boolean isPackageFilename(String name) {
163 return name.endsWith(".apk");
164 }
165
166 /**
167 * Generate and return the {@link PackageInfo} for a parsed package.
168 *
169 * @param p the parsed package.
170 * @param flags indicating which optional information is included.
171 */
172 public static PackageInfo generatePackageInfo(PackageParser.Package p,
173 int gids[], int flags) {
174
175 PackageInfo pi = new PackageInfo();
176 pi.packageName = p.packageName;
177 pi.versionCode = p.mVersionCode;
178 pi.versionName = p.mVersionName;
179 pi.sharedUserId = p.mSharedUserId;
180 pi.sharedUserLabel = p.mSharedUserLabel;
181 pi.applicationInfo = p.applicationInfo;
Suchi Amalapurapu117818e2010-02-09 03:45:40 -0800182 pi.installLocation = p.installLocation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 if ((flags&PackageManager.GET_GIDS) != 0) {
184 pi.gids = gids;
185 }
186 if ((flags&PackageManager.GET_CONFIGURATIONS) != 0) {
187 int N = p.configPreferences.size();
188 if (N > 0) {
189 pi.configPreferences = new ConfigurationInfo[N];
Dianne Hackborn49237342009-08-27 20:08:01 -0700190 p.configPreferences.toArray(pi.configPreferences);
191 }
192 N = p.reqFeatures != null ? p.reqFeatures.size() : 0;
193 if (N > 0) {
194 pi.reqFeatures = new FeatureInfo[N];
195 p.reqFeatures.toArray(pi.reqFeatures);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 }
197 }
198 if ((flags&PackageManager.GET_ACTIVITIES) != 0) {
199 int N = p.activities.size();
200 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700201 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
202 pi.activities = new ActivityInfo[N];
203 } else {
204 int num = 0;
205 for (int i=0; i<N; i++) {
206 if (p.activities.get(i).info.enabled) num++;
207 }
208 pi.activities = new ActivityInfo[num];
209 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700210 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 final Activity activity = p.activities.get(i);
212 if (activity.info.enabled
213 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700214 pi.activities[j++] = generateActivityInfo(p.activities.get(i), flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 }
216 }
217 }
218 }
219 if ((flags&PackageManager.GET_RECEIVERS) != 0) {
220 int N = p.receivers.size();
221 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700222 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
223 pi.receivers = new ActivityInfo[N];
224 } else {
225 int num = 0;
226 for (int i=0; i<N; i++) {
227 if (p.receivers.get(i).info.enabled) num++;
228 }
229 pi.receivers = new ActivityInfo[num];
230 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700231 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 final Activity activity = p.receivers.get(i);
233 if (activity.info.enabled
234 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700235 pi.receivers[j++] = generateActivityInfo(p.receivers.get(i), flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 }
237 }
238 }
239 }
240 if ((flags&PackageManager.GET_SERVICES) != 0) {
241 int N = p.services.size();
242 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700243 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
244 pi.services = new ServiceInfo[N];
245 } else {
246 int num = 0;
247 for (int i=0; i<N; i++) {
248 if (p.services.get(i).info.enabled) num++;
249 }
250 pi.services = new ServiceInfo[num];
251 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700252 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 final Service service = p.services.get(i);
254 if (service.info.enabled
255 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700256 pi.services[j++] = generateServiceInfo(p.services.get(i), flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
258 }
259 }
260 }
261 if ((flags&PackageManager.GET_PROVIDERS) != 0) {
262 int N = p.providers.size();
263 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700264 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
265 pi.providers = new ProviderInfo[N];
266 } else {
267 int num = 0;
268 for (int i=0; i<N; i++) {
269 if (p.providers.get(i).info.enabled) num++;
270 }
271 pi.providers = new ProviderInfo[num];
272 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700273 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 final Provider provider = p.providers.get(i);
275 if (provider.info.enabled
276 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700277 pi.providers[j++] = generateProviderInfo(p.providers.get(i), flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 }
279 }
280 }
281 }
282 if ((flags&PackageManager.GET_INSTRUMENTATION) != 0) {
283 int N = p.instrumentation.size();
284 if (N > 0) {
285 pi.instrumentation = new InstrumentationInfo[N];
286 for (int i=0; i<N; i++) {
287 pi.instrumentation[i] = generateInstrumentationInfo(
288 p.instrumentation.get(i), flags);
289 }
290 }
291 }
292 if ((flags&PackageManager.GET_PERMISSIONS) != 0) {
293 int N = p.permissions.size();
294 if (N > 0) {
295 pi.permissions = new PermissionInfo[N];
296 for (int i=0; i<N; i++) {
297 pi.permissions[i] = generatePermissionInfo(p.permissions.get(i), flags);
298 }
299 }
300 N = p.requestedPermissions.size();
301 if (N > 0) {
302 pi.requestedPermissions = new String[N];
303 for (int i=0; i<N; i++) {
304 pi.requestedPermissions[i] = p.requestedPermissions.get(i);
305 }
306 }
307 }
308 if ((flags&PackageManager.GET_SIGNATURES) != 0) {
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700309 int N = (p.mSignatures != null) ? p.mSignatures.length : 0;
310 if (N > 0) {
311 pi.signatures = new Signature[N];
312 System.arraycopy(p.mSignatures, 0, pi.signatures, 0, N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 }
314 }
315 return pi;
316 }
317
318 private Certificate[] loadCertificates(JarFile jarFile, JarEntry je,
319 byte[] readBuffer) {
320 try {
321 // We must read the stream for the JarEntry to retrieve
322 // its certificates.
323 InputStream is = jarFile.getInputStream(je);
324 while (is.read(readBuffer, 0, readBuffer.length) != -1) {
325 // not using
326 }
327 is.close();
328 return je != null ? je.getCertificates() : null;
329 } catch (IOException e) {
330 Log.w(TAG, "Exception reading " + je.getName() + " in "
331 + jarFile.getName(), e);
332 }
333 return null;
334 }
335
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800336 public final static int PARSE_IS_SYSTEM = 1<<0;
337 public final static int PARSE_CHATTY = 1<<1;
338 public final static int PARSE_MUST_BE_APK = 1<<2;
339 public final static int PARSE_IGNORE_PROCESSES = 1<<3;
340 public final static int PARSE_FORWARD_LOCK = 1<<4;
341 public final static int PARSE_ON_SDCARD = 1<<5;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342
343 public int getParseError() {
344 return mParseError;
345 }
346
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800347 public Package parsePackage(File sourceFile, String destCodePath,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 DisplayMetrics metrics, int flags) {
349 mParseError = PackageManager.INSTALL_SUCCEEDED;
350
351 mArchiveSourcePath = sourceFile.getPath();
352 if (!sourceFile.isFile()) {
353 Log.w(TAG, "Skipping dir: " + mArchiveSourcePath);
354 mParseError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK;
355 return null;
356 }
357 if (!isPackageFilename(sourceFile.getName())
358 && (flags&PARSE_MUST_BE_APK) != 0) {
359 if ((flags&PARSE_IS_SYSTEM) == 0) {
360 // We expect to have non-.apk files in the system dir,
361 // so don't warn about them.
362 Log.w(TAG, "Skipping non-package file: " + mArchiveSourcePath);
363 }
364 mParseError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK;
365 return null;
366 }
367
368 if ((flags&PARSE_CHATTY) != 0 && Config.LOGD) Log.d(
369 TAG, "Scanning package: " + mArchiveSourcePath);
370
371 XmlResourceParser parser = null;
372 AssetManager assmgr = null;
373 boolean assetError = true;
374 try {
375 assmgr = new AssetManager();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700376 int cookie = assmgr.addAssetPath(mArchiveSourcePath);
377 if(cookie != 0) {
378 parser = assmgr.openXmlResourceParser(cookie, "AndroidManifest.xml");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 assetError = false;
380 } else {
381 Log.w(TAG, "Failed adding asset path:"+mArchiveSourcePath);
382 }
383 } catch (Exception e) {
384 Log.w(TAG, "Unable to read AndroidManifest.xml of "
385 + mArchiveSourcePath, e);
386 }
387 if(assetError) {
388 if (assmgr != null) assmgr.close();
389 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST;
390 return null;
391 }
392 String[] errorText = new String[1];
393 Package pkg = null;
394 Exception errorException = null;
395 try {
396 // XXXX todo: need to figure out correct configuration.
397 Resources res = new Resources(assmgr, metrics, null);
398 pkg = parsePackage(res, parser, flags, errorText);
399 } catch (Exception e) {
400 errorException = e;
401 mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
402 }
403
404
405 if (pkg == null) {
406 if (errorException != null) {
407 Log.w(TAG, mArchiveSourcePath, errorException);
408 } else {
409 Log.w(TAG, mArchiveSourcePath + " (at "
410 + parser.getPositionDescription()
411 + "): " + errorText[0]);
412 }
413 parser.close();
414 assmgr.close();
415 if (mParseError == PackageManager.INSTALL_SUCCEEDED) {
416 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
417 }
418 return null;
419 }
420
421 parser.close();
422 assmgr.close();
423
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800424 // Set code and resource paths
425 pkg.mPath = destCodePath;
426 pkg.mScanPath = mArchiveSourcePath;
427 //pkg.applicationInfo.sourceDir = destCodePath;
428 //pkg.applicationInfo.publicSourceDir = destRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 pkg.mSignatures = null;
430
431 return pkg;
432 }
433
434 public boolean collectCertificates(Package pkg, int flags) {
435 pkg.mSignatures = null;
436
437 WeakReference<byte[]> readBufferRef;
438 byte[] readBuffer = null;
439 synchronized (mSync) {
440 readBufferRef = mReadBuffer;
441 if (readBufferRef != null) {
442 mReadBuffer = null;
443 readBuffer = readBufferRef.get();
444 }
445 if (readBuffer == null) {
446 readBuffer = new byte[8192];
447 readBufferRef = new WeakReference<byte[]>(readBuffer);
448 }
449 }
450
451 try {
452 JarFile jarFile = new JarFile(mArchiveSourcePath);
453
454 Certificate[] certs = null;
455
456 if ((flags&PARSE_IS_SYSTEM) != 0) {
457 // If this package comes from the system image, then we
458 // can trust it... we'll just use the AndroidManifest.xml
459 // to retrieve its signatures, not validating all of the
460 // files.
461 JarEntry jarEntry = jarFile.getJarEntry("AndroidManifest.xml");
462 certs = loadCertificates(jarFile, jarEntry, readBuffer);
463 if (certs == null) {
464 Log.e(TAG, "Package " + pkg.packageName
465 + " has no certificates at entry "
466 + jarEntry.getName() + "; ignoring!");
467 jarFile.close();
468 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
469 return false;
470 }
471 if (false) {
472 Log.i(TAG, "File " + mArchiveSourcePath + ": entry=" + jarEntry
473 + " certs=" + (certs != null ? certs.length : 0));
474 if (certs != null) {
475 final int N = certs.length;
476 for (int i=0; i<N; i++) {
477 Log.i(TAG, " Public key: "
478 + certs[i].getPublicKey().getEncoded()
479 + " " + certs[i].getPublicKey());
480 }
481 }
482 }
483
484 } else {
485 Enumeration entries = jarFile.entries();
486 while (entries.hasMoreElements()) {
487 JarEntry je = (JarEntry)entries.nextElement();
488 if (je.isDirectory()) continue;
489 if (je.getName().startsWith("META-INF/")) continue;
490 Certificate[] localCerts = loadCertificates(jarFile, je,
491 readBuffer);
492 if (false) {
493 Log.i(TAG, "File " + mArchiveSourcePath + " entry " + je.getName()
494 + ": certs=" + certs + " ("
495 + (certs != null ? certs.length : 0) + ")");
496 }
497 if (localCerts == null) {
498 Log.e(TAG, "Package " + pkg.packageName
499 + " has no certificates at entry "
500 + je.getName() + "; ignoring!");
501 jarFile.close();
502 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
503 return false;
504 } else if (certs == null) {
505 certs = localCerts;
506 } else {
507 // Ensure all certificates match.
508 for (int i=0; i<certs.length; i++) {
509 boolean found = false;
510 for (int j=0; j<localCerts.length; j++) {
511 if (certs[i] != null &&
512 certs[i].equals(localCerts[j])) {
513 found = true;
514 break;
515 }
516 }
517 if (!found || certs.length != localCerts.length) {
518 Log.e(TAG, "Package " + pkg.packageName
519 + " has mismatched certificates at entry "
520 + je.getName() + "; ignoring!");
521 jarFile.close();
522 mParseError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
523 return false;
524 }
525 }
526 }
527 }
528 }
529 jarFile.close();
530
531 synchronized (mSync) {
532 mReadBuffer = readBufferRef;
533 }
534
535 if (certs != null && certs.length > 0) {
536 final int N = certs.length;
537 pkg.mSignatures = new Signature[certs.length];
538 for (int i=0; i<N; i++) {
539 pkg.mSignatures[i] = new Signature(
540 certs[i].getEncoded());
541 }
542 } else {
543 Log.e(TAG, "Package " + pkg.packageName
544 + " has no certificates; ignoring!");
545 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
546 return false;
547 }
548 } catch (CertificateEncodingException e) {
549 Log.w(TAG, "Exception reading " + mArchiveSourcePath, e);
550 mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
551 return false;
552 } catch (IOException e) {
553 Log.w(TAG, "Exception reading " + mArchiveSourcePath, e);
554 mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
555 return false;
556 } catch (RuntimeException e) {
557 Log.w(TAG, "Exception reading " + mArchiveSourcePath, e);
558 mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
559 return false;
560 }
561
562 return true;
563 }
564
565 public static String parsePackageName(String packageFilePath, int flags) {
566 XmlResourceParser parser = null;
567 AssetManager assmgr = null;
568 try {
569 assmgr = new AssetManager();
570 int cookie = assmgr.addAssetPath(packageFilePath);
571 parser = assmgr.openXmlResourceParser(cookie, "AndroidManifest.xml");
572 } catch (Exception e) {
573 if (assmgr != null) assmgr.close();
574 Log.w(TAG, "Unable to read AndroidManifest.xml of "
575 + packageFilePath, e);
576 return null;
577 }
578 AttributeSet attrs = parser;
579 String errors[] = new String[1];
580 String packageName = null;
581 try {
582 packageName = parsePackageName(parser, attrs, flags, errors);
583 } catch (IOException e) {
584 Log.w(TAG, packageFilePath, e);
585 } catch (XmlPullParserException e) {
586 Log.w(TAG, packageFilePath, e);
587 } finally {
588 if (parser != null) parser.close();
589 if (assmgr != null) assmgr.close();
590 }
591 if (packageName == null) {
592 Log.e(TAG, "parsePackageName error: " + errors[0]);
593 return null;
594 }
595 return packageName;
596 }
597
598 private static String validateName(String name, boolean requiresSeparator) {
599 final int N = name.length();
600 boolean hasSep = false;
601 boolean front = true;
602 for (int i=0; i<N; i++) {
603 final char c = name.charAt(i);
604 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
605 front = false;
606 continue;
607 }
608 if (!front) {
609 if ((c >= '0' && c <= '9') || c == '_') {
610 continue;
611 }
612 }
613 if (c == '.') {
614 hasSep = true;
615 front = true;
616 continue;
617 }
618 return "bad character '" + c + "'";
619 }
620 return hasSep || !requiresSeparator
621 ? null : "must have at least one '.' separator";
622 }
623
624 private static String parsePackageName(XmlPullParser parser,
625 AttributeSet attrs, int flags, String[] outError)
626 throws IOException, XmlPullParserException {
627
628 int type;
629 while ((type=parser.next()) != parser.START_TAG
630 && type != parser.END_DOCUMENT) {
631 ;
632 }
633
634 if (type != parser.START_TAG) {
635 outError[0] = "No start tag found";
636 return null;
637 }
638 if ((flags&PARSE_CHATTY) != 0 && Config.LOGV) Log.v(
639 TAG, "Root element name: '" + parser.getName() + "'");
640 if (!parser.getName().equals("manifest")) {
641 outError[0] = "No <manifest> tag";
642 return null;
643 }
644 String pkgName = attrs.getAttributeValue(null, "package");
645 if (pkgName == null || pkgName.length() == 0) {
646 outError[0] = "<manifest> does not specify package";
647 return null;
648 }
649 String nameError = validateName(pkgName, true);
650 if (nameError != null && !"android".equals(pkgName)) {
651 outError[0] = "<manifest> specifies bad package name \""
652 + pkgName + "\": " + nameError;
653 return null;
654 }
655
656 return pkgName.intern();
657 }
658
659 /**
660 * Temporary.
661 */
662 static public Signature stringToSignature(String str) {
663 final int N = str.length();
664 byte[] sig = new byte[N];
665 for (int i=0; i<N; i++) {
666 sig[i] = (byte)str.charAt(i);
667 }
668 return new Signature(sig);
669 }
670
671 private Package parsePackage(
672 Resources res, XmlResourceParser parser, int flags, String[] outError)
673 throws XmlPullParserException, IOException {
674 AttributeSet attrs = parser;
675
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700676 mParseInstrumentationArgs = null;
677 mParseActivityArgs = null;
678 mParseServiceArgs = null;
679 mParseProviderArgs = null;
680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 String pkgName = parsePackageName(parser, attrs, flags, outError);
682 if (pkgName == null) {
683 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME;
684 return null;
685 }
686 int type;
687
688 final Package pkg = new Package(pkgName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 boolean foundApp = false;
Dianne Hackborn851a5412009-05-08 12:06:44 -0700690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 TypedArray sa = res.obtainAttributes(attrs,
692 com.android.internal.R.styleable.AndroidManifest);
693 pkg.mVersionCode = sa.getInteger(
694 com.android.internal.R.styleable.AndroidManifest_versionCode, 0);
695 pkg.mVersionName = sa.getNonResourceString(
696 com.android.internal.R.styleable.AndroidManifest_versionName);
697 if (pkg.mVersionName != null) {
698 pkg.mVersionName = pkg.mVersionName.intern();
699 }
700 String str = sa.getNonResourceString(
701 com.android.internal.R.styleable.AndroidManifest_sharedUserId);
702 if (str != null) {
703 String nameError = validateName(str, true);
704 if (nameError != null && !"android".equals(pkgName)) {
705 outError[0] = "<manifest> specifies bad sharedUserId name \""
706 + str + "\": " + nameError;
707 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID;
708 return null;
709 }
710 pkg.mSharedUserId = str.intern();
711 pkg.mSharedUserLabel = sa.getResourceId(
712 com.android.internal.R.styleable.AndroidManifest_sharedUserLabel, 0);
713 }
714 sa.recycle();
Suchi Amalapurapuaaec7792010-02-25 11:49:43 -0800715
Suchi Amalapurapu117818e2010-02-09 03:45:40 -0800716 pkg.installLocation = sa.getInteger(
717 com.android.internal.R.styleable.AndroidManifest_installLocation,
Suchi Amalapurapuaaec7792010-02-25 11:49:43 -0800718 PackageInfo.INSTALL_LOCATION_AUTO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719
Dianne Hackborn723738c2009-06-25 19:48:04 -0700720 // Resource boolean are -1, so 1 means we don't know the value.
721 int supportsSmallScreens = 1;
722 int supportsNormalScreens = 1;
723 int supportsLargeScreens = 1;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700724 int resizeable = 1;
Dianne Hackborn11b822d2009-07-21 20:03:02 -0700725 int anyDensity = 1;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 int outerDepth = parser.getDepth();
728 while ((type=parser.next()) != parser.END_DOCUMENT
729 && (type != parser.END_TAG || parser.getDepth() > outerDepth)) {
730 if (type == parser.END_TAG || type == parser.TEXT) {
731 continue;
732 }
733
734 String tagName = parser.getName();
735 if (tagName.equals("application")) {
736 if (foundApp) {
737 if (RIGID_PARSER) {
738 outError[0] = "<manifest> has more than one <application>";
739 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
740 return null;
741 } else {
742 Log.w(TAG, "<manifest> has more than one <application>");
743 XmlUtils.skipCurrentTag(parser);
744 continue;
745 }
746 }
747
748 foundApp = true;
749 if (!parseApplication(pkg, res, parser, attrs, flags, outError)) {
750 return null;
751 }
752 } else if (tagName.equals("permission-group")) {
753 if (parsePermissionGroup(pkg, res, parser, attrs, outError) == null) {
754 return null;
755 }
756 } else if (tagName.equals("permission")) {
757 if (parsePermission(pkg, res, parser, attrs, outError) == null) {
758 return null;
759 }
760 } else if (tagName.equals("permission-tree")) {
761 if (parsePermissionTree(pkg, res, parser, attrs, outError) == null) {
762 return null;
763 }
764 } else if (tagName.equals("uses-permission")) {
765 sa = res.obtainAttributes(attrs,
766 com.android.internal.R.styleable.AndroidManifestUsesPermission);
767
768 String name = sa.getNonResourceString(
769 com.android.internal.R.styleable.AndroidManifestUsesPermission_name);
770
771 sa.recycle();
772
773 if (name != null && !pkg.requestedPermissions.contains(name)) {
Dianne Hackborn854060a2009-07-09 18:14:31 -0700774 pkg.requestedPermissions.add(name.intern());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 }
776
777 XmlUtils.skipCurrentTag(parser);
778
779 } else if (tagName.equals("uses-configuration")) {
780 ConfigurationInfo cPref = new ConfigurationInfo();
781 sa = res.obtainAttributes(attrs,
782 com.android.internal.R.styleable.AndroidManifestUsesConfiguration);
783 cPref.reqTouchScreen = sa.getInt(
784 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqTouchScreen,
785 Configuration.TOUCHSCREEN_UNDEFINED);
786 cPref.reqKeyboardType = sa.getInt(
787 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqKeyboardType,
788 Configuration.KEYBOARD_UNDEFINED);
789 if (sa.getBoolean(
790 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqHardKeyboard,
791 false)) {
792 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
793 }
794 cPref.reqNavigation = sa.getInt(
795 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqNavigation,
796 Configuration.NAVIGATION_UNDEFINED);
797 if (sa.getBoolean(
798 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqFiveWayNav,
799 false)) {
800 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
801 }
802 sa.recycle();
803 pkg.configPreferences.add(cPref);
804
805 XmlUtils.skipCurrentTag(parser);
806
Suchi Amalapurapud299b812009-06-05 10:26:19 -0700807 } else if (tagName.equals("uses-feature")) {
Dianne Hackborn49237342009-08-27 20:08:01 -0700808 FeatureInfo fi = new FeatureInfo();
Suchi Amalapurapud299b812009-06-05 10:26:19 -0700809 sa = res.obtainAttributes(attrs,
810 com.android.internal.R.styleable.AndroidManifestUsesFeature);
Dianne Hackborn49237342009-08-27 20:08:01 -0700811 fi.name = sa.getNonResourceString(
812 com.android.internal.R.styleable.AndroidManifestUsesFeature_name);
813 if (fi.name == null) {
814 fi.reqGlEsVersion = sa.getInt(
815 com.android.internal.R.styleable.AndroidManifestUsesFeature_glEsVersion,
816 FeatureInfo.GL_ES_VERSION_UNDEFINED);
817 }
818 if (sa.getBoolean(
819 com.android.internal.R.styleable.AndroidManifestUsesFeature_required,
820 true)) {
821 fi.flags |= FeatureInfo.FLAG_REQUIRED;
822 }
Suchi Amalapurapud299b812009-06-05 10:26:19 -0700823 sa.recycle();
Dianne Hackborn49237342009-08-27 20:08:01 -0700824 if (pkg.reqFeatures == null) {
825 pkg.reqFeatures = new ArrayList<FeatureInfo>();
826 }
827 pkg.reqFeatures.add(fi);
828
829 if (fi.name == null) {
830 ConfigurationInfo cPref = new ConfigurationInfo();
831 cPref.reqGlEsVersion = fi.reqGlEsVersion;
832 pkg.configPreferences.add(cPref);
833 }
Suchi Amalapurapud299b812009-06-05 10:26:19 -0700834
835 XmlUtils.skipCurrentTag(parser);
836
Dianne Hackborn851a5412009-05-08 12:06:44 -0700837 } else if (tagName.equals("uses-sdk")) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -0700838 if (SDK_VERSION > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 sa = res.obtainAttributes(attrs,
840 com.android.internal.R.styleable.AndroidManifestUsesSdk);
841
Dianne Hackborn851a5412009-05-08 12:06:44 -0700842 int minVers = 0;
843 String minCode = null;
844 int targetVers = 0;
845 String targetCode = null;
846
847 TypedValue val = sa.peekValue(
848 com.android.internal.R.styleable.AndroidManifestUsesSdk_minSdkVersion);
849 if (val != null) {
850 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
851 targetCode = minCode = val.string.toString();
852 } else {
853 // If it's not a string, it's an integer.
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700854 targetVers = minVers = val.data;
Dianne Hackborn851a5412009-05-08 12:06:44 -0700855 }
856 }
857
858 val = sa.peekValue(
859 com.android.internal.R.styleable.AndroidManifestUsesSdk_targetSdkVersion);
860 if (val != null) {
861 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
862 targetCode = minCode = val.string.toString();
863 } else {
864 // If it's not a string, it's an integer.
865 targetVers = val.data;
866 }
867 }
868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 sa.recycle();
870
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700871 if (minCode != null) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -0700872 if (!minCode.equals(SDK_CODENAME)) {
873 if (SDK_CODENAME != null) {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700874 outError[0] = "Requires development platform " + minCode
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -0700875 + " (current platform is " + SDK_CODENAME + ")";
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700876 } else {
877 outError[0] = "Requires development platform " + minCode
878 + " but this is a release platform.";
879 }
880 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
881 return null;
882 }
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -0700883 } else if (minVers > SDK_VERSION) {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700884 outError[0] = "Requires newer sdk version #" + minVers
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -0700885 + " (current version is #" + SDK_VERSION + ")";
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700886 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
887 return null;
888 }
889
Dianne Hackborn851a5412009-05-08 12:06:44 -0700890 if (targetCode != null) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -0700891 if (!targetCode.equals(SDK_CODENAME)) {
892 if (SDK_CODENAME != null) {
Dianne Hackborn851a5412009-05-08 12:06:44 -0700893 outError[0] = "Requires development platform " + targetCode
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -0700894 + " (current platform is " + SDK_CODENAME + ")";
Dianne Hackborn851a5412009-05-08 12:06:44 -0700895 } else {
896 outError[0] = "Requires development platform " + targetCode
897 + " but this is a release platform.";
898 }
899 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
900 return null;
901 }
902 // If the code matches, it definitely targets this SDK.
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700903 pkg.applicationInfo.targetSdkVersion
904 = android.os.Build.VERSION_CODES.CUR_DEVELOPMENT;
905 } else {
906 pkg.applicationInfo.targetSdkVersion = targetVers;
Dianne Hackborn851a5412009-05-08 12:06:44 -0700907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 }
909
910 XmlUtils.skipCurrentTag(parser);
911
Dianne Hackborn723738c2009-06-25 19:48:04 -0700912 } else if (tagName.equals("supports-screens")) {
913 sa = res.obtainAttributes(attrs,
914 com.android.internal.R.styleable.AndroidManifestSupportsScreens);
915
916 // This is a trick to get a boolean and still able to detect
917 // if a value was actually set.
918 supportsSmallScreens = sa.getInteger(
919 com.android.internal.R.styleable.AndroidManifestSupportsScreens_smallScreens,
920 supportsSmallScreens);
921 supportsNormalScreens = sa.getInteger(
922 com.android.internal.R.styleable.AndroidManifestSupportsScreens_normalScreens,
923 supportsNormalScreens);
924 supportsLargeScreens = sa.getInteger(
925 com.android.internal.R.styleable.AndroidManifestSupportsScreens_largeScreens,
926 supportsLargeScreens);
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700927 resizeable = sa.getInteger(
928 com.android.internal.R.styleable.AndroidManifestSupportsScreens_resizeable,
929 supportsLargeScreens);
Dianne Hackborn11b822d2009-07-21 20:03:02 -0700930 anyDensity = sa.getInteger(
931 com.android.internal.R.styleable.AndroidManifestSupportsScreens_anyDensity,
932 anyDensity);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700933
934 sa.recycle();
935
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700936 XmlUtils.skipCurrentTag(parser);
Dianne Hackborn854060a2009-07-09 18:14:31 -0700937
938 } else if (tagName.equals("protected-broadcast")) {
939 sa = res.obtainAttributes(attrs,
940 com.android.internal.R.styleable.AndroidManifestProtectedBroadcast);
941
942 String name = sa.getNonResourceString(
943 com.android.internal.R.styleable.AndroidManifestProtectedBroadcast_name);
944
945 sa.recycle();
946
947 if (name != null && (flags&PARSE_IS_SYSTEM) != 0) {
948 if (pkg.protectedBroadcasts == null) {
949 pkg.protectedBroadcasts = new ArrayList<String>();
950 }
951 if (!pkg.protectedBroadcasts.contains(name)) {
952 pkg.protectedBroadcasts.add(name.intern());
953 }
954 }
955
956 XmlUtils.skipCurrentTag(parser);
957
958 } else if (tagName.equals("instrumentation")) {
959 if (parseInstrumentation(pkg, res, parser, attrs, outError) == null) {
960 return null;
961 }
962
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800963 } else if (tagName.equals("original-package")) {
964 sa = res.obtainAttributes(attrs,
965 com.android.internal.R.styleable.AndroidManifestOriginalPackage);
966
Dianne Hackborn6dee18c2010-02-09 23:59:16 -0800967 String orig =sa.getNonResourceString(
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800968 com.android.internal.R.styleable.AndroidManifestOriginalPackage_name);
Dianne Hackborn6dee18c2010-02-09 23:59:16 -0800969 if (!pkg.packageName.equals(orig)) {
970 pkg.mOriginalPackage = orig;
971 pkg.mRealPackage = pkg.packageName;
972 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800973
974 sa.recycle();
975
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800976 XmlUtils.skipCurrentTag(parser);
977
978 } else if (tagName.equals("adopt-permissions")) {
979 sa = res.obtainAttributes(attrs,
980 com.android.internal.R.styleable.AndroidManifestOriginalPackage);
981
982 String name = sa.getNonResourceString(
983 com.android.internal.R.styleable.AndroidManifestOriginalPackage_name);
984
985 sa.recycle();
986
Dianne Hackborn6dee18c2010-02-09 23:59:16 -0800987 if (name != null) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800988 if (pkg.mAdoptPermissions == null) {
989 pkg.mAdoptPermissions = new ArrayList<String>();
990 }
991 pkg.mAdoptPermissions.add(name);
992 }
993
994 XmlUtils.skipCurrentTag(parser);
995
Dianne Hackborn854060a2009-07-09 18:14:31 -0700996 } else if (tagName.equals("eat-comment")) {
997 // Just skip this tag
998 XmlUtils.skipCurrentTag(parser);
999 continue;
1000
1001 } else if (RIGID_PARSER) {
1002 outError[0] = "Bad element under <manifest>: "
1003 + parser.getName();
1004 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1005 return null;
1006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 } else {
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07001008 Log.w(TAG, "Unknown element under <manifest>: " + parser.getName()
1009 + " at " + mArchiveSourcePath + " "
1010 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 XmlUtils.skipCurrentTag(parser);
1012 continue;
1013 }
1014 }
1015
1016 if (!foundApp && pkg.instrumentation.size() == 0) {
1017 outError[0] = "<manifest> does not contain an <application> or <instrumentation>";
1018 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_EMPTY;
1019 }
1020
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001021 final int NP = PackageParser.NEW_PERMISSIONS.length;
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001022 StringBuilder implicitPerms = null;
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001023 for (int ip=0; ip<NP; ip++) {
1024 final PackageParser.NewPermissionInfo npi
1025 = PackageParser.NEW_PERMISSIONS[ip];
1026 if (pkg.applicationInfo.targetSdkVersion >= npi.sdkVersion) {
1027 break;
1028 }
1029 if (!pkg.requestedPermissions.contains(npi.name)) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001030 if (implicitPerms == null) {
1031 implicitPerms = new StringBuilder(128);
1032 implicitPerms.append(pkg.packageName);
1033 implicitPerms.append(": compat added ");
1034 } else {
1035 implicitPerms.append(' ');
1036 }
1037 implicitPerms.append(npi.name);
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001038 pkg.requestedPermissions.add(npi.name);
1039 }
Dianne Hackborn851a5412009-05-08 12:06:44 -07001040 }
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001041 if (implicitPerms != null) {
1042 Log.i(TAG, implicitPerms.toString());
1043 }
Dianne Hackborn851a5412009-05-08 12:06:44 -07001044
Dianne Hackborn723738c2009-06-25 19:48:04 -07001045 if (supportsSmallScreens < 0 || (supportsSmallScreens > 0
1046 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001047 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07001048 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS;
1049 }
1050 if (supportsNormalScreens != 0) {
1051 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS;
1052 }
1053 if (supportsLargeScreens < 0 || (supportsLargeScreens > 0
1054 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001055 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07001056 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS;
1057 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001058 if (resizeable < 0 || (resizeable > 0
1059 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001060 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001061 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS;
1062 }
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001063 if (anyDensity < 0 || (anyDensity > 0
1064 && pkg.applicationInfo.targetSdkVersion
1065 >= android.os.Build.VERSION_CODES.DONUT)) {
1066 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES;
Mitsuru Oshima8d112672009-04-27 12:01:23 -07001067 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07001068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 return pkg;
1070 }
1071
1072 private static String buildClassName(String pkg, CharSequence clsSeq,
1073 String[] outError) {
1074 if (clsSeq == null || clsSeq.length() <= 0) {
1075 outError[0] = "Empty class name in package " + pkg;
1076 return null;
1077 }
1078 String cls = clsSeq.toString();
1079 char c = cls.charAt(0);
1080 if (c == '.') {
1081 return (pkg + cls).intern();
1082 }
1083 if (cls.indexOf('.') < 0) {
1084 StringBuilder b = new StringBuilder(pkg);
1085 b.append('.');
1086 b.append(cls);
1087 return b.toString().intern();
1088 }
1089 if (c >= 'a' && c <= 'z') {
1090 return cls.intern();
1091 }
1092 outError[0] = "Bad class name " + cls + " in package " + pkg;
1093 return null;
1094 }
1095
1096 private static String buildCompoundName(String pkg,
1097 CharSequence procSeq, String type, String[] outError) {
1098 String proc = procSeq.toString();
1099 char c = proc.charAt(0);
1100 if (pkg != null && c == ':') {
1101 if (proc.length() < 2) {
1102 outError[0] = "Bad " + type + " name " + proc + " in package " + pkg
1103 + ": must be at least two characters";
1104 return null;
1105 }
1106 String subName = proc.substring(1);
1107 String nameError = validateName(subName, false);
1108 if (nameError != null) {
1109 outError[0] = "Invalid " + type + " name " + proc + " in package "
1110 + pkg + ": " + nameError;
1111 return null;
1112 }
1113 return (pkg + proc).intern();
1114 }
1115 String nameError = validateName(proc, true);
1116 if (nameError != null && !"system".equals(proc)) {
1117 outError[0] = "Invalid " + type + " name " + proc + " in package "
1118 + pkg + ": " + nameError;
1119 return null;
1120 }
1121 return proc.intern();
1122 }
1123
1124 private static String buildProcessName(String pkg, String defProc,
1125 CharSequence procSeq, int flags, String[] separateProcesses,
1126 String[] outError) {
1127 if ((flags&PARSE_IGNORE_PROCESSES) != 0 && !"system".equals(procSeq)) {
1128 return defProc != null ? defProc : pkg;
1129 }
1130 if (separateProcesses != null) {
1131 for (int i=separateProcesses.length-1; i>=0; i--) {
1132 String sp = separateProcesses[i];
1133 if (sp.equals(pkg) || sp.equals(defProc) || sp.equals(procSeq)) {
1134 return pkg;
1135 }
1136 }
1137 }
1138 if (procSeq == null || procSeq.length() <= 0) {
1139 return defProc;
1140 }
1141 return buildCompoundName(pkg, procSeq, "package", outError);
1142 }
1143
1144 private static String buildTaskAffinityName(String pkg, String defProc,
1145 CharSequence procSeq, String[] outError) {
1146 if (procSeq == null) {
1147 return defProc;
1148 }
1149 if (procSeq.length() <= 0) {
1150 return null;
1151 }
1152 return buildCompoundName(pkg, procSeq, "taskAffinity", outError);
1153 }
1154
1155 private PermissionGroup parsePermissionGroup(Package owner, Resources res,
1156 XmlPullParser parser, AttributeSet attrs, String[] outError)
1157 throws XmlPullParserException, IOException {
1158 PermissionGroup perm = new PermissionGroup(owner);
1159
1160 TypedArray sa = res.obtainAttributes(attrs,
1161 com.android.internal.R.styleable.AndroidManifestPermissionGroup);
1162
1163 if (!parsePackageItemInfo(owner, perm.info, outError,
1164 "<permission-group>", sa,
1165 com.android.internal.R.styleable.AndroidManifestPermissionGroup_name,
1166 com.android.internal.R.styleable.AndroidManifestPermissionGroup_label,
1167 com.android.internal.R.styleable.AndroidManifestPermissionGroup_icon)) {
1168 sa.recycle();
1169 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1170 return null;
1171 }
1172
1173 perm.info.descriptionRes = sa.getResourceId(
1174 com.android.internal.R.styleable.AndroidManifestPermissionGroup_description,
1175 0);
1176
1177 sa.recycle();
1178
1179 if (!parseAllMetaData(res, parser, attrs, "<permission-group>", perm,
1180 outError)) {
1181 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1182 return null;
1183 }
1184
1185 owner.permissionGroups.add(perm);
1186
1187 return perm;
1188 }
1189
1190 private Permission parsePermission(Package owner, Resources res,
1191 XmlPullParser parser, AttributeSet attrs, String[] outError)
1192 throws XmlPullParserException, IOException {
1193 Permission perm = new Permission(owner);
1194
1195 TypedArray sa = res.obtainAttributes(attrs,
1196 com.android.internal.R.styleable.AndroidManifestPermission);
1197
1198 if (!parsePackageItemInfo(owner, perm.info, outError,
1199 "<permission>", sa,
1200 com.android.internal.R.styleable.AndroidManifestPermission_name,
1201 com.android.internal.R.styleable.AndroidManifestPermission_label,
1202 com.android.internal.R.styleable.AndroidManifestPermission_icon)) {
1203 sa.recycle();
1204 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1205 return null;
1206 }
1207
1208 perm.info.group = sa.getNonResourceString(
1209 com.android.internal.R.styleable.AndroidManifestPermission_permissionGroup);
1210 if (perm.info.group != null) {
1211 perm.info.group = perm.info.group.intern();
1212 }
1213
1214 perm.info.descriptionRes = sa.getResourceId(
1215 com.android.internal.R.styleable.AndroidManifestPermission_description,
1216 0);
1217
1218 perm.info.protectionLevel = sa.getInt(
1219 com.android.internal.R.styleable.AndroidManifestPermission_protectionLevel,
1220 PermissionInfo.PROTECTION_NORMAL);
1221
1222 sa.recycle();
1223
1224 if (perm.info.protectionLevel == -1) {
1225 outError[0] = "<permission> does not specify protectionLevel";
1226 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1227 return null;
1228 }
1229
1230 if (!parseAllMetaData(res, parser, attrs, "<permission>", perm,
1231 outError)) {
1232 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1233 return null;
1234 }
1235
1236 owner.permissions.add(perm);
1237
1238 return perm;
1239 }
1240
1241 private Permission parsePermissionTree(Package owner, Resources res,
1242 XmlPullParser parser, AttributeSet attrs, String[] outError)
1243 throws XmlPullParserException, IOException {
1244 Permission perm = new Permission(owner);
1245
1246 TypedArray sa = res.obtainAttributes(attrs,
1247 com.android.internal.R.styleable.AndroidManifestPermissionTree);
1248
1249 if (!parsePackageItemInfo(owner, perm.info, outError,
1250 "<permission-tree>", sa,
1251 com.android.internal.R.styleable.AndroidManifestPermissionTree_name,
1252 com.android.internal.R.styleable.AndroidManifestPermissionTree_label,
1253 com.android.internal.R.styleable.AndroidManifestPermissionTree_icon)) {
1254 sa.recycle();
1255 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1256 return null;
1257 }
1258
1259 sa.recycle();
1260
1261 int index = perm.info.name.indexOf('.');
1262 if (index > 0) {
1263 index = perm.info.name.indexOf('.', index+1);
1264 }
1265 if (index < 0) {
1266 outError[0] = "<permission-tree> name has less than three segments: "
1267 + perm.info.name;
1268 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1269 return null;
1270 }
1271
1272 perm.info.descriptionRes = 0;
1273 perm.info.protectionLevel = PermissionInfo.PROTECTION_NORMAL;
1274 perm.tree = true;
1275
1276 if (!parseAllMetaData(res, parser, attrs, "<permission-tree>", perm,
1277 outError)) {
1278 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1279 return null;
1280 }
1281
1282 owner.permissions.add(perm);
1283
1284 return perm;
1285 }
1286
1287 private Instrumentation parseInstrumentation(Package owner, Resources res,
1288 XmlPullParser parser, AttributeSet attrs, String[] outError)
1289 throws XmlPullParserException, IOException {
1290 TypedArray sa = res.obtainAttributes(attrs,
1291 com.android.internal.R.styleable.AndroidManifestInstrumentation);
1292
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001293 if (mParseInstrumentationArgs == null) {
1294 mParseInstrumentationArgs = new ParsePackageItemArgs(owner, outError,
1295 com.android.internal.R.styleable.AndroidManifestInstrumentation_name,
1296 com.android.internal.R.styleable.AndroidManifestInstrumentation_label,
1297 com.android.internal.R.styleable.AndroidManifestInstrumentation_icon);
1298 mParseInstrumentationArgs.tag = "<instrumentation>";
1299 }
1300
1301 mParseInstrumentationArgs.sa = sa;
1302
1303 Instrumentation a = new Instrumentation(mParseInstrumentationArgs,
1304 new InstrumentationInfo());
1305 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 sa.recycle();
1307 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1308 return null;
1309 }
1310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 String str;
1312 str = sa.getNonResourceString(
1313 com.android.internal.R.styleable.AndroidManifestInstrumentation_targetPackage);
1314 a.info.targetPackage = str != null ? str.intern() : null;
1315
1316 a.info.handleProfiling = sa.getBoolean(
1317 com.android.internal.R.styleable.AndroidManifestInstrumentation_handleProfiling,
1318 false);
1319
1320 a.info.functionalTest = sa.getBoolean(
1321 com.android.internal.R.styleable.AndroidManifestInstrumentation_functionalTest,
1322 false);
1323
1324 sa.recycle();
1325
1326 if (a.info.targetPackage == null) {
1327 outError[0] = "<instrumentation> does not specify targetPackage";
1328 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1329 return null;
1330 }
1331
1332 if (!parseAllMetaData(res, parser, attrs, "<instrumentation>", a,
1333 outError)) {
1334 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1335 return null;
1336 }
1337
1338 owner.instrumentation.add(a);
1339
1340 return a;
1341 }
1342
1343 private boolean parseApplication(Package owner, Resources res,
1344 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
1345 throws XmlPullParserException, IOException {
1346 final ApplicationInfo ai = owner.applicationInfo;
1347 final String pkgName = owner.applicationInfo.packageName;
1348
1349 TypedArray sa = res.obtainAttributes(attrs,
1350 com.android.internal.R.styleable.AndroidManifestApplication);
1351
1352 String name = sa.getNonResourceString(
1353 com.android.internal.R.styleable.AndroidManifestApplication_name);
1354 if (name != null) {
1355 ai.className = buildClassName(pkgName, name, outError);
1356 if (ai.className == null) {
1357 sa.recycle();
1358 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1359 return false;
1360 }
1361 }
1362
1363 String manageSpaceActivity = sa.getNonResourceString(
1364 com.android.internal.R.styleable.AndroidManifestApplication_manageSpaceActivity);
1365 if (manageSpaceActivity != null) {
1366 ai.manageSpaceActivityName = buildClassName(pkgName, manageSpaceActivity,
1367 outError);
1368 }
1369
Christopher Tate181fafa2009-05-14 11:12:14 -07001370 boolean allowBackup = sa.getBoolean(
1371 com.android.internal.R.styleable.AndroidManifestApplication_allowBackup, true);
1372 if (allowBackup) {
1373 ai.flags |= ApplicationInfo.FLAG_ALLOW_BACKUP;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001374
Christopher Tate3dda5182010-02-24 16:06:18 -08001375 // backupAgent, killAfterRestore, restoreNeedsApplication, and restoreAnyVersion
1376 // are only relevant if backup is possible for the given application.
Christopher Tate181fafa2009-05-14 11:12:14 -07001377 String backupAgent = sa.getNonResourceString(
1378 com.android.internal.R.styleable.AndroidManifestApplication_backupAgent);
1379 if (backupAgent != null) {
1380 ai.backupAgentName = buildClassName(pkgName, backupAgent, outError);
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001381 if (false) {
1382 Log.v(TAG, "android:backupAgent = " + ai.backupAgentName
1383 + " from " + pkgName + "+" + backupAgent);
1384 }
Christopher Tate5e1ab332009-09-01 20:32:49 -07001385
1386 if (sa.getBoolean(
1387 com.android.internal.R.styleable.AndroidManifestApplication_killAfterRestore,
1388 true)) {
1389 ai.flags |= ApplicationInfo.FLAG_KILL_AFTER_RESTORE;
1390 }
1391 if (sa.getBoolean(
1392 com.android.internal.R.styleable.AndroidManifestApplication_restoreNeedsApplication,
1393 false)) {
1394 ai.flags |= ApplicationInfo.FLAG_RESTORE_NEEDS_APPLICATION;
1395 }
Christopher Tate3dda5182010-02-24 16:06:18 -08001396 if (sa.getBoolean(
1397 com.android.internal.R.styleable.AndroidManifestApplication_restoreAnyVersion,
1398 false)) {
1399 ai.flags |= ApplicationInfo.FLAG_RESTORE_ANY_VERSION;
1400 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001401 }
1402 }
1403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 TypedValue v = sa.peekValue(
1405 com.android.internal.R.styleable.AndroidManifestApplication_label);
1406 if (v != null && (ai.labelRes=v.resourceId) == 0) {
1407 ai.nonLocalizedLabel = v.coerceToString();
1408 }
1409
1410 ai.icon = sa.getResourceId(
1411 com.android.internal.R.styleable.AndroidManifestApplication_icon, 0);
1412 ai.theme = sa.getResourceId(
1413 com.android.internal.R.styleable.AndroidManifestApplication_theme, 0);
1414 ai.descriptionRes = sa.getResourceId(
1415 com.android.internal.R.styleable.AndroidManifestApplication_description, 0);
1416
1417 if ((flags&PARSE_IS_SYSTEM) != 0) {
1418 if (sa.getBoolean(
1419 com.android.internal.R.styleable.AndroidManifestApplication_persistent,
1420 false)) {
1421 ai.flags |= ApplicationInfo.FLAG_PERSISTENT;
1422 }
1423 }
1424
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -08001425 if ((flags & PARSE_FORWARD_LOCK) != 0) {
1426 ai.flags |= ApplicationInfo.FLAG_FORWARD_LOCK;
1427 }
1428
1429 if ((flags & PARSE_ON_SDCARD) != 0) {
1430 ai.flags |= ApplicationInfo.FLAG_ON_SDCARD;
1431 }
1432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 if (sa.getBoolean(
1434 com.android.internal.R.styleable.AndroidManifestApplication_debuggable,
1435 false)) {
1436 ai.flags |= ApplicationInfo.FLAG_DEBUGGABLE;
1437 }
1438
1439 if (sa.getBoolean(
Ben Cheng23085b72010-02-08 16:06:32 -08001440 com.android.internal.R.styleable.AndroidManifestApplication_safeMode,
1441 false)) {
1442 ai.flags |= ApplicationInfo.FLAG_VM_SAFE_MODE;
1443 }
1444
1445 if (sa.getBoolean(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 com.android.internal.R.styleable.AndroidManifestApplication_hasCode,
1447 true)) {
1448 ai.flags |= ApplicationInfo.FLAG_HAS_CODE;
1449 }
1450
1451 if (sa.getBoolean(
1452 com.android.internal.R.styleable.AndroidManifestApplication_allowTaskReparenting,
1453 false)) {
1454 ai.flags |= ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING;
1455 }
1456
1457 if (sa.getBoolean(
1458 com.android.internal.R.styleable.AndroidManifestApplication_allowClearUserData,
1459 true)) {
1460 ai.flags |= ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA;
1461 }
1462
Dianne Hackbornade3eca2009-05-11 18:54:45 -07001463 if (sa.getBoolean(
1464 com.android.internal.R.styleable.AndroidManifestApplication_testOnly,
Dianne Hackborne7fe35b2009-05-13 10:53:41 -07001465 false)) {
Dianne Hackbornade3eca2009-05-11 18:54:45 -07001466 ai.flags |= ApplicationInfo.FLAG_TEST_ONLY;
1467 }
1468
Oscar Montemayor1874aa42009-11-10 18:35:33 -08001469 if (sa.getBoolean(
1470 com.android.internal.R.styleable.AndroidManifestApplication_neverEncrypt,
1471 false)) {
1472 ai.flags |= ApplicationInfo.FLAG_NEVER_ENCRYPT;
1473 }
1474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 String str;
1476 str = sa.getNonResourceString(
1477 com.android.internal.R.styleable.AndroidManifestApplication_permission);
1478 ai.permission = (str != null && str.length() > 0) ? str.intern() : null;
1479
1480 str = sa.getNonResourceString(
1481 com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity);
1482 ai.taskAffinity = buildTaskAffinityName(ai.packageName, ai.packageName,
1483 str, outError);
1484
1485 if (outError[0] == null) {
1486 ai.processName = buildProcessName(ai.packageName, null, sa.getNonResourceString(
1487 com.android.internal.R.styleable.AndroidManifestApplication_process),
1488 flags, mSeparateProcesses, outError);
1489
1490 ai.enabled = sa.getBoolean(com.android.internal.R.styleable.AndroidManifestApplication_enabled, true);
1491 }
1492
1493 sa.recycle();
1494
1495 if (outError[0] != null) {
1496 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1497 return false;
1498 }
1499
1500 final int innerDepth = parser.getDepth();
1501
1502 int type;
1503 while ((type=parser.next()) != parser.END_DOCUMENT
1504 && (type != parser.END_TAG || parser.getDepth() > innerDepth)) {
1505 if (type == parser.END_TAG || type == parser.TEXT) {
1506 continue;
1507 }
1508
1509 String tagName = parser.getName();
1510 if (tagName.equals("activity")) {
1511 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, false);
1512 if (a == null) {
1513 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1514 return false;
1515 }
1516
1517 owner.activities.add(a);
1518
1519 } else if (tagName.equals("receiver")) {
1520 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, true);
1521 if (a == null) {
1522 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1523 return false;
1524 }
1525
1526 owner.receivers.add(a);
1527
1528 } else if (tagName.equals("service")) {
1529 Service s = parseService(owner, res, parser, attrs, flags, outError);
1530 if (s == null) {
1531 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1532 return false;
1533 }
1534
1535 owner.services.add(s);
1536
1537 } else if (tagName.equals("provider")) {
1538 Provider p = parseProvider(owner, res, parser, attrs, flags, outError);
1539 if (p == null) {
1540 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1541 return false;
1542 }
1543
1544 owner.providers.add(p);
1545
1546 } else if (tagName.equals("activity-alias")) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001547 Activity a = parseActivityAlias(owner, res, parser, attrs, flags, outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 if (a == null) {
1549 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1550 return false;
1551 }
1552
1553 owner.activities.add(a);
1554
1555 } else if (parser.getName().equals("meta-data")) {
1556 // note: application meta-data is stored off to the side, so it can
1557 // remain null in the primary copy (we like to avoid extra copies because
1558 // it can be large)
1559 if ((owner.mAppMetaData = parseMetaData(res, parser, attrs, owner.mAppMetaData,
1560 outError)) == null) {
1561 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1562 return false;
1563 }
1564
1565 } else if (tagName.equals("uses-library")) {
1566 sa = res.obtainAttributes(attrs,
1567 com.android.internal.R.styleable.AndroidManifestUsesLibrary);
1568
1569 String lname = sa.getNonResourceString(
1570 com.android.internal.R.styleable.AndroidManifestUsesLibrary_name);
Dianne Hackborn49237342009-08-27 20:08:01 -07001571 boolean req = sa.getBoolean(
1572 com.android.internal.R.styleable.AndroidManifestUsesLibrary_required,
1573 true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574
1575 sa.recycle();
1576
Dianne Hackborn49237342009-08-27 20:08:01 -07001577 if (lname != null) {
1578 if (req) {
1579 if (owner.usesLibraries == null) {
1580 owner.usesLibraries = new ArrayList<String>();
1581 }
1582 if (!owner.usesLibraries.contains(lname)) {
1583 owner.usesLibraries.add(lname.intern());
1584 }
1585 } else {
1586 if (owner.usesOptionalLibraries == null) {
1587 owner.usesOptionalLibraries = new ArrayList<String>();
1588 }
1589 if (!owner.usesOptionalLibraries.contains(lname)) {
1590 owner.usesOptionalLibraries.add(lname.intern());
1591 }
1592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 }
1594
1595 XmlUtils.skipCurrentTag(parser);
1596
1597 } else {
1598 if (!RIGID_PARSER) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001599 Log.w(TAG, "Unknown element under <application>: " + tagName
1600 + " at " + mArchiveSourcePath + " "
1601 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 XmlUtils.skipCurrentTag(parser);
1603 continue;
1604 } else {
1605 outError[0] = "Bad element under <application>: " + tagName;
1606 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1607 return false;
1608 }
1609 }
1610 }
1611
1612 return true;
1613 }
1614
1615 private boolean parsePackageItemInfo(Package owner, PackageItemInfo outInfo,
1616 String[] outError, String tag, TypedArray sa,
1617 int nameRes, int labelRes, int iconRes) {
1618 String name = sa.getNonResourceString(nameRes);
1619 if (name == null) {
1620 outError[0] = tag + " does not specify android:name";
1621 return false;
1622 }
1623
1624 outInfo.name
1625 = buildClassName(owner.applicationInfo.packageName, name, outError);
1626 if (outInfo.name == null) {
1627 return false;
1628 }
1629
1630 int iconVal = sa.getResourceId(iconRes, 0);
1631 if (iconVal != 0) {
1632 outInfo.icon = iconVal;
1633 outInfo.nonLocalizedLabel = null;
1634 }
1635
1636 TypedValue v = sa.peekValue(labelRes);
1637 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
1638 outInfo.nonLocalizedLabel = v.coerceToString();
1639 }
1640
1641 outInfo.packageName = owner.packageName;
1642
1643 return true;
1644 }
1645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 private Activity parseActivity(Package owner, Resources res,
1647 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError,
1648 boolean receiver) throws XmlPullParserException, IOException {
1649 TypedArray sa = res.obtainAttributes(attrs,
1650 com.android.internal.R.styleable.AndroidManifestActivity);
1651
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001652 if (mParseActivityArgs == null) {
1653 mParseActivityArgs = new ParseComponentArgs(owner, outError,
1654 com.android.internal.R.styleable.AndroidManifestActivity_name,
1655 com.android.internal.R.styleable.AndroidManifestActivity_label,
1656 com.android.internal.R.styleable.AndroidManifestActivity_icon,
1657 mSeparateProcesses,
1658 com.android.internal.R.styleable.AndroidManifestActivity_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001659 com.android.internal.R.styleable.AndroidManifestActivity_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001660 com.android.internal.R.styleable.AndroidManifestActivity_enabled);
1661 }
1662
1663 mParseActivityArgs.tag = receiver ? "<receiver>" : "<activity>";
1664 mParseActivityArgs.sa = sa;
1665 mParseActivityArgs.flags = flags;
1666
1667 Activity a = new Activity(mParseActivityArgs, new ActivityInfo());
1668 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 sa.recycle();
1670 return null;
1671 }
1672
1673 final boolean setExported = sa.hasValue(
1674 com.android.internal.R.styleable.AndroidManifestActivity_exported);
1675 if (setExported) {
1676 a.info.exported = sa.getBoolean(
1677 com.android.internal.R.styleable.AndroidManifestActivity_exported, false);
1678 }
1679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 a.info.theme = sa.getResourceId(
1681 com.android.internal.R.styleable.AndroidManifestActivity_theme, 0);
1682
1683 String str;
1684 str = sa.getNonResourceString(
1685 com.android.internal.R.styleable.AndroidManifestActivity_permission);
1686 if (str == null) {
1687 a.info.permission = owner.applicationInfo.permission;
1688 } else {
1689 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
1690 }
1691
1692 str = sa.getNonResourceString(
1693 com.android.internal.R.styleable.AndroidManifestActivity_taskAffinity);
1694 a.info.taskAffinity = buildTaskAffinityName(owner.applicationInfo.packageName,
1695 owner.applicationInfo.taskAffinity, str, outError);
1696
1697 a.info.flags = 0;
1698 if (sa.getBoolean(
1699 com.android.internal.R.styleable.AndroidManifestActivity_multiprocess,
1700 false)) {
1701 a.info.flags |= ActivityInfo.FLAG_MULTIPROCESS;
1702 }
1703
1704 if (sa.getBoolean(
1705 com.android.internal.R.styleable.AndroidManifestActivity_finishOnTaskLaunch,
1706 false)) {
1707 a.info.flags |= ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH;
1708 }
1709
1710 if (sa.getBoolean(
1711 com.android.internal.R.styleable.AndroidManifestActivity_clearTaskOnLaunch,
1712 false)) {
1713 a.info.flags |= ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH;
1714 }
1715
1716 if (sa.getBoolean(
1717 com.android.internal.R.styleable.AndroidManifestActivity_noHistory,
1718 false)) {
1719 a.info.flags |= ActivityInfo.FLAG_NO_HISTORY;
1720 }
1721
1722 if (sa.getBoolean(
1723 com.android.internal.R.styleable.AndroidManifestActivity_alwaysRetainTaskState,
1724 false)) {
1725 a.info.flags |= ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE;
1726 }
1727
1728 if (sa.getBoolean(
1729 com.android.internal.R.styleable.AndroidManifestActivity_stateNotNeeded,
1730 false)) {
1731 a.info.flags |= ActivityInfo.FLAG_STATE_NOT_NEEDED;
1732 }
1733
1734 if (sa.getBoolean(
1735 com.android.internal.R.styleable.AndroidManifestActivity_excludeFromRecents,
1736 false)) {
1737 a.info.flags |= ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
1738 }
1739
1740 if (sa.getBoolean(
1741 com.android.internal.R.styleable.AndroidManifestActivity_allowTaskReparenting,
1742 (owner.applicationInfo.flags&ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING) != 0)) {
1743 a.info.flags |= ActivityInfo.FLAG_ALLOW_TASK_REPARENTING;
1744 }
1745
Dianne Hackbornffa42482009-09-23 22:20:11 -07001746 if (sa.getBoolean(
1747 com.android.internal.R.styleable.AndroidManifestActivity_finishOnCloseSystemDialogs,
1748 false)) {
1749 a.info.flags |= ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS;
1750 }
1751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 if (!receiver) {
1753 a.info.launchMode = sa.getInt(
1754 com.android.internal.R.styleable.AndroidManifestActivity_launchMode,
1755 ActivityInfo.LAUNCH_MULTIPLE);
1756 a.info.screenOrientation = sa.getInt(
1757 com.android.internal.R.styleable.AndroidManifestActivity_screenOrientation,
1758 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
1759 a.info.configChanges = sa.getInt(
1760 com.android.internal.R.styleable.AndroidManifestActivity_configChanges,
1761 0);
1762 a.info.softInputMode = sa.getInt(
1763 com.android.internal.R.styleable.AndroidManifestActivity_windowSoftInputMode,
1764 0);
1765 } else {
1766 a.info.launchMode = ActivityInfo.LAUNCH_MULTIPLE;
1767 a.info.configChanges = 0;
1768 }
1769
1770 sa.recycle();
1771
1772 if (outError[0] != null) {
1773 return null;
1774 }
1775
1776 int outerDepth = parser.getDepth();
1777 int type;
1778 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1779 && (type != XmlPullParser.END_TAG
1780 || parser.getDepth() > outerDepth)) {
1781 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1782 continue;
1783 }
1784
1785 if (parser.getName().equals("intent-filter")) {
1786 ActivityIntentInfo intent = new ActivityIntentInfo(a);
1787 if (!parseIntent(res, parser, attrs, flags, intent, outError, !receiver)) {
1788 return null;
1789 }
1790 if (intent.countActions() == 0) {
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07001791 Log.w(TAG, "No actions in intent filter at "
1792 + mArchiveSourcePath + " "
1793 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 } else {
1795 a.intents.add(intent);
1796 }
1797 } else if (parser.getName().equals("meta-data")) {
1798 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
1799 outError)) == null) {
1800 return null;
1801 }
1802 } else {
1803 if (!RIGID_PARSER) {
1804 Log.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
1805 if (receiver) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001806 Log.w(TAG, "Unknown element under <receiver>: " + parser.getName()
1807 + " at " + mArchiveSourcePath + " "
1808 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 } else {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001810 Log.w(TAG, "Unknown element under <activity>: " + parser.getName()
1811 + " at " + mArchiveSourcePath + " "
1812 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 }
1814 XmlUtils.skipCurrentTag(parser);
1815 continue;
1816 }
1817 if (receiver) {
1818 outError[0] = "Bad element under <receiver>: " + parser.getName();
1819 } else {
1820 outError[0] = "Bad element under <activity>: " + parser.getName();
1821 }
1822 return null;
1823 }
1824 }
1825
1826 if (!setExported) {
1827 a.info.exported = a.intents.size() > 0;
1828 }
1829
1830 return a;
1831 }
1832
1833 private Activity parseActivityAlias(Package owner, Resources res,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001834 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
1835 throws XmlPullParserException, IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 TypedArray sa = res.obtainAttributes(attrs,
1837 com.android.internal.R.styleable.AndroidManifestActivityAlias);
1838
1839 String targetActivity = sa.getNonResourceString(
1840 com.android.internal.R.styleable.AndroidManifestActivityAlias_targetActivity);
1841 if (targetActivity == null) {
1842 outError[0] = "<activity-alias> does not specify android:targetActivity";
1843 sa.recycle();
1844 return null;
1845 }
1846
1847 targetActivity = buildClassName(owner.applicationInfo.packageName,
1848 targetActivity, outError);
1849 if (targetActivity == null) {
1850 sa.recycle();
1851 return null;
1852 }
1853
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001854 if (mParseActivityAliasArgs == null) {
1855 mParseActivityAliasArgs = new ParseComponentArgs(owner, outError,
1856 com.android.internal.R.styleable.AndroidManifestActivityAlias_name,
1857 com.android.internal.R.styleable.AndroidManifestActivityAlias_label,
1858 com.android.internal.R.styleable.AndroidManifestActivityAlias_icon,
1859 mSeparateProcesses,
1860 0,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001861 com.android.internal.R.styleable.AndroidManifestActivityAlias_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001862 com.android.internal.R.styleable.AndroidManifestActivityAlias_enabled);
1863 mParseActivityAliasArgs.tag = "<activity-alias>";
1864 }
1865
1866 mParseActivityAliasArgs.sa = sa;
1867 mParseActivityAliasArgs.flags = flags;
1868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 Activity target = null;
1870
1871 final int NA = owner.activities.size();
1872 for (int i=0; i<NA; i++) {
1873 Activity t = owner.activities.get(i);
1874 if (targetActivity.equals(t.info.name)) {
1875 target = t;
1876 break;
1877 }
1878 }
1879
1880 if (target == null) {
1881 outError[0] = "<activity-alias> target activity " + targetActivity
1882 + " not found in manifest";
1883 sa.recycle();
1884 return null;
1885 }
1886
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001887 ActivityInfo info = new ActivityInfo();
1888 info.targetActivity = targetActivity;
1889 info.configChanges = target.info.configChanges;
1890 info.flags = target.info.flags;
1891 info.icon = target.info.icon;
1892 info.labelRes = target.info.labelRes;
1893 info.nonLocalizedLabel = target.info.nonLocalizedLabel;
1894 info.launchMode = target.info.launchMode;
1895 info.processName = target.info.processName;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001896 if (info.descriptionRes == 0) {
1897 info.descriptionRes = target.info.descriptionRes;
1898 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001899 info.screenOrientation = target.info.screenOrientation;
1900 info.taskAffinity = target.info.taskAffinity;
1901 info.theme = target.info.theme;
1902
1903 Activity a = new Activity(mParseActivityAliasArgs, info);
1904 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 sa.recycle();
1906 return null;
1907 }
1908
1909 final boolean setExported = sa.hasValue(
1910 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported);
1911 if (setExported) {
1912 a.info.exported = sa.getBoolean(
1913 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported, false);
1914 }
1915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 String str;
1917 str = sa.getNonResourceString(
1918 com.android.internal.R.styleable.AndroidManifestActivityAlias_permission);
1919 if (str != null) {
1920 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
1921 }
1922
1923 sa.recycle();
1924
1925 if (outError[0] != null) {
1926 return null;
1927 }
1928
1929 int outerDepth = parser.getDepth();
1930 int type;
1931 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1932 && (type != XmlPullParser.END_TAG
1933 || parser.getDepth() > outerDepth)) {
1934 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1935 continue;
1936 }
1937
1938 if (parser.getName().equals("intent-filter")) {
1939 ActivityIntentInfo intent = new ActivityIntentInfo(a);
1940 if (!parseIntent(res, parser, attrs, flags, intent, outError, true)) {
1941 return null;
1942 }
1943 if (intent.countActions() == 0) {
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07001944 Log.w(TAG, "No actions in intent filter at "
1945 + mArchiveSourcePath + " "
1946 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 } else {
1948 a.intents.add(intent);
1949 }
1950 } else if (parser.getName().equals("meta-data")) {
1951 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
1952 outError)) == null) {
1953 return null;
1954 }
1955 } else {
1956 if (!RIGID_PARSER) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001957 Log.w(TAG, "Unknown element under <activity-alias>: " + parser.getName()
1958 + " at " + mArchiveSourcePath + " "
1959 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 XmlUtils.skipCurrentTag(parser);
1961 continue;
1962 }
1963 outError[0] = "Bad element under <activity-alias>: " + parser.getName();
1964 return null;
1965 }
1966 }
1967
1968 if (!setExported) {
1969 a.info.exported = a.intents.size() > 0;
1970 }
1971
1972 return a;
1973 }
1974
1975 private Provider parseProvider(Package owner, Resources res,
1976 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
1977 throws XmlPullParserException, IOException {
1978 TypedArray sa = res.obtainAttributes(attrs,
1979 com.android.internal.R.styleable.AndroidManifestProvider);
1980
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001981 if (mParseProviderArgs == null) {
1982 mParseProviderArgs = new ParseComponentArgs(owner, outError,
1983 com.android.internal.R.styleable.AndroidManifestProvider_name,
1984 com.android.internal.R.styleable.AndroidManifestProvider_label,
1985 com.android.internal.R.styleable.AndroidManifestProvider_icon,
1986 mSeparateProcesses,
1987 com.android.internal.R.styleable.AndroidManifestProvider_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001988 com.android.internal.R.styleable.AndroidManifestProvider_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001989 com.android.internal.R.styleable.AndroidManifestProvider_enabled);
1990 mParseProviderArgs.tag = "<provider>";
1991 }
1992
1993 mParseProviderArgs.sa = sa;
1994 mParseProviderArgs.flags = flags;
1995
1996 Provider p = new Provider(mParseProviderArgs, new ProviderInfo());
1997 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 sa.recycle();
1999 return null;
2000 }
2001
2002 p.info.exported = sa.getBoolean(
2003 com.android.internal.R.styleable.AndroidManifestProvider_exported, true);
2004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 String cpname = sa.getNonResourceString(
2006 com.android.internal.R.styleable.AndroidManifestProvider_authorities);
2007
2008 p.info.isSyncable = sa.getBoolean(
2009 com.android.internal.R.styleable.AndroidManifestProvider_syncable,
2010 false);
2011
2012 String permission = sa.getNonResourceString(
2013 com.android.internal.R.styleable.AndroidManifestProvider_permission);
2014 String str = sa.getNonResourceString(
2015 com.android.internal.R.styleable.AndroidManifestProvider_readPermission);
2016 if (str == null) {
2017 str = permission;
2018 }
2019 if (str == null) {
2020 p.info.readPermission = owner.applicationInfo.permission;
2021 } else {
2022 p.info.readPermission =
2023 str.length() > 0 ? str.toString().intern() : null;
2024 }
2025 str = sa.getNonResourceString(
2026 com.android.internal.R.styleable.AndroidManifestProvider_writePermission);
2027 if (str == null) {
2028 str = permission;
2029 }
2030 if (str == null) {
2031 p.info.writePermission = owner.applicationInfo.permission;
2032 } else {
2033 p.info.writePermission =
2034 str.length() > 0 ? str.toString().intern() : null;
2035 }
2036
2037 p.info.grantUriPermissions = sa.getBoolean(
2038 com.android.internal.R.styleable.AndroidManifestProvider_grantUriPermissions,
2039 false);
2040
2041 p.info.multiprocess = sa.getBoolean(
2042 com.android.internal.R.styleable.AndroidManifestProvider_multiprocess,
2043 false);
2044
2045 p.info.initOrder = sa.getInt(
2046 com.android.internal.R.styleable.AndroidManifestProvider_initOrder,
2047 0);
2048
2049 sa.recycle();
2050
2051 if (cpname == null) {
2052 outError[0] = "<provider> does not incude authorities attribute";
2053 return null;
2054 }
2055 p.info.authority = cpname.intern();
2056
2057 if (!parseProviderTags(res, parser, attrs, p, outError)) {
2058 return null;
2059 }
2060
2061 return p;
2062 }
2063
2064 private boolean parseProviderTags(Resources res,
2065 XmlPullParser parser, AttributeSet attrs,
2066 Provider outInfo, String[] outError)
2067 throws XmlPullParserException, IOException {
2068 int outerDepth = parser.getDepth();
2069 int type;
2070 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2071 && (type != XmlPullParser.END_TAG
2072 || parser.getDepth() > outerDepth)) {
2073 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2074 continue;
2075 }
2076
2077 if (parser.getName().equals("meta-data")) {
2078 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
2079 outInfo.metaData, outError)) == null) {
2080 return false;
2081 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 } else if (parser.getName().equals("grant-uri-permission")) {
2084 TypedArray sa = res.obtainAttributes(attrs,
2085 com.android.internal.R.styleable.AndroidManifestGrantUriPermission);
2086
2087 PatternMatcher pa = null;
2088
2089 String str = sa.getNonResourceString(
2090 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_path);
2091 if (str != null) {
2092 pa = new PatternMatcher(str, PatternMatcher.PATTERN_LITERAL);
2093 }
2094
2095 str = sa.getNonResourceString(
2096 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPrefix);
2097 if (str != null) {
2098 pa = new PatternMatcher(str, PatternMatcher.PATTERN_PREFIX);
2099 }
2100
2101 str = sa.getNonResourceString(
2102 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPattern);
2103 if (str != null) {
2104 pa = new PatternMatcher(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
2105 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 sa.recycle();
2108
2109 if (pa != null) {
2110 if (outInfo.info.uriPermissionPatterns == null) {
2111 outInfo.info.uriPermissionPatterns = new PatternMatcher[1];
2112 outInfo.info.uriPermissionPatterns[0] = pa;
2113 } else {
2114 final int N = outInfo.info.uriPermissionPatterns.length;
2115 PatternMatcher[] newp = new PatternMatcher[N+1];
2116 System.arraycopy(outInfo.info.uriPermissionPatterns, 0, newp, 0, N);
2117 newp[N] = pa;
2118 outInfo.info.uriPermissionPatterns = newp;
2119 }
2120 outInfo.info.grantUriPermissions = true;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002121 } else {
2122 if (!RIGID_PARSER) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002123 Log.w(TAG, "Unknown element under <path-permission>: "
2124 + parser.getName() + " at " + mArchiveSourcePath + " "
2125 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002126 XmlUtils.skipCurrentTag(parser);
2127 continue;
2128 }
2129 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
2130 return false;
2131 }
2132 XmlUtils.skipCurrentTag(parser);
2133
2134 } else if (parser.getName().equals("path-permission")) {
2135 TypedArray sa = res.obtainAttributes(attrs,
2136 com.android.internal.R.styleable.AndroidManifestPathPermission);
2137
2138 PathPermission pa = null;
2139
2140 String permission = sa.getNonResourceString(
2141 com.android.internal.R.styleable.AndroidManifestPathPermission_permission);
2142 String readPermission = sa.getNonResourceString(
2143 com.android.internal.R.styleable.AndroidManifestPathPermission_readPermission);
2144 if (readPermission == null) {
2145 readPermission = permission;
2146 }
2147 String writePermission = sa.getNonResourceString(
2148 com.android.internal.R.styleable.AndroidManifestPathPermission_writePermission);
2149 if (writePermission == null) {
2150 writePermission = permission;
2151 }
2152
2153 boolean havePerm = false;
2154 if (readPermission != null) {
2155 readPermission = readPermission.intern();
2156 havePerm = true;
2157 }
2158 if (writePermission != null) {
Bjorn Bringerte04b1ad2010-02-09 13:56:08 +00002159 writePermission = writePermission.intern();
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002160 havePerm = true;
2161 }
2162
2163 if (!havePerm) {
2164 if (!RIGID_PARSER) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002165 Log.w(TAG, "No readPermission or writePermssion for <path-permission>: "
2166 + parser.getName() + " at " + mArchiveSourcePath + " "
2167 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002168 XmlUtils.skipCurrentTag(parser);
2169 continue;
2170 }
2171 outError[0] = "No readPermission or writePermssion for <path-permission>";
2172 return false;
2173 }
2174
2175 String path = sa.getNonResourceString(
2176 com.android.internal.R.styleable.AndroidManifestPathPermission_path);
2177 if (path != null) {
2178 pa = new PathPermission(path,
2179 PatternMatcher.PATTERN_LITERAL, readPermission, writePermission);
2180 }
2181
2182 path = sa.getNonResourceString(
2183 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPrefix);
2184 if (path != null) {
2185 pa = new PathPermission(path,
2186 PatternMatcher.PATTERN_PREFIX, readPermission, writePermission);
2187 }
2188
2189 path = sa.getNonResourceString(
2190 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPattern);
2191 if (path != null) {
2192 pa = new PathPermission(path,
2193 PatternMatcher.PATTERN_SIMPLE_GLOB, readPermission, writePermission);
2194 }
2195
2196 sa.recycle();
2197
2198 if (pa != null) {
2199 if (outInfo.info.pathPermissions == null) {
2200 outInfo.info.pathPermissions = new PathPermission[1];
2201 outInfo.info.pathPermissions[0] = pa;
2202 } else {
2203 final int N = outInfo.info.pathPermissions.length;
2204 PathPermission[] newp = new PathPermission[N+1];
2205 System.arraycopy(outInfo.info.pathPermissions, 0, newp, 0, N);
2206 newp[N] = pa;
2207 outInfo.info.pathPermissions = newp;
2208 }
2209 } else {
2210 if (!RIGID_PARSER) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002211 Log.w(TAG, "No path, pathPrefix, or pathPattern for <path-permission>: "
2212 + parser.getName() + " at " + mArchiveSourcePath + " "
2213 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002214 XmlUtils.skipCurrentTag(parser);
2215 continue;
2216 }
2217 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
2218 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 }
2220 XmlUtils.skipCurrentTag(parser);
2221
2222 } else {
2223 if (!RIGID_PARSER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 Log.w(TAG, "Unknown element under <provider>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002225 + parser.getName() + " at " + mArchiveSourcePath + " "
2226 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 XmlUtils.skipCurrentTag(parser);
2228 continue;
2229 }
2230 outError[0] = "Bad element under <provider>: "
2231 + parser.getName();
2232 return false;
2233 }
2234 }
2235 return true;
2236 }
2237
2238 private Service parseService(Package owner, Resources res,
2239 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2240 throws XmlPullParserException, IOException {
2241 TypedArray sa = res.obtainAttributes(attrs,
2242 com.android.internal.R.styleable.AndroidManifestService);
2243
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002244 if (mParseServiceArgs == null) {
2245 mParseServiceArgs = new ParseComponentArgs(owner, outError,
2246 com.android.internal.R.styleable.AndroidManifestService_name,
2247 com.android.internal.R.styleable.AndroidManifestService_label,
2248 com.android.internal.R.styleable.AndroidManifestService_icon,
2249 mSeparateProcesses,
2250 com.android.internal.R.styleable.AndroidManifestService_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002251 com.android.internal.R.styleable.AndroidManifestService_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002252 com.android.internal.R.styleable.AndroidManifestService_enabled);
2253 mParseServiceArgs.tag = "<service>";
2254 }
2255
2256 mParseServiceArgs.sa = sa;
2257 mParseServiceArgs.flags = flags;
2258
2259 Service s = new Service(mParseServiceArgs, new ServiceInfo());
2260 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 sa.recycle();
2262 return null;
2263 }
2264
2265 final boolean setExported = sa.hasValue(
2266 com.android.internal.R.styleable.AndroidManifestService_exported);
2267 if (setExported) {
2268 s.info.exported = sa.getBoolean(
2269 com.android.internal.R.styleable.AndroidManifestService_exported, false);
2270 }
2271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 String str = sa.getNonResourceString(
2273 com.android.internal.R.styleable.AndroidManifestService_permission);
2274 if (str == null) {
2275 s.info.permission = owner.applicationInfo.permission;
2276 } else {
2277 s.info.permission = str.length() > 0 ? str.toString().intern() : null;
2278 }
2279
2280 sa.recycle();
2281
2282 int outerDepth = parser.getDepth();
2283 int type;
2284 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2285 && (type != XmlPullParser.END_TAG
2286 || parser.getDepth() > outerDepth)) {
2287 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2288 continue;
2289 }
2290
2291 if (parser.getName().equals("intent-filter")) {
2292 ServiceIntentInfo intent = new ServiceIntentInfo(s);
2293 if (!parseIntent(res, parser, attrs, flags, intent, outError, false)) {
2294 return null;
2295 }
2296
2297 s.intents.add(intent);
2298 } else if (parser.getName().equals("meta-data")) {
2299 if ((s.metaData=parseMetaData(res, parser, attrs, s.metaData,
2300 outError)) == null) {
2301 return null;
2302 }
2303 } else {
2304 if (!RIGID_PARSER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 Log.w(TAG, "Unknown element under <service>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002306 + parser.getName() + " at " + mArchiveSourcePath + " "
2307 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 XmlUtils.skipCurrentTag(parser);
2309 continue;
2310 }
2311 outError[0] = "Bad element under <service>: "
2312 + parser.getName();
2313 return null;
2314 }
2315 }
2316
2317 if (!setExported) {
2318 s.info.exported = s.intents.size() > 0;
2319 }
2320
2321 return s;
2322 }
2323
2324 private boolean parseAllMetaData(Resources res,
2325 XmlPullParser parser, AttributeSet attrs, String tag,
2326 Component outInfo, String[] outError)
2327 throws XmlPullParserException, IOException {
2328 int outerDepth = parser.getDepth();
2329 int type;
2330 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2331 && (type != XmlPullParser.END_TAG
2332 || parser.getDepth() > outerDepth)) {
2333 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2334 continue;
2335 }
2336
2337 if (parser.getName().equals("meta-data")) {
2338 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
2339 outInfo.metaData, outError)) == null) {
2340 return false;
2341 }
2342 } else {
2343 if (!RIGID_PARSER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 Log.w(TAG, "Unknown element under " + tag + ": "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002345 + parser.getName() + " at " + mArchiveSourcePath + " "
2346 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 XmlUtils.skipCurrentTag(parser);
2348 continue;
2349 }
2350 outError[0] = "Bad element under " + tag + ": "
2351 + parser.getName();
2352 return false;
2353 }
2354 }
2355 return true;
2356 }
2357
2358 private Bundle parseMetaData(Resources res,
2359 XmlPullParser parser, AttributeSet attrs,
2360 Bundle data, String[] outError)
2361 throws XmlPullParserException, IOException {
2362
2363 TypedArray sa = res.obtainAttributes(attrs,
2364 com.android.internal.R.styleable.AndroidManifestMetaData);
2365
2366 if (data == null) {
2367 data = new Bundle();
2368 }
2369
2370 String name = sa.getNonResourceString(
2371 com.android.internal.R.styleable.AndroidManifestMetaData_name);
2372 if (name == null) {
2373 outError[0] = "<meta-data> requires an android:name attribute";
2374 sa.recycle();
2375 return null;
2376 }
2377
Dianne Hackborn854060a2009-07-09 18:14:31 -07002378 name = name.intern();
2379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 TypedValue v = sa.peekValue(
2381 com.android.internal.R.styleable.AndroidManifestMetaData_resource);
2382 if (v != null && v.resourceId != 0) {
2383 //Log.i(TAG, "Meta data ref " + name + ": " + v);
2384 data.putInt(name, v.resourceId);
2385 } else {
2386 v = sa.peekValue(
2387 com.android.internal.R.styleable.AndroidManifestMetaData_value);
2388 //Log.i(TAG, "Meta data " + name + ": " + v);
2389 if (v != null) {
2390 if (v.type == TypedValue.TYPE_STRING) {
2391 CharSequence cs = v.coerceToString();
Dianne Hackborn854060a2009-07-09 18:14:31 -07002392 data.putString(name, cs != null ? cs.toString().intern() : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
2394 data.putBoolean(name, v.data != 0);
2395 } else if (v.type >= TypedValue.TYPE_FIRST_INT
2396 && v.type <= TypedValue.TYPE_LAST_INT) {
2397 data.putInt(name, v.data);
2398 } else if (v.type == TypedValue.TYPE_FLOAT) {
2399 data.putFloat(name, v.getFloat());
2400 } else {
2401 if (!RIGID_PARSER) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002402 Log.w(TAG, "<meta-data> only supports string, integer, float, color, boolean, and resource reference types: "
2403 + parser.getName() + " at " + mArchiveSourcePath + " "
2404 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 } else {
2406 outError[0] = "<meta-data> only supports string, integer, float, color, boolean, and resource reference types";
2407 data = null;
2408 }
2409 }
2410 } else {
2411 outError[0] = "<meta-data> requires an android:value or android:resource attribute";
2412 data = null;
2413 }
2414 }
2415
2416 sa.recycle();
2417
2418 XmlUtils.skipCurrentTag(parser);
2419
2420 return data;
2421 }
2422
2423 private static final String ANDROID_RESOURCES
2424 = "http://schemas.android.com/apk/res/android";
2425
2426 private boolean parseIntent(Resources res,
2427 XmlPullParser parser, AttributeSet attrs, int flags,
2428 IntentInfo outInfo, String[] outError, boolean isActivity)
2429 throws XmlPullParserException, IOException {
2430
2431 TypedArray sa = res.obtainAttributes(attrs,
2432 com.android.internal.R.styleable.AndroidManifestIntentFilter);
2433
2434 int priority = sa.getInt(
2435 com.android.internal.R.styleable.AndroidManifestIntentFilter_priority, 0);
2436 if (priority > 0 && isActivity && (flags&PARSE_IS_SYSTEM) == 0) {
2437 Log.w(TAG, "Activity with priority > 0, forcing to 0 at "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002438 + mArchiveSourcePath + " "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 + parser.getPositionDescription());
2440 priority = 0;
2441 }
2442 outInfo.setPriority(priority);
2443
2444 TypedValue v = sa.peekValue(
2445 com.android.internal.R.styleable.AndroidManifestIntentFilter_label);
2446 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
2447 outInfo.nonLocalizedLabel = v.coerceToString();
2448 }
2449
2450 outInfo.icon = sa.getResourceId(
2451 com.android.internal.R.styleable.AndroidManifestIntentFilter_icon, 0);
2452
2453 sa.recycle();
2454
2455 int outerDepth = parser.getDepth();
2456 int type;
2457 while ((type=parser.next()) != parser.END_DOCUMENT
2458 && (type != parser.END_TAG || parser.getDepth() > outerDepth)) {
2459 if (type == parser.END_TAG || type == parser.TEXT) {
2460 continue;
2461 }
2462
2463 String nodeName = parser.getName();
2464 if (nodeName.equals("action")) {
2465 String value = attrs.getAttributeValue(
2466 ANDROID_RESOURCES, "name");
2467 if (value == null || value == "") {
2468 outError[0] = "No value supplied for <android:name>";
2469 return false;
2470 }
2471 XmlUtils.skipCurrentTag(parser);
2472
2473 outInfo.addAction(value);
2474 } else if (nodeName.equals("category")) {
2475 String value = attrs.getAttributeValue(
2476 ANDROID_RESOURCES, "name");
2477 if (value == null || value == "") {
2478 outError[0] = "No value supplied for <android:name>";
2479 return false;
2480 }
2481 XmlUtils.skipCurrentTag(parser);
2482
2483 outInfo.addCategory(value);
2484
2485 } else if (nodeName.equals("data")) {
2486 sa = res.obtainAttributes(attrs,
2487 com.android.internal.R.styleable.AndroidManifestData);
2488
2489 String str = sa.getNonResourceString(
2490 com.android.internal.R.styleable.AndroidManifestData_mimeType);
2491 if (str != null) {
2492 try {
2493 outInfo.addDataType(str);
2494 } catch (IntentFilter.MalformedMimeTypeException e) {
2495 outError[0] = e.toString();
2496 sa.recycle();
2497 return false;
2498 }
2499 }
2500
2501 str = sa.getNonResourceString(
2502 com.android.internal.R.styleable.AndroidManifestData_scheme);
2503 if (str != null) {
2504 outInfo.addDataScheme(str);
2505 }
2506
2507 String host = sa.getNonResourceString(
2508 com.android.internal.R.styleable.AndroidManifestData_host);
2509 String port = sa.getNonResourceString(
2510 com.android.internal.R.styleable.AndroidManifestData_port);
2511 if (host != null) {
2512 outInfo.addDataAuthority(host, port);
2513 }
2514
2515 str = sa.getNonResourceString(
2516 com.android.internal.R.styleable.AndroidManifestData_path);
2517 if (str != null) {
2518 outInfo.addDataPath(str, PatternMatcher.PATTERN_LITERAL);
2519 }
2520
2521 str = sa.getNonResourceString(
2522 com.android.internal.R.styleable.AndroidManifestData_pathPrefix);
2523 if (str != null) {
2524 outInfo.addDataPath(str, PatternMatcher.PATTERN_PREFIX);
2525 }
2526
2527 str = sa.getNonResourceString(
2528 com.android.internal.R.styleable.AndroidManifestData_pathPattern);
2529 if (str != null) {
2530 outInfo.addDataPath(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
2531 }
2532
2533 sa.recycle();
2534 XmlUtils.skipCurrentTag(parser);
2535 } else if (!RIGID_PARSER) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002536 Log.w(TAG, "Unknown element under <intent-filter>: "
2537 + parser.getName() + " at " + mArchiveSourcePath + " "
2538 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 XmlUtils.skipCurrentTag(parser);
2540 } else {
2541 outError[0] = "Bad element under <intent-filter>: " + parser.getName();
2542 return false;
2543 }
2544 }
2545
2546 outInfo.hasDefault = outInfo.hasCategory(Intent.CATEGORY_DEFAULT);
2547 if (false) {
2548 String cats = "";
2549 Iterator<String> it = outInfo.categoriesIterator();
2550 while (it != null && it.hasNext()) {
2551 cats += " " + it.next();
2552 }
2553 System.out.println("Intent d=" +
2554 outInfo.hasDefault + ", cat=" + cats);
2555 }
2556
2557 return true;
2558 }
2559
2560 public final static class Package {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002561 public String packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562
2563 // For now we only support one application per package.
2564 public final ApplicationInfo applicationInfo = new ApplicationInfo();
2565
2566 public final ArrayList<Permission> permissions = new ArrayList<Permission>(0);
2567 public final ArrayList<PermissionGroup> permissionGroups = new ArrayList<PermissionGroup>(0);
2568 public final ArrayList<Activity> activities = new ArrayList<Activity>(0);
2569 public final ArrayList<Activity> receivers = new ArrayList<Activity>(0);
2570 public final ArrayList<Provider> providers = new ArrayList<Provider>(0);
2571 public final ArrayList<Service> services = new ArrayList<Service>(0);
2572 public final ArrayList<Instrumentation> instrumentation = new ArrayList<Instrumentation>(0);
2573
2574 public final ArrayList<String> requestedPermissions = new ArrayList<String>();
2575
Dianne Hackborn854060a2009-07-09 18:14:31 -07002576 public ArrayList<String> protectedBroadcasts;
2577
Dianne Hackborn49237342009-08-27 20:08:01 -07002578 public ArrayList<String> usesLibraries = null;
2579 public ArrayList<String> usesOptionalLibraries = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 public String[] usesLibraryFiles = null;
2581
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08002582 public String mOriginalPackage = null;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002583 public String mRealPackage = null;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08002584 public ArrayList<String> mAdoptPermissions = null;
2585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 // We store the application meta-data independently to avoid multiple unwanted references
2587 public Bundle mAppMetaData = null;
2588
2589 // If this is a 3rd party app, this is the path of the zip file.
2590 public String mPath;
2591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 // The version code declared for this package.
2593 public int mVersionCode;
2594
2595 // The version name declared for this package.
2596 public String mVersionName;
2597
2598 // The shared user id that this package wants to use.
2599 public String mSharedUserId;
2600
2601 // The shared user label that this package wants to use.
2602 public int mSharedUserLabel;
2603
2604 // Signatures that were read from the package.
2605 public Signature mSignatures[];
2606
2607 // For use by package manager service for quick lookup of
2608 // preferred up order.
2609 public int mPreferredOrder = 0;
2610
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07002611 // For use by the package manager to keep track of the path to the
2612 // file an app came from.
2613 public String mScanPath;
2614
2615 // For use by package manager to keep track of where it has done dexopt.
2616 public boolean mDidDexOpt;
2617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 // Additional data supplied by callers.
2619 public Object mExtras;
2620
2621 /*
2622 * Applications hardware preferences
2623 */
2624 public final ArrayList<ConfigurationInfo> configPreferences =
2625 new ArrayList<ConfigurationInfo>();
2626
Dianne Hackborn49237342009-08-27 20:08:01 -07002627 /*
2628 * Applications requested features
2629 */
2630 public ArrayList<FeatureInfo> reqFeatures = null;
2631
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08002632 public int installLocation;
2633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 public Package(String _name) {
2635 packageName = _name;
2636 applicationInfo.packageName = _name;
2637 applicationInfo.uid = -1;
2638 }
2639
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002640 public void setPackageName(String newName) {
2641 packageName = newName;
2642 applicationInfo.packageName = newName;
2643 for (int i=permissions.size()-1; i>=0; i--) {
2644 permissions.get(i).setPackageName(newName);
2645 }
2646 for (int i=permissionGroups.size()-1; i>=0; i--) {
2647 permissionGroups.get(i).setPackageName(newName);
2648 }
2649 for (int i=activities.size()-1; i>=0; i--) {
2650 activities.get(i).setPackageName(newName);
2651 }
2652 for (int i=receivers.size()-1; i>=0; i--) {
2653 receivers.get(i).setPackageName(newName);
2654 }
2655 for (int i=providers.size()-1; i>=0; i--) {
2656 providers.get(i).setPackageName(newName);
2657 }
2658 for (int i=services.size()-1; i>=0; i--) {
2659 services.get(i).setPackageName(newName);
2660 }
2661 for (int i=instrumentation.size()-1; i>=0; i--) {
2662 instrumentation.get(i).setPackageName(newName);
2663 }
2664 }
2665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 public String toString() {
2667 return "Package{"
2668 + Integer.toHexString(System.identityHashCode(this))
2669 + " " + packageName + "}";
2670 }
2671 }
2672
2673 public static class Component<II extends IntentInfo> {
2674 public final Package owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002675 public final ArrayList<II> intents;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002676 public final String className;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 public Bundle metaData;
2678
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002679 ComponentName componentName;
2680 String componentShortName;
2681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 public Component(Package _owner) {
2683 owner = _owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002684 intents = null;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002685 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002686 }
2687
2688 public Component(final ParsePackageItemArgs args, final PackageItemInfo outInfo) {
2689 owner = args.owner;
2690 intents = new ArrayList<II>(0);
2691 String name = args.sa.getNonResourceString(args.nameRes);
2692 if (name == null) {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002693 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002694 args.outError[0] = args.tag + " does not specify android:name";
2695 return;
2696 }
2697
2698 outInfo.name
2699 = buildClassName(owner.applicationInfo.packageName, name, args.outError);
2700 if (outInfo.name == null) {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002701 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002702 args.outError[0] = args.tag + " does not have valid android:name";
2703 return;
2704 }
2705
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002706 className = outInfo.name;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002707
2708 int iconVal = args.sa.getResourceId(args.iconRes, 0);
2709 if (iconVal != 0) {
2710 outInfo.icon = iconVal;
2711 outInfo.nonLocalizedLabel = null;
2712 }
2713
2714 TypedValue v = args.sa.peekValue(args.labelRes);
2715 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
2716 outInfo.nonLocalizedLabel = v.coerceToString();
2717 }
2718
2719 outInfo.packageName = owner.packageName;
2720 }
2721
2722 public Component(final ParseComponentArgs args, final ComponentInfo outInfo) {
2723 this(args, (PackageItemInfo)outInfo);
2724 if (args.outError[0] != null) {
2725 return;
2726 }
2727
2728 if (args.processRes != 0) {
2729 outInfo.processName = buildProcessName(owner.applicationInfo.packageName,
2730 owner.applicationInfo.processName, args.sa.getNonResourceString(args.processRes),
2731 args.flags, args.sepProcesses, args.outError);
2732 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002733
2734 if (args.descriptionRes != 0) {
2735 outInfo.descriptionRes = args.sa.getResourceId(args.descriptionRes, 0);
2736 }
2737
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002738 outInfo.enabled = args.sa.getBoolean(args.enabledRes, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 }
2740
2741 public Component(Component<II> clone) {
2742 owner = clone.owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002743 intents = clone.intents;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002744 className = clone.className;
2745 componentName = clone.componentName;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002746 componentShortName = clone.componentShortName;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002747 }
2748
2749 public ComponentName getComponentName() {
2750 if (componentName != null) {
2751 return componentName;
2752 }
2753 if (className != null) {
2754 componentName = new ComponentName(owner.applicationInfo.packageName,
2755 className);
2756 }
2757 return componentName;
2758 }
2759
2760 public String getComponentShortName() {
2761 if (componentShortName != null) {
2762 return componentShortName;
2763 }
2764 ComponentName component = getComponentName();
2765 if (component != null) {
2766 componentShortName = component.flattenToShortString();
2767 }
2768 return componentShortName;
2769 }
2770
2771 public void setPackageName(String packageName) {
2772 componentName = null;
2773 componentShortName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 }
2775 }
2776
2777 public final static class Permission extends Component<IntentInfo> {
2778 public final PermissionInfo info;
2779 public boolean tree;
2780 public PermissionGroup group;
2781
2782 public Permission(Package _owner) {
2783 super(_owner);
2784 info = new PermissionInfo();
2785 }
2786
2787 public Permission(Package _owner, PermissionInfo _info) {
2788 super(_owner);
2789 info = _info;
2790 }
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002791
2792 public void setPackageName(String packageName) {
2793 super.setPackageName(packageName);
2794 info.packageName = packageName;
2795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796
2797 public String toString() {
2798 return "Permission{"
2799 + Integer.toHexString(System.identityHashCode(this))
2800 + " " + info.name + "}";
2801 }
2802 }
2803
2804 public final static class PermissionGroup extends Component<IntentInfo> {
2805 public final PermissionGroupInfo info;
2806
2807 public PermissionGroup(Package _owner) {
2808 super(_owner);
2809 info = new PermissionGroupInfo();
2810 }
2811
2812 public PermissionGroup(Package _owner, PermissionGroupInfo _info) {
2813 super(_owner);
2814 info = _info;
2815 }
2816
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002817 public void setPackageName(String packageName) {
2818 super.setPackageName(packageName);
2819 info.packageName = packageName;
2820 }
2821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 public String toString() {
2823 return "PermissionGroup{"
2824 + Integer.toHexString(System.identityHashCode(this))
2825 + " " + info.name + "}";
2826 }
2827 }
2828
2829 private static boolean copyNeeded(int flags, Package p, Bundle metaData) {
2830 if ((flags & PackageManager.GET_META_DATA) != 0
2831 && (metaData != null || p.mAppMetaData != null)) {
2832 return true;
2833 }
2834 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0
2835 && p.usesLibraryFiles != null) {
2836 return true;
2837 }
2838 return false;
2839 }
2840
2841 public static ApplicationInfo generateApplicationInfo(Package p, int flags) {
2842 if (p == null) return null;
2843 if (!copyNeeded(flags, p, null)) {
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -07002844 // CompatibilityMode is global state. It's safe to modify the instance
2845 // of the package.
2846 if (!sCompatibilityModeEnabled) {
2847 p.applicationInfo.disableCompatibilityMode();
2848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 return p.applicationInfo;
2850 }
2851
2852 // Make shallow copy so we can store the metadata/libraries safely
2853 ApplicationInfo ai = new ApplicationInfo(p.applicationInfo);
2854 if ((flags & PackageManager.GET_META_DATA) != 0) {
2855 ai.metaData = p.mAppMetaData;
2856 }
2857 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0) {
2858 ai.sharedLibraryFiles = p.usesLibraryFiles;
2859 }
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -07002860 if (!sCompatibilityModeEnabled) {
2861 ai.disableCompatibilityMode();
2862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 return ai;
2864 }
2865
2866 public static final PermissionInfo generatePermissionInfo(
2867 Permission p, int flags) {
2868 if (p == null) return null;
2869 if ((flags&PackageManager.GET_META_DATA) == 0) {
2870 return p.info;
2871 }
2872 PermissionInfo pi = new PermissionInfo(p.info);
2873 pi.metaData = p.metaData;
2874 return pi;
2875 }
2876
2877 public static final PermissionGroupInfo generatePermissionGroupInfo(
2878 PermissionGroup pg, int flags) {
2879 if (pg == null) return null;
2880 if ((flags&PackageManager.GET_META_DATA) == 0) {
2881 return pg.info;
2882 }
2883 PermissionGroupInfo pgi = new PermissionGroupInfo(pg.info);
2884 pgi.metaData = pg.metaData;
2885 return pgi;
2886 }
2887
2888 public final static class Activity extends Component<ActivityIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002889 public final ActivityInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002891 public Activity(final ParseComponentArgs args, final ActivityInfo _info) {
2892 super(args, _info);
2893 info = _info;
2894 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002896
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002897 public void setPackageName(String packageName) {
2898 super.setPackageName(packageName);
2899 info.packageName = packageName;
2900 }
2901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 public String toString() {
2903 return "Activity{"
2904 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002905 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 }
2907 }
2908
2909 public static final ActivityInfo generateActivityInfo(Activity a,
2910 int flags) {
2911 if (a == null) return null;
2912 if (!copyNeeded(flags, a.owner, a.metaData)) {
2913 return a.info;
2914 }
2915 // Make shallow copies so we can store the metadata safely
2916 ActivityInfo ai = new ActivityInfo(a.info);
2917 ai.metaData = a.metaData;
2918 ai.applicationInfo = generateApplicationInfo(a.owner, flags);
2919 return ai;
2920 }
2921
2922 public final static class Service extends Component<ServiceIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002923 public final ServiceInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002925 public Service(final ParseComponentArgs args, final ServiceInfo _info) {
2926 super(args, _info);
2927 info = _info;
2928 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002930
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002931 public void setPackageName(String packageName) {
2932 super.setPackageName(packageName);
2933 info.packageName = packageName;
2934 }
2935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 public String toString() {
2937 return "Service{"
2938 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002939 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 }
2941 }
2942
2943 public static final ServiceInfo generateServiceInfo(Service s, int flags) {
2944 if (s == null) return null;
2945 if (!copyNeeded(flags, s.owner, s.metaData)) {
2946 return s.info;
2947 }
2948 // Make shallow copies so we can store the metadata safely
2949 ServiceInfo si = new ServiceInfo(s.info);
2950 si.metaData = s.metaData;
2951 si.applicationInfo = generateApplicationInfo(s.owner, flags);
2952 return si;
2953 }
2954
2955 public final static class Provider extends Component {
2956 public final ProviderInfo info;
2957 public boolean syncable;
2958
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002959 public Provider(final ParseComponentArgs args, final ProviderInfo _info) {
2960 super(args, _info);
2961 info = _info;
2962 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 syncable = false;
2964 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 public Provider(Provider existingProvider) {
2967 super(existingProvider);
2968 this.info = existingProvider.info;
2969 this.syncable = existingProvider.syncable;
2970 }
2971
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002972 public void setPackageName(String packageName) {
2973 super.setPackageName(packageName);
2974 info.packageName = packageName;
2975 }
2976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 public String toString() {
2978 return "Provider{"
2979 + Integer.toHexString(System.identityHashCode(this))
2980 + " " + info.name + "}";
2981 }
2982 }
2983
2984 public static final ProviderInfo generateProviderInfo(Provider p,
2985 int flags) {
2986 if (p == null) return null;
2987 if (!copyNeeded(flags, p.owner, p.metaData)
2988 && ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) != 0
2989 || p.info.uriPermissionPatterns == null)) {
2990 return p.info;
2991 }
2992 // Make shallow copies so we can store the metadata safely
2993 ProviderInfo pi = new ProviderInfo(p.info);
2994 pi.metaData = p.metaData;
2995 if ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) == 0) {
2996 pi.uriPermissionPatterns = null;
2997 }
2998 pi.applicationInfo = generateApplicationInfo(p.owner, flags);
2999 return pi;
3000 }
3001
3002 public final static class Instrumentation extends Component {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003003 public final InstrumentationInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003005 public Instrumentation(final ParsePackageItemArgs args, final InstrumentationInfo _info) {
3006 super(args, _info);
3007 info = _info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003009
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003010 public void setPackageName(String packageName) {
3011 super.setPackageName(packageName);
3012 info.packageName = packageName;
3013 }
3014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 public String toString() {
3016 return "Instrumentation{"
3017 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003018 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 }
3020 }
3021
3022 public static final InstrumentationInfo generateInstrumentationInfo(
3023 Instrumentation i, int flags) {
3024 if (i == null) return null;
3025 if ((flags&PackageManager.GET_META_DATA) == 0) {
3026 return i.info;
3027 }
3028 InstrumentationInfo ii = new InstrumentationInfo(i.info);
3029 ii.metaData = i.metaData;
3030 return ii;
3031 }
3032
3033 public static class IntentInfo extends IntentFilter {
3034 public boolean hasDefault;
3035 public int labelRes;
3036 public CharSequence nonLocalizedLabel;
3037 public int icon;
3038 }
3039
3040 public final static class ActivityIntentInfo extends IntentInfo {
3041 public final Activity activity;
3042
3043 public ActivityIntentInfo(Activity _activity) {
3044 activity = _activity;
3045 }
3046
3047 public String toString() {
3048 return "ActivityIntentInfo{"
3049 + Integer.toHexString(System.identityHashCode(this))
3050 + " " + activity.info.name + "}";
3051 }
3052 }
3053
3054 public final static class ServiceIntentInfo extends IntentInfo {
3055 public final Service service;
3056
3057 public ServiceIntentInfo(Service _service) {
3058 service = _service;
3059 }
3060
3061 public String toString() {
3062 return "ServiceIntentInfo{"
3063 + Integer.toHexString(System.identityHashCode(this))
3064 + " " + service.info.name + "}";
3065 }
3066 }
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -07003067
3068 /**
3069 * @hide
3070 */
3071 public static void setCompatibilityModeEnabled(boolean compatibilityModeEnabled) {
3072 sCompatibilityModeEnabled = compatibilityModeEnabled;
3073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074}