Home | History | Annotate | Download | only in pm
      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 
     17 package android.content.pm;
     18 
     19 import org.xmlpull.v1.XmlPullParser;
     20 import org.xmlpull.v1.XmlPullParserException;
     21 
     22 import android.content.ComponentName;
     23 import android.content.Intent;
     24 import android.content.IntentFilter;
     25 import android.content.res.AssetManager;
     26 import android.content.res.Configuration;
     27 import android.content.res.Resources;
     28 import android.content.res.TypedArray;
     29 import android.content.res.XmlResourceParser;
     30 import android.os.Build;
     31 import android.os.Bundle;
     32 import android.os.PatternMatcher;
     33 import android.provider.Settings;
     34 import android.util.AttributeSet;
     35 import android.util.Config;
     36 import android.util.DisplayMetrics;
     37 import android.util.Log;
     38 import android.util.TypedValue;
     39 
     40 import com.android.internal.util.XmlUtils;
     41 
     42 import java.io.File;
     43 import java.io.IOException;
     44 import java.io.InputStream;
     45 import java.lang.ref.WeakReference;
     46 import java.security.cert.Certificate;
     47 import java.security.cert.CertificateEncodingException;
     48 import java.util.ArrayList;
     49 import java.util.Enumeration;
     50 import java.util.Iterator;
     51 import java.util.List;
     52 import java.util.jar.JarEntry;
     53 import java.util.jar.JarFile;
     54 
     55 /**
     56  * Package archive parsing
     57  *
     58  * {@hide}
     59  */
     60 public 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         String str;
   1546         str = sa.getNonConfigurationString(
   1547                 com.android.internal.R.styleable.AndroidManifestApplication_permission, 0);
   1548         ai.permission = (str != null && str.length() > 0) ? str.intern() : null;
   1549 
   1550         if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
   1551             str = sa.getNonConfigurationString(
   1552                     com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity, 0);
   1553         } else {
   1554             // Some older apps have been seen to use a resource reference
   1555             // here that on older builds was ignored (with a warning).  We
   1556             // need to continue to do this for them so they don't break.
   1557             str = sa.getNonResourceString(
   1558                     com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity);
   1559         }
   1560         ai.taskAffinity = buildTaskAffinityName(ai.packageName, ai.packageName,
   1561                 str, outError);
   1562 
   1563         if (outError[0] == null) {
   1564             CharSequence pname;
   1565             if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
   1566                 pname = sa.getNonConfigurationString(
   1567                         com.android.internal.R.styleable.AndroidManifestApplication_process, 0);
   1568             } else {
   1569                 // Some older apps have been seen to use a resource reference
   1570                 // here that on older builds was ignored (with a warning).  We
   1571                 // need to continue to do this for them so they don't break.
   1572                 pname = sa.getNonResourceString(
   1573                         com.android.internal.R.styleable.AndroidManifestApplication_process);
   1574             }
   1575             ai.processName = buildProcessName(ai.packageName, null, pname,
   1576                     flags, mSeparateProcesses, outError);
   1577 
   1578             ai.enabled = sa.getBoolean(
   1579                     com.android.internal.R.styleable.AndroidManifestApplication_enabled, true);
   1580         }
   1581 
   1582         sa.recycle();
   1583 
   1584         if (outError[0] != null) {
   1585             mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
   1586             return false;
   1587         }
   1588 
   1589         final int innerDepth = parser.getDepth();
   1590 
   1591         int type;
   1592         while ((type=parser.next()) != parser.END_DOCUMENT
   1593                && (type != parser.END_TAG || parser.getDepth() > innerDepth)) {
   1594             if (type == parser.END_TAG || type == parser.TEXT) {
   1595                 continue;
   1596             }
   1597 
   1598             String tagName = parser.getName();
   1599             if (tagName.equals("activity")) {
   1600                 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, false);
   1601                 if (a == null) {
   1602                     mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
   1603                     return false;
   1604                 }
   1605 
   1606                 owner.activities.add(a);
   1607 
   1608             } else if (tagName.equals("receiver")) {
   1609                 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, true);
   1610                 if (a == null) {
   1611                     mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
   1612                     return false;
   1613                 }
   1614 
   1615                 owner.receivers.add(a);
   1616 
   1617             } else if (tagName.equals("service")) {
   1618                 Service s = parseService(owner, res, parser, attrs, flags, outError);
   1619                 if (s == null) {
   1620                     mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
   1621                     return false;
   1622                 }
   1623 
   1624                 owner.services.add(s);
   1625 
   1626             } else if (tagName.equals("provider")) {
   1627                 Provider p = parseProvider(owner, res, parser, attrs, flags, outError);
   1628                 if (p == null) {
   1629                     mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
   1630                     return false;
   1631                 }
   1632 
   1633                 owner.providers.add(p);
   1634 
   1635             } else if (tagName.equals("activity-alias")) {
   1636                 Activity a = parseActivityAlias(owner, res, parser, attrs, flags, outError);
   1637                 if (a == null) {
   1638                     mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
   1639                     return false;
   1640                 }
   1641 
   1642                 owner.activities.add(a);
   1643 
   1644             } else if (parser.getName().equals("meta-data")) {
   1645                 // note: application meta-data is stored off to the side, so it can
   1646                 // remain null in the primary copy (we like to avoid extra copies because
   1647                 // it can be large)
   1648                 if ((owner.mAppMetaData = parseMetaData(res, parser, attrs, owner.mAppMetaData,
   1649                         outError)) == null) {
   1650                     mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
   1651                     return false;
   1652                 }
   1653 
   1654             } else if (tagName.equals("uses-library")) {
   1655                 sa = res.obtainAttributes(attrs,
   1656                         com.android.internal.R.styleable.AndroidManifestUsesLibrary);
   1657 
   1658                 // Note: don't allow this value to be a reference to a resource
   1659                 // that may change.
   1660                 String lname = sa.getNonResourceString(
   1661                         com.android.internal.R.styleable.AndroidManifestUsesLibrary_name);
   1662                 boolean req = sa.getBoolean(
   1663                         com.android.internal.R.styleable.AndroidManifestUsesLibrary_required,
   1664                         true);
   1665 
   1666                 sa.recycle();
   1667 
   1668                 if (lname != null) {
   1669                     if (req) {
   1670                         if (owner.usesLibraries == null) {
   1671                             owner.usesLibraries = new ArrayList<String>();
   1672                         }
   1673                         if (!owner.usesLibraries.contains(lname)) {
   1674                             owner.usesLibraries.add(lname.intern());
   1675                         }
   1676                     } else {
   1677                         if (owner.usesOptionalLibraries == null) {
   1678                             owner.usesOptionalLibraries = new ArrayList<String>();
   1679                         }
   1680                         if (!owner.usesOptionalLibraries.contains(lname)) {
   1681                             owner.usesOptionalLibraries.add(lname.intern());
   1682                         }
   1683                     }
   1684                 }
   1685 
   1686                 XmlUtils.skipCurrentTag(parser);
   1687 
   1688             } else {
   1689                 if (!RIGID_PARSER) {
   1690                     Log.w(TAG, "Unknown element under <application>: " + tagName
   1691                             + " at " + mArchiveSourcePath + " "
   1692                             + parser.getPositionDescription());
   1693                     XmlUtils.skipCurrentTag(parser);
   1694                     continue;
   1695                 } else {
   1696                     outError[0] = "Bad element under <application>: " + tagName;
   1697                     mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
   1698                     return false;
   1699                 }
   1700             }
   1701         }
   1702 
   1703         return true;
   1704     }
   1705 
   1706     private boolean parsePackageItemInfo(Package owner, PackageItemInfo outInfo,
   1707             String[] outError, String tag, TypedArray sa,
   1708             int nameRes, int labelRes, int iconRes) {
   1709         String name = sa.getNonConfigurationString(nameRes, 0);
   1710         if (name == null) {
   1711             outError[0] = tag + " does not specify android:name";
   1712             return false;
   1713         }
   1714 
   1715         outInfo.name
   1716             = buildClassName(owner.applicationInfo.packageName, name, outError);
   1717         if (outInfo.name == null) {
   1718             return false;
   1719         }
   1720 
   1721         int iconVal = sa.getResourceId(iconRes, 0);
   1722         if (iconVal != 0) {
   1723             outInfo.icon = iconVal;
   1724             outInfo.nonLocalizedLabel = null;
   1725         }
   1726 
   1727         TypedValue v = sa.peekValue(labelRes);
   1728         if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
   1729             outInfo.nonLocalizedLabel = v.coerceToString();
   1730         }
   1731 
   1732         outInfo.packageName = owner.packageName;
   1733 
   1734         return true;
   1735     }
   1736 
   1737     private Activity parseActivity(Package owner, Resources res,
   1738             XmlPullParser parser, AttributeSet attrs, int flags, String[] outError,
   1739             boolean receiver) throws XmlPullParserException, IOException {
   1740         TypedArray sa = res.obtainAttributes(attrs,
   1741                 com.android.internal.R.styleable.AndroidManifestActivity);
   1742 
   1743         if (mParseActivityArgs == null) {
   1744             mParseActivityArgs = new ParseComponentArgs(owner, outError,
   1745                     com.android.internal.R.styleable.AndroidManifestActivity_name,
   1746                     com.android.internal.R.styleable.AndroidManifestActivity_label,
   1747                     com.android.internal.R.styleable.AndroidManifestActivity_icon,
   1748                     mSeparateProcesses,
   1749                     com.android.internal.R.styleable.AndroidManifestActivity_process,
   1750                     com.android.internal.R.styleable.AndroidManifestActivity_description,
   1751                     com.android.internal.R.styleable.AndroidManifestActivity_enabled);
   1752         }
   1753 
   1754         mParseActivityArgs.tag = receiver ? "<receiver>" : "<activity>";
   1755         mParseActivityArgs.sa = sa;
   1756         mParseActivityArgs.flags = flags;
   1757 
   1758         Activity a = new Activity(mParseActivityArgs, new ActivityInfo());
   1759         if (outError[0] != null) {
   1760             sa.recycle();
   1761             return null;
   1762         }
   1763 
   1764         final boolean setExported = sa.hasValue(
   1765                 com.android.internal.R.styleable.AndroidManifestActivity_exported);
   1766         if (setExported) {
   1767             a.info.exported = sa.getBoolean(
   1768                     com.android.internal.R.styleable.AndroidManifestActivity_exported, false);
   1769         }
   1770 
   1771         a.info.theme = sa.getResourceId(
   1772                 com.android.internal.R.styleable.AndroidManifestActivity_theme, 0);
   1773 
   1774         String str;
   1775         str = sa.getNonConfigurationString(
   1776                 com.android.internal.R.styleable.AndroidManifestActivity_permission, 0);
   1777         if (str == null) {
   1778             a.info.permission = owner.applicationInfo.permission;
   1779         } else {
   1780             a.info.permission = str.length() > 0 ? str.toString().intern() : null;
   1781         }
   1782 
   1783         str = sa.getNonConfigurationString(
   1784                 com.android.internal.R.styleable.AndroidManifestActivity_taskAffinity, 0);
   1785         a.info.taskAffinity = buildTaskAffinityName(owner.applicationInfo.packageName,
   1786                 owner.applicationInfo.taskAffinity, str, outError);
   1787 
   1788         a.info.flags = 0;
   1789         if (sa.getBoolean(
   1790                 com.android.internal.R.styleable.AndroidManifestActivity_multiprocess,
   1791                 false)) {
   1792             a.info.flags |= ActivityInfo.FLAG_MULTIPROCESS;
   1793         }
   1794 
   1795         if (sa.getBoolean(
   1796                 com.android.internal.R.styleable.AndroidManifestActivity_finishOnTaskLaunch,
   1797                 false)) {
   1798             a.info.flags |= ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH;
   1799         }
   1800 
   1801         if (sa.getBoolean(
   1802                 com.android.internal.R.styleable.AndroidManifestActivity_clearTaskOnLaunch,
   1803                 false)) {
   1804             a.info.flags |= ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH;
   1805         }
   1806 
   1807         if (sa.getBoolean(
   1808                 com.android.internal.R.styleable.AndroidManifestActivity_noHistory,
   1809                 false)) {
   1810             a.info.flags |= ActivityInfo.FLAG_NO_HISTORY;
   1811         }
   1812 
   1813         if (sa.getBoolean(
   1814                 com.android.internal.R.styleable.AndroidManifestActivity_alwaysRetainTaskState,
   1815                 false)) {
   1816             a.info.flags |= ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE;
   1817         }
   1818 
   1819         if (sa.getBoolean(
   1820                 com.android.internal.R.styleable.AndroidManifestActivity_stateNotNeeded,
   1821                 false)) {
   1822             a.info.flags |= ActivityInfo.FLAG_STATE_NOT_NEEDED;
   1823         }
   1824 
   1825         if (sa.getBoolean(
   1826                 com.android.internal.R.styleable.AndroidManifestActivity_excludeFromRecents,
   1827                 false)) {
   1828             a.info.flags |= ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
   1829         }
   1830 
   1831         if (sa.getBoolean(
   1832                 com.android.internal.R.styleable.AndroidManifestActivity_allowTaskReparenting,
   1833                 (owner.applicationInfo.flags&ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING) != 0)) {
   1834             a.info.flags |= ActivityInfo.FLAG_ALLOW_TASK_REPARENTING;
   1835         }
   1836 
   1837         if (sa.getBoolean(
   1838                 com.android.internal.R.styleable.AndroidManifestActivity_finishOnCloseSystemDialogs,
   1839                 false)) {
   1840             a.info.flags |= ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS;
   1841         }
   1842 
   1843         if (!receiver) {
   1844             a.info.launchMode = sa.getInt(
   1845                     com.android.internal.R.styleable.AndroidManifestActivity_launchMode,
   1846                     ActivityInfo.LAUNCH_MULTIPLE);
   1847             a.info.screenOrientation = sa.getInt(
   1848                     com.android.internal.R.styleable.AndroidManifestActivity_screenOrientation,
   1849                     ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
   1850             a.info.configChanges = sa.getInt(
   1851                     com.android.internal.R.styleable.AndroidManifestActivity_configChanges,
   1852                     0);
   1853             a.info.softInputMode = sa.getInt(
   1854                     com.android.internal.R.styleable.AndroidManifestActivity_windowSoftInputMode,
   1855                     0);
   1856         } else {
   1857             a.info.launchMode = ActivityInfo.LAUNCH_MULTIPLE;
   1858             a.info.configChanges = 0;
   1859         }
   1860 
   1861         sa.recycle();
   1862 
   1863         if (outError[0] != null) {
   1864             return null;
   1865         }
   1866 
   1867         int outerDepth = parser.getDepth();
   1868         int type;
   1869         while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
   1870                && (type != XmlPullParser.END_TAG
   1871                        || parser.getDepth() > outerDepth)) {
   1872             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   1873                 continue;
   1874             }
   1875 
   1876             if (parser.getName().equals("intent-filter")) {
   1877                 ActivityIntentInfo intent = new ActivityIntentInfo(a);
   1878                 if (!parseIntent(res, parser, attrs, flags, intent, outError, !receiver)) {
   1879                     return null;
   1880                 }
   1881                 if (intent.countActions() == 0) {
   1882                     Log.w(TAG, "No actions in intent filter at "
   1883                             + mArchiveSourcePath + " "
   1884                             + parser.getPositionDescription());
   1885                 } else {
   1886                     a.intents.add(intent);
   1887                 }
   1888             } else if (parser.getName().equals("meta-data")) {
   1889                 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
   1890                         outError)) == null) {
   1891                     return null;
   1892                 }
   1893             } else {
   1894                 if (!RIGID_PARSER) {
   1895                     Log.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
   1896                     if (receiver) {
   1897                         Log.w(TAG, "Unknown element under <receiver>: " + parser.getName()
   1898                                 + " at " + mArchiveSourcePath + " "
   1899                                 + parser.getPositionDescription());
   1900                     } else {
   1901                         Log.w(TAG, "Unknown element under <activity>: " + parser.getName()
   1902                                 + " at " + mArchiveSourcePath + " "
   1903                                 + parser.getPositionDescription());
   1904                     }
   1905                     XmlUtils.skipCurrentTag(parser);
   1906                     continue;
   1907                 }
   1908                 if (receiver) {
   1909                     outError[0] = "Bad element under <receiver>: " + parser.getName();
   1910                 } else {
   1911                     outError[0] = "Bad element under <activity>: " + parser.getName();
   1912                 }
   1913                 return null;
   1914             }
   1915         }
   1916 
   1917         if (!setExported) {
   1918             a.info.exported = a.intents.size() > 0;
   1919         }
   1920 
   1921         return a;
   1922     }
   1923 
   1924     private Activity parseActivityAlias(Package owner, Resources res,
   1925             XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
   1926             throws XmlPullParserException, IOException {
   1927         TypedArray sa = res.obtainAttributes(attrs,
   1928                 com.android.internal.R.styleable.AndroidManifestActivityAlias);
   1929 
   1930         String targetActivity = sa.getNonConfigurationString(
   1931                 com.android.internal.R.styleable.AndroidManifestActivityAlias_targetActivity, 0);
   1932         if (targetActivity == null) {
   1933             outError[0] = "<activity-alias> does not specify android:targetActivity";
   1934             sa.recycle();
   1935             return null;
   1936         }
   1937 
   1938         targetActivity = buildClassName(owner.applicationInfo.packageName,
   1939                 targetActivity, outError);
   1940         if (targetActivity == null) {
   1941             sa.recycle();
   1942             return null;
   1943         }
   1944 
   1945         if (mParseActivityAliasArgs == null) {
   1946             mParseActivityAliasArgs = new ParseComponentArgs(owner, outError,
   1947                     com.android.internal.R.styleable.AndroidManifestActivityAlias_name,
   1948                     com.android.internal.R.styleable.AndroidManifestActivityAlias_label,
   1949                     com.android.internal.R.styleable.AndroidManifestActivityAlias_icon,
   1950                     mSeparateProcesses,
   1951                     0,
   1952                     com.android.internal.R.styleable.AndroidManifestActivityAlias_description,
   1953                     com.android.internal.R.styleable.AndroidManifestActivityAlias_enabled);
   1954             mParseActivityAliasArgs.tag = "<activity-alias>";
   1955         }
   1956 
   1957         mParseActivityAliasArgs.sa = sa;
   1958         mParseActivityAliasArgs.flags = flags;
   1959 
   1960         Activity target = null;
   1961 
   1962         final int NA = owner.activities.size();
   1963         for (int i=0; i<NA; i++) {
   1964             Activity t = owner.activities.get(i);
   1965             if (targetActivity.equals(t.info.name)) {
   1966                 target = t;
   1967                 break;
   1968             }
   1969         }
   1970 
   1971         if (target == null) {
   1972             outError[0] = "<activity-alias> target activity " + targetActivity
   1973                     + " not found in manifest";
   1974             sa.recycle();
   1975             return null;
   1976         }
   1977 
   1978         ActivityInfo info = new ActivityInfo();
   1979         info.targetActivity = targetActivity;
   1980         info.configChanges = target.info.configChanges;
   1981         info.flags = target.info.flags;
   1982         info.icon = target.info.icon;
   1983         info.labelRes = target.info.labelRes;
   1984         info.nonLocalizedLabel = target.info.nonLocalizedLabel;
   1985         info.launchMode = target.info.launchMode;
   1986         info.processName = target.info.processName;
   1987         if (info.descriptionRes == 0) {
   1988             info.descriptionRes = target.info.descriptionRes;
   1989         }
   1990         info.screenOrientation = target.info.screenOrientation;
   1991         info.taskAffinity = target.info.taskAffinity;
   1992         info.theme = target.info.theme;
   1993 
   1994         Activity a = new Activity(mParseActivityAliasArgs, info);
   1995         if (outError[0] != null) {
   1996             sa.recycle();
   1997             return null;
   1998         }
   1999 
   2000         final boolean setExported = sa.hasValue(
   2001                 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported);
   2002         if (setExported) {
   2003             a.info.exported = sa.getBoolean(
   2004                     com.android.internal.R.styleable.AndroidManifestActivityAlias_exported, false);
   2005         }
   2006 
   2007         String str;
   2008         str = sa.getNonConfigurationString(
   2009                 com.android.internal.R.styleable.AndroidManifestActivityAlias_permission, 0);
   2010         if (str != null) {
   2011             a.info.permission = str.length() > 0 ? str.toString().intern() : null;
   2012         }
   2013 
   2014         sa.recycle();
   2015 
   2016         if (outError[0] != null) {
   2017             return null;
   2018         }
   2019 
   2020         int outerDepth = parser.getDepth();
   2021         int type;
   2022         while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
   2023                && (type != XmlPullParser.END_TAG
   2024                        || parser.getDepth() > outerDepth)) {
   2025             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   2026                 continue;
   2027             }
   2028 
   2029             if (parser.getName().equals("intent-filter")) {
   2030                 ActivityIntentInfo intent = new ActivityIntentInfo(a);
   2031                 if (!parseIntent(res, parser, attrs, flags, intent, outError, true)) {
   2032                     return null;
   2033                 }
   2034                 if (intent.countActions() == 0) {
   2035                     Log.w(TAG, "No actions in intent filter at "
   2036                             + mArchiveSourcePath + " "
   2037                             + parser.getPositionDescription());
   2038                 } else {
   2039                     a.intents.add(intent);
   2040                 }
   2041             } else if (parser.getName().equals("meta-data")) {
   2042                 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
   2043                         outError)) == null) {
   2044                     return null;
   2045                 }
   2046             } else {
   2047                 if (!RIGID_PARSER) {
   2048                     Log.w(TAG, "Unknown element under <activity-alias>: " + parser.getName()
   2049                             + " at " + mArchiveSourcePath + " "
   2050                             + parser.getPositionDescription());
   2051                     XmlUtils.skipCurrentTag(parser);
   2052                     continue;
   2053                 }
   2054                 outError[0] = "Bad element under <activity-alias>: " + parser.getName();
   2055                 return null;
   2056             }
   2057         }
   2058 
   2059         if (!setExported) {
   2060             a.info.exported = a.intents.size() > 0;
   2061         }
   2062 
   2063         return a;
   2064     }
   2065 
   2066     private Provider parseProvider(Package owner, Resources res,
   2067             XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
   2068             throws XmlPullParserException, IOException {
   2069         TypedArray sa = res.obtainAttributes(attrs,
   2070                 com.android.internal.R.styleable.AndroidManifestProvider);
   2071 
   2072         if (mParseProviderArgs == null) {
   2073             mParseProviderArgs = new ParseComponentArgs(owner, outError,
   2074                     com.android.internal.R.styleable.AndroidManifestProvider_name,
   2075                     com.android.internal.R.styleable.AndroidManifestProvider_label,
   2076                     com.android.internal.R.styleable.AndroidManifestProvider_icon,
   2077                     mSeparateProcesses,
   2078                     com.android.internal.R.styleable.AndroidManifestProvider_process,
   2079                     com.android.internal.R.styleable.AndroidManifestProvider_description,
   2080                     com.android.internal.R.styleable.AndroidManifestProvider_enabled);
   2081             mParseProviderArgs.tag = "<provider>";
   2082         }
   2083 
   2084         mParseProviderArgs.sa = sa;
   2085         mParseProviderArgs.flags = flags;
   2086 
   2087         Provider p = new Provider(mParseProviderArgs, new ProviderInfo());
   2088         if (outError[0] != null) {
   2089             sa.recycle();
   2090             return null;
   2091         }
   2092 
   2093         p.info.exported = sa.getBoolean(
   2094                 com.android.internal.R.styleable.AndroidManifestProvider_exported, true);
   2095 
   2096         String cpname = sa.getNonConfigurationString(
   2097                 com.android.internal.R.styleable.AndroidManifestProvider_authorities, 0);
   2098 
   2099         p.info.isSyncable = sa.getBoolean(
   2100                 com.android.internal.R.styleable.AndroidManifestProvider_syncable,
   2101                 false);
   2102 
   2103         String permission = sa.getNonConfigurationString(
   2104                 com.android.internal.R.styleable.AndroidManifestProvider_permission, 0);
   2105         String str = sa.getNonConfigurationString(
   2106                 com.android.internal.R.styleable.AndroidManifestProvider_readPermission, 0);
   2107         if (str == null) {
   2108             str = permission;
   2109         }
   2110         if (str == null) {
   2111             p.info.readPermission = owner.applicationInfo.permission;
   2112         } else {
   2113             p.info.readPermission =
   2114                 str.length() > 0 ? str.toString().intern() : null;
   2115         }
   2116         str = sa.getNonConfigurationString(
   2117                 com.android.internal.R.styleable.AndroidManifestProvider_writePermission, 0);
   2118         if (str == null) {
   2119             str = permission;
   2120         }
   2121         if (str == null) {
   2122             p.info.writePermission = owner.applicationInfo.permission;
   2123         } else {
   2124             p.info.writePermission =
   2125                 str.length() > 0 ? str.toString().intern() : null;
   2126         }
   2127 
   2128         p.info.grantUriPermissions = sa.getBoolean(
   2129                 com.android.internal.R.styleable.AndroidManifestProvider_grantUriPermissions,
   2130                 false);
   2131 
   2132         p.info.multiprocess = sa.getBoolean(
   2133                 com.android.internal.R.styleable.AndroidManifestProvider_multiprocess,
   2134                 false);
   2135 
   2136         p.info.initOrder = sa.getInt(
   2137                 com.android.internal.R.styleable.AndroidManifestProvider_initOrder,
   2138                 0);
   2139 
   2140         sa.recycle();
   2141 
   2142         if (cpname == null) {
   2143             outError[0] = "<provider> does not incude authorities attribute";
   2144             return null;
   2145         }
   2146         p.info.authority = cpname.intern();
   2147 
   2148         if (!parseProviderTags(res, parser, attrs, p, outError)) {
   2149             return null;
   2150         }
   2151 
   2152         return p;
   2153     }
   2154 
   2155     private boolean parseProviderTags(Resources res,
   2156             XmlPullParser parser, AttributeSet attrs,
   2157             Provider outInfo, String[] outError)
   2158             throws XmlPullParserException, IOException {
   2159         int outerDepth = parser.getDepth();
   2160         int type;
   2161         while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
   2162                && (type != XmlPullParser.END_TAG
   2163                        || parser.getDepth() > outerDepth)) {
   2164             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   2165                 continue;
   2166             }
   2167 
   2168             if (parser.getName().equals("meta-data")) {
   2169                 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
   2170                         outInfo.metaData, outError)) == null) {
   2171                     return false;
   2172                 }
   2173 
   2174             } else if (parser.getName().equals("grant-uri-permission")) {
   2175                 TypedArray sa = res.obtainAttributes(attrs,
   2176                         com.android.internal.R.styleable.AndroidManifestGrantUriPermission);
   2177 
   2178                 PatternMatcher pa = null;
   2179 
   2180                 String str = sa.getNonConfigurationString(
   2181                         com.android.internal.R.styleable.AndroidManifestGrantUriPermission_path, 0);
   2182                 if (str != null) {
   2183                     pa = new PatternMatcher(str, PatternMatcher.PATTERN_LITERAL);
   2184                 }
   2185 
   2186                 str = sa.getNonConfigurationString(
   2187                         com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPrefix, 0);
   2188                 if (str != null) {
   2189                     pa = new PatternMatcher(str, PatternMatcher.PATTERN_PREFIX);
   2190                 }
   2191 
   2192                 str = sa.getNonConfigurationString(
   2193                         com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPattern, 0);
   2194                 if (str != null) {
   2195                     pa = new PatternMatcher(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
   2196                 }
   2197 
   2198                 sa.recycle();
   2199 
   2200                 if (pa != null) {
   2201                     if (outInfo.info.uriPermissionPatterns == null) {
   2202                         outInfo.info.uriPermissionPatterns = new PatternMatcher[1];
   2203                         outInfo.info.uriPermissionPatterns[0] = pa;
   2204                     } else {
   2205                         final int N = outInfo.info.uriPermissionPatterns.length;
   2206                         PatternMatcher[] newp = new PatternMatcher[N+1];
   2207                         System.arraycopy(outInfo.info.uriPermissionPatterns, 0, newp, 0, N);
   2208                         newp[N] = pa;
   2209                         outInfo.info.uriPermissionPatterns = newp;
   2210                     }
   2211                     outInfo.info.grantUriPermissions = true;
   2212                 } else {
   2213                     if (!RIGID_PARSER) {
   2214                         Log.w(TAG, "Unknown element under <path-permission>: "
   2215                                 + parser.getName() + " at " + mArchiveSourcePath + " "
   2216                                 + parser.getPositionDescription());
   2217                         XmlUtils.skipCurrentTag(parser);
   2218                         continue;
   2219                     }
   2220                     outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
   2221                     return false;
   2222                 }
   2223                 XmlUtils.skipCurrentTag(parser);
   2224 
   2225             } else if (parser.getName().equals("path-permission")) {
   2226                 TypedArray sa = res.obtainAttributes(attrs,
   2227                         com.android.internal.R.styleable.AndroidManifestPathPermission);
   2228 
   2229                 PathPermission pa = null;
   2230 
   2231                 String permission = sa.getNonConfigurationString(
   2232                         com.android.internal.R.styleable.AndroidManifestPathPermission_permission, 0);
   2233                 String readPermission = sa.getNonConfigurationString(
   2234                         com.android.internal.R.styleable.AndroidManifestPathPermission_readPermission, 0);
   2235                 if (readPermission == null) {
   2236                     readPermission = permission;
   2237                 }
   2238                 String writePermission = sa.getNonConfigurationString(
   2239                         com.android.internal.R.styleable.AndroidManifestPathPermission_writePermission, 0);
   2240                 if (writePermission == null) {
   2241                     writePermission = permission;
   2242                 }
   2243 
   2244                 boolean havePerm = false;
   2245                 if (readPermission != null) {
   2246                     readPermission = readPermission.intern();
   2247                     havePerm = true;
   2248                 }
   2249                 if (writePermission != null) {
   2250                     writePermission = writePermission.intern();
   2251                     havePerm = true;
   2252                 }
   2253 
   2254                 if (!havePerm) {
   2255                     if (!RIGID_PARSER) {
   2256                         Log.w(TAG, "No readPermission or writePermssion for <path-permission>: "
   2257                                 + parser.getName() + " at " + mArchiveSourcePath + " "
   2258                                 + parser.getPositionDescription());
   2259                         XmlUtils.skipCurrentTag(parser);
   2260                         continue;
   2261                     }
   2262                     outError[0] = "No readPermission or writePermssion for <path-permission>";
   2263                     return false;
   2264                 }
   2265 
   2266                 String path = sa.getNonConfigurationString(
   2267                         com.android.internal.R.styleable.AndroidManifestPathPermission_path, 0);
   2268                 if (path != null) {
   2269                     pa = new PathPermission(path,
   2270                             PatternMatcher.PATTERN_LITERAL, readPermission, writePermission);
   2271                 }
   2272 
   2273                 path = sa.getNonConfigurationString(
   2274                         com.android.internal.R.styleable.AndroidManifestPathPermission_pathPrefix, 0);
   2275                 if (path != null) {
   2276                     pa = new PathPermission(path,
   2277                             PatternMatcher.PATTERN_PREFIX, readPermission, writePermission);
   2278                 }
   2279 
   2280                 path = sa.getNonConfigurationString(
   2281                         com.android.internal.R.styleable.AndroidManifestPathPermission_pathPattern, 0);
   2282                 if (path != null) {
   2283                     pa = new PathPermission(path,
   2284                             PatternMatcher.PATTERN_SIMPLE_GLOB, readPermission, writePermission);
   2285                 }
   2286 
   2287                 sa.recycle();
   2288 
   2289                 if (pa != null) {
   2290                     if (outInfo.info.pathPermissions == null) {
   2291                         outInfo.info.pathPermissions = new PathPermission[1];
   2292                         outInfo.info.pathPermissions[0] = pa;
   2293                     } else {
   2294                         final int N = outInfo.info.pathPermissions.length;
   2295                         PathPermission[] newp = new PathPermission[N+1];
   2296                         System.arraycopy(outInfo.info.pathPermissions, 0, newp, 0, N);
   2297                         newp[N] = pa;
   2298                         outInfo.info.pathPermissions = newp;
   2299                     }
   2300                 } else {
   2301                     if (!RIGID_PARSER) {
   2302                         Log.w(TAG, "No path, pathPrefix, or pathPattern for <path-permission>: "
   2303                                 + parser.getName() + " at " + mArchiveSourcePath + " "
   2304                                 + parser.getPositionDescription());
   2305                         XmlUtils.skipCurrentTag(parser);
   2306                         continue;
   2307                     }
   2308                     outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
   2309                     return false;
   2310                 }
   2311                 XmlUtils.skipCurrentTag(parser);
   2312 
   2313             } else {
   2314                 if (!RIGID_PARSER) {
   2315                     Log.w(TAG, "Unknown element under <provider>: "
   2316                             + parser.getName() + " at " + mArchiveSourcePath + " "
   2317                             + parser.getPositionDescription());
   2318                     XmlUtils.skipCurrentTag(parser);
   2319                     continue;
   2320                 }
   2321                 outError[0] = "Bad element under <provider>: "
   2322                     + parser.getName();
   2323                 return false;
   2324             }
   2325         }
   2326         return true;
   2327     }
   2328 
   2329     private Service parseService(Package owner, Resources res,
   2330             XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
   2331             throws XmlPullParserException, IOException {
   2332         TypedArray sa = res.obtainAttributes(attrs,
   2333                 com.android.internal.R.styleable.AndroidManifestService);
   2334 
   2335         if (mParseServiceArgs == null) {
   2336             mParseServiceArgs = new ParseComponentArgs(owner, outError,
   2337                     com.android.internal.R.styleable.AndroidManifestService_name,
   2338                     com.android.internal.R.styleable.AndroidManifestService_label,
   2339                     com.android.internal.R.styleable.AndroidManifestService_icon,
   2340                     mSeparateProcesses,
   2341                     com.android.internal.R.styleable.AndroidManifestService_process,
   2342                     com.android.internal.R.styleable.AndroidManifestService_description,
   2343                     com.android.internal.R.styleable.AndroidManifestService_enabled);
   2344             mParseServiceArgs.tag = "<service>";
   2345         }
   2346 
   2347         mParseServiceArgs.sa = sa;
   2348         mParseServiceArgs.flags = flags;
   2349 
   2350         Service s = new Service(mParseServiceArgs, new ServiceInfo());
   2351         if (outError[0] != null) {
   2352             sa.recycle();
   2353             return null;
   2354         }
   2355 
   2356         final boolean setExported = sa.hasValue(
   2357                 com.android.internal.R.styleable.AndroidManifestService_exported);
   2358         if (setExported) {
   2359             s.info.exported = sa.getBoolean(
   2360                     com.android.internal.R.styleable.AndroidManifestService_exported, false);
   2361         }
   2362 
   2363         String str = sa.getNonConfigurationString(
   2364                 com.android.internal.R.styleable.AndroidManifestService_permission, 0);
   2365         if (str == null) {
   2366             s.info.permission = owner.applicationInfo.permission;
   2367         } else {
   2368             s.info.permission = str.length() > 0 ? str.toString().intern() : null;
   2369         }
   2370 
   2371         sa.recycle();
   2372 
   2373         int outerDepth = parser.getDepth();
   2374         int type;
   2375         while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
   2376                && (type != XmlPullParser.END_TAG
   2377                        || parser.getDepth() > outerDepth)) {
   2378             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   2379                 continue;
   2380             }
   2381 
   2382             if (parser.getName().equals("intent-filter")) {
   2383                 ServiceIntentInfo intent = new ServiceIntentInfo(s);
   2384                 if (!parseIntent(res, parser, attrs, flags, intent, outError, false)) {
   2385                     return null;
   2386                 }
   2387 
   2388                 s.intents.add(intent);
   2389             } else if (parser.getName().equals("meta-data")) {
   2390                 if ((s.metaData=parseMetaData(res, parser, attrs, s.metaData,
   2391                         outError)) == null) {
   2392                     return null;
   2393                 }
   2394             } else {
   2395                 if (!RIGID_PARSER) {
   2396                     Log.w(TAG, "Unknown element under <service>: "
   2397                             + parser.getName() + " at " + mArchiveSourcePath + " "
   2398                             + parser.getPositionDescription());
   2399                     XmlUtils.skipCurrentTag(parser);
   2400                     continue;
   2401                 }
   2402                 outError[0] = "Bad element under <service>: "
   2403                     + parser.getName();
   2404                 return null;
   2405             }
   2406         }
   2407 
   2408         if (!setExported) {
   2409             s.info.exported = s.intents.size() > 0;
   2410         }
   2411 
   2412         return s;
   2413     }
   2414 
   2415     private boolean parseAllMetaData(Resources res,
   2416             XmlPullParser parser, AttributeSet attrs, String tag,
   2417             Component outInfo, String[] outError)
   2418             throws XmlPullParserException, IOException {
   2419         int outerDepth = parser.getDepth();
   2420         int type;
   2421         while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
   2422                && (type != XmlPullParser.END_TAG
   2423                        || parser.getDepth() > outerDepth)) {
   2424             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   2425                 continue;
   2426             }
   2427 
   2428             if (parser.getName().equals("meta-data")) {
   2429                 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
   2430                         outInfo.metaData, outError)) == null) {
   2431                     return false;
   2432                 }
   2433             } else {
   2434                 if (!RIGID_PARSER) {
   2435                     Log.w(TAG, "Unknown element under " + tag + ": "
   2436                             + parser.getName() + " at " + mArchiveSourcePath + " "
   2437                             + parser.getPositionDescription());
   2438                     XmlUtils.skipCurrentTag(parser);
   2439                     continue;
   2440                 }
   2441                 outError[0] = "Bad element under " + tag + ": "
   2442                     + parser.getName();
   2443                 return false;
   2444             }
   2445         }
   2446         return true;
   2447     }
   2448 
   2449     private Bundle parseMetaData(Resources res,
   2450             XmlPullParser parser, AttributeSet attrs,
   2451             Bundle data, String[] outError)
   2452             throws XmlPullParserException, IOException {
   2453 
   2454         TypedArray sa = res.obtainAttributes(attrs,
   2455                 com.android.internal.R.styleable.AndroidManifestMetaData);
   2456 
   2457         if (data == null) {
   2458             data = new Bundle();
   2459         }
   2460 
   2461         String name = sa.getNonConfigurationString(
   2462                 com.android.internal.R.styleable.AndroidManifestMetaData_name, 0);
   2463         if (name == null) {
   2464             outError[0] = "<meta-data> requires an android:name attribute";
   2465             sa.recycle();
   2466             return null;
   2467         }
   2468 
   2469         name = name.intern();
   2470 
   2471         TypedValue v = sa.peekValue(
   2472                 com.android.internal.R.styleable.AndroidManifestMetaData_resource);
   2473         if (v != null && v.resourceId != 0) {
   2474             //Log.i(TAG, "Meta data ref " + name + ": " + v);
   2475             data.putInt(name, v.resourceId);
   2476         } else {
   2477             v = sa.peekValue(
   2478                     com.android.internal.R.styleable.AndroidManifestMetaData_value);
   2479             //Log.i(TAG, "Meta data " + name + ": " + v);
   2480             if (v != null) {
   2481                 if (v.type == TypedValue.TYPE_STRING) {
   2482                     CharSequence cs = v.coerceToString();
   2483                     data.putString(name, cs != null ? cs.toString().intern() : null);
   2484                 } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
   2485                     data.putBoolean(name, v.data != 0);
   2486                 } else if (v.type >= TypedValue.TYPE_FIRST_INT
   2487                         && v.type <= TypedValue.TYPE_LAST_INT) {
   2488                     data.putInt(name, v.data);
   2489                 } else if (v.type == TypedValue.TYPE_FLOAT) {
   2490                     data.putFloat(name, v.getFloat());
   2491                 } else {
   2492                     if (!RIGID_PARSER) {
   2493                         Log.w(TAG, "<meta-data> only supports string, integer, float, color, boolean, and resource reference types: "
   2494                                 + parser.getName() + " at " + mArchiveSourcePath + " "
   2495                                 + parser.getPositionDescription());
   2496                     } else {
   2497                         outError[0] = "<meta-data> only supports string, integer, float, color, boolean, and resource reference types";
   2498                         data = null;
   2499                     }
   2500                 }
   2501             } else {
   2502                 outError[0] = "<meta-data> requires an android:value or android:resource attribute";
   2503                 data = null;
   2504             }
   2505         }
   2506 
   2507         sa.recycle();
   2508 
   2509         XmlUtils.skipCurrentTag(parser);
   2510 
   2511         return data;
   2512     }
   2513 
   2514     private static final String ANDROID_RESOURCES
   2515             = "http://schemas.android.com/apk/res/android";
   2516 
   2517     private boolean parseIntent(Resources res,
   2518             XmlPullParser parser, AttributeSet attrs, int flags,
   2519             IntentInfo outInfo, String[] outError, boolean isActivity)
   2520             throws XmlPullParserException, IOException {
   2521 
   2522         TypedArray sa = res.obtainAttributes(attrs,
   2523                 com.android.internal.R.styleable.AndroidManifestIntentFilter);
   2524 
   2525         int priority = sa.getInt(
   2526                 com.android.internal.R.styleable.AndroidManifestIntentFilter_priority, 0);
   2527         if (priority > 0 && isActivity && (flags&PARSE_IS_SYSTEM) == 0) {
   2528             Log.w(TAG, "Activity with priority > 0, forcing to 0 at "
   2529                     + mArchiveSourcePath + " "
   2530                     + parser.getPositionDescription());
   2531             priority = 0;
   2532         }
   2533         outInfo.setPriority(priority);
   2534 
   2535         TypedValue v = sa.peekValue(
   2536                 com.android.internal.R.styleable.AndroidManifestIntentFilter_label);
   2537         if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
   2538             outInfo.nonLocalizedLabel = v.coerceToString();
   2539         }
   2540 
   2541         outInfo.icon = sa.getResourceId(
   2542                 com.android.internal.R.styleable.AndroidManifestIntentFilter_icon, 0);
   2543 
   2544         sa.recycle();
   2545 
   2546         int outerDepth = parser.getDepth();
   2547         int type;
   2548         while ((type=parser.next()) != parser.END_DOCUMENT
   2549                && (type != parser.END_TAG || parser.getDepth() > outerDepth)) {
   2550             if (type == parser.END_TAG || type == parser.TEXT) {
   2551                 continue;
   2552             }
   2553 
   2554             String nodeName = parser.getName();
   2555             if (nodeName.equals("action")) {
   2556                 String value = attrs.getAttributeValue(
   2557                         ANDROID_RESOURCES, "name");
   2558                 if (value == null || value == "") {
   2559                     outError[0] = "No value supplied for <android:name>";
   2560                     return false;
   2561                 }
   2562                 XmlUtils.skipCurrentTag(parser);
   2563 
   2564                 outInfo.addAction(value);
   2565             } else if (nodeName.equals("category")) {
   2566                 String value = attrs.getAttributeValue(
   2567                         ANDROID_RESOURCES, "name");
   2568                 if (value == null || value == "") {
   2569                     outError[0] = "No value supplied for <android:name>";
   2570                     return false;
   2571                 }
   2572                 XmlUtils.skipCurrentTag(parser);
   2573 
   2574                 outInfo.addCategory(value);
   2575 
   2576             } else if (nodeName.equals("data")) {
   2577                 sa = res.obtainAttributes(attrs,
   2578                         com.android.internal.R.styleable.AndroidManifestData);
   2579 
   2580                 String str = sa.getNonConfigurationString(
   2581                         com.android.internal.R.styleable.AndroidManifestData_mimeType, 0);
   2582                 if (str != null) {
   2583                     try {
   2584                         outInfo.addDataType(str);
   2585                     } catch (IntentFilter.MalformedMimeTypeException e) {
   2586                         outError[0] = e.toString();
   2587                         sa.recycle();
   2588                         return false;
   2589                     }
   2590                 }
   2591 
   2592                 str = sa.getNonConfigurationString(
   2593                         com.android.internal.R.styleable.AndroidManifestData_scheme, 0);
   2594                 if (str != null) {
   2595                     outInfo.addDataScheme(str);
   2596                 }
   2597 
   2598                 String host = sa.getNonConfigurationString(
   2599                         com.android.internal.R.styleable.AndroidManifestData_host, 0);
   2600                 String port = sa.getNonConfigurationString(
   2601                         com.android.internal.R.styleable.AndroidManifestData_port, 0);
   2602                 if (host != null) {
   2603                     outInfo.addDataAuthority(host, port);
   2604                 }
   2605 
   2606                 str = sa.getNonConfigurationString(
   2607                         com.android.internal.R.styleable.AndroidManifestData_path, 0);
   2608                 if (str != null) {
   2609                     outInfo.addDataPath(str, PatternMatcher.PATTERN_LITERAL);
   2610                 }
   2611 
   2612                 str = sa.getNonConfigurationString(
   2613                         com.android.internal.R.styleable.AndroidManifestData_pathPrefix, 0);
   2614                 if (str != null) {
   2615                     outInfo.addDataPath(str, PatternMatcher.PATTERN_PREFIX);
   2616                 }
   2617 
   2618                 str = sa.getNonConfigurationString(
   2619                         com.android.internal.R.styleable.AndroidManifestData_pathPattern, 0);
   2620                 if (str != null) {
   2621                     outInfo.addDataPath(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
   2622                 }
   2623 
   2624                 sa.recycle();
   2625                 XmlUtils.skipCurrentTag(parser);
   2626             } else if (!RIGID_PARSER) {
   2627                 Log.w(TAG, "Unknown element under <intent-filter>: "
   2628                         + parser.getName() + " at " + mArchiveSourcePath + " "
   2629                         + parser.getPositionDescription());
   2630                 XmlUtils.skipCurrentTag(parser);
   2631             } else {
   2632                 outError[0] = "Bad element under <intent-filter>: " + parser.getName();
   2633                 return false;
   2634             }
   2635         }
   2636 
   2637         outInfo.hasDefault = outInfo.hasCategory(Intent.CATEGORY_DEFAULT);
   2638         if (false) {
   2639             String cats = "";
   2640             Iterator<String> it = outInfo.categoriesIterator();
   2641             while (it != null && it.hasNext()) {
   2642                 cats += " " + it.next();
   2643             }
   2644             System.out.println("Intent d=" +
   2645                     outInfo.hasDefault + ", cat=" + cats);
   2646         }
   2647 
   2648         return true;
   2649     }
   2650 
   2651     public final static class Package {
   2652         public String packageName;
   2653 
   2654         // For now we only support one application per package.
   2655         public final ApplicationInfo applicationInfo = new ApplicationInfo();
   2656 
   2657         public final ArrayList<Permission> permissions = new ArrayList<Permission>(0);
   2658         public final ArrayList<PermissionGroup> permissionGroups = new ArrayList<PermissionGroup>(0);
   2659         public final ArrayList<Activity> activities = new ArrayList<Activity>(0);
   2660         public final ArrayList<Activity> receivers = new ArrayList<Activity>(0);
   2661         public final ArrayList<Provider> providers = new ArrayList<Provider>(0);
   2662         public final ArrayList<Service> services = new ArrayList<Service>(0);
   2663         public final ArrayList<Instrumentation> instrumentation = new ArrayList<Instrumentation>(0);
   2664 
   2665         public final ArrayList<String> requestedPermissions = new ArrayList<String>();
   2666 
   2667         public ArrayList<String> protectedBroadcasts;
   2668 
   2669         public ArrayList<String> usesLibraries = null;
   2670         public ArrayList<String> usesOptionalLibraries = null;
   2671         public String[] usesLibraryFiles = null;
   2672 
   2673         public ArrayList<String> mOriginalPackages = null;
   2674         public String mRealPackage = null;
   2675         public ArrayList<String> mAdoptPermissions = null;
   2676 
   2677         // We store the application meta-data independently to avoid multiple unwanted references
   2678         public Bundle mAppMetaData = null;
   2679 
   2680         // If this is a 3rd party app, this is the path of the zip file.
   2681         public String mPath;
   2682 
   2683         // The version code declared for this package.
   2684         public int mVersionCode;
   2685 
   2686         // The version name declared for this package.
   2687         public String mVersionName;
   2688 
   2689         // The shared user id that this package wants to use.
   2690         public String mSharedUserId;
   2691 
   2692         // The shared user label that this package wants to use.
   2693         public int mSharedUserLabel;
   2694 
   2695         // Signatures that were read from the package.
   2696         public Signature mSignatures[];
   2697 
   2698         // For use by package manager service for quick lookup of
   2699         // preferred up order.
   2700         public int mPreferredOrder = 0;
   2701 
   2702         // For use by the package manager to keep track of the path to the
   2703         // file an app came from.
   2704         public String mScanPath;
   2705 
   2706         // For use by package manager to keep track of where it has done dexopt.
   2707         public boolean mDidDexOpt;
   2708 
   2709         // Additional data supplied by callers.
   2710         public Object mExtras;
   2711 
   2712         /*
   2713          *  Applications hardware preferences
   2714          */
   2715         public final ArrayList<ConfigurationInfo> configPreferences =
   2716                 new ArrayList<ConfigurationInfo>();
   2717 
   2718         /*
   2719          *  Applications requested features
   2720          */
   2721         public ArrayList<FeatureInfo> reqFeatures = null;
   2722 
   2723         public int installLocation;
   2724 
   2725         public Package(String _name) {
   2726             packageName = _name;
   2727             applicationInfo.packageName = _name;
   2728             applicationInfo.uid = -1;
   2729         }
   2730 
   2731         public void setPackageName(String newName) {
   2732             packageName = newName;
   2733             applicationInfo.packageName = newName;
   2734             for (int i=permissions.size()-1; i>=0; i--) {
   2735                 permissions.get(i).setPackageName(newName);
   2736             }
   2737             for (int i=permissionGroups.size()-1; i>=0; i--) {
   2738                 permissionGroups.get(i).setPackageName(newName);
   2739             }
   2740             for (int i=activities.size()-1; i>=0; i--) {
   2741                 activities.get(i).setPackageName(newName);
   2742             }
   2743             for (int i=receivers.size()-1; i>=0; i--) {
   2744                 receivers.get(i).setPackageName(newName);
   2745             }
   2746             for (int i=providers.size()-1; i>=0; i--) {
   2747                 providers.get(i).setPackageName(newName);
   2748             }
   2749             for (int i=services.size()-1; i>=0; i--) {
   2750                 services.get(i).setPackageName(newName);
   2751             }
   2752             for (int i=instrumentation.size()-1; i>=0; i--) {
   2753                 instrumentation.get(i).setPackageName(newName);
   2754             }
   2755         }
   2756 
   2757         public String toString() {
   2758             return "Package{"
   2759                 + Integer.toHexString(System.identityHashCode(this))
   2760                 + " " + packageName + "}";
   2761         }
   2762     }
   2763 
   2764     public static class Component<II extends IntentInfo> {
   2765         public final Package owner;
   2766         public final ArrayList<II> intents;
   2767         public final String className;
   2768         public Bundle metaData;
   2769 
   2770         ComponentName componentName;
   2771         String componentShortName;
   2772 
   2773         public Component(Package _owner) {
   2774             owner = _owner;
   2775             intents = null;
   2776             className = null;
   2777         }
   2778 
   2779         public Component(final ParsePackageItemArgs args, final PackageItemInfo outInfo) {
   2780             owner = args.owner;
   2781             intents = new ArrayList<II>(0);
   2782             String name = args.sa.getNonConfigurationString(args.nameRes, 0);
   2783             if (name == null) {
   2784                 className = null;
   2785                 args.outError[0] = args.tag + " does not specify android:name";
   2786                 return;
   2787             }
   2788 
   2789             outInfo.name
   2790                 = buildClassName(owner.applicationInfo.packageName, name, args.outError);
   2791             if (outInfo.name == null) {
   2792                 className = null;
   2793                 args.outError[0] = args.tag + " does not have valid android:name";
   2794                 return;
   2795             }
   2796 
   2797             className = outInfo.name;
   2798 
   2799             int iconVal = args.sa.getResourceId(args.iconRes, 0);
   2800             if (iconVal != 0) {
   2801                 outInfo.icon = iconVal;
   2802                 outInfo.nonLocalizedLabel = null;
   2803             }
   2804 
   2805             TypedValue v = args.sa.peekValue(args.labelRes);
   2806             if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
   2807                 outInfo.nonLocalizedLabel = v.coerceToString();
   2808             }
   2809 
   2810             outInfo.packageName = owner.packageName;
   2811         }
   2812 
   2813         public Component(final ParseComponentArgs args, final ComponentInfo outInfo) {
   2814             this(args, (PackageItemInfo)outInfo);
   2815             if (args.outError[0] != null) {
   2816                 return;
   2817             }
   2818 
   2819             if (args.processRes != 0) {
   2820                 CharSequence pname;
   2821                 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
   2822                     pname = args.sa.getNonConfigurationString(args.processRes, 0);
   2823                 } else {
   2824                     // Some older apps have been seen to use a resource reference
   2825                     // here that on older builds was ignored (with a warning).  We
   2826                     // need to continue to do this for them so they don't break.
   2827                     pname = args.sa.getNonResourceString(args.processRes);
   2828                 }
   2829                 outInfo.processName = buildProcessName(owner.applicationInfo.packageName,
   2830                         owner.applicationInfo.processName, pname,
   2831                         args.flags, args.sepProcesses, args.outError);
   2832             }
   2833 
   2834             if (args.descriptionRes != 0) {
   2835                 outInfo.descriptionRes = args.sa.getResourceId(args.descriptionRes, 0);
   2836             }
   2837 
   2838             outInfo.enabled = args.sa.getBoolean(args.enabledRes, true);
   2839         }
   2840 
   2841         public Component(Component<II> clone) {
   2842             owner = clone.owner;
   2843             intents = clone.intents;
   2844             className = clone.className;
   2845             componentName = clone.componentName;
   2846             componentShortName = clone.componentShortName;
   2847         }
   2848 
   2849         public ComponentName getComponentName() {
   2850             if (componentName != null) {
   2851                 return componentName;
   2852             }
   2853             if (className != null) {
   2854                 componentName = new ComponentName(owner.applicationInfo.packageName,
   2855                         className);
   2856             }
   2857             return componentName;
   2858         }
   2859 
   2860         public String getComponentShortName() {
   2861             if (componentShortName != null) {
   2862                 return componentShortName;
   2863             }
   2864             ComponentName component = getComponentName();
   2865             if (component != null) {
   2866                 componentShortName = component.flattenToShortString();
   2867             }
   2868             return componentShortName;
   2869         }
   2870 
   2871         public void setPackageName(String packageName) {
   2872             componentName = null;
   2873             componentShortName = null;
   2874         }
   2875     }
   2876 
   2877     public final static class Permission extends Component<IntentInfo> {
   2878         public final PermissionInfo info;
   2879         public boolean tree;
   2880         public PermissionGroup group;
   2881 
   2882         public Permission(Package _owner) {
   2883             super(_owner);
   2884             info = new PermissionInfo();
   2885         }
   2886 
   2887         public Permission(Package _owner, PermissionInfo _info) {
   2888             super(_owner);
   2889             info = _info;
   2890         }
   2891 
   2892         public void setPackageName(String packageName) {
   2893             super.setPackageName(packageName);
   2894             info.packageName = packageName;
   2895         }
   2896 
   2897         public String toString() {
   2898             return "Permission{"
   2899                 + Integer.toHexString(System.identityHashCode(this))
   2900                 + " " + info.name + "}";
   2901         }
   2902     }
   2903 
   2904     public final static class PermissionGroup extends Component<IntentInfo> {
   2905         public final PermissionGroupInfo info;
   2906 
   2907         public PermissionGroup(Package _owner) {
   2908             super(_owner);
   2909             info = new PermissionGroupInfo();
   2910         }
   2911 
   2912         public PermissionGroup(Package _owner, PermissionGroupInfo _info) {
   2913             super(_owner);
   2914             info = _info;
   2915         }
   2916 
   2917         public void setPackageName(String packageName) {
   2918             super.setPackageName(packageName);
   2919             info.packageName = packageName;
   2920         }
   2921 
   2922         public String toString() {
   2923             return "PermissionGroup{"
   2924                 + Integer.toHexString(System.identityHashCode(this))
   2925                 + " " + info.name + "}";
   2926         }
   2927     }
   2928 
   2929     private static boolean copyNeeded(int flags, Package p, Bundle metaData) {
   2930         if ((flags & PackageManager.GET_META_DATA) != 0
   2931                 && (metaData != null || p.mAppMetaData != null)) {
   2932             return true;
   2933         }
   2934         if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0
   2935                 && p.usesLibraryFiles != null) {
   2936             return true;
   2937         }
   2938         return false;
   2939     }
   2940 
   2941     public static ApplicationInfo generateApplicationInfo(Package p, int flags) {
   2942         if (p == null) return null;
   2943         if (!copyNeeded(flags, p, null)) {
   2944             // CompatibilityMode is global state. It's safe to modify the instance
   2945             // of the package.
   2946             if (!sCompatibilityModeEnabled) {
   2947                 p.applicationInfo.disableCompatibilityMode();
   2948             }
   2949             return p.applicationInfo;
   2950         }
   2951 
   2952         // Make shallow copy so we can store the metadata/libraries safely
   2953         ApplicationInfo ai = new ApplicationInfo(p.applicationInfo);
   2954         if ((flags & PackageManager.GET_META_DATA) != 0) {
   2955             ai.metaData = p.mAppMetaData;
   2956         }
   2957         if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0) {
   2958             ai.sharedLibraryFiles = p.usesLibraryFiles;
   2959         }
   2960         if (!sCompatibilityModeEnabled) {
   2961             ai.disableCompatibilityMode();
   2962         }
   2963         return ai;
   2964     }
   2965 
   2966     public static final PermissionInfo generatePermissionInfo(
   2967             Permission p, int flags) {
   2968         if (p == null) return null;
   2969         if ((flags&PackageManager.GET_META_DATA) == 0) {
   2970             return p.info;
   2971         }
   2972         PermissionInfo pi = new PermissionInfo(p.info);
   2973         pi.metaData = p.metaData;
   2974         return pi;
   2975     }
   2976 
   2977     public static final PermissionGroupInfo generatePermissionGroupInfo(
   2978             PermissionGroup pg, int flags) {
   2979         if (pg == null) return null;
   2980         if ((flags&PackageManager.GET_META_DATA) == 0) {
   2981             return pg.info;
   2982         }
   2983         PermissionGroupInfo pgi = new PermissionGroupInfo(pg.info);
   2984         pgi.metaData = pg.metaData;
   2985         return pgi;
   2986     }
   2987 
   2988     public final static class Activity extends Component<ActivityIntentInfo> {
   2989         public final ActivityInfo info;
   2990 
   2991         public Activity(final ParseComponentArgs args, final ActivityInfo _info) {
   2992             super(args, _info);
   2993             info = _info;
   2994             info.applicationInfo = args.owner.applicationInfo;
   2995         }
   2996 
   2997         public void setPackageName(String packageName) {
   2998             super.setPackageName(packageName);
   2999             info.packageName = packageName;
   3000         }
   3001 
   3002         public String toString() {
   3003             return "Activity{"
   3004                 + Integer.toHexString(System.identityHashCode(this))
   3005                 + " " + getComponentShortName() + "}";
   3006         }
   3007     }
   3008 
   3009     public static final ActivityInfo generateActivityInfo(Activity a,
   3010             int flags) {
   3011         if (a == null) return null;
   3012         if (!copyNeeded(flags, a.owner, a.metaData)) {
   3013             return a.info;
   3014         }
   3015         // Make shallow copies so we can store the metadata safely
   3016         ActivityInfo ai = new ActivityInfo(a.info);
   3017         ai.metaData = a.metaData;
   3018         ai.applicationInfo = generateApplicationInfo(a.owner, flags);
   3019         return ai;
   3020     }
   3021 
   3022     public final static class Service extends Component<ServiceIntentInfo> {
   3023         public final ServiceInfo info;
   3024 
   3025         public Service(final ParseComponentArgs args, final ServiceInfo _info) {
   3026             super(args, _info);
   3027             info = _info;
   3028             info.applicationInfo = args.owner.applicationInfo;
   3029         }
   3030 
   3031         public void setPackageName(String packageName) {
   3032             super.setPackageName(packageName);
   3033             info.packageName = packageName;
   3034         }
   3035 
   3036         public String toString() {
   3037             return "Service{"
   3038                 + Integer.toHexString(System.identityHashCode(this))
   3039                 + " " + getComponentShortName() + "}";
   3040         }
   3041     }
   3042 
   3043     public static final ServiceInfo generateServiceInfo(Service s, int flags) {
   3044         if (s == null) return null;
   3045         if (!copyNeeded(flags, s.owner, s.metaData)) {
   3046             return s.info;
   3047         }
   3048         // Make shallow copies so we can store the metadata safely
   3049         ServiceInfo si = new ServiceInfo(s.info);
   3050         si.metaData = s.metaData;
   3051         si.applicationInfo = generateApplicationInfo(s.owner, flags);
   3052         return si;
   3053     }
   3054 
   3055     public final static class Provider extends Component {
   3056         public final ProviderInfo info;
   3057         public boolean syncable;
   3058 
   3059         public Provider(final ParseComponentArgs args, final ProviderInfo _info) {
   3060             super(args, _info);
   3061             info = _info;
   3062             info.applicationInfo = args.owner.applicationInfo;
   3063             syncable = false;
   3064         }
   3065 
   3066         public Provider(Provider existingProvider) {
   3067             super(existingProvider);
   3068             this.info = existingProvider.info;
   3069             this.syncable = existingProvider.syncable;
   3070         }
   3071 
   3072         public void setPackageName(String packageName) {
   3073             super.setPackageName(packageName);
   3074             info.packageName = packageName;
   3075         }
   3076 
   3077         public String toString() {
   3078             return "Provider{"
   3079                 + Integer.toHexString(System.identityHashCode(this))
   3080                 + " " + info.name + "}";
   3081         }
   3082     }
   3083 
   3084     public static final ProviderInfo generateProviderInfo(Provider p,
   3085             int flags) {
   3086         if (p == null) return null;
   3087         if (!copyNeeded(flags, p.owner, p.metaData)
   3088                 && ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) != 0
   3089                         || p.info.uriPermissionPatterns == null)) {
   3090             return p.info;
   3091         }
   3092         // Make shallow copies so we can store the metadata safely
   3093         ProviderInfo pi = new ProviderInfo(p.info);
   3094         pi.metaData = p.metaData;
   3095         if ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) == 0) {
   3096             pi.uriPermissionPatterns = null;
   3097         }
   3098         pi.applicationInfo = generateApplicationInfo(p.owner, flags);
   3099         return pi;
   3100     }
   3101 
   3102     public final static class Instrumentation extends Component {
   3103         public final InstrumentationInfo info;
   3104 
   3105         public Instrumentation(final ParsePackageItemArgs args, final InstrumentationInfo _info) {
   3106             super(args, _info);
   3107             info = _info;
   3108         }
   3109 
   3110         public void setPackageName(String packageName) {
   3111             super.setPackageName(packageName);
   3112             info.packageName = packageName;
   3113         }
   3114 
   3115         public String toString() {
   3116             return "Instrumentation{"
   3117                 + Integer.toHexString(System.identityHashCode(this))
   3118                 + " " + getComponentShortName() + "}";
   3119         }
   3120     }
   3121 
   3122     public static final InstrumentationInfo generateInstrumentationInfo(
   3123             Instrumentation i, int flags) {
   3124         if (i == null) return null;
   3125         if ((flags&PackageManager.GET_META_DATA) == 0) {
   3126             return i.info;
   3127         }
   3128         InstrumentationInfo ii = new InstrumentationInfo(i.info);
   3129         ii.metaData = i.metaData;
   3130         return ii;
   3131     }
   3132 
   3133     public static class IntentInfo extends IntentFilter {
   3134         public boolean hasDefault;
   3135         public int labelRes;
   3136         public CharSequence nonLocalizedLabel;
   3137         public int icon;
   3138     }
   3139 
   3140     public final static class ActivityIntentInfo extends IntentInfo {
   3141         public final Activity activity;
   3142 
   3143         public ActivityIntentInfo(Activity _activity) {
   3144             activity = _activity;
   3145         }
   3146 
   3147         public String toString() {
   3148             return "ActivityIntentInfo{"
   3149                 + Integer.toHexString(System.identityHashCode(this))
   3150                 + " " + activity.info.name + "}";
   3151         }
   3152     }
   3153 
   3154     public final static class ServiceIntentInfo extends IntentInfo {
   3155         public final Service service;
   3156 
   3157         public ServiceIntentInfo(Service _service) {
   3158             service = _service;
   3159         }
   3160 
   3161         public String toString() {
   3162             return "ServiceIntentInfo{"
   3163                 + Integer.toHexString(System.identityHashCode(this))
   3164                 + " " + service.info.name + "}";
   3165         }
   3166     }
   3167 
   3168     /**
   3169      * @hide
   3170      */
   3171     public static void setCompatibilityModeEnabled(boolean compatibilityModeEnabled) {
   3172         sCompatibilityModeEnabled = compatibilityModeEnabled;
   3173     }
   3174 }
   3175