Home | History | Annotate | Download | only in pm
      1 /*
      2  * Copyright (C) 2011 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 com.android.server.pm;
     18 
     19 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
     20 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
     21 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
     22 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER;
     23 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
     24 import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
     25 import static android.os.Process.SYSTEM_UID;
     26 import static android.os.Process.PACKAGE_INFO_GID;
     27 
     28 import android.content.IntentFilter;
     29 import android.content.pm.ActivityInfo;
     30 import android.content.pm.ResolveInfo;
     31 import android.net.Uri;
     32 import android.os.PatternMatcher;
     33 import android.util.LogPrinter;
     34 import com.android.internal.util.FastXmlSerializer;
     35 import com.android.internal.util.JournaledFile;
     36 import com.android.internal.util.XmlUtils;
     37 import com.android.server.pm.PackageManagerService.DumpState;
     38 
     39 import org.xmlpull.v1.XmlPullParser;
     40 import org.xmlpull.v1.XmlPullParserException;
     41 import org.xmlpull.v1.XmlSerializer;
     42 
     43 import android.content.ComponentName;
     44 import android.content.Context;
     45 import android.content.Intent;
     46 import android.content.pm.ApplicationInfo;
     47 import android.content.pm.ComponentInfo;
     48 import android.content.pm.KeySet;
     49 import android.content.pm.PackageCleanItem;
     50 import android.content.pm.PackageManager;
     51 import android.content.pm.PackageParser;
     52 import android.content.pm.PermissionInfo;
     53 import android.content.pm.Signature;
     54 import android.content.pm.UserInfo;
     55 import android.content.pm.PackageUserState;
     56 import android.content.pm.VerifierDeviceIdentity;
     57 import android.os.Binder;
     58 import android.os.Environment;
     59 import android.os.FileUtils;
     60 import android.os.Process;
     61 import android.os.UserHandle;
     62 import android.util.Log;
     63 import android.util.LongSparseArray;
     64 import android.util.Slog;
     65 import android.util.SparseArray;
     66 import android.util.Xml;
     67 
     68 import java.io.BufferedOutputStream;
     69 import java.io.File;
     70 import java.io.FileInputStream;
     71 import java.io.FileOutputStream;
     72 import java.io.IOException;
     73 import java.io.PrintWriter;
     74 import java.security.PublicKey;
     75 import java.text.SimpleDateFormat;
     76 import java.util.ArrayList;
     77 import java.util.Arrays;
     78 import java.util.Date;
     79 import java.util.HashMap;
     80 import java.util.HashSet;
     81 import java.util.Iterator;
     82 import java.util.List;
     83 import java.util.Map;
     84 import java.util.Set;
     85 import java.util.Map.Entry;
     86 
     87 import libcore.io.IoUtils;
     88 
     89 /**
     90  * Holds information about dynamic settings.
     91  */
     92 final class Settings {
     93     private static final String TAG = "PackageSettings";
     94 
     95     private static final boolean DEBUG_STOPPED = false;
     96     private static final boolean DEBUG_MU = false;
     97 
     98     private static final String TAG_READ_EXTERNAL_STORAGE = "read-external-storage";
     99     private static final String ATTR_ENFORCEMENT = "enforcement";
    100 
    101     private static final String TAG_ITEM = "item";
    102     private static final String TAG_DISABLED_COMPONENTS = "disabled-components";
    103     private static final String TAG_ENABLED_COMPONENTS = "enabled-components";
    104     private static final String TAG_PACKAGE_RESTRICTIONS = "package-restrictions";
    105     private static final String TAG_PACKAGE = "pkg";
    106 
    107     private static final String ATTR_NAME = "name";
    108     private static final String ATTR_USER = "user";
    109     private static final String ATTR_CODE = "code";
    110     private static final String ATTR_NOT_LAUNCHED = "nl";
    111     private static final String ATTR_ENABLED = "enabled";
    112     private static final String ATTR_ENABLED_CALLER = "enabledCaller";
    113     private static final String ATTR_STOPPED = "stopped";
    114     private static final String ATTR_BLOCKED = "blocked";
    115     private static final String ATTR_INSTALLED = "inst";
    116 
    117     private final File mSettingsFilename;
    118     private final File mBackupSettingsFilename;
    119     private final File mPackageListFilename;
    120     private final File mStoppedPackagesFilename;
    121     private final File mBackupStoppedPackagesFilename;
    122 
    123     final HashMap<String, PackageSetting> mPackages =
    124             new HashMap<String, PackageSetting>();
    125     // List of replaced system applications
    126     private final HashMap<String, PackageSetting> mDisabledSysPackages =
    127         new HashMap<String, PackageSetting>();
    128 
    129     private static int mFirstAvailableUid = 0;
    130 
    131     // These are the last platform API version we were using for
    132     // the apps installed on internal and external storage.  It is
    133     // used to grant newer permissions one time during a system upgrade.
    134     int mInternalSdkPlatform;
    135     int mExternalSdkPlatform;
    136 
    137     Boolean mReadExternalStorageEnforced;
    138 
    139     /** Device identity for the purpose of package verification. */
    140     private VerifierDeviceIdentity mVerifierDeviceIdentity;
    141 
    142     // The user's preferred activities associated with particular intent
    143     // filters.
    144     final SparseArray<PreferredIntentResolver> mPreferredActivities =
    145             new SparseArray<PreferredIntentResolver>();
    146 
    147     final HashMap<String, SharedUserSetting> mSharedUsers =
    148             new HashMap<String, SharedUserSetting>();
    149     private final ArrayList<Object> mUserIds = new ArrayList<Object>();
    150     private final SparseArray<Object> mOtherUserIds =
    151             new SparseArray<Object>();
    152 
    153     // For reading/writing settings file.
    154     private final ArrayList<Signature> mPastSignatures =
    155             new ArrayList<Signature>();
    156 
    157     // Mapping from permission names to info about them.
    158     final HashMap<String, BasePermission> mPermissions =
    159             new HashMap<String, BasePermission>();
    160 
    161     // Mapping from permission tree names to info about them.
    162     final HashMap<String, BasePermission> mPermissionTrees =
    163             new HashMap<String, BasePermission>();
    164 
    165     // Packages that have been uninstalled and still need their external
    166     // storage data deleted.
    167     final ArrayList<PackageCleanItem> mPackagesToBeCleaned = new ArrayList<PackageCleanItem>();
    168 
    169     // Packages that have been renamed since they were first installed.
    170     // Keys are the new names of the packages, values are the original
    171     // names.  The packages appear everwhere else under their original
    172     // names.
    173     final HashMap<String, String> mRenamedPackages = new HashMap<String, String>();
    174 
    175     final StringBuilder mReadMessages = new StringBuilder();
    176 
    177     /**
    178      * Used to track packages that have a shared user ID that hasn't been read
    179      * in yet.
    180      * <p>
    181      * TODO: make this just a local variable that is passed in during package
    182      * scanning to make it less confusing.
    183      */
    184     private final ArrayList<PendingPackage> mPendingPackages = new ArrayList<PendingPackage>();
    185 
    186     private final Context mContext;
    187 
    188     private final File mSystemDir;
    189 
    190     public final KeySetManager mKeySetManager = new KeySetManager(mPackages);
    191 
    192     Settings(Context context) {
    193         this(context, Environment.getDataDirectory());
    194     }
    195 
    196     Settings(Context context, File dataDir) {
    197         mContext = context;
    198         mSystemDir = new File(dataDir, "system");
    199         mSystemDir.mkdirs();
    200         FileUtils.setPermissions(mSystemDir.toString(),
    201                 FileUtils.S_IRWXU|FileUtils.S_IRWXG
    202                 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
    203                 -1, -1);
    204         mSettingsFilename = new File(mSystemDir, "packages.xml");
    205         mBackupSettingsFilename = new File(mSystemDir, "packages-backup.xml");
    206         mPackageListFilename = new File(mSystemDir, "packages.list");
    207         FileUtils.setPermissions(mPackageListFilename, 0660, SYSTEM_UID, PACKAGE_INFO_GID);
    208 
    209         // Deprecated: Needed for migration
    210         mStoppedPackagesFilename = new File(mSystemDir, "packages-stopped.xml");
    211         mBackupStoppedPackagesFilename = new File(mSystemDir, "packages-stopped-backup.xml");
    212     }
    213 
    214     PackageSetting getPackageLPw(PackageParser.Package pkg, PackageSetting origPackage,
    215             String realName, SharedUserSetting sharedUser, File codePath, File resourcePath,
    216             String nativeLibraryPathString, int pkgFlags, UserHandle user, boolean add) {
    217         final String name = pkg.packageName;
    218         PackageSetting p = getPackageLPw(name, origPackage, realName, sharedUser, codePath,
    219                 resourcePath, nativeLibraryPathString, pkg.mVersionCode, pkgFlags,
    220                 user, add, true /* allowInstall */);
    221         return p;
    222     }
    223 
    224     PackageSetting peekPackageLPr(String name) {
    225         return mPackages.get(name);
    226     }
    227 
    228     void setInstallStatus(String pkgName, int status) {
    229         PackageSetting p = mPackages.get(pkgName);
    230         if(p != null) {
    231             if(p.getInstallStatus() != status) {
    232                 p.setInstallStatus(status);
    233             }
    234         }
    235     }
    236 
    237     void setInstallerPackageName(String pkgName,
    238             String installerPkgName) {
    239         PackageSetting p = mPackages.get(pkgName);
    240         if(p != null) {
    241             p.setInstallerPackageName(installerPkgName);
    242         }
    243     }
    244 
    245     SharedUserSetting getSharedUserLPw(String name,
    246             int pkgFlags, boolean create) {
    247         SharedUserSetting s = mSharedUsers.get(name);
    248         if (s == null) {
    249             if (!create) {
    250                 return null;
    251             }
    252             s = new SharedUserSetting(name, pkgFlags);
    253             s.userId = newUserIdLPw(s);
    254             Log.i(PackageManagerService.TAG, "New shared user " + name + ": id=" + s.userId);
    255             // < 0 means we couldn't assign a userid; fall out and return
    256             // s, which is currently null
    257             if (s.userId >= 0) {
    258                 mSharedUsers.put(name, s);
    259             }
    260         }
    261 
    262         return s;
    263     }
    264 
    265     boolean disableSystemPackageLPw(String name) {
    266         final PackageSetting p = mPackages.get(name);
    267         if(p == null) {
    268             Log.w(PackageManagerService.TAG, "Package:"+name+" is not an installed package");
    269             return false;
    270         }
    271         final PackageSetting dp = mDisabledSysPackages.get(name);
    272         // always make sure the system package code and resource paths dont change
    273         if (dp == null) {
    274             if((p.pkg != null) && (p.pkg.applicationInfo != null)) {
    275                 p.pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
    276             }
    277             mDisabledSysPackages.put(name, p);
    278 
    279             // a little trick...  when we install the new package, we don't
    280             // want to modify the existing PackageSetting for the built-in
    281             // version.  so at this point we need a new PackageSetting that
    282             // is okay to muck with.
    283             PackageSetting newp = new PackageSetting(p);
    284             replacePackageLPw(name, newp);
    285             return true;
    286         }
    287         return false;
    288     }
    289 
    290     PackageSetting enableSystemPackageLPw(String name) {
    291         PackageSetting p = mDisabledSysPackages.get(name);
    292         if(p == null) {
    293             Log.w(PackageManagerService.TAG, "Package:"+name+" is not disabled");
    294             return null;
    295         }
    296         // Reset flag in ApplicationInfo object
    297         if((p.pkg != null) && (p.pkg.applicationInfo != null)) {
    298             p.pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
    299         }
    300         PackageSetting ret = addPackageLPw(name, p.realName, p.codePath, p.resourcePath,
    301                 p.nativeLibraryPathString, p.appId, p.versionCode, p.pkgFlags);
    302         mDisabledSysPackages.remove(name);
    303         return ret;
    304     }
    305 
    306     boolean isDisabledSystemPackageLPr(String name) {
    307         return mDisabledSysPackages.containsKey(name);
    308     }
    309 
    310     void removeDisabledSystemPackageLPw(String name) {
    311         mDisabledSysPackages.remove(name);
    312     }
    313 
    314     PackageSetting addPackageLPw(String name, String realName, File codePath, File resourcePath,
    315             String nativeLibraryPathString, int uid, int vc, int pkgFlags) {
    316         PackageSetting p = mPackages.get(name);
    317         if (p != null) {
    318             if (p.appId == uid) {
    319                 return p;
    320             }
    321             PackageManagerService.reportSettingsProblem(Log.ERROR,
    322                     "Adding duplicate package, keeping first: " + name);
    323             return null;
    324         }
    325         p = new PackageSetting(name, realName, codePath, resourcePath, nativeLibraryPathString,
    326                 vc, pkgFlags);
    327         p.appId = uid;
    328         if (addUserIdLPw(uid, p, name)) {
    329             mPackages.put(name, p);
    330             return p;
    331         }
    332         return null;
    333     }
    334 
    335     SharedUserSetting addSharedUserLPw(String name, int uid, int pkgFlags) {
    336         SharedUserSetting s = mSharedUsers.get(name);
    337         if (s != null) {
    338             if (s.userId == uid) {
    339                 return s;
    340             }
    341             PackageManagerService.reportSettingsProblem(Log.ERROR,
    342                     "Adding duplicate shared user, keeping first: " + name);
    343             return null;
    344         }
    345         s = new SharedUserSetting(name, pkgFlags);
    346         s.userId = uid;
    347         if (addUserIdLPw(uid, s, name)) {
    348             mSharedUsers.put(name, s);
    349             return s;
    350         }
    351         return null;
    352     }
    353 
    354     void pruneSharedUsersLPw() {
    355         ArrayList<String> removeStage = new ArrayList<String>();
    356         for (Map.Entry<String,SharedUserSetting> entry : mSharedUsers.entrySet()) {
    357             final SharedUserSetting sus = entry.getValue();
    358             if (sus == null || sus.packages.size() == 0) {
    359                 removeStage.add(entry.getKey());
    360             }
    361         }
    362         for (int i = 0; i < removeStage.size(); i++) {
    363             mSharedUsers.remove(removeStage.get(i));
    364         }
    365     }
    366 
    367     // Transfer ownership of permissions from one package to another.
    368     void transferPermissionsLPw(String origPkg, String newPkg) {
    369         // Transfer ownership of permissions to the new package.
    370         for (int i=0; i<2; i++) {
    371             HashMap<String, BasePermission> permissions =
    372                     i == 0 ? mPermissionTrees : mPermissions;
    373             for (BasePermission bp : permissions.values()) {
    374                 if (origPkg.equals(bp.sourcePackage)) {
    375                     if (PackageManagerService.DEBUG_UPGRADE) Log.v(PackageManagerService.TAG,
    376                             "Moving permission " + bp.name
    377                             + " from pkg " + bp.sourcePackage
    378                             + " to " + newPkg);
    379                     bp.sourcePackage = newPkg;
    380                     bp.packageSetting = null;
    381                     bp.perm = null;
    382                     if (bp.pendingInfo != null) {
    383                         bp.pendingInfo.packageName = newPkg;
    384                     }
    385                     bp.uid = 0;
    386                     bp.gids = null;
    387                 }
    388             }
    389         }
    390     }
    391 
    392     private PackageSetting getPackageLPw(String name, PackageSetting origPackage,
    393             String realName, SharedUserSetting sharedUser, File codePath, File resourcePath,
    394             String nativeLibraryPathString, int vc, int pkgFlags,
    395             UserHandle installUser, boolean add, boolean allowInstall) {
    396         PackageSetting p = mPackages.get(name);
    397         if (p != null) {
    398             if (!p.codePath.equals(codePath)) {
    399                 // Check to see if its a disabled system app
    400                 if ((p.pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0) {
    401                     // This is an updated system app with versions in both system
    402                     // and data partition. Just let the most recent version
    403                     // take precedence.
    404                     Slog.w(PackageManagerService.TAG, "Trying to update system app code path from "
    405                             + p.codePathString + " to " + codePath.toString());
    406                 } else {
    407                     // Just a change in the code path is not an issue, but
    408                     // let's log a message about it.
    409                     Slog.i(PackageManagerService.TAG, "Package " + name + " codePath changed from "
    410                             + p.codePath + " to " + codePath + "; Retaining data and using new");
    411                     /*
    412                      * Since we've changed paths, we need to prefer the new
    413                      * native library path over the one stored in the
    414                      * package settings since we might have moved from
    415                      * internal to external storage or vice versa.
    416                      */
    417                     p.nativeLibraryPathString = nativeLibraryPathString;
    418                 }
    419             }
    420             if (p.sharedUser != sharedUser) {
    421                 PackageManagerService.reportSettingsProblem(Log.WARN,
    422                         "Package " + name + " shared user changed from "
    423                         + (p.sharedUser != null ? p.sharedUser.name : "<nothing>")
    424                         + " to "
    425                         + (sharedUser != null ? sharedUser.name : "<nothing>")
    426                         + "; replacing with new");
    427                 p = null;
    428             } else {
    429                 // If what we are scanning is a system (and possibly privileged) package,
    430                 // then make it so, regardless of whether it was previously installed only
    431                 // in the data partition.
    432                 final int sysPrivFlags = pkgFlags
    433                         & (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_PRIVILEGED);
    434                 p.pkgFlags |= sysPrivFlags;
    435             }
    436         }
    437         if (p == null) {
    438             if (origPackage != null) {
    439                 // We are consuming the data from an existing package.
    440                 p = new PackageSetting(origPackage.name, name, codePath, resourcePath,
    441                         nativeLibraryPathString, vc, pkgFlags);
    442                 if (PackageManagerService.DEBUG_UPGRADE) Log.v(PackageManagerService.TAG, "Package "
    443                         + name + " is adopting original package " + origPackage.name);
    444                 // Note that we will retain the new package's signature so
    445                 // that we can keep its data.
    446                 PackageSignatures s = p.signatures;
    447                 p.copyFrom(origPackage);
    448                 p.signatures = s;
    449                 p.sharedUser = origPackage.sharedUser;
    450                 p.appId = origPackage.appId;
    451                 p.origPackage = origPackage;
    452                 mRenamedPackages.put(name, origPackage.name);
    453                 name = origPackage.name;
    454                 // Update new package state.
    455                 p.setTimeStamp(codePath.lastModified());
    456             } else {
    457                 p = new PackageSetting(name, realName, codePath, resourcePath,
    458                         nativeLibraryPathString, vc, pkgFlags);
    459                 p.setTimeStamp(codePath.lastModified());
    460                 p.sharedUser = sharedUser;
    461                 // If this is not a system app, it starts out stopped.
    462                 if ((pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) {
    463                     if (DEBUG_STOPPED) {
    464                         RuntimeException e = new RuntimeException("here");
    465                         e.fillInStackTrace();
    466                         Slog.i(PackageManagerService.TAG, "Stopping package " + name, e);
    467                     }
    468                     List<UserInfo> users = getAllUsers();
    469                     if (users != null && allowInstall) {
    470                         for (UserInfo user : users) {
    471                             // By default we consider this app to be installed
    472                             // for the user if no user has been specified (which
    473                             // means to leave it at its original value, and the
    474                             // original default value is true), or we are being
    475                             // asked to install for all users, or this is the
    476                             // user we are installing for.
    477                             final boolean installed = installUser == null
    478                                     || installUser.getIdentifier() == UserHandle.USER_ALL
    479                                     || installUser.getIdentifier() == user.id;
    480                             p.setUserState(user.id, COMPONENT_ENABLED_STATE_DEFAULT,
    481                                     installed,
    482                                     true, // stopped,
    483                                     true, // notLaunched
    484                                     false, // blocked
    485                                     null, null, null);
    486                             writePackageRestrictionsLPr(user.id);
    487                         }
    488                     }
    489                 }
    490                 if (sharedUser != null) {
    491                     p.appId = sharedUser.userId;
    492                 } else {
    493                     // Clone the setting here for disabled system packages
    494                     PackageSetting dis = mDisabledSysPackages.get(name);
    495                     if (dis != null) {
    496                         // For disabled packages a new setting is created
    497                         // from the existing user id. This still has to be
    498                         // added to list of user id's
    499                         // Copy signatures from previous setting
    500                         if (dis.signatures.mSignatures != null) {
    501                             p.signatures.mSignatures = dis.signatures.mSignatures.clone();
    502                         }
    503                         p.appId = dis.appId;
    504                         // Clone permissions
    505                         p.grantedPermissions = new HashSet<String>(dis.grantedPermissions);
    506                         // Clone component info
    507                         List<UserInfo> users = getAllUsers();
    508                         if (users != null) {
    509                             for (UserInfo user : users) {
    510                                 int userId = user.id;
    511                                 p.setDisabledComponentsCopy(
    512                                         dis.getDisabledComponents(userId), userId);
    513                                 p.setEnabledComponentsCopy(
    514                                         dis.getEnabledComponents(userId), userId);
    515                             }
    516                         }
    517                         // Add new setting to list of user ids
    518                         addUserIdLPw(p.appId, p, name);
    519                     } else {
    520                         // Assign new user id
    521                         p.appId = newUserIdLPw(p);
    522                     }
    523                 }
    524             }
    525             if (p.appId < 0) {
    526                 PackageManagerService.reportSettingsProblem(Log.WARN,
    527                         "Package " + name + " could not be assigned a valid uid");
    528                 return null;
    529             }
    530             if (add) {
    531                 // Finish adding new package by adding it and updating shared
    532                 // user preferences
    533                 addPackageSettingLPw(p, name, sharedUser);
    534             }
    535         } else {
    536             if (installUser != null && allowInstall) {
    537                 // The caller has explicitly specified the user they want this
    538                 // package installed for, and the package already exists.
    539                 // Make sure it conforms to the new request.
    540                 List<UserInfo> users = getAllUsers();
    541                 if (users != null) {
    542                     for (UserInfo user : users) {
    543                         if (installUser.getIdentifier() == UserHandle.USER_ALL
    544                                 || installUser.getIdentifier() == user.id) {
    545                             boolean installed = p.getInstalled(user.id);
    546                             if (!installed) {
    547                                 p.setInstalled(true, user.id);
    548                                 writePackageRestrictionsLPr(user.id);
    549                             }
    550                         }
    551                     }
    552                 }
    553             }
    554         }
    555         return p;
    556     }
    557 
    558     void insertPackageSettingLPw(PackageSetting p, PackageParser.Package pkg) {
    559         p.pkg = pkg;
    560         // pkg.mSetEnabled = p.getEnabled(userId);
    561         // pkg.mSetStopped = p.getStopped(userId);
    562         final String codePath = pkg.applicationInfo.sourceDir;
    563         final String resourcePath = pkg.applicationInfo.publicSourceDir;
    564         // Update code path if needed
    565         if (!codePath.equalsIgnoreCase(p.codePathString)) {
    566             Slog.w(PackageManagerService.TAG, "Code path for pkg : " + p.pkg.packageName +
    567                     " changing from " + p.codePathString + " to " + codePath);
    568             p.codePath = new File(codePath);
    569             p.codePathString = codePath;
    570         }
    571         //Update resource path if needed
    572         if (!resourcePath.equalsIgnoreCase(p.resourcePathString)) {
    573             Slog.w(PackageManagerService.TAG, "Resource path for pkg : " + p.pkg.packageName +
    574                     " changing from " + p.resourcePathString + " to " + resourcePath);
    575             p.resourcePath = new File(resourcePath);
    576             p.resourcePathString = resourcePath;
    577         }
    578         // Update the native library path if needed
    579         final String nativeLibraryPath = pkg.applicationInfo.nativeLibraryDir;
    580         if (nativeLibraryPath != null
    581                 && !nativeLibraryPath.equalsIgnoreCase(p.nativeLibraryPathString)) {
    582             p.nativeLibraryPathString = nativeLibraryPath;
    583         }
    584         // Update version code if needed
    585         if (pkg.mVersionCode != p.versionCode) {
    586             p.versionCode = pkg.mVersionCode;
    587         }
    588         // Update signatures if needed.
    589         if (p.signatures.mSignatures == null) {
    590             p.signatures.assignSignatures(pkg.mSignatures);
    591         }
    592         // Update flags if needed.
    593         if (pkg.applicationInfo.flags != p.pkgFlags) {
    594             p.pkgFlags = pkg.applicationInfo.flags;
    595         }
    596         // If this app defines a shared user id initialize
    597         // the shared user signatures as well.
    598         if (p.sharedUser != null && p.sharedUser.signatures.mSignatures == null) {
    599             p.sharedUser.signatures.assignSignatures(pkg.mSignatures);
    600         }
    601         addPackageSettingLPw(p, pkg.packageName, p.sharedUser);
    602     }
    603 
    604     // Utility method that adds a PackageSetting to mPackages and
    605     // completes updating the shared user attributes
    606     private void addPackageSettingLPw(PackageSetting p, String name,
    607             SharedUserSetting sharedUser) {
    608         mPackages.put(name, p);
    609         if (sharedUser != null) {
    610             if (p.sharedUser != null && p.sharedUser != sharedUser) {
    611                 PackageManagerService.reportSettingsProblem(Log.ERROR,
    612                         "Package " + p.name + " was user "
    613                         + p.sharedUser + " but is now " + sharedUser
    614                         + "; I am not changing its files so it will probably fail!");
    615                 p.sharedUser.removePackage(p);
    616             } else if (p.appId != sharedUser.userId) {
    617                 PackageManagerService.reportSettingsProblem(Log.ERROR,
    618                     "Package " + p.name + " was user id " + p.appId
    619                     + " but is now user " + sharedUser
    620                     + " with id " + sharedUser.userId
    621                     + "; I am not changing its files so it will probably fail!");
    622             }
    623 
    624             sharedUser.addPackage(p);
    625             p.sharedUser = sharedUser;
    626             p.appId = sharedUser.userId;
    627         }
    628     }
    629 
    630     /*
    631      * Update the shared user setting when a package using
    632      * specifying the shared user id is removed. The gids
    633      * associated with each permission of the deleted package
    634      * are removed from the shared user's gid list only if its
    635      * not in use by other permissions of packages in the
    636      * shared user setting.
    637      */
    638     void updateSharedUserPermsLPw(PackageSetting deletedPs, int[] globalGids) {
    639         if ((deletedPs == null) || (deletedPs.pkg == null)) {
    640             Slog.i(PackageManagerService.TAG,
    641                     "Trying to update info for null package. Just ignoring");
    642             return;
    643         }
    644         // No sharedUserId
    645         if (deletedPs.sharedUser == null) {
    646             return;
    647         }
    648         SharedUserSetting sus = deletedPs.sharedUser;
    649         // Update permissions
    650         for (String eachPerm : deletedPs.pkg.requestedPermissions) {
    651             boolean used = false;
    652             if (!sus.grantedPermissions.contains(eachPerm)) {
    653                 continue;
    654             }
    655             for (PackageSetting pkg:sus.packages) {
    656                 if (pkg.pkg != null &&
    657                         !pkg.pkg.packageName.equals(deletedPs.pkg.packageName) &&
    658                         pkg.pkg.requestedPermissions.contains(eachPerm)) {
    659                     used = true;
    660                     break;
    661                 }
    662             }
    663             if (!used) {
    664                 // can safely delete this permission from list
    665                 sus.grantedPermissions.remove(eachPerm);
    666             }
    667         }
    668         // Update gids
    669         int newGids[] = globalGids;
    670         for (String eachPerm : sus.grantedPermissions) {
    671             BasePermission bp = mPermissions.get(eachPerm);
    672             if (bp != null) {
    673                 newGids = PackageManagerService.appendInts(newGids, bp.gids);
    674             }
    675         }
    676         sus.gids = newGids;
    677     }
    678 
    679     int removePackageLPw(String name) {
    680         final PackageSetting p = mPackages.get(name);
    681         if (p != null) {
    682             mPackages.remove(name);
    683             if (p.sharedUser != null) {
    684                 p.sharedUser.removePackage(p);
    685                 if (p.sharedUser.packages.size() == 0) {
    686                     mSharedUsers.remove(p.sharedUser.name);
    687                     removeUserIdLPw(p.sharedUser.userId);
    688                     return p.sharedUser.userId;
    689                 }
    690             } else {
    691                 removeUserIdLPw(p.appId);
    692                 return p.appId;
    693             }
    694         }
    695         return -1;
    696     }
    697 
    698     private void replacePackageLPw(String name, PackageSetting newp) {
    699         final PackageSetting p = mPackages.get(name);
    700         if (p != null) {
    701             if (p.sharedUser != null) {
    702                 p.sharedUser.removePackage(p);
    703                 p.sharedUser.addPackage(newp);
    704             } else {
    705                 replaceUserIdLPw(p.appId, newp);
    706             }
    707         }
    708         mPackages.put(name, newp);
    709     }
    710 
    711     private boolean addUserIdLPw(int uid, Object obj, Object name) {
    712         if (uid > Process.LAST_APPLICATION_UID) {
    713             return false;
    714         }
    715 
    716         if (uid >= Process.FIRST_APPLICATION_UID) {
    717             int N = mUserIds.size();
    718             final int index = uid - Process.FIRST_APPLICATION_UID;
    719             while (index >= N) {
    720                 mUserIds.add(null);
    721                 N++;
    722             }
    723             if (mUserIds.get(index) != null) {
    724                 PackageManagerService.reportSettingsProblem(Log.ERROR,
    725                         "Adding duplicate user id: " + uid
    726                         + " name=" + name);
    727                 return false;
    728             }
    729             mUserIds.set(index, obj);
    730         } else {
    731             if (mOtherUserIds.get(uid) != null) {
    732                 PackageManagerService.reportSettingsProblem(Log.ERROR,
    733                         "Adding duplicate shared id: " + uid
    734                         + " name=" + name);
    735                 return false;
    736             }
    737             mOtherUserIds.put(uid, obj);
    738         }
    739         return true;
    740     }
    741 
    742     public Object getUserIdLPr(int uid) {
    743         if (uid >= Process.FIRST_APPLICATION_UID) {
    744             final int N = mUserIds.size();
    745             final int index = uid - Process.FIRST_APPLICATION_UID;
    746             return index < N ? mUserIds.get(index) : null;
    747         } else {
    748             return mOtherUserIds.get(uid);
    749         }
    750     }
    751 
    752     private void removeUserIdLPw(int uid) {
    753         if (uid >= Process.FIRST_APPLICATION_UID) {
    754             final int N = mUserIds.size();
    755             final int index = uid - Process.FIRST_APPLICATION_UID;
    756             if (index < N) mUserIds.set(index, null);
    757         } else {
    758             mOtherUserIds.remove(uid);
    759         }
    760         setFirstAvailableUid(uid+1);
    761     }
    762 
    763     private void replaceUserIdLPw(int uid, Object obj) {
    764         if (uid >= Process.FIRST_APPLICATION_UID) {
    765             final int N = mUserIds.size();
    766             final int index = uid - Process.FIRST_APPLICATION_UID;
    767             if (index < N) mUserIds.set(index, obj);
    768         } else {
    769             mOtherUserIds.put(uid, obj);
    770         }
    771     }
    772 
    773     PreferredIntentResolver editPreferredActivitiesLPw(int userId) {
    774         PreferredIntentResolver pir = mPreferredActivities.get(userId);
    775         if (pir == null) {
    776             pir = new PreferredIntentResolver();
    777             mPreferredActivities.put(userId, pir);
    778         }
    779         return pir;
    780     }
    781 
    782     private File getUserPackagesStateFile(int userId) {
    783         return new File(Environment.getUserSystemDirectory(userId), "package-restrictions.xml");
    784     }
    785 
    786     private File getUserPackagesStateBackupFile(int userId) {
    787         return new File(Environment.getUserSystemDirectory(userId),
    788                 "package-restrictions-backup.xml");
    789     }
    790 
    791     void writeAllUsersPackageRestrictionsLPr() {
    792         List<UserInfo> users = getAllUsers();
    793         if (users == null) return;
    794 
    795         for (UserInfo user : users) {
    796             writePackageRestrictionsLPr(user.id);
    797         }
    798     }
    799 
    800     void readAllUsersPackageRestrictionsLPr() {
    801         List<UserInfo> users = getAllUsers();
    802         if (users == null) {
    803             readPackageRestrictionsLPr(0);
    804             return;
    805         }
    806 
    807         for (UserInfo user : users) {
    808             readPackageRestrictionsLPr(user.id);
    809         }
    810     }
    811 
    812     private void readPreferredActivitiesLPw(XmlPullParser parser, int userId)
    813             throws XmlPullParserException, IOException {
    814         int outerDepth = parser.getDepth();
    815         int type;
    816         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
    817                 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
    818             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
    819                 continue;
    820             }
    821 
    822             String tagName = parser.getName();
    823             if (tagName.equals(TAG_ITEM)) {
    824                 PreferredActivity pa = new PreferredActivity(parser);
    825                 if (pa.mPref.getParseError() == null) {
    826                     editPreferredActivitiesLPw(userId).addFilter(pa);
    827                 } else {
    828                     PackageManagerService.reportSettingsProblem(Log.WARN,
    829                             "Error in package manager settings: <preferred-activity> "
    830                                     + pa.mPref.getParseError() + " at "
    831                                     + parser.getPositionDescription());
    832                 }
    833             } else {
    834                 PackageManagerService.reportSettingsProblem(Log.WARN,
    835                         "Unknown element under <preferred-activities>: " + parser.getName());
    836                 XmlUtils.skipCurrentTag(parser);
    837             }
    838         }
    839     }
    840 
    841     void readPackageRestrictionsLPr(int userId) {
    842         if (DEBUG_MU) {
    843             Log.i(TAG, "Reading package restrictions for user=" + userId);
    844         }
    845         FileInputStream str = null;
    846         File userPackagesStateFile = getUserPackagesStateFile(userId);
    847         File backupFile = getUserPackagesStateBackupFile(userId);
    848         if (backupFile.exists()) {
    849             try {
    850                 str = new FileInputStream(backupFile);
    851                 mReadMessages.append("Reading from backup stopped packages file\n");
    852                 PackageManagerService.reportSettingsProblem(Log.INFO,
    853                         "Need to read from backup stopped packages file");
    854                 if (userPackagesStateFile.exists()) {
    855                     // If both the backup and normal file exist, we
    856                     // ignore the normal one since it might have been
    857                     // corrupted.
    858                     Slog.w(PackageManagerService.TAG, "Cleaning up stopped packages file "
    859                             + userPackagesStateFile);
    860                     userPackagesStateFile.delete();
    861                 }
    862             } catch (java.io.IOException e) {
    863                 // We'll try for the normal settings file.
    864             }
    865         }
    866 
    867         try {
    868             if (str == null) {
    869                 if (!userPackagesStateFile.exists()) {
    870                     mReadMessages.append("No stopped packages file found\n");
    871                     PackageManagerService.reportSettingsProblem(Log.INFO,
    872                             "No stopped packages file; "
    873                             + "assuming all started");
    874                     // At first boot, make sure no packages are stopped.
    875                     // We usually want to have third party apps initialize
    876                     // in the stopped state, but not at first boot.  Also
    877                     // consider all applications to be installed.
    878                     for (PackageSetting pkg : mPackages.values()) {
    879                         pkg.setUserState(userId, COMPONENT_ENABLED_STATE_DEFAULT,
    880                                 true,   // installed
    881                                 false,  // stopped
    882                                 false,  // notLaunched
    883                                 false,  // blocked
    884                                 null, null, null);
    885                     }
    886                     return;
    887                 }
    888                 str = new FileInputStream(userPackagesStateFile);
    889             }
    890             final XmlPullParser parser = Xml.newPullParser();
    891             parser.setInput(str, null);
    892 
    893             int type;
    894             while ((type=parser.next()) != XmlPullParser.START_TAG
    895                        && type != XmlPullParser.END_DOCUMENT) {
    896                 ;
    897             }
    898 
    899             if (type != XmlPullParser.START_TAG) {
    900                 mReadMessages.append("No start tag found in package restrictions file\n");
    901                 PackageManagerService.reportSettingsProblem(Log.WARN,
    902                         "No start tag found in package manager stopped packages");
    903                 return;
    904             }
    905 
    906             int outerDepth = parser.getDepth();
    907             PackageSetting ps = null;
    908             while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
    909                    && (type != XmlPullParser.END_TAG
    910                            || parser.getDepth() > outerDepth)) {
    911                 if (type == XmlPullParser.END_TAG
    912                         || type == XmlPullParser.TEXT) {
    913                     continue;
    914                 }
    915 
    916                 String tagName = parser.getName();
    917                 if (tagName.equals(TAG_PACKAGE)) {
    918                     String name = parser.getAttributeValue(null, ATTR_NAME);
    919                     ps = mPackages.get(name);
    920                     if (ps == null) {
    921                         Slog.w(PackageManagerService.TAG, "No package known for stopped package: "
    922                                 + name);
    923                         XmlUtils.skipCurrentTag(parser);
    924                         continue;
    925                     }
    926                     final String enabledStr = parser.getAttributeValue(null, ATTR_ENABLED);
    927                     final int enabled = enabledStr == null
    928                             ? COMPONENT_ENABLED_STATE_DEFAULT : Integer.parseInt(enabledStr);
    929                     final String enabledCaller = parser.getAttributeValue(null,
    930                             ATTR_ENABLED_CALLER);
    931                     final String installedStr = parser.getAttributeValue(null, ATTR_INSTALLED);
    932                     final boolean installed = installedStr == null
    933                             ? true : Boolean.parseBoolean(installedStr);
    934                     final String stoppedStr = parser.getAttributeValue(null, ATTR_STOPPED);
    935                     final boolean stopped = stoppedStr == null
    936                             ? false : Boolean.parseBoolean(stoppedStr);
    937                     final String blockedStr = parser.getAttributeValue(null, ATTR_BLOCKED);
    938                     final boolean blocked = blockedStr == null
    939                             ? false : Boolean.parseBoolean(blockedStr);
    940                     final String notLaunchedStr = parser.getAttributeValue(null, ATTR_NOT_LAUNCHED);
    941                     final boolean notLaunched = stoppedStr == null
    942                             ? false : Boolean.parseBoolean(notLaunchedStr);
    943 
    944                     HashSet<String> enabledComponents = null;
    945                     HashSet<String> disabledComponents = null;
    946 
    947                     int packageDepth = parser.getDepth();
    948                     while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
    949                             && (type != XmlPullParser.END_TAG
    950                             || parser.getDepth() > packageDepth)) {
    951                         if (type == XmlPullParser.END_TAG
    952                                 || type == XmlPullParser.TEXT) {
    953                             continue;
    954                         }
    955                         tagName = parser.getName();
    956                         if (tagName.equals(TAG_ENABLED_COMPONENTS)) {
    957                             enabledComponents = readComponentsLPr(parser);
    958                         } else if (tagName.equals(TAG_DISABLED_COMPONENTS)) {
    959                             disabledComponents = readComponentsLPr(parser);
    960                         }
    961                     }
    962 
    963                     ps.setUserState(userId, enabled, installed, stopped, notLaunched, blocked,
    964                             enabledCaller, enabledComponents, disabledComponents);
    965                 } else if (tagName.equals("preferred-activities")) {
    966                     readPreferredActivitiesLPw(parser, userId);
    967                 } else {
    968                     Slog.w(PackageManagerService.TAG, "Unknown element under <stopped-packages>: "
    969                           + parser.getName());
    970                     XmlUtils.skipCurrentTag(parser);
    971                 }
    972             }
    973 
    974             str.close();
    975 
    976         } catch (XmlPullParserException e) {
    977             mReadMessages.append("Error reading: " + e.toString());
    978             PackageManagerService.reportSettingsProblem(Log.ERROR,
    979                     "Error reading stopped packages: " + e);
    980             Log.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages", e);
    981 
    982         } catch (java.io.IOException e) {
    983             mReadMessages.append("Error reading: " + e.toString());
    984             PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
    985             Log.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages", e);
    986         }
    987     }
    988 
    989     private HashSet<String> readComponentsLPr(XmlPullParser parser)
    990             throws IOException, XmlPullParserException {
    991         HashSet<String> components = null;
    992         int type;
    993         int outerDepth = parser.getDepth();
    994         String tagName;
    995         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
    996                 && (type != XmlPullParser.END_TAG
    997                 || parser.getDepth() > outerDepth)) {
    998             if (type == XmlPullParser.END_TAG
    999                     || type == XmlPullParser.TEXT) {
   1000                 continue;
   1001             }
   1002             tagName = parser.getName();
   1003             if (tagName.equals(TAG_ITEM)) {
   1004                 String componentName = parser.getAttributeValue(null, ATTR_NAME);
   1005                 if (componentName != null) {
   1006                     if (components == null) {
   1007                         components = new HashSet<String>();
   1008                     }
   1009                     components.add(componentName);
   1010                 }
   1011             }
   1012         }
   1013         return components;
   1014     }
   1015 
   1016     void writePreferredActivitiesLPr(XmlSerializer serializer, int userId, boolean full)
   1017             throws IllegalArgumentException, IllegalStateException, IOException {
   1018         serializer.startTag(null, "preferred-activities");
   1019         PreferredIntentResolver pir = mPreferredActivities.get(userId);
   1020         if (pir != null) {
   1021             for (final PreferredActivity pa : pir.filterSet()) {
   1022                 serializer.startTag(null, TAG_ITEM);
   1023                 pa.writeToXml(serializer, full);
   1024                 serializer.endTag(null, TAG_ITEM);
   1025             }
   1026         }
   1027         serializer.endTag(null, "preferred-activities");
   1028     }
   1029 
   1030     void writePackageRestrictionsLPr(int userId) {
   1031         if (DEBUG_MU) {
   1032             Log.i(TAG, "Writing package restrictions for user=" + userId);
   1033         }
   1034         // Keep the old stopped packages around until we know the new ones have
   1035         // been successfully written.
   1036         File userPackagesStateFile = getUserPackagesStateFile(userId);
   1037         File backupFile = getUserPackagesStateBackupFile(userId);
   1038         new File(userPackagesStateFile.getParent()).mkdirs();
   1039         if (userPackagesStateFile.exists()) {
   1040             // Presence of backup settings file indicates that we failed
   1041             // to persist packages earlier. So preserve the older
   1042             // backup for future reference since the current packages
   1043             // might have been corrupted.
   1044             if (!backupFile.exists()) {
   1045                 if (!userPackagesStateFile.renameTo(backupFile)) {
   1046                     Log.wtf(PackageManagerService.TAG, "Unable to backup user packages state file, "
   1047                             + "current changes will be lost at reboot");
   1048                     return;
   1049                 }
   1050             } else {
   1051                 userPackagesStateFile.delete();
   1052                 Slog.w(PackageManagerService.TAG, "Preserving older stopped packages backup");
   1053             }
   1054         }
   1055 
   1056         try {
   1057             final FileOutputStream fstr = new FileOutputStream(userPackagesStateFile);
   1058             final BufferedOutputStream str = new BufferedOutputStream(fstr);
   1059 
   1060             final XmlSerializer serializer = new FastXmlSerializer();
   1061             serializer.setOutput(str, "utf-8");
   1062             serializer.startDocument(null, true);
   1063             serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
   1064 
   1065             serializer.startTag(null, TAG_PACKAGE_RESTRICTIONS);
   1066 
   1067             for (final PackageSetting pkg : mPackages.values()) {
   1068                 PackageUserState ustate = pkg.readUserState(userId);
   1069                 if (ustate.stopped || ustate.notLaunched || !ustate.installed
   1070                         || ustate.enabled != COMPONENT_ENABLED_STATE_DEFAULT
   1071                         || ustate.blocked
   1072                         || (ustate.enabledComponents != null
   1073                                 && ustate.enabledComponents.size() > 0)
   1074                         || (ustate.disabledComponents != null
   1075                                 && ustate.disabledComponents.size() > 0)) {
   1076                     serializer.startTag(null, TAG_PACKAGE);
   1077                     serializer.attribute(null, ATTR_NAME, pkg.name);
   1078                     if (DEBUG_MU) Log.i(TAG, "  pkg=" + pkg.name + ", state=" + ustate.enabled);
   1079 
   1080                     if (!ustate.installed) {
   1081                         serializer.attribute(null, ATTR_INSTALLED, "false");
   1082                     }
   1083                     if (ustate.stopped) {
   1084                         serializer.attribute(null, ATTR_STOPPED, "true");
   1085                     }
   1086                     if (ustate.notLaunched) {
   1087                         serializer.attribute(null, ATTR_NOT_LAUNCHED, "true");
   1088                     }
   1089                     if (ustate.blocked) {
   1090                         serializer.attribute(null, ATTR_BLOCKED, "true");
   1091                     }
   1092                     if (ustate.enabled != COMPONENT_ENABLED_STATE_DEFAULT) {
   1093                         serializer.attribute(null, ATTR_ENABLED,
   1094                                 Integer.toString(ustate.enabled));
   1095                         if (ustate.lastDisableAppCaller != null) {
   1096                             serializer.attribute(null, ATTR_ENABLED_CALLER,
   1097                                     ustate.lastDisableAppCaller);
   1098                         }
   1099                     }
   1100                     if (ustate.enabledComponents != null
   1101                             && ustate.enabledComponents.size() > 0) {
   1102                         serializer.startTag(null, TAG_ENABLED_COMPONENTS);
   1103                         for (final String name : ustate.enabledComponents) {
   1104                             serializer.startTag(null, TAG_ITEM);
   1105                             serializer.attribute(null, ATTR_NAME, name);
   1106                             serializer.endTag(null, TAG_ITEM);
   1107                         }
   1108                         serializer.endTag(null, TAG_ENABLED_COMPONENTS);
   1109                     }
   1110                     if (ustate.disabledComponents != null
   1111                             && ustate.disabledComponents.size() > 0) {
   1112                         serializer.startTag(null, TAG_DISABLED_COMPONENTS);
   1113                         for (final String name : ustate.disabledComponents) {
   1114                             serializer.startTag(null, TAG_ITEM);
   1115                             serializer.attribute(null, ATTR_NAME, name);
   1116                             serializer.endTag(null, TAG_ITEM);
   1117                         }
   1118                         serializer.endTag(null, TAG_DISABLED_COMPONENTS);
   1119                     }
   1120                     serializer.endTag(null, TAG_PACKAGE);
   1121                 }
   1122             }
   1123 
   1124             writePreferredActivitiesLPr(serializer, userId, true);
   1125 
   1126             serializer.endTag(null, TAG_PACKAGE_RESTRICTIONS);
   1127 
   1128             serializer.endDocument();
   1129 
   1130             str.flush();
   1131             FileUtils.sync(fstr);
   1132             str.close();
   1133 
   1134             // New settings successfully written, old ones are no longer
   1135             // needed.
   1136             backupFile.delete();
   1137             FileUtils.setPermissions(userPackagesStateFile.toString(),
   1138                     FileUtils.S_IRUSR|FileUtils.S_IWUSR
   1139                     |FileUtils.S_IRGRP|FileUtils.S_IWGRP,
   1140                     -1, -1);
   1141 
   1142             // Done, all is good!
   1143             return;
   1144         } catch(java.io.IOException e) {
   1145             Log.wtf(PackageManagerService.TAG,
   1146                     "Unable to write package manager user packages state, "
   1147                     + " current changes will be lost at reboot", e);
   1148         }
   1149 
   1150         // Clean up partially written files
   1151         if (userPackagesStateFile.exists()) {
   1152             if (!userPackagesStateFile.delete()) {
   1153                 Log.i(PackageManagerService.TAG, "Failed to clean up mangled file: "
   1154                         + mStoppedPackagesFilename);
   1155             }
   1156         }
   1157     }
   1158 
   1159     // Note: assumed "stopped" field is already cleared in all packages.
   1160     // Legacy reader, used to read in the old file format after an upgrade. Not used after that.
   1161     void readStoppedLPw() {
   1162         FileInputStream str = null;
   1163         if (mBackupStoppedPackagesFilename.exists()) {
   1164             try {
   1165                 str = new FileInputStream(mBackupStoppedPackagesFilename);
   1166                 mReadMessages.append("Reading from backup stopped packages file\n");
   1167                 PackageManagerService.reportSettingsProblem(Log.INFO,
   1168                         "Need to read from backup stopped packages file");
   1169                 if (mSettingsFilename.exists()) {
   1170                     // If both the backup and normal file exist, we
   1171                     // ignore the normal one since it might have been
   1172                     // corrupted.
   1173                     Slog.w(PackageManagerService.TAG, "Cleaning up stopped packages file "
   1174                             + mStoppedPackagesFilename);
   1175                     mStoppedPackagesFilename.delete();
   1176                 }
   1177             } catch (java.io.IOException e) {
   1178                 // We'll try for the normal settings file.
   1179             }
   1180         }
   1181 
   1182         try {
   1183             if (str == null) {
   1184                 if (!mStoppedPackagesFilename.exists()) {
   1185                     mReadMessages.append("No stopped packages file found\n");
   1186                     PackageManagerService.reportSettingsProblem(Log.INFO,
   1187                             "No stopped packages file file; assuming all started");
   1188                     // At first boot, make sure no packages are stopped.
   1189                     // We usually want to have third party apps initialize
   1190                     // in the stopped state, but not at first boot.
   1191                     for (PackageSetting pkg : mPackages.values()) {
   1192                         pkg.setStopped(false, 0);
   1193                         pkg.setNotLaunched(false, 0);
   1194                     }
   1195                     return;
   1196                 }
   1197                 str = new FileInputStream(mStoppedPackagesFilename);
   1198             }
   1199             final XmlPullParser parser = Xml.newPullParser();
   1200             parser.setInput(str, null);
   1201 
   1202             int type;
   1203             while ((type=parser.next()) != XmlPullParser.START_TAG
   1204                        && type != XmlPullParser.END_DOCUMENT) {
   1205                 ;
   1206             }
   1207 
   1208             if (type != XmlPullParser.START_TAG) {
   1209                 mReadMessages.append("No start tag found in stopped packages file\n");
   1210                 PackageManagerService.reportSettingsProblem(Log.WARN,
   1211                         "No start tag found in package manager stopped packages");
   1212                 return;
   1213             }
   1214 
   1215             int outerDepth = parser.getDepth();
   1216             while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
   1217                    && (type != XmlPullParser.END_TAG
   1218                            || parser.getDepth() > outerDepth)) {
   1219                 if (type == XmlPullParser.END_TAG
   1220                         || type == XmlPullParser.TEXT) {
   1221                     continue;
   1222                 }
   1223 
   1224                 String tagName = parser.getName();
   1225                 if (tagName.equals(TAG_PACKAGE)) {
   1226                     String name = parser.getAttributeValue(null, ATTR_NAME);
   1227                     PackageSetting ps = mPackages.get(name);
   1228                     if (ps != null) {
   1229                         ps.setStopped(true, 0);
   1230                         if ("1".equals(parser.getAttributeValue(null, ATTR_NOT_LAUNCHED))) {
   1231                             ps.setNotLaunched(true, 0);
   1232                         }
   1233                     } else {
   1234                         Slog.w(PackageManagerService.TAG,
   1235                                 "No package known for stopped package: " + name);
   1236                     }
   1237                     XmlUtils.skipCurrentTag(parser);
   1238                 } else {
   1239                     Slog.w(PackageManagerService.TAG, "Unknown element under <stopped-packages>: "
   1240                           + parser.getName());
   1241                     XmlUtils.skipCurrentTag(parser);
   1242                 }
   1243             }
   1244 
   1245             str.close();
   1246 
   1247         } catch (XmlPullParserException e) {
   1248             mReadMessages.append("Error reading: " + e.toString());
   1249             PackageManagerService.reportSettingsProblem(Log.ERROR,
   1250                     "Error reading stopped packages: " + e);
   1251             Log.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages", e);
   1252 
   1253         } catch (java.io.IOException e) {
   1254             mReadMessages.append("Error reading: " + e.toString());
   1255             PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
   1256             Log.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages", e);
   1257 
   1258         }
   1259     }
   1260 
   1261     void writeLPr() {
   1262         //Debug.startMethodTracing("/data/system/packageprof", 8 * 1024 * 1024);
   1263 
   1264         // Keep the old settings around until we know the new ones have
   1265         // been successfully written.
   1266         if (mSettingsFilename.exists()) {
   1267             // Presence of backup settings file indicates that we failed
   1268             // to persist settings earlier. So preserve the older
   1269             // backup for future reference since the current settings
   1270             // might have been corrupted.
   1271             if (!mBackupSettingsFilename.exists()) {
   1272                 if (!mSettingsFilename.renameTo(mBackupSettingsFilename)) {
   1273                     Log.wtf(PackageManagerService.TAG, "Unable to backup package manager settings, "
   1274                             + " current changes will be lost at reboot");
   1275                     return;
   1276                 }
   1277             } else {
   1278                 mSettingsFilename.delete();
   1279                 Slog.w(PackageManagerService.TAG, "Preserving older settings backup");
   1280             }
   1281         }
   1282 
   1283         mPastSignatures.clear();
   1284 
   1285         try {
   1286             FileOutputStream fstr = new FileOutputStream(mSettingsFilename);
   1287             BufferedOutputStream str = new BufferedOutputStream(fstr);
   1288 
   1289             //XmlSerializer serializer = XmlUtils.serializerInstance();
   1290             XmlSerializer serializer = new FastXmlSerializer();
   1291             serializer.setOutput(str, "utf-8");
   1292             serializer.startDocument(null, true);
   1293             serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
   1294 
   1295             serializer.startTag(null, "packages");
   1296 
   1297             serializer.startTag(null, "last-platform-version");
   1298             serializer.attribute(null, "internal", Integer.toString(mInternalSdkPlatform));
   1299             serializer.attribute(null, "external", Integer.toString(mExternalSdkPlatform));
   1300             serializer.endTag(null, "last-platform-version");
   1301 
   1302             if (mVerifierDeviceIdentity != null) {
   1303                 serializer.startTag(null, "verifier");
   1304                 serializer.attribute(null, "device", mVerifierDeviceIdentity.toString());
   1305                 serializer.endTag(null, "verifier");
   1306             }
   1307 
   1308             if (mReadExternalStorageEnforced != null) {
   1309                 serializer.startTag(null, TAG_READ_EXTERNAL_STORAGE);
   1310                 serializer.attribute(
   1311                         null, ATTR_ENFORCEMENT, mReadExternalStorageEnforced ? "1" : "0");
   1312                 serializer.endTag(null, TAG_READ_EXTERNAL_STORAGE);
   1313             }
   1314 
   1315             serializer.startTag(null, "permission-trees");
   1316             for (BasePermission bp : mPermissionTrees.values()) {
   1317                 writePermissionLPr(serializer, bp);
   1318             }
   1319             serializer.endTag(null, "permission-trees");
   1320 
   1321             serializer.startTag(null, "permissions");
   1322             for (BasePermission bp : mPermissions.values()) {
   1323                 writePermissionLPr(serializer, bp);
   1324             }
   1325             serializer.endTag(null, "permissions");
   1326 
   1327             for (final PackageSetting pkg : mPackages.values()) {
   1328                 writePackageLPr(serializer, pkg);
   1329             }
   1330 
   1331             for (final PackageSetting pkg : mDisabledSysPackages.values()) {
   1332                 writeDisabledSysPackageLPr(serializer, pkg);
   1333             }
   1334 
   1335             for (final SharedUserSetting usr : mSharedUsers.values()) {
   1336                 serializer.startTag(null, "shared-user");
   1337                 serializer.attribute(null, ATTR_NAME, usr.name);
   1338                 serializer.attribute(null, "userId",
   1339                         Integer.toString(usr.userId));
   1340                 usr.signatures.writeXml(serializer, "sigs", mPastSignatures);
   1341                 serializer.startTag(null, "perms");
   1342                 for (String name : usr.grantedPermissions) {
   1343                     serializer.startTag(null, TAG_ITEM);
   1344                     serializer.attribute(null, ATTR_NAME, name);
   1345                     serializer.endTag(null, TAG_ITEM);
   1346                 }
   1347                 serializer.endTag(null, "perms");
   1348                 serializer.endTag(null, "shared-user");
   1349             }
   1350 
   1351             if (mPackagesToBeCleaned.size() > 0) {
   1352                 for (PackageCleanItem item : mPackagesToBeCleaned) {
   1353                     final String userStr = Integer.toString(item.userId);
   1354                     serializer.startTag(null, "cleaning-package");
   1355                     serializer.attribute(null, ATTR_NAME, item.packageName);
   1356                     serializer.attribute(null, ATTR_CODE, item.andCode ? "true" : "false");
   1357                     serializer.attribute(null, ATTR_USER, userStr);
   1358                     serializer.endTag(null, "cleaning-package");
   1359                 }
   1360             }
   1361 
   1362             if (mRenamedPackages.size() > 0) {
   1363                 for (Map.Entry<String, String> e : mRenamedPackages.entrySet()) {
   1364                     serializer.startTag(null, "renamed-package");
   1365                     serializer.attribute(null, "new", e.getKey());
   1366                     serializer.attribute(null, "old", e.getValue());
   1367                     serializer.endTag(null, "renamed-package");
   1368                 }
   1369             }
   1370 
   1371             mKeySetManager.writeKeySetManagerLPr(serializer);
   1372 
   1373             serializer.endTag(null, "packages");
   1374 
   1375             serializer.endDocument();
   1376 
   1377             str.flush();
   1378             FileUtils.sync(fstr);
   1379             str.close();
   1380 
   1381             // New settings successfully written, old ones are no longer
   1382             // needed.
   1383             mBackupSettingsFilename.delete();
   1384             FileUtils.setPermissions(mSettingsFilename.toString(),
   1385                     FileUtils.S_IRUSR|FileUtils.S_IWUSR
   1386                     |FileUtils.S_IRGRP|FileUtils.S_IWGRP,
   1387                     -1, -1);
   1388 
   1389             // Write package list file now, use a JournaledFile.
   1390             File tempFile = new File(mPackageListFilename.getAbsolutePath() + ".tmp");
   1391             JournaledFile journal = new JournaledFile(mPackageListFilename, tempFile);
   1392 
   1393             final File writeTarget = journal.chooseForWrite();
   1394             fstr = new FileOutputStream(writeTarget);
   1395             str = new BufferedOutputStream(fstr);
   1396             try {
   1397                 FileUtils.setPermissions(fstr.getFD(), 0660, SYSTEM_UID, PACKAGE_INFO_GID);
   1398 
   1399                 StringBuilder sb = new StringBuilder();
   1400                 for (final PackageSetting pkg : mPackages.values()) {
   1401                     if (pkg.pkg == null || pkg.pkg.applicationInfo == null) {
   1402                         Slog.w(TAG, "Skipping " + pkg + " due to missing metadata");
   1403                         continue;
   1404                     }
   1405 
   1406                     final ApplicationInfo ai = pkg.pkg.applicationInfo;
   1407                     final String dataPath = ai.dataDir;
   1408                     final boolean isDebug = (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
   1409                     final int[] gids = pkg.getGids();
   1410 
   1411                     // Avoid any application that has a space in its path.
   1412                     if (dataPath.indexOf(" ") >= 0)
   1413                         continue;
   1414 
   1415                     // we store on each line the following information for now:
   1416                     //
   1417                     // pkgName    - package name
   1418                     // userId     - application-specific user id
   1419                     // debugFlag  - 0 or 1 if the package is debuggable.
   1420                     // dataPath   - path to package's data path
   1421                     // seinfo     - seinfo label for the app (assigned at install time)
   1422                     // gids       - supplementary gids this app launches with
   1423                     //
   1424                     // NOTE: We prefer not to expose all ApplicationInfo flags for now.
   1425                     //
   1426                     // DO NOT MODIFY THIS FORMAT UNLESS YOU CAN ALSO MODIFY ITS USERS
   1427                     // FROM NATIVE CODE. AT THE MOMENT, LOOK AT THE FOLLOWING SOURCES:
   1428                     //   system/core/run-as/run-as.c
   1429                     //   system/core/sdcard/sdcard.c
   1430                     //
   1431                     sb.setLength(0);
   1432                     sb.append(ai.packageName);
   1433                     sb.append(" ");
   1434                     sb.append((int)ai.uid);
   1435                     sb.append(isDebug ? " 1 " : " 0 ");
   1436                     sb.append(dataPath);
   1437                     sb.append(" ");
   1438                     sb.append(ai.seinfo);
   1439                     sb.append(" ");
   1440                     if (gids != null && gids.length > 0) {
   1441                         sb.append(gids[0]);
   1442                         for (int i = 1; i < gids.length; i++) {
   1443                             sb.append(",");
   1444                             sb.append(gids[i]);
   1445                         }
   1446                     } else {
   1447                         sb.append("none");
   1448                     }
   1449                     sb.append("\n");
   1450                     str.write(sb.toString().getBytes());
   1451                 }
   1452                 str.flush();
   1453                 FileUtils.sync(fstr);
   1454                 str.close();
   1455                 journal.commit();
   1456             } catch (Exception e) {
   1457                 Log.wtf(TAG, "Failed to write packages.list", e);
   1458                 IoUtils.closeQuietly(str);
   1459                 journal.rollback();
   1460             }
   1461 
   1462             writeAllUsersPackageRestrictionsLPr();
   1463             return;
   1464 
   1465         } catch(XmlPullParserException e) {
   1466             Log.wtf(PackageManagerService.TAG, "Unable to write package manager settings, "
   1467                     + "current changes will be lost at reboot", e);
   1468         } catch(java.io.IOException e) {
   1469             Log.wtf(PackageManagerService.TAG, "Unable to write package manager settings, "
   1470                     + "current changes will be lost at reboot", e);
   1471         }
   1472         // Clean up partially written files
   1473         if (mSettingsFilename.exists()) {
   1474             if (!mSettingsFilename.delete()) {
   1475                 Log.wtf(PackageManagerService.TAG, "Failed to clean up mangled file: "
   1476                         + mSettingsFilename);
   1477             }
   1478         }
   1479         //Debug.stopMethodTracing();
   1480     }
   1481 
   1482     void writeDisabledSysPackageLPr(XmlSerializer serializer, final PackageSetting pkg)
   1483             throws java.io.IOException {
   1484         serializer.startTag(null, "updated-package");
   1485         serializer.attribute(null, ATTR_NAME, pkg.name);
   1486         if (pkg.realName != null) {
   1487             serializer.attribute(null, "realName", pkg.realName);
   1488         }
   1489         serializer.attribute(null, "codePath", pkg.codePathString);
   1490         serializer.attribute(null, "ft", Long.toHexString(pkg.timeStamp));
   1491         serializer.attribute(null, "it", Long.toHexString(pkg.firstInstallTime));
   1492         serializer.attribute(null, "ut", Long.toHexString(pkg.lastUpdateTime));
   1493         serializer.attribute(null, "version", String.valueOf(pkg.versionCode));
   1494         if (!pkg.resourcePathString.equals(pkg.codePathString)) {
   1495             serializer.attribute(null, "resourcePath", pkg.resourcePathString);
   1496         }
   1497         if (pkg.nativeLibraryPathString != null) {
   1498             serializer.attribute(null, "nativeLibraryPath", pkg.nativeLibraryPathString);
   1499         }
   1500         if (pkg.sharedUser == null) {
   1501             serializer.attribute(null, "userId", Integer.toString(pkg.appId));
   1502         } else {
   1503             serializer.attribute(null, "sharedUserId", Integer.toString(pkg.appId));
   1504         }
   1505         serializer.startTag(null, "perms");
   1506         if (pkg.sharedUser == null) {
   1507             // If this is a shared user, the permissions will
   1508             // be written there. We still need to write an
   1509             // empty permissions list so permissionsFixed will
   1510             // be set.
   1511             for (final String name : pkg.grantedPermissions) {
   1512                 BasePermission bp = mPermissions.get(name);
   1513                 if (bp != null) {
   1514                     // We only need to write signature or system permissions but
   1515                     // this wont
   1516                     // match the semantics of grantedPermissions. So write all
   1517                     // permissions.
   1518                     serializer.startTag(null, TAG_ITEM);
   1519                     serializer.attribute(null, ATTR_NAME, name);
   1520                     serializer.endTag(null, TAG_ITEM);
   1521                 }
   1522             }
   1523         }
   1524         serializer.endTag(null, "perms");
   1525         serializer.endTag(null, "updated-package");
   1526     }
   1527 
   1528     void writePackageLPr(XmlSerializer serializer, final PackageSetting pkg)
   1529             throws java.io.IOException {
   1530         serializer.startTag(null, "package");
   1531         serializer.attribute(null, ATTR_NAME, pkg.name);
   1532         if (pkg.realName != null) {
   1533             serializer.attribute(null, "realName", pkg.realName);
   1534         }
   1535         serializer.attribute(null, "codePath", pkg.codePathString);
   1536         if (!pkg.resourcePathString.equals(pkg.codePathString)) {
   1537             serializer.attribute(null, "resourcePath", pkg.resourcePathString);
   1538         }
   1539         if (pkg.nativeLibraryPathString != null) {
   1540             serializer.attribute(null, "nativeLibraryPath", pkg.nativeLibraryPathString);
   1541         }
   1542         serializer.attribute(null, "flags", Integer.toString(pkg.pkgFlags));
   1543         serializer.attribute(null, "ft", Long.toHexString(pkg.timeStamp));
   1544         serializer.attribute(null, "it", Long.toHexString(pkg.firstInstallTime));
   1545         serializer.attribute(null, "ut", Long.toHexString(pkg.lastUpdateTime));
   1546         serializer.attribute(null, "version", String.valueOf(pkg.versionCode));
   1547         if (pkg.sharedUser == null) {
   1548             serializer.attribute(null, "userId", Integer.toString(pkg.appId));
   1549         } else {
   1550             serializer.attribute(null, "sharedUserId", Integer.toString(pkg.appId));
   1551         }
   1552         if (pkg.uidError) {
   1553             serializer.attribute(null, "uidError", "true");
   1554         }
   1555         if (pkg.installStatus == PackageSettingBase.PKG_INSTALL_INCOMPLETE) {
   1556             serializer.attribute(null, "installStatus", "false");
   1557         }
   1558         if (pkg.installerPackageName != null) {
   1559             serializer.attribute(null, "installer", pkg.installerPackageName);
   1560         }
   1561         pkg.signatures.writeXml(serializer, "sigs", mPastSignatures);
   1562         if ((pkg.pkgFlags & ApplicationInfo.FLAG_SYSTEM) == 0) {
   1563             serializer.startTag(null, "perms");
   1564             if (pkg.sharedUser == null) {
   1565                 // If this is a shared user, the permissions will
   1566                 // be written there. We still need to write an
   1567                 // empty permissions list so permissionsFixed will
   1568                 // be set.
   1569                 for (final String name : pkg.grantedPermissions) {
   1570                     serializer.startTag(null, TAG_ITEM);
   1571                     serializer.attribute(null, ATTR_NAME, name);
   1572                     serializer.endTag(null, TAG_ITEM);
   1573                 }
   1574             }
   1575             serializer.endTag(null, "perms");
   1576         }
   1577 
   1578         writeSigningKeySetsLPr(serializer, pkg.keySetData);
   1579         writeKeySetAliasesLPr(serializer, pkg.keySetData);
   1580 
   1581         serializer.endTag(null, "package");
   1582     }
   1583 
   1584     void writeSigningKeySetsLPr(XmlSerializer serializer,
   1585             PackageKeySetData data) throws IOException {
   1586         for (long id : data.getSigningKeySets()) {
   1587             serializer.startTag(null, "signing-keyset");
   1588             serializer.attribute(null, "identifier", Long.toString(id));
   1589             serializer.endTag(null, "signing-keyset");
   1590         }
   1591     }
   1592 
   1593     void writeKeySetAliasesLPr(XmlSerializer serializer,
   1594             PackageKeySetData data) throws IOException {
   1595         for (Map.Entry<String, Long> e: data.getAliases().entrySet()) {
   1596             serializer.startTag(null, "defined-keyset");
   1597             serializer.attribute(null, "alias", e.getKey());
   1598             serializer.attribute(null, "identifier", Long.toString(e.getValue()));
   1599             serializer.endTag(null, "defined-keyset");
   1600         }
   1601     }
   1602 
   1603     void writePermissionLPr(XmlSerializer serializer, BasePermission bp)
   1604             throws XmlPullParserException, java.io.IOException {
   1605         if (bp.type != BasePermission.TYPE_BUILTIN && bp.sourcePackage != null) {
   1606             serializer.startTag(null, TAG_ITEM);
   1607             serializer.attribute(null, ATTR_NAME, bp.name);
   1608             serializer.attribute(null, "package", bp.sourcePackage);
   1609             if (bp.protectionLevel != PermissionInfo.PROTECTION_NORMAL) {
   1610                 serializer.attribute(null, "protection", Integer.toString(bp.protectionLevel));
   1611             }
   1612             if (PackageManagerService.DEBUG_SETTINGS)
   1613                 Log.v(PackageManagerService.TAG, "Writing perm: name=" + bp.name + " type="
   1614                         + bp.type);
   1615             if (bp.type == BasePermission.TYPE_DYNAMIC) {
   1616                 final PermissionInfo pi = bp.perm != null ? bp.perm.info : bp.pendingInfo;
   1617                 if (pi != null) {
   1618                     serializer.attribute(null, "type", "dynamic");
   1619                     if (pi.icon != 0) {
   1620                         serializer.attribute(null, "icon", Integer.toString(pi.icon));
   1621                     }
   1622                     if (pi.nonLocalizedLabel != null) {
   1623                         serializer.attribute(null, "label", pi.nonLocalizedLabel.toString());
   1624                     }
   1625                 }
   1626             }
   1627             serializer.endTag(null, TAG_ITEM);
   1628         }
   1629     }
   1630 
   1631     ArrayList<PackageSetting> getListOfIncompleteInstallPackagesLPr() {
   1632         final HashSet<String> kList = new HashSet<String>(mPackages.keySet());
   1633         final Iterator<String> its = kList.iterator();
   1634         final ArrayList<PackageSetting> ret = new ArrayList<PackageSetting>();
   1635         while (its.hasNext()) {
   1636             final String key = its.next();
   1637             final PackageSetting ps = mPackages.get(key);
   1638             if (ps.getInstallStatus() == PackageSettingBase.PKG_INSTALL_INCOMPLETE) {
   1639                 ret.add(ps);
   1640             }
   1641         }
   1642         return ret;
   1643     }
   1644 
   1645     void addPackageToCleanLPw(PackageCleanItem pkg) {
   1646         if (!mPackagesToBeCleaned.contains(pkg)) {
   1647             mPackagesToBeCleaned.add(pkg);
   1648         }
   1649     }
   1650 
   1651     boolean readLPw(PackageManagerService service, List<UserInfo> users, int sdkVersion,
   1652             boolean onlyCore) {
   1653         FileInputStream str = null;
   1654         if (mBackupSettingsFilename.exists()) {
   1655             try {
   1656                 str = new FileInputStream(mBackupSettingsFilename);
   1657                 mReadMessages.append("Reading from backup settings file\n");
   1658                 PackageManagerService.reportSettingsProblem(Log.INFO,
   1659                         "Need to read from backup settings file");
   1660                 if (mSettingsFilename.exists()) {
   1661                     // If both the backup and settings file exist, we
   1662                     // ignore the settings since it might have been
   1663                     // corrupted.
   1664                     Slog.w(PackageManagerService.TAG, "Cleaning up settings file "
   1665                             + mSettingsFilename);
   1666                     mSettingsFilename.delete();
   1667                 }
   1668             } catch (java.io.IOException e) {
   1669                 // We'll try for the normal settings file.
   1670             }
   1671         }
   1672 
   1673         mPendingPackages.clear();
   1674         mPastSignatures.clear();
   1675 
   1676         try {
   1677             if (str == null) {
   1678                 if (!mSettingsFilename.exists()) {
   1679                     mReadMessages.append("No settings file found\n");
   1680                     PackageManagerService.reportSettingsProblem(Log.INFO,
   1681                             "No settings file; creating initial state");
   1682                     mInternalSdkPlatform = mExternalSdkPlatform = sdkVersion;
   1683                     return false;
   1684                 }
   1685                 str = new FileInputStream(mSettingsFilename);
   1686             }
   1687             XmlPullParser parser = Xml.newPullParser();
   1688             parser.setInput(str, null);
   1689 
   1690             int type;
   1691             while ((type = parser.next()) != XmlPullParser.START_TAG
   1692                     && type != XmlPullParser.END_DOCUMENT) {
   1693                 ;
   1694             }
   1695 
   1696             if (type != XmlPullParser.START_TAG) {
   1697                 mReadMessages.append("No start tag found in settings file\n");
   1698                 PackageManagerService.reportSettingsProblem(Log.WARN,
   1699                         "No start tag found in package manager settings");
   1700                 Log.wtf(PackageManagerService.TAG,
   1701                         "No start tag found in package manager settings");
   1702                 return false;
   1703             }
   1704 
   1705             int outerDepth = parser.getDepth();
   1706             while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
   1707                     && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
   1708                 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   1709                     continue;
   1710                 }
   1711 
   1712                 String tagName = parser.getName();
   1713                 if (tagName.equals("package")) {
   1714                     readPackageLPw(parser);
   1715                 } else if (tagName.equals("permissions")) {
   1716                     readPermissionsLPw(mPermissions, parser);
   1717                 } else if (tagName.equals("permission-trees")) {
   1718                     readPermissionsLPw(mPermissionTrees, parser);
   1719                 } else if (tagName.equals("shared-user")) {
   1720                     readSharedUserLPw(parser);
   1721                 } else if (tagName.equals("preferred-packages")) {
   1722                     // no longer used.
   1723                 } else if (tagName.equals("preferred-activities")) {
   1724                     // Upgrading from old single-user implementation;
   1725                     // these are the preferred activities for user 0.
   1726                     readPreferredActivitiesLPw(parser, 0);
   1727                 } else if (tagName.equals("updated-package")) {
   1728                     readDisabledSysPackageLPw(parser);
   1729                 } else if (tagName.equals("cleaning-package")) {
   1730                     String name = parser.getAttributeValue(null, ATTR_NAME);
   1731                     String userStr = parser.getAttributeValue(null, ATTR_USER);
   1732                     String codeStr = parser.getAttributeValue(null, ATTR_CODE);
   1733                     if (name != null) {
   1734                         int userId = 0;
   1735                         boolean andCode = true;
   1736                         try {
   1737                             if (userStr != null) {
   1738                                 userId = Integer.parseInt(userStr);
   1739                             }
   1740                         } catch (NumberFormatException e) {
   1741                         }
   1742                         if (codeStr != null) {
   1743                             andCode = Boolean.parseBoolean(codeStr);
   1744                         }
   1745                         addPackageToCleanLPw(new PackageCleanItem(userId, name, andCode));
   1746                     }
   1747                 } else if (tagName.equals("renamed-package")) {
   1748                     String nname = parser.getAttributeValue(null, "new");
   1749                     String oname = parser.getAttributeValue(null, "old");
   1750                     if (nname != null && oname != null) {
   1751                         mRenamedPackages.put(nname, oname);
   1752                     }
   1753                 } else if (tagName.equals("last-platform-version")) {
   1754                     mInternalSdkPlatform = mExternalSdkPlatform = 0;
   1755                     try {
   1756                         String internal = parser.getAttributeValue(null, "internal");
   1757                         if (internal != null) {
   1758                             mInternalSdkPlatform = Integer.parseInt(internal);
   1759                         }
   1760                         String external = parser.getAttributeValue(null, "external");
   1761                         if (external != null) {
   1762                             mExternalSdkPlatform = Integer.parseInt(external);
   1763                         }
   1764                     } catch (NumberFormatException e) {
   1765                     }
   1766                 } else if (tagName.equals("verifier")) {
   1767                     final String deviceIdentity = parser.getAttributeValue(null, "device");
   1768                     try {
   1769                         mVerifierDeviceIdentity = VerifierDeviceIdentity.parse(deviceIdentity);
   1770                     } catch (IllegalArgumentException e) {
   1771                         Slog.w(PackageManagerService.TAG, "Discard invalid verifier device id: "
   1772                                 + e.getMessage());
   1773                     }
   1774                 } else if (TAG_READ_EXTERNAL_STORAGE.equals(tagName)) {
   1775                     final String enforcement = parser.getAttributeValue(null, ATTR_ENFORCEMENT);
   1776                     mReadExternalStorageEnforced = "1".equals(enforcement);
   1777                 } else if (tagName.equals("keyset-settings")) {
   1778                     mKeySetManager.readKeySetsLPw(parser);
   1779                 } else {
   1780                     Slog.w(PackageManagerService.TAG, "Unknown element under <packages>: "
   1781                             + parser.getName());
   1782                     XmlUtils.skipCurrentTag(parser);
   1783                 }
   1784             }
   1785 
   1786             str.close();
   1787 
   1788         } catch (XmlPullParserException e) {
   1789             mReadMessages.append("Error reading: " + e.toString());
   1790             PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
   1791             Log.wtf(PackageManagerService.TAG, "Error reading package manager settings", e);
   1792 
   1793         } catch (java.io.IOException e) {
   1794             mReadMessages.append("Error reading: " + e.toString());
   1795             PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
   1796             Log.wtf(PackageManagerService.TAG, "Error reading package manager settings", e);
   1797         }
   1798 
   1799         final int N = mPendingPackages.size();
   1800         for (int i = 0; i < N; i++) {
   1801             final PendingPackage pp = mPendingPackages.get(i);
   1802             Object idObj = getUserIdLPr(pp.sharedId);
   1803             if (idObj != null && idObj instanceof SharedUserSetting) {
   1804                 PackageSetting p = getPackageLPw(pp.name, null, pp.realName,
   1805                         (SharedUserSetting) idObj, pp.codePath, pp.resourcePath,
   1806                         pp.nativeLibraryPathString, pp.versionCode, pp.pkgFlags,
   1807                         null, true /* add */, false /* allowInstall */);
   1808                 if (p == null) {
   1809                     PackageManagerService.reportSettingsProblem(Log.WARN,
   1810                             "Unable to create application package for " + pp.name);
   1811                     continue;
   1812                 }
   1813                 p.copyFrom(pp);
   1814             } else if (idObj != null) {
   1815                 String msg = "Bad package setting: package " + pp.name + " has shared uid "
   1816                         + pp.sharedId + " that is not a shared uid\n";
   1817                 mReadMessages.append(msg);
   1818                 PackageManagerService.reportSettingsProblem(Log.ERROR, msg);
   1819             } else {
   1820                 String msg = "Bad package setting: package " + pp.name + " has shared uid "
   1821                         + pp.sharedId + " that is not defined\n";
   1822                 mReadMessages.append(msg);
   1823                 PackageManagerService.reportSettingsProblem(Log.ERROR, msg);
   1824             }
   1825         }
   1826         mPendingPackages.clear();
   1827 
   1828         if (mBackupStoppedPackagesFilename.exists()
   1829                 || mStoppedPackagesFilename.exists()) {
   1830             // Read old file
   1831             readStoppedLPw();
   1832             mBackupStoppedPackagesFilename.delete();
   1833             mStoppedPackagesFilename.delete();
   1834             // Migrate to new file format
   1835             writePackageRestrictionsLPr(0);
   1836         } else {
   1837             if (users == null) {
   1838                 readPackageRestrictionsLPr(0);
   1839             } else {
   1840                 for (UserInfo user : users) {
   1841                     readPackageRestrictionsLPr(user.id);
   1842                 }
   1843             }
   1844         }
   1845 
   1846         /*
   1847          * Make sure all the updated system packages have their shared users
   1848          * associated with them.
   1849          */
   1850         final Iterator<PackageSetting> disabledIt = mDisabledSysPackages.values().iterator();
   1851         while (disabledIt.hasNext()) {
   1852             final PackageSetting disabledPs = disabledIt.next();
   1853             final Object id = getUserIdLPr(disabledPs.appId);
   1854             if (id != null && id instanceof SharedUserSetting) {
   1855                 disabledPs.sharedUser = (SharedUserSetting) id;
   1856             }
   1857         }
   1858 
   1859         mReadMessages.append("Read completed successfully: " + mPackages.size() + " packages, "
   1860                 + mSharedUsers.size() + " shared uids\n");
   1861 
   1862         return true;
   1863     }
   1864 
   1865     void readDefaultPreferredAppsLPw(PackageManagerService service, int userId) {
   1866         // First pull data from any pre-installed apps.
   1867         for (PackageSetting ps : mPackages.values()) {
   1868             if ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) != 0 && ps.pkg != null
   1869                     && ps.pkg.preferredActivityFilters != null) {
   1870                 ArrayList<PackageParser.ActivityIntentInfo> intents
   1871                         = ps.pkg.preferredActivityFilters;
   1872                 for (int i=0; i<intents.size(); i++) {
   1873                     PackageParser.ActivityIntentInfo aii = intents.get(i);
   1874                     applyDefaultPreferredActivityLPw(service, aii, new ComponentName(
   1875                             ps.name, aii.activity.className), userId);
   1876                 }
   1877             }
   1878         }
   1879 
   1880         // Read preferred apps from .../etc/preferred-apps directory.
   1881         File preferredDir = new File(Environment.getRootDirectory(), "etc/preferred-apps");
   1882         if (!preferredDir.exists() || !preferredDir.isDirectory()) {
   1883             return;
   1884         }
   1885         if (!preferredDir.canRead()) {
   1886             Slog.w(TAG, "Directory " + preferredDir + " cannot be read");
   1887             return;
   1888         }
   1889 
   1890         // Iterate over the files in the directory and scan .xml files
   1891         for (File f : preferredDir.listFiles()) {
   1892             if (!f.getPath().endsWith(".xml")) {
   1893                 Slog.i(TAG, "Non-xml file " + f + " in " + preferredDir + " directory, ignoring");
   1894                 continue;
   1895             }
   1896             if (!f.canRead()) {
   1897                 Slog.w(TAG, "Preferred apps file " + f + " cannot be read");
   1898                 continue;
   1899             }
   1900 
   1901             if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Reading default preferred " + f);
   1902             FileInputStream str = null;
   1903             try {
   1904                 str = new FileInputStream(f);
   1905                 XmlPullParser parser = Xml.newPullParser();
   1906                 parser.setInput(str, null);
   1907 
   1908                 int type;
   1909                 while ((type = parser.next()) != XmlPullParser.START_TAG
   1910                         && type != XmlPullParser.END_DOCUMENT) {
   1911                     ;
   1912                 }
   1913 
   1914                 if (type != XmlPullParser.START_TAG) {
   1915                     Slog.w(TAG, "Preferred apps file " + f + " does not have start tag");
   1916                     continue;
   1917                 }
   1918                 if (!"preferred-activities".equals(parser.getName())) {
   1919                     Slog.w(TAG, "Preferred apps file " + f
   1920                             + " does not start with 'preferred-activities'");
   1921                     continue;
   1922                 }
   1923                 readDefaultPreferredActivitiesLPw(service, parser, userId);
   1924             } catch (XmlPullParserException e) {
   1925                 Slog.w(TAG, "Error reading apps file " + f, e);
   1926             } catch (IOException e) {
   1927                 Slog.w(TAG, "Error reading apps file " + f, e);
   1928             } finally {
   1929                 if (str != null) {
   1930                     try {
   1931                         str.close();
   1932                     } catch (IOException e) {
   1933                     }
   1934                 }
   1935             }
   1936         }
   1937     }
   1938 
   1939     private void applyDefaultPreferredActivityLPw(PackageManagerService service,
   1940             IntentFilter tmpPa, ComponentName cn, int userId) {
   1941         // The initial preferences only specify the target activity
   1942         // component and intent-filter, not the set of matches.  So we
   1943         // now need to query for the matches to build the correct
   1944         // preferred activity entry.
   1945         if (PackageManagerService.DEBUG_PREFERRED) {
   1946             Log.d(TAG, "Processing preferred:");
   1947             tmpPa.dump(new LogPrinter(Log.DEBUG, TAG), "  ");
   1948         }
   1949         Intent intent = new Intent();
   1950         int flags = 0;
   1951         intent.setAction(tmpPa.getAction(0));
   1952         for (int i=0; i<tmpPa.countCategories(); i++) {
   1953             String cat = tmpPa.getCategory(i);
   1954             if (cat.equals(Intent.CATEGORY_DEFAULT)) {
   1955                 flags |= PackageManager.MATCH_DEFAULT_ONLY;
   1956             } else {
   1957                 intent.addCategory(cat);
   1958             }
   1959         }
   1960 
   1961         boolean doNonData = true;
   1962 
   1963         for (int ischeme=0; ischeme<tmpPa.countDataSchemes(); ischeme++) {
   1964             boolean doScheme = true;
   1965             String scheme = tmpPa.getDataScheme(ischeme);
   1966             for (int issp=0; issp<tmpPa.countDataSchemeSpecificParts(); issp++) {
   1967                 Uri.Builder builder = new Uri.Builder();
   1968                 builder.scheme(scheme);
   1969                 PatternMatcher ssp = tmpPa.getDataSchemeSpecificPart(issp);
   1970                 builder.opaquePart(ssp.getPath());
   1971                 Intent finalIntent = new Intent(intent);
   1972                 finalIntent.setData(builder.build());
   1973                 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
   1974                         scheme, ssp, null, null, null, userId);
   1975                 doScheme = false;
   1976             }
   1977             for (int iauth=0; iauth<tmpPa.countDataAuthorities(); iauth++) {
   1978                 boolean doAuth = true;
   1979                 IntentFilter.AuthorityEntry auth = tmpPa.getDataAuthority(iauth);
   1980                 for (int ipath=0; ipath<tmpPa.countDataPaths(); ipath++) {
   1981                     Uri.Builder builder = new Uri.Builder();
   1982                     builder.scheme(scheme);
   1983                     if (auth.getHost() != null) {
   1984                         builder.authority(auth.getHost());
   1985                     }
   1986                     PatternMatcher path = tmpPa.getDataPath(ipath);
   1987                     builder.path(path.getPath());
   1988                     Intent finalIntent = new Intent(intent);
   1989                     finalIntent.setData(builder.build());
   1990                     applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
   1991                             scheme, null, auth, path, null, userId);
   1992                     doAuth = doScheme = false;
   1993                 }
   1994                 if (doAuth) {
   1995                     Uri.Builder builder = new Uri.Builder();
   1996                     builder.scheme(scheme);
   1997                     if (auth.getHost() != null) {
   1998                         builder.authority(auth.getHost());
   1999                     }
   2000                     Intent finalIntent = new Intent(intent);
   2001                     finalIntent.setData(builder.build());
   2002                     applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
   2003                             scheme, null, auth, null, null, userId);
   2004                     doScheme = false;
   2005                 }
   2006             }
   2007             if (doScheme) {
   2008                 Uri.Builder builder = new Uri.Builder();
   2009                 builder.scheme(scheme);
   2010                 Intent finalIntent = new Intent(intent);
   2011                 finalIntent.setData(builder.build());
   2012                 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
   2013                         scheme, null, null, null, null, userId);
   2014             }
   2015             doNonData = false;
   2016         }
   2017 
   2018         for (int idata=0; idata<tmpPa.countDataTypes(); idata++) {
   2019             Intent finalIntent = new Intent(intent);
   2020             String mimeType = tmpPa.getDataType(idata);
   2021             finalIntent.setType(mimeType);
   2022             applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
   2023                     null, null, null, null, mimeType, userId);
   2024             doNonData = false;
   2025         }
   2026 
   2027         if (doNonData) {
   2028             applyDefaultPreferredActivityLPw(service, intent, flags, cn,
   2029                     null, null, null, null, null, userId);
   2030         }
   2031     }
   2032 
   2033     private void applyDefaultPreferredActivityLPw(PackageManagerService service,
   2034             Intent intent, int flags, ComponentName cn, String scheme, PatternMatcher ssp,
   2035             IntentFilter.AuthorityEntry auth, PatternMatcher path, String mimeType,
   2036             int userId) {
   2037         List<ResolveInfo> ri = service.mActivities.queryIntent(intent,
   2038                 intent.getType(), flags, 0);
   2039         if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Queried " + intent
   2040                 + " results: " + ri);
   2041         int match = 0;
   2042         if (ri != null && ri.size() > 1) {
   2043             boolean haveAct = false;
   2044             boolean haveNonSys = false;
   2045             ComponentName[] set = new ComponentName[ri.size()];
   2046             for (int i=0; i<ri.size(); i++) {
   2047                 ActivityInfo ai = ri.get(i).activityInfo;
   2048                 set[i] = new ComponentName(ai.packageName, ai.name);
   2049                 if ((ai.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) {
   2050                     // If any of the matches are not system apps, then
   2051                     // there is a third party app that is now an option...
   2052                     // so don't set a default since we don't want to hide it.
   2053                     if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result "
   2054                             + ai.packageName + "/" + ai.name + ": non-system!");
   2055                     haveNonSys = true;
   2056                     break;
   2057                 } else if (cn.getPackageName().equals(ai.packageName)
   2058                         && cn.getClassName().equals(ai.name)) {
   2059                     if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result "
   2060                             + ai.packageName + "/" + ai.name + ": default!");
   2061                     haveAct = true;
   2062                     match = ri.get(i).match;
   2063                 } else {
   2064                     if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result "
   2065                             + ai.packageName + "/" + ai.name + ": skipped");
   2066                 }
   2067             }
   2068             if (haveAct && !haveNonSys) {
   2069                 IntentFilter filter = new IntentFilter();
   2070                 if (intent.getAction() != null) {
   2071                     filter.addAction(intent.getAction());
   2072                 }
   2073                 for (String cat : intent.getCategories()) {
   2074                     filter.addCategory(cat);
   2075                 }
   2076                 if ((flags&PackageManager.MATCH_DEFAULT_ONLY) != 0) {
   2077                     filter.addCategory(Intent.CATEGORY_DEFAULT);
   2078                 }
   2079                 if (scheme != null) {
   2080                     filter.addDataScheme(scheme);
   2081                 }
   2082                 if (ssp != null) {
   2083                     filter.addDataSchemeSpecificPart(ssp.getPath(), ssp.getType());
   2084                 }
   2085                 if (auth != null) {
   2086                     filter.addDataAuthority(auth);
   2087                 }
   2088                 if (path != null) {
   2089                     filter.addDataPath(path);
   2090                 }
   2091                 PreferredActivity pa = new PreferredActivity(filter, match, set, cn, true);
   2092                 editPreferredActivitiesLPw(userId).addFilter(pa);
   2093             } else if (!haveNonSys) {
   2094                 Slog.w(TAG, "No component found for default preferred activity " + cn);
   2095             }
   2096         }
   2097     }
   2098 
   2099     private void readDefaultPreferredActivitiesLPw(PackageManagerService service,
   2100             XmlPullParser parser, int userId)
   2101             throws XmlPullParserException, IOException {
   2102         int outerDepth = parser.getDepth();
   2103         int type;
   2104         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
   2105                 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
   2106             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   2107                 continue;
   2108             }
   2109 
   2110             String tagName = parser.getName();
   2111             if (tagName.equals(TAG_ITEM)) {
   2112                 PreferredActivity tmpPa = new PreferredActivity(parser);
   2113                 if (tmpPa.mPref.getParseError() == null) {
   2114                     applyDefaultPreferredActivityLPw(service, tmpPa, tmpPa.mPref.mComponent,
   2115                             userId);
   2116                 } else {
   2117                     PackageManagerService.reportSettingsProblem(Log.WARN,
   2118                             "Error in package manager settings: <preferred-activity> "
   2119                                     + tmpPa.mPref.getParseError() + " at "
   2120                                     + parser.getPositionDescription());
   2121                 }
   2122             } else {
   2123                 PackageManagerService.reportSettingsProblem(Log.WARN,
   2124                         "Unknown element under <preferred-activities>: " + parser.getName());
   2125                 XmlUtils.skipCurrentTag(parser);
   2126             }
   2127         }
   2128     }
   2129 
   2130     private int readInt(XmlPullParser parser, String ns, String name, int defValue) {
   2131         String v = parser.getAttributeValue(ns, name);
   2132         try {
   2133             if (v == null) {
   2134                 return defValue;
   2135             }
   2136             return Integer.parseInt(v);
   2137         } catch (NumberFormatException e) {
   2138             PackageManagerService.reportSettingsProblem(Log.WARN,
   2139                     "Error in package manager settings: attribute " + name
   2140                             + " has bad integer value " + v + " at "
   2141                             + parser.getPositionDescription());
   2142         }
   2143         return defValue;
   2144     }
   2145 
   2146     private void readPermissionsLPw(HashMap<String, BasePermission> out, XmlPullParser parser)
   2147             throws IOException, XmlPullParserException {
   2148         int outerDepth = parser.getDepth();
   2149         int type;
   2150         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
   2151                 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
   2152             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   2153                 continue;
   2154             }
   2155 
   2156             final String tagName = parser.getName();
   2157             if (tagName.equals(TAG_ITEM)) {
   2158                 final String name = parser.getAttributeValue(null, ATTR_NAME);
   2159                 final String sourcePackage = parser.getAttributeValue(null, "package");
   2160                 final String ptype = parser.getAttributeValue(null, "type");
   2161                 if (name != null && sourcePackage != null) {
   2162                     final boolean dynamic = "dynamic".equals(ptype);
   2163                     final BasePermission bp = new BasePermission(name, sourcePackage,
   2164                             dynamic ? BasePermission.TYPE_DYNAMIC : BasePermission.TYPE_NORMAL);
   2165                     bp.protectionLevel = readInt(parser, null, "protection",
   2166                             PermissionInfo.PROTECTION_NORMAL);
   2167                     bp.protectionLevel = PermissionInfo.fixProtectionLevel(bp.protectionLevel);
   2168                     if (dynamic) {
   2169                         PermissionInfo pi = new PermissionInfo();
   2170                         pi.packageName = sourcePackage.intern();
   2171                         pi.name = name.intern();
   2172                         pi.icon = readInt(parser, null, "icon", 0);
   2173                         pi.nonLocalizedLabel = parser.getAttributeValue(null, "label");
   2174                         pi.protectionLevel = bp.protectionLevel;
   2175                         bp.pendingInfo = pi;
   2176                     }
   2177                     out.put(bp.name, bp);
   2178                 } else {
   2179                     PackageManagerService.reportSettingsProblem(Log.WARN,
   2180                             "Error in package manager settings: permissions has" + " no name at "
   2181                                     + parser.getPositionDescription());
   2182                 }
   2183             } else {
   2184                 PackageManagerService.reportSettingsProblem(Log.WARN,
   2185                         "Unknown element reading permissions: " + parser.getName() + " at "
   2186                                 + parser.getPositionDescription());
   2187             }
   2188             XmlUtils.skipCurrentTag(parser);
   2189         }
   2190     }
   2191 
   2192     private void readDisabledSysPackageLPw(XmlPullParser parser) throws XmlPullParserException,
   2193             IOException {
   2194         String name = parser.getAttributeValue(null, ATTR_NAME);
   2195         String realName = parser.getAttributeValue(null, "realName");
   2196         String codePathStr = parser.getAttributeValue(null, "codePath");
   2197         String resourcePathStr = parser.getAttributeValue(null, "resourcePath");
   2198         String nativeLibraryPathStr = parser.getAttributeValue(null, "nativeLibraryPath");
   2199         if (resourcePathStr == null) {
   2200             resourcePathStr = codePathStr;
   2201         }
   2202         String version = parser.getAttributeValue(null, "version");
   2203         int versionCode = 0;
   2204         if (version != null) {
   2205             try {
   2206                 versionCode = Integer.parseInt(version);
   2207             } catch (NumberFormatException e) {
   2208             }
   2209         }
   2210 
   2211         int pkgFlags = 0;
   2212         pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
   2213         final File codePathFile = new File(codePathStr);
   2214         if (PackageManagerService.locationIsPrivileged(codePathFile)) {
   2215             pkgFlags |= ApplicationInfo.FLAG_PRIVILEGED;
   2216         }
   2217         PackageSetting ps = new PackageSetting(name, realName, codePathFile,
   2218                 new File(resourcePathStr), nativeLibraryPathStr, versionCode, pkgFlags);
   2219         String timeStampStr = parser.getAttributeValue(null, "ft");
   2220         if (timeStampStr != null) {
   2221             try {
   2222                 long timeStamp = Long.parseLong(timeStampStr, 16);
   2223                 ps.setTimeStamp(timeStamp);
   2224             } catch (NumberFormatException e) {
   2225             }
   2226         } else {
   2227             timeStampStr = parser.getAttributeValue(null, "ts");
   2228             if (timeStampStr != null) {
   2229                 try {
   2230                     long timeStamp = Long.parseLong(timeStampStr);
   2231                     ps.setTimeStamp(timeStamp);
   2232                 } catch (NumberFormatException e) {
   2233                 }
   2234             }
   2235         }
   2236         timeStampStr = parser.getAttributeValue(null, "it");
   2237         if (timeStampStr != null) {
   2238             try {
   2239                 ps.firstInstallTime = Long.parseLong(timeStampStr, 16);
   2240             } catch (NumberFormatException e) {
   2241             }
   2242         }
   2243         timeStampStr = parser.getAttributeValue(null, "ut");
   2244         if (timeStampStr != null) {
   2245             try {
   2246                 ps.lastUpdateTime = Long.parseLong(timeStampStr, 16);
   2247             } catch (NumberFormatException e) {
   2248             }
   2249         }
   2250         String idStr = parser.getAttributeValue(null, "userId");
   2251         ps.appId = idStr != null ? Integer.parseInt(idStr) : 0;
   2252         if (ps.appId <= 0) {
   2253             String sharedIdStr = parser.getAttributeValue(null, "sharedUserId");
   2254             ps.appId = sharedIdStr != null ? Integer.parseInt(sharedIdStr) : 0;
   2255         }
   2256         int outerDepth = parser.getDepth();
   2257         int type;
   2258         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
   2259                 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
   2260             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   2261                 continue;
   2262             }
   2263 
   2264             String tagName = parser.getName();
   2265             if (tagName.equals("perms")) {
   2266                 readGrantedPermissionsLPw(parser, ps.grantedPermissions);
   2267             } else {
   2268                 PackageManagerService.reportSettingsProblem(Log.WARN,
   2269                         "Unknown element under <updated-package>: " + parser.getName());
   2270                 XmlUtils.skipCurrentTag(parser);
   2271             }
   2272         }
   2273 
   2274         mDisabledSysPackages.put(name, ps);
   2275     }
   2276 
   2277     private void readPackageLPw(XmlPullParser parser) throws XmlPullParserException, IOException {
   2278         String name = null;
   2279         String realName = null;
   2280         String idStr = null;
   2281         String sharedIdStr = null;
   2282         String codePathStr = null;
   2283         String resourcePathStr = null;
   2284         String nativeLibraryPathStr = null;
   2285         String systemStr = null;
   2286         String installerPackageName = null;
   2287         String uidError = null;
   2288         int pkgFlags = 0;
   2289         long timeStamp = 0;
   2290         long firstInstallTime = 0;
   2291         long lastUpdateTime = 0;
   2292         PackageSettingBase packageSetting = null;
   2293         String version = null;
   2294         int versionCode = 0;
   2295         try {
   2296             name = parser.getAttributeValue(null, ATTR_NAME);
   2297             realName = parser.getAttributeValue(null, "realName");
   2298             idStr = parser.getAttributeValue(null, "userId");
   2299             uidError = parser.getAttributeValue(null, "uidError");
   2300             sharedIdStr = parser.getAttributeValue(null, "sharedUserId");
   2301             codePathStr = parser.getAttributeValue(null, "codePath");
   2302             resourcePathStr = parser.getAttributeValue(null, "resourcePath");
   2303             nativeLibraryPathStr = parser.getAttributeValue(null, "nativeLibraryPath");
   2304             version = parser.getAttributeValue(null, "version");
   2305             if (version != null) {
   2306                 try {
   2307                     versionCode = Integer.parseInt(version);
   2308                 } catch (NumberFormatException e) {
   2309                 }
   2310             }
   2311             installerPackageName = parser.getAttributeValue(null, "installer");
   2312 
   2313             systemStr = parser.getAttributeValue(null, "flags");
   2314             if (systemStr != null) {
   2315                 try {
   2316                     pkgFlags = Integer.parseInt(systemStr);
   2317                 } catch (NumberFormatException e) {
   2318                 }
   2319             } else {
   2320                 // For backward compatibility
   2321                 systemStr = parser.getAttributeValue(null, "system");
   2322                 if (systemStr != null) {
   2323                     pkgFlags |= ("true".equalsIgnoreCase(systemStr)) ? ApplicationInfo.FLAG_SYSTEM
   2324                             : 0;
   2325                 } else {
   2326                     // Old settings that don't specify system... just treat
   2327                     // them as system, good enough.
   2328                     pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
   2329                 }
   2330             }
   2331             String timeStampStr = parser.getAttributeValue(null, "ft");
   2332             if (timeStampStr != null) {
   2333                 try {
   2334                     timeStamp = Long.parseLong(timeStampStr, 16);
   2335                 } catch (NumberFormatException e) {
   2336                 }
   2337             } else {
   2338                 timeStampStr = parser.getAttributeValue(null, "ts");
   2339                 if (timeStampStr != null) {
   2340                     try {
   2341                         timeStamp = Long.parseLong(timeStampStr);
   2342                     } catch (NumberFormatException e) {
   2343                     }
   2344                 }
   2345             }
   2346             timeStampStr = parser.getAttributeValue(null, "it");
   2347             if (timeStampStr != null) {
   2348                 try {
   2349                     firstInstallTime = Long.parseLong(timeStampStr, 16);
   2350                 } catch (NumberFormatException e) {
   2351                 }
   2352             }
   2353             timeStampStr = parser.getAttributeValue(null, "ut");
   2354             if (timeStampStr != null) {
   2355                 try {
   2356                     lastUpdateTime = Long.parseLong(timeStampStr, 16);
   2357                 } catch (NumberFormatException e) {
   2358                 }
   2359             }
   2360             if (PackageManagerService.DEBUG_SETTINGS)
   2361                 Log.v(PackageManagerService.TAG, "Reading package: " + name + " userId=" + idStr
   2362                         + " sharedUserId=" + sharedIdStr);
   2363             int userId = idStr != null ? Integer.parseInt(idStr) : 0;
   2364             if (resourcePathStr == null) {
   2365                 resourcePathStr = codePathStr;
   2366             }
   2367             if (realName != null) {
   2368                 realName = realName.intern();
   2369             }
   2370             if (name == null) {
   2371                 PackageManagerService.reportSettingsProblem(Log.WARN,
   2372                         "Error in package manager settings: <package> has no name at "
   2373                                 + parser.getPositionDescription());
   2374             } else if (codePathStr == null) {
   2375                 PackageManagerService.reportSettingsProblem(Log.WARN,
   2376                         "Error in package manager settings: <package> has no codePath at "
   2377                                 + parser.getPositionDescription());
   2378             } else if (userId > 0) {
   2379                 packageSetting = addPackageLPw(name.intern(), realName, new File(codePathStr),
   2380                         new File(resourcePathStr), nativeLibraryPathStr, userId, versionCode,
   2381                         pkgFlags);
   2382                 if (PackageManagerService.DEBUG_SETTINGS)
   2383                     Log.i(PackageManagerService.TAG, "Reading package " + name + ": userId="
   2384                             + userId + " pkg=" + packageSetting);
   2385                 if (packageSetting == null) {
   2386                     PackageManagerService.reportSettingsProblem(Log.ERROR, "Failure adding uid "
   2387                             + userId + " while parsing settings at "
   2388                             + parser.getPositionDescription());
   2389                 } else {
   2390                     packageSetting.setTimeStamp(timeStamp);
   2391                     packageSetting.firstInstallTime = firstInstallTime;
   2392                     packageSetting.lastUpdateTime = lastUpdateTime;
   2393                 }
   2394             } else if (sharedIdStr != null) {
   2395                 userId = sharedIdStr != null ? Integer.parseInt(sharedIdStr) : 0;
   2396                 if (userId > 0) {
   2397                     packageSetting = new PendingPackage(name.intern(), realName, new File(
   2398                             codePathStr), new File(resourcePathStr), nativeLibraryPathStr, userId,
   2399                             versionCode, pkgFlags);
   2400                     packageSetting.setTimeStamp(timeStamp);
   2401                     packageSetting.firstInstallTime = firstInstallTime;
   2402                     packageSetting.lastUpdateTime = lastUpdateTime;
   2403                     mPendingPackages.add((PendingPackage) packageSetting);
   2404                     if (PackageManagerService.DEBUG_SETTINGS)
   2405                         Log.i(PackageManagerService.TAG, "Reading package " + name
   2406                                 + ": sharedUserId=" + userId + " pkg=" + packageSetting);
   2407                 } else {
   2408                     PackageManagerService.reportSettingsProblem(Log.WARN,
   2409                             "Error in package manager settings: package " + name
   2410                                     + " has bad sharedId " + sharedIdStr + " at "
   2411                                     + parser.getPositionDescription());
   2412                 }
   2413             } else {
   2414                 PackageManagerService.reportSettingsProblem(Log.WARN,
   2415                         "Error in package manager settings: package " + name + " has bad userId "
   2416                                 + idStr + " at " + parser.getPositionDescription());
   2417             }
   2418         } catch (NumberFormatException e) {
   2419             PackageManagerService.reportSettingsProblem(Log.WARN,
   2420                     "Error in package manager settings: package " + name + " has bad userId "
   2421                             + idStr + " at " + parser.getPositionDescription());
   2422         }
   2423         if (packageSetting != null) {
   2424             packageSetting.uidError = "true".equals(uidError);
   2425             packageSetting.installerPackageName = installerPackageName;
   2426             packageSetting.nativeLibraryPathString = nativeLibraryPathStr;
   2427             // Handle legacy string here for single-user mode
   2428             final String enabledStr = parser.getAttributeValue(null, ATTR_ENABLED);
   2429             if (enabledStr != null) {
   2430                 try {
   2431                     packageSetting.setEnabled(Integer.parseInt(enabledStr), 0 /* userId */, null);
   2432                 } catch (NumberFormatException e) {
   2433                     if (enabledStr.equalsIgnoreCase("true")) {
   2434                         packageSetting.setEnabled(COMPONENT_ENABLED_STATE_ENABLED, 0, null);
   2435                     } else if (enabledStr.equalsIgnoreCase("false")) {
   2436                         packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DISABLED, 0, null);
   2437                     } else if (enabledStr.equalsIgnoreCase("default")) {
   2438                         packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DEFAULT, 0, null);
   2439                     } else {
   2440                         PackageManagerService.reportSettingsProblem(Log.WARN,
   2441                                 "Error in package manager settings: package " + name
   2442                                         + " has bad enabled value: " + idStr + " at "
   2443                                         + parser.getPositionDescription());
   2444                     }
   2445                 }
   2446             } else {
   2447                 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DEFAULT, 0, null);
   2448             }
   2449 
   2450             final String installStatusStr = parser.getAttributeValue(null, "installStatus");
   2451             if (installStatusStr != null) {
   2452                 if (installStatusStr.equalsIgnoreCase("false")) {
   2453                     packageSetting.installStatus = PackageSettingBase.PKG_INSTALL_INCOMPLETE;
   2454                 } else {
   2455                     packageSetting.installStatus = PackageSettingBase.PKG_INSTALL_COMPLETE;
   2456                 }
   2457             }
   2458 
   2459             int outerDepth = parser.getDepth();
   2460             int type;
   2461             while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
   2462                     && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
   2463                 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   2464                     continue;
   2465                 }
   2466 
   2467                 String tagName = parser.getName();
   2468                 // Legacy
   2469                 if (tagName.equals(TAG_DISABLED_COMPONENTS)) {
   2470                     readDisabledComponentsLPw(packageSetting, parser, 0);
   2471                 } else if (tagName.equals(TAG_ENABLED_COMPONENTS)) {
   2472                     readEnabledComponentsLPw(packageSetting, parser, 0);
   2473                 } else if (tagName.equals("sigs")) {
   2474                     packageSetting.signatures.readXml(parser, mPastSignatures);
   2475                 } else if (tagName.equals("perms")) {
   2476                     readGrantedPermissionsLPw(parser, packageSetting.grantedPermissions);
   2477                     packageSetting.permissionsFixed = true;
   2478                 } else if (tagName.equals("signing-keyset")) {
   2479                     long id = Long.parseLong(parser.getAttributeValue(null, "identifier"));
   2480                     packageSetting.keySetData.addSigningKeySet(id);
   2481                     if (false) Slog.d(TAG, "Adding signing keyset " + Long.toString(id)
   2482                             + " to " + name);
   2483                 } else if (tagName.equals("defined-keyset")) {
   2484                     long id = Long.parseLong(parser.getAttributeValue(null, "identifier"));
   2485                     String alias = parser.getAttributeValue(null, "alias");
   2486                     packageSetting.keySetData.addDefinedKeySet(id, alias);
   2487                 } else {
   2488                     PackageManagerService.reportSettingsProblem(Log.WARN,
   2489                             "Unknown element under <package>: " + parser.getName());
   2490                     XmlUtils.skipCurrentTag(parser);
   2491                 }
   2492             }
   2493 
   2494 
   2495         } else {
   2496             XmlUtils.skipCurrentTag(parser);
   2497         }
   2498     }
   2499 
   2500     private void readDisabledComponentsLPw(PackageSettingBase packageSetting, XmlPullParser parser,
   2501             int userId) throws IOException, XmlPullParserException {
   2502         int outerDepth = parser.getDepth();
   2503         int type;
   2504         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
   2505                 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
   2506             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   2507                 continue;
   2508             }
   2509 
   2510             String tagName = parser.getName();
   2511             if (tagName.equals(TAG_ITEM)) {
   2512                 String name = parser.getAttributeValue(null, ATTR_NAME);
   2513                 if (name != null) {
   2514                     packageSetting.addDisabledComponent(name.intern(), userId);
   2515                 } else {
   2516                     PackageManagerService.reportSettingsProblem(Log.WARN,
   2517                             "Error in package manager settings: <disabled-components> has"
   2518                                     + " no name at " + parser.getPositionDescription());
   2519                 }
   2520             } else {
   2521                 PackageManagerService.reportSettingsProblem(Log.WARN,
   2522                         "Unknown element under <disabled-components>: " + parser.getName());
   2523             }
   2524             XmlUtils.skipCurrentTag(parser);
   2525         }
   2526     }
   2527 
   2528     private void readEnabledComponentsLPw(PackageSettingBase packageSetting, XmlPullParser parser,
   2529             int userId) throws IOException, XmlPullParserException {
   2530         int outerDepth = parser.getDepth();
   2531         int type;
   2532         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
   2533                 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
   2534             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   2535                 continue;
   2536             }
   2537 
   2538             String tagName = parser.getName();
   2539             if (tagName.equals(TAG_ITEM)) {
   2540                 String name = parser.getAttributeValue(null, ATTR_NAME);
   2541                 if (name != null) {
   2542                     packageSetting.addEnabledComponent(name.intern(), userId);
   2543                 } else {
   2544                     PackageManagerService.reportSettingsProblem(Log.WARN,
   2545                             "Error in package manager settings: <enabled-components> has"
   2546                                     + " no name at " + parser.getPositionDescription());
   2547                 }
   2548             } else {
   2549                 PackageManagerService.reportSettingsProblem(Log.WARN,
   2550                         "Unknown element under <enabled-components>: " + parser.getName());
   2551             }
   2552             XmlUtils.skipCurrentTag(parser);
   2553         }
   2554     }
   2555 
   2556     private void readSharedUserLPw(XmlPullParser parser) throws XmlPullParserException,IOException {
   2557         String name = null;
   2558         String idStr = null;
   2559         int pkgFlags = 0;
   2560         SharedUserSetting su = null;
   2561         try {
   2562             name = parser.getAttributeValue(null, ATTR_NAME);
   2563             idStr = parser.getAttributeValue(null, "userId");
   2564             int userId = idStr != null ? Integer.parseInt(idStr) : 0;
   2565             if ("true".equals(parser.getAttributeValue(null, "system"))) {
   2566                 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
   2567             }
   2568             if (name == null) {
   2569                 PackageManagerService.reportSettingsProblem(Log.WARN,
   2570                         "Error in package manager settings: <shared-user> has no name at "
   2571                                 + parser.getPositionDescription());
   2572             } else if (userId == 0) {
   2573                 PackageManagerService.reportSettingsProblem(Log.WARN,
   2574                         "Error in package manager settings: shared-user " + name
   2575                                 + " has bad userId " + idStr + " at "
   2576                                 + parser.getPositionDescription());
   2577             } else {
   2578                 if ((su = addSharedUserLPw(name.intern(), userId, pkgFlags)) == null) {
   2579                     PackageManagerService
   2580                             .reportSettingsProblem(Log.ERROR, "Occurred while parsing settings at "
   2581                                     + parser.getPositionDescription());
   2582                 }
   2583             }
   2584         } catch (NumberFormatException e) {
   2585             PackageManagerService.reportSettingsProblem(Log.WARN,
   2586                     "Error in package manager settings: package " + name + " has bad userId "
   2587                             + idStr + " at " + parser.getPositionDescription());
   2588         }
   2589         ;
   2590 
   2591         if (su != null) {
   2592             int outerDepth = parser.getDepth();
   2593             int type;
   2594             while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
   2595                     && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
   2596                 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   2597                     continue;
   2598                 }
   2599 
   2600                 String tagName = parser.getName();
   2601                 if (tagName.equals("sigs")) {
   2602                     su.signatures.readXml(parser, mPastSignatures);
   2603                 } else if (tagName.equals("perms")) {
   2604                     readGrantedPermissionsLPw(parser, su.grantedPermissions);
   2605                 } else {
   2606                     PackageManagerService.reportSettingsProblem(Log.WARN,
   2607                             "Unknown element under <shared-user>: " + parser.getName());
   2608                     XmlUtils.skipCurrentTag(parser);
   2609                 }
   2610             }
   2611 
   2612         } else {
   2613             XmlUtils.skipCurrentTag(parser);
   2614         }
   2615     }
   2616 
   2617     private void readGrantedPermissionsLPw(XmlPullParser parser, HashSet<String> outPerms)
   2618             throws IOException, XmlPullParserException {
   2619         int outerDepth = parser.getDepth();
   2620         int type;
   2621         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
   2622                 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
   2623             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
   2624                 continue;
   2625             }
   2626 
   2627             String tagName = parser.getName();
   2628             if (tagName.equals(TAG_ITEM)) {
   2629                 String name = parser.getAttributeValue(null, ATTR_NAME);
   2630                 if (name != null) {
   2631                     outPerms.add(name.intern());
   2632                 } else {
   2633                     PackageManagerService.reportSettingsProblem(Log.WARN,
   2634                             "Error in package manager settings: <perms> has" + " no name at "
   2635                                     + parser.getPositionDescription());
   2636                 }
   2637             } else {
   2638                 PackageManagerService.reportSettingsProblem(Log.WARN,
   2639                         "Unknown element under <perms>: " + parser.getName());
   2640             }
   2641             XmlUtils.skipCurrentTag(parser);
   2642         }
   2643     }
   2644 
   2645     void createNewUserLILPw(PackageManagerService service, Installer installer,
   2646             int userHandle, File path) {
   2647         path.mkdir();
   2648         FileUtils.setPermissions(path.toString(), FileUtils.S_IRWXU | FileUtils.S_IRWXG
   2649                 | FileUtils.S_IXOTH, -1, -1);
   2650         for (PackageSetting ps : mPackages.values()) {
   2651             // Only system apps are initially installed.
   2652             ps.setInstalled((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) != 0, userHandle);
   2653             // Need to create a data directory for all apps under this user.
   2654             installer.createUserData(ps.name,
   2655                     UserHandle.getUid(userHandle, ps.appId), userHandle);
   2656         }
   2657         readDefaultPreferredAppsLPw(service, userHandle);
   2658         writePackageRestrictionsLPr(userHandle);
   2659     }
   2660 
   2661     void removeUserLPr(int userId) {
   2662         Set<Entry<String, PackageSetting>> entries = mPackages.entrySet();
   2663         for (Entry<String, PackageSetting> entry : entries) {
   2664             entry.getValue().removeUser(userId);
   2665         }
   2666         mPreferredActivities.remove(userId);
   2667         File file = getUserPackagesStateFile(userId);
   2668         file.delete();
   2669         file = getUserPackagesStateBackupFile(userId);
   2670         file.delete();
   2671     }
   2672 
   2673     // This should be called (at least) whenever an application is removed
   2674     private void setFirstAvailableUid(int uid) {
   2675         if (uid > mFirstAvailableUid) {
   2676             mFirstAvailableUid = uid;
   2677         }
   2678     }
   2679 
   2680     // Returns -1 if we could not find an available UserId to assign
   2681     private int newUserIdLPw(Object obj) {
   2682         // Let's be stupidly inefficient for now...
   2683         final int N = mUserIds.size();
   2684         for (int i = mFirstAvailableUid; i < N; i++) {
   2685             if (mUserIds.get(i) == null) {
   2686                 mUserIds.set(i, obj);
   2687                 return Process.FIRST_APPLICATION_UID + i;
   2688             }
   2689         }
   2690 
   2691         // None left?
   2692         if (N > (Process.LAST_APPLICATION_UID-Process.FIRST_APPLICATION_UID)) {
   2693             return -1;
   2694         }
   2695 
   2696         mUserIds.add(obj);
   2697         return Process.FIRST_APPLICATION_UID + N;
   2698     }
   2699 
   2700     public VerifierDeviceIdentity getVerifierDeviceIdentityLPw() {
   2701         if (mVerifierDeviceIdentity == null) {
   2702             mVerifierDeviceIdentity = VerifierDeviceIdentity.generate();
   2703 
   2704             writeLPr();
   2705         }
   2706 
   2707         return mVerifierDeviceIdentity;
   2708     }
   2709 
   2710     public PackageSetting getDisabledSystemPkgLPr(String name) {
   2711         PackageSetting ps = mDisabledSysPackages.get(name);
   2712         return ps;
   2713     }
   2714 
   2715     private String compToString(HashSet<String> cmp) {
   2716         return cmp != null ? Arrays.toString(cmp.toArray()) : "[]";
   2717     }
   2718 
   2719     boolean isEnabledLPr(ComponentInfo componentInfo, int flags, int userId) {
   2720         if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
   2721             return true;
   2722         }
   2723         final String pkgName = componentInfo.packageName;
   2724         final PackageSetting packageSettings = mPackages.get(pkgName);
   2725         if (PackageManagerService.DEBUG_SETTINGS) {
   2726             Log.v(PackageManagerService.TAG, "isEnabledLock - packageName = "
   2727                     + componentInfo.packageName + " componentName = " + componentInfo.name);
   2728             Log.v(PackageManagerService.TAG, "enabledComponents: "
   2729                     + compToString(packageSettings.getEnabledComponents(userId)));
   2730             Log.v(PackageManagerService.TAG, "disabledComponents: "
   2731                     + compToString(packageSettings.getDisabledComponents(userId)));
   2732         }
   2733         if (packageSettings == null) {
   2734             return false;
   2735         }
   2736         PackageUserState ustate = packageSettings.readUserState(userId);
   2737         if ((flags&PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS) != 0) {
   2738             if (ustate.enabled == COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
   2739                 return true;
   2740             }
   2741         }
   2742         if (ustate.enabled == COMPONENT_ENABLED_STATE_DISABLED
   2743                 || ustate.enabled == COMPONENT_ENABLED_STATE_DISABLED_USER
   2744                 || ustate.enabled == COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED
   2745                 || (packageSettings.pkg != null && !packageSettings.pkg.applicationInfo.enabled
   2746                     && ustate.enabled == COMPONENT_ENABLED_STATE_DEFAULT)) {
   2747             return false;
   2748         }
   2749         if (ustate.enabledComponents != null
   2750                 && ustate.enabledComponents.contains(componentInfo.name)) {
   2751             return true;
   2752         }
   2753         if (ustate.disabledComponents != null
   2754                 && ustate.disabledComponents.contains(componentInfo.name)) {
   2755             return false;
   2756         }
   2757         return componentInfo.enabled;
   2758     }
   2759 
   2760     String getInstallerPackageNameLPr(String packageName) {
   2761         final PackageSetting pkg = mPackages.get(packageName);
   2762         if (pkg == null) {
   2763             throw new IllegalArgumentException("Unknown package: " + packageName);
   2764         }
   2765         return pkg.installerPackageName;
   2766     }
   2767 
   2768     int getApplicationEnabledSettingLPr(String packageName, int userId) {
   2769         final PackageSetting pkg = mPackages.get(packageName);
   2770         if (pkg == null) {
   2771             throw new IllegalArgumentException("Unknown package: " + packageName);
   2772         }
   2773         return pkg.getEnabled(userId);
   2774     }
   2775 
   2776     int getComponentEnabledSettingLPr(ComponentName componentName, int userId) {
   2777         final String packageName = componentName.getPackageName();
   2778         final PackageSetting pkg = mPackages.get(packageName);
   2779         if (pkg == null) {
   2780             throw new IllegalArgumentException("Unknown component: " + componentName);
   2781         }
   2782         final String classNameStr = componentName.getClassName();
   2783         return pkg.getCurrentEnabledStateLPr(classNameStr, userId);
   2784     }
   2785 
   2786     boolean setPackageStoppedStateLPw(String packageName, boolean stopped,
   2787             boolean allowedByPermission, int uid, int userId) {
   2788         int appId = UserHandle.getAppId(uid);
   2789         final PackageSetting pkgSetting = mPackages.get(packageName);
   2790         if (pkgSetting == null) {
   2791             throw new IllegalArgumentException("Unknown package: " + packageName);
   2792         }
   2793         if (!allowedByPermission && (appId != pkgSetting.appId)) {
   2794             throw new SecurityException(
   2795                     "Permission Denial: attempt to change stopped state from pid="
   2796                     + Binder.getCallingPid()
   2797                     + ", uid=" + uid + ", package uid=" + pkgSetting.appId);
   2798         }
   2799         if (DEBUG_STOPPED) {
   2800             if (stopped) {
   2801                 RuntimeException e = new RuntimeException("here");
   2802                 e.fillInStackTrace();
   2803                 Slog.i(TAG, "Stopping package " + packageName, e);
   2804             }
   2805         }
   2806         if (pkgSetting.getStopped(userId) != stopped) {
   2807             pkgSetting.setStopped(stopped, userId);
   2808             // pkgSetting.pkg.mSetStopped = stopped;
   2809             if (pkgSetting.getNotLaunched(userId)) {
   2810                 if (pkgSetting.installerPackageName != null) {
   2811                     PackageManagerService.sendPackageBroadcast(Intent.ACTION_PACKAGE_FIRST_LAUNCH,
   2812                             pkgSetting.name, null,
   2813                             pkgSetting.installerPackageName, null, new int[] {userId});
   2814                 }
   2815                 pkgSetting.setNotLaunched(false, userId);
   2816             }
   2817             return true;
   2818         }
   2819         return false;
   2820     }
   2821 
   2822     private List<UserInfo> getAllUsers() {
   2823         long id = Binder.clearCallingIdentity();
   2824         try {
   2825             return UserManagerService.getInstance().getUsers(false);
   2826         } catch (NullPointerException npe) {
   2827             // packagemanager not yet initialized
   2828         } finally {
   2829             Binder.restoreCallingIdentity(id);
   2830         }
   2831         return null;
   2832     }
   2833 
   2834     static final void printFlags(PrintWriter pw, int val, Object[] spec) {
   2835         pw.print("[ ");
   2836         for (int i=0; i<spec.length; i+=2) {
   2837             int mask = (Integer)spec[i];
   2838             if ((val & mask) != 0) {
   2839                 pw.print(spec[i+1]);
   2840                 pw.print(" ");
   2841             }
   2842         }
   2843         pw.print("]");
   2844     }
   2845 
   2846     static final Object[] FLAG_DUMP_SPEC = new Object[] {
   2847         ApplicationInfo.FLAG_SYSTEM, "SYSTEM",
   2848         ApplicationInfo.FLAG_DEBUGGABLE, "DEBUGGABLE",
   2849         ApplicationInfo.FLAG_HAS_CODE, "HAS_CODE",
   2850         ApplicationInfo.FLAG_PERSISTENT, "PERSISTENT",
   2851         ApplicationInfo.FLAG_FACTORY_TEST, "FACTORY_TEST",
   2852         ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING, "ALLOW_TASK_REPARENTING",
   2853         ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA, "ALLOW_CLEAR_USER_DATA",
   2854         ApplicationInfo.FLAG_UPDATED_SYSTEM_APP, "UPDATED_SYSTEM_APP",
   2855         ApplicationInfo.FLAG_TEST_ONLY, "TEST_ONLY",
   2856         ApplicationInfo.FLAG_VM_SAFE_MODE, "VM_SAFE_MODE",
   2857         ApplicationInfo.FLAG_ALLOW_BACKUP, "ALLOW_BACKUP",
   2858         ApplicationInfo.FLAG_KILL_AFTER_RESTORE, "KILL_AFTER_RESTORE",
   2859         ApplicationInfo.FLAG_RESTORE_ANY_VERSION, "RESTORE_ANY_VERSION",
   2860         ApplicationInfo.FLAG_EXTERNAL_STORAGE, "EXTERNAL_STORAGE",
   2861         ApplicationInfo.FLAG_LARGE_HEAP, "LARGE_HEAP",
   2862         ApplicationInfo.FLAG_PRIVILEGED, "PRIVILEGED",
   2863         ApplicationInfo.FLAG_FORWARD_LOCK, "FORWARD_LOCK",
   2864         ApplicationInfo.FLAG_CANT_SAVE_STATE, "CANT_SAVE_STATE",
   2865     };
   2866 
   2867     void dumpPackageLPr(PrintWriter pw, String prefix, PackageSetting ps, SimpleDateFormat sdf,
   2868             Date date, List<UserInfo> users) {
   2869         pw.print(prefix); pw.print("Package [");
   2870             pw.print(ps.realName != null ? ps.realName : ps.name);
   2871             pw.print("] (");
   2872             pw.print(Integer.toHexString(System.identityHashCode(ps)));
   2873             pw.println("):");
   2874 
   2875         if (ps.realName != null) {
   2876             pw.print(prefix); pw.print("  compat name=");
   2877             pw.println(ps.name);
   2878         }
   2879 
   2880         pw.print(prefix); pw.print("  userId="); pw.print(ps.appId);
   2881                 pw.print(" gids="); pw.println(PackageManagerService.arrayToString(ps.gids));
   2882         if (ps.sharedUser != null) {
   2883             pw.print(prefix); pw.print("  sharedUser="); pw.println(ps.sharedUser);
   2884         }
   2885         pw.print(prefix); pw.print("  pkg="); pw.println(ps.pkg);
   2886         pw.print(prefix); pw.print("  codePath="); pw.println(ps.codePathString);
   2887         pw.print(prefix); pw.print("  resourcePath="); pw.println(ps.resourcePathString);
   2888         pw.print(prefix); pw.print("  nativeLibraryPath="); pw.println(ps.nativeLibraryPathString);
   2889         pw.print(prefix); pw.print("  versionCode="); pw.print(ps.versionCode);
   2890         if (ps.pkg != null) {
   2891             pw.print(" targetSdk="); pw.print(ps.pkg.applicationInfo.targetSdkVersion);
   2892         }
   2893         pw.println();
   2894         if (ps.pkg != null) {
   2895             pw.print(prefix); pw.print("  versionName="); pw.println(ps.pkg.mVersionName);
   2896             pw.print(prefix); pw.print("  applicationInfo=");
   2897                 pw.println(ps.pkg.applicationInfo.toString());
   2898             pw.print(prefix); pw.print("  flags="); printFlags(pw, ps.pkg.applicationInfo.flags,
   2899                     FLAG_DUMP_SPEC); pw.println();
   2900             pw.print(prefix); pw.print("  dataDir="); pw.println(ps.pkg.applicationInfo.dataDir);
   2901             if (ps.pkg.mOperationPending) {
   2902                 pw.print(prefix); pw.println("  mOperationPending=true");
   2903             }
   2904             pw.print(prefix); pw.print("  supportsScreens=[");
   2905             boolean first = true;
   2906             if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS) != 0) {
   2907                 if (!first)
   2908                     pw.print(", ");
   2909                 first = false;
   2910                 pw.print("small");
   2911             }
   2912             if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS) != 0) {
   2913                 if (!first)
   2914                     pw.print(", ");
   2915                 first = false;
   2916                 pw.print("medium");
   2917             }
   2918             if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS) != 0) {
   2919                 if (!first)
   2920                     pw.print(", ");
   2921                 first = false;
   2922                 pw.print("large");
   2923             }
   2924             if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS) != 0) {
   2925                 if (!first)
   2926                     pw.print(", ");
   2927                 first = false;
   2928                 pw.print("xlarge");
   2929             }
   2930             if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS) != 0) {
   2931                 if (!first)
   2932                     pw.print(", ");
   2933                 first = false;
   2934                 pw.print("resizeable");
   2935             }
   2936             if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES) != 0) {
   2937                 if (!first)
   2938                     pw.print(", ");
   2939                 first = false;
   2940                 pw.print("anyDensity");
   2941             }
   2942             pw.println("]");
   2943             if (ps.pkg.libraryNames != null && ps.pkg.libraryNames.size() > 0) {
   2944                 pw.print(prefix); pw.println("  libraries:");
   2945                 for (int i=0; i<ps.pkg.libraryNames.size(); i++) {
   2946                     pw.print(prefix); pw.print("    "); pw.println(ps.pkg.libraryNames.get(i));
   2947                 }
   2948             }
   2949             if (ps.pkg.usesLibraries != null && ps.pkg.usesLibraries.size() > 0) {
   2950                 pw.print(prefix); pw.println("  usesLibraries:");
   2951                 for (int i=0; i<ps.pkg.usesLibraries.size(); i++) {
   2952                     pw.print(prefix); pw.print("    "); pw.println(ps.pkg.usesLibraries.get(i));
   2953                 }
   2954             }
   2955             if (ps.pkg.usesOptionalLibraries != null
   2956                     && ps.pkg.usesOptionalLibraries.size() > 0) {
   2957                 pw.print(prefix); pw.println("  usesOptionalLibraries:");
   2958                 for (int i=0; i<ps.pkg.usesOptionalLibraries.size(); i++) {
   2959                     pw.print(prefix); pw.print("    ");
   2960                         pw.println(ps.pkg.usesOptionalLibraries.get(i));
   2961                 }
   2962             }
   2963             if (ps.pkg.usesLibraryFiles != null
   2964                     && ps.pkg.usesLibraryFiles.length > 0) {
   2965                 pw.print(prefix); pw.println("  usesLibraryFiles:");
   2966                 for (int i=0; i<ps.pkg.usesLibraryFiles.length; i++) {
   2967                     pw.print(prefix); pw.print("    "); pw.println(ps.pkg.usesLibraryFiles[i]);
   2968                 }
   2969             }
   2970         }
   2971         pw.print(prefix); pw.print("  timeStamp=");
   2972             date.setTime(ps.timeStamp);
   2973             pw.println(sdf.format(date));
   2974         pw.print(prefix); pw.print("  firstInstallTime=");
   2975             date.setTime(ps.firstInstallTime);
   2976             pw.println(sdf.format(date));
   2977         pw.print(prefix); pw.print("  lastUpdateTime=");
   2978             date.setTime(ps.lastUpdateTime);
   2979             pw.println(sdf.format(date));
   2980         if (ps.installerPackageName != null) {
   2981             pw.print(prefix); pw.print("  installerPackageName=");
   2982                     pw.println(ps.installerPackageName);
   2983         }
   2984         pw.print(prefix); pw.print("  signatures="); pw.println(ps.signatures);
   2985         pw.print(prefix); pw.print("  permissionsFixed="); pw.print(ps.permissionsFixed);
   2986                 pw.print(" haveGids="); pw.print(ps.haveGids);
   2987                 pw.print(" installStatus="); pw.println(ps.installStatus);
   2988         pw.print(prefix); pw.print("  pkgFlags="); printFlags(pw, ps.pkgFlags, FLAG_DUMP_SPEC);
   2989                 pw.println();
   2990         for (UserInfo user : users) {
   2991             pw.print(prefix); pw.print("  User "); pw.print(user.id); pw.print(": ");
   2992             pw.print(" installed=");
   2993             pw.print(ps.getInstalled(user.id));
   2994             pw.print(" blocked=");
   2995             pw.print(ps.getBlocked(user.id));
   2996             pw.print(" stopped=");
   2997             pw.print(ps.getStopped(user.id));
   2998             pw.print(" notLaunched=");
   2999             pw.print(ps.getNotLaunched(user.id));
   3000             pw.print(" enabled=");
   3001             pw.println(ps.getEnabled(user.id));
   3002             String lastDisabledAppCaller = ps.getLastDisabledAppCaller(user.id);
   3003             if (lastDisabledAppCaller != null) {
   3004                 pw.print(prefix); pw.print("    lastDisabledCaller: ");
   3005                         pw.println(lastDisabledAppCaller);
   3006             }
   3007             HashSet<String> cmp = ps.getDisabledComponents(user.id);
   3008             if (cmp != null && cmp.size() > 0) {
   3009                 pw.print(prefix); pw.println("    disabledComponents:");
   3010                 for (String s : cmp) {
   3011                     pw.print(prefix); pw.print("    "); pw.println(s);
   3012                 }
   3013             }
   3014             cmp = ps.getEnabledComponents(user.id);
   3015             if (cmp != null && cmp.size() > 0) {
   3016                 pw.print(prefix); pw.println("    enabledComponents:");
   3017                 for (String s : cmp) {
   3018                     pw.print(prefix); pw.print("    "); pw.println(s);
   3019                 }
   3020             }
   3021         }
   3022         if (ps.grantedPermissions.size() > 0) {
   3023             pw.print(prefix); pw.println("  grantedPermissions:");
   3024             for (String s : ps.grantedPermissions) {
   3025                 pw.print(prefix); pw.print("    "); pw.println(s);
   3026             }
   3027         }
   3028     }
   3029 
   3030     void dumpPackagesLPr(PrintWriter pw, String packageName, DumpState dumpState) {
   3031         final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   3032         final Date date = new Date();
   3033         boolean printedSomething = false;
   3034         List<UserInfo> users = getAllUsers();
   3035         for (final PackageSetting ps : mPackages.values()) {
   3036             if (packageName != null && !packageName.equals(ps.realName)
   3037                     && !packageName.equals(ps.name)) {
   3038                 continue;
   3039             }
   3040 
   3041             if (packageName != null) {
   3042                 dumpState.setSharedUser(ps.sharedUser);
   3043             }
   3044 
   3045             if (!printedSomething) {
   3046                 if (dumpState.onTitlePrinted())
   3047                     pw.println();
   3048                 pw.println("Packages:");
   3049                 printedSomething = true;
   3050             }
   3051             dumpPackageLPr(pw, "  ", ps, sdf, date, users);
   3052         }
   3053 
   3054         printedSomething = false;
   3055         if (mRenamedPackages.size() > 0) {
   3056             for (final Map.Entry<String, String> e : mRenamedPackages.entrySet()) {
   3057                 if (packageName != null && !packageName.equals(e.getKey())
   3058                         && !packageName.equals(e.getValue())) {
   3059                     continue;
   3060                 }
   3061                 if (!printedSomething) {
   3062                     if (dumpState.onTitlePrinted())
   3063                         pw.println();
   3064                     pw.println("Renamed packages:");
   3065                     printedSomething = true;
   3066                 }
   3067                 pw.print("  ");
   3068                 pw.print(e.getKey());
   3069                 pw.print(" -> ");
   3070                 pw.println(e.getValue());
   3071             }
   3072         }
   3073 
   3074         printedSomething = false;
   3075         if (mDisabledSysPackages.size() > 0) {
   3076             for (final PackageSetting ps : mDisabledSysPackages.values()) {
   3077                 if (packageName != null && !packageName.equals(ps.realName)
   3078                         && !packageName.equals(ps.name)) {
   3079                     continue;
   3080                 }
   3081                 if (!printedSomething) {
   3082                     if (dumpState.onTitlePrinted())
   3083                         pw.println();
   3084                     pw.println("Hidden system packages:");
   3085                     printedSomething = true;
   3086                 }
   3087                 dumpPackageLPr(pw, "  ", ps, sdf, date, users);
   3088             }
   3089         }
   3090     }
   3091 
   3092     void dumpPermissionsLPr(PrintWriter pw, String packageName, DumpState dumpState) {
   3093         boolean printedSomething = false;
   3094         for (BasePermission p : mPermissions.values()) {
   3095             if (packageName != null && !packageName.equals(p.sourcePackage)) {
   3096                 continue;
   3097             }
   3098             if (!printedSomething) {
   3099                 if (dumpState.onTitlePrinted())
   3100                     pw.println();
   3101                 pw.println("Permissions:");
   3102                 printedSomething = true;
   3103             }
   3104             pw.print("  Permission ["); pw.print(p.name); pw.print("] (");
   3105                     pw.print(Integer.toHexString(System.identityHashCode(p)));
   3106                     pw.println("):");
   3107             pw.print("    sourcePackage="); pw.println(p.sourcePackage);
   3108             pw.print("    uid="); pw.print(p.uid);
   3109                     pw.print(" gids="); pw.print(PackageManagerService.arrayToString(p.gids));
   3110                     pw.print(" type="); pw.print(p.type);
   3111                     pw.print(" prot=");
   3112                     pw.println(PermissionInfo.protectionToString(p.protectionLevel));
   3113             if (p.packageSetting != null) {
   3114                 pw.print("    packageSetting="); pw.println(p.packageSetting);
   3115             }
   3116             if (p.perm != null) {
   3117                 pw.print("    perm="); pw.println(p.perm);
   3118             }
   3119             if (READ_EXTERNAL_STORAGE.equals(p.name)) {
   3120                 pw.print("    enforced=");
   3121                 pw.println(mReadExternalStorageEnforced);
   3122             }
   3123         }
   3124     }
   3125 
   3126     void dumpSharedUsersLPr(PrintWriter pw, String packageName, DumpState dumpState) {
   3127         boolean printedSomething = false;
   3128         for (SharedUserSetting su : mSharedUsers.values()) {
   3129             if (packageName != null && su != dumpState.getSharedUser()) {
   3130                 continue;
   3131             }
   3132             if (!printedSomething) {
   3133                 if (dumpState.onTitlePrinted())
   3134                     pw.println();
   3135                 pw.println("Shared users:");
   3136                 printedSomething = true;
   3137             }
   3138             pw.print("  SharedUser [");
   3139             pw.print(su.name);
   3140             pw.print("] (");
   3141             pw.print(Integer.toHexString(System.identityHashCode(su)));
   3142                     pw.println("):");
   3143             pw.print("    userId=");
   3144             pw.print(su.userId);
   3145             pw.print(" gids=");
   3146             pw.println(PackageManagerService.arrayToString(su.gids));
   3147             pw.println("    grantedPermissions:");
   3148             for (String s : su.grantedPermissions) {
   3149                 pw.print("      ");
   3150                 pw.println(s);
   3151             }
   3152         }
   3153     }
   3154 
   3155     void dumpReadMessagesLPr(PrintWriter pw, DumpState dumpState) {
   3156         pw.println("Settings parse messages:");
   3157         pw.print(mReadMessages.toString());
   3158     }
   3159 }
   3160