Settings.java revision 5217cacbd9f382068bb9e176cd5a0b15388a335c
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 17package com.android.server.pm; 18 19import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; 20import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED; 21import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED; 22import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER; 23import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED; 24import static android.Manifest.permission.READ_EXTERNAL_STORAGE; 25import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS; 26import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED; 27import static android.os.Process.SYSTEM_UID; 28import static android.os.Process.PACKAGE_INFO_GID; 29import static com.android.server.pm.PackageManagerService.DEBUG_DOMAIN_VERIFICATION; 30 31import android.annotation.NonNull; 32import android.content.IntentFilter; 33import android.content.pm.ActivityInfo; 34import android.content.pm.IntentFilterVerificationInfo; 35import android.content.pm.ResolveInfo; 36import android.net.Uri; 37import android.os.Binder; 38import android.os.Build; 39import android.os.Environment; 40import android.os.FileUtils; 41import android.os.Handler; 42import android.os.Message; 43import android.os.PatternMatcher; 44import android.os.Process; 45import android.os.SystemClock; 46import android.os.UserHandle; 47import android.os.UserManager; 48import android.os.storage.StorageManager; 49import android.os.storage.VolumeInfo; 50import android.util.AtomicFile; 51import android.text.TextUtils; 52import android.util.LogPrinter; 53import android.util.SparseBooleanArray; 54import android.util.SparseLongArray; 55 56import com.android.internal.annotations.GuardedBy; 57import com.android.internal.os.BackgroundThread; 58import com.android.internal.util.ArrayUtils; 59import com.android.internal.util.FastXmlSerializer; 60import com.android.internal.util.IndentingPrintWriter; 61import com.android.internal.util.JournaledFile; 62import com.android.internal.util.Preconditions; 63import com.android.internal.util.XmlUtils; 64import com.android.server.backup.PreferredActivityBackupHelper; 65import com.android.server.pm.PackageManagerService.DumpState; 66import com.android.server.pm.PermissionsState.PermissionState; 67 68import java.io.BufferedInputStream; 69import java.io.FileNotFoundException; 70import java.io.InputStream; 71import java.util.Collection; 72 73import org.xmlpull.v1.XmlPullParser; 74import org.xmlpull.v1.XmlPullParserException; 75import org.xmlpull.v1.XmlSerializer; 76 77import android.content.ComponentName; 78import android.content.Intent; 79import android.content.pm.ApplicationInfo; 80import android.content.pm.ComponentInfo; 81import android.content.pm.PackageCleanItem; 82import android.content.pm.PackageManager; 83import android.content.pm.PackageParser; 84import android.content.pm.PermissionInfo; 85import android.content.pm.Signature; 86import android.content.pm.UserInfo; 87import android.content.pm.PackageUserState; 88import android.content.pm.VerifierDeviceIdentity; 89import android.util.ArrayMap; 90import android.util.ArraySet; 91import android.util.Log; 92import android.util.Slog; 93import android.util.SparseArray; 94import android.util.SparseIntArray; 95import android.util.Xml; 96 97import java.io.BufferedOutputStream; 98import java.io.File; 99import java.io.FileInputStream; 100import java.io.FileOutputStream; 101import java.io.IOException; 102import java.io.PrintWriter; 103import java.nio.charset.StandardCharsets; 104import java.text.SimpleDateFormat; 105import java.util.ArrayList; 106import java.util.Arrays; 107import java.util.Collections; 108import java.util.Date; 109import java.util.Iterator; 110import java.util.List; 111import java.util.Map; 112import java.util.Objects; 113import java.util.Set; 114import java.util.Map.Entry; 115 116import libcore.io.IoUtils; 117 118/** 119 * Holds information about dynamic settings. 120 */ 121final class Settings { 122 private static final String TAG = "PackageSettings"; 123 124 /** 125 * Current version of the package database. Set it to the latest version in 126 * the {@link DatabaseVersion} class below to ensure the database upgrade 127 * doesn't happen repeatedly. 128 * <p> 129 * Note that care should be taken to make sure all database upgrades are 130 * idempotent. 131 */ 132 public static final int CURRENT_DATABASE_VERSION = DatabaseVersion.SIGNATURE_MALFORMED_RECOVER; 133 134 /** 135 * This class contains constants that can be referred to from upgrade code. 136 * Insert constant values here that describe the upgrade reason. The version 137 * code must be monotonically increasing. 138 */ 139 public static class DatabaseVersion { 140 /** 141 * The initial version of the database. 142 */ 143 public static final int FIRST_VERSION = 1; 144 145 /** 146 * Migrating the Signature array from the entire certificate chain to 147 * just the signing certificate. 148 */ 149 public static final int SIGNATURE_END_ENTITY = 2; 150 151 /** 152 * There was a window of time in 153 * {@link android.os.Build.VERSION_CODES#LOLLIPOP} where we persisted 154 * certificates after potentially mutating them. To switch back to the 155 * original untouched certificates, we need to force a collection pass. 156 */ 157 public static final int SIGNATURE_MALFORMED_RECOVER = 3; 158 } 159 160 private static final boolean DEBUG_STOPPED = false; 161 private static final boolean DEBUG_MU = false; 162 163 private static final String RUNTIME_PERMISSIONS_FILE_NAME = "runtime-permissions.xml"; 164 165 private static final String TAG_READ_EXTERNAL_STORAGE = "read-external-storage"; 166 private static final String ATTR_ENFORCEMENT = "enforcement"; 167 168 private static final String TAG_ITEM = "item"; 169 private static final String TAG_DISABLED_COMPONENTS = "disabled-components"; 170 private static final String TAG_ENABLED_COMPONENTS = "enabled-components"; 171 private static final String TAG_PACKAGE_RESTRICTIONS = "package-restrictions"; 172 private static final String TAG_PACKAGE = "pkg"; 173 private static final String TAG_SHARED_USER = "shared-user"; 174 private static final String TAG_RUNTIME_PERMISSIONS = "runtime-permissions"; 175 private static final String TAG_PERMISSIONS = "perms"; 176 private static final String TAG_PERSISTENT_PREFERRED_ACTIVITIES = 177 "persistent-preferred-activities"; 178 static final String TAG_CROSS_PROFILE_INTENT_FILTERS = 179 "crossProfile-intent-filters"; 180 private static final String TAG_DOMAIN_VERIFICATION = "domain-verification"; 181 private static final String TAG_DEFAULT_APPS = "default-apps"; 182 private static final String TAG_ALL_INTENT_FILTER_VERIFICATION = 183 "all-intent-filter-verifications"; 184 private static final String TAG_DEFAULT_BROWSER = "default-browser"; 185 private static final String TAG_VERSION = "version"; 186 187 private static final String ATTR_NAME = "name"; 188 private static final String ATTR_USER = "user"; 189 private static final String ATTR_CODE = "code"; 190 private static final String ATTR_NOT_LAUNCHED = "nl"; 191 private static final String ATTR_ENABLED = "enabled"; 192 private static final String ATTR_GRANTED = "granted"; 193 private static final String ATTR_FLAGS = "flags"; 194 private static final String ATTR_ENABLED_CALLER = "enabledCaller"; 195 private static final String ATTR_STOPPED = "stopped"; 196 // Legacy, here for reading older versions of the package-restrictions. 197 private static final String ATTR_BLOCKED = "blocked"; 198 // New name for the above attribute. 199 private static final String ATTR_HIDDEN = "hidden"; 200 private static final String ATTR_SUSPENDED = "suspended"; 201 private static final String ATTR_INSTALLED = "inst"; 202 private static final String ATTR_BLOCK_UNINSTALL = "blockUninstall"; 203 private static final String ATTR_DOMAIN_VERIFICATON_STATE = "domainVerificationStatus"; 204 private static final String ATTR_PACKAGE_NAME = "packageName"; 205 private static final String ATTR_FINGERPRINT = "fingerprint"; 206 private static final String ATTR_APP_LINK_GENERATION = "app-link-generation"; 207 private static final String ATTR_VOLUME_UUID = "volumeUuid"; 208 private static final String ATTR_SDK_VERSION = "sdkVersion"; 209 private static final String ATTR_DATABASE_VERSION = "databaseVersion"; 210 211 private final Object mLock; 212 213 private final RuntimePermissionPersistence mRuntimePermissionsPersistence; 214 215 private final File mSettingsFilename; 216 private final File mBackupSettingsFilename; 217 private final File mPackageListFilename; 218 private final File mStoppedPackagesFilename; 219 private final File mBackupStoppedPackagesFilename; 220 221 final ArrayMap<String, PackageSetting> mPackages = 222 new ArrayMap<String, PackageSetting>(); 223 224 // List of replaced system applications 225 private final ArrayMap<String, PackageSetting> mDisabledSysPackages = 226 new ArrayMap<String, PackageSetting>(); 227 228 // Set of restored intent-filter verification states 229 private final ArrayMap<String, IntentFilterVerificationInfo> mRestoredIntentFilterVerifications = 230 new ArrayMap<String, IntentFilterVerificationInfo>(); 231 232 private static int mFirstAvailableUid = 0; 233 234 /** Map from volume UUID to {@link VersionInfo} */ 235 private ArrayMap<String, VersionInfo> mVersion = new ArrayMap<>(); 236 237 /** 238 * Version details for a storage volume that may hold apps. 239 */ 240 public static class VersionInfo { 241 /** 242 * These are the last platform API version we were using for the apps 243 * installed on internal and external storage. It is used to grant newer 244 * permissions one time during a system upgrade. 245 */ 246 int sdkVersion; 247 248 /** 249 * The current database version for apps on internal storage. This is 250 * used to upgrade the format of the packages.xml database not 251 * necessarily tied to an SDK version. 252 */ 253 int databaseVersion; 254 255 /** 256 * Last known value of {@link Build#FINGERPRINT}. Used to determine when 257 * an system update has occurred, meaning we need to clear code caches. 258 */ 259 String fingerprint; 260 261 /** 262 * Force all version information to match current system values, 263 * typically after resolving any required upgrade steps. 264 */ 265 public void forceCurrent() { 266 sdkVersion = Build.VERSION.SDK_INT; 267 databaseVersion = CURRENT_DATABASE_VERSION; 268 fingerprint = Build.FINGERPRINT; 269 } 270 } 271 272 Boolean mReadExternalStorageEnforced; 273 274 /** Device identity for the purpose of package verification. */ 275 private VerifierDeviceIdentity mVerifierDeviceIdentity; 276 277 // The user's preferred activities associated with particular intent 278 // filters. 279 final SparseArray<PreferredIntentResolver> mPreferredActivities = 280 new SparseArray<PreferredIntentResolver>(); 281 282 // The persistent preferred activities of the user's profile/device owner 283 // associated with particular intent filters. 284 final SparseArray<PersistentPreferredIntentResolver> mPersistentPreferredActivities = 285 new SparseArray<PersistentPreferredIntentResolver>(); 286 287 // For every user, it is used to find to which other users the intent can be forwarded. 288 final SparseArray<CrossProfileIntentResolver> mCrossProfileIntentResolvers = 289 new SparseArray<CrossProfileIntentResolver>(); 290 291 final ArrayMap<String, SharedUserSetting> mSharedUsers = 292 new ArrayMap<String, SharedUserSetting>(); 293 private final ArrayList<Object> mUserIds = new ArrayList<Object>(); 294 private final SparseArray<Object> mOtherUserIds = 295 new SparseArray<Object>(); 296 297 // For reading/writing settings file. 298 private final ArrayList<Signature> mPastSignatures = 299 new ArrayList<Signature>(); 300 private final ArrayMap<Long, Integer> mKeySetRefs = 301 new ArrayMap<Long, Integer>(); 302 303 // Mapping from permission names to info about them. 304 final ArrayMap<String, BasePermission> mPermissions = 305 new ArrayMap<String, BasePermission>(); 306 307 // Mapping from permission tree names to info about them. 308 final ArrayMap<String, BasePermission> mPermissionTrees = 309 new ArrayMap<String, BasePermission>(); 310 311 // Packages that have been uninstalled and still need their external 312 // storage data deleted. 313 final ArrayList<PackageCleanItem> mPackagesToBeCleaned = new ArrayList<PackageCleanItem>(); 314 315 // Packages that have been renamed since they were first installed. 316 // Keys are the new names of the packages, values are the original 317 // names. The packages appear everwhere else under their original 318 // names. 319 final ArrayMap<String, String> mRenamedPackages = new ArrayMap<String, String>(); 320 321 // For every user, it is used to find the package name of the default Browser App. 322 final SparseArray<String> mDefaultBrowserApp = new SparseArray<String>(); 323 324 // App-link priority tracking, per-user 325 final SparseIntArray mNextAppLinkGeneration = new SparseIntArray(); 326 327 final StringBuilder mReadMessages = new StringBuilder(); 328 329 /** 330 * Used to track packages that have a shared user ID that hasn't been read 331 * in yet. 332 * <p> 333 * TODO: make this just a local variable that is passed in during package 334 * scanning to make it less confusing. 335 */ 336 private final ArrayList<PendingPackage> mPendingPackages = new ArrayList<PendingPackage>(); 337 338 private final File mSystemDir; 339 340 public final KeySetManagerService mKeySetManagerService = new KeySetManagerService(mPackages); 341 342 Settings(Object lock) { 343 this(Environment.getDataDirectory(), lock); 344 } 345 346 Settings(File dataDir, Object lock) { 347 mLock = lock; 348 349 mRuntimePermissionsPersistence = new RuntimePermissionPersistence(mLock); 350 351 mSystemDir = new File(dataDir, "system"); 352 mSystemDir.mkdirs(); 353 FileUtils.setPermissions(mSystemDir.toString(), 354 FileUtils.S_IRWXU|FileUtils.S_IRWXG 355 |FileUtils.S_IROTH|FileUtils.S_IXOTH, 356 -1, -1); 357 mSettingsFilename = new File(mSystemDir, "packages.xml"); 358 mBackupSettingsFilename = new File(mSystemDir, "packages-backup.xml"); 359 mPackageListFilename = new File(mSystemDir, "packages.list"); 360 FileUtils.setPermissions(mPackageListFilename, 0640, SYSTEM_UID, PACKAGE_INFO_GID); 361 362 // Deprecated: Needed for migration 363 mStoppedPackagesFilename = new File(mSystemDir, "packages-stopped.xml"); 364 mBackupStoppedPackagesFilename = new File(mSystemDir, "packages-stopped-backup.xml"); 365 } 366 367 PackageSetting getPackageLPw(PackageParser.Package pkg, PackageSetting origPackage, 368 String realName, SharedUserSetting sharedUser, File codePath, File resourcePath, 369 String legacyNativeLibraryPathString, String primaryCpuAbi, String secondaryCpuAbi, 370 int pkgFlags, int pkgPrivateFlags, UserHandle user, boolean add) { 371 final String name = pkg.packageName; 372 PackageSetting p = getPackageLPw(name, origPackage, realName, sharedUser, codePath, 373 resourcePath, legacyNativeLibraryPathString, primaryCpuAbi, secondaryCpuAbi, 374 pkg.mVersionCode, pkgFlags, pkgPrivateFlags, user, add, true /* allowInstall */); 375 return p; 376 } 377 378 PackageSetting peekPackageLPr(String name) { 379 return mPackages.get(name); 380 } 381 382 void setInstallStatus(String pkgName, int status) { 383 PackageSetting p = mPackages.get(pkgName); 384 if(p != null) { 385 if(p.getInstallStatus() != status) { 386 p.setInstallStatus(status); 387 } 388 } 389 } 390 391 void setInstallerPackageName(String pkgName, String installerPkgName) { 392 PackageSetting p = mPackages.get(pkgName); 393 if (p != null) { 394 p.setInstallerPackageName(installerPkgName); 395 } 396 } 397 398 SharedUserSetting getSharedUserLPw(String name, 399 int pkgFlags, int pkgPrivateFlags, boolean create) { 400 SharedUserSetting s = mSharedUsers.get(name); 401 if (s == null) { 402 if (!create) { 403 return null; 404 } 405 s = new SharedUserSetting(name, pkgFlags, pkgPrivateFlags); 406 s.userId = newUserIdLPw(s); 407 Log.i(PackageManagerService.TAG, "New shared user " + name + ": id=" + s.userId); 408 // < 0 means we couldn't assign a userid; fall out and return 409 // s, which is currently null 410 if (s.userId >= 0) { 411 mSharedUsers.put(name, s); 412 } 413 } 414 415 return s; 416 } 417 418 Collection<SharedUserSetting> getAllSharedUsersLPw() { 419 return mSharedUsers.values(); 420 } 421 422 423 boolean disableSystemPackageLPw(String name) { 424 final PackageSetting p = mPackages.get(name); 425 if(p == null) { 426 Log.w(PackageManagerService.TAG, "Package:"+name+" is not an installed package"); 427 return false; 428 } 429 final PackageSetting dp = mDisabledSysPackages.get(name); 430 // always make sure the system package code and resource paths dont change 431 if (dp == null) { 432 if((p.pkg != null) && (p.pkg.applicationInfo != null)) { 433 p.pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; 434 } 435 mDisabledSysPackages.put(name, p); 436 437 // a little trick... when we install the new package, we don't 438 // want to modify the existing PackageSetting for the built-in 439 // version. so at this point we need a new PackageSetting that 440 // is okay to muck with. 441 PackageSetting newp = new PackageSetting(p); 442 replacePackageLPw(name, newp); 443 return true; 444 } 445 return false; 446 } 447 448 PackageSetting enableSystemPackageLPw(String name) { 449 PackageSetting p = mDisabledSysPackages.get(name); 450 if(p == null) { 451 Log.w(PackageManagerService.TAG, "Package:"+name+" is not disabled"); 452 return null; 453 } 454 // Reset flag in ApplicationInfo object 455 if((p.pkg != null) && (p.pkg.applicationInfo != null)) { 456 p.pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; 457 } 458 PackageSetting ret = addPackageLPw(name, p.realName, p.codePath, p.resourcePath, 459 p.legacyNativeLibraryPathString, p.primaryCpuAbiString, 460 p.secondaryCpuAbiString, p.secondaryCpuAbiString, 461 p.appId, p.versionCode, p.pkgFlags, p.pkgPrivateFlags); 462 mDisabledSysPackages.remove(name); 463 return ret; 464 } 465 466 boolean isDisabledSystemPackageLPr(String name) { 467 return mDisabledSysPackages.containsKey(name); 468 } 469 470 void removeDisabledSystemPackageLPw(String name) { 471 mDisabledSysPackages.remove(name); 472 } 473 474 PackageSetting addPackageLPw(String name, String realName, File codePath, File resourcePath, 475 String legacyNativeLibraryPathString, String primaryCpuAbiString, String secondaryCpuAbiString, 476 String cpuAbiOverrideString, int uid, int vc, int pkgFlags, int pkgPrivateFlags) { 477 PackageSetting p = mPackages.get(name); 478 if (p != null) { 479 if (p.appId == uid) { 480 return p; 481 } 482 PackageManagerService.reportSettingsProblem(Log.ERROR, 483 "Adding duplicate package, keeping first: " + name); 484 return null; 485 } 486 p = new PackageSetting(name, realName, codePath, resourcePath, 487 legacyNativeLibraryPathString, primaryCpuAbiString, secondaryCpuAbiString, 488 cpuAbiOverrideString, vc, pkgFlags, pkgPrivateFlags); 489 p.appId = uid; 490 if (addUserIdLPw(uid, p, name)) { 491 mPackages.put(name, p); 492 return p; 493 } 494 return null; 495 } 496 497 SharedUserSetting addSharedUserLPw(String name, int uid, int pkgFlags, int pkgPrivateFlags) { 498 SharedUserSetting s = mSharedUsers.get(name); 499 if (s != null) { 500 if (s.userId == uid) { 501 return s; 502 } 503 PackageManagerService.reportSettingsProblem(Log.ERROR, 504 "Adding duplicate shared user, keeping first: " + name); 505 return null; 506 } 507 s = new SharedUserSetting(name, pkgFlags, pkgPrivateFlags); 508 s.userId = uid; 509 if (addUserIdLPw(uid, s, name)) { 510 mSharedUsers.put(name, s); 511 return s; 512 } 513 return null; 514 } 515 516 void pruneSharedUsersLPw() { 517 ArrayList<String> removeStage = new ArrayList<String>(); 518 for (Map.Entry<String,SharedUserSetting> entry : mSharedUsers.entrySet()) { 519 final SharedUserSetting sus = entry.getValue(); 520 if (sus == null) { 521 removeStage.add(entry.getKey()); 522 continue; 523 } 524 // remove packages that are no longer installed 525 for (Iterator<PackageSetting> iter = sus.packages.iterator(); iter.hasNext();) { 526 PackageSetting ps = iter.next(); 527 if (mPackages.get(ps.name) == null) { 528 iter.remove(); 529 } 530 } 531 if (sus.packages.size() == 0) { 532 removeStage.add(entry.getKey()); 533 } 534 } 535 for (int i = 0; i < removeStage.size(); i++) { 536 mSharedUsers.remove(removeStage.get(i)); 537 } 538 } 539 540 // Transfer ownership of permissions from one package to another. 541 void transferPermissionsLPw(String origPkg, String newPkg) { 542 // Transfer ownership of permissions to the new package. 543 for (int i=0; i<2; i++) { 544 ArrayMap<String, BasePermission> permissions = 545 i == 0 ? mPermissionTrees : mPermissions; 546 for (BasePermission bp : permissions.values()) { 547 if (origPkg.equals(bp.sourcePackage)) { 548 if (PackageManagerService.DEBUG_UPGRADE) Log.v(PackageManagerService.TAG, 549 "Moving permission " + bp.name 550 + " from pkg " + bp.sourcePackage 551 + " to " + newPkg); 552 bp.sourcePackage = newPkg; 553 bp.packageSetting = null; 554 bp.perm = null; 555 if (bp.pendingInfo != null) { 556 bp.pendingInfo.packageName = newPkg; 557 } 558 bp.uid = 0; 559 bp.setGids(null, false); 560 } 561 } 562 } 563 } 564 565 private PackageSetting getPackageLPw(String name, PackageSetting origPackage, 566 String realName, SharedUserSetting sharedUser, File codePath, File resourcePath, 567 String legacyNativeLibraryPathString, String primaryCpuAbiString, 568 String secondaryCpuAbiString, int vc, int pkgFlags, int pkgPrivateFlags, 569 UserHandle installUser, boolean add, boolean allowInstall) { 570 PackageSetting p = mPackages.get(name); 571 UserManagerService userManager = UserManagerService.getInstance(); 572 if (p != null) { 573 p.primaryCpuAbiString = primaryCpuAbiString; 574 p.secondaryCpuAbiString = secondaryCpuAbiString; 575 576 if (!p.codePath.equals(codePath)) { 577 // Check to see if its a disabled system app 578 if ((p.pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0) { 579 // This is an updated system app with versions in both system 580 // and data partition. Just let the most recent version 581 // take precedence. 582 Slog.w(PackageManagerService.TAG, "Trying to update system app code path from " 583 + p.codePathString + " to " + codePath.toString()); 584 } else { 585 // Just a change in the code path is not an issue, but 586 // let's log a message about it. 587 Slog.i(PackageManagerService.TAG, "Package " + name + " codePath changed from " 588 + p.codePath + " to " + codePath + "; Retaining data and using new"); 589 /* 590 * Since we've changed paths, we need to prefer the new 591 * native library path over the one stored in the 592 * package settings since we might have moved from 593 * internal to external storage or vice versa. 594 */ 595 p.legacyNativeLibraryPathString = legacyNativeLibraryPathString; 596 } 597 } 598 if (p.sharedUser != sharedUser) { 599 PackageManagerService.reportSettingsProblem(Log.WARN, 600 "Package " + name + " shared user changed from " 601 + (p.sharedUser != null ? p.sharedUser.name : "<nothing>") 602 + " to " 603 + (sharedUser != null ? sharedUser.name : "<nothing>") 604 + "; replacing with new"); 605 p = null; 606 } else { 607 // If what we are scanning is a system (and possibly privileged) package, 608 // then make it so, regardless of whether it was previously installed only 609 // in the data partition. 610 p.pkgFlags |= pkgFlags & ApplicationInfo.FLAG_SYSTEM; 611 p.pkgPrivateFlags |= pkgPrivateFlags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED; 612 } 613 } 614 if (p == null) { 615 if (origPackage != null) { 616 // We are consuming the data from an existing package. 617 p = new PackageSetting(origPackage.name, name, codePath, resourcePath, 618 legacyNativeLibraryPathString, primaryCpuAbiString, secondaryCpuAbiString, 619 null /* cpuAbiOverrideString */, vc, pkgFlags, pkgPrivateFlags); 620 if (PackageManagerService.DEBUG_UPGRADE) Log.v(PackageManagerService.TAG, "Package " 621 + name + " is adopting original package " + origPackage.name); 622 // Note that we will retain the new package's signature so 623 // that we can keep its data. 624 PackageSignatures s = p.signatures; 625 p.copyFrom(origPackage); 626 p.signatures = s; 627 p.sharedUser = origPackage.sharedUser; 628 p.appId = origPackage.appId; 629 p.origPackage = origPackage; 630 p.getPermissionsState().copyFrom(origPackage.getPermissionsState()); 631 mRenamedPackages.put(name, origPackage.name); 632 name = origPackage.name; 633 // Update new package state. 634 p.setTimeStamp(codePath.lastModified()); 635 } else { 636 p = new PackageSetting(name, realName, codePath, resourcePath, 637 legacyNativeLibraryPathString, primaryCpuAbiString, secondaryCpuAbiString, 638 null /* cpuAbiOverrideString */, vc, pkgFlags, pkgPrivateFlags); 639 p.setTimeStamp(codePath.lastModified()); 640 p.sharedUser = sharedUser; 641 // If this is not a system app, it starts out stopped. 642 if ((pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) { 643 if (DEBUG_STOPPED) { 644 RuntimeException e = new RuntimeException("here"); 645 e.fillInStackTrace(); 646 Slog.i(PackageManagerService.TAG, "Stopping package " + name, e); 647 } 648 List<UserInfo> users = getAllUsers(); 649 final int installUserId = installUser != null ? installUser.getIdentifier() : 0; 650 if (users != null && allowInstall) { 651 for (UserInfo user : users) { 652 // By default we consider this app to be installed 653 // for the user if no user has been specified (which 654 // means to leave it at its original value, and the 655 // original default value is true), or we are being 656 // asked to install for all users, or this is the 657 // user we are installing for. 658 final boolean installed = installUser == null 659 || (installUserId == UserHandle.USER_ALL 660 && !isAdbInstallDisallowed(userManager, user.id)) 661 || installUserId == user.id; 662 p.setUserState(user.id, COMPONENT_ENABLED_STATE_DEFAULT, 663 installed, 664 true, // stopped, 665 true, // notLaunched 666 false, // hidden 667 false, // suspended 668 null, null, null, 669 false, // blockUninstall 670 INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED, 0); 671 writePackageRestrictionsLPr(user.id); 672 } 673 } 674 } 675 if (sharedUser != null) { 676 p.appId = sharedUser.userId; 677 } else { 678 // Clone the setting here for disabled system packages 679 PackageSetting dis = mDisabledSysPackages.get(name); 680 if (dis != null) { 681 // For disabled packages a new setting is created 682 // from the existing user id. This still has to be 683 // added to list of user id's 684 // Copy signatures from previous setting 685 if (dis.signatures.mSignatures != null) { 686 p.signatures.mSignatures = dis.signatures.mSignatures.clone(); 687 } 688 p.appId = dis.appId; 689 // Clone permissions 690 p.getPermissionsState().copyFrom(dis.getPermissionsState()); 691 // Clone component info 692 List<UserInfo> users = getAllUsers(); 693 if (users != null) { 694 for (UserInfo user : users) { 695 int userId = user.id; 696 p.setDisabledComponentsCopy( 697 dis.getDisabledComponents(userId), userId); 698 p.setEnabledComponentsCopy( 699 dis.getEnabledComponents(userId), userId); 700 } 701 } 702 // Add new setting to list of user ids 703 addUserIdLPw(p.appId, p, name); 704 } else { 705 // Assign new user id 706 p.appId = newUserIdLPw(p); 707 } 708 } 709 } 710 if (p.appId < 0) { 711 PackageManagerService.reportSettingsProblem(Log.WARN, 712 "Package " + name + " could not be assigned a valid uid"); 713 return null; 714 } 715 if (add) { 716 // Finish adding new package by adding it and updating shared 717 // user preferences 718 addPackageSettingLPw(p, name, sharedUser); 719 } 720 } else { 721 if (installUser != null && allowInstall) { 722 // The caller has explicitly specified the user they want this 723 // package installed for, and the package already exists. 724 // Make sure it conforms to the new request. 725 List<UserInfo> users = getAllUsers(); 726 if (users != null) { 727 for (UserInfo user : users) { 728 if ((installUser.getIdentifier() == UserHandle.USER_ALL 729 && !isAdbInstallDisallowed(userManager, user.id)) 730 || installUser.getIdentifier() == user.id) { 731 boolean installed = p.getInstalled(user.id); 732 if (!installed) { 733 p.setInstalled(true, user.id); 734 writePackageRestrictionsLPr(user.id); 735 } 736 } 737 } 738 } 739 } 740 } 741 return p; 742 } 743 744 boolean isAdbInstallDisallowed(UserManagerService userManager, int userId) { 745 return userManager.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES, 746 userId); 747 } 748 749 void insertPackageSettingLPw(PackageSetting p, PackageParser.Package pkg) { 750 p.pkg = pkg; 751 // pkg.mSetEnabled = p.getEnabled(userId); 752 // pkg.mSetStopped = p.getStopped(userId); 753 final String volumeUuid = pkg.applicationInfo.volumeUuid; 754 final String codePath = pkg.applicationInfo.getCodePath(); 755 final String resourcePath = pkg.applicationInfo.getResourcePath(); 756 final String legacyNativeLibraryPath = pkg.applicationInfo.nativeLibraryRootDir; 757 // Update volume if needed 758 if (!Objects.equals(volumeUuid, p.volumeUuid)) { 759 Slog.w(PackageManagerService.TAG, "Volume for " + p.pkg.packageName + 760 " changing from " + p.volumeUuid + " to " + volumeUuid); 761 p.volumeUuid = volumeUuid; 762 } 763 // Update code path if needed 764 if (!Objects.equals(codePath, p.codePathString)) { 765 Slog.w(PackageManagerService.TAG, "Code path for " + p.pkg.packageName + 766 " changing from " + p.codePathString + " to " + codePath); 767 p.codePath = new File(codePath); 768 p.codePathString = codePath; 769 } 770 //Update resource path if needed 771 if (!Objects.equals(resourcePath, p.resourcePathString)) { 772 Slog.w(PackageManagerService.TAG, "Resource path for " + p.pkg.packageName + 773 " changing from " + p.resourcePathString + " to " + resourcePath); 774 p.resourcePath = new File(resourcePath); 775 p.resourcePathString = resourcePath; 776 } 777 // Update the native library paths if needed 778 if (!Objects.equals(legacyNativeLibraryPath, p.legacyNativeLibraryPathString)) { 779 p.legacyNativeLibraryPathString = legacyNativeLibraryPath; 780 } 781 782 // Update the required Cpu Abi 783 p.primaryCpuAbiString = pkg.applicationInfo.primaryCpuAbi; 784 p.secondaryCpuAbiString = pkg.applicationInfo.secondaryCpuAbi; 785 p.cpuAbiOverrideString = pkg.cpuAbiOverride; 786 // Update version code if needed 787 if (pkg.mVersionCode != p.versionCode) { 788 p.versionCode = pkg.mVersionCode; 789 } 790 // Update signatures if needed. 791 if (p.signatures.mSignatures == null) { 792 p.signatures.assignSignatures(pkg.mSignatures); 793 } 794 // Update flags if needed. 795 if (pkg.applicationInfo.flags != p.pkgFlags) { 796 p.pkgFlags = pkg.applicationInfo.flags; 797 } 798 // If this app defines a shared user id initialize 799 // the shared user signatures as well. 800 if (p.sharedUser != null && p.sharedUser.signatures.mSignatures == null) { 801 p.sharedUser.signatures.assignSignatures(pkg.mSignatures); 802 } 803 addPackageSettingLPw(p, pkg.packageName, p.sharedUser); 804 } 805 806 // Utility method that adds a PackageSetting to mPackages and 807 // completes updating the shared user attributes and any restored 808 // app link verification state 809 private void addPackageSettingLPw(PackageSetting p, String name, 810 SharedUserSetting sharedUser) { 811 mPackages.put(name, p); 812 if (sharedUser != null) { 813 if (p.sharedUser != null && p.sharedUser != sharedUser) { 814 PackageManagerService.reportSettingsProblem(Log.ERROR, 815 "Package " + p.name + " was user " 816 + p.sharedUser + " but is now " + sharedUser 817 + "; I am not changing its files so it will probably fail!"); 818 p.sharedUser.removePackage(p); 819 } else if (p.appId != sharedUser.userId) { 820 PackageManagerService.reportSettingsProblem(Log.ERROR, 821 "Package " + p.name + " was user id " + p.appId 822 + " but is now user " + sharedUser 823 + " with id " + sharedUser.userId 824 + "; I am not changing its files so it will probably fail!"); 825 } 826 827 sharedUser.addPackage(p); 828 p.sharedUser = sharedUser; 829 p.appId = sharedUser.userId; 830 } 831 832 // If the we know about this user id, we have to update it as it 833 // has to point to the same PackageSetting instance as the package. 834 Object userIdPs = getUserIdLPr(p.appId); 835 if (sharedUser == null) { 836 if (userIdPs != null && userIdPs != p) { 837 replaceUserIdLPw(p.appId, p); 838 } 839 } else { 840 if (userIdPs != null && userIdPs != sharedUser) { 841 replaceUserIdLPw(p.appId, sharedUser); 842 } 843 } 844 845 IntentFilterVerificationInfo ivi = mRestoredIntentFilterVerifications.get(name); 846 if (ivi != null) { 847 if (DEBUG_DOMAIN_VERIFICATION) { 848 Slog.i(TAG, "Applying restored IVI for " + name + " : " + ivi.getStatusString()); 849 } 850 mRestoredIntentFilterVerifications.remove(name); 851 p.setIntentFilterVerificationInfo(ivi); 852 } 853 } 854 855 /* 856 * Update the shared user setting when a package using 857 * specifying the shared user id is removed. The gids 858 * associated with each permission of the deleted package 859 * are removed from the shared user's gid list only if its 860 * not in use by other permissions of packages in the 861 * shared user setting. 862 */ 863 int updateSharedUserPermsLPw(PackageSetting deletedPs, int userId) { 864 if ((deletedPs == null) || (deletedPs.pkg == null)) { 865 Slog.i(PackageManagerService.TAG, 866 "Trying to update info for null package. Just ignoring"); 867 return UserHandle.USER_NULL; 868 } 869 870 // No sharedUserId 871 if (deletedPs.sharedUser == null) { 872 return UserHandle.USER_NULL; 873 } 874 875 SharedUserSetting sus = deletedPs.sharedUser; 876 877 // Update permissions 878 for (String eachPerm : deletedPs.pkg.requestedPermissions) { 879 BasePermission bp = mPermissions.get(eachPerm); 880 if (bp == null) { 881 continue; 882 } 883 884 // Check if another package in the shared user needs the permission. 885 boolean used = false; 886 for (PackageSetting pkg : sus.packages) { 887 if (pkg.pkg != null 888 && !pkg.pkg.packageName.equals(deletedPs.pkg.packageName) 889 && pkg.pkg.requestedPermissions.contains(eachPerm)) { 890 used = true; 891 break; 892 } 893 } 894 if (used) { 895 continue; 896 } 897 898 PermissionsState permissionsState = sus.getPermissionsState(); 899 PackageSetting disabledPs = getDisabledSystemPkgLPr(deletedPs.pkg.packageName); 900 901 // If the package is shadowing is a disabled system package, 902 // do not drop permissions that the shadowed package requests. 903 if (disabledPs != null) { 904 boolean reqByDisabledSysPkg = false; 905 for (String permission : disabledPs.pkg.requestedPermissions) { 906 if (permission.equals(eachPerm)) { 907 reqByDisabledSysPkg = true; 908 break; 909 } 910 } 911 if (reqByDisabledSysPkg) { 912 continue; 913 } 914 } 915 916 // Try to revoke as an install permission which is for all users. 917 // The package is gone - no need to keep flags for applying policy. 918 permissionsState.updatePermissionFlags(bp, userId, 919 PackageManager.MASK_PERMISSION_FLAGS, 0); 920 921 if (permissionsState.revokeInstallPermission(bp) == 922 PermissionsState.PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED) { 923 return UserHandle.USER_ALL; 924 } 925 926 // Try to revoke as an install permission which is per user. 927 if (permissionsState.revokeRuntimePermission(bp, userId) == 928 PermissionsState.PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED) { 929 return userId; 930 } 931 } 932 933 return UserHandle.USER_NULL; 934 } 935 936 int removePackageLPw(String name) { 937 final PackageSetting p = mPackages.get(name); 938 if (p != null) { 939 mPackages.remove(name); 940 if (p.sharedUser != null) { 941 p.sharedUser.removePackage(p); 942 if (p.sharedUser.packages.size() == 0) { 943 mSharedUsers.remove(p.sharedUser.name); 944 removeUserIdLPw(p.sharedUser.userId); 945 return p.sharedUser.userId; 946 } 947 } else { 948 removeUserIdLPw(p.appId); 949 return p.appId; 950 } 951 } 952 return -1; 953 } 954 955 private void replacePackageLPw(String name, PackageSetting newp) { 956 final PackageSetting p = mPackages.get(name); 957 if (p != null) { 958 if (p.sharedUser != null) { 959 p.sharedUser.removePackage(p); 960 p.sharedUser.addPackage(newp); 961 } else { 962 replaceUserIdLPw(p.appId, newp); 963 } 964 } 965 mPackages.put(name, newp); 966 } 967 968 private boolean addUserIdLPw(int uid, Object obj, Object name) { 969 if (uid > Process.LAST_APPLICATION_UID) { 970 return false; 971 } 972 973 if (uid >= Process.FIRST_APPLICATION_UID) { 974 int N = mUserIds.size(); 975 final int index = uid - Process.FIRST_APPLICATION_UID; 976 while (index >= N) { 977 mUserIds.add(null); 978 N++; 979 } 980 if (mUserIds.get(index) != null) { 981 PackageManagerService.reportSettingsProblem(Log.ERROR, 982 "Adding duplicate user id: " + uid 983 + " name=" + name); 984 return false; 985 } 986 mUserIds.set(index, obj); 987 } else { 988 if (mOtherUserIds.get(uid) != null) { 989 PackageManagerService.reportSettingsProblem(Log.ERROR, 990 "Adding duplicate shared id: " + uid 991 + " name=" + name); 992 return false; 993 } 994 mOtherUserIds.put(uid, obj); 995 } 996 return true; 997 } 998 999 public Object getUserIdLPr(int uid) { 1000 if (uid >= Process.FIRST_APPLICATION_UID) { 1001 final int N = mUserIds.size(); 1002 final int index = uid - Process.FIRST_APPLICATION_UID; 1003 return index < N ? mUserIds.get(index) : null; 1004 } else { 1005 return mOtherUserIds.get(uid); 1006 } 1007 } 1008 1009 private void removeUserIdLPw(int uid) { 1010 if (uid >= Process.FIRST_APPLICATION_UID) { 1011 final int N = mUserIds.size(); 1012 final int index = uid - Process.FIRST_APPLICATION_UID; 1013 if (index < N) mUserIds.set(index, null); 1014 } else { 1015 mOtherUserIds.remove(uid); 1016 } 1017 setFirstAvailableUid(uid+1); 1018 } 1019 1020 private void replaceUserIdLPw(int uid, Object obj) { 1021 if (uid >= Process.FIRST_APPLICATION_UID) { 1022 final int N = mUserIds.size(); 1023 final int index = uid - Process.FIRST_APPLICATION_UID; 1024 if (index < N) mUserIds.set(index, obj); 1025 } else { 1026 mOtherUserIds.put(uid, obj); 1027 } 1028 } 1029 1030 PreferredIntentResolver editPreferredActivitiesLPw(int userId) { 1031 PreferredIntentResolver pir = mPreferredActivities.get(userId); 1032 if (pir == null) { 1033 pir = new PreferredIntentResolver(); 1034 mPreferredActivities.put(userId, pir); 1035 } 1036 return pir; 1037 } 1038 1039 PersistentPreferredIntentResolver editPersistentPreferredActivitiesLPw(int userId) { 1040 PersistentPreferredIntentResolver ppir = mPersistentPreferredActivities.get(userId); 1041 if (ppir == null) { 1042 ppir = new PersistentPreferredIntentResolver(); 1043 mPersistentPreferredActivities.put(userId, ppir); 1044 } 1045 return ppir; 1046 } 1047 1048 CrossProfileIntentResolver editCrossProfileIntentResolverLPw(int userId) { 1049 CrossProfileIntentResolver cpir = mCrossProfileIntentResolvers.get(userId); 1050 if (cpir == null) { 1051 cpir = new CrossProfileIntentResolver(); 1052 mCrossProfileIntentResolvers.put(userId, cpir); 1053 } 1054 return cpir; 1055 } 1056 1057 /** 1058 * The following functions suppose that you have a lock for managing access to the 1059 * mIntentFiltersVerifications map. 1060 */ 1061 1062 /* package protected */ 1063 IntentFilterVerificationInfo getIntentFilterVerificationLPr(String packageName) { 1064 PackageSetting ps = mPackages.get(packageName); 1065 if (ps == null) { 1066 if (DEBUG_DOMAIN_VERIFICATION) { 1067 Slog.w(PackageManagerService.TAG, "No package known: " + packageName); 1068 } 1069 return null; 1070 } 1071 return ps.getIntentFilterVerificationInfo(); 1072 } 1073 1074 /* package protected */ 1075 IntentFilterVerificationInfo createIntentFilterVerificationIfNeededLPw(String packageName, 1076 ArrayList<String> domains) { 1077 PackageSetting ps = mPackages.get(packageName); 1078 if (ps == null) { 1079 if (DEBUG_DOMAIN_VERIFICATION) { 1080 Slog.w(PackageManagerService.TAG, "No package known: " + packageName); 1081 } 1082 return null; 1083 } 1084 IntentFilterVerificationInfo ivi = ps.getIntentFilterVerificationInfo(); 1085 if (ivi == null) { 1086 ivi = new IntentFilterVerificationInfo(packageName, domains); 1087 ps.setIntentFilterVerificationInfo(ivi); 1088 if (DEBUG_DOMAIN_VERIFICATION) { 1089 Slog.d(PackageManagerService.TAG, 1090 "Creating new IntentFilterVerificationInfo for pkg: " + packageName); 1091 } 1092 } else { 1093 ivi.setDomains(domains); 1094 if (DEBUG_DOMAIN_VERIFICATION) { 1095 Slog.d(PackageManagerService.TAG, 1096 "Setting domains to existing IntentFilterVerificationInfo for pkg: " + 1097 packageName + " and with domains: " + ivi.getDomainsString()); 1098 } 1099 } 1100 return ivi; 1101 } 1102 1103 int getIntentFilterVerificationStatusLPr(String packageName, int userId) { 1104 PackageSetting ps = mPackages.get(packageName); 1105 if (ps == null) { 1106 if (DEBUG_DOMAIN_VERIFICATION) { 1107 Slog.w(PackageManagerService.TAG, "No package known: " + packageName); 1108 } 1109 return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED; 1110 } 1111 return (int)(ps.getDomainVerificationStatusForUser(userId) >> 32); 1112 } 1113 1114 boolean updateIntentFilterVerificationStatusLPw(String packageName, final int status, int userId) { 1115 // Update the status for the current package 1116 PackageSetting current = mPackages.get(packageName); 1117 if (current == null) { 1118 if (DEBUG_DOMAIN_VERIFICATION) { 1119 Slog.w(PackageManagerService.TAG, "No package known: " + packageName); 1120 } 1121 return false; 1122 } 1123 1124 final int alwaysGeneration; 1125 if (status == INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS) { 1126 alwaysGeneration = mNextAppLinkGeneration.get(userId) + 1; 1127 mNextAppLinkGeneration.put(userId, alwaysGeneration); 1128 } else { 1129 alwaysGeneration = 0; 1130 } 1131 1132 current.setDomainVerificationStatusForUser(status, alwaysGeneration, userId); 1133 return true; 1134 } 1135 1136 /** 1137 * Used for Settings App and PackageManagerService dump. Should be read only. 1138 */ 1139 List<IntentFilterVerificationInfo> getIntentFilterVerificationsLPr( 1140 String packageName) { 1141 if (packageName == null) { 1142 return Collections.<IntentFilterVerificationInfo>emptyList(); 1143 } 1144 ArrayList<IntentFilterVerificationInfo> result = new ArrayList<>(); 1145 for (PackageSetting ps : mPackages.values()) { 1146 IntentFilterVerificationInfo ivi = ps.getIntentFilterVerificationInfo(); 1147 if (ivi == null || TextUtils.isEmpty(ivi.getPackageName()) || 1148 !ivi.getPackageName().equalsIgnoreCase(packageName)) { 1149 continue; 1150 } 1151 result.add(ivi); 1152 } 1153 return result; 1154 } 1155 1156 boolean removeIntentFilterVerificationLPw(String packageName, int userId) { 1157 PackageSetting ps = mPackages.get(packageName); 1158 if (ps == null) { 1159 if (DEBUG_DOMAIN_VERIFICATION) { 1160 Slog.w(PackageManagerService.TAG, "No package known: " + packageName); 1161 } 1162 return false; 1163 } 1164 ps.clearDomainVerificationStatusForUser(userId); 1165 return true; 1166 } 1167 1168 boolean removeIntentFilterVerificationLPw(String packageName, int[] userIds) { 1169 boolean result = false; 1170 for (int userId : userIds) { 1171 result |= removeIntentFilterVerificationLPw(packageName, userId); 1172 } 1173 return result; 1174 } 1175 1176 boolean setDefaultBrowserPackageNameLPw(String packageName, int userId) { 1177 if (userId == UserHandle.USER_ALL) { 1178 return false; 1179 } 1180 mDefaultBrowserApp.put(userId, packageName); 1181 writePackageRestrictionsLPr(userId); 1182 return true; 1183 } 1184 1185 String getDefaultBrowserPackageNameLPw(int userId) { 1186 return (userId == UserHandle.USER_ALL) ? null : mDefaultBrowserApp.get(userId); 1187 } 1188 1189 private File getUserPackagesStateFile(int userId) { 1190 // TODO: Implement a cleaner solution when adding tests. 1191 // This instead of Environment.getUserSystemDirectory(userId) to support testing. 1192 File userDir = new File(new File(mSystemDir, "users"), Integer.toString(userId)); 1193 return new File(userDir, "package-restrictions.xml"); 1194 } 1195 1196 private File getUserRuntimePermissionsFile(int userId) { 1197 // TODO: Implement a cleaner solution when adding tests. 1198 // This instead of Environment.getUserSystemDirectory(userId) to support testing. 1199 File userDir = new File(new File(mSystemDir, "users"), Integer.toString(userId)); 1200 return new File(userDir, RUNTIME_PERMISSIONS_FILE_NAME); 1201 } 1202 1203 private File getUserPackagesStateBackupFile(int userId) { 1204 return new File(Environment.getUserSystemDirectory(userId), 1205 "package-restrictions-backup.xml"); 1206 } 1207 1208 void writeAllUsersPackageRestrictionsLPr() { 1209 List<UserInfo> users = getAllUsers(); 1210 if (users == null) return; 1211 1212 for (UserInfo user : users) { 1213 writePackageRestrictionsLPr(user.id); 1214 } 1215 } 1216 1217 void writeAllRuntimePermissionsLPr() { 1218 for (int userId : UserManagerService.getInstance().getUserIds()) { 1219 mRuntimePermissionsPersistence.writePermissionsForUserAsyncLPr(userId); 1220 } 1221 } 1222 1223 boolean areDefaultRuntimePermissionsGrantedLPr(int userId) { 1224 return mRuntimePermissionsPersistence 1225 .areDefaultRuntimPermissionsGrantedLPr(userId); 1226 } 1227 1228 void onDefaultRuntimePermissionsGrantedLPr(int userId) { 1229 mRuntimePermissionsPersistence 1230 .onDefaultRuntimePermissionsGrantedLPr(userId); 1231 } 1232 1233 public VersionInfo findOrCreateVersion(String volumeUuid) { 1234 VersionInfo ver = mVersion.get(volumeUuid); 1235 if (ver == null) { 1236 ver = new VersionInfo(); 1237 ver.forceCurrent(); 1238 mVersion.put(volumeUuid, ver); 1239 } 1240 return ver; 1241 } 1242 1243 public VersionInfo getInternalVersion() { 1244 return mVersion.get(StorageManager.UUID_PRIVATE_INTERNAL); 1245 } 1246 1247 public VersionInfo getExternalVersion() { 1248 return mVersion.get(StorageManager.UUID_PRIMARY_PHYSICAL); 1249 } 1250 1251 public void onVolumeForgotten(String fsUuid) { 1252 mVersion.remove(fsUuid); 1253 } 1254 1255 /** 1256 * Applies the preferred activity state described by the given XML. This code 1257 * also supports the restore-from-backup code path. 1258 * 1259 * @see PreferredActivityBackupHelper 1260 */ 1261 void readPreferredActivitiesLPw(XmlPullParser parser, int userId) 1262 throws XmlPullParserException, IOException { 1263 int outerDepth = parser.getDepth(); 1264 int type; 1265 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 1266 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 1267 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 1268 continue; 1269 } 1270 1271 String tagName = parser.getName(); 1272 if (tagName.equals(TAG_ITEM)) { 1273 PreferredActivity pa = new PreferredActivity(parser); 1274 if (pa.mPref.getParseError() == null) { 1275 editPreferredActivitiesLPw(userId).addFilter(pa); 1276 } else { 1277 PackageManagerService.reportSettingsProblem(Log.WARN, 1278 "Error in package manager settings: <preferred-activity> " 1279 + pa.mPref.getParseError() + " at " 1280 + parser.getPositionDescription()); 1281 } 1282 } else { 1283 PackageManagerService.reportSettingsProblem(Log.WARN, 1284 "Unknown element under <preferred-activities>: " + parser.getName()); 1285 XmlUtils.skipCurrentTag(parser); 1286 } 1287 } 1288 } 1289 1290 private void readPersistentPreferredActivitiesLPw(XmlPullParser parser, int userId) 1291 throws XmlPullParserException, IOException { 1292 int outerDepth = parser.getDepth(); 1293 int type; 1294 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 1295 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 1296 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 1297 continue; 1298 } 1299 String tagName = parser.getName(); 1300 if (tagName.equals(TAG_ITEM)) { 1301 PersistentPreferredActivity ppa = new PersistentPreferredActivity(parser); 1302 editPersistentPreferredActivitiesLPw(userId).addFilter(ppa); 1303 } else { 1304 PackageManagerService.reportSettingsProblem(Log.WARN, 1305 "Unknown element under <" + TAG_PERSISTENT_PREFERRED_ACTIVITIES + ">: " 1306 + parser.getName()); 1307 XmlUtils.skipCurrentTag(parser); 1308 } 1309 } 1310 } 1311 1312 private void readCrossProfileIntentFiltersLPw(XmlPullParser parser, int userId) 1313 throws XmlPullParserException, IOException { 1314 int outerDepth = parser.getDepth(); 1315 int type; 1316 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 1317 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 1318 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 1319 continue; 1320 } 1321 final String tagName = parser.getName(); 1322 if (tagName.equals(TAG_ITEM)) { 1323 CrossProfileIntentFilter cpif = new CrossProfileIntentFilter(parser); 1324 editCrossProfileIntentResolverLPw(userId).addFilter(cpif); 1325 } else { 1326 String msg = "Unknown element under " + TAG_CROSS_PROFILE_INTENT_FILTERS + ": " + 1327 tagName; 1328 PackageManagerService.reportSettingsProblem(Log.WARN, msg); 1329 XmlUtils.skipCurrentTag(parser); 1330 } 1331 } 1332 } 1333 1334 private void readDomainVerificationLPw(XmlPullParser parser, PackageSettingBase packageSetting) 1335 throws XmlPullParserException, IOException { 1336 IntentFilterVerificationInfo ivi = new IntentFilterVerificationInfo(parser); 1337 packageSetting.setIntentFilterVerificationInfo(ivi); 1338 Log.d(TAG, "Read domain verification for package:" + ivi.getPackageName()); 1339 } 1340 1341 private void readRestoredIntentFilterVerifications(XmlPullParser parser) 1342 throws XmlPullParserException, IOException { 1343 int outerDepth = parser.getDepth(); 1344 int type; 1345 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 1346 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 1347 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 1348 continue; 1349 } 1350 final String tagName = parser.getName(); 1351 if (tagName.equals(TAG_DOMAIN_VERIFICATION)) { 1352 IntentFilterVerificationInfo ivi = new IntentFilterVerificationInfo(parser); 1353 if (DEBUG_DOMAIN_VERIFICATION) { 1354 Slog.i(TAG, "Restored IVI for " + ivi.getPackageName() 1355 + " status=" + ivi.getStatusString()); 1356 } 1357 mRestoredIntentFilterVerifications.put(ivi.getPackageName(), ivi); 1358 } else { 1359 Slog.w(TAG, "Unknown element: " + tagName); 1360 XmlUtils.skipCurrentTag(parser); 1361 } 1362 } 1363 } 1364 1365 void readDefaultAppsLPw(XmlPullParser parser, int userId) 1366 throws XmlPullParserException, IOException { 1367 int outerDepth = parser.getDepth(); 1368 int type; 1369 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 1370 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 1371 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 1372 continue; 1373 } 1374 String tagName = parser.getName(); 1375 if (tagName.equals(TAG_DEFAULT_BROWSER)) { 1376 String packageName = parser.getAttributeValue(null, ATTR_PACKAGE_NAME); 1377 mDefaultBrowserApp.put(userId, packageName); 1378 } else { 1379 String msg = "Unknown element under " + TAG_DEFAULT_APPS + ": " + 1380 parser.getName(); 1381 PackageManagerService.reportSettingsProblem(Log.WARN, msg); 1382 XmlUtils.skipCurrentTag(parser); 1383 } 1384 } 1385 } 1386 1387 void readPackageRestrictionsLPr(int userId) { 1388 if (DEBUG_MU) { 1389 Log.i(TAG, "Reading package restrictions for user=" + userId); 1390 } 1391 FileInputStream str = null; 1392 File userPackagesStateFile = getUserPackagesStateFile(userId); 1393 File backupFile = getUserPackagesStateBackupFile(userId); 1394 if (backupFile.exists()) { 1395 try { 1396 str = new FileInputStream(backupFile); 1397 mReadMessages.append("Reading from backup stopped packages file\n"); 1398 PackageManagerService.reportSettingsProblem(Log.INFO, 1399 "Need to read from backup stopped packages file"); 1400 if (userPackagesStateFile.exists()) { 1401 // If both the backup and normal file exist, we 1402 // ignore the normal one since it might have been 1403 // corrupted. 1404 Slog.w(PackageManagerService.TAG, "Cleaning up stopped packages file " 1405 + userPackagesStateFile); 1406 userPackagesStateFile.delete(); 1407 } 1408 } catch (java.io.IOException e) { 1409 // We'll try for the normal settings file. 1410 } 1411 } 1412 1413 try { 1414 if (str == null) { 1415 if (!userPackagesStateFile.exists()) { 1416 mReadMessages.append("No stopped packages file found\n"); 1417 PackageManagerService.reportSettingsProblem(Log.INFO, 1418 "No stopped packages file; " 1419 + "assuming all started"); 1420 // At first boot, make sure no packages are stopped. 1421 // We usually want to have third party apps initialize 1422 // in the stopped state, but not at first boot. Also 1423 // consider all applications to be installed. 1424 for (PackageSetting pkg : mPackages.values()) { 1425 pkg.setUserState(userId, COMPONENT_ENABLED_STATE_DEFAULT, 1426 true, // installed 1427 false, // stopped 1428 false, // notLaunched 1429 false, // hidden 1430 false, // suspended 1431 null, null, null, 1432 false, // blockUninstall 1433 INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED, 0); 1434 } 1435 return; 1436 } 1437 str = new FileInputStream(userPackagesStateFile); 1438 } 1439 final XmlPullParser parser = Xml.newPullParser(); 1440 parser.setInput(str, StandardCharsets.UTF_8.name()); 1441 1442 int type; 1443 while ((type=parser.next()) != XmlPullParser.START_TAG 1444 && type != XmlPullParser.END_DOCUMENT) { 1445 ; 1446 } 1447 1448 if (type != XmlPullParser.START_TAG) { 1449 mReadMessages.append("No start tag found in package restrictions file\n"); 1450 PackageManagerService.reportSettingsProblem(Log.WARN, 1451 "No start tag found in package manager stopped packages"); 1452 return; 1453 } 1454 1455 int maxAppLinkGeneration = 0; 1456 1457 int outerDepth = parser.getDepth(); 1458 PackageSetting ps = null; 1459 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 1460 && (type != XmlPullParser.END_TAG 1461 || parser.getDepth() > outerDepth)) { 1462 if (type == XmlPullParser.END_TAG 1463 || type == XmlPullParser.TEXT) { 1464 continue; 1465 } 1466 1467 String tagName = parser.getName(); 1468 if (tagName.equals(TAG_PACKAGE)) { 1469 String name = parser.getAttributeValue(null, ATTR_NAME); 1470 ps = mPackages.get(name); 1471 if (ps == null) { 1472 Slog.w(PackageManagerService.TAG, "No package known for stopped package: " 1473 + name); 1474 XmlUtils.skipCurrentTag(parser); 1475 continue; 1476 } 1477 final String enabledStr = parser.getAttributeValue(null, ATTR_ENABLED); 1478 final int enabled = enabledStr == null 1479 ? COMPONENT_ENABLED_STATE_DEFAULT : Integer.parseInt(enabledStr); 1480 final String enabledCaller = parser.getAttributeValue(null, 1481 ATTR_ENABLED_CALLER); 1482 final String installedStr = parser.getAttributeValue(null, ATTR_INSTALLED); 1483 final boolean installed = installedStr == null 1484 ? true : Boolean.parseBoolean(installedStr); 1485 final String stoppedStr = parser.getAttributeValue(null, ATTR_STOPPED); 1486 final boolean stopped = stoppedStr == null 1487 ? false : Boolean.parseBoolean(stoppedStr); 1488 // For backwards compatibility with the previous name of "blocked", which 1489 // now means hidden, read the old attribute as well. 1490 final String blockedStr = parser.getAttributeValue(null, ATTR_BLOCKED); 1491 boolean hidden = blockedStr == null 1492 ? false : Boolean.parseBoolean(blockedStr); 1493 final String hiddenStr = parser.getAttributeValue(null, ATTR_HIDDEN); 1494 hidden = hiddenStr == null 1495 ? hidden : Boolean.parseBoolean(hiddenStr); 1496 final String suspendedStr = parser.getAttributeValue(null, ATTR_SUSPENDED); 1497 final boolean suspended = suspendedStr == null 1498 ? false : Boolean.parseBoolean(suspendedStr); 1499 final String notLaunchedStr = parser.getAttributeValue(null, ATTR_NOT_LAUNCHED); 1500 final boolean notLaunched = stoppedStr == null 1501 ? false : Boolean.parseBoolean(notLaunchedStr); 1502 final String blockUninstallStr = parser.getAttributeValue(null, 1503 ATTR_BLOCK_UNINSTALL); 1504 final boolean blockUninstall = blockUninstallStr == null 1505 ? false : Boolean.parseBoolean(blockUninstallStr); 1506 1507 final String verifStateStr = 1508 parser.getAttributeValue(null, ATTR_DOMAIN_VERIFICATON_STATE); 1509 final int verifState = (verifStateStr == null) ? 1510 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED : 1511 Integer.parseInt(verifStateStr); 1512 1513 final String linkGenStr = parser.getAttributeValue(null, ATTR_APP_LINK_GENERATION); 1514 final int linkGeneration = linkGenStr == null ? 0 : Integer.parseInt(linkGenStr); 1515 if (linkGeneration > maxAppLinkGeneration) { 1516 maxAppLinkGeneration = linkGeneration; 1517 } 1518 1519 ArraySet<String> enabledComponents = null; 1520 ArraySet<String> disabledComponents = null; 1521 1522 int packageDepth = parser.getDepth(); 1523 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 1524 && (type != XmlPullParser.END_TAG 1525 || parser.getDepth() > packageDepth)) { 1526 if (type == XmlPullParser.END_TAG 1527 || type == XmlPullParser.TEXT) { 1528 continue; 1529 } 1530 tagName = parser.getName(); 1531 if (tagName.equals(TAG_ENABLED_COMPONENTS)) { 1532 enabledComponents = readComponentsLPr(parser); 1533 } else if (tagName.equals(TAG_DISABLED_COMPONENTS)) { 1534 disabledComponents = readComponentsLPr(parser); 1535 } 1536 } 1537 1538 ps.setUserState(userId, enabled, installed, stopped, notLaunched, hidden, 1539 suspended, enabledCaller, enabledComponents, disabledComponents, 1540 blockUninstall, verifState, linkGeneration); 1541 } else if (tagName.equals("preferred-activities")) { 1542 readPreferredActivitiesLPw(parser, userId); 1543 } else if (tagName.equals(TAG_PERSISTENT_PREFERRED_ACTIVITIES)) { 1544 readPersistentPreferredActivitiesLPw(parser, userId); 1545 } else if (tagName.equals(TAG_CROSS_PROFILE_INTENT_FILTERS)) { 1546 readCrossProfileIntentFiltersLPw(parser, userId); 1547 } else if (tagName.equals(TAG_DEFAULT_APPS)) { 1548 readDefaultAppsLPw(parser, userId); 1549 } else { 1550 Slog.w(PackageManagerService.TAG, "Unknown element under <stopped-packages>: " 1551 + parser.getName()); 1552 XmlUtils.skipCurrentTag(parser); 1553 } 1554 } 1555 1556 str.close(); 1557 1558 mNextAppLinkGeneration.put(userId, maxAppLinkGeneration + 1); 1559 1560 } catch (XmlPullParserException e) { 1561 mReadMessages.append("Error reading: " + e.toString()); 1562 PackageManagerService.reportSettingsProblem(Log.ERROR, 1563 "Error reading stopped packages: " + e); 1564 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages", 1565 e); 1566 1567 } catch (java.io.IOException e) { 1568 mReadMessages.append("Error reading: " + e.toString()); 1569 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e); 1570 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages", 1571 e); 1572 } 1573 } 1574 1575 private ArraySet<String> readComponentsLPr(XmlPullParser parser) 1576 throws IOException, XmlPullParserException { 1577 ArraySet<String> components = null; 1578 int type; 1579 int outerDepth = parser.getDepth(); 1580 String tagName; 1581 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 1582 && (type != XmlPullParser.END_TAG 1583 || parser.getDepth() > outerDepth)) { 1584 if (type == XmlPullParser.END_TAG 1585 || type == XmlPullParser.TEXT) { 1586 continue; 1587 } 1588 tagName = parser.getName(); 1589 if (tagName.equals(TAG_ITEM)) { 1590 String componentName = parser.getAttributeValue(null, ATTR_NAME); 1591 if (componentName != null) { 1592 if (components == null) { 1593 components = new ArraySet<String>(); 1594 } 1595 components.add(componentName); 1596 } 1597 } 1598 } 1599 return components; 1600 } 1601 1602 /** 1603 * Record the state of preferred activity configuration into XML. This is used both 1604 * for recording packages.xml internally and for supporting backup/restore of the 1605 * preferred activity configuration. 1606 */ 1607 void writePreferredActivitiesLPr(XmlSerializer serializer, int userId, boolean full) 1608 throws IllegalArgumentException, IllegalStateException, IOException { 1609 serializer.startTag(null, "preferred-activities"); 1610 PreferredIntentResolver pir = mPreferredActivities.get(userId); 1611 if (pir != null) { 1612 for (final PreferredActivity pa : pir.filterSet()) { 1613 serializer.startTag(null, TAG_ITEM); 1614 pa.writeToXml(serializer, full); 1615 serializer.endTag(null, TAG_ITEM); 1616 } 1617 } 1618 serializer.endTag(null, "preferred-activities"); 1619 } 1620 1621 void writePersistentPreferredActivitiesLPr(XmlSerializer serializer, int userId) 1622 throws IllegalArgumentException, IllegalStateException, IOException { 1623 serializer.startTag(null, TAG_PERSISTENT_PREFERRED_ACTIVITIES); 1624 PersistentPreferredIntentResolver ppir = mPersistentPreferredActivities.get(userId); 1625 if (ppir != null) { 1626 for (final PersistentPreferredActivity ppa : ppir.filterSet()) { 1627 serializer.startTag(null, TAG_ITEM); 1628 ppa.writeToXml(serializer); 1629 serializer.endTag(null, TAG_ITEM); 1630 } 1631 } 1632 serializer.endTag(null, TAG_PERSISTENT_PREFERRED_ACTIVITIES); 1633 } 1634 1635 void writeCrossProfileIntentFiltersLPr(XmlSerializer serializer, int userId) 1636 throws IllegalArgumentException, IllegalStateException, IOException { 1637 serializer.startTag(null, TAG_CROSS_PROFILE_INTENT_FILTERS); 1638 CrossProfileIntentResolver cpir = mCrossProfileIntentResolvers.get(userId); 1639 if (cpir != null) { 1640 for (final CrossProfileIntentFilter cpif : cpir.filterSet()) { 1641 serializer.startTag(null, TAG_ITEM); 1642 cpif.writeToXml(serializer); 1643 serializer.endTag(null, TAG_ITEM); 1644 } 1645 } 1646 serializer.endTag(null, TAG_CROSS_PROFILE_INTENT_FILTERS); 1647 } 1648 1649 void writeDomainVerificationsLPr(XmlSerializer serializer, 1650 IntentFilterVerificationInfo verificationInfo) 1651 throws IllegalArgumentException, IllegalStateException, IOException { 1652 if (verificationInfo != null && verificationInfo.getPackageName() != null) { 1653 serializer.startTag(null, TAG_DOMAIN_VERIFICATION); 1654 verificationInfo.writeToXml(serializer); 1655 if (DEBUG_DOMAIN_VERIFICATION) { 1656 Slog.d(TAG, "Wrote domain verification for package: " 1657 + verificationInfo.getPackageName()); 1658 } 1659 serializer.endTag(null, TAG_DOMAIN_VERIFICATION); 1660 } 1661 } 1662 1663 // Specifically for backup/restore 1664 void writeAllDomainVerificationsLPr(XmlSerializer serializer, int userId) 1665 throws IllegalArgumentException, IllegalStateException, IOException { 1666 serializer.startTag(null, TAG_ALL_INTENT_FILTER_VERIFICATION); 1667 final int N = mPackages.size(); 1668 for (int i = 0; i < N; i++) { 1669 PackageSetting ps = mPackages.valueAt(i); 1670 IntentFilterVerificationInfo ivi = ps.getIntentFilterVerificationInfo(); 1671 if (ivi != null) { 1672 writeDomainVerificationsLPr(serializer, ivi); 1673 } 1674 } 1675 serializer.endTag(null, TAG_ALL_INTENT_FILTER_VERIFICATION); 1676 } 1677 1678 // Specifically for backup/restore 1679 void readAllDomainVerificationsLPr(XmlPullParser parser, int userId) 1680 throws XmlPullParserException, IOException { 1681 mRestoredIntentFilterVerifications.clear(); 1682 1683 int outerDepth = parser.getDepth(); 1684 int type; 1685 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 1686 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 1687 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 1688 continue; 1689 } 1690 1691 String tagName = parser.getName(); 1692 if (tagName.equals(TAG_DOMAIN_VERIFICATION)) { 1693 IntentFilterVerificationInfo ivi = new IntentFilterVerificationInfo(parser); 1694 final String pkgName = ivi.getPackageName(); 1695 final PackageSetting ps = mPackages.get(pkgName); 1696 if (ps != null) { 1697 // known/existing package; update in place 1698 ps.setIntentFilterVerificationInfo(ivi); 1699 if (DEBUG_DOMAIN_VERIFICATION) { 1700 Slog.d(TAG, "Restored IVI for existing app " + pkgName 1701 + " status=" + ivi.getStatusString()); 1702 } 1703 } else { 1704 mRestoredIntentFilterVerifications.put(pkgName, ivi); 1705 if (DEBUG_DOMAIN_VERIFICATION) { 1706 Slog.d(TAG, "Restored IVI for pending app " + pkgName 1707 + " status=" + ivi.getStatusString()); 1708 } 1709 } 1710 } else { 1711 PackageManagerService.reportSettingsProblem(Log.WARN, 1712 "Unknown element under <all-intent-filter-verification>: " 1713 + parser.getName()); 1714 XmlUtils.skipCurrentTag(parser); 1715 } 1716 } 1717 } 1718 1719 void writeDefaultAppsLPr(XmlSerializer serializer, int userId) 1720 throws IllegalArgumentException, IllegalStateException, IOException { 1721 serializer.startTag(null, TAG_DEFAULT_APPS); 1722 String packageName = mDefaultBrowserApp.get(userId); 1723 if (!TextUtils.isEmpty(packageName)) { 1724 serializer.startTag(null, TAG_DEFAULT_BROWSER); 1725 serializer.attribute(null, ATTR_PACKAGE_NAME, packageName); 1726 serializer.endTag(null, TAG_DEFAULT_BROWSER); 1727 } 1728 serializer.endTag(null, TAG_DEFAULT_APPS); 1729 } 1730 1731 void writePackageRestrictionsLPr(int userId) { 1732 if (DEBUG_MU) { 1733 Log.i(TAG, "Writing package restrictions for user=" + userId); 1734 } 1735 // Keep the old stopped packages around until we know the new ones have 1736 // been successfully written. 1737 File userPackagesStateFile = getUserPackagesStateFile(userId); 1738 File backupFile = getUserPackagesStateBackupFile(userId); 1739 new File(userPackagesStateFile.getParent()).mkdirs(); 1740 if (userPackagesStateFile.exists()) { 1741 // Presence of backup settings file indicates that we failed 1742 // to persist packages earlier. So preserve the older 1743 // backup for future reference since the current packages 1744 // might have been corrupted. 1745 if (!backupFile.exists()) { 1746 if (!userPackagesStateFile.renameTo(backupFile)) { 1747 Slog.wtf(PackageManagerService.TAG, 1748 "Unable to backup user packages state file, " 1749 + "current changes will be lost at reboot"); 1750 return; 1751 } 1752 } else { 1753 userPackagesStateFile.delete(); 1754 Slog.w(PackageManagerService.TAG, "Preserving older stopped packages backup"); 1755 } 1756 } 1757 1758 try { 1759 final FileOutputStream fstr = new FileOutputStream(userPackagesStateFile); 1760 final BufferedOutputStream str = new BufferedOutputStream(fstr); 1761 1762 final XmlSerializer serializer = new FastXmlSerializer(); 1763 serializer.setOutput(str, StandardCharsets.UTF_8.name()); 1764 serializer.startDocument(null, true); 1765 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); 1766 1767 serializer.startTag(null, TAG_PACKAGE_RESTRICTIONS); 1768 1769 for (final PackageSetting pkg : mPackages.values()) { 1770 PackageUserState ustate = pkg.readUserState(userId); 1771 if (ustate.stopped || ustate.notLaunched || !ustate.installed 1772 || ustate.enabled != COMPONENT_ENABLED_STATE_DEFAULT 1773 || ustate.hidden 1774 || ustate.suspended 1775 || (ustate.enabledComponents != null 1776 && ustate.enabledComponents.size() > 0) 1777 || (ustate.disabledComponents != null 1778 && ustate.disabledComponents.size() > 0) 1779 || ustate.blockUninstall 1780 || (ustate.domainVerificationStatus != 1781 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED)) { 1782 serializer.startTag(null, TAG_PACKAGE); 1783 serializer.attribute(null, ATTR_NAME, pkg.name); 1784 if (DEBUG_MU) Log.i(TAG, " pkg=" + pkg.name + ", state=" + ustate.enabled); 1785 1786 if (!ustate.installed) { 1787 serializer.attribute(null, ATTR_INSTALLED, "false"); 1788 } 1789 if (ustate.stopped) { 1790 serializer.attribute(null, ATTR_STOPPED, "true"); 1791 } 1792 if (ustate.notLaunched) { 1793 serializer.attribute(null, ATTR_NOT_LAUNCHED, "true"); 1794 } 1795 if (ustate.hidden) { 1796 serializer.attribute(null, ATTR_HIDDEN, "true"); 1797 } 1798 if (ustate.suspended) { 1799 serializer.attribute(null, ATTR_SUSPENDED, "true"); 1800 } 1801 if (ustate.blockUninstall) { 1802 serializer.attribute(null, ATTR_BLOCK_UNINSTALL, "true"); 1803 } 1804 if (ustate.enabled != COMPONENT_ENABLED_STATE_DEFAULT) { 1805 serializer.attribute(null, ATTR_ENABLED, 1806 Integer.toString(ustate.enabled)); 1807 if (ustate.lastDisableAppCaller != null) { 1808 serializer.attribute(null, ATTR_ENABLED_CALLER, 1809 ustate.lastDisableAppCaller); 1810 } 1811 } 1812 if (ustate.domainVerificationStatus != 1813 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED) { 1814 serializer.attribute(null, ATTR_DOMAIN_VERIFICATON_STATE, 1815 Integer.toString(ustate.domainVerificationStatus)); 1816 } 1817 if (ustate.appLinkGeneration != 0) { 1818 serializer.attribute(null, ATTR_APP_LINK_GENERATION, 1819 Integer.toString(ustate.appLinkGeneration)); 1820 } 1821 if (ustate.enabledComponents != null 1822 && ustate.enabledComponents.size() > 0) { 1823 serializer.startTag(null, TAG_ENABLED_COMPONENTS); 1824 for (final String name : ustate.enabledComponents) { 1825 serializer.startTag(null, TAG_ITEM); 1826 serializer.attribute(null, ATTR_NAME, name); 1827 serializer.endTag(null, TAG_ITEM); 1828 } 1829 serializer.endTag(null, TAG_ENABLED_COMPONENTS); 1830 } 1831 if (ustate.disabledComponents != null 1832 && ustate.disabledComponents.size() > 0) { 1833 serializer.startTag(null, TAG_DISABLED_COMPONENTS); 1834 for (final String name : ustate.disabledComponents) { 1835 serializer.startTag(null, TAG_ITEM); 1836 serializer.attribute(null, ATTR_NAME, name); 1837 serializer.endTag(null, TAG_ITEM); 1838 } 1839 serializer.endTag(null, TAG_DISABLED_COMPONENTS); 1840 } 1841 1842 serializer.endTag(null, TAG_PACKAGE); 1843 } 1844 } 1845 1846 writePreferredActivitiesLPr(serializer, userId, true); 1847 writePersistentPreferredActivitiesLPr(serializer, userId); 1848 writeCrossProfileIntentFiltersLPr(serializer, userId); 1849 writeDefaultAppsLPr(serializer, userId); 1850 1851 serializer.endTag(null, TAG_PACKAGE_RESTRICTIONS); 1852 1853 serializer.endDocument(); 1854 1855 str.flush(); 1856 FileUtils.sync(fstr); 1857 str.close(); 1858 1859 // New settings successfully written, old ones are no longer 1860 // needed. 1861 backupFile.delete(); 1862 FileUtils.setPermissions(userPackagesStateFile.toString(), 1863 FileUtils.S_IRUSR|FileUtils.S_IWUSR 1864 |FileUtils.S_IRGRP|FileUtils.S_IWGRP, 1865 -1, -1); 1866 1867 // Done, all is good! 1868 return; 1869 } catch(java.io.IOException e) { 1870 Slog.wtf(PackageManagerService.TAG, 1871 "Unable to write package manager user packages state, " 1872 + " current changes will be lost at reboot", e); 1873 } 1874 1875 // Clean up partially written files 1876 if (userPackagesStateFile.exists()) { 1877 if (!userPackagesStateFile.delete()) { 1878 Log.i(PackageManagerService.TAG, "Failed to clean up mangled file: " 1879 + mStoppedPackagesFilename); 1880 } 1881 } 1882 } 1883 1884 void readInstallPermissionsLPr(XmlPullParser parser, 1885 PermissionsState permissionsState) throws IOException, XmlPullParserException { 1886 int outerDepth = parser.getDepth(); 1887 int type; 1888 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 1889 && (type != XmlPullParser.END_TAG 1890 || parser.getDepth() > outerDepth)) { 1891 if (type == XmlPullParser.END_TAG 1892 || type == XmlPullParser.TEXT) { 1893 continue; 1894 } 1895 String tagName = parser.getName(); 1896 if (tagName.equals(TAG_ITEM)) { 1897 String name = parser.getAttributeValue(null, ATTR_NAME); 1898 1899 BasePermission bp = mPermissions.get(name); 1900 if (bp == null) { 1901 Slog.w(PackageManagerService.TAG, "Unknown permission: " + name); 1902 XmlUtils.skipCurrentTag(parser); 1903 continue; 1904 } 1905 1906 String grantedStr = parser.getAttributeValue(null, ATTR_GRANTED); 1907 final boolean granted = grantedStr == null 1908 || Boolean.parseBoolean(grantedStr); 1909 1910 String flagsStr = parser.getAttributeValue(null, ATTR_FLAGS); 1911 final int flags = (flagsStr != null) 1912 ? Integer.parseInt(flagsStr, 16) : 0; 1913 1914 if (granted) { 1915 if (permissionsState.grantInstallPermission(bp) == 1916 PermissionsState.PERMISSION_OPERATION_FAILURE) { 1917 Slog.w(PackageManagerService.TAG, "Permission already added: " + name); 1918 XmlUtils.skipCurrentTag(parser); 1919 } else { 1920 permissionsState.updatePermissionFlags(bp, UserHandle.USER_ALL, 1921 PackageManager.MASK_PERMISSION_FLAGS, flags); 1922 } 1923 } else { 1924 if (permissionsState.revokeInstallPermission(bp) == 1925 PermissionsState.PERMISSION_OPERATION_FAILURE) { 1926 Slog.w(PackageManagerService.TAG, "Permission already added: " + name); 1927 XmlUtils.skipCurrentTag(parser); 1928 } else { 1929 permissionsState.updatePermissionFlags(bp, UserHandle.USER_ALL, 1930 PackageManager.MASK_PERMISSION_FLAGS, flags); 1931 } 1932 } 1933 } else { 1934 Slog.w(PackageManagerService.TAG, "Unknown element under <permissions>: " 1935 + parser.getName()); 1936 XmlUtils.skipCurrentTag(parser); 1937 } 1938 } 1939 } 1940 1941 void writePermissionsLPr(XmlSerializer serializer, List<PermissionState> permissionStates) 1942 throws IOException { 1943 if (permissionStates.isEmpty()) { 1944 return; 1945 } 1946 1947 serializer.startTag(null, TAG_PERMISSIONS); 1948 1949 for (PermissionState permissionState : permissionStates) { 1950 serializer.startTag(null, TAG_ITEM); 1951 serializer.attribute(null, ATTR_NAME, permissionState.getName()); 1952 serializer.attribute(null, ATTR_GRANTED, String.valueOf(permissionState.isGranted())); 1953 serializer.attribute(null, ATTR_FLAGS, Integer.toHexString(permissionState.getFlags())); 1954 serializer.endTag(null, TAG_ITEM); 1955 } 1956 1957 serializer.endTag(null, TAG_PERMISSIONS); 1958 } 1959 1960 // Note: assumed "stopped" field is already cleared in all packages. 1961 // Legacy reader, used to read in the old file format after an upgrade. Not used after that. 1962 void readStoppedLPw() { 1963 FileInputStream str = null; 1964 if (mBackupStoppedPackagesFilename.exists()) { 1965 try { 1966 str = new FileInputStream(mBackupStoppedPackagesFilename); 1967 mReadMessages.append("Reading from backup stopped packages file\n"); 1968 PackageManagerService.reportSettingsProblem(Log.INFO, 1969 "Need to read from backup stopped packages file"); 1970 if (mSettingsFilename.exists()) { 1971 // If both the backup and normal file exist, we 1972 // ignore the normal one since it might have been 1973 // corrupted. 1974 Slog.w(PackageManagerService.TAG, "Cleaning up stopped packages file " 1975 + mStoppedPackagesFilename); 1976 mStoppedPackagesFilename.delete(); 1977 } 1978 } catch (java.io.IOException e) { 1979 // We'll try for the normal settings file. 1980 } 1981 } 1982 1983 try { 1984 if (str == null) { 1985 if (!mStoppedPackagesFilename.exists()) { 1986 mReadMessages.append("No stopped packages file found\n"); 1987 PackageManagerService.reportSettingsProblem(Log.INFO, 1988 "No stopped packages file file; assuming all started"); 1989 // At first boot, make sure no packages are stopped. 1990 // We usually want to have third party apps initialize 1991 // in the stopped state, but not at first boot. 1992 for (PackageSetting pkg : mPackages.values()) { 1993 pkg.setStopped(false, 0); 1994 pkg.setNotLaunched(false, 0); 1995 } 1996 return; 1997 } 1998 str = new FileInputStream(mStoppedPackagesFilename); 1999 } 2000 final XmlPullParser parser = Xml.newPullParser(); 2001 parser.setInput(str, null); 2002 2003 int type; 2004 while ((type=parser.next()) != XmlPullParser.START_TAG 2005 && type != XmlPullParser.END_DOCUMENT) { 2006 ; 2007 } 2008 2009 if (type != XmlPullParser.START_TAG) { 2010 mReadMessages.append("No start tag found in stopped packages file\n"); 2011 PackageManagerService.reportSettingsProblem(Log.WARN, 2012 "No start tag found in package manager stopped packages"); 2013 return; 2014 } 2015 2016 int outerDepth = parser.getDepth(); 2017 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 2018 && (type != XmlPullParser.END_TAG 2019 || parser.getDepth() > outerDepth)) { 2020 if (type == XmlPullParser.END_TAG 2021 || type == XmlPullParser.TEXT) { 2022 continue; 2023 } 2024 2025 String tagName = parser.getName(); 2026 if (tagName.equals(TAG_PACKAGE)) { 2027 String name = parser.getAttributeValue(null, ATTR_NAME); 2028 PackageSetting ps = mPackages.get(name); 2029 if (ps != null) { 2030 ps.setStopped(true, 0); 2031 if ("1".equals(parser.getAttributeValue(null, ATTR_NOT_LAUNCHED))) { 2032 ps.setNotLaunched(true, 0); 2033 } 2034 } else { 2035 Slog.w(PackageManagerService.TAG, 2036 "No package known for stopped package: " + name); 2037 } 2038 XmlUtils.skipCurrentTag(parser); 2039 } else { 2040 Slog.w(PackageManagerService.TAG, "Unknown element under <stopped-packages>: " 2041 + parser.getName()); 2042 XmlUtils.skipCurrentTag(parser); 2043 } 2044 } 2045 2046 str.close(); 2047 2048 } catch (XmlPullParserException e) { 2049 mReadMessages.append("Error reading: " + e.toString()); 2050 PackageManagerService.reportSettingsProblem(Log.ERROR, 2051 "Error reading stopped packages: " + e); 2052 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages", 2053 e); 2054 2055 } catch (java.io.IOException e) { 2056 mReadMessages.append("Error reading: " + e.toString()); 2057 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e); 2058 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages", 2059 e); 2060 2061 } 2062 } 2063 2064 void writeLPr() { 2065 //Debug.startMethodTracing("/data/system/packageprof", 8 * 1024 * 1024); 2066 2067 // Keep the old settings around until we know the new ones have 2068 // been successfully written. 2069 if (mSettingsFilename.exists()) { 2070 // Presence of backup settings file indicates that we failed 2071 // to persist settings earlier. So preserve the older 2072 // backup for future reference since the current settings 2073 // might have been corrupted. 2074 if (!mBackupSettingsFilename.exists()) { 2075 if (!mSettingsFilename.renameTo(mBackupSettingsFilename)) { 2076 Slog.wtf(PackageManagerService.TAG, 2077 "Unable to backup package manager settings, " 2078 + " current changes will be lost at reboot"); 2079 return; 2080 } 2081 } else { 2082 mSettingsFilename.delete(); 2083 Slog.w(PackageManagerService.TAG, "Preserving older settings backup"); 2084 } 2085 } 2086 2087 mPastSignatures.clear(); 2088 2089 try { 2090 FileOutputStream fstr = new FileOutputStream(mSettingsFilename); 2091 BufferedOutputStream str = new BufferedOutputStream(fstr); 2092 2093 //XmlSerializer serializer = XmlUtils.serializerInstance(); 2094 XmlSerializer serializer = new FastXmlSerializer(); 2095 serializer.setOutput(str, StandardCharsets.UTF_8.name()); 2096 serializer.startDocument(null, true); 2097 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); 2098 2099 serializer.startTag(null, "packages"); 2100 2101 for (int i = 0; i < mVersion.size(); i++) { 2102 final String volumeUuid = mVersion.keyAt(i); 2103 final VersionInfo ver = mVersion.valueAt(i); 2104 2105 serializer.startTag(null, TAG_VERSION); 2106 XmlUtils.writeStringAttribute(serializer, ATTR_VOLUME_UUID, volumeUuid); 2107 XmlUtils.writeIntAttribute(serializer, ATTR_SDK_VERSION, ver.sdkVersion); 2108 XmlUtils.writeIntAttribute(serializer, ATTR_DATABASE_VERSION, ver.databaseVersion); 2109 XmlUtils.writeStringAttribute(serializer, ATTR_FINGERPRINT, ver.fingerprint); 2110 serializer.endTag(null, TAG_VERSION); 2111 } 2112 2113 if (mVerifierDeviceIdentity != null) { 2114 serializer.startTag(null, "verifier"); 2115 serializer.attribute(null, "device", mVerifierDeviceIdentity.toString()); 2116 serializer.endTag(null, "verifier"); 2117 } 2118 2119 if (mReadExternalStorageEnforced != null) { 2120 serializer.startTag(null, TAG_READ_EXTERNAL_STORAGE); 2121 serializer.attribute( 2122 null, ATTR_ENFORCEMENT, mReadExternalStorageEnforced ? "1" : "0"); 2123 serializer.endTag(null, TAG_READ_EXTERNAL_STORAGE); 2124 } 2125 2126 serializer.startTag(null, "permission-trees"); 2127 for (BasePermission bp : mPermissionTrees.values()) { 2128 writePermissionLPr(serializer, bp); 2129 } 2130 serializer.endTag(null, "permission-trees"); 2131 2132 serializer.startTag(null, "permissions"); 2133 for (BasePermission bp : mPermissions.values()) { 2134 writePermissionLPr(serializer, bp); 2135 } 2136 serializer.endTag(null, "permissions"); 2137 2138 for (final PackageSetting pkg : mPackages.values()) { 2139 writePackageLPr(serializer, pkg); 2140 } 2141 2142 for (final PackageSetting pkg : mDisabledSysPackages.values()) { 2143 writeDisabledSysPackageLPr(serializer, pkg); 2144 } 2145 2146 for (final SharedUserSetting usr : mSharedUsers.values()) { 2147 serializer.startTag(null, "shared-user"); 2148 serializer.attribute(null, ATTR_NAME, usr.name); 2149 serializer.attribute(null, "userId", 2150 Integer.toString(usr.userId)); 2151 usr.signatures.writeXml(serializer, "sigs", mPastSignatures); 2152 writePermissionsLPr(serializer, usr.getPermissionsState() 2153 .getInstallPermissionStates()); 2154 serializer.endTag(null, "shared-user"); 2155 } 2156 2157 if (mPackagesToBeCleaned.size() > 0) { 2158 for (PackageCleanItem item : mPackagesToBeCleaned) { 2159 final String userStr = Integer.toString(item.userId); 2160 serializer.startTag(null, "cleaning-package"); 2161 serializer.attribute(null, ATTR_NAME, item.packageName); 2162 serializer.attribute(null, ATTR_CODE, item.andCode ? "true" : "false"); 2163 serializer.attribute(null, ATTR_USER, userStr); 2164 serializer.endTag(null, "cleaning-package"); 2165 } 2166 } 2167 2168 if (mRenamedPackages.size() > 0) { 2169 for (Map.Entry<String, String> e : mRenamedPackages.entrySet()) { 2170 serializer.startTag(null, "renamed-package"); 2171 serializer.attribute(null, "new", e.getKey()); 2172 serializer.attribute(null, "old", e.getValue()); 2173 serializer.endTag(null, "renamed-package"); 2174 } 2175 } 2176 2177 final int numIVIs = mRestoredIntentFilterVerifications.size(); 2178 if (numIVIs > 0) { 2179 if (DEBUG_DOMAIN_VERIFICATION) { 2180 Slog.i(TAG, "Writing restored-ivi entries to packages.xml"); 2181 } 2182 serializer.startTag(null, "restored-ivi"); 2183 for (int i = 0; i < numIVIs; i++) { 2184 IntentFilterVerificationInfo ivi = mRestoredIntentFilterVerifications.valueAt(i); 2185 writeDomainVerificationsLPr(serializer, ivi); 2186 } 2187 serializer.endTag(null, "restored-ivi"); 2188 } else { 2189 if (DEBUG_DOMAIN_VERIFICATION) { 2190 Slog.i(TAG, " no restored IVI entries to write"); 2191 } 2192 } 2193 2194 mKeySetManagerService.writeKeySetManagerServiceLPr(serializer); 2195 2196 serializer.endTag(null, "packages"); 2197 2198 serializer.endDocument(); 2199 2200 str.flush(); 2201 FileUtils.sync(fstr); 2202 str.close(); 2203 2204 // New settings successfully written, old ones are no longer 2205 // needed. 2206 mBackupSettingsFilename.delete(); 2207 FileUtils.setPermissions(mSettingsFilename.toString(), 2208 FileUtils.S_IRUSR|FileUtils.S_IWUSR 2209 |FileUtils.S_IRGRP|FileUtils.S_IWGRP, 2210 -1, -1); 2211 2212 writePackageListLPr(); 2213 writeAllUsersPackageRestrictionsLPr(); 2214 writeAllRuntimePermissionsLPr(); 2215 return; 2216 2217 } catch(XmlPullParserException e) { 2218 Slog.wtf(PackageManagerService.TAG, "Unable to write package manager settings, " 2219 + "current changes will be lost at reboot", e); 2220 } catch(java.io.IOException e) { 2221 Slog.wtf(PackageManagerService.TAG, "Unable to write package manager settings, " 2222 + "current changes will be lost at reboot", e); 2223 } 2224 // Clean up partially written files 2225 if (mSettingsFilename.exists()) { 2226 if (!mSettingsFilename.delete()) { 2227 Slog.wtf(PackageManagerService.TAG, "Failed to clean up mangled file: " 2228 + mSettingsFilename); 2229 } 2230 } 2231 //Debug.stopMethodTracing(); 2232 } 2233 2234 void writePackageListLPr() { 2235 writePackageListLPr(-1); 2236 } 2237 2238 void writePackageListLPr(int creatingUserId) { 2239 // Only derive GIDs for active users (not dying) 2240 final List<UserInfo> users = UserManagerService.getInstance().getUsers(true); 2241 int[] userIds = new int[users.size()]; 2242 for (int i = 0; i < userIds.length; i++) { 2243 userIds[i] = users.get(i).id; 2244 } 2245 if (creatingUserId != -1) { 2246 userIds = ArrayUtils.appendInt(userIds, creatingUserId); 2247 } 2248 2249 // Write package list file now, use a JournaledFile. 2250 File tempFile = new File(mPackageListFilename.getAbsolutePath() + ".tmp"); 2251 JournaledFile journal = new JournaledFile(mPackageListFilename, tempFile); 2252 2253 final File writeTarget = journal.chooseForWrite(); 2254 FileOutputStream fstr = null; 2255 BufferedOutputStream str = null; 2256 try { 2257 fstr = new FileOutputStream(writeTarget); 2258 str = new BufferedOutputStream(fstr); 2259 FileUtils.setPermissions(fstr.getFD(), 0640, SYSTEM_UID, PACKAGE_INFO_GID); 2260 2261 StringBuilder sb = new StringBuilder(); 2262 for (final PackageSetting pkg : mPackages.values()) { 2263 if (pkg.pkg == null || pkg.pkg.applicationInfo == null 2264 || pkg.pkg.applicationInfo.dataDir == null) { 2265 Slog.w(TAG, "Skipping " + pkg + " due to missing metadata"); 2266 continue; 2267 } 2268 2269 final ApplicationInfo ai = pkg.pkg.applicationInfo; 2270 final String dataPath = new File(ai.dataDir).getCanonicalPath(); 2271 final boolean isDebug = (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; 2272 final int[] gids = pkg.getPermissionsState().computeGids(userIds); 2273 2274 // Avoid any application that has a space in its path. 2275 if (dataPath.indexOf(" ") >= 0) 2276 continue; 2277 2278 // we store on each line the following information for now: 2279 // 2280 // pkgName - package name 2281 // userId - application-specific user id 2282 // debugFlag - 0 or 1 if the package is debuggable. 2283 // dataPath - path to package's data path 2284 // seinfo - seinfo label for the app (assigned at install time) 2285 // gids - supplementary gids this app launches with 2286 // 2287 // NOTE: We prefer not to expose all ApplicationInfo flags for now. 2288 // 2289 // DO NOT MODIFY THIS FORMAT UNLESS YOU CAN ALSO MODIFY ITS USERS 2290 // FROM NATIVE CODE. AT THE MOMENT, LOOK AT THE FOLLOWING SOURCES: 2291 // frameworks/base/libs/packagelistparser 2292 // system/core/run-as/run-as.c 2293 // 2294 sb.setLength(0); 2295 sb.append(ai.packageName); 2296 sb.append(" "); 2297 sb.append((int)ai.uid); 2298 sb.append(isDebug ? " 1 " : " 0 "); 2299 sb.append(dataPath); 2300 sb.append(" "); 2301 sb.append(ai.seinfo); 2302 sb.append(" "); 2303 if (gids != null && gids.length > 0) { 2304 sb.append(gids[0]); 2305 for (int i = 1; i < gids.length; i++) { 2306 sb.append(","); 2307 sb.append(gids[i]); 2308 } 2309 } else { 2310 sb.append("none"); 2311 } 2312 sb.append("\n"); 2313 str.write(sb.toString().getBytes()); 2314 } 2315 str.flush(); 2316 FileUtils.sync(fstr); 2317 str.close(); 2318 journal.commit(); 2319 } catch (Exception e) { 2320 Slog.wtf(TAG, "Failed to write packages.list", e); 2321 IoUtils.closeQuietly(str); 2322 journal.rollback(); 2323 } 2324 } 2325 2326 void writeDisabledSysPackageLPr(XmlSerializer serializer, final PackageSetting pkg) 2327 throws java.io.IOException { 2328 serializer.startTag(null, "updated-package"); 2329 serializer.attribute(null, ATTR_NAME, pkg.name); 2330 if (pkg.realName != null) { 2331 serializer.attribute(null, "realName", pkg.realName); 2332 } 2333 serializer.attribute(null, "codePath", pkg.codePathString); 2334 serializer.attribute(null, "ft", Long.toHexString(pkg.timeStamp)); 2335 serializer.attribute(null, "it", Long.toHexString(pkg.firstInstallTime)); 2336 serializer.attribute(null, "ut", Long.toHexString(pkg.lastUpdateTime)); 2337 serializer.attribute(null, "version", String.valueOf(pkg.versionCode)); 2338 if (!pkg.resourcePathString.equals(pkg.codePathString)) { 2339 serializer.attribute(null, "resourcePath", pkg.resourcePathString); 2340 } 2341 if (pkg.legacyNativeLibraryPathString != null) { 2342 serializer.attribute(null, "nativeLibraryPath", pkg.legacyNativeLibraryPathString); 2343 } 2344 if (pkg.primaryCpuAbiString != null) { 2345 serializer.attribute(null, "primaryCpuAbi", pkg.primaryCpuAbiString); 2346 } 2347 if (pkg.secondaryCpuAbiString != null) { 2348 serializer.attribute(null, "secondaryCpuAbi", pkg.secondaryCpuAbiString); 2349 } 2350 if (pkg.cpuAbiOverrideString != null) { 2351 serializer.attribute(null, "cpuAbiOverride", pkg.cpuAbiOverrideString); 2352 } 2353 2354 if (pkg.sharedUser == null) { 2355 serializer.attribute(null, "userId", Integer.toString(pkg.appId)); 2356 } else { 2357 serializer.attribute(null, "sharedUserId", Integer.toString(pkg.appId)); 2358 } 2359 2360 // If this is a shared user, the permissions will be written there. 2361 if (pkg.sharedUser == null) { 2362 writePermissionsLPr(serializer, pkg.getPermissionsState() 2363 .getInstallPermissionStates()); 2364 } 2365 2366 serializer.endTag(null, "updated-package"); 2367 } 2368 2369 void writePackageLPr(XmlSerializer serializer, final PackageSetting pkg) 2370 throws java.io.IOException { 2371 serializer.startTag(null, "package"); 2372 serializer.attribute(null, ATTR_NAME, pkg.name); 2373 if (pkg.realName != null) { 2374 serializer.attribute(null, "realName", pkg.realName); 2375 } 2376 serializer.attribute(null, "codePath", pkg.codePathString); 2377 if (!pkg.resourcePathString.equals(pkg.codePathString)) { 2378 serializer.attribute(null, "resourcePath", pkg.resourcePathString); 2379 } 2380 2381 if (pkg.legacyNativeLibraryPathString != null) { 2382 serializer.attribute(null, "nativeLibraryPath", pkg.legacyNativeLibraryPathString); 2383 } 2384 if (pkg.primaryCpuAbiString != null) { 2385 serializer.attribute(null, "primaryCpuAbi", pkg.primaryCpuAbiString); 2386 } 2387 if (pkg.secondaryCpuAbiString != null) { 2388 serializer.attribute(null, "secondaryCpuAbi", pkg.secondaryCpuAbiString); 2389 } 2390 if (pkg.cpuAbiOverrideString != null) { 2391 serializer.attribute(null, "cpuAbiOverride", pkg.cpuAbiOverrideString); 2392 } 2393 2394 serializer.attribute(null, "publicFlags", Integer.toString(pkg.pkgFlags)); 2395 serializer.attribute(null, "privateFlags", Integer.toString(pkg.pkgPrivateFlags)); 2396 serializer.attribute(null, "ft", Long.toHexString(pkg.timeStamp)); 2397 serializer.attribute(null, "it", Long.toHexString(pkg.firstInstallTime)); 2398 serializer.attribute(null, "ut", Long.toHexString(pkg.lastUpdateTime)); 2399 serializer.attribute(null, "version", String.valueOf(pkg.versionCode)); 2400 if (pkg.sharedUser == null) { 2401 serializer.attribute(null, "userId", Integer.toString(pkg.appId)); 2402 } else { 2403 serializer.attribute(null, "sharedUserId", Integer.toString(pkg.appId)); 2404 } 2405 if (pkg.uidError) { 2406 serializer.attribute(null, "uidError", "true"); 2407 } 2408 if (pkg.installStatus == PackageSettingBase.PKG_INSTALL_INCOMPLETE) { 2409 serializer.attribute(null, "installStatus", "false"); 2410 } 2411 if (pkg.installerPackageName != null) { 2412 serializer.attribute(null, "installer", pkg.installerPackageName); 2413 } 2414 if (pkg.volumeUuid != null) { 2415 serializer.attribute(null, "volumeUuid", pkg.volumeUuid); 2416 } 2417 pkg.signatures.writeXml(serializer, "sigs", mPastSignatures); 2418 2419 writePermissionsLPr(serializer, pkg.getPermissionsState() 2420 .getInstallPermissionStates()); 2421 2422 writeSigningKeySetLPr(serializer, pkg.keySetData); 2423 writeUpgradeKeySetsLPr(serializer, pkg.keySetData); 2424 writeKeySetAliasesLPr(serializer, pkg.keySetData); 2425 writeDomainVerificationsLPr(serializer, pkg.verificationInfo); 2426 2427 serializer.endTag(null, "package"); 2428 } 2429 2430 void writeSigningKeySetLPr(XmlSerializer serializer, 2431 PackageKeySetData data) throws IOException { 2432 serializer.startTag(null, "proper-signing-keyset"); 2433 serializer.attribute(null, "identifier", 2434 Long.toString(data.getProperSigningKeySet())); 2435 serializer.endTag(null, "proper-signing-keyset"); 2436 } 2437 2438 void writeUpgradeKeySetsLPr(XmlSerializer serializer, 2439 PackageKeySetData data) throws IOException { 2440 long properSigning = data.getProperSigningKeySet(); 2441 if (data.isUsingUpgradeKeySets()) { 2442 for (long id : data.getUpgradeKeySets()) { 2443 serializer.startTag(null, "upgrade-keyset"); 2444 serializer.attribute(null, "identifier", Long.toString(id)); 2445 serializer.endTag(null, "upgrade-keyset"); 2446 } 2447 } 2448 } 2449 2450 void writeKeySetAliasesLPr(XmlSerializer serializer, 2451 PackageKeySetData data) throws IOException { 2452 for (Map.Entry<String, Long> e: data.getAliases().entrySet()) { 2453 serializer.startTag(null, "defined-keyset"); 2454 serializer.attribute(null, "alias", e.getKey()); 2455 serializer.attribute(null, "identifier", Long.toString(e.getValue())); 2456 serializer.endTag(null, "defined-keyset"); 2457 } 2458 } 2459 2460 void writePermissionLPr(XmlSerializer serializer, BasePermission bp) 2461 throws XmlPullParserException, java.io.IOException { 2462 if (bp.type != BasePermission.TYPE_BUILTIN && bp.sourcePackage != null) { 2463 serializer.startTag(null, TAG_ITEM); 2464 serializer.attribute(null, ATTR_NAME, bp.name); 2465 serializer.attribute(null, "package", bp.sourcePackage); 2466 if (bp.protectionLevel != PermissionInfo.PROTECTION_NORMAL) { 2467 serializer.attribute(null, "protection", Integer.toString(bp.protectionLevel)); 2468 } 2469 if (PackageManagerService.DEBUG_SETTINGS) 2470 Log.v(PackageManagerService.TAG, "Writing perm: name=" + bp.name + " type=" 2471 + bp.type); 2472 if (bp.type == BasePermission.TYPE_DYNAMIC) { 2473 final PermissionInfo pi = bp.perm != null ? bp.perm.info : bp.pendingInfo; 2474 if (pi != null) { 2475 serializer.attribute(null, "type", "dynamic"); 2476 if (pi.icon != 0) { 2477 serializer.attribute(null, "icon", Integer.toString(pi.icon)); 2478 } 2479 if (pi.nonLocalizedLabel != null) { 2480 serializer.attribute(null, "label", pi.nonLocalizedLabel.toString()); 2481 } 2482 } 2483 } 2484 serializer.endTag(null, TAG_ITEM); 2485 } 2486 } 2487 2488 ArrayList<PackageSetting> getListOfIncompleteInstallPackagesLPr() { 2489 final ArraySet<String> kList = new ArraySet<String>(mPackages.keySet()); 2490 final Iterator<String> its = kList.iterator(); 2491 final ArrayList<PackageSetting> ret = new ArrayList<PackageSetting>(); 2492 while (its.hasNext()) { 2493 final String key = its.next(); 2494 final PackageSetting ps = mPackages.get(key); 2495 if (ps.getInstallStatus() == PackageSettingBase.PKG_INSTALL_INCOMPLETE) { 2496 ret.add(ps); 2497 } 2498 } 2499 return ret; 2500 } 2501 2502 void addPackageToCleanLPw(PackageCleanItem pkg) { 2503 if (!mPackagesToBeCleaned.contains(pkg)) { 2504 mPackagesToBeCleaned.add(pkg); 2505 } 2506 } 2507 2508 boolean readLPw(@NonNull List<UserInfo> users) { 2509 FileInputStream str = null; 2510 if (mBackupSettingsFilename.exists()) { 2511 try { 2512 str = new FileInputStream(mBackupSettingsFilename); 2513 mReadMessages.append("Reading from backup settings file\n"); 2514 PackageManagerService.reportSettingsProblem(Log.INFO, 2515 "Need to read from backup settings file"); 2516 if (mSettingsFilename.exists()) { 2517 // If both the backup and settings file exist, we 2518 // ignore the settings since it might have been 2519 // corrupted. 2520 Slog.w(PackageManagerService.TAG, "Cleaning up settings file " 2521 + mSettingsFilename); 2522 mSettingsFilename.delete(); 2523 } 2524 } catch (java.io.IOException e) { 2525 // We'll try for the normal settings file. 2526 } 2527 } 2528 2529 mPendingPackages.clear(); 2530 mPastSignatures.clear(); 2531 mKeySetRefs.clear(); 2532 2533 try { 2534 if (str == null) { 2535 if (!mSettingsFilename.exists()) { 2536 mReadMessages.append("No settings file found\n"); 2537 PackageManagerService.reportSettingsProblem(Log.INFO, 2538 "No settings file; creating initial state"); 2539 // It's enough to just touch version details to create them 2540 // with default values 2541 findOrCreateVersion(StorageManager.UUID_PRIVATE_INTERNAL); 2542 findOrCreateVersion(StorageManager.UUID_PRIMARY_PHYSICAL); 2543 return false; 2544 } 2545 str = new FileInputStream(mSettingsFilename); 2546 } 2547 XmlPullParser parser = Xml.newPullParser(); 2548 parser.setInput(str, StandardCharsets.UTF_8.name()); 2549 2550 int type; 2551 while ((type = parser.next()) != XmlPullParser.START_TAG 2552 && type != XmlPullParser.END_DOCUMENT) { 2553 ; 2554 } 2555 2556 if (type != XmlPullParser.START_TAG) { 2557 mReadMessages.append("No start tag found in settings file\n"); 2558 PackageManagerService.reportSettingsProblem(Log.WARN, 2559 "No start tag found in package manager settings"); 2560 Slog.wtf(PackageManagerService.TAG, 2561 "No start tag found in package manager settings"); 2562 return false; 2563 } 2564 2565 int outerDepth = parser.getDepth(); 2566 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 2567 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 2568 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 2569 continue; 2570 } 2571 2572 String tagName = parser.getName(); 2573 if (tagName.equals("package")) { 2574 readPackageLPw(parser); 2575 } else if (tagName.equals("permissions")) { 2576 readPermissionsLPw(mPermissions, parser); 2577 } else if (tagName.equals("permission-trees")) { 2578 readPermissionsLPw(mPermissionTrees, parser); 2579 } else if (tagName.equals("shared-user")) { 2580 readSharedUserLPw(parser); 2581 } else if (tagName.equals("preferred-packages")) { 2582 // no longer used. 2583 } else if (tagName.equals("preferred-activities")) { 2584 // Upgrading from old single-user implementation; 2585 // these are the preferred activities for user 0. 2586 readPreferredActivitiesLPw(parser, 0); 2587 } else if (tagName.equals(TAG_PERSISTENT_PREFERRED_ACTIVITIES)) { 2588 // TODO: check whether this is okay! as it is very 2589 // similar to how preferred-activities are treated 2590 readPersistentPreferredActivitiesLPw(parser, 0); 2591 } else if (tagName.equals(TAG_CROSS_PROFILE_INTENT_FILTERS)) { 2592 // TODO: check whether this is okay! as it is very 2593 // similar to how preferred-activities are treated 2594 readCrossProfileIntentFiltersLPw(parser, 0); 2595 } else if (tagName.equals(TAG_DEFAULT_BROWSER)) { 2596 readDefaultAppsLPw(parser, 0); 2597 } else if (tagName.equals("updated-package")) { 2598 readDisabledSysPackageLPw(parser); 2599 } else if (tagName.equals("cleaning-package")) { 2600 String name = parser.getAttributeValue(null, ATTR_NAME); 2601 String userStr = parser.getAttributeValue(null, ATTR_USER); 2602 String codeStr = parser.getAttributeValue(null, ATTR_CODE); 2603 if (name != null) { 2604 int userId = UserHandle.USER_SYSTEM; 2605 boolean andCode = true; 2606 try { 2607 if (userStr != null) { 2608 userId = Integer.parseInt(userStr); 2609 } 2610 } catch (NumberFormatException e) { 2611 } 2612 if (codeStr != null) { 2613 andCode = Boolean.parseBoolean(codeStr); 2614 } 2615 addPackageToCleanLPw(new PackageCleanItem(userId, name, andCode)); 2616 } 2617 } else if (tagName.equals("renamed-package")) { 2618 String nname = parser.getAttributeValue(null, "new"); 2619 String oname = parser.getAttributeValue(null, "old"); 2620 if (nname != null && oname != null) { 2621 mRenamedPackages.put(nname, oname); 2622 } 2623 } else if (tagName.equals("restored-ivi")) { 2624 readRestoredIntentFilterVerifications(parser); 2625 } else if (tagName.equals("last-platform-version")) { 2626 // Upgrade from older XML schema 2627 final VersionInfo internal = findOrCreateVersion( 2628 StorageManager.UUID_PRIVATE_INTERNAL); 2629 final VersionInfo external = findOrCreateVersion( 2630 StorageManager.UUID_PRIMARY_PHYSICAL); 2631 2632 internal.sdkVersion = XmlUtils.readIntAttribute(parser, "internal", 0); 2633 external.sdkVersion = XmlUtils.readIntAttribute(parser, "external", 0); 2634 internal.fingerprint = external.fingerprint = 2635 XmlUtils.readStringAttribute(parser, "fingerprint"); 2636 2637 } else if (tagName.equals("database-version")) { 2638 // Upgrade from older XML schema 2639 final VersionInfo internal = findOrCreateVersion( 2640 StorageManager.UUID_PRIVATE_INTERNAL); 2641 final VersionInfo external = findOrCreateVersion( 2642 StorageManager.UUID_PRIMARY_PHYSICAL); 2643 2644 internal.databaseVersion = XmlUtils.readIntAttribute(parser, "internal", 0); 2645 external.databaseVersion = XmlUtils.readIntAttribute(parser, "external", 0); 2646 2647 } else if (tagName.equals("verifier")) { 2648 final String deviceIdentity = parser.getAttributeValue(null, "device"); 2649 try { 2650 mVerifierDeviceIdentity = VerifierDeviceIdentity.parse(deviceIdentity); 2651 } catch (IllegalArgumentException e) { 2652 Slog.w(PackageManagerService.TAG, "Discard invalid verifier device id: " 2653 + e.getMessage()); 2654 } 2655 } else if (TAG_READ_EXTERNAL_STORAGE.equals(tagName)) { 2656 final String enforcement = parser.getAttributeValue(null, ATTR_ENFORCEMENT); 2657 mReadExternalStorageEnforced = "1".equals(enforcement); 2658 } else if (tagName.equals("keyset-settings")) { 2659 mKeySetManagerService.readKeySetsLPw(parser, mKeySetRefs); 2660 } else if (TAG_VERSION.equals(tagName)) { 2661 final String volumeUuid = XmlUtils.readStringAttribute(parser, 2662 ATTR_VOLUME_UUID); 2663 final VersionInfo ver = findOrCreateVersion(volumeUuid); 2664 ver.sdkVersion = XmlUtils.readIntAttribute(parser, ATTR_SDK_VERSION); 2665 ver.databaseVersion = XmlUtils.readIntAttribute(parser, ATTR_SDK_VERSION); 2666 ver.fingerprint = XmlUtils.readStringAttribute(parser, ATTR_FINGERPRINT); 2667 2668 } else { 2669 Slog.w(PackageManagerService.TAG, "Unknown element under <packages>: " 2670 + parser.getName()); 2671 XmlUtils.skipCurrentTag(parser); 2672 } 2673 } 2674 2675 str.close(); 2676 2677 } catch (XmlPullParserException e) { 2678 mReadMessages.append("Error reading: " + e.toString()); 2679 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e); 2680 Slog.wtf(PackageManagerService.TAG, "Error reading package manager settings", e); 2681 2682 } catch (java.io.IOException e) { 2683 mReadMessages.append("Error reading: " + e.toString()); 2684 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e); 2685 Slog.wtf(PackageManagerService.TAG, "Error reading package manager settings", e); 2686 } 2687 2688 // If the build is setup to drop runtime permissions 2689 // on update drop the files before loading them. 2690 if (PackageManagerService.CLEAR_RUNTIME_PERMISSIONS_ON_UPGRADE) { 2691 final VersionInfo internal = getInternalVersion(); 2692 if (!Build.FINGERPRINT.equals(internal.fingerprint)) { 2693 for (UserInfo user : users) { 2694 mRuntimePermissionsPersistence.deleteUserRuntimePermissionsFile(user.id); 2695 } 2696 } 2697 } 2698 2699 final int N = mPendingPackages.size(); 2700 2701 for (int i = 0; i < N; i++) { 2702 final PendingPackage pp = mPendingPackages.get(i); 2703 Object idObj = getUserIdLPr(pp.sharedId); 2704 if (idObj != null && idObj instanceof SharedUserSetting) { 2705 PackageSetting p = getPackageLPw(pp.name, null, pp.realName, 2706 (SharedUserSetting) idObj, pp.codePath, pp.resourcePath, 2707 pp.legacyNativeLibraryPathString, pp.primaryCpuAbiString, 2708 pp.secondaryCpuAbiString, pp.versionCode, pp.pkgFlags, pp.pkgPrivateFlags, 2709 null, true /* add */, false /* allowInstall */); 2710 if (p == null) { 2711 PackageManagerService.reportSettingsProblem(Log.WARN, 2712 "Unable to create application package for " + pp.name); 2713 continue; 2714 } 2715 p.copyFrom(pp); 2716 } else if (idObj != null) { 2717 String msg = "Bad package setting: package " + pp.name + " has shared uid " 2718 + pp.sharedId + " that is not a shared uid\n"; 2719 mReadMessages.append(msg); 2720 PackageManagerService.reportSettingsProblem(Log.ERROR, msg); 2721 } else { 2722 String msg = "Bad package setting: package " + pp.name + " has shared uid " 2723 + pp.sharedId + " that is not defined\n"; 2724 mReadMessages.append(msg); 2725 PackageManagerService.reportSettingsProblem(Log.ERROR, msg); 2726 } 2727 } 2728 mPendingPackages.clear(); 2729 2730 if (mBackupStoppedPackagesFilename.exists() 2731 || mStoppedPackagesFilename.exists()) { 2732 // Read old file 2733 readStoppedLPw(); 2734 mBackupStoppedPackagesFilename.delete(); 2735 mStoppedPackagesFilename.delete(); 2736 // Migrate to new file format 2737 writePackageRestrictionsLPr(UserHandle.USER_SYSTEM); 2738 } else { 2739 for (UserInfo user : users) { 2740 readPackageRestrictionsLPr(user.id); 2741 } 2742 } 2743 2744 for (UserInfo user : users) { 2745 mRuntimePermissionsPersistence.readStateForUserSyncLPr(user.id); 2746 } 2747 2748 /* 2749 * Make sure all the updated system packages have their shared users 2750 * associated with them. 2751 */ 2752 final Iterator<PackageSetting> disabledIt = mDisabledSysPackages.values().iterator(); 2753 while (disabledIt.hasNext()) { 2754 final PackageSetting disabledPs = disabledIt.next(); 2755 final Object id = getUserIdLPr(disabledPs.appId); 2756 if (id != null && id instanceof SharedUserSetting) { 2757 disabledPs.sharedUser = (SharedUserSetting) id; 2758 } 2759 } 2760 2761 mReadMessages.append("Read completed successfully: " + mPackages.size() + " packages, " 2762 + mSharedUsers.size() + " shared uids\n"); 2763 2764 return true; 2765 } 2766 2767 void applyDefaultPreferredAppsLPw(PackageManagerService service, int userId) { 2768 // First pull data from any pre-installed apps. 2769 for (PackageSetting ps : mPackages.values()) { 2770 if ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) != 0 && ps.pkg != null 2771 && ps.pkg.preferredActivityFilters != null) { 2772 ArrayList<PackageParser.ActivityIntentInfo> intents 2773 = ps.pkg.preferredActivityFilters; 2774 for (int i=0; i<intents.size(); i++) { 2775 PackageParser.ActivityIntentInfo aii = intents.get(i); 2776 applyDefaultPreferredActivityLPw(service, aii, new ComponentName( 2777 ps.name, aii.activity.className), userId); 2778 } 2779 } 2780 } 2781 2782 // Read preferred apps from .../etc/preferred-apps directory. 2783 File preferredDir = new File(Environment.getRootDirectory(), "etc/preferred-apps"); 2784 if (!preferredDir.exists() || !preferredDir.isDirectory()) { 2785 return; 2786 } 2787 if (!preferredDir.canRead()) { 2788 Slog.w(TAG, "Directory " + preferredDir + " cannot be read"); 2789 return; 2790 } 2791 2792 // Iterate over the files in the directory and scan .xml files 2793 for (File f : preferredDir.listFiles()) { 2794 if (!f.getPath().endsWith(".xml")) { 2795 Slog.i(TAG, "Non-xml file " + f + " in " + preferredDir + " directory, ignoring"); 2796 continue; 2797 } 2798 if (!f.canRead()) { 2799 Slog.w(TAG, "Preferred apps file " + f + " cannot be read"); 2800 continue; 2801 } 2802 2803 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Reading default preferred " + f); 2804 InputStream str = null; 2805 try { 2806 str = new BufferedInputStream(new FileInputStream(f)); 2807 XmlPullParser parser = Xml.newPullParser(); 2808 parser.setInput(str, null); 2809 2810 int type; 2811 while ((type = parser.next()) != XmlPullParser.START_TAG 2812 && type != XmlPullParser.END_DOCUMENT) { 2813 ; 2814 } 2815 2816 if (type != XmlPullParser.START_TAG) { 2817 Slog.w(TAG, "Preferred apps file " + f + " does not have start tag"); 2818 continue; 2819 } 2820 if (!"preferred-activities".equals(parser.getName())) { 2821 Slog.w(TAG, "Preferred apps file " + f 2822 + " does not start with 'preferred-activities'"); 2823 continue; 2824 } 2825 readDefaultPreferredActivitiesLPw(service, parser, userId); 2826 } catch (XmlPullParserException e) { 2827 Slog.w(TAG, "Error reading apps file " + f, e); 2828 } catch (IOException e) { 2829 Slog.w(TAG, "Error reading apps file " + f, e); 2830 } finally { 2831 if (str != null) { 2832 try { 2833 str.close(); 2834 } catch (IOException e) { 2835 } 2836 } 2837 } 2838 } 2839 } 2840 2841 private void applyDefaultPreferredActivityLPw(PackageManagerService service, 2842 IntentFilter tmpPa, ComponentName cn, int userId) { 2843 // The initial preferences only specify the target activity 2844 // component and intent-filter, not the set of matches. So we 2845 // now need to query for the matches to build the correct 2846 // preferred activity entry. 2847 if (PackageManagerService.DEBUG_PREFERRED) { 2848 Log.d(TAG, "Processing preferred:"); 2849 tmpPa.dump(new LogPrinter(Log.DEBUG, TAG), " "); 2850 } 2851 Intent intent = new Intent(); 2852 int flags = 0; 2853 intent.setAction(tmpPa.getAction(0)); 2854 for (int i=0; i<tmpPa.countCategories(); i++) { 2855 String cat = tmpPa.getCategory(i); 2856 if (cat.equals(Intent.CATEGORY_DEFAULT)) { 2857 flags |= PackageManager.MATCH_DEFAULT_ONLY; 2858 } else { 2859 intent.addCategory(cat); 2860 } 2861 } 2862 2863 boolean doNonData = true; 2864 boolean hasSchemes = false; 2865 2866 for (int ischeme=0; ischeme<tmpPa.countDataSchemes(); ischeme++) { 2867 boolean doScheme = true; 2868 String scheme = tmpPa.getDataScheme(ischeme); 2869 if (scheme != null && !scheme.isEmpty()) { 2870 hasSchemes = true; 2871 } 2872 for (int issp=0; issp<tmpPa.countDataSchemeSpecificParts(); issp++) { 2873 Uri.Builder builder = new Uri.Builder(); 2874 builder.scheme(scheme); 2875 PatternMatcher ssp = tmpPa.getDataSchemeSpecificPart(issp); 2876 builder.opaquePart(ssp.getPath()); 2877 Intent finalIntent = new Intent(intent); 2878 finalIntent.setData(builder.build()); 2879 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn, 2880 scheme, ssp, null, null, userId); 2881 doScheme = false; 2882 } 2883 for (int iauth=0; iauth<tmpPa.countDataAuthorities(); iauth++) { 2884 boolean doAuth = true; 2885 IntentFilter.AuthorityEntry auth = tmpPa.getDataAuthority(iauth); 2886 for (int ipath=0; ipath<tmpPa.countDataPaths(); ipath++) { 2887 Uri.Builder builder = new Uri.Builder(); 2888 builder.scheme(scheme); 2889 if (auth.getHost() != null) { 2890 builder.authority(auth.getHost()); 2891 } 2892 PatternMatcher path = tmpPa.getDataPath(ipath); 2893 builder.path(path.getPath()); 2894 Intent finalIntent = new Intent(intent); 2895 finalIntent.setData(builder.build()); 2896 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn, 2897 scheme, null, auth, path, userId); 2898 doAuth = doScheme = false; 2899 } 2900 if (doAuth) { 2901 Uri.Builder builder = new Uri.Builder(); 2902 builder.scheme(scheme); 2903 if (auth.getHost() != null) { 2904 builder.authority(auth.getHost()); 2905 } 2906 Intent finalIntent = new Intent(intent); 2907 finalIntent.setData(builder.build()); 2908 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn, 2909 scheme, null, auth, null, userId); 2910 doScheme = false; 2911 } 2912 } 2913 if (doScheme) { 2914 Uri.Builder builder = new Uri.Builder(); 2915 builder.scheme(scheme); 2916 Intent finalIntent = new Intent(intent); 2917 finalIntent.setData(builder.build()); 2918 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn, 2919 scheme, null, null, null, userId); 2920 } 2921 doNonData = false; 2922 } 2923 2924 for (int idata=0; idata<tmpPa.countDataTypes(); idata++) { 2925 String mimeType = tmpPa.getDataType(idata); 2926 if (hasSchemes) { 2927 Uri.Builder builder = new Uri.Builder(); 2928 for (int ischeme=0; ischeme<tmpPa.countDataSchemes(); ischeme++) { 2929 String scheme = tmpPa.getDataScheme(ischeme); 2930 if (scheme != null && !scheme.isEmpty()) { 2931 Intent finalIntent = new Intent(intent); 2932 builder.scheme(scheme); 2933 finalIntent.setDataAndType(builder.build(), mimeType); 2934 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn, 2935 scheme, null, null, null, userId); 2936 } 2937 } 2938 } else { 2939 Intent finalIntent = new Intent(intent); 2940 finalIntent.setType(mimeType); 2941 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn, 2942 null, null, null, null, userId); 2943 } 2944 doNonData = false; 2945 } 2946 2947 if (doNonData) { 2948 applyDefaultPreferredActivityLPw(service, intent, flags, cn, 2949 null, null, null, null, userId); 2950 } 2951 } 2952 2953 private void applyDefaultPreferredActivityLPw(PackageManagerService service, 2954 Intent intent, int flags, ComponentName cn, String scheme, PatternMatcher ssp, 2955 IntentFilter.AuthorityEntry auth, PatternMatcher path, int userId) { 2956 List<ResolveInfo> ri = service.mActivities.queryIntent(intent, 2957 intent.getType(), flags, 0); 2958 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Queried " + intent 2959 + " results: " + ri); 2960 int systemMatch = 0; 2961 int thirdPartyMatch = 0; 2962 if (ri != null && ri.size() > 1) { 2963 boolean haveAct = false; 2964 ComponentName haveNonSys = null; 2965 ComponentName[] set = new ComponentName[ri.size()]; 2966 for (int i=0; i<ri.size(); i++) { 2967 ActivityInfo ai = ri.get(i).activityInfo; 2968 set[i] = new ComponentName(ai.packageName, ai.name); 2969 if ((ai.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { 2970 if (ri.get(i).match >= thirdPartyMatch) { 2971 // Keep track of the best match we find of all third 2972 // party apps, for use later to determine if we actually 2973 // want to set a preferred app for this intent. 2974 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result " 2975 + ai.packageName + "/" + ai.name + ": non-system!"); 2976 haveNonSys = set[i]; 2977 break; 2978 } 2979 } else if (cn.getPackageName().equals(ai.packageName) 2980 && cn.getClassName().equals(ai.name)) { 2981 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result " 2982 + ai.packageName + "/" + ai.name + ": default!"); 2983 haveAct = true; 2984 systemMatch = ri.get(i).match; 2985 } else { 2986 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result " 2987 + ai.packageName + "/" + ai.name + ": skipped"); 2988 } 2989 } 2990 if (haveNonSys != null && thirdPartyMatch < systemMatch) { 2991 // If we have a matching third party app, but its match is not as 2992 // good as the built-in system app, then we don't want to actually 2993 // consider it a match because presumably the built-in app is still 2994 // the thing we want users to see by default. 2995 haveNonSys = null; 2996 } 2997 if (haveAct && haveNonSys == null) { 2998 IntentFilter filter = new IntentFilter(); 2999 if (intent.getAction() != null) { 3000 filter.addAction(intent.getAction()); 3001 } 3002 if (intent.getCategories() != null) { 3003 for (String cat : intent.getCategories()) { 3004 filter.addCategory(cat); 3005 } 3006 } 3007 if ((flags&PackageManager.MATCH_DEFAULT_ONLY) != 0) { 3008 filter.addCategory(Intent.CATEGORY_DEFAULT); 3009 } 3010 if (scheme != null) { 3011 filter.addDataScheme(scheme); 3012 } 3013 if (ssp != null) { 3014 filter.addDataSchemeSpecificPart(ssp.getPath(), ssp.getType()); 3015 } 3016 if (auth != null) { 3017 filter.addDataAuthority(auth); 3018 } 3019 if (path != null) { 3020 filter.addDataPath(path); 3021 } 3022 if (intent.getType() != null) { 3023 try { 3024 filter.addDataType(intent.getType()); 3025 } catch (IntentFilter.MalformedMimeTypeException ex) { 3026 Slog.w(TAG, "Malformed mimetype " + intent.getType() + " for " + cn); 3027 } 3028 } 3029 PreferredActivity pa = new PreferredActivity(filter, systemMatch, set, cn, true); 3030 editPreferredActivitiesLPw(userId).addFilter(pa); 3031 } else if (haveNonSys == null) { 3032 StringBuilder sb = new StringBuilder(); 3033 sb.append("No component "); 3034 sb.append(cn.flattenToShortString()); 3035 sb.append(" found setting preferred "); 3036 sb.append(intent); 3037 sb.append("; possible matches are "); 3038 for (int i=0; i<set.length; i++) { 3039 if (i > 0) sb.append(", "); 3040 sb.append(set[i].flattenToShortString()); 3041 } 3042 Slog.w(TAG, sb.toString()); 3043 } else { 3044 Slog.i(TAG, "Not setting preferred " + intent + "; found third party match " 3045 + haveNonSys.flattenToShortString()); 3046 } 3047 } else { 3048 Slog.w(TAG, "No potential matches found for " + intent + " while setting preferred " 3049 + cn.flattenToShortString()); 3050 } 3051 } 3052 3053 private void readDefaultPreferredActivitiesLPw(PackageManagerService service, 3054 XmlPullParser parser, int userId) 3055 throws XmlPullParserException, IOException { 3056 int outerDepth = parser.getDepth(); 3057 int type; 3058 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 3059 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 3060 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 3061 continue; 3062 } 3063 3064 String tagName = parser.getName(); 3065 if (tagName.equals(TAG_ITEM)) { 3066 PreferredActivity tmpPa = new PreferredActivity(parser); 3067 if (tmpPa.mPref.getParseError() == null) { 3068 applyDefaultPreferredActivityLPw(service, tmpPa, tmpPa.mPref.mComponent, 3069 userId); 3070 } else { 3071 PackageManagerService.reportSettingsProblem(Log.WARN, 3072 "Error in package manager settings: <preferred-activity> " 3073 + tmpPa.mPref.getParseError() + " at " 3074 + parser.getPositionDescription()); 3075 } 3076 } else { 3077 PackageManagerService.reportSettingsProblem(Log.WARN, 3078 "Unknown element under <preferred-activities>: " + parser.getName()); 3079 XmlUtils.skipCurrentTag(parser); 3080 } 3081 } 3082 } 3083 3084 private int readInt(XmlPullParser parser, String ns, String name, int defValue) { 3085 String v = parser.getAttributeValue(ns, name); 3086 try { 3087 if (v == null) { 3088 return defValue; 3089 } 3090 return Integer.parseInt(v); 3091 } catch (NumberFormatException e) { 3092 PackageManagerService.reportSettingsProblem(Log.WARN, 3093 "Error in package manager settings: attribute " + name 3094 + " has bad integer value " + v + " at " 3095 + parser.getPositionDescription()); 3096 } 3097 return defValue; 3098 } 3099 3100 private void readPermissionsLPw(ArrayMap<String, BasePermission> out, XmlPullParser parser) 3101 throws IOException, XmlPullParserException { 3102 int outerDepth = parser.getDepth(); 3103 int type; 3104 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 3105 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 3106 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 3107 continue; 3108 } 3109 3110 final String tagName = parser.getName(); 3111 if (tagName.equals(TAG_ITEM)) { 3112 final String name = parser.getAttributeValue(null, ATTR_NAME); 3113 final String sourcePackage = parser.getAttributeValue(null, "package"); 3114 final String ptype = parser.getAttributeValue(null, "type"); 3115 if (name != null && sourcePackage != null) { 3116 final boolean dynamic = "dynamic".equals(ptype); 3117 final BasePermission bp = new BasePermission(name.intern(), sourcePackage, 3118 dynamic ? BasePermission.TYPE_DYNAMIC : BasePermission.TYPE_NORMAL); 3119 bp.protectionLevel = readInt(parser, null, "protection", 3120 PermissionInfo.PROTECTION_NORMAL); 3121 bp.protectionLevel = PermissionInfo.fixProtectionLevel(bp.protectionLevel); 3122 if (dynamic) { 3123 PermissionInfo pi = new PermissionInfo(); 3124 pi.packageName = sourcePackage.intern(); 3125 pi.name = name.intern(); 3126 pi.icon = readInt(parser, null, "icon", 0); 3127 pi.nonLocalizedLabel = parser.getAttributeValue(null, "label"); 3128 pi.protectionLevel = bp.protectionLevel; 3129 bp.pendingInfo = pi; 3130 } 3131 out.put(bp.name, bp); 3132 } else { 3133 PackageManagerService.reportSettingsProblem(Log.WARN, 3134 "Error in package manager settings: permissions has" + " no name at " 3135 + parser.getPositionDescription()); 3136 } 3137 } else { 3138 PackageManagerService.reportSettingsProblem(Log.WARN, 3139 "Unknown element reading permissions: " + parser.getName() + " at " 3140 + parser.getPositionDescription()); 3141 } 3142 XmlUtils.skipCurrentTag(parser); 3143 } 3144 } 3145 3146 private void readDisabledSysPackageLPw(XmlPullParser parser) throws XmlPullParserException, 3147 IOException { 3148 String name = parser.getAttributeValue(null, ATTR_NAME); 3149 String realName = parser.getAttributeValue(null, "realName"); 3150 String codePathStr = parser.getAttributeValue(null, "codePath"); 3151 String resourcePathStr = parser.getAttributeValue(null, "resourcePath"); 3152 3153 String legacyCpuAbiStr = parser.getAttributeValue(null, "requiredCpuAbi"); 3154 String legacyNativeLibraryPathStr = parser.getAttributeValue(null, "nativeLibraryPath"); 3155 3156 String primaryCpuAbiStr = parser.getAttributeValue(null, "primaryCpuAbi"); 3157 String secondaryCpuAbiStr = parser.getAttributeValue(null, "secondaryCpuAbi"); 3158 String cpuAbiOverrideStr = parser.getAttributeValue(null, "cpuAbiOverride"); 3159 3160 if (primaryCpuAbiStr == null && legacyCpuAbiStr != null) { 3161 primaryCpuAbiStr = legacyCpuAbiStr; 3162 } 3163 3164 if (resourcePathStr == null) { 3165 resourcePathStr = codePathStr; 3166 } 3167 String version = parser.getAttributeValue(null, "version"); 3168 int versionCode = 0; 3169 if (version != null) { 3170 try { 3171 versionCode = Integer.parseInt(version); 3172 } catch (NumberFormatException e) { 3173 } 3174 } 3175 3176 int pkgFlags = 0; 3177 int pkgPrivateFlags = 0; 3178 pkgFlags |= ApplicationInfo.FLAG_SYSTEM; 3179 final File codePathFile = new File(codePathStr); 3180 if (PackageManagerService.locationIsPrivileged(codePathFile)) { 3181 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_PRIVILEGED; 3182 } 3183 PackageSetting ps = new PackageSetting(name, realName, codePathFile, 3184 new File(resourcePathStr), legacyNativeLibraryPathStr, primaryCpuAbiStr, 3185 secondaryCpuAbiStr, cpuAbiOverrideStr, versionCode, pkgFlags, pkgPrivateFlags); 3186 String timeStampStr = parser.getAttributeValue(null, "ft"); 3187 if (timeStampStr != null) { 3188 try { 3189 long timeStamp = Long.parseLong(timeStampStr, 16); 3190 ps.setTimeStamp(timeStamp); 3191 } catch (NumberFormatException e) { 3192 } 3193 } else { 3194 timeStampStr = parser.getAttributeValue(null, "ts"); 3195 if (timeStampStr != null) { 3196 try { 3197 long timeStamp = Long.parseLong(timeStampStr); 3198 ps.setTimeStamp(timeStamp); 3199 } catch (NumberFormatException e) { 3200 } 3201 } 3202 } 3203 timeStampStr = parser.getAttributeValue(null, "it"); 3204 if (timeStampStr != null) { 3205 try { 3206 ps.firstInstallTime = Long.parseLong(timeStampStr, 16); 3207 } catch (NumberFormatException e) { 3208 } 3209 } 3210 timeStampStr = parser.getAttributeValue(null, "ut"); 3211 if (timeStampStr != null) { 3212 try { 3213 ps.lastUpdateTime = Long.parseLong(timeStampStr, 16); 3214 } catch (NumberFormatException e) { 3215 } 3216 } 3217 String idStr = parser.getAttributeValue(null, "userId"); 3218 ps.appId = idStr != null ? Integer.parseInt(idStr) : 0; 3219 if (ps.appId <= 0) { 3220 String sharedIdStr = parser.getAttributeValue(null, "sharedUserId"); 3221 ps.appId = sharedIdStr != null ? Integer.parseInt(sharedIdStr) : 0; 3222 } 3223 3224 int outerDepth = parser.getDepth(); 3225 int type; 3226 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 3227 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 3228 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 3229 continue; 3230 } 3231 3232 if (parser.getName().equals(TAG_PERMISSIONS)) { 3233 readInstallPermissionsLPr(parser, ps.getPermissionsState()); 3234 } else { 3235 PackageManagerService.reportSettingsProblem(Log.WARN, 3236 "Unknown element under <updated-package>: " + parser.getName()); 3237 XmlUtils.skipCurrentTag(parser); 3238 } 3239 } 3240 3241 mDisabledSysPackages.put(name, ps); 3242 } 3243 3244 private static int PRE_M_APP_INFO_FLAG_HIDDEN = 1<<27; 3245 private static int PRE_M_APP_INFO_FLAG_CANT_SAVE_STATE = 1<<28; 3246 private static int PRE_M_APP_INFO_FLAG_FORWARD_LOCK = 1<<29; 3247 private static int PRE_M_APP_INFO_FLAG_PRIVILEGED = 1<<30; 3248 3249 private void readPackageLPw(XmlPullParser parser) throws XmlPullParserException, IOException { 3250 String name = null; 3251 String realName = null; 3252 String idStr = null; 3253 String sharedIdStr = null; 3254 String codePathStr = null; 3255 String resourcePathStr = null; 3256 String legacyCpuAbiString = null; 3257 String legacyNativeLibraryPathStr = null; 3258 String primaryCpuAbiString = null; 3259 String secondaryCpuAbiString = null; 3260 String cpuAbiOverrideString = null; 3261 String systemStr = null; 3262 String installerPackageName = null; 3263 String volumeUuid = null; 3264 String uidError = null; 3265 int pkgFlags = 0; 3266 int pkgPrivateFlags = 0; 3267 long timeStamp = 0; 3268 long firstInstallTime = 0; 3269 long lastUpdateTime = 0; 3270 PackageSettingBase packageSetting = null; 3271 String version = null; 3272 int versionCode = 0; 3273 try { 3274 name = parser.getAttributeValue(null, ATTR_NAME); 3275 realName = parser.getAttributeValue(null, "realName"); 3276 idStr = parser.getAttributeValue(null, "userId"); 3277 uidError = parser.getAttributeValue(null, "uidError"); 3278 sharedIdStr = parser.getAttributeValue(null, "sharedUserId"); 3279 codePathStr = parser.getAttributeValue(null, "codePath"); 3280 resourcePathStr = parser.getAttributeValue(null, "resourcePath"); 3281 3282 legacyCpuAbiString = parser.getAttributeValue(null, "requiredCpuAbi"); 3283 3284 legacyNativeLibraryPathStr = parser.getAttributeValue(null, "nativeLibraryPath"); 3285 primaryCpuAbiString = parser.getAttributeValue(null, "primaryCpuAbi"); 3286 secondaryCpuAbiString = parser.getAttributeValue(null, "secondaryCpuAbi"); 3287 cpuAbiOverrideString = parser.getAttributeValue(null, "cpuAbiOverride"); 3288 3289 if (primaryCpuAbiString == null && legacyCpuAbiString != null) { 3290 primaryCpuAbiString = legacyCpuAbiString; 3291 } 3292 3293 version = parser.getAttributeValue(null, "version"); 3294 if (version != null) { 3295 try { 3296 versionCode = Integer.parseInt(version); 3297 } catch (NumberFormatException e) { 3298 } 3299 } 3300 installerPackageName = parser.getAttributeValue(null, "installer"); 3301 volumeUuid = parser.getAttributeValue(null, "volumeUuid"); 3302 3303 systemStr = parser.getAttributeValue(null, "publicFlags"); 3304 if (systemStr != null) { 3305 try { 3306 pkgFlags = Integer.parseInt(systemStr); 3307 } catch (NumberFormatException e) { 3308 } 3309 systemStr = parser.getAttributeValue(null, "privateFlags"); 3310 if (systemStr != null) { 3311 try { 3312 pkgPrivateFlags = Integer.parseInt(systemStr); 3313 } catch (NumberFormatException e) { 3314 } 3315 } 3316 } else { 3317 // Pre-M -- both public and private flags were stored in one "flags" field. 3318 systemStr = parser.getAttributeValue(null, "flags"); 3319 if (systemStr != null) { 3320 try { 3321 pkgFlags = Integer.parseInt(systemStr); 3322 } catch (NumberFormatException e) { 3323 } 3324 if ((pkgFlags & PRE_M_APP_INFO_FLAG_HIDDEN) != 0) { 3325 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_HIDDEN; 3326 } 3327 if ((pkgFlags & PRE_M_APP_INFO_FLAG_CANT_SAVE_STATE) != 0) { 3328 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE; 3329 } 3330 if ((pkgFlags & PRE_M_APP_INFO_FLAG_FORWARD_LOCK) != 0) { 3331 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK; 3332 } 3333 if ((pkgFlags & PRE_M_APP_INFO_FLAG_PRIVILEGED) != 0) { 3334 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_PRIVILEGED; 3335 } 3336 pkgFlags &= ~(PRE_M_APP_INFO_FLAG_HIDDEN 3337 | PRE_M_APP_INFO_FLAG_CANT_SAVE_STATE 3338 | PRE_M_APP_INFO_FLAG_FORWARD_LOCK 3339 | PRE_M_APP_INFO_FLAG_PRIVILEGED); 3340 } else { 3341 // For backward compatibility 3342 systemStr = parser.getAttributeValue(null, "system"); 3343 if (systemStr != null) { 3344 pkgFlags |= ("true".equalsIgnoreCase(systemStr)) ? ApplicationInfo.FLAG_SYSTEM 3345 : 0; 3346 } else { 3347 // Old settings that don't specify system... just treat 3348 // them as system, good enough. 3349 pkgFlags |= ApplicationInfo.FLAG_SYSTEM; 3350 } 3351 } 3352 } 3353 String timeStampStr = parser.getAttributeValue(null, "ft"); 3354 if (timeStampStr != null) { 3355 try { 3356 timeStamp = Long.parseLong(timeStampStr, 16); 3357 } catch (NumberFormatException e) { 3358 } 3359 } else { 3360 timeStampStr = parser.getAttributeValue(null, "ts"); 3361 if (timeStampStr != null) { 3362 try { 3363 timeStamp = Long.parseLong(timeStampStr); 3364 } catch (NumberFormatException e) { 3365 } 3366 } 3367 } 3368 timeStampStr = parser.getAttributeValue(null, "it"); 3369 if (timeStampStr != null) { 3370 try { 3371 firstInstallTime = Long.parseLong(timeStampStr, 16); 3372 } catch (NumberFormatException e) { 3373 } 3374 } 3375 timeStampStr = parser.getAttributeValue(null, "ut"); 3376 if (timeStampStr != null) { 3377 try { 3378 lastUpdateTime = Long.parseLong(timeStampStr, 16); 3379 } catch (NumberFormatException e) { 3380 } 3381 } 3382 if (PackageManagerService.DEBUG_SETTINGS) 3383 Log.v(PackageManagerService.TAG, "Reading package: " + name + " userId=" + idStr 3384 + " sharedUserId=" + sharedIdStr); 3385 int userId = idStr != null ? Integer.parseInt(idStr) : 0; 3386 if (resourcePathStr == null) { 3387 resourcePathStr = codePathStr; 3388 } 3389 if (realName != null) { 3390 realName = realName.intern(); 3391 } 3392 if (name == null) { 3393 PackageManagerService.reportSettingsProblem(Log.WARN, 3394 "Error in package manager settings: <package> has no name at " 3395 + parser.getPositionDescription()); 3396 } else if (codePathStr == null) { 3397 PackageManagerService.reportSettingsProblem(Log.WARN, 3398 "Error in package manager settings: <package> has no codePath at " 3399 + parser.getPositionDescription()); 3400 } else if (userId > 0) { 3401 packageSetting = addPackageLPw(name.intern(), realName, new File(codePathStr), 3402 new File(resourcePathStr), legacyNativeLibraryPathStr, primaryCpuAbiString, 3403 secondaryCpuAbiString, cpuAbiOverrideString, userId, versionCode, pkgFlags, 3404 pkgPrivateFlags); 3405 if (PackageManagerService.DEBUG_SETTINGS) 3406 Log.i(PackageManagerService.TAG, "Reading package " + name + ": userId=" 3407 + userId + " pkg=" + packageSetting); 3408 if (packageSetting == null) { 3409 PackageManagerService.reportSettingsProblem(Log.ERROR, "Failure adding uid " 3410 + userId + " while parsing settings at " 3411 + parser.getPositionDescription()); 3412 } else { 3413 packageSetting.setTimeStamp(timeStamp); 3414 packageSetting.firstInstallTime = firstInstallTime; 3415 packageSetting.lastUpdateTime = lastUpdateTime; 3416 } 3417 } else if (sharedIdStr != null) { 3418 userId = sharedIdStr != null ? Integer.parseInt(sharedIdStr) : 0; 3419 if (userId > 0) { 3420 packageSetting = new PendingPackage(name.intern(), realName, new File( 3421 codePathStr), new File(resourcePathStr), legacyNativeLibraryPathStr, 3422 primaryCpuAbiString, secondaryCpuAbiString, cpuAbiOverrideString, 3423 userId, versionCode, pkgFlags, pkgPrivateFlags); 3424 packageSetting.setTimeStamp(timeStamp); 3425 packageSetting.firstInstallTime = firstInstallTime; 3426 packageSetting.lastUpdateTime = lastUpdateTime; 3427 mPendingPackages.add((PendingPackage) packageSetting); 3428 if (PackageManagerService.DEBUG_SETTINGS) 3429 Log.i(PackageManagerService.TAG, "Reading package " + name 3430 + ": sharedUserId=" + userId + " pkg=" + packageSetting); 3431 } else { 3432 PackageManagerService.reportSettingsProblem(Log.WARN, 3433 "Error in package manager settings: package " + name 3434 + " has bad sharedId " + sharedIdStr + " at " 3435 + parser.getPositionDescription()); 3436 } 3437 } else { 3438 PackageManagerService.reportSettingsProblem(Log.WARN, 3439 "Error in package manager settings: package " + name + " has bad userId " 3440 + idStr + " at " + parser.getPositionDescription()); 3441 } 3442 } catch (NumberFormatException e) { 3443 PackageManagerService.reportSettingsProblem(Log.WARN, 3444 "Error in package manager settings: package " + name + " has bad userId " 3445 + idStr + " at " + parser.getPositionDescription()); 3446 } 3447 if (packageSetting != null) { 3448 packageSetting.uidError = "true".equals(uidError); 3449 packageSetting.installerPackageName = installerPackageName; 3450 packageSetting.volumeUuid = volumeUuid; 3451 packageSetting.legacyNativeLibraryPathString = legacyNativeLibraryPathStr; 3452 packageSetting.primaryCpuAbiString = primaryCpuAbiString; 3453 packageSetting.secondaryCpuAbiString = secondaryCpuAbiString; 3454 // Handle legacy string here for single-user mode 3455 final String enabledStr = parser.getAttributeValue(null, ATTR_ENABLED); 3456 if (enabledStr != null) { 3457 try { 3458 packageSetting.setEnabled(Integer.parseInt(enabledStr), 0 /* userId */, null); 3459 } catch (NumberFormatException e) { 3460 if (enabledStr.equalsIgnoreCase("true")) { 3461 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_ENABLED, 0, null); 3462 } else if (enabledStr.equalsIgnoreCase("false")) { 3463 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DISABLED, 0, null); 3464 } else if (enabledStr.equalsIgnoreCase("default")) { 3465 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DEFAULT, 0, null); 3466 } else { 3467 PackageManagerService.reportSettingsProblem(Log.WARN, 3468 "Error in package manager settings: package " + name 3469 + " has bad enabled value: " + idStr + " at " 3470 + parser.getPositionDescription()); 3471 } 3472 } 3473 } else { 3474 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DEFAULT, 0, null); 3475 } 3476 3477 final String installStatusStr = parser.getAttributeValue(null, "installStatus"); 3478 if (installStatusStr != null) { 3479 if (installStatusStr.equalsIgnoreCase("false")) { 3480 packageSetting.installStatus = PackageSettingBase.PKG_INSTALL_INCOMPLETE; 3481 } else { 3482 packageSetting.installStatus = PackageSettingBase.PKG_INSTALL_COMPLETE; 3483 } 3484 } 3485 int outerDepth = parser.getDepth(); 3486 int type; 3487 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 3488 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 3489 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 3490 continue; 3491 } 3492 3493 String tagName = parser.getName(); 3494 // Legacy 3495 if (tagName.equals(TAG_DISABLED_COMPONENTS)) { 3496 readDisabledComponentsLPw(packageSetting, parser, 0); 3497 } else if (tagName.equals(TAG_ENABLED_COMPONENTS)) { 3498 readEnabledComponentsLPw(packageSetting, parser, 0); 3499 } else if (tagName.equals("sigs")) { 3500 packageSetting.signatures.readXml(parser, mPastSignatures); 3501 } else if (tagName.equals(TAG_PERMISSIONS)) { 3502 readInstallPermissionsLPr(parser, 3503 packageSetting.getPermissionsState()); 3504 packageSetting.installPermissionsFixed = true; 3505 } else if (tagName.equals("proper-signing-keyset")) { 3506 long id = Long.parseLong(parser.getAttributeValue(null, "identifier")); 3507 Integer refCt = mKeySetRefs.get(id); 3508 if (refCt != null) { 3509 mKeySetRefs.put(id, refCt + 1); 3510 } else { 3511 mKeySetRefs.put(id, 1); 3512 } 3513 packageSetting.keySetData.setProperSigningKeySet(id); 3514 } else if (tagName.equals("signing-keyset")) { 3515 // from v1 of keysetmanagerservice - no longer used 3516 } else if (tagName.equals("upgrade-keyset")) { 3517 long id = Long.parseLong(parser.getAttributeValue(null, "identifier")); 3518 packageSetting.keySetData.addUpgradeKeySetById(id); 3519 } else if (tagName.equals("defined-keyset")) { 3520 long id = Long.parseLong(parser.getAttributeValue(null, "identifier")); 3521 String alias = parser.getAttributeValue(null, "alias"); 3522 Integer refCt = mKeySetRefs.get(id); 3523 if (refCt != null) { 3524 mKeySetRefs.put(id, refCt + 1); 3525 } else { 3526 mKeySetRefs.put(id, 1); 3527 } 3528 packageSetting.keySetData.addDefinedKeySet(id, alias); 3529 } else if (tagName.equals(TAG_DOMAIN_VERIFICATION)) { 3530 readDomainVerificationLPw(parser, packageSetting); 3531 } else { 3532 PackageManagerService.reportSettingsProblem(Log.WARN, 3533 "Unknown element under <package>: " + parser.getName()); 3534 XmlUtils.skipCurrentTag(parser); 3535 } 3536 } 3537 } else { 3538 XmlUtils.skipCurrentTag(parser); 3539 } 3540 } 3541 3542 private void readDisabledComponentsLPw(PackageSettingBase packageSetting, XmlPullParser parser, 3543 int userId) throws IOException, XmlPullParserException { 3544 int outerDepth = parser.getDepth(); 3545 int type; 3546 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 3547 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 3548 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 3549 continue; 3550 } 3551 3552 String tagName = parser.getName(); 3553 if (tagName.equals(TAG_ITEM)) { 3554 String name = parser.getAttributeValue(null, ATTR_NAME); 3555 if (name != null) { 3556 packageSetting.addDisabledComponent(name.intern(), userId); 3557 } else { 3558 PackageManagerService.reportSettingsProblem(Log.WARN, 3559 "Error in package manager settings: <disabled-components> has" 3560 + " no name at " + parser.getPositionDescription()); 3561 } 3562 } else { 3563 PackageManagerService.reportSettingsProblem(Log.WARN, 3564 "Unknown element under <disabled-components>: " + parser.getName()); 3565 } 3566 XmlUtils.skipCurrentTag(parser); 3567 } 3568 } 3569 3570 private void readEnabledComponentsLPw(PackageSettingBase packageSetting, XmlPullParser parser, 3571 int userId) throws IOException, XmlPullParserException { 3572 int outerDepth = parser.getDepth(); 3573 int type; 3574 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 3575 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 3576 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 3577 continue; 3578 } 3579 3580 String tagName = parser.getName(); 3581 if (tagName.equals(TAG_ITEM)) { 3582 String name = parser.getAttributeValue(null, ATTR_NAME); 3583 if (name != null) { 3584 packageSetting.addEnabledComponent(name.intern(), userId); 3585 } else { 3586 PackageManagerService.reportSettingsProblem(Log.WARN, 3587 "Error in package manager settings: <enabled-components> has" 3588 + " no name at " + parser.getPositionDescription()); 3589 } 3590 } else { 3591 PackageManagerService.reportSettingsProblem(Log.WARN, 3592 "Unknown element under <enabled-components>: " + parser.getName()); 3593 } 3594 XmlUtils.skipCurrentTag(parser); 3595 } 3596 } 3597 3598 private void readSharedUserLPw(XmlPullParser parser) throws XmlPullParserException,IOException { 3599 String name = null; 3600 String idStr = null; 3601 int pkgFlags = 0; 3602 int pkgPrivateFlags = 0; 3603 SharedUserSetting su = null; 3604 try { 3605 name = parser.getAttributeValue(null, ATTR_NAME); 3606 idStr = parser.getAttributeValue(null, "userId"); 3607 int userId = idStr != null ? Integer.parseInt(idStr) : 0; 3608 if ("true".equals(parser.getAttributeValue(null, "system"))) { 3609 pkgFlags |= ApplicationInfo.FLAG_SYSTEM; 3610 } 3611 if (name == null) { 3612 PackageManagerService.reportSettingsProblem(Log.WARN, 3613 "Error in package manager settings: <shared-user> has no name at " 3614 + parser.getPositionDescription()); 3615 } else if (userId == 0) { 3616 PackageManagerService.reportSettingsProblem(Log.WARN, 3617 "Error in package manager settings: shared-user " + name 3618 + " has bad userId " + idStr + " at " 3619 + parser.getPositionDescription()); 3620 } else { 3621 if ((su = addSharedUserLPw(name.intern(), userId, pkgFlags, pkgPrivateFlags)) 3622 == null) { 3623 PackageManagerService 3624 .reportSettingsProblem(Log.ERROR, "Occurred while parsing settings at " 3625 + parser.getPositionDescription()); 3626 } 3627 } 3628 } catch (NumberFormatException e) { 3629 PackageManagerService.reportSettingsProblem(Log.WARN, 3630 "Error in package manager settings: package " + name + " has bad userId " 3631 + idStr + " at " + parser.getPositionDescription()); 3632 } 3633 3634 if (su != null) { 3635 int outerDepth = parser.getDepth(); 3636 int type; 3637 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 3638 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 3639 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 3640 continue; 3641 } 3642 3643 String tagName = parser.getName(); 3644 if (tagName.equals("sigs")) { 3645 su.signatures.readXml(parser, mPastSignatures); 3646 } else if (tagName.equals("perms")) { 3647 readInstallPermissionsLPr(parser, su.getPermissionsState()); 3648 } else { 3649 PackageManagerService.reportSettingsProblem(Log.WARN, 3650 "Unknown element under <shared-user>: " + parser.getName()); 3651 XmlUtils.skipCurrentTag(parser); 3652 } 3653 } 3654 } else { 3655 XmlUtils.skipCurrentTag(parser); 3656 } 3657 } 3658 3659 void createNewUserLI(@NonNull PackageManagerService service, @NonNull Installer installer, 3660 int userHandle) { 3661 String[] volumeUuids; 3662 String[] names; 3663 int[] uids; 3664 String[] seinfos; 3665 int packagesCount; 3666 synchronized (mPackages) { 3667 Collection<PackageSetting> packages = mPackages.values(); 3668 packagesCount = packages.size(); 3669 volumeUuids = new String[packagesCount]; 3670 names = new String[packagesCount]; 3671 uids = new int[packagesCount]; 3672 seinfos = new String[packagesCount]; 3673 Iterator<PackageSetting> packagesIterator = packages.iterator(); 3674 for (int i = 0; i < packagesCount; i++) { 3675 PackageSetting ps = packagesIterator.next(); 3676 if (ps.pkg == null || ps.pkg.applicationInfo == null) { 3677 continue; 3678 } 3679 // Only system apps are initially installed. 3680 ps.setInstalled(ps.isSystem(), userHandle); 3681 // Need to create a data directory for all apps under this user. Accumulate all 3682 // required args and call the installer after mPackages lock has been released 3683 volumeUuids[i] = ps.volumeUuid; 3684 names[i] = ps.name; 3685 uids[i] = UserHandle.getUid(userHandle, ps.appId); 3686 seinfos[i] = ps.pkg.applicationInfo.seinfo; 3687 } 3688 } 3689 for (int i = 0; i < packagesCount; i++) { 3690 if (names[i] == null) { 3691 continue; 3692 } 3693 installer.createUserData(volumeUuids[i], names[i], uids[i], userHandle, seinfos[i]); 3694 } 3695 synchronized (mPackages) { 3696 applyDefaultPreferredAppsLPw(service, userHandle); 3697 writePackageRestrictionsLPr(userHandle); 3698 writePackageListLPr(userHandle); 3699 } 3700 } 3701 3702 void removeUserLPw(int userId) { 3703 Set<Entry<String, PackageSetting>> entries = mPackages.entrySet(); 3704 for (Entry<String, PackageSetting> entry : entries) { 3705 entry.getValue().removeUser(userId); 3706 } 3707 mPreferredActivities.remove(userId); 3708 File file = getUserPackagesStateFile(userId); 3709 file.delete(); 3710 file = getUserPackagesStateBackupFile(userId); 3711 file.delete(); 3712 removeCrossProfileIntentFiltersLPw(userId); 3713 3714 mRuntimePermissionsPersistence.onUserRemoved(userId); 3715 3716 writePackageListLPr(); 3717 } 3718 3719 void removeCrossProfileIntentFiltersLPw(int userId) { 3720 synchronized (mCrossProfileIntentResolvers) { 3721 // userId is the source user 3722 if (mCrossProfileIntentResolvers.get(userId) != null) { 3723 mCrossProfileIntentResolvers.remove(userId); 3724 writePackageRestrictionsLPr(userId); 3725 } 3726 // userId is the target user 3727 int count = mCrossProfileIntentResolvers.size(); 3728 for (int i = 0; i < count; i++) { 3729 int sourceUserId = mCrossProfileIntentResolvers.keyAt(i); 3730 CrossProfileIntentResolver cpir = mCrossProfileIntentResolvers.get(sourceUserId); 3731 boolean needsWriting = false; 3732 ArraySet<CrossProfileIntentFilter> cpifs = 3733 new ArraySet<CrossProfileIntentFilter>(cpir.filterSet()); 3734 for (CrossProfileIntentFilter cpif : cpifs) { 3735 if (cpif.getTargetUserId() == userId) { 3736 needsWriting = true; 3737 cpir.removeFilter(cpif); 3738 } 3739 } 3740 if (needsWriting) { 3741 writePackageRestrictionsLPr(sourceUserId); 3742 } 3743 } 3744 } 3745 } 3746 3747 // This should be called (at least) whenever an application is removed 3748 private void setFirstAvailableUid(int uid) { 3749 if (uid > mFirstAvailableUid) { 3750 mFirstAvailableUid = uid; 3751 } 3752 } 3753 3754 // Returns -1 if we could not find an available UserId to assign 3755 private int newUserIdLPw(Object obj) { 3756 // Let's be stupidly inefficient for now... 3757 final int N = mUserIds.size(); 3758 for (int i = mFirstAvailableUid; i < N; i++) { 3759 if (mUserIds.get(i) == null) { 3760 mUserIds.set(i, obj); 3761 return Process.FIRST_APPLICATION_UID + i; 3762 } 3763 } 3764 3765 // None left? 3766 if (N > (Process.LAST_APPLICATION_UID-Process.FIRST_APPLICATION_UID)) { 3767 return -1; 3768 } 3769 3770 mUserIds.add(obj); 3771 return Process.FIRST_APPLICATION_UID + N; 3772 } 3773 3774 public VerifierDeviceIdentity getVerifierDeviceIdentityLPw() { 3775 if (mVerifierDeviceIdentity == null) { 3776 mVerifierDeviceIdentity = VerifierDeviceIdentity.generate(); 3777 3778 writeLPr(); 3779 } 3780 3781 return mVerifierDeviceIdentity; 3782 } 3783 3784 public PackageSetting getDisabledSystemPkgLPr(String name) { 3785 PackageSetting ps = mDisabledSysPackages.get(name); 3786 return ps; 3787 } 3788 3789 private String compToString(ArraySet<String> cmp) { 3790 return cmp != null ? Arrays.toString(cmp.toArray()) : "[]"; 3791 } 3792 3793 boolean isEnabledAndMatchLPr(ComponentInfo componentInfo, int flags, int userId) { 3794 return isEnabledLPr(componentInfo, flags, userId) 3795 && isMatchLPr(componentInfo, flags); 3796 } 3797 3798 private boolean isEnabledLPr(ComponentInfo componentInfo, int flags, int userId) { 3799 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) { 3800 return true; 3801 } 3802 final PackageSetting packageSettings = mPackages.get(componentInfo.packageName); 3803 if (PackageManagerService.DEBUG_SETTINGS) { 3804 Log.v(PackageManagerService.TAG, "isEnabledLock - packageName = " 3805 + componentInfo.packageName + " componentName = " + componentInfo.name); 3806 Log.v(PackageManagerService.TAG, "enabledComponents: " 3807 + compToString(packageSettings.getEnabledComponents(userId))); 3808 Log.v(PackageManagerService.TAG, "disabledComponents: " 3809 + compToString(packageSettings.getDisabledComponents(userId))); 3810 } 3811 if (packageSettings == null) { 3812 return false; 3813 } 3814 PackageUserState ustate = packageSettings.readUserState(userId); 3815 if ((flags&PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS) != 0) { 3816 if (ustate.enabled == COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) { 3817 return true; 3818 } 3819 } 3820 if (ustate.enabled == COMPONENT_ENABLED_STATE_DISABLED 3821 || ustate.enabled == COMPONENT_ENABLED_STATE_DISABLED_USER 3822 || ustate.enabled == COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED 3823 || (packageSettings.pkg != null && !packageSettings.pkg.applicationInfo.enabled 3824 && ustate.enabled == COMPONENT_ENABLED_STATE_DEFAULT)) { 3825 return false; 3826 } 3827 if (ustate.enabledComponents != null 3828 && ustate.enabledComponents.contains(componentInfo.name)) { 3829 return true; 3830 } 3831 if (ustate.disabledComponents != null 3832 && ustate.disabledComponents.contains(componentInfo.name)) { 3833 return false; 3834 } 3835 return componentInfo.enabled; 3836 } 3837 3838 private boolean isMatchLPr(ComponentInfo componentInfo, int flags) { 3839 if ((flags & PackageManager.MATCH_SYSTEM_ONLY) != 0) { 3840 final PackageSetting ps = mPackages.get(componentInfo.packageName); 3841 if ((ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) == 0) { 3842 return false; 3843 } 3844 } 3845 3846 final boolean matchesUnaware = ((flags & PackageManager.MATCH_ENCRYPTION_UNAWARE_ONLY) != 0) 3847 && !componentInfo.encryptionAware; 3848 final boolean matchesAware = ((flags & PackageManager.MATCH_ENCRYPTION_AWARE_ONLY) != 0) 3849 && componentInfo.encryptionAware; 3850 return matchesUnaware || matchesAware; 3851 } 3852 3853 String getInstallerPackageNameLPr(String packageName) { 3854 final PackageSetting pkg = mPackages.get(packageName); 3855 if (pkg == null) { 3856 throw new IllegalArgumentException("Unknown package: " + packageName); 3857 } 3858 return pkg.installerPackageName; 3859 } 3860 3861 int getApplicationEnabledSettingLPr(String packageName, int userId) { 3862 final PackageSetting pkg = mPackages.get(packageName); 3863 if (pkg == null) { 3864 throw new IllegalArgumentException("Unknown package: " + packageName); 3865 } 3866 return pkg.getEnabled(userId); 3867 } 3868 3869 int getComponentEnabledSettingLPr(ComponentName componentName, int userId) { 3870 final String packageName = componentName.getPackageName(); 3871 final PackageSetting pkg = mPackages.get(packageName); 3872 if (pkg == null) { 3873 throw new IllegalArgumentException("Unknown component: " + componentName); 3874 } 3875 final String classNameStr = componentName.getClassName(); 3876 return pkg.getCurrentEnabledStateLPr(classNameStr, userId); 3877 } 3878 3879 boolean setPackageStoppedStateLPw(PackageManagerService yucky, String packageName, 3880 boolean stopped, boolean allowedByPermission, int uid, int userId) { 3881 int appId = UserHandle.getAppId(uid); 3882 final PackageSetting pkgSetting = mPackages.get(packageName); 3883 if (pkgSetting == null) { 3884 throw new IllegalArgumentException("Unknown package: " + packageName); 3885 } 3886 if (!allowedByPermission && (appId != pkgSetting.appId)) { 3887 throw new SecurityException( 3888 "Permission Denial: attempt to change stopped state from pid=" 3889 + Binder.getCallingPid() 3890 + ", uid=" + uid + ", package uid=" + pkgSetting.appId); 3891 } 3892 if (DEBUG_STOPPED) { 3893 if (stopped) { 3894 RuntimeException e = new RuntimeException("here"); 3895 e.fillInStackTrace(); 3896 Slog.i(TAG, "Stopping package " + packageName, e); 3897 } 3898 } 3899 if (pkgSetting.getStopped(userId) != stopped) { 3900 pkgSetting.setStopped(stopped, userId); 3901 // pkgSetting.pkg.mSetStopped = stopped; 3902 if (pkgSetting.getNotLaunched(userId)) { 3903 if (pkgSetting.installerPackageName != null) { 3904 yucky.sendPackageBroadcast(Intent.ACTION_PACKAGE_FIRST_LAUNCH, 3905 pkgSetting.name, null, 0, 3906 pkgSetting.installerPackageName, null, new int[] {userId}); 3907 } 3908 pkgSetting.setNotLaunched(false, userId); 3909 } 3910 return true; 3911 } 3912 return false; 3913 } 3914 3915 List<UserInfo> getAllUsers() { 3916 long id = Binder.clearCallingIdentity(); 3917 try { 3918 return UserManagerService.getInstance().getUsers(false); 3919 } catch (NullPointerException npe) { 3920 // packagemanager not yet initialized 3921 } finally { 3922 Binder.restoreCallingIdentity(id); 3923 } 3924 return null; 3925 } 3926 3927 /** 3928 * Return all {@link PackageSetting} that are actively installed on the 3929 * given {@link VolumeInfo#fsUuid}. 3930 */ 3931 List<PackageSetting> getVolumePackagesLPr(String volumeUuid) { 3932 Preconditions.checkNotNull(volumeUuid); 3933 ArrayList<PackageSetting> res = new ArrayList<>(); 3934 for (int i = 0; i < mPackages.size(); i++) { 3935 final PackageSetting setting = mPackages.valueAt(i); 3936 if (Objects.equals(volumeUuid, setting.volumeUuid)) { 3937 res.add(setting); 3938 } 3939 } 3940 return res; 3941 } 3942 3943 static void printFlags(PrintWriter pw, int val, Object[] spec) { 3944 pw.print("[ "); 3945 for (int i=0; i<spec.length; i+=2) { 3946 int mask = (Integer)spec[i]; 3947 if ((val & mask) != 0) { 3948 pw.print(spec[i+1]); 3949 pw.print(" "); 3950 } 3951 } 3952 pw.print("]"); 3953 } 3954 3955 static final Object[] FLAG_DUMP_SPEC = new Object[] { 3956 ApplicationInfo.FLAG_SYSTEM, "SYSTEM", 3957 ApplicationInfo.FLAG_DEBUGGABLE, "DEBUGGABLE", 3958 ApplicationInfo.FLAG_HAS_CODE, "HAS_CODE", 3959 ApplicationInfo.FLAG_PERSISTENT, "PERSISTENT", 3960 ApplicationInfo.FLAG_FACTORY_TEST, "FACTORY_TEST", 3961 ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING, "ALLOW_TASK_REPARENTING", 3962 ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA, "ALLOW_CLEAR_USER_DATA", 3963 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP, "UPDATED_SYSTEM_APP", 3964 ApplicationInfo.FLAG_TEST_ONLY, "TEST_ONLY", 3965 ApplicationInfo.FLAG_VM_SAFE_MODE, "VM_SAFE_MODE", 3966 ApplicationInfo.FLAG_ALLOW_BACKUP, "ALLOW_BACKUP", 3967 ApplicationInfo.FLAG_KILL_AFTER_RESTORE, "KILL_AFTER_RESTORE", 3968 ApplicationInfo.FLAG_RESTORE_ANY_VERSION, "RESTORE_ANY_VERSION", 3969 ApplicationInfo.FLAG_EXTERNAL_STORAGE, "EXTERNAL_STORAGE", 3970 ApplicationInfo.FLAG_LARGE_HEAP, "LARGE_HEAP", 3971 }; 3972 3973 static final Object[] PRIVATE_FLAG_DUMP_SPEC = new Object[] { 3974 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED, "PRIVILEGED", 3975 ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK, "FORWARD_LOCK", 3976 ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE, "CANT_SAVE_STATE", 3977 }; 3978 3979 void dumpVersionLPr(IndentingPrintWriter pw) { 3980 pw.increaseIndent(); 3981 for (int i= 0; i < mVersion.size(); i++) { 3982 final String volumeUuid = mVersion.keyAt(i); 3983 final VersionInfo ver = mVersion.valueAt(i); 3984 if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, volumeUuid)) { 3985 pw.println("Internal:"); 3986 } else if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) { 3987 pw.println("External:"); 3988 } else { 3989 pw.println("UUID " + volumeUuid + ":"); 3990 } 3991 pw.increaseIndent(); 3992 pw.printPair("sdkVersion", ver.sdkVersion); 3993 pw.printPair("databaseVersion", ver.databaseVersion); 3994 pw.println(); 3995 pw.printPair("fingerprint", ver.fingerprint); 3996 pw.println(); 3997 pw.decreaseIndent(); 3998 } 3999 pw.decreaseIndent(); 4000 } 4001 4002 void dumpPackageLPr(PrintWriter pw, String prefix, String checkinTag, 4003 ArraySet<String> permissionNames, PackageSetting ps, SimpleDateFormat sdf, 4004 Date date, List<UserInfo> users, boolean dumpAll) { 4005 if (checkinTag != null) { 4006 pw.print(checkinTag); 4007 pw.print(","); 4008 pw.print(ps.realName != null ? ps.realName : ps.name); 4009 pw.print(","); 4010 pw.print(ps.appId); 4011 pw.print(","); 4012 pw.print(ps.versionCode); 4013 pw.print(","); 4014 pw.print(ps.firstInstallTime); 4015 pw.print(","); 4016 pw.print(ps.lastUpdateTime); 4017 pw.print(","); 4018 pw.print(ps.installerPackageName != null ? ps.installerPackageName : "?"); 4019 pw.println(); 4020 if (ps.pkg != null) { 4021 pw.print(checkinTag); pw.print("-"); pw.print("splt,"); 4022 pw.print("base,"); 4023 pw.println(ps.pkg.baseRevisionCode); 4024 if (ps.pkg.splitNames != null) { 4025 for (int i = 0; i < ps.pkg.splitNames.length; i++) { 4026 pw.print(checkinTag); pw.print("-"); pw.print("splt,"); 4027 pw.print(ps.pkg.splitNames[i]); pw.print(","); 4028 pw.println(ps.pkg.splitRevisionCodes[i]); 4029 } 4030 } 4031 } 4032 for (UserInfo user : users) { 4033 pw.print(checkinTag); 4034 pw.print("-"); 4035 pw.print("usr"); 4036 pw.print(","); 4037 pw.print(user.id); 4038 pw.print(","); 4039 pw.print(ps.getInstalled(user.id) ? "I" : "i"); 4040 pw.print(ps.getHidden(user.id) ? "B" : "b"); 4041 pw.print(ps.getSuspended(user.id) ? "SU" : "su"); 4042 pw.print(ps.getStopped(user.id) ? "S" : "s"); 4043 pw.print(ps.getNotLaunched(user.id) ? "l" : "L"); 4044 pw.print(","); 4045 pw.print(ps.getEnabled(user.id)); 4046 String lastDisabledAppCaller = ps.getLastDisabledAppCaller(user.id); 4047 pw.print(","); 4048 pw.print(lastDisabledAppCaller != null ? lastDisabledAppCaller : "?"); 4049 pw.println(); 4050 } 4051 return; 4052 } 4053 4054 pw.print(prefix); pw.print("Package ["); 4055 pw.print(ps.realName != null ? ps.realName : ps.name); 4056 pw.print("] ("); 4057 pw.print(Integer.toHexString(System.identityHashCode(ps))); 4058 pw.println("):"); 4059 4060 if (ps.frozen) { 4061 pw.print(prefix); pw.println(" FROZEN!"); 4062 } 4063 4064 if (ps.realName != null) { 4065 pw.print(prefix); pw.print(" compat name="); 4066 pw.println(ps.name); 4067 } 4068 4069 pw.print(prefix); pw.print(" userId="); pw.println(ps.appId); 4070 4071 if (ps.sharedUser != null) { 4072 pw.print(prefix); pw.print(" sharedUser="); pw.println(ps.sharedUser); 4073 } 4074 pw.print(prefix); pw.print(" pkg="); pw.println(ps.pkg); 4075 pw.print(prefix); pw.print(" codePath="); pw.println(ps.codePathString); 4076 if (permissionNames == null) { 4077 pw.print(prefix); pw.print(" resourcePath="); pw.println(ps.resourcePathString); 4078 pw.print(prefix); pw.print(" legacyNativeLibraryDir="); 4079 pw.println(ps.legacyNativeLibraryPathString); 4080 pw.print(prefix); pw.print(" primaryCpuAbi="); pw.println(ps.primaryCpuAbiString); 4081 pw.print(prefix); pw.print(" secondaryCpuAbi="); pw.println(ps.secondaryCpuAbiString); 4082 } 4083 pw.print(prefix); pw.print(" versionCode="); pw.print(ps.versionCode); 4084 if (ps.pkg != null) { 4085 pw.print(" targetSdk="); pw.print(ps.pkg.applicationInfo.targetSdkVersion); 4086 } 4087 pw.println(); 4088 if (ps.pkg != null) { 4089 pw.print(prefix); pw.print(" versionName="); pw.println(ps.pkg.mVersionName); 4090 pw.print(prefix); pw.print(" splits="); dumpSplitNames(pw, ps.pkg); pw.println(); 4091 pw.print(prefix); pw.print(" applicationInfo="); 4092 pw.println(ps.pkg.applicationInfo.toString()); 4093 pw.print(prefix); pw.print(" flags="); printFlags(pw, ps.pkg.applicationInfo.flags, 4094 FLAG_DUMP_SPEC); pw.println(); 4095 if (ps.pkg.applicationInfo.privateFlags != 0) { 4096 pw.print(prefix); pw.print(" privateFlags="); printFlags(pw, 4097 ps.pkg.applicationInfo.privateFlags, PRIVATE_FLAG_DUMP_SPEC); pw.println(); 4098 } 4099 pw.print(prefix); pw.print(" dataDir="); pw.println(ps.pkg.applicationInfo.dataDir); 4100 pw.print(prefix); pw.print(" supportsScreens=["); 4101 boolean first = true; 4102 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS) != 0) { 4103 if (!first) 4104 pw.print(", "); 4105 first = false; 4106 pw.print("small"); 4107 } 4108 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS) != 0) { 4109 if (!first) 4110 pw.print(", "); 4111 first = false; 4112 pw.print("medium"); 4113 } 4114 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS) != 0) { 4115 if (!first) 4116 pw.print(", "); 4117 first = false; 4118 pw.print("large"); 4119 } 4120 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS) != 0) { 4121 if (!first) 4122 pw.print(", "); 4123 first = false; 4124 pw.print("xlarge"); 4125 } 4126 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS) != 0) { 4127 if (!first) 4128 pw.print(", "); 4129 first = false; 4130 pw.print("resizeable"); 4131 } 4132 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES) != 0) { 4133 if (!first) 4134 pw.print(", "); 4135 first = false; 4136 pw.print("anyDensity"); 4137 } 4138 pw.println("]"); 4139 if (ps.pkg.libraryNames != null && ps.pkg.libraryNames.size() > 0) { 4140 pw.print(prefix); pw.println(" libraries:"); 4141 for (int i=0; i<ps.pkg.libraryNames.size(); i++) { 4142 pw.print(prefix); pw.print(" "); pw.println(ps.pkg.libraryNames.get(i)); 4143 } 4144 } 4145 if (ps.pkg.usesLibraries != null && ps.pkg.usesLibraries.size() > 0) { 4146 pw.print(prefix); pw.println(" usesLibraries:"); 4147 for (int i=0; i<ps.pkg.usesLibraries.size(); i++) { 4148 pw.print(prefix); pw.print(" "); pw.println(ps.pkg.usesLibraries.get(i)); 4149 } 4150 } 4151 if (ps.pkg.usesOptionalLibraries != null 4152 && ps.pkg.usesOptionalLibraries.size() > 0) { 4153 pw.print(prefix); pw.println(" usesOptionalLibraries:"); 4154 for (int i=0; i<ps.pkg.usesOptionalLibraries.size(); i++) { 4155 pw.print(prefix); pw.print(" "); 4156 pw.println(ps.pkg.usesOptionalLibraries.get(i)); 4157 } 4158 } 4159 if (ps.pkg.usesLibraryFiles != null 4160 && ps.pkg.usesLibraryFiles.length > 0) { 4161 pw.print(prefix); pw.println(" usesLibraryFiles:"); 4162 for (int i=0; i<ps.pkg.usesLibraryFiles.length; i++) { 4163 pw.print(prefix); pw.print(" "); pw.println(ps.pkg.usesLibraryFiles[i]); 4164 } 4165 } 4166 } 4167 pw.print(prefix); pw.print(" timeStamp="); 4168 date.setTime(ps.timeStamp); 4169 pw.println(sdf.format(date)); 4170 pw.print(prefix); pw.print(" firstInstallTime="); 4171 date.setTime(ps.firstInstallTime); 4172 pw.println(sdf.format(date)); 4173 pw.print(prefix); pw.print(" lastUpdateTime="); 4174 date.setTime(ps.lastUpdateTime); 4175 pw.println(sdf.format(date)); 4176 if (ps.installerPackageName != null) { 4177 pw.print(prefix); pw.print(" installerPackageName="); 4178 pw.println(ps.installerPackageName); 4179 } 4180 if (ps.volumeUuid != null) { 4181 pw.print(prefix); pw.print(" volumeUuid="); 4182 pw.println(ps.volumeUuid); 4183 } 4184 pw.print(prefix); pw.print(" signatures="); pw.println(ps.signatures); 4185 pw.print(prefix); pw.print(" installPermissionsFixed="); 4186 pw.print(ps.installPermissionsFixed); 4187 pw.print(" installStatus="); pw.println(ps.installStatus); 4188 pw.print(prefix); pw.print(" pkgFlags="); printFlags(pw, ps.pkgFlags, FLAG_DUMP_SPEC); 4189 pw.println(); 4190 4191 if (ps.pkg != null && ps.pkg.permissions != null && ps.pkg.permissions.size() > 0) { 4192 final ArrayList<PackageParser.Permission> perms = ps.pkg.permissions; 4193 pw.print(prefix); pw.println(" declared permissions:"); 4194 for (int i=0; i<perms.size(); i++) { 4195 PackageParser.Permission perm = perms.get(i); 4196 if (permissionNames != null 4197 && !permissionNames.contains(perm.info.name)) { 4198 continue; 4199 } 4200 pw.print(prefix); pw.print(" "); pw.print(perm.info.name); 4201 pw.print(": prot="); 4202 pw.print(PermissionInfo.protectionToString(perm.info.protectionLevel)); 4203 if ((perm.info.flags&PermissionInfo.FLAG_COSTS_MONEY) != 0) { 4204 pw.print(", COSTS_MONEY"); 4205 } 4206 if ((perm.info.flags&PermissionInfo.FLAG_HIDDEN) != 0) { 4207 pw.print(", HIDDEN"); 4208 } 4209 if ((perm.info.flags&PermissionInfo.FLAG_INSTALLED) != 0) { 4210 pw.print(", INSTALLED"); 4211 } 4212 pw.println(); 4213 } 4214 } 4215 4216 if ((permissionNames != null || dumpAll) && ps.pkg.requestedPermissions != null 4217 && ps.pkg.requestedPermissions.size() > 0) { 4218 final ArrayList<String> perms = ps.pkg.requestedPermissions; 4219 pw.print(prefix); pw.println(" requested permissions:"); 4220 for (int i=0; i<perms.size(); i++) { 4221 String perm = perms.get(i); 4222 if (permissionNames != null 4223 && !permissionNames.contains(perm)) { 4224 continue; 4225 } 4226 pw.print(prefix); pw.print(" "); pw.println(perm); 4227 } 4228 } 4229 4230 if (ps.sharedUser == null || permissionNames != null || dumpAll) { 4231 PermissionsState permissionsState = ps.getPermissionsState(); 4232 dumpInstallPermissionsLPr(pw, prefix + " ", permissionNames, permissionsState); 4233 } 4234 4235 for (UserInfo user : users) { 4236 pw.print(prefix); pw.print(" User "); pw.print(user.id); pw.print(": "); 4237 pw.print(" installed="); 4238 pw.print(ps.getInstalled(user.id)); 4239 pw.print(" hidden="); 4240 pw.print(ps.getHidden(user.id)); 4241 pw.print(" suspended="); 4242 pw.print(ps.getSuspended(user.id)); 4243 pw.print(" stopped="); 4244 pw.print(ps.getStopped(user.id)); 4245 pw.print(" notLaunched="); 4246 pw.print(ps.getNotLaunched(user.id)); 4247 pw.print(" enabled="); 4248 pw.println(ps.getEnabled(user.id)); 4249 String lastDisabledAppCaller = ps.getLastDisabledAppCaller(user.id); 4250 if (lastDisabledAppCaller != null) { 4251 pw.print(prefix); pw.print(" lastDisabledCaller: "); 4252 pw.println(lastDisabledAppCaller); 4253 } 4254 4255 if (ps.sharedUser == null) { 4256 PermissionsState permissionsState = ps.getPermissionsState(); 4257 dumpGidsLPr(pw, prefix + " ", permissionsState.computeGids(user.id)); 4258 dumpRuntimePermissionsLPr(pw, prefix + " ", permissionNames, permissionsState 4259 .getRuntimePermissionStates(user.id), dumpAll); 4260 } 4261 4262 if (permissionNames == null) { 4263 ArraySet<String> cmp = ps.getDisabledComponents(user.id); 4264 if (cmp != null && cmp.size() > 0) { 4265 pw.print(prefix); pw.println(" disabledComponents:"); 4266 for (String s : cmp) { 4267 pw.print(prefix); pw.print(" "); pw.println(s); 4268 } 4269 } 4270 cmp = ps.getEnabledComponents(user.id); 4271 if (cmp != null && cmp.size() > 0) { 4272 pw.print(prefix); pw.println(" enabledComponents:"); 4273 for (String s : cmp) { 4274 pw.print(prefix); pw.print(" "); pw.println(s); 4275 } 4276 } 4277 } 4278 } 4279 } 4280 4281 void dumpPackagesLPr(PrintWriter pw, String packageName, ArraySet<String> permissionNames, 4282 DumpState dumpState, boolean checkin) { 4283 final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 4284 final Date date = new Date(); 4285 boolean printedSomething = false; 4286 List<UserInfo> users = getAllUsers(); 4287 for (final PackageSetting ps : mPackages.values()) { 4288 if (packageName != null && !packageName.equals(ps.realName) 4289 && !packageName.equals(ps.name)) { 4290 continue; 4291 } 4292 if (permissionNames != null 4293 && !ps.getPermissionsState().hasRequestedPermission(permissionNames)) { 4294 continue; 4295 } 4296 4297 if (!checkin && packageName != null) { 4298 dumpState.setSharedUser(ps.sharedUser); 4299 } 4300 4301 if (!checkin && !printedSomething) { 4302 if (dumpState.onTitlePrinted()) 4303 pw.println(); 4304 pw.println("Packages:"); 4305 printedSomething = true; 4306 } 4307 dumpPackageLPr(pw, " ", checkin ? "pkg" : null, permissionNames, ps, sdf, date, users, 4308 packageName != null); 4309 } 4310 4311 printedSomething = false; 4312 if (mRenamedPackages.size() > 0 && permissionNames == null) { 4313 for (final Map.Entry<String, String> e : mRenamedPackages.entrySet()) { 4314 if (packageName != null && !packageName.equals(e.getKey()) 4315 && !packageName.equals(e.getValue())) { 4316 continue; 4317 } 4318 if (!checkin) { 4319 if (!printedSomething) { 4320 if (dumpState.onTitlePrinted()) 4321 pw.println(); 4322 pw.println("Renamed packages:"); 4323 printedSomething = true; 4324 } 4325 pw.print(" "); 4326 } else { 4327 pw.print("ren,"); 4328 } 4329 pw.print(e.getKey()); 4330 pw.print(checkin ? " -> " : ","); 4331 pw.println(e.getValue()); 4332 } 4333 } 4334 4335 printedSomething = false; 4336 if (mDisabledSysPackages.size() > 0 && permissionNames == null) { 4337 for (final PackageSetting ps : mDisabledSysPackages.values()) { 4338 if (packageName != null && !packageName.equals(ps.realName) 4339 && !packageName.equals(ps.name)) { 4340 continue; 4341 } 4342 if (!checkin && !printedSomething) { 4343 if (dumpState.onTitlePrinted()) 4344 pw.println(); 4345 pw.println("Hidden system packages:"); 4346 printedSomething = true; 4347 } 4348 dumpPackageLPr(pw, " ", checkin ? "dis" : null, permissionNames, ps, sdf, date, 4349 users, packageName != null); 4350 } 4351 } 4352 } 4353 4354 void dumpPermissionsLPr(PrintWriter pw, String packageName, ArraySet<String> permissionNames, 4355 DumpState dumpState) { 4356 boolean printedSomething = false; 4357 for (BasePermission p : mPermissions.values()) { 4358 if (packageName != null && !packageName.equals(p.sourcePackage)) { 4359 continue; 4360 } 4361 if (permissionNames != null && !permissionNames.contains(p.name)) { 4362 continue; 4363 } 4364 if (!printedSomething) { 4365 if (dumpState.onTitlePrinted()) 4366 pw.println(); 4367 pw.println("Permissions:"); 4368 printedSomething = true; 4369 } 4370 pw.print(" Permission ["); pw.print(p.name); pw.print("] ("); 4371 pw.print(Integer.toHexString(System.identityHashCode(p))); 4372 pw.println("):"); 4373 pw.print(" sourcePackage="); pw.println(p.sourcePackage); 4374 pw.print(" uid="); pw.print(p.uid); 4375 pw.print(" gids="); pw.print(Arrays.toString( 4376 p.computeGids(UserHandle.USER_SYSTEM))); 4377 pw.print(" type="); pw.print(p.type); 4378 pw.print(" prot="); 4379 pw.println(PermissionInfo.protectionToString(p.protectionLevel)); 4380 if (p.perm != null) { 4381 pw.print(" perm="); pw.println(p.perm); 4382 if ((p.perm.info.flags & PermissionInfo.FLAG_INSTALLED) == 0 4383 || (p.perm.info.flags & PermissionInfo.FLAG_HIDDEN) != 0) { 4384 pw.print(" flags=0x"); pw.println(Integer.toHexString(p.perm.info.flags)); 4385 } 4386 } 4387 if (p.packageSetting != null) { 4388 pw.print(" packageSetting="); pw.println(p.packageSetting); 4389 } 4390 if (READ_EXTERNAL_STORAGE.equals(p.name)) { 4391 pw.print(" enforced="); 4392 pw.println(mReadExternalStorageEnforced); 4393 } 4394 } 4395 } 4396 4397 void dumpSharedUsersLPr(PrintWriter pw, String packageName, ArraySet<String> permissionNames, 4398 DumpState dumpState, boolean checkin) { 4399 boolean printedSomething = false; 4400 for (SharedUserSetting su : mSharedUsers.values()) { 4401 if (packageName != null && su != dumpState.getSharedUser()) { 4402 continue; 4403 } 4404 if (permissionNames != null 4405 && !su.getPermissionsState().hasRequestedPermission(permissionNames)) { 4406 continue; 4407 } 4408 if (!checkin) { 4409 if (!printedSomething) { 4410 if (dumpState.onTitlePrinted()) 4411 pw.println(); 4412 pw.println("Shared users:"); 4413 printedSomething = true; 4414 } 4415 pw.print(" SharedUser ["); 4416 pw.print(su.name); 4417 pw.print("] ("); 4418 pw.print(Integer.toHexString(System.identityHashCode(su))); 4419 pw.println("):"); 4420 4421 String prefix = " "; 4422 pw.print(prefix); pw.print("userId="); pw.println(su.userId); 4423 4424 PermissionsState permissionsState = su.getPermissionsState(); 4425 dumpInstallPermissionsLPr(pw, prefix, permissionNames, permissionsState); 4426 4427 for (int userId : UserManagerService.getInstance().getUserIds()) { 4428 final int[] gids = permissionsState.computeGids(userId); 4429 List<PermissionState> permissions = permissionsState 4430 .getRuntimePermissionStates(userId); 4431 if (!ArrayUtils.isEmpty(gids) || !permissions.isEmpty()) { 4432 pw.print(prefix); pw.print("User "); pw.print(userId); pw.println(": "); 4433 dumpGidsLPr(pw, prefix + " ", gids); 4434 dumpRuntimePermissionsLPr(pw, prefix + " ", permissionNames, permissions, 4435 packageName != null); 4436 } 4437 } 4438 } else { 4439 pw.print("suid,"); pw.print(su.userId); pw.print(","); pw.println(su.name); 4440 } 4441 } 4442 } 4443 4444 void dumpReadMessagesLPr(PrintWriter pw, DumpState dumpState) { 4445 pw.println("Settings parse messages:"); 4446 pw.print(mReadMessages.toString()); 4447 } 4448 4449 private static void dumpSplitNames(PrintWriter pw, PackageParser.Package pkg) { 4450 if (pkg == null) { 4451 pw.print("unknown"); 4452 } else { 4453 // [base:10, config.mdpi, config.xhdpi:12] 4454 pw.print("["); 4455 pw.print("base"); 4456 if (pkg.baseRevisionCode != 0) { 4457 pw.print(":"); pw.print(pkg.baseRevisionCode); 4458 } 4459 if (pkg.splitNames != null) { 4460 for (int i = 0; i < pkg.splitNames.length; i++) { 4461 pw.print(", "); 4462 pw.print(pkg.splitNames[i]); 4463 if (pkg.splitRevisionCodes[i] != 0) { 4464 pw.print(":"); pw.print(pkg.splitRevisionCodes[i]); 4465 } 4466 } 4467 } 4468 pw.print("]"); 4469 } 4470 } 4471 4472 void dumpGidsLPr(PrintWriter pw, String prefix, int[] gids) { 4473 if (!ArrayUtils.isEmpty(gids)) { 4474 pw.print(prefix); 4475 pw.print("gids="); pw.println( 4476 PackageManagerService.arrayToString(gids)); 4477 } 4478 } 4479 4480 void dumpRuntimePermissionsLPr(PrintWriter pw, String prefix, ArraySet<String> permissionNames, 4481 List<PermissionState> permissionStates, boolean dumpAll) { 4482 if (!permissionStates.isEmpty() || dumpAll) { 4483 pw.print(prefix); pw.println("runtime permissions:"); 4484 for (PermissionState permissionState : permissionStates) { 4485 if (permissionNames != null 4486 && !permissionNames.contains(permissionState.getName())) { 4487 continue; 4488 } 4489 pw.print(prefix); pw.print(" "); pw.print(permissionState.getName()); 4490 pw.print(": granted="); pw.print(permissionState.isGranted()); 4491 pw.println(permissionFlagsToString(", flags=", 4492 permissionState.getFlags())); 4493 } 4494 } 4495 } 4496 4497 private static String permissionFlagsToString(String prefix, int flags) { 4498 StringBuilder flagsString = null; 4499 while (flags != 0) { 4500 if (flagsString == null) { 4501 flagsString = new StringBuilder(); 4502 flagsString.append(prefix); 4503 flagsString.append("[ "); 4504 } 4505 final int flag = 1 << Integer.numberOfTrailingZeros(flags); 4506 flags &= ~flag; 4507 flagsString.append(PackageManager.permissionFlagToString(flag)); 4508 flagsString.append(' '); 4509 } 4510 if (flagsString != null) { 4511 flagsString.append(']'); 4512 return flagsString.toString(); 4513 } else { 4514 return ""; 4515 } 4516 } 4517 4518 void dumpInstallPermissionsLPr(PrintWriter pw, String prefix, ArraySet<String> permissionNames, 4519 PermissionsState permissionsState) { 4520 List<PermissionState> permissionStates = permissionsState.getInstallPermissionStates(); 4521 if (!permissionStates.isEmpty()) { 4522 pw.print(prefix); pw.println("install permissions:"); 4523 for (PermissionState permissionState : permissionStates) { 4524 if (permissionNames != null 4525 && !permissionNames.contains(permissionState.getName())) { 4526 continue; 4527 } 4528 pw.print(prefix); pw.print(" "); pw.print(permissionState.getName()); 4529 pw.print(": granted="); pw.print(permissionState.isGranted()); 4530 pw.println(permissionFlagsToString(", flags=", 4531 permissionState.getFlags())); 4532 } 4533 } 4534 } 4535 4536 public void writeRuntimePermissionsForUserLPr(int userId, boolean sync) { 4537 if (sync) { 4538 mRuntimePermissionsPersistence.writePermissionsForUserSyncLPr(userId); 4539 } else { 4540 mRuntimePermissionsPersistence.writePermissionsForUserAsyncLPr(userId); 4541 } 4542 } 4543 4544 private final class RuntimePermissionPersistence { 4545 private static final long WRITE_PERMISSIONS_DELAY_MILLIS = 200; 4546 4547 private static final long MAX_WRITE_PERMISSIONS_DELAY_MILLIS = 2000; 4548 4549 private final Handler mHandler = new MyHandler(); 4550 4551 private final Object mLock; 4552 4553 @GuardedBy("mLock") 4554 private final SparseBooleanArray mWriteScheduled = new SparseBooleanArray(); 4555 4556 @GuardedBy("mLock") 4557 // The mapping keys are user ids. 4558 private final SparseLongArray mLastNotWrittenMutationTimesMillis = new SparseLongArray(); 4559 4560 @GuardedBy("mLock") 4561 // The mapping keys are user ids. 4562 private final SparseArray<String> mFingerprints = new SparseArray<>(); 4563 4564 @GuardedBy("mLock") 4565 // The mapping keys are user ids. 4566 private final SparseBooleanArray mDefaultPermissionsGranted = new SparseBooleanArray(); 4567 4568 public RuntimePermissionPersistence(Object lock) { 4569 mLock = lock; 4570 } 4571 4572 public boolean areDefaultRuntimPermissionsGrantedLPr(int userId) { 4573 return mDefaultPermissionsGranted.get(userId); 4574 } 4575 4576 public void onDefaultRuntimePermissionsGrantedLPr(int userId) { 4577 mFingerprints.put(userId, Build.FINGERPRINT); 4578 writePermissionsForUserAsyncLPr(userId); 4579 } 4580 4581 public void writePermissionsForUserSyncLPr(int userId) { 4582 mHandler.removeMessages(userId); 4583 writePermissionsSync(userId); 4584 } 4585 4586 public void writePermissionsForUserAsyncLPr(int userId) { 4587 final long currentTimeMillis = SystemClock.uptimeMillis(); 4588 4589 if (mWriteScheduled.get(userId)) { 4590 mHandler.removeMessages(userId); 4591 4592 // If enough time passed, write without holding off anymore. 4593 final long lastNotWrittenMutationTimeMillis = mLastNotWrittenMutationTimesMillis 4594 .get(userId); 4595 final long timeSinceLastNotWrittenMutationMillis = currentTimeMillis 4596 - lastNotWrittenMutationTimeMillis; 4597 if (timeSinceLastNotWrittenMutationMillis >= MAX_WRITE_PERMISSIONS_DELAY_MILLIS) { 4598 mHandler.obtainMessage(userId).sendToTarget(); 4599 return; 4600 } 4601 4602 // Hold off a bit more as settings are frequently changing. 4603 final long maxDelayMillis = Math.max(lastNotWrittenMutationTimeMillis 4604 + MAX_WRITE_PERMISSIONS_DELAY_MILLIS - currentTimeMillis, 0); 4605 final long writeDelayMillis = Math.min(WRITE_PERMISSIONS_DELAY_MILLIS, 4606 maxDelayMillis); 4607 4608 Message message = mHandler.obtainMessage(userId); 4609 mHandler.sendMessageDelayed(message, writeDelayMillis); 4610 } else { 4611 mLastNotWrittenMutationTimesMillis.put(userId, currentTimeMillis); 4612 Message message = mHandler.obtainMessage(userId); 4613 mHandler.sendMessageDelayed(message, WRITE_PERMISSIONS_DELAY_MILLIS); 4614 mWriteScheduled.put(userId, true); 4615 } 4616 } 4617 4618 private void writePermissionsSync(int userId) { 4619 AtomicFile destination = new AtomicFile(getUserRuntimePermissionsFile(userId)); 4620 4621 ArrayMap<String, List<PermissionState>> permissionsForPackage = new ArrayMap<>(); 4622 ArrayMap<String, List<PermissionState>> permissionsForSharedUser = new ArrayMap<>(); 4623 4624 synchronized (mLock) { 4625 mWriteScheduled.delete(userId); 4626 4627 final int packageCount = mPackages.size(); 4628 for (int i = 0; i < packageCount; i++) { 4629 String packageName = mPackages.keyAt(i); 4630 PackageSetting packageSetting = mPackages.valueAt(i); 4631 if (packageSetting.sharedUser == null) { 4632 PermissionsState permissionsState = packageSetting.getPermissionsState(); 4633 List<PermissionState> permissionsStates = permissionsState 4634 .getRuntimePermissionStates(userId); 4635 if (!permissionsStates.isEmpty()) { 4636 permissionsForPackage.put(packageName, permissionsStates); 4637 } 4638 } 4639 } 4640 4641 final int sharedUserCount = mSharedUsers.size(); 4642 for (int i = 0; i < sharedUserCount; i++) { 4643 String sharedUserName = mSharedUsers.keyAt(i); 4644 SharedUserSetting sharedUser = mSharedUsers.valueAt(i); 4645 PermissionsState permissionsState = sharedUser.getPermissionsState(); 4646 List<PermissionState> permissionsStates = permissionsState 4647 .getRuntimePermissionStates(userId); 4648 if (!permissionsStates.isEmpty()) { 4649 permissionsForSharedUser.put(sharedUserName, permissionsStates); 4650 } 4651 } 4652 } 4653 4654 FileOutputStream out = null; 4655 try { 4656 out = destination.startWrite(); 4657 4658 XmlSerializer serializer = Xml.newSerializer(); 4659 serializer.setOutput(out, StandardCharsets.UTF_8.name()); 4660 serializer.setFeature( 4661 "http://xmlpull.org/v1/doc/features.html#indent-output", true); 4662 serializer.startDocument(null, true); 4663 serializer.startTag(null, TAG_RUNTIME_PERMISSIONS); 4664 4665 String fingerprint = mFingerprints.get(userId); 4666 if (fingerprint != null) { 4667 serializer.attribute(null, ATTR_FINGERPRINT, fingerprint); 4668 } 4669 4670 final int packageCount = permissionsForPackage.size(); 4671 for (int i = 0; i < packageCount; i++) { 4672 String packageName = permissionsForPackage.keyAt(i); 4673 List<PermissionState> permissionStates = permissionsForPackage.valueAt(i); 4674 serializer.startTag(null, TAG_PACKAGE); 4675 serializer.attribute(null, ATTR_NAME, packageName); 4676 writePermissions(serializer, permissionStates); 4677 serializer.endTag(null, TAG_PACKAGE); 4678 } 4679 4680 final int sharedUserCount = permissionsForSharedUser.size(); 4681 for (int i = 0; i < sharedUserCount; i++) { 4682 String packageName = permissionsForSharedUser.keyAt(i); 4683 List<PermissionState> permissionStates = permissionsForSharedUser.valueAt(i); 4684 serializer.startTag(null, TAG_SHARED_USER); 4685 serializer.attribute(null, ATTR_NAME, packageName); 4686 writePermissions(serializer, permissionStates); 4687 serializer.endTag(null, TAG_SHARED_USER); 4688 } 4689 4690 serializer.endTag(null, TAG_RUNTIME_PERMISSIONS); 4691 serializer.endDocument(); 4692 destination.finishWrite(out); 4693 4694 if (Build.FINGERPRINT.equals(fingerprint)) { 4695 mDefaultPermissionsGranted.put(userId, true); 4696 } 4697 // Any error while writing is fatal. 4698 } catch (Throwable t) { 4699 Slog.wtf(PackageManagerService.TAG, 4700 "Failed to write settings, restoring backup", t); 4701 destination.failWrite(out); 4702 } finally { 4703 IoUtils.closeQuietly(out); 4704 } 4705 } 4706 4707 private void onUserRemoved(int userId) { 4708 // Make sure we do not 4709 mHandler.removeMessages(userId); 4710 4711 for (SettingBase sb : mPackages.values()) { 4712 revokeRuntimePermissionsAndClearFlags(sb, userId); 4713 } 4714 4715 for (SettingBase sb : mSharedUsers.values()) { 4716 revokeRuntimePermissionsAndClearFlags(sb, userId); 4717 } 4718 } 4719 4720 private void revokeRuntimePermissionsAndClearFlags(SettingBase sb, int userId) { 4721 PermissionsState permissionsState = sb.getPermissionsState(); 4722 for (PermissionState permissionState 4723 : permissionsState.getRuntimePermissionStates(userId)) { 4724 BasePermission bp = mPermissions.get(permissionState.getName()); 4725 if (bp != null) { 4726 permissionsState.revokeRuntimePermission(bp, userId); 4727 permissionsState.updatePermissionFlags(bp, userId, 4728 PackageManager.MASK_PERMISSION_FLAGS, 0); 4729 } 4730 } 4731 } 4732 4733 public void deleteUserRuntimePermissionsFile(int userId) { 4734 getUserRuntimePermissionsFile(userId).delete(); 4735 } 4736 4737 public void readStateForUserSyncLPr(int userId) { 4738 File permissionsFile = getUserRuntimePermissionsFile(userId); 4739 if (!permissionsFile.exists()) { 4740 return; 4741 } 4742 4743 FileInputStream in; 4744 try { 4745 in = new AtomicFile(permissionsFile).openRead(); 4746 } catch (FileNotFoundException fnfe) { 4747 Slog.i(PackageManagerService.TAG, "No permissions state"); 4748 return; 4749 } 4750 4751 try { 4752 XmlPullParser parser = Xml.newPullParser(); 4753 parser.setInput(in, null); 4754 parseRuntimePermissionsLPr(parser, userId); 4755 4756 } catch (XmlPullParserException | IOException e) { 4757 throw new IllegalStateException("Failed parsing permissions file: " 4758 + permissionsFile , e); 4759 } finally { 4760 IoUtils.closeQuietly(in); 4761 } 4762 } 4763 4764 private void parseRuntimePermissionsLPr(XmlPullParser parser, int userId) 4765 throws IOException, XmlPullParserException { 4766 final int outerDepth = parser.getDepth(); 4767 int type; 4768 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 4769 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 4770 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 4771 continue; 4772 } 4773 4774 switch (parser.getName()) { 4775 case TAG_RUNTIME_PERMISSIONS: { 4776 String fingerprint = parser.getAttributeValue(null, ATTR_FINGERPRINT); 4777 mFingerprints.put(userId, fingerprint); 4778 final boolean defaultsGranted = Build.FINGERPRINT.equals(fingerprint); 4779 mDefaultPermissionsGranted.put(userId, defaultsGranted); 4780 } break; 4781 4782 case TAG_PACKAGE: { 4783 String name = parser.getAttributeValue(null, ATTR_NAME); 4784 PackageSetting ps = mPackages.get(name); 4785 if (ps == null) { 4786 Slog.w(PackageManagerService.TAG, "Unknown package:" + name); 4787 XmlUtils.skipCurrentTag(parser); 4788 continue; 4789 } 4790 parsePermissionsLPr(parser, ps.getPermissionsState(), userId); 4791 } break; 4792 4793 case TAG_SHARED_USER: { 4794 String name = parser.getAttributeValue(null, ATTR_NAME); 4795 SharedUserSetting sus = mSharedUsers.get(name); 4796 if (sus == null) { 4797 Slog.w(PackageManagerService.TAG, "Unknown shared user:" + name); 4798 XmlUtils.skipCurrentTag(parser); 4799 continue; 4800 } 4801 parsePermissionsLPr(parser, sus.getPermissionsState(), userId); 4802 } break; 4803 } 4804 } 4805 } 4806 4807 private void parsePermissionsLPr(XmlPullParser parser, PermissionsState permissionsState, 4808 int userId) throws IOException, XmlPullParserException { 4809 final int outerDepth = parser.getDepth(); 4810 int type; 4811 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 4812 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 4813 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 4814 continue; 4815 } 4816 4817 switch (parser.getName()) { 4818 case TAG_ITEM: { 4819 String name = parser.getAttributeValue(null, ATTR_NAME); 4820 BasePermission bp = mPermissions.get(name); 4821 if (bp == null) { 4822 Slog.w(PackageManagerService.TAG, "Unknown permission:" + name); 4823 XmlUtils.skipCurrentTag(parser); 4824 continue; 4825 } 4826 4827 String grantedStr = parser.getAttributeValue(null, ATTR_GRANTED); 4828 final boolean granted = grantedStr == null 4829 || Boolean.parseBoolean(grantedStr); 4830 4831 String flagsStr = parser.getAttributeValue(null, ATTR_FLAGS); 4832 final int flags = (flagsStr != null) 4833 ? Integer.parseInt(flagsStr, 16) : 0; 4834 4835 if (granted) { 4836 permissionsState.grantRuntimePermission(bp, userId); 4837 permissionsState.updatePermissionFlags(bp, userId, 4838 PackageManager.MASK_PERMISSION_FLAGS, flags); 4839 } else { 4840 permissionsState.updatePermissionFlags(bp, userId, 4841 PackageManager.MASK_PERMISSION_FLAGS, flags); 4842 } 4843 4844 } break; 4845 } 4846 } 4847 } 4848 4849 private void writePermissions(XmlSerializer serializer, 4850 List<PermissionState> permissionStates) throws IOException { 4851 for (PermissionState permissionState : permissionStates) { 4852 serializer.startTag(null, TAG_ITEM); 4853 serializer.attribute(null, ATTR_NAME,permissionState.getName()); 4854 serializer.attribute(null, ATTR_GRANTED, 4855 String.valueOf(permissionState.isGranted())); 4856 serializer.attribute(null, ATTR_FLAGS, 4857 Integer.toHexString(permissionState.getFlags())); 4858 serializer.endTag(null, TAG_ITEM); 4859 } 4860 } 4861 4862 private final class MyHandler extends Handler { 4863 public MyHandler() { 4864 super(BackgroundThread.getHandler().getLooper()); 4865 } 4866 4867 @Override 4868 public void handleMessage(Message message) { 4869 final int userId = message.what; 4870 Runnable callback = (Runnable) message.obj; 4871 writePermissionsSync(userId); 4872 if (callback != null) { 4873 callback.run(); 4874 } 4875 } 4876 } 4877 } 4878} 4879