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