Merge "Remove PMS snapshot lambdas" into tm-dev

This commit is contained in:
Winson Chiu
2022-02-18 22:04:02 +00:00
committed by Android (Google) Code Review
28 changed files with 986 additions and 1338 deletions

View File

@@ -67,7 +67,7 @@ import java.util.function.Consumer;
*
* @hide Only for use within the system server.
*/
public abstract class PackageManagerInternal implements PackageSettingsSnapshotProvider {
public abstract class PackageManagerInternal {
@IntDef(prefix = "PACKAGE_", value = {
PACKAGE_SYSTEM,
PACKAGE_SETUP_WIZARD,

View File

@@ -1,77 +0,0 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.content.pm;
import android.annotation.NonNull;
import com.android.internal.util.FunctionalUtils;
import com.android.server.pm.PackageManagerService;
import com.android.server.pm.PackageSetting;
import com.android.server.pm.pkg.PackageStateInternal;
import java.util.function.Consumer;
import java.util.function.Function;
/** @hide */
public interface PackageSettingsSnapshotProvider {
/**
* Run a function block that requires access to {@link PackageStateInternal} data. This will
* ensure the {@link PackageManagerService} lock is taken before any caller's internal lock
* to avoid deadlock. Note that this method may or may not lock. If a snapshot is available
* and valid, it will iterate the snapshot set of data.
*/
void withPackageSettingsSnapshot(
@NonNull Consumer<Function<String, PackageStateInternal>> block);
/**
* Variant which returns a value to the caller.
* @see #withPackageSettingsSnapshot(Consumer)
*/
<Output> Output withPackageSettingsSnapshotReturning(
@NonNull FunctionalUtils.ThrowingFunction<Function<String, PackageStateInternal>,
Output> block);
/**
* Variant which throws.
* @see #withPackageSettingsSnapshot(Consumer)
*/
<ExceptionType extends Exception> void withPackageSettingsSnapshotThrowing(
@NonNull FunctionalUtils.ThrowingCheckedConsumer<Function<String, PackageStateInternal>,
ExceptionType> block) throws ExceptionType;
/**
* Variant which throws 2 exceptions.
* @see #withPackageSettingsSnapshot(Consumer)
*/
<ExceptionOne extends Exception, ExceptionTwo extends Exception> void
withPackageSettingsSnapshotThrowing2(
@NonNull FunctionalUtils.ThrowingChecked2Consumer<
Function<String, PackageStateInternal>,
ExceptionOne, ExceptionTwo> block)
throws ExceptionOne, ExceptionTwo;
/**
* Variant which returns a value to the caller and throws.
* @see #withPackageSettingsSnapshot(Consumer)
*/
<Output, ExceptionType extends Exception> Output
withPackageSettingsSnapshotReturningThrowing(
@NonNull FunctionalUtils.ThrowingCheckedFunction<
Function<String, PackageStateInternal>, Output, ExceptionType> block)
throws ExceptionType;
}

View File

@@ -3134,8 +3134,8 @@ public class ComputerEngine implements Computer {
writer.println("Domain verification status:");
writer.increaseIndent();
try {
mDomainVerificationManager.printState(writer, packageName,
UserHandle.USER_ALL, mSettings::getPackage);
mDomainVerificationManager.printState(this, writer, packageName,
UserHandle.USER_ALL);
} catch (Exception e) {
pw.println("Failure printing domain verification information");
Slog.e(TAG, "Failure printing domain verification information", e);

View File

@@ -453,7 +453,8 @@ final class DeletePackageHelper {
}
for (final int affectedUserId : affectedUserIds) {
if (hadSuspendAppsPermission.get(affectedUserId)) {
mPm.unsuspendForSuspendingPackage(packageName, affectedUserId);
mPm.unsuspendForSuspendingPackage(mPm.snapshotComputer(), packageName,
affectedUserId);
mPm.removeAllDistractingPackageRestrictions(affectedUserId);
}
}

View File

@@ -26,17 +26,12 @@ import android.os.Binder;
import android.os.Message;
import android.os.UserHandle;
import com.android.internal.util.FunctionalUtils;
import com.android.server.DeviceIdleInternal;
import com.android.server.pm.parsing.pkg.AndroidPackage;
import com.android.server.pm.pkg.PackageStateInternal;
import com.android.server.pm.verify.domain.DomainVerificationService;
import com.android.server.pm.verify.domain.proxy.DomainVerificationProxyV1;
import com.android.server.pm.verify.domain.proxy.DomainVerificationProxyV2;
import java.util.function.Consumer;
import java.util.function.Function;
public final class DomainVerificationConnection implements DomainVerificationService.Connection,
DomainVerificationProxyV1.Connection, DomainVerificationProxyV2.Connection {
final PackageManagerService mPm;
@@ -111,42 +106,8 @@ public final class DomainVerificationConnection implements DomainVerificationSer
return mUmInternal.exists(userId);
}
@Override
public void withPackageSettingsSnapshot(
@NonNull Consumer<Function<String, PackageStateInternal>> block) {
mPmInternal.withPackageSettingsSnapshot(block);
}
@Override
public <Output> Output withPackageSettingsSnapshotReturning(
@NonNull FunctionalUtils.ThrowingFunction<Function<String, PackageStateInternal>,
Output> block) {
return mPmInternal.withPackageSettingsSnapshotReturning(block);
}
@Override
public <ExceptionType extends Exception> void withPackageSettingsSnapshotThrowing(
@NonNull FunctionalUtils.ThrowingCheckedConsumer<Function<String, PackageStateInternal>,
ExceptionType> block) throws ExceptionType {
mPmInternal.withPackageSettingsSnapshotThrowing(block);
}
@Override
public <ExceptionOne extends Exception, ExceptionTwo extends Exception> void
withPackageSettingsSnapshotThrowing2(
@NonNull FunctionalUtils.ThrowingChecked2Consumer<
Function<String, PackageStateInternal>, ExceptionOne,
ExceptionTwo> block)
throws ExceptionOne, ExceptionTwo {
mPmInternal.withPackageSettingsSnapshotThrowing2(block);
}
@Override
public <Output, ExceptionType extends Exception> Output
withPackageSettingsSnapshotReturningThrowing(
@NonNull FunctionalUtils.ThrowingCheckedFunction<
Function<String, PackageStateInternal>, Output, ExceptionType> block)
throws ExceptionType {
return mPmInternal.withPackageSettingsSnapshotReturningThrowing(block);
@NonNull
public Computer snapshot() {
return (Computer) mPmInternal.snapshot();
}
}

View File

@@ -36,7 +36,6 @@ import static android.content.pm.PackageManager.INSTALL_REASON_DEVICE_RESTORE;
import static android.content.pm.PackageManager.INSTALL_REASON_DEVICE_SETUP;
import static android.content.pm.PackageManager.INSTALL_SUCCEEDED;
import static android.content.pm.PackageManager.UNINSTALL_REASON_UNKNOWN;
import static android.content.pm.PackageManagerInternal.PACKAGE_SETUP_WIZARD;
import static android.content.pm.SigningDetails.SignatureSchemeVersion.SIGNING_BLOCK_V4;
import static android.content.pm.parsing.ApkLiteParseUtils.isApkFile;
import static android.os.PowerExemptionManager.REASON_PACKAGE_REPLACED;
@@ -145,6 +144,7 @@ import com.android.internal.content.F2fsUtils;
import com.android.internal.content.InstallLocationUtils;
import com.android.internal.security.VerityUtils;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.CollectionUtils;
import com.android.internal.util.FrameworkStatsLog;
import com.android.server.EventLogTags;
import com.android.server.pm.dex.ArtManagerService;
@@ -2563,7 +2563,9 @@ final class InstallPackageHelper {
Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
synchronized (mPm.mLock) {
size = mPm.mPendingBroadcasts.size();
final SparseArray<ArrayMap<String, ArrayList<String>>> userIdToPackagesToComponents =
mPm.mPendingBroadcasts.copiedMap();
size = userIdToPackagesToComponents.size();
if (size <= 0) {
// Nothing to be done. Just return
return;
@@ -2573,11 +2575,11 @@ final class InstallPackageHelper {
uids = new int[size];
int i = 0; // filling out the above arrays
for (int n = 0; n < mPm.mPendingBroadcasts.userIdCount(); n++) {
final int packageUserId = mPm.mPendingBroadcasts.userIdAt(n);
for (int n = 0; n < size; n++) {
final int packageUserId = userIdToPackagesToComponents.keyAt(n);
final ArrayMap<String, ArrayList<String>> componentsToBroadcast =
mPm.mPendingBroadcasts.packagesForUserId(packageUserId);
final int numComponents = componentsToBroadcast.size();
userIdToPackagesToComponents.valueAt(n);
final int numComponents = CollectionUtils.size(componentsToBroadcast);
for (int index = 0; i < size && index < numComponents; index++) {
packages[i] = componentsToBroadcast.keyAt(index);
components[i] = componentsToBroadcast.valueAt(index);

View File

@@ -382,6 +382,7 @@ final class PackageHandler extends Handler {
case PRUNE_UNUSED_STATIC_SHARED_LIBRARIES: {
try {
mPm.mInjector.getSharedLibrariesImpl().pruneUnusedStaticSharedLibraries(
mPm.snapshotComputer(),
Long.MAX_VALUE,
Settings.Global.getLong(mPm.mContext.getContentResolver(),
Settings.Global.UNUSED_STATIC_SHARED_LIB_MIN_CACHE_PERIOD,

View File

@@ -293,7 +293,6 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* Keep track of all those APKs everywhere.
@@ -1898,7 +1897,8 @@ public class PackageManagerService extends IPackageManager.Stub
t.traceEnd();
t.traceBegin("read user settings");
mFirstBoot = !mSettings.readLPw(mInjector.getUserManagerInternal().getUsers(
mFirstBoot = !mSettings.readLPw(mLiveComputer,
mInjector.getUserManagerInternal().getUsers(
/* excludePartial= */ true,
/* excludeDying= */ false,
/* excludePreCreated= */ false));
@@ -2841,8 +2841,9 @@ public class PackageManagerService extends IPackageManager.Stub
}
if (file.getUsableSpace() >= bytes) return;
Computer computer = snapshotComputer();
// 5. Consider shared libraries with refcount=0 and age>min cache period
if (internalVolume && mSharedLibraries.pruneUnusedStaticSharedLibraries(bytes,
if (internalVolume && mSharedLibraries.pruneUnusedStaticSharedLibraries(computer, bytes,
android.provider.Settings.Global.getLong(mContext.getContentResolver(),
Global.UNUSED_STATIC_SHARED_LIB_MIN_CACHE_PERIOD,
FREE_STORAGE_UNUSED_STATIC_SHARED_LIB_MIN_CACHE_PERIOD))) {
@@ -2854,14 +2855,12 @@ public class PackageManagerService extends IPackageManager.Stub
// 7. Consider installed instant apps unused longer than min cache period
if (internalVolume) {
if (executeWithConsistentComputerReturning(computer ->
mInstantAppRegistry.pruneInstalledInstantApps(computer, bytes,
android.provider.Settings.Global.getLong(
mContext.getContentResolver(),
Global.INSTALLED_INSTANT_APP_MIN_CACHE_PERIOD,
InstantAppRegistry
.DEFAULT_INSTALLED_INSTANT_APP_MIN_CACHE_PERIOD)))
) {
if (mInstantAppRegistry.pruneInstalledInstantApps(computer, bytes,
android.provider.Settings.Global.getLong(
mContext.getContentResolver(),
Global.INSTALLED_INSTANT_APP_MIN_CACHE_PERIOD,
InstantAppRegistry
.DEFAULT_INSTALLED_INSTANT_APP_MIN_CACHE_PERIOD))) {
return;
}
}
@@ -2881,14 +2880,12 @@ public class PackageManagerService extends IPackageManager.Stub
// 10. Consider instant meta-data (uninstalled apps) older that min cache period
if (internalVolume) {
if (executeWithConsistentComputerReturning(computer ->
mInstantAppRegistry.pruneUninstalledInstantApps(computer, bytes,
android.provider.Settings.Global.getLong(
mContext.getContentResolver(),
Global.UNINSTALLED_INSTANT_APP_MIN_CACHE_PERIOD,
InstantAppRegistry
.DEFAULT_UNINSTALLED_INSTANT_APP_MIN_CACHE_PERIOD)))
) {
if (mInstantAppRegistry.pruneUninstalledInstantApps(computer, bytes,
android.provider.Settings.Global.getLong(
mContext.getContentResolver(),
Global.UNINSTALLED_INSTANT_APP_MIN_CACHE_PERIOD,
InstantAppRegistry
.DEFAULT_UNINSTALLED_INSTANT_APP_MIN_CACHE_PERIOD))) {
return;
}
}
@@ -3493,8 +3490,8 @@ public class PackageManagerService extends IPackageManager.Stub
enforceCrossUserPermission(Binder.getCallingUid(), userId, true /* requireFullPermission */,
false /* checkShell */, "getEphemeralApplications");
List<InstantAppInfo> instantApps = executeWithConsistentComputerReturning(computer ->
mInstantAppRegistry.getInstantApps(computer, userId));
Computer computer = snapshotComputer();
List<InstantAppInfo> instantApps = mInstantAppRegistry.getInstantApps(computer, userId);
if (instantApps != null) {
return new ParceledListSlice<>(instantApps);
}
@@ -3710,13 +3707,13 @@ public class PackageManagerService extends IPackageManager.Stub
public void notifyPackageUse(String packageName, int reason) {
final int callingUid = Binder.getCallingUid();
final int callingUserId = UserHandle.getUserId(callingUid);
boolean notify = executeWithConsistentComputerReturning(computer -> {
if (getInstantAppPackageName(callingUid) != null) {
return isCallerSameApp(packageName, callingUid);
} else {
return !isInstantAppInternal(packageName, callingUserId, Process.SYSTEM_UID);
}
});
Computer computer = snapshotComputer();
final boolean notify;
if (getInstantAppPackageName(callingUid) != null) {
notify = isCallerSameApp(packageName, callingUid);
} else {
notify = !isInstantAppInternal(packageName, callingUserId, Process.SYSTEM_UID);
}
if (!notify) {
return;
}
@@ -4255,34 +4252,33 @@ public class PackageManagerService extends IPackageManager.Stub
final List<String> unactionedPackages = new ArrayList<>(packageNames.length);
ArraySet<String> changesToCommit = new ArraySet<>();
executeWithConsistentComputer(computer -> {
final boolean[] canRestrict = (restrictionFlags != 0)
? mSuspendPackageHelper.canSuspendPackageForUser(computer, packageNames, userId,
callingUid) : null;
for (int i = 0; i < packageNames.length; i++) {
final String packageName = packageNames[i];
final PackageStateInternal packageState =
computer.getPackageStateInternal(packageName);
if (packageState == null
|| shouldFilterApplication(packageState, callingUid, userId)) {
Slog.w(TAG, "Could not find package setting for package: " + packageName
+ ". Skipping...");
unactionedPackages.add(packageName);
continue;
}
if (canRestrict != null && !canRestrict[i]) {
unactionedPackages.add(packageName);
continue;
}
final int oldDistractionFlags = packageState.getUserStateOrDefault(userId)
.getDistractionFlags();
if (restrictionFlags != oldDistractionFlags) {
changedPackagesList.add(packageName);
changedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
changesToCommit.add(packageName);
}
Computer computer = snapshotComputer();
final boolean[] canRestrict = (restrictionFlags != 0)
? mSuspendPackageHelper.canSuspendPackageForUser(computer, packageNames, userId,
callingUid) : null;
for (int i = 0; i < packageNames.length; i++) {
final String packageName = packageNames[i];
final PackageStateInternal packageState =
computer.getPackageStateInternal(packageName);
if (packageState == null
|| computer.shouldFilterApplication(packageState, callingUid, userId)) {
Slog.w(TAG, "Could not find package setting for package: " + packageName
+ ". Skipping...");
unactionedPackages.add(packageName);
continue;
}
});
if (canRestrict != null && !canRestrict[i]) {
unactionedPackages.add(packageName);
continue;
}
final int oldDistractionFlags = packageState.getUserStateOrDefault(userId)
.getDistractionFlags();
if (restrictionFlags != oldDistractionFlags) {
changedPackagesList.add(packageName);
changedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
changesToCommit.add(packageName);
}
}
commitPackageStateMutation(null, mutator -> {
final int size = changesToCommit.size();
@@ -4341,8 +4337,9 @@ public class PackageManagerService extends IPackageManager.Stub
final int callingUid = Binder.getCallingUid();
enforceCanSetPackagesSuspendedAsUser(callingPackage, callingUid, userId,
"setPackagesSuspendedAsUser");
return mSuspendPackageHelper.setPackagesSuspended(packageNames, suspended, appExtras,
launcherExtras, dialogInfo, callingPackage, userId, callingUid);
return mSuspendPackageHelper.setPackagesSuspended(snapshotComputer(), packageNames,
suspended, appExtras, launcherExtras, dialogInfo, callingPackage, userId,
callingUid);
}
@Override
@@ -4361,11 +4358,12 @@ public class PackageManagerService extends IPackageManager.Stub
return mComputer.isPackageSuspendedForUser(packageName, userId);
}
void unsuspendForSuspendingPackage(String suspendingPackage, int userId) {
void unsuspendForSuspendingPackage(@NonNull Computer computer, String suspendingPackage,
@UserIdInt int userId) {
// TODO: This can be replaced by a special parameter to iterate all packages, rather than
// this weird pre-collect of all packages.
final String[] allPackages = getPackageStates().keySet().toArray(new String[0]);
mSuspendPackageHelper.removeSuspensionsBySuspendingPackage(
final String[] allPackages = computer.getPackageStates().keySet().toArray(new String[0]);
mSuspendPackageHelper.removeSuspensionsBySuspendingPackage(computer,
allPackages, suspendingPackage::equals, userId);
}
@@ -4424,7 +4422,7 @@ public class PackageManagerService extends IPackageManager.Stub
throw new SecurityException("Calling uid " + callingUid
+ " cannot query getUnsuspendablePackagesForUser for user " + userId);
}
return mSuspendPackageHelper.getUnsuspendablePackagesForUser(
return mSuspendPackageHelper.getUnsuspendablePackagesForUser(snapshotComputer(),
packageNames, userId, callingUid);
}
@@ -4625,7 +4623,7 @@ public class PackageManagerService extends IPackageManager.Stub
return true;
};
PackageStateMutator.InitialState initialState = recordInitialState();
boolean allowed = executeWithConsistentComputerReturningThrowing(implementation);
boolean allowed = implementation.apply(snapshotComputer());
if (allowed) {
// TODO: Need to lock around here to handle mSettings.addInstallerPackageNames,
// should find an alternative which avoids any race conditions
@@ -4635,7 +4633,7 @@ public class PackageManagerService extends IPackageManager.Stub
targetPackage, state -> state.setInstaller(installerPackageName));
if (result.isPackagesChanged() || result.isStateChanged()) {
synchronized (mPackageStateWriteLock) {
allowed = executeWithConsistentComputerReturningThrowing(implementation);
allowed = implementation.apply(snapshotComputer());
if (allowed) {
commitPackageStateMutation(null, targetPackage,
state -> state.setInstaller(installerPackageName));
@@ -4685,12 +4683,12 @@ public class PackageManagerService extends IPackageManager.Stub
}
};
PackageStateMutator.Result result = executeWithConsistentComputerReturning(implementation);
PackageStateMutator.Result result = implementation.apply(snapshotComputer());
if (result != null && result.isStateChanged() && !result.isSpecificPackageNull()) {
// TODO: Specific return value of what state changed?
// The installer on record might have changed, retry with lock
synchronized (mPackageStateWriteLock) {
result = executeWithConsistentComputerReturning(implementation);
result = implementation.apply(snapshotComputer());
}
}
@@ -4988,7 +4986,7 @@ public class PackageManagerService extends IPackageManager.Stub
}
if (checkPermission(Manifest.permission.SUSPEND_APPS, packageName, userId)
== PERMISSION_GRANTED) {
unsuspendForSuspendingPackage(packageName, userId);
unsuspendForSuspendingPackage(snapshotComputer(), packageName, userId);
removeAllDistractingPackageRestrictions(userId);
flushPackageRestrictionsAsUserInternalLocked(userId);
}
@@ -5080,17 +5078,7 @@ public class PackageManagerService extends IPackageManager.Stub
updateInstantAppInstallerLocked(packageName);
scheduleWritePackageRestrictions(userId);
final ArrayList<String> pendingComponents = mPendingBroadcasts.get(userId, packageName);
if (pendingComponents == null) {
mPendingBroadcasts.put(userId, packageName, updatedComponents);
} else {
for (int i = 0; i < updatedComponents.size(); i++) {
final String updatedComponent = updatedComponents.get(i);
if (!pendingComponents.contains(updatedComponent)) {
pendingComponents.add(updatedComponent);
}
}
}
mPendingBroadcasts.addComponents(userId, packageName, updatedComponents);
if (!mHandler.hasMessages(SEND_PENDING_BROADCAST)) {
mHandler.sendEmptyMessageDelayed(SEND_PENDING_BROADCAST, BROADCAST_DELAY);
}
@@ -5278,7 +5266,8 @@ public class PackageManagerService extends IPackageManager.Stub
try {
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
TypedXmlSerializer serializer = Xml.resolveSerializer(output);
mDomainVerificationManager.writeSettings(serializer, true, userId);
mDomainVerificationManager.writeSettings(snapshotComputer(), serializer, true,
userId);
return output.toByteArray();
}
} catch (Exception e) {
@@ -5301,7 +5290,7 @@ public class PackageManagerService extends IPackageManager.Stub
// User ID input isn't necessary here as it assumes the user integers match and that
// the only states inside the backup XML are for the target user.
mDomainVerificationManager.restoreSettings(parser);
mDomainVerificationManager.restoreSettings(snapshotComputer(), parser);
input.close();
} catch (Exception e) {
if (DEBUG_BACKUP) {
@@ -5689,48 +5678,48 @@ public class PackageManagerService extends IPackageManager.Stub
int callingUid = Binder.getCallingUid();
String componentPkgName = componentName.getPackageName();
boolean changed = executeWithConsistentComputerReturning(computer -> {
int componentUid = getPackageUid(componentPkgName, 0, userId);
if (!UserHandle.isSameApp(callingUid, componentUid)) {
throw new SecurityException("The calling UID (" + callingUid + ")"
+ " does not match the target UID");
}
Computer computer = snapshotComputer();
String allowedCallerPkg =
mContext.getString(R.string.config_overrideComponentUiPackage);
if (TextUtils.isEmpty(allowedCallerPkg)) {
throw new SecurityException( "There is no package defined as allowed to change a "
+ "component's label or icon");
}
int componentUid = computer.getPackageUid(componentPkgName, 0, userId);
if (!UserHandle.isSameApp(callingUid, componentUid)) {
throw new SecurityException("The calling UID (" + callingUid + ")"
+ " does not match the target UID");
}
int allowedCallerUid = getPackageUid(allowedCallerPkg, PackageManager.MATCH_SYSTEM_ONLY,
userId);
if (allowedCallerUid == -1 || !UserHandle.isSameApp(callingUid, allowedCallerUid)) {
throw new SecurityException("The calling UID (" + callingUid + ")"
+ " is not allowed to change a component's label or icon");
}
PackageStateInternal packageState = computer.getPackageStateInternal(componentPkgName);
if (packageState == null || packageState.getPkg() == null
|| (!packageState.isSystem()
&& !packageState.getTransientState().isUpdatedSystemApp())) {
throw new SecurityException(
"Changing the label is not allowed for " + componentName);
}
String allowedCallerPkg =
mContext.getString(R.string.config_overrideComponentUiPackage);
if (TextUtils.isEmpty(allowedCallerPkg)) {
throw new SecurityException( "There is no package defined as allowed to change a "
+ "component's label or icon");
}
if (!mComponentResolver.componentExists(componentName)) {
throw new IllegalArgumentException("Component " + componentName + " not found");
}
int allowedCallerUid = computer.getPackageUid(allowedCallerPkg,
PackageManager.MATCH_SYSTEM_ONLY, userId);
if (allowedCallerUid == -1 || !UserHandle.isSameApp(callingUid, allowedCallerUid)) {
throw new SecurityException("The calling UID (" + callingUid + ")"
+ " is not allowed to change a component's label or icon");
}
PackageStateInternal packageState = computer.getPackageStateInternal(componentPkgName);
if (packageState == null || packageState.getPkg() == null
|| (!packageState.isSystem()
&& !packageState.getTransientState().isUpdatedSystemApp())) {
throw new SecurityException(
"Changing the label is not allowed for " + componentName);
}
Pair<String, Integer> overrideLabelIcon = packageState.getUserStateOrDefault(userId)
.getOverrideLabelIconForComponent(componentName);
if (!computer.getComponentResolver().componentExists(componentName)) {
throw new IllegalArgumentException("Component " + componentName + " not found");
}
String existingLabel = overrideLabelIcon == null ? null : overrideLabelIcon.first;
Integer existingIcon = overrideLabelIcon == null ? null : overrideLabelIcon.second;
Pair<String, Integer> overrideLabelIcon = packageState.getUserStateOrDefault(userId)
.getOverrideLabelIconForComponent(componentName);
return !TextUtils.equals(existingLabel, nonLocalizedLabel)
|| !Objects.equals(existingIcon, icon);
});
if (!changed) {
String existingLabel = overrideLabelIcon == null ? null : overrideLabelIcon.first;
Integer existingIcon = overrideLabelIcon == null ? null : overrideLabelIcon.second;
if (TextUtils.equals(existingLabel, nonLocalizedLabel)
&& Objects.equals(existingIcon, icon)) {
// Nothing changed
return;
}
@@ -5738,16 +5727,7 @@ public class PackageManagerService extends IPackageManager.Stub
state -> state.userState(userId)
.setComponentLabelIcon(componentName, nonLocalizedLabel, icon));
ArrayList<String> components = mPendingBroadcasts.get(userId, componentPkgName);
if (components == null) {
components = new ArrayList<>();
mPendingBroadcasts.put(userId, componentPkgName, components);
}
String className = componentName.getClassName();
if (!components.contains(className)) {
components.add(className);
}
mPendingBroadcasts.addComponent(userId, componentPkgName, componentName.getClassName());
if (!mHandler.hasMessages(SEND_PENDING_BROADCAST)) {
mHandler.sendEmptyMessageDelayed(SEND_PENDING_BROADCAST, BROADCAST_DELAY);
@@ -5956,6 +5936,7 @@ public class PackageManagerService extends IPackageManager.Stub
// packageName -> list of components to send broadcasts now
final ArrayMap<String, ArrayList<String>> sendNowBroadcasts = new ArrayMap<>(targetSize);
synchronized (mLock) {
Computer computer = snapshotComputer();
boolean scheduleBroadcastMessage = false;
boolean isSynchronous = false;
boolean anyChanged = false;
@@ -5967,8 +5948,8 @@ public class PackageManagerService extends IPackageManager.Stub
// update enabled settings
final ComponentEnabledSetting setting = settings.get(i);
final String packageName = setting.getPackageName();
if (!setEnabledSettingInternalLocked(pkgSettings.get(packageName), setting,
userId, callingPackage)) {
if (!setEnabledSettingInternalLocked(computer, pkgSettings.get(packageName),
setting, userId, callingPackage)) {
continue;
}
anyChanged = true;
@@ -5979,26 +5960,18 @@ public class PackageManagerService extends IPackageManager.Stub
// collect broadcast list for the package
final String componentName = setting.isComponent()
? setting.getClassName() : packageName;
ArrayList<String> componentList = sendNowBroadcasts.get(packageName);
if (componentList == null) {
componentList = mPendingBroadcasts.get(userId, packageName);
}
final boolean newPackage = componentList == null;
if (newPackage) {
componentList = new ArrayList<>();
}
if (!componentList.contains(componentName)) {
componentList.add(componentName);
}
if ((setting.getEnabledFlags() & PackageManager.DONT_KILL_APP) == 0) {
ArrayList<String> componentList = sendNowBroadcasts.get(packageName);
componentList = componentList == null ? new ArrayList<>() : componentList;
if (!componentList.contains(componentName)) {
componentList.add(componentName);
}
sendNowBroadcasts.put(packageName, componentList);
// Purge entry from pending broadcast list if another one exists already
// since we are sending one right away.
mPendingBroadcasts.remove(userId, packageName);
} else {
if (newPackage) {
mPendingBroadcasts.put(userId, packageName, componentList);
}
mPendingBroadcasts.addComponent(userId, packageName, componentName);
scheduleBroadcastMessage = true;
}
}
@@ -6041,8 +6014,9 @@ public class PackageManagerService extends IPackageManager.Stub
}
}
private boolean setEnabledSettingInternalLocked(PackageSetting pkgSetting,
ComponentEnabledSetting setting, int userId, String callingPackage) {
private boolean setEnabledSettingInternalLocked(@NonNull Computer computer,
PackageSetting pkgSetting, ComponentEnabledSetting setting, @UserIdInt int userId,
String callingPackage) {
final int newState = setting.getEnabledState();
final String packageName = setting.getPackageName();
boolean success = false;
@@ -6061,7 +6035,7 @@ public class PackageManagerService extends IPackageManager.Stub
// This app should not generally be allowed to get disabled by the UI, but
// if it ever does, we don't want to end up with some of the user's apps
// permanently suspended.
unsuspendForSuspendingPackage(packageName, userId);
unsuspendForSuspendingPackage(computer, packageName, userId);
removeAllDistractingPackageRestrictions(userId);
}
success = true;
@@ -6152,11 +6126,8 @@ public class PackageManagerService extends IPackageManager.Stub
public void setPackageStoppedState(String packageName, boolean stopped, int userId) {
if (!mUserManager.exists(userId)) return;
final int callingUid = Binder.getCallingUid();
Pair<Boolean, String> wasNotLaunchedAndInstallerPackageName =
executeWithConsistentComputerReturningThrowing(computer -> {
if (computer.getInstantAppPackageName(callingUid) != null) {
return null;
}
final Computer computer = snapshotComputer();
if (computer.getInstantAppPackageName(callingUid) == null) {
final int permission = mContext.checkCallingOrSelfPermission(
android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE);
final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED);
@@ -6171,36 +6142,30 @@ public class PackageManagerService extends IPackageManager.Stub
true /* requireFullPermission */, true /* checkShell */, "stop package");
final PackageStateInternal packageState = computer.getPackageStateInternal(packageName);
final PackageUserState PackageUserState = packageState == null
final PackageUserState packageUserState = packageState == null
? null : packageState.getUserStateOrDefault(userId);
if (packageState == null
|| computer.shouldFilterApplication(packageState, callingUid, userId)
|| PackageUserState.isStopped() == stopped) {
return null;
}
if (packageState != null
&& !computer.shouldFilterApplication(packageState, callingUid, userId)
&& packageUserState.isStopped() != stopped) {
boolean wasNotLaunched = packageUserState.isNotLaunched();
commitPackageStateMutation(null, packageName, state -> {
PackageUserStateWrite userState = state.userState(userId);
userState.setStopped(stopped);
if (wasNotLaunched) {
userState.setNotLaunched(false);
}
});
return Pair.create(PackageUserState.isNotLaunched(),
packageState.getInstallSource().installerPackageName);
});
if (wasNotLaunchedAndInstallerPackageName != null) {
boolean wasNotLaunched = wasNotLaunchedAndInstallerPackageName.first;
commitPackageStateMutation(null, packageName, packageState -> {
PackageUserStateWrite userState = packageState.userState(userId);
userState.setStopped(stopped);
if (wasNotLaunched) {
userState.setNotLaunched(false);
final String installerPackageName =
packageState.getInstallSource().installerPackageName;
if (installerPackageName != null) {
notifyFirstLaunch(packageName, installerPackageName, userId);
}
}
});
if (wasNotLaunched) {
final String installerPackageName = wasNotLaunchedAndInstallerPackageName.second;
if (installerPackageName != null) {
notifyFirstLaunch(packageName, installerPackageName, userId);
}
scheduleWritePackageRestrictions(userId);
}
scheduleWritePackageRestrictions(userId);
}
// If this would cause the app to leave force-stop, then also make sure to unhibernate the
@@ -6687,7 +6652,7 @@ public class PackageManagerService extends IPackageManager.Stub
"Only package verification agents can read the verifier device identity");
synchronized (mLock) {
return mSettings.getVerifierDeviceIdentityLPw();
return mSettings.getVerifierDeviceIdentityLPw(mLiveComputer);
}
}
@@ -7026,15 +6991,16 @@ public class PackageManagerService extends IPackageManager.Stub
@Override
public void removeAllNonSystemPackageSuspensions(int userId) {
final String[] allPackages = mComputer.getAllAvailablePackageNames();
mSuspendPackageHelper.removeSuspensionsBySuspendingPackage(allPackages,
final Computer computer = snapshotComputer();
final String[] allPackages = computer.getAllAvailablePackageNames();
mSuspendPackageHelper.removeSuspensionsBySuspendingPackage(computer, allPackages,
(suspendingPackage) -> !PLATFORM_PACKAGE_NAME.equals(suspendingPackage),
userId);
}
@Override
public void removeNonSystemPackageSuspensions(String packageName, int userId) {
mSuspendPackageHelper.removeSuspensionsBySuspendingPackage(
mSuspendPackageHelper.removeSuspensionsBySuspendingPackage(snapshotComputer(),
new String[]{packageName},
(suspendingPackage) -> !PLATFORM_PACKAGE_NAME.equals(suspendingPackage),
userId);
@@ -7233,29 +7199,29 @@ public class PackageManagerService extends IPackageManager.Stub
@Override
public void grantImplicitAccess(int userId, Intent intent,
int recipientAppId, int visibleUid, boolean direct, boolean retainOnUpdate) {
boolean accessGranted = executeWithConsistentComputerReturning(computer -> {
final AndroidPackage visiblePackage = computer.getPackage(visibleUid);
final int recipientUid = UserHandle.getUid(userId, recipientAppId);
if (visiblePackage == null || computer.getPackage(recipientUid) == null) {
return false;
}
Computer computer = snapshotComputer();
final AndroidPackage visiblePackage = computer.getPackage(visibleUid);
final int recipientUid = UserHandle.getUid(userId, recipientAppId);
if (visiblePackage == null || computer.getPackage(recipientUid) == null) {
return;
}
final boolean instantApp = computer.isInstantAppInternal(
visiblePackage.getPackageName(), userId, visibleUid);
if (instantApp) {
if (!direct) {
// if the interaction that lead to this granting access to an instant app
// was indirect (i.e.: URI permission grant), do not actually execute the
// grant.
return false;
}
return mInstantAppRegistry.grantInstantAccess(userId, intent,
recipientAppId, UserHandle.getAppId(visibleUid) /*instantAppId*/);
} else {
return mAppsFilter.grantImplicitAccess(recipientUid, visibleUid,
retainOnUpdate);
final boolean instantApp = computer.isInstantAppInternal(
visiblePackage.getPackageName(), userId, visibleUid);
final boolean accessGranted;
if (instantApp) {
if (!direct) {
// if the interaction that lead to this granting access to an instant app
// was indirect (i.e.: URI permission grant), do not actually execute the
// grant.
return;
}
});
accessGranted = mInstantAppRegistry.grantInstantAccess(userId, intent,
recipientAppId, UserHandle.getAppId(visibleUid) /*instantAppId*/);
} else {
accessGranted = mAppsFilter.grantImplicitAccess(recipientUid, visibleUid,
retainOnUpdate);
}
if (accessGranted) {
ApplicationPackageManager.invalidateGetPackagesForUidCache();
@@ -7271,8 +7237,7 @@ public class PackageManagerService extends IPackageManager.Stub
@Override
public void pruneInstantApps() {
executeWithConsistentComputer(computer ->
mInstantAppRegistry.pruneInstantApps(computer));
mInstantAppRegistry.pruneInstantApps(snapshotComputer());
}
@Override
@@ -7680,7 +7645,8 @@ public class PackageManagerService extends IPackageManager.Stub
@Override
public void unsuspendForSuspendingPackage(final String packageName, int affectedUser) {
PackageManagerService.this.unsuspendForSuspendingPackage(packageName, affectedUser);
PackageManagerService.this.unsuspendForSuspendingPackage(snapshotComputer(),
packageName, affectedUser);
}
@Override
@@ -7743,52 +7709,6 @@ public class PackageManagerService extends IPackageManager.Stub
return PackageManagerService.this.deleteOatArtifactsOfPackage(packageName);
}
@Override
public void withPackageSettingsSnapshot(
@NonNull Consumer<Function<String, PackageStateInternal>> block) {
executeWithConsistentComputer(computer ->
block.accept(computer::getPackageStateInternal));
}
@Override
public <Output> Output withPackageSettingsSnapshotReturning(
@NonNull FunctionalUtils.ThrowingFunction<Function<String, PackageStateInternal>,
Output> block) {
return executeWithConsistentComputerReturning(computer ->
block.apply(computer::getPackageStateInternal));
}
@Override
public <ExceptionType extends Exception> void withPackageSettingsSnapshotThrowing(
@NonNull FunctionalUtils.ThrowingCheckedConsumer<Function<String,
PackageStateInternal>, ExceptionType> block) throws ExceptionType {
executeWithConsistentComputerThrowing(computer ->
block.accept(computer::getPackageStateInternal));
}
@Override
public <ExceptionOne extends Exception, ExceptionTwo extends Exception> void
withPackageSettingsSnapshotThrowing2(
@NonNull FunctionalUtils.ThrowingChecked2Consumer<
Function<String, PackageStateInternal>, ExceptionOne,
ExceptionTwo> block)
throws ExceptionOne, ExceptionTwo {
executeWithConsistentComputerThrowing2(
(FunctionalUtils.ThrowingChecked2Consumer<Computer, ExceptionOne,
ExceptionTwo>) computer -> block.accept(computer::getPackageStateInternal));
}
@Override
public <Output, ExceptionType extends Exception> Output
withPackageSettingsSnapshotReturningThrowing(
@NonNull FunctionalUtils.ThrowingCheckedFunction<
Function<String, PackageStateInternal>, Output,
ExceptionType> block)
throws ExceptionType {
return executeWithConsistentComputerReturningThrowing(computer ->
block.apply(computer::getPackageStateInternal));
}
@Override
public void reconcileAppsData(int userId, @StorageManager.StorageFlags int flags,
boolean migrateAppsData) {
@@ -7834,70 +7754,59 @@ public class PackageManagerService extends IPackageManager.Stub
@NonNull Set<String> outUpdatedPackageNames) {
synchronized (mOverlayPathsLock) {
final ArrayMap<String, ArraySet<String>> libNameToModifiedDependents = new ArrayMap<>();
Boolean targetModified = executeWithConsistentComputerReturning(computer -> {
final PackageStateInternal packageState = computer.getPackageStateInternal(
targetPackageName);
final AndroidPackage targetPkg =
packageState == null ? null : packageState.getPkg();
if (targetPackageName == null || targetPkg == null) {
Slog.e(TAG, "failed to find package " + targetPackageName);
return null;
}
if (Objects.equals(packageState.getUserStateOrDefault(userId).getOverlayPaths(),
newOverlayPaths)) {
return false;
}
if (targetPkg.getLibraryNames() != null) {
// Set the overlay paths for dependencies of the shared library.
for (final String libName : targetPkg.getLibraryNames()) {
ArraySet<String> modifiedDependents = null;
final SharedLibraryInfo info = computer.getSharedLibraryInfo(libName,
SharedLibraryInfo.VERSION_UNDEFINED);
if (info == null) {
continue;
}
final List<VersionedPackage> dependents = computer
.getPackagesUsingSharedLibrary(info, 0, Process.SYSTEM_UID, userId);
if (dependents == null) {
continue;
}
for (final VersionedPackage dependent : dependents) {
final PackageStateInternal dependentState =
computer.getPackageStateInternal(dependent.getPackageName());
if (dependentState == null) {
continue;
}
if (!Objects.equals(dependentState.getUserStateOrDefault(userId)
.getSharedLibraryOverlayPaths()
.get(libName), newOverlayPaths)) {
String dependentPackageName = dependent.getPackageName();
modifiedDependents = ArrayUtils.add(modifiedDependents,
dependentPackageName);
outUpdatedPackageNames.add(dependentPackageName);
}
}
if (modifiedDependents != null) {
libNameToModifiedDependents.put(libName, modifiedDependents);
}
}
}
outUpdatedPackageNames.add(targetPackageName);
return true;
});
if (targetModified == null) {
// Null indicates error
Computer computer = snapshotComputer();
final PackageStateInternal packageState = computer.getPackageStateInternal(
targetPackageName);
final AndroidPackage targetPkg = packageState == null ? null : packageState.getPkg();
if (targetPackageName == null || targetPkg == null) {
Slog.e(TAG, "failed to find package " + targetPackageName);
return false;
} else if (!targetModified) {
// Treat non-modification as a successful commit
}
if (Objects.equals(packageState.getUserStateOrDefault(userId).getOverlayPaths(),
newOverlayPaths)) {
return true;
}
if (targetPkg.getLibraryNames() != null) {
// Set the overlay paths for dependencies of the shared library.
for (final String libName : targetPkg.getLibraryNames()) {
ArraySet<String> modifiedDependents = null;
final SharedLibraryInfo info = computer.getSharedLibraryInfo(libName,
SharedLibraryInfo.VERSION_UNDEFINED);
if (info == null) {
continue;
}
final List<VersionedPackage> dependents = computer
.getPackagesUsingSharedLibrary(info, 0, Process.SYSTEM_UID, userId);
if (dependents == null) {
continue;
}
for (final VersionedPackage dependent : dependents) {
final PackageStateInternal dependentState =
computer.getPackageStateInternal(dependent.getPackageName());
if (dependentState == null) {
continue;
}
if (!Objects.equals(dependentState.getUserStateOrDefault(userId)
.getSharedLibraryOverlayPaths()
.get(libName), newOverlayPaths)) {
String dependentPackageName = dependent.getPackageName();
modifiedDependents = ArrayUtils.add(modifiedDependents,
dependentPackageName);
outUpdatedPackageNames.add(dependentPackageName);
}
}
if (modifiedDependents != null) {
libNameToModifiedDependents.put(libName, modifiedDependents);
}
}
}
outUpdatedPackageNames.add(targetPackageName);
commitPackageStateMutation(null, mutator -> {
mutator.forPackage(targetPackageName)
.userState(userId)
@@ -8062,36 +7971,6 @@ public class PackageManagerService extends IPackageManager.Stub
forEachPackageState(mComputer.getPackageStates(), actionWrapped);
}
// TODO: Make private
void executeWithConsistentComputer(
@NonNull FunctionalUtils.ThrowingConsumer<Computer> consumer) {
consumer.accept(snapshotComputer());
}
private <T> T executeWithConsistentComputerReturning(
@NonNull FunctionalUtils.ThrowingFunction<Computer, T> function) {
return function.apply(snapshotComputer());
}
private <ExceptionType extends Exception> void executeWithConsistentComputerThrowing(
@NonNull FunctionalUtils.ThrowingCheckedConsumer<Computer, ExceptionType> consumer)
throws ExceptionType {
consumer.accept(snapshotComputer());
}
private <ExceptionOne extends Exception, ExceptionTwo extends Exception> void
executeWithConsistentComputerThrowing2(
@NonNull FunctionalUtils.ThrowingChecked2Consumer<Computer, ExceptionOne,
ExceptionTwo> consumer) throws ExceptionOne, ExceptionTwo {
consumer.accept(snapshotComputer());
}
private <T, ExceptionType extends Exception> T executeWithConsistentComputerReturningThrowing(
@NonNull FunctionalUtils.ThrowingCheckedFunction<Computer, T, ExceptionType> function)
throws ExceptionType {
return function.apply(snapshotComputer());
}
boolean isHistoricalPackageUsageAvailable() {
return mPackageUsage.isHistoricalPackageUsageAvailable();
}
@@ -8357,7 +8236,7 @@ public class PackageManagerService extends IPackageManager.Stub
*/
void writeSettingsLPrTEMP() {
mPermissionManager.writeLegacyPermissionsTEMP(mSettings.mPermissions);
mSettings.writeLPr();
mSettings.writeLPr(mLiveComputer);
}
@Override
@@ -8856,7 +8735,6 @@ public class PackageManagerService extends IPackageManager.Stub
}
void notifyInstantAppPackageInstalled(String packageName, int[] newUsers) {
executeWithConsistentComputer(computer ->
mInstantAppRegistry.onPackageInstalled(computer, packageName, newUsers));
mInstantAppRegistry.onPackageInstalled(snapshotComputer(), packageName, newUsers);
}
}

View File

@@ -16,12 +16,17 @@
package com.android.server.pm;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.util.ArrayMap;
import android.util.SparseArray;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import java.util.ArrayList;
import java.util.List;
/**
* Set of pending broadcasts for aggregating enable/disable of components.
@@ -29,65 +34,111 @@ import java.util.ArrayList;
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
public final class PendingPackageBroadcasts {
private final Object mLock = new PackageManagerTracedLock();
// for each user id, a map of <package name -> components within that package>
@GuardedBy("mLock")
final SparseArray<ArrayMap<String, ArrayList<String>>> mUidMap;
public PendingPackageBroadcasts() {
mUidMap = new SparseArray<>(2);
}
public ArrayList<String> get(int userId, String packageName) {
ArrayMap<String, ArrayList<String>> packages = getOrAllocate(userId);
return packages.get(packageName);
public boolean hasPackage(@UserIdInt int userId, @NonNull String packageName) {
synchronized (mLock) {
final ArrayMap<String, ArrayList<String>> packages = mUidMap.get(userId);
return packages != null && packages.containsKey(packageName);
}
}
public void put(int userId, String packageName, ArrayList<String> components) {
ArrayMap<String, ArrayList<String>> packages = getOrAllocate(userId);
packages.put(packageName, components);
synchronized (mLock) {
ArrayMap<String, ArrayList<String>> packages = getOrAllocate(userId);
packages.put(packageName, components);
}
}
public void addComponent(@UserIdInt int userId, @NonNull String packageName,
@NonNull String componentClassName) {
synchronized (mLock) {
ArrayList<String> components = getOrAllocate(userId, packageName);
if (!components.contains(componentClassName)) {
components.add(componentClassName);
}
}
}
public void addComponents(@UserIdInt int userId, @NonNull String packageName,
@NonNull List<String> componentClassNames) {
synchronized (mLock) {
ArrayList<String> components = getOrAllocate(userId, packageName);
for (int index = 0; index < componentClassNames.size(); index++) {
String componentClassName = componentClassNames.get(index);
if (!components.contains(componentClassName)) {
components.add(componentClassName);
}
}
}
}
public void remove(int userId, String packageName) {
ArrayMap<String, ArrayList<String>> packages = mUidMap.get(userId);
if (packages != null) {
packages.remove(packageName);
synchronized (mLock) {
ArrayMap<String, ArrayList<String>> packages = mUidMap.get(userId);
if (packages != null) {
packages.remove(packageName);
}
}
}
public void remove(int userId) {
mUidMap.remove(userId);
}
public int userIdCount() {
return mUidMap.size();
}
public int userIdAt(int n) {
return mUidMap.keyAt(n);
}
public ArrayMap<String, ArrayList<String>> packagesForUserId(int userId) {
return mUidMap.get(userId);
}
public int size() {
// total number of pending broadcast entries across all userIds
int num = 0;
for (int i = 0; i < mUidMap.size(); i++) {
num += mUidMap.valueAt(i).size();
synchronized (mLock) {
mUidMap.remove(userId);
}
}
@Nullable
public SparseArray<ArrayMap<String, ArrayList<String>>> copiedMap() {
synchronized (mLock) {
SparseArray<ArrayMap<String, ArrayList<String>>> copy = new SparseArray<>();
for (int userIdIndex = 0; userIdIndex < mUidMap.size(); userIdIndex++) {
final ArrayMap<String, ArrayList<String>> packages = mUidMap.valueAt(userIdIndex);
ArrayMap<String, ArrayList<String>> packagesCopy = new ArrayMap<>();
for (int packagesIndex = 0; packagesIndex < packages.size(); packagesIndex++) {
packagesCopy.put(packages.keyAt(packagesIndex),
new ArrayList<>(packages.valueAt(packagesIndex)));
}
copy.put(mUidMap.keyAt(userIdIndex), packagesCopy);
}
return copy;
}
return num;
}
public void clear() {
mUidMap.clear();
synchronized (mLock) {
mUidMap.clear();
}
}
private ArrayMap<String, ArrayList<String>> getOrAllocate(int userId) {
ArrayMap<String, ArrayList<String>> map = mUidMap.get(userId);
if (map == null) {
map = new ArrayMap<>();
mUidMap.put(userId, map);
synchronized (mLock) {
ArrayMap<String, ArrayList<String>> map = mUidMap.get(userId);
if (map == null) {
map = new ArrayMap<>();
mUidMap.put(userId, map);
}
return map;
}
}
private ArrayList<String> getOrAllocate(int userId, @NonNull String packageName) {
synchronized (mLock) {
ArrayMap<String, ArrayList<String>> map = mUidMap.get(userId);
if (map == null) {
map = new ArrayMap<>();
mUidMap.put(userId, map);
}
return map.computeIfAbsent(packageName, k -> new ArrayList<>());
}
return map;
}
}

View File

@@ -2432,7 +2432,7 @@ public final class Settings implements Watchable, Snappable {
}
}
void writeLPr() {
void writeLPr(@NonNull Computer computer) {
//Debug.startMethodTracing("/data/system/packageprof", 8 * 1024 * 1024);
final long startTime = SystemClock.uptimeMillis();
@@ -2524,8 +2524,8 @@ public final class Settings implements Watchable, Snappable {
}
}
mDomainVerificationManager.writeSettings(serializer, false /* includeSignatures */,
UserHandle.USER_ALL);
mDomainVerificationManager.writeSettings(computer, serializer,
false /* includeSignatures */, UserHandle.USER_ALL);
mKeySetManagerService.writeKeySetManagerServiceLPr(serializer);
@@ -2967,7 +2967,7 @@ public final class Settings implements Watchable, Snappable {
}
}
boolean readLPw(@NonNull List<UserInfo> users) {
boolean readLPw(@NonNull Computer computer, @NonNull List<UserInfo> users) {
FileInputStream str = null;
if (mBackupSettingsFilename.exists()) {
try {
@@ -3111,7 +3111,7 @@ public final class Settings implements Watchable, Snappable {
ver.databaseVersion = parser.getAttributeInt(null, ATTR_DATABASE_VERSION);
ver.fingerprint = XmlUtils.readStringAttribute(parser, ATTR_FINGERPRINT);
} else if (tagName.equals(DomainVerificationPersistence.TAG_DOMAIN_VERIFICATIONS)) {
mDomainVerificationManager.readSettings(parser);
mDomainVerificationManager.readSettings(computer, parser);
} else if (tagName.equals(
DomainVerificationLegacySettings.TAG_DOMAIN_VERIFICATIONS_LEGACY)) {
mDomainVerificationManager.readLegacySettings(parser);
@@ -4287,11 +4287,11 @@ public final class Settings implements Watchable, Snappable {
return Process.FIRST_APPLICATION_UID + size;
}
public VerifierDeviceIdentity getVerifierDeviceIdentityLPw() {
public VerifierDeviceIdentity getVerifierDeviceIdentityLPw(@NonNull Computer computer) {
if (mVerifierDeviceIdentity == null) {
mVerifierDeviceIdentity = VerifierDeviceIdentity.generate();
writeLPr();
writeLPr(computer);
}
return mVerifierDeviceIdentity;

View File

@@ -321,7 +321,8 @@ public final class SharedLibrariesImpl implements SharedLibrariesRead, Watchable
* on the device.
* @return {@code true} if the available storage space is reached.
*/
boolean pruneUnusedStaticSharedLibraries(long neededSpace, long maxCachePeriod)
boolean pruneUnusedStaticSharedLibraries(@NonNull Computer computer, long neededSpace,
long maxCachePeriod)
throws IOException {
final StorageManager storage = mInjector.getSystemService(StorageManager.class);
final File volume = storage.findPathForUuid(StorageManager.UUID_PRIVATE_INTERNAL);
@@ -332,38 +333,36 @@ public final class SharedLibrariesImpl implements SharedLibrariesRead, Watchable
// Important: We skip shared libs used for some user since
// in such a case we need to keep the APK on the device. The check for
// a lib being used for any user is performed by the uninstall call.
mPm.executeWithConsistentComputer(computer -> {
final WatchedArrayMap<String, WatchedLongSparseArray<SharedLibraryInfo>>
sharedLibraries = computer.getSharedLibraries();
final int libCount = sharedLibraries.size();
for (int i = 0; i < libCount; i++) {
final WatchedLongSparseArray<SharedLibraryInfo> versionedLib =
sharedLibraries.valueAt(i);
if (versionedLib == null) {
final WatchedArrayMap<String, WatchedLongSparseArray<SharedLibraryInfo>>
sharedLibraries = computer.getSharedLibraries();
final int libCount = sharedLibraries.size();
for (int i = 0; i < libCount; i++) {
final WatchedLongSparseArray<SharedLibraryInfo> versionedLib =
sharedLibraries.valueAt(i);
if (versionedLib == null) {
continue;
}
final int versionCount = versionedLib.size();
for (int j = 0; j < versionCount; j++) {
SharedLibraryInfo libInfo = versionedLib.valueAt(j);
final PackageStateInternal ps = getLibraryPackage(computer, libInfo);
if (ps == null) {
continue;
}
final int versionCount = versionedLib.size();
for (int j = 0; j < versionCount; j++) {
SharedLibraryInfo libInfo = versionedLib.valueAt(j);
final PackageStateInternal ps = getLibraryPackage(computer, libInfo);
if (ps == null) {
continue;
}
// Skip unused libs cached less than the min period to prevent pruning a lib
// needed by a subsequently installed package.
if (now - ps.getLastUpdateTime() < maxCachePeriod) {
continue;
}
if (ps.getPkg().isSystem()) {
continue;
}
packagesToDelete.add(new VersionedPackage(ps.getPkg().getPackageName(),
libInfo.getDeclaringPackage().getLongVersionCode()));
// Skip unused libs cached less than the min period to prevent pruning a lib
// needed by a subsequently installed package.
if (now - ps.getLastUpdateTime() < maxCachePeriod) {
continue;
}
if (ps.getPkg().isSystem()) {
continue;
}
packagesToDelete.add(new VersionedPackage(ps.getPkg().getPackageName(),
libInfo.getDeclaringPackage().getLongVersionCode()));
}
});
}
final int packageCount = packagesToDelete.size();
for (int i = 0; i < packageCount; i++) {

View File

@@ -27,6 +27,7 @@ import static com.android.server.pm.PackageManagerService.TAG;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.ActivityManager;
import android.app.IActivityManager;
import android.content.Intent;
@@ -99,10 +100,10 @@ public final class SuspendPackageHelper {
* @return The names of failed packages.
*/
@Nullable
String[] setPackagesSuspended(@Nullable String[] packageNames, boolean suspended,
@Nullable PersistableBundle appExtras, @Nullable PersistableBundle launcherExtras,
@Nullable SuspendDialogInfo dialogInfo, @NonNull String callingPackage,
int userId, int callingUid) {
String[] setPackagesSuspended(@NonNull Computer computer, @Nullable String[] packageNames,
boolean suspended, @Nullable PersistableBundle appExtras,
@Nullable PersistableBundle launcherExtras, @Nullable SuspendDialogInfo dialogInfo,
@NonNull String callingPackage, @UserIdInt int userId, int callingUid) {
if (ArrayUtils.isEmpty(packageNames)) {
return packageNames;
}
@@ -121,62 +122,60 @@ public final class SuspendPackageHelper {
ArraySet<String> modifiedPackages = new ArraySet<>();
mPm.executeWithConsistentComputer(computer -> {
final boolean[] canSuspend = suspended
? canSuspendPackageForUser(computer, packageNames, userId, callingUid) : null;
for (int i = 0; i < packageNames.length; i++) {
final String packageName = packageNames[i];
if (callingPackage.equals(packageName)) {
Slog.w(TAG, "Calling package: " + callingPackage + " trying to "
+ (suspended ? "" : "un") + "suspend itself. Ignoring");
unmodifiablePackages.add(packageName);
continue;
}
final PackageStateInternal packageState =
computer.getPackageStateInternal(packageName);
if (packageState == null
|| computer.shouldFilterApplication(packageState, callingUid, userId)) {
Slog.w(TAG, "Could not find package setting for package: " + packageName
+ ". Skipping suspending/un-suspending.");
unmodifiablePackages.add(packageName);
continue;
}
if (canSuspend != null && !canSuspend[i]) {
unmodifiablePackages.add(packageName);
continue;
}
final boolean[] canSuspend = suspended
? canSuspendPackageForUser(computer, packageNames, userId, callingUid) : null;
for (int i = 0; i < packageNames.length; i++) {
final String packageName = packageNames[i];
if (callingPackage.equals(packageName)) {
Slog.w(TAG, "Calling package: " + callingPackage + " trying to "
+ (suspended ? "" : "un") + "suspend itself. Ignoring");
unmodifiablePackages.add(packageName);
continue;
}
final PackageStateInternal packageState =
computer.getPackageStateInternal(packageName);
if (packageState == null
|| computer.shouldFilterApplication(packageState, callingUid, userId)) {
Slog.w(TAG, "Could not find package setting for package: " + packageName
+ ". Skipping suspending/un-suspending.");
unmodifiablePackages.add(packageName);
continue;
}
if (canSuspend != null && !canSuspend[i]) {
unmodifiablePackages.add(packageName);
continue;
}
final WatchedArrayMap<String, SuspendParams> suspendParamsMap =
packageState.getUserStateOrDefault(userId).getSuspendParams();
if (suspended) {
if (suspendParamsMap != null && suspendParamsMap.containsKey(packageName)) {
final SuspendParams suspendParams = suspendParamsMap.get(packageName);
// Skip if there's no changes
if (suspendParams != null
&& Objects.equals(suspendParams.getDialogInfo(), dialogInfo)
&& Objects.equals(suspendParams.getAppExtras(), appExtras)
&& Objects.equals(suspendParams.getLauncherExtras(),
launcherExtras)) {
// Carried over API behavior, must notify change even if no change
changedPackagesList.add(packageName);
changedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
continue;
}
final WatchedArrayMap<String, SuspendParams> suspendParamsMap =
packageState.getUserStateOrDefault(userId).getSuspendParams();
if (suspended) {
if (suspendParamsMap != null && suspendParamsMap.containsKey(packageName)) {
final SuspendParams suspendParams = suspendParamsMap.get(packageName);
// Skip if there's no changes
if (suspendParams != null
&& Objects.equals(suspendParams.getDialogInfo(), dialogInfo)
&& Objects.equals(suspendParams.getAppExtras(), appExtras)
&& Objects.equals(suspendParams.getLauncherExtras(),
launcherExtras)) {
// Carried over API behavior, must notify change even if no change
changedPackagesList.add(packageName);
changedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
continue;
}
}
// If size one, the package will be unsuspended from this call
boolean packageUnsuspended =
!suspended && CollectionUtils.size(suspendParamsMap) <= 1;
if (suspended || packageUnsuspended) {
changedPackagesList.add(packageName);
changedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
}
modifiedPackages.add(packageName);
modifiedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
}
});
// If size one, the package will be unsuspended from this call
boolean packageUnsuspended =
!suspended && CollectionUtils.size(suspendParamsMap) <= 1;
if (suspended || packageUnsuspended) {
changedPackagesList.add(packageName);
changedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
}
modifiedPackages.add(packageName);
modifiedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
}
mPm.commitPackageStateMutation(null, mutator -> {
final int size = modifiedPackages.size();
@@ -218,29 +217,27 @@ public final class SuspendPackageHelper {
* @return The names of packages which are Unsuspendable.
*/
@NonNull
String[] getUnsuspendablePackagesForUser(@NonNull String[] packageNames, int userId,
int callingUid) {
String[] getUnsuspendablePackagesForUser(@NonNull Computer computer,
@NonNull String[] packageNames, @UserIdInt int userId, int callingUid) {
if (!isSuspendAllowedForUser(userId, callingUid)) {
Slog.w(TAG, "Cannot suspend due to restrictions on user " + userId);
return packageNames;
}
final ArraySet<String> unactionablePackages = new ArraySet<>();
mPm.executeWithConsistentComputer(computer -> {
final boolean[] canSuspend = canSuspendPackageForUser(computer, packageNames, userId,
callingUid);
for (int i = 0; i < packageNames.length; i++) {
if (!canSuspend[i]) {
unactionablePackages.add(packageNames[i]);
continue;
}
final PackageStateInternal packageState =
computer.getPackageStateFiltered(packageNames[i], callingUid, userId);
if (packageState == null) {
Slog.w(TAG, "Could not find package setting for package: " + packageNames[i]);
unactionablePackages.add(packageNames[i]);
}
final boolean[] canSuspend = canSuspendPackageForUser(computer, packageNames, userId,
callingUid);
for (int i = 0; i < packageNames.length; i++) {
if (!canSuspend[i]) {
unactionablePackages.add(packageNames[i]);
continue;
}
});
final PackageStateInternal packageState =
computer.getPackageStateFiltered(packageNames[i], callingUid, userId);
if (packageState == null) {
Slog.w(TAG, "Could not find package setting for package: " + packageNames[i]);
unactionablePackages.add(packageNames[i]);
}
}
return unactionablePackages.toArray(new String[unactionablePackages.size()]);
}
@@ -282,45 +279,44 @@ public final class SuspendPackageHelper {
* suspensions will be removed.
* @param userId The user for which the changes are taking place.
*/
void removeSuspensionsBySuspendingPackage(@NonNull String[] packagesToChange,
void removeSuspensionsBySuspendingPackage(@NonNull Computer computer,
@NonNull String[] packagesToChange,
@NonNull Predicate<String> suspendingPackagePredicate, int userId) {
final List<String> unsuspendedPackages = new ArrayList<>();
final IntArray unsuspendedUids = new IntArray();
final ArrayMap<String, ArraySet<String>> pkgToSuspendingPkgsToCommit = new ArrayMap<>();
mPm.executeWithConsistentComputer(computer -> {
for (String packageName : packagesToChange) {
final PackageStateInternal packageState =
computer.getPackageStateInternal(packageName);
final PackageUserStateInternal packageUserState = packageState == null
? null : packageState.getUserStateOrDefault(userId);
if (packageUserState == null || !packageUserState.isSuspended()) {
continue;
}
for (String packageName : packagesToChange) {
final PackageStateInternal packageState =
computer.getPackageStateInternal(packageName);
final PackageUserStateInternal packageUserState = packageState == null
? null : packageState.getUserStateOrDefault(userId);
if (packageUserState == null || !packageUserState.isSuspended()) {
continue;
}
WatchedArrayMap<String, SuspendParams> suspendParamsMap =
packageUserState.getSuspendParams();
int countRemoved = 0;
for (int index = 0; index < suspendParamsMap.size(); index++) {
String suspendingPackage = suspendParamsMap.keyAt(index);
if (suspendingPackagePredicate.test(suspendingPackage)) {
ArraySet<String> suspendingPkgsToCommit =
pkgToSuspendingPkgsToCommit.get(packageName);
if (suspendingPkgsToCommit == null) {
suspendingPkgsToCommit = new ArraySet<>();
pkgToSuspendingPkgsToCommit.put(packageName, suspendingPkgsToCommit);
}
suspendingPkgsToCommit.add(suspendingPackage);
countRemoved++;
WatchedArrayMap<String, SuspendParams> suspendParamsMap =
packageUserState.getSuspendParams();
int countRemoved = 0;
for (int index = 0; index < suspendParamsMap.size(); index++) {
String suspendingPackage = suspendParamsMap.keyAt(index);
if (suspendingPackagePredicate.test(suspendingPackage)) {
ArraySet<String> suspendingPkgsToCommit =
pkgToSuspendingPkgsToCommit.get(packageName);
if (suspendingPkgsToCommit == null) {
suspendingPkgsToCommit = new ArraySet<>();
pkgToSuspendingPkgsToCommit.put(packageName, suspendingPkgsToCommit);
}
}
// Everything would be removed and package unsuspended
if (countRemoved == suspendParamsMap.size()) {
unsuspendedPackages.add(packageState.getPackageName());
unsuspendedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
suspendingPkgsToCommit.add(suspendingPackage);
countRemoved++;
}
}
});
// Everything would be removed and package unsuspended
if (countRemoved == suspendParamsMap.size()) {
unsuspendedPackages.add(packageState.getPackageName());
unsuspendedUids.add(UserHandle.getUid(userId, packageState.getAppId()));
}
}
mPm.commitPackageStateMutation(null, mutator -> {
for (int mapIndex = 0; mapIndex < pkgToSuspendingPkgsToCommit.size(); mapIndex++) {

View File

@@ -30,6 +30,7 @@ import android.util.PackageUtils;
import android.util.SparseArray;
import com.android.internal.util.CollectionUtils;
import com.android.server.pm.Computer;
import com.android.server.pm.parsing.pkg.AndroidPackage;
import com.android.server.pm.pkg.PackageStateInternal;
import com.android.server.pm.verify.domain.models.DomainVerificationInternalUserState;
@@ -38,7 +39,6 @@ import com.android.server.pm.verify.domain.models.DomainVerificationStateMap;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
@SuppressWarnings("PointlessBooleanExpression")
public class DomainVerificationDebug {
@@ -62,8 +62,7 @@ public class DomainVerificationDebug {
}
public void printState(@NonNull IndentingPrintWriter writer, @Nullable String packageName,
@Nullable @UserIdInt Integer userId,
@NonNull Function<String, PackageStateInternal> pkgSettingFunction,
@Nullable @UserIdInt Integer userId, @NonNull Computer snapshot,
@NonNull DomainVerificationStateMap<DomainVerificationPkgState> stateMap)
throws NameNotFoundException {
ArrayMap<String, Integer> reusedMap = new ArrayMap<>();
@@ -74,7 +73,7 @@ public class DomainVerificationDebug {
for (int index = 0; index < size; index++) {
DomainVerificationPkgState pkgState = stateMap.valueAt(index);
String pkgName = pkgState.getPackageName();
PackageStateInternal pkgSetting = pkgSettingFunction.apply(pkgName);
PackageStateInternal pkgSetting = snapshot.getPackageStateInternal(pkgName);
if (pkgSetting == null || pkgSetting.getPkg() == null) {
continue;
}
@@ -90,7 +89,7 @@ public class DomainVerificationDebug {
throw DomainVerificationUtils.throwPackageUnavailable(packageName);
}
PackageStateInternal pkgSetting = pkgSettingFunction.apply(packageName);
PackageStateInternal pkgSetting = snapshot.getPackageStateInternal(packageName);
if (pkgSetting == null || pkgSetting.getPkg() == null) {
throw DomainVerificationUtils.throwPackageUnavailable(packageName);
}

View File

@@ -25,7 +25,6 @@ import android.content.Intent;
import android.content.pm.IntentFilterVerificationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PackageSettingsSnapshotProvider;
import android.content.pm.ResolveInfo;
import android.content.pm.verify.domain.DomainVerificationInfo;
import android.content.pm.verify.domain.DomainVerificationManager;
@@ -37,7 +36,7 @@ import android.util.Pair;
import android.util.TypedXmlPullParser;
import android.util.TypedXmlSerializer;
import com.android.server.pm.PackageManagerService;
import com.android.server.pm.Computer;
import com.android.server.pm.PackageSetting;
import com.android.server.pm.Settings;
import com.android.server.pm.pkg.PackageStateInternal;
@@ -227,14 +226,14 @@ public interface DomainVerificationManagerInternal {
* assumed nothing has changed since the device rebooted.
* <p>
* If this is a new install, state will be restored from a previous call to {@link
* #restoreSettings(TypedXmlPullParser)}, or a new one will be generated. In either case, a
* #restoreSettings(Computer, TypedXmlPullParser)}, or a new one will be generated. In either case, a
* broadcast will be sent to the domain verification agent so it may re-run any verification
* logic for the newly associated domains.
* <p>
* This will mutate internal {@link DomainVerificationPkgState} and so will hold the internal
* lock. This should never be called from within the domain verification classes themselves.
* <p>
* This will NOT call {@link #writeSettings(TypedXmlSerializer, boolean, int)}. That must be
* This will NOT call {@link #writeSettings(Computer, TypedXmlSerializer, boolean, int)}. That must be
* handled by the caller.
*/
void addPackage(@NonNull PackageStateInternal newPkgSetting);
@@ -249,7 +248,7 @@ public interface DomainVerificationManagerInternal {
* This will mutate internal {@link DomainVerificationPkgState} and so will hold the internal
* lock. This should never be called from within the domain verification classes themselves.
* <p>
* This will NOT call {@link #writeSettings(TypedXmlSerializer, boolean, int)}. That must be
* This will NOT call {@link #writeSettings(Computer, TypedXmlSerializer, boolean, int)}. That must be
* handled by the caller.
*/
void migrateState(@NonNull PackageStateInternal oldPkgSetting,
@@ -259,24 +258,24 @@ public interface DomainVerificationManagerInternal {
* Serializes the entire internal state. This is equivalent to a full backup of the existing
* verification state. This write includes legacy state, as a sibling tag the modern state.
*
* @param snapshot
* @param includeSignatures Whether to include the package signatures in the output, mainly
* used for backing up the user settings and ensuring they're
* re-attached to the same package.
* @param userId The user to write out. Supports {@link UserHandle#USER_ALL} if all users
* should be written.
*/
void writeSettings(@NonNull TypedXmlSerializer serializer, boolean includeSignatures,
@UserIdInt int userId) throws IOException;
void writeSettings(@NonNull Computer snapshot, @NonNull TypedXmlSerializer serializer,
boolean includeSignatures, @UserIdInt int userId) throws IOException;
/**
* Read back a list of {@link DomainVerificationPkgState}s previously written by {@link
* #writeSettings(TypedXmlSerializer, boolean, int)}. Assumes that the
* #writeSettings(Computer, TypedXmlSerializer, boolean, int)}. Assumes that the
* {@link DomainVerificationPersistence#TAG_DOMAIN_VERIFICATIONS} tag has already been entered.
* <p>
* This is expected to only be used to re-attach states for packages already known to be on the
* device. If restoring from a backup, use {@link #restoreSettings(TypedXmlPullParser)}.
* device. If restoring from a backup, use {@link #restoreSettings(Computer, TypedXmlPullParser)}.
*/
void readSettings(@NonNull TypedXmlPullParser parser)
void readSettings(@NonNull Computer snapshot, @NonNull TypedXmlPullParser parser)
throws IOException, XmlPullParserException;
/**
@@ -306,7 +305,7 @@ public interface DomainVerificationManagerInternal {
/**
* Restore a list of {@link DomainVerificationPkgState}s previously written by {@link
* #writeSettings(TypedXmlSerializer, boolean, int)}. Assumes that the
* #writeSettings(Computer, TypedXmlSerializer, boolean, int)}. Assumes that the
* {@link DomainVerificationPersistence#TAG_DOMAIN_VERIFICATIONS}
* tag has already been entered.
* <p>
@@ -321,7 +320,7 @@ public interface DomainVerificationManagerInternal {
* TODO(b/170746586): Figure out how to verify that package signatures match at snapshot time
* and restore time.
*/
void restoreSettings(@NonNull TypedXmlPullParser parser)
void restoreSettings(@NonNull Computer snapshot, @NonNull TypedXmlPullParser parser)
throws IOException, XmlPullParserException;
/**
@@ -349,17 +348,14 @@ public interface DomainVerificationManagerInternal {
/**
* Print the verification state and user selection state of a package.
*
* @param snapshot
* @param packageName the package whose state to change, or all packages if none is
* specified
* @param userId the specific user to print, or null to skip printing user selection
* states, supports {@link android.os.UserHandle#USER_ALL}
* @param pkgSettingFunction the method by which to retrieve package data; if this is called
* from {@link PackageManagerService}, it is expected to pass in the
* snapshot of {@link PackageStateInternal} objects
* states, supports {@link UserHandle#USER_ALL}
*/
void printState(@NonNull IndentingPrintWriter writer, @Nullable String packageName,
@Nullable @UserIdInt Integer userId,
@NonNull Function<String, PackageStateInternal> pkgSettingFunction)
void printState(@NonNull Computer snapshot, @NonNull IndentingPrintWriter writer,
@Nullable String packageName, @Nullable @UserIdInt Integer userId)
throws NameNotFoundException;
@NonNull
@@ -406,12 +402,11 @@ public interface DomainVerificationManagerInternal {
@NonNull Set<String> domains, int state) throws NameNotFoundException;
interface Connection extends DomainVerificationEnforcer.Callback,
PackageSettingsSnapshotProvider {
interface Connection extends DomainVerificationEnforcer.Callback {
/**
* Notify that a settings change has been made and that eventually
* {@link #writeSettings(TypedXmlSerializer, boolean, int)} should be invoked by the parent.
* {@link #writeSettings(Computer, TypedXmlSerializer, boolean, int)} should be invoked by the parent.
*/
void scheduleWriteSettings();
@@ -433,5 +428,8 @@ public interface DomainVerificationManagerInternal {
@UserIdInt
int[] getAllUserIds();
@NonNull
Computer snapshot();
}
}

View File

@@ -28,6 +28,7 @@ import android.util.TypedXmlSerializer;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.pm.Computer;
import com.android.server.pm.parsing.pkg.AndroidPackage;
import com.android.server.pm.pkg.PackageStateInternal;
import com.android.server.pm.verify.domain.models.DomainVerificationInternalUserState;
@@ -101,8 +102,7 @@ class DomainVerificationSettings {
*/
public void readSettings(@NonNull TypedXmlPullParser parser,
@NonNull DomainVerificationStateMap<DomainVerificationPkgState> liveState,
@NonNull Function<String, PackageStateInternal> pkgSettingFunction)
throws IOException, XmlPullParserException {
@NonNull Computer snapshot) throws IOException, XmlPullParserException {
DomainVerificationPersistence.ReadResult result =
DomainVerificationPersistence.readFromXml(parser);
ArrayMap<String, DomainVerificationPkgState> active = result.active;
@@ -118,7 +118,7 @@ class DomainVerificationSettings {
// This branch should never be possible. Settings should be read from disk
// before any states are attached. But just in case, handle it.
if (!existingState.getId().equals(pkgState.getId())) {
mergePkgState(existingState, pkgState, pkgSettingFunction);
mergePkgState(existingState, pkgState, snapshot);
}
} else {
mPendingPkgStates.put(pkgName, pkgState);
@@ -139,8 +139,7 @@ class DomainVerificationSettings {
*/
public void restoreSettings(@NonNull TypedXmlPullParser parser,
@NonNull DomainVerificationStateMap<DomainVerificationPkgState> liveState,
@NonNull Function<String, PackageStateInternal> pkgSettingFunction)
throws IOException, XmlPullParserException {
@NonNull Computer snapshot) throws IOException, XmlPullParserException {
// TODO(b/170746586): Restoration assumes user IDs match, which is probably not the case on
// a new device.
@@ -166,7 +165,7 @@ class DomainVerificationSettings {
}
if (existingState != null) {
mergePkgState(existingState, newState, pkgSettingFunction);
mergePkgState(existingState, newState, snapshot);
} else {
// If there's no existing state, that means the new state has to be transformed
// in preparation for attaching to brand new package that may eventually be
@@ -216,9 +215,9 @@ class DomainVerificationSettings {
*/
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
public void mergePkgState(@NonNull DomainVerificationPkgState oldState,
@NonNull DomainVerificationPkgState newState,
@NonNull Function<String, PackageStateInternal> pkgSettingFunction) {
PackageStateInternal pkgSetting = pkgSettingFunction.apply(oldState.getPackageName());
@NonNull DomainVerificationPkgState newState, @NonNull Computer snapshot) {
PackageStateInternal pkgSetting =
snapshot.getPackageStateInternal(oldState.getPackageName());
AndroidPackage pkg = pkgSetting == null ? null : pkgSetting.getPkg();
Set<String> validDomains = pkg == null
? Collections.emptySet() : mCollector.collectValidAutoVerifyDomains(pkg);

View File

@@ -32,6 +32,7 @@ import android.util.ArraySet;
import android.util.IndentingPrintWriter;
import com.android.modules.utils.BasicShellCommandHandler;
import com.android.server.pm.Computer;
import java.io.PrintWriter;
import java.util.ArrayList;
@@ -39,7 +40,6 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;
public class DomainVerificationShell {
@@ -599,8 +599,8 @@ public class DomainVerificationShell {
void verifyPackages(@Nullable List<String> packageNames, boolean reVerify);
/**
* @see DomainVerificationManagerInternal#printState(IndentingPrintWriter, String, Integer,
* Function)
* @see DomainVerificationManagerInternal#printState(Computer, IndentingPrintWriter, String,
* Integer)
*/
void printState(@NonNull IndentingPrintWriter writer, @Nullable String packageName,
@Nullable @UserIdInt Integer userId) throws NameNotFoundException;

View File

@@ -265,12 +265,14 @@ class PackageManagerComponentLabelIconOverrideTest {
assertServiceInitialized() ?: return
when (params.result) {
is Result.Changed, Result.ChangedWithoutNotify -> {
assertThat(mockPendingBroadcasts.get(userId, params.pkgName) ?: emptyList<String>())
assertThat(mockPendingBroadcasts.copiedMap()?.get(userId)?.get(params.pkgName)
?: emptyList<String>())
.containsExactly(params.componentName!!.className)
.inOrder()
}
is Result.NotChanged, is Result.Exception -> {
assertThat(mockPendingBroadcasts.get(userId, params.pkgName)).isNull()
assertThat(mockPendingBroadcasts.copiedMap()?.get(userId)?.get(params.pkgName))
.isNull()
}
}.run { /*exhaust*/ }
}

View File

@@ -20,8 +20,6 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.SigningDetails
import com.android.server.pm.pkg.component.ParsedActivityImpl
import com.android.server.pm.pkg.component.ParsedIntentInfoImpl
import android.content.pm.verify.domain.DomainVerificationManager
import android.content.pm.verify.domain.DomainVerificationState
import android.os.Build
@@ -30,10 +28,12 @@ import android.util.ArraySet
import android.util.IndentingPrintWriter
import android.util.SparseArray
import androidx.test.platform.app.InstrumentationRegistry
import com.android.server.pm.Computer
import com.android.server.pm.parsing.pkg.AndroidPackage
import com.android.server.pm.pkg.PackageStateInternal
import com.android.server.pm.pkg.PackageUserStateInternal
import com.android.server.pm.test.verify.domain.DomainVerificationTestUtils.mockPackageStates
import com.android.server.pm.pkg.component.ParsedActivityImpl
import com.android.server.pm.pkg.component.ParsedIntentInfoImpl
import com.android.server.pm.verify.domain.DomainVerificationEnforcer
import com.android.server.pm.verify.domain.DomainVerificationManagerInternal
import com.android.server.pm.verify.domain.DomainVerificationService
@@ -100,11 +100,15 @@ class DomainVerificationEnforcerTest {
mockThrowOnUnmocked {
whenever(callingUid) { callingUidInt.get() }
whenever(callingUserId) { callingUserIdInt.get() }
mockPackageStates {
when (it) {
VISIBLE_PKG -> visiblePkgState
INVISIBLE_PKG -> invisiblePkgState
else -> null
whenever(snapshot()) {
mockThrowOnUnmocked {
whenever(getPackageStateInternal(anyString())) {
when (getArgument<String>(0)) {
VISIBLE_PKG -> visiblePkgState
INVISIBLE_PKG -> invisiblePkgState
else -> null
}
}
}
}
whenever(schedule(anyInt(), any()))
@@ -211,9 +215,8 @@ class DomainVerificationEnforcerTest {
printState(mock(IndentingPrintWriter::class.java), null, null)
},
service(Type.QUERENT, "printStateInternal") {
printState(mock(IndentingPrintWriter::class.java), null, null) {
mockPkgState(it, UUID.randomUUID())
}
printState(mock(Computer::class.java), mock(IndentingPrintWriter::class.java),
null, null)
},
service(Type.VERIFIER, "setStatus") {
setDomainVerificationStatus(

View File

@@ -19,9 +19,6 @@ package com.android.server.pm.test.verify.domain
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import com.android.server.pm.pkg.component.ParsedActivityImpl
import com.android.server.pm.pkg.component.ParsedIntentInfoImpl
import com.android.server.pm.pkg.PackageUserStateInternal
import android.content.pm.verify.domain.DomainOwner
import android.content.pm.verify.domain.DomainVerificationInfo
import android.content.pm.verify.domain.DomainVerificationManager
@@ -34,7 +31,9 @@ import android.util.ArraySet
import android.util.SparseArray
import com.android.server.pm.parsing.pkg.AndroidPackage
import com.android.server.pm.pkg.PackageStateInternal
import com.android.server.pm.test.verify.domain.DomainVerificationTestUtils.mockPackageStates
import com.android.server.pm.pkg.PackageUserStateInternal
import com.android.server.pm.pkg.component.ParsedActivityImpl
import com.android.server.pm.pkg.component.ParsedIntentInfoImpl
import com.android.server.pm.verify.domain.DomainVerificationManagerStub
import com.android.server.pm.verify.domain.DomainVerificationService
import com.android.server.testutils.mockThrowOnUnmocked
@@ -502,8 +501,12 @@ class DomainVerificationManagerApiTest {
whenever(callingUid) { Process.ROOT_UID }
whenever(callingUserId) { 0 }
mockPackageStates {
pkgStateFunction(it)
whenever(snapshot()) {
mockThrowOnUnmocked {
whenever(getPackageStateInternal(anyString())) {
pkgStateFunction(getArgument(0))
}
}
}
})
}

View File

@@ -20,9 +20,6 @@ import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.Signature
import android.content.pm.SigningDetails
import com.android.server.pm.pkg.component.ParsedActivityImpl
import com.android.server.pm.pkg.component.ParsedIntentInfoImpl
import com.android.server.pm.pkg.PackageUserStateInternal
import android.content.pm.verify.domain.DomainOwner
import android.content.pm.verify.domain.DomainVerificationInfo.STATE_MODIFIABLE_VERIFIED
import android.content.pm.verify.domain.DomainVerificationInfo.STATE_NO_RESPONSE
@@ -39,9 +36,12 @@ import android.os.Process
import android.util.ArraySet
import android.util.SparseArray
import android.util.Xml
import com.android.server.pm.Computer
import com.android.server.pm.parsing.pkg.AndroidPackage
import com.android.server.pm.pkg.PackageStateInternal
import com.android.server.pm.test.verify.domain.DomainVerificationTestUtils.mockPackageStates
import com.android.server.pm.pkg.PackageUserStateInternal
import com.android.server.pm.pkg.component.ParsedActivityImpl
import com.android.server.pm.pkg.component.ParsedIntentInfoImpl
import com.android.server.pm.verify.domain.DomainVerificationService
import com.android.server.testutils.mock
import com.android.server.testutils.mockThrowOnUnmocked
@@ -194,7 +194,8 @@ class DomainVerificationPackageTest {
"""
val service = makeService(pkg1, pkg2)
service.restoreSettings(Xml.resolvePullParser(xml.byteInputStream()))
val computer = mockComputer(pkg1, pkg2)
service.restoreSettings(computer, Xml.resolvePullParser(xml.byteInputStream()))
service.addPackage(pkg1)
val info = service.getInfo(pkg1.packageName)
assertThat(info.packageName).isEqualTo(pkg1.packageName)
@@ -243,7 +244,8 @@ class DomainVerificationPackageTest {
"""
val service = makeService(pkg1, pkg2)
service.restoreSettings(Xml.resolvePullParser(xml.byteInputStream()))
val computer = mockComputer(pkg1, pkg2)
service.restoreSettings(computer, Xml.resolvePullParser(xml.byteInputStream()))
service.addPackage(pkg1)
val info = service.getInfo(pkg1.packageName)
assertThat(info.packageName).isEqualTo(pkg1.packageName)
@@ -298,8 +300,9 @@ class DomainVerificationPackageTest {
""".trimIndent()
val service = makeService(pkg1, pkg2)
val computer = mockComputer(pkg1, pkg2)
xml.byteInputStream().use {
service.readSettings(Xml.resolvePullParser(it))
service.readSettings(computer, Xml.resolvePullParser(it))
}
service.addPackage(pkg1)
@@ -311,8 +314,9 @@ class DomainVerificationPackageTest {
fun addPackagePendingStripInvalidDomains() {
val xml = addPackagePendingOrRestoredWithInvalidDomains()
val service = makeService(pkg1, pkg2)
val computer = mockComputer(pkg1, pkg2)
xml.byteInputStream().use {
service.readSettings(Xml.resolvePullParser(it))
service.readSettings(computer, Xml.resolvePullParser(it))
}
service.addPackage(pkg1)
@@ -334,8 +338,9 @@ class DomainVerificationPackageTest {
fun addPackageRestoredStripInvalidDomains() {
val xml = addPackagePendingOrRestoredWithInvalidDomains()
val service = makeService(pkg1, pkg2)
val computer = mockComputer(pkg1, pkg2)
xml.byteInputStream().use {
service.restoreSettings(Xml.resolvePullParser(it))
service.restoreSettings(computer, Xml.resolvePullParser(it))
}
service.addPackage(pkg1)
@@ -686,6 +691,7 @@ class DomainVerificationPackageTest {
val pkg2 = mockPkgState(PKG_TWO, UUID_TWO, SIGNATURE_TWO,
listOf(DOMAIN_1, DOMAIN_2, DOMAIN_3))
val serviceBefore = makeService(pkg1, pkg2)
val computerBefore = mockComputer(pkg1, pkg2)
serviceBefore.addPackage(pkg1)
serviceBefore.addPackage(pkg2)
@@ -729,16 +735,17 @@ class DomainVerificationPackageTest {
assertExpectedState(serviceBefore)
val backupUser0 = ByteArrayOutputStream().use {
serviceBefore.writeSettings(Xml.resolveSerializer(it), true, 0)
serviceBefore.writeSettings(computerBefore, Xml.resolveSerializer(it), true, 0)
it.toByteArray()
}
val backupUser1 = ByteArrayOutputStream().use {
serviceBefore.writeSettings(Xml.resolveSerializer(it), true, 10)
serviceBefore.writeSettings(computerBefore, Xml.resolveSerializer(it), true, 10)
it.toByteArray()
}
val serviceAfter = makeService(pkg1, pkg2)
val computerAfter = mockComputer(pkg1, pkg2)
serviceAfter.addPackage(pkg1)
serviceAfter.addPackage(pkg2)
@@ -763,7 +770,7 @@ class DomainVerificationPackageTest {
}
ByteArrayInputStream(backupUser1).use {
serviceAfter.restoreSettings(Xml.resolvePullParser(it))
serviceAfter.restoreSettings(computerAfter, Xml.resolvePullParser(it))
}
// Assert user 1 was restored
@@ -800,7 +807,7 @@ class DomainVerificationPackageTest {
)
ByteArrayInputStream(backupUser0).use {
serviceAfter.restoreSettings(Xml.resolvePullParser(it))
serviceAfter.restoreSettings(computerAfter, Xml.resolvePullParser(it))
}
assertExpectedState(serviceAfter)
@@ -848,12 +855,20 @@ class DomainVerificationPackageTest {
whenever(callingUid) { Process.ROOT_UID }
whenever(callingUserId) { 0 }
mockPackageStates {
pkgStateFunction(it)
}
whenever(snapshot()) { mockComputer(pkgStateFunction) }
})
}
private fun mockComputer(vararg pkgStates: PackageStateInternal) =
mockComputer { pkgName -> pkgStates.find { pkgName == it.packageName } }
private fun mockComputer(pkgStateFunction: (String) -> PackageStateInternal? = { null }) =
mockThrowOnUnmocked<Computer> {
whenever(getPackageStateInternal(anyString())) {
pkgStateFunction(getArgument(0))
}
}
private fun mockPkgState(
pkgName: String,
domainSetId: UUID,

View File

@@ -29,7 +29,6 @@ import android.util.ArraySet
import android.util.SparseArray
import com.android.server.pm.parsing.pkg.AndroidPackage
import com.android.server.pm.pkg.PackageStateInternal
import com.android.server.pm.test.verify.domain.DomainVerificationTestUtils.mockPackageStates
import com.android.server.pm.verify.domain.DomainVerificationManagerInternal
import com.android.server.pm.verify.domain.DomainVerificationService
import com.android.server.pm.verify.domain.proxy.DomainVerificationProxy
@@ -245,10 +244,14 @@ class DomainVerificationSettingsMutationTest {
mockThrowOnUnmocked {
whenever(callingUid) { TEST_UID }
whenever(callingUserId) { TEST_USER_ID }
mockPackageStates {
when (it) {
TEST_PKG -> mockPkgState()
else -> null
whenever(snapshot()) {
mockThrowOnUnmocked {
whenever(getPackageStateInternal(anyString())) {
when (getArgument<String>(0)) {
TEST_PKG -> mockPkgState()
else -> null
}
}
}
}
whenever(schedule(anyInt(), any()))

View File

@@ -1,57 +0,0 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.pm.test.verify.domain
import com.android.internal.util.FunctionalUtils
import com.android.server.pm.pkg.PackageStateInternal
import com.android.server.pm.verify.domain.DomainVerificationManagerInternal
import com.android.server.testutils.whenever
import org.mockito.ArgumentMatchers.any
import java.util.function.Consumer
import java.util.function.Function
internal object DomainVerificationTestUtils {
@Suppress("UNCHECKED_CAST")
fun DomainVerificationManagerInternal.Connection.mockPackageStates(
block: (String) -> PackageStateInternal?
) {
whenever(withPackageSettingsSnapshot(any())) {
(arguments[0] as Consumer<Function<String, PackageStateInternal?>>).accept { block(it) }
}
whenever(withPackageSettingsSnapshotReturning(any())) {
(arguments[0] as FunctionalUtils.ThrowingFunction<
Function<String, PackageStateInternal?>, *>)
.apply { block(it) }
}
whenever(withPackageSettingsSnapshotThrowing<Exception>(any())) {
(arguments[0] as FunctionalUtils.ThrowingCheckedConsumer<
Function<String, PackageStateInternal?>, *>)
.accept { block(it) }
}
whenever(withPackageSettingsSnapshotThrowing2<Exception, Exception>(any())) {
(arguments[0] as FunctionalUtils.ThrowingChecked2Consumer<
Function<String, PackageStateInternal?>, *, *>)
.accept { block(it) }
}
whenever(withPackageSettingsSnapshotReturningThrowing<Any, Exception>(any())) {
(arguments[0] as FunctionalUtils.ThrowingCheckedFunction<
Function<String, PackageStateInternal?>, *, *>)
.apply { block(it) }
}
}
}

View File

@@ -31,7 +31,6 @@ import android.util.SparseArray
import com.android.server.pm.parsing.pkg.AndroidPackage
import com.android.server.pm.pkg.PackageStateInternal
import com.android.server.pm.pkg.PackageUserStateInternal
import com.android.server.pm.test.verify.domain.DomainVerificationTestUtils.mockPackageStates
import com.android.server.pm.verify.domain.DomainVerificationService
import com.android.server.testutils.mockThrowOnUnmocked
import com.android.server.testutils.whenever
@@ -85,11 +84,15 @@ class DomainVerificationUserStateOverrideTest {
// Need to provide an internal UID so some permission checks are ignored
whenever(callingUid) { Process.ROOT_UID }
whenever(callingUserId) { 0 }
mockPackageStates {
when (it) {
PKG_ONE -> pkg1
PKG_TWO -> pkg2
else -> null
whenever(snapshot()) {
mockThrowOnUnmocked {
whenever(getPackageStateInternal(anyString())) {
when (getArgument<String>(0)) {
PKG_ONE -> pkg1
PKG_TWO -> pkg2
else -> null
}
}
}
}
})

View File

@@ -167,7 +167,7 @@ class MockSystem(withSession: (StaticMockitoSessionBuilder) -> Unit = {}) {
}
whenever(mocks.settings.packagesLocked).thenReturn(mSettingsMap)
whenever(mocks.settings.getPackageLPr(anyString())) { mSettingsMap[getArgument<Any>(0)] }
whenever(mocks.settings.readLPw(nullable())) {
whenever(mocks.settings.readLPw(any(), nullable())) {
mSettingsMap.putAll(mPreExistingSettings)
!mPreExistingSettings.isEmpty()
}

View File

@@ -120,17 +120,16 @@ class SharedLibrariesImplTest {
whenever(mStorageManager.findPathForUuid(nullable())).thenReturn(mFile)
doAnswer { it.arguments[0] }.`when`(mPms).resolveInternalPackageName(any(), any())
doAnswer {
it.getArgument<FunctionalUtils.ThrowingConsumer<Computer>>(0).acceptOrThrow(
mockThrowOnUnmocked {
whenever(sharedLibraries) { mSharedLibrariesImpl.sharedLibraries }
whenever(resolveInternalPackageName(anyString(), anyLong())) {
mPms.resolveInternalPackageName(getArgument(0), getArgument(1))
}
whenever(getPackageStateInternal(anyString())) {
mPms.getPackageStateInternal(getArgument(0))
}
})
}.`when`(mPms).executeWithConsistentComputer(any())
mockThrowOnUnmocked<Computer> {
whenever(sharedLibraries) { mSharedLibrariesImpl.sharedLibraries }
whenever(resolveInternalPackageName(anyString(), anyLong())) {
mPms.resolveInternalPackageName(getArgument(0), getArgument(1))
}
whenever(getPackageStateInternal(anyString())) {
mPms.getPackageStateInternal(getArgument(0))
}
}
}.`when`(mPms).snapshotComputer()
whenever(mDeletePackageHelper.deletePackageX(any(), any(), any(), any(), any()))
.thenReturn(PackageManager.DELETE_SUCCEEDED)
whenever(mRule.mocks().injector.compatibility).thenReturn(mPlatformCompat)
@@ -206,7 +205,8 @@ class SharedLibrariesImplTest {
@Test
fun pruneUnusedStaticSharedLibraries() {
mSharedLibrariesImpl.pruneUnusedStaticSharedLibraries(Long.MAX_VALUE, 0)
mSharedLibrariesImpl.pruneUnusedStaticSharedLibraries(mPms.snapshotComputer(),
Long.MAX_VALUE, 0)
verify(mDeletePackageHelper)
.deletePackageX(eq(STATIC_LIB_PACKAGE_NAME), any(), any(), any(), any())

View File

@@ -123,8 +123,8 @@ class SuspendPackageHelperTest {
@Test
fun setPackagesSuspended() {
val targetPackages = arrayOf(TEST_PACKAGE_1, TEST_PACKAGE_2)
val failedNames = suspendPackageHelper.setPackagesSuspended(targetPackages,
true /* suspended */, null /* appExtras */, null /* launcherExtras */,
val failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
targetPackages, true /* suspended */, null /* appExtras */, null /* launcherExtras */,
null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
testHandler.flush()
@@ -144,14 +144,15 @@ class SuspendPackageHelperTest {
@Test
fun setPackagesSuspended_emptyPackageName() {
var failedNames = suspendPackageHelper.setPackagesSuspended(null /* packageNames */,
true /* suspended */, null /* appExtras */, null /* launcherExtras */,
null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
var failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
null /* packageNames */, true /* suspended */, null /* appExtras */,
null /* launcherExtras */, null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID,
deviceOwnerUid)
assertThat(failedNames).isNull()
failedNames = suspendPackageHelper.setPackagesSuspended(arrayOfNulls(0),
true /* suspended */, null /* appExtras */, null /* launcherExtras */,
failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
arrayOfNulls(0), true /* suspended */, null /* appExtras */, null /* launcherExtras */,
null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
assertThat(failedNames).isEmpty()
@@ -159,9 +160,10 @@ class SuspendPackageHelperTest {
@Test
fun setPackagesSuspended_callerIsNotAllowed() {
val failedNames = suspendPackageHelper.setPackagesSuspended(arrayOf(TEST_PACKAGE_2),
true /* suspended */, null /* appExtras */, null /* launcherExtras */,
null /* dialogInfo */, TEST_PACKAGE_1, TEST_USER_ID, Binder.getCallingUid())
val failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
arrayOf(TEST_PACKAGE_2), true /* suspended */, null /* appExtras */,
null /* launcherExtras */, null /* dialogInfo */, TEST_PACKAGE_1, TEST_USER_ID,
Binder.getCallingUid())
assertThat(failedNames).asList().hasSize(1)
assertThat(failedNames).asList().contains(TEST_PACKAGE_2)
@@ -169,9 +171,10 @@ class SuspendPackageHelperTest {
@Test
fun setPackagesSuspended_callerSuspendItself() {
val failedNames = suspendPackageHelper.setPackagesSuspended(arrayOf(DEVICE_OWNER_PACKAGE),
true /* suspended */, null /* appExtras */, null /* launcherExtras */,
null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
val failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
arrayOf(DEVICE_OWNER_PACKAGE), true /* suspended */, null /* appExtras */,
null /* launcherExtras */, null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID,
deviceOwnerUid)
assertThat(failedNames).asList().hasSize(1)
assertThat(failedNames).asList().contains(DEVICE_OWNER_PACKAGE)
@@ -179,9 +182,10 @@ class SuspendPackageHelperTest {
@Test
fun setPackagesSuspended_nonexistentPackage() {
val failedNames = suspendPackageHelper.setPackagesSuspended(arrayOf(NONEXISTENT_PACKAGE),
true /* suspended */, null /* appExtras */, null /* launcherExtras */,
null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
val failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
arrayOf(NONEXISTENT_PACKAGE), true /* suspended */, null /* appExtras */,
null /* launcherExtras */, null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID,
deviceOwnerUid)
assertThat(failedNames).asList().hasSize(1)
assertThat(failedNames).asList().contains(NONEXISTENT_PACKAGE)
@@ -191,8 +195,8 @@ class SuspendPackageHelperTest {
fun setPackagesSuspended_knownPackages() {
val knownPackages = arrayOf(DEVICE_ADMIN_PACKAGE, DEFAULT_HOME_PACKAGE, DIALER_PACKAGE,
INSTALLER_PACKAGE, UNINSTALLER_PACKAGE, VERIFIER_PACKAGE, PERMISSION_CONTROLLER_PACKAGE)
val failedNames = suspendPackageHelper.setPackagesSuspended(knownPackages,
true /* suspended */, null /* appExtras */, null /* launcherExtras */,
val failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
knownPackages, true /* suspended */, null /* appExtras */, null /* launcherExtras */,
null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)!!
assertThat(failedNames.size).isEqualTo(knownPackages.size)
@@ -204,13 +208,13 @@ class SuspendPackageHelperTest {
@Test
fun setPackagesUnsuspended() {
val targetPackages = arrayOf(TEST_PACKAGE_1, TEST_PACKAGE_2)
var failedNames = suspendPackageHelper.setPackagesSuspended(targetPackages,
true /* suspended */, null /* appExtras */, null /* launcherExtras */,
var failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
targetPackages, true /* suspended */, null /* appExtras */, null /* launcherExtras */,
null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
testHandler.flush()
assertThat(failedNames).isEmpty()
failedNames = suspendPackageHelper.setPackagesSuspended(targetPackages,
false /* suspended */, null /* appExtras */, null /* launcherExtras */,
failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
targetPackages, false /* suspended */, null /* appExtras */, null /* launcherExtras */,
null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
testHandler.flush()
@@ -235,7 +239,7 @@ class SuspendPackageHelperTest {
val suspendables = arrayOf(TEST_PACKAGE_1, TEST_PACKAGE_2)
val unsuspendables = arrayOf(DEVICE_ADMIN_PACKAGE, DEFAULT_HOME_PACKAGE, DIALER_PACKAGE,
INSTALLER_PACKAGE, UNINSTALLER_PACKAGE, VERIFIER_PACKAGE, PERMISSION_CONTROLLER_PACKAGE)
val results = suspendPackageHelper.getUnsuspendablePackagesForUser(
val results = suspendPackageHelper.getUnsuspendablePackagesForUser(pms.snapshotComputer(),
suspendables + unsuspendables, TEST_USER_ID, deviceOwnerUid)
assertThat(results.size).isEqualTo(unsuspendables.size)
@@ -247,7 +251,7 @@ class SuspendPackageHelperTest {
@Test
fun getUnsuspendablePackagesForUser_callerIsNotAllowed() {
val suspendables = arrayOf(TEST_PACKAGE_1, TEST_PACKAGE_2)
val results = suspendPackageHelper.getUnsuspendablePackagesForUser(
val results = suspendPackageHelper.getUnsuspendablePackagesForUser(pms.snapshotComputer(),
suspendables, TEST_USER_ID, Binder.getCallingUid())
assertThat(results.size).isEqualTo(suspendables.size)
@@ -260,8 +264,8 @@ class SuspendPackageHelperTest {
fun getSuspendedPackageAppExtras() {
val appExtras = PersistableBundle()
appExtras.putString(TEST_PACKAGE_1, TEST_PACKAGE_1)
var failedNames = suspendPackageHelper.setPackagesSuspended(arrayOf(TEST_PACKAGE_1),
true /* suspended */, appExtras, null /* launcherExtras */,
var failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
arrayOf(TEST_PACKAGE_1), true /* suspended */, appExtras, null /* launcherExtras */,
null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
testHandler.flush()
assertThat(failedNames).isEmpty()
@@ -277,8 +281,8 @@ class SuspendPackageHelperTest {
val appExtras = PersistableBundle()
appExtras.putString(TEST_PACKAGE_1, TEST_PACKAGE_2)
val targetPackages = arrayOf(TEST_PACKAGE_1, TEST_PACKAGE_2)
var failedNames = suspendPackageHelper.setPackagesSuspended(targetPackages,
true /* suspended */, appExtras, null /* launcherExtras */,
var failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
targetPackages, true /* suspended */, appExtras, null /* launcherExtras */,
null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
testHandler.flush()
assertThat(failedNames).isEmpty()
@@ -291,8 +295,9 @@ class SuspendPackageHelperTest {
assertThat(suspendPackageHelper.getSuspendedPackageAppExtras(
TEST_PACKAGE_2, TEST_USER_ID, deviceOwnerUid)).isNotNull()
suspendPackageHelper.removeSuspensionsBySuspendingPackage(targetPackages,
{ suspendingPackage -> suspendingPackage == DEVICE_OWNER_PACKAGE }, TEST_USER_ID)
suspendPackageHelper.removeSuspensionsBySuspendingPackage(pms.snapshotComputer(),
targetPackages, { suspendingPackage -> suspendingPackage == DEVICE_OWNER_PACKAGE },
TEST_USER_ID)
testHandler.flush()
verify(pms, times(2)).scheduleWritePackageRestrictions(eq(TEST_USER_ID))
@@ -320,8 +325,8 @@ class SuspendPackageHelperTest {
fun getSuspendedPackageLauncherExtras() {
val launcherExtras = PersistableBundle()
launcherExtras.putString(TEST_PACKAGE_2, TEST_PACKAGE_2)
var failedNames = suspendPackageHelper.setPackagesSuspended(arrayOf(TEST_PACKAGE_2),
true /* suspended */, null /* appExtras */, launcherExtras,
var failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
arrayOf(TEST_PACKAGE_2), true /* suspended */, null /* appExtras */, launcherExtras,
null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
testHandler.flush()
assertThat(failedNames).isEmpty()
@@ -334,9 +339,10 @@ class SuspendPackageHelperTest {
@Test
fun isPackageSuspended() {
var failedNames = suspendPackageHelper.setPackagesSuspended(arrayOf(TEST_PACKAGE_1),
true /* suspended */, null /* appExtras */, null /* launcherExtras */,
null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
var failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
arrayOf(TEST_PACKAGE_1), true /* suspended */, null /* appExtras */,
null /* launcherExtras */, null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID,
deviceOwnerUid)
testHandler.flush()
assertThat(failedNames).isEmpty()
@@ -348,8 +354,8 @@ class SuspendPackageHelperTest {
fun getSuspendingPackage() {
val launcherExtras = PersistableBundle()
launcherExtras.putString(TEST_PACKAGE_2, TEST_PACKAGE_2)
var failedNames = suspendPackageHelper.setPackagesSuspended(arrayOf(TEST_PACKAGE_2),
true /* suspended */, null /* appExtras */, launcherExtras,
var failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
arrayOf(TEST_PACKAGE_2), true /* suspended */, null /* appExtras */, launcherExtras,
null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
testHandler.flush()
assertThat(failedNames).isEmpty()
@@ -362,9 +368,10 @@ class SuspendPackageHelperTest {
fun getSuspendedDialogInfo() {
val dialogInfo = SuspendDialogInfo.Builder()
.setTitle(TEST_PACKAGE_1).build()
var failedNames = suspendPackageHelper.setPackagesSuspended(arrayOf(TEST_PACKAGE_1),
true /* suspended */, null /* appExtras */, null /* launcherExtras */,
dialogInfo, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
var failedNames = suspendPackageHelper.setPackagesSuspended(pms.snapshotComputer(),
arrayOf(TEST_PACKAGE_1), true /* suspended */, null /* appExtras */,
null /* launcherExtras */, dialogInfo, DEVICE_OWNER_PACKAGE, TEST_USER_ID,
deviceOwnerUid)
testHandler.flush()
assertThat(failedNames).isEmpty()

View File

@@ -35,6 +35,7 @@ import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.annotation.NonNull;
@@ -109,6 +110,8 @@ public class PackageManagerSettingsTests {
LegacyPermissionDataProvider mPermissionDataProvider;
@Mock
DomainVerificationManagerInternal mDomainVerificationManager;
@Mock
Computer computer;
final ArrayMap<String, Long> mOrigFirstInstallTimes = new ArrayMap<>();
@@ -132,7 +135,7 @@ public class PackageManagerSettingsTests {
/* write out files and read */
writeOldFiles();
Settings settings = makeSettings();
assertThat(settings.readLPw(createFakeUsers()), is(true));
assertThat(settings.readLPw(computer, createFakeUsers()), is(true));
verifyKeySetMetaData(settings);
}
@@ -143,11 +146,11 @@ public class PackageManagerSettingsTests {
// write out files and read
writeOldFiles();
Settings settings = makeSettings();
assertThat(settings.readLPw(createFakeUsers()), is(true));
assertThat(settings.readLPw(computer, createFakeUsers()), is(true));
// write out, read back in and verify the same
settings.writeLPr();
assertThat(settings.readLPw(createFakeUsers()), is(true));
settings.writeLPr(computer);
assertThat(settings.readLPw(computer, createFakeUsers()), is(true));
verifyKeySetMetaData(settings);
}
@@ -156,7 +159,7 @@ public class PackageManagerSettingsTests {
// Write delegateshellthe package files and make sure they're parsed properly the first time
writeOldFiles();
Settings settings = makeSettings();
assertThat(settings.readLPw(createFakeUsers()), is(true));
assertThat(settings.readLPw(computer, createFakeUsers()), is(true));
assertThat(settings.getPackageLPr(PACKAGE_NAME_3), is(notNullValue()));
assertThat(settings.getPackageLPr(PACKAGE_NAME_1), is(notNullValue()));
@@ -175,12 +178,12 @@ public class PackageManagerSettingsTests {
// Write the package files and make sure they're parsed properly the first time
writeOldFiles();
Settings settings = makeSettings();
assertThat(settings.readLPw(createFakeUsers()), is(true));
settings.writeLPr();
assertThat(settings.readLPw(computer, createFakeUsers()), is(true));
settings.writeLPr(computer);
// Create Settings again to make it read from the new files
settings = makeSettings();
assertThat(settings.readLPw(createFakeUsers()), is(true));
assertThat(settings.readLPw(computer, createFakeUsers()), is(true));
PackageSetting ps = settings.getPackageLPr(PACKAGE_NAME_2);
assertThat(ps.getEnabled(0), is(COMPONENT_ENABLED_STATE_DISABLED_USER));
@@ -469,12 +472,12 @@ public class PackageManagerSettingsTests {
ps2.setUsesStaticLibrariesVersions(new long[] { 34 });
settingsUnderTest.mPackages.put(PACKAGE_NAME_2, ps2);
settingsUnderTest.writeLPr();
settingsUnderTest.writeLPr(computer);
settingsUnderTest.mPackages.clear();
settingsUnderTest.mDisabledSysPackages.clear();
assertThat(settingsUnderTest.readLPw(createFakeUsers()), is(true));
assertThat(settingsUnderTest.readLPw(computer, createFakeUsers()), is(true));
PackageSetting readPs1 = settingsUnderTest.getPackageLPr(PACKAGE_NAME_1);
PackageSetting readPs2 = settingsUnderTest.getPackageLPr(PACKAGE_NAME_2);
@@ -534,12 +537,12 @@ public class PackageManagerSettingsTests {
ps2.setUsesSdkLibrariesVersionsMajor(new long[] { 34 });
settingsUnderTest.mPackages.put(PACKAGE_NAME_2, ps2);
settingsUnderTest.writeLPr();
settingsUnderTest.writeLPr(computer);
settingsUnderTest.mPackages.clear();
settingsUnderTest.mDisabledSysPackages.clear();
assertThat(settingsUnderTest.readLPw(createFakeUsers()), is(true));
assertThat(settingsUnderTest.readLPw(computer, createFakeUsers()), is(true));
PackageSetting readPs1 = settingsUnderTest.getPackageLPr(PACKAGE_NAME_1);
PackageSetting readPs2 = settingsUnderTest.getPackageLPr(PACKAGE_NAME_2);
@@ -587,7 +590,7 @@ public class PackageManagerSettingsTests {
Settings settings = makeSettings();
final WatchableTester watcher = new WatchableTester(settings, "testEnableDisable");
watcher.register();
assertThat(settings.readLPw(createFakeUsers()), is(true));
assertThat(settings.readLPw(computer, createFakeUsers()), is(true));
watcher.verifyChangeReported("readLPw");
// Enable/Disable a package