Having an unified method for separated broadcasting

Both DistractingPackageHelper and SuspendPackageHelper broadcast with
package names/uids separately. Having an unified method to calculate
the list to reduce code duplication.

Bug: 227270414
Test: atest AppEnumerationTests
Test: atest BroadcastHelperTest
Test: atest CtsSuspendAppsTestCases
Test: atest DistractingPackageHelperTest
Test: atest SuspendPackageHelperTest
Change-Id: Ie900be9e8f8a306f06e9856a25965c7a9e08b684
This commit is contained in:
Jackal Guo
2022-04-27 14:49:42 +08:00
parent 65313bc750
commit 5f693a3721
8 changed files with 316 additions and 116 deletions

View File

@@ -28,6 +28,7 @@ import android.Manifest;
import android.annotation.AppIdInt; import android.annotation.AppIdInt;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.app.ActivityManagerInternal; import android.app.ActivityManagerInternal;
import android.app.BroadcastOptions; import android.app.BroadcastOptions;
@@ -40,8 +41,10 @@ import android.content.pm.PackageInstaller;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.PowerExemptionManager; import android.os.PowerExemptionManager;
import android.os.Process;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.UserHandle; import android.os.UserHandle;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.Slog; import android.util.Slog;
import android.util.SparseArray; import android.util.SparseArray;
@@ -49,6 +52,8 @@ import android.util.SparseArray;
import com.android.internal.util.ArrayUtils; import com.android.internal.util.ArrayUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/** /**
* Helper class to send broadcasts for various situations. * Helper class to send broadcasts for various situations.
@@ -327,4 +332,45 @@ public final class BroadcastHelper {
sendPackageBroadcast(Intent.ACTION_PACKAGE_FIRST_LAUNCH, pkgName, null, 0, sendPackageBroadcast(Intent.ACTION_PACKAGE_FIRST_LAUNCH, pkgName, null, 0,
installerPkg, null, userIds, instantUserIds, null /* broadcastAllowList */, null); installerPkg, null, userIds, instantUserIds, null /* broadcastAllowList */, null);
} }
/**
* Get broadcast params list based on the given package and uid list. The broadcast params are
* used to send broadcast separately if the given packages have different visibility allow list.
*
* @param pkgList The names of packages which have changes.
* @param uidList The uids of packages which have changes.
* @param userId The user where packages reside.
* @return The list of {@link BroadcastParams} object.
*/
public List<BroadcastParams> getBroadcastParams(@NonNull Computer snapshot,
@NonNull String[] pkgList, @NonNull int[] uidList, @UserIdInt int userId) {
final List<BroadcastParams> lists = new ArrayList<>(pkgList.length);
// Get allow lists for the pkg in the pkgList. Merge into the existed pkgs and uids if
// allow lists are the same.
for (int i = 0; i < pkgList.length; i++) {
final String pkgName = pkgList[i];
final int uid = uidList[i];
if (TextUtils.isEmpty(pkgName) || Process.INVALID_UID == uid) {
continue;
}
int[] allowList = snapshot.getVisibilityAllowList(pkgName, userId);
if (allowList == null) {
allowList = new int[0];
}
boolean merged = false;
for (int j = 0; j < lists.size(); j++) {
final BroadcastParams list = lists.get(j);
if (Arrays.equals(list.getAllowList().get(userId), allowList)) {
list.addPackage(pkgName, uid);
merged = true;
break;
}
}
if (!merged) {
lists.add(new BroadcastParams(pkgName, uid, allowList, userId));
}
}
return lists;
}
} }

View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2022 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;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.util.IntArray;
import android.util.SparseArray;
import com.android.internal.util.DataClass;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* A helper class that contains information about package names and uids that share the same allow
* list for sending broadcasts. Used by various package helpers.
*/
@DataClass(genConstructor = false, genConstDefs = false)
final class BroadcastParams {
private final @NonNull List<String> mPackageNames;
private final @NonNull IntArray mUids;
private final @NonNull SparseArray<int[]> mAllowList;
BroadcastParams(@NonNull String packageName, @IntRange(from = 0) int uid,
@NonNull int[] allowList, @UserIdInt int userId) {
mPackageNames = new ArrayList<>(Arrays.asList(packageName));
mUids = IntArray.wrap(new int[]{uid});
mAllowList = new SparseArray<>(1);
mAllowList.put(userId, allowList);
}
public void addPackage(@NonNull String packageName, @IntRange(from = 0) int uid) {
mPackageNames.add(packageName);
mUids.add(uid);
}
// Code below generated by codegen v1.0.23.
//
// DO NOT MODIFY!
// CHECKSTYLE:OFF Generated code
//
// To regenerate run:
// $ codegen $ANDROID_BUILD_TOP/frameworks/base/services/core/java/com/android/server/pm/BroadcastParams.java
//
// To exclude the generated code from IntelliJ auto-formatting enable (one-time):
// Settings > Editor > Code Style > Formatter Control
//@formatter:off
@DataClass.Generated.Member
public @NonNull List<String> getPackageNames() {
return mPackageNames;
}
@DataClass.Generated.Member
public @NonNull IntArray getUids() {
return mUids;
}
@DataClass.Generated.Member
public @NonNull SparseArray<int[]> getAllowList() {
return mAllowList;
}
@DataClass.Generated(
time = 1651554793681L,
codegenVersion = "1.0.23",
sourceFile = "frameworks/base/services/core/java/com/android/server/pm/BroadcastParams.java",
inputSignatures = "private final @android.annotation.NonNull java.util.List<java.lang.String> mPackageNames\nprivate final @android.annotation.NonNull android.util.IntArray mUids\nprivate final @android.annotation.NonNull android.util.SparseArray<int[]> mAllowList\npublic void addPackage(java.lang.String,int)\nclass BroadcastParams extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genConstructor=false, genConstDefs=false)")
@Deprecated
private void __metadata() {}
//@formatter:on
// End of generated code
}

View File

@@ -17,7 +17,6 @@
package com.android.server.pm; package com.android.server.pm;
import static android.content.pm.PackageManager.RESTRICTION_NONE; import static android.content.pm.PackageManager.RESTRICTION_NONE;
import static android.os.Process.SYSTEM_UID;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.content.Intent; import android.content.Intent;
@@ -34,7 +33,6 @@ import com.android.internal.util.ArrayUtils;
import com.android.server.pm.pkg.PackageStateInternal; import com.android.server.pm.pkg.PackageStateInternal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
@@ -186,50 +184,22 @@ public final class DistractingPackageHelper {
*/ */
void sendDistractingPackagesChanged(@NonNull Computer snapshot, @NonNull String[] pkgList, void sendDistractingPackagesChanged(@NonNull Computer snapshot, @NonNull String[] pkgList,
int[] uidList, int userId, int distractionFlags) { int[] uidList, int userId, int distractionFlags) {
final List<List<String>> pkgsToSend = new ArrayList(pkgList.length); final List<BroadcastParams> lists = mBroadcastHelper.getBroadcastParams(
final List<IntArray> uidsToSend = new ArrayList(pkgList.length); snapshot, pkgList, uidList, userId);
final List<SparseArray<int[]>> allowListsToSend = new ArrayList(pkgList.length);
final int[] userIds = new int[] {userId};
// Get allow lists for the pkg in the pkgList. Merge into the existed pkgs and uids if
// allow lists are the same.
for (int i = 0; i < pkgList.length; i++) {
final String pkgName = pkgList[i];
final int uid = uidList[i];
SparseArray<int[]> allowList = mInjector.getAppsFilter().getVisibilityAllowList(
snapshot, snapshot.getPackageStateInternal(pkgName, SYSTEM_UID),
userIds, snapshot.getPackageStates());
if (allowList == null) {
allowList = new SparseArray<>(0);
}
boolean merged = false;
for (int j = 0; j < allowListsToSend.size(); j++) {
if (Arrays.equals(allowListsToSend.get(j).get(userId), allowList.get(userId))) {
pkgsToSend.get(j).add(pkgName);
uidsToSend.get(j).add(uid);
merged = true;
break;
}
}
if (!merged) {
pkgsToSend.add(new ArrayList<>(Arrays.asList(pkgName)));
uidsToSend.add(IntArray.wrap(new int[] {uid}));
allowListsToSend.add(allowList);
}
}
final Handler handler = mInjector.getHandler(); final Handler handler = mInjector.getHandler();
for (int i = 0; i < pkgsToSend.size(); i++) { for (int i = 0; i < lists.size(); i++) {
final Bundle extras = new Bundle(3); final Bundle extras = new Bundle(3);
final BroadcastParams list = lists.get(i);
extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST, extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST,
pkgsToSend.get(i).toArray(new String[pkgsToSend.get(i).size()])); list.getPackageNames().toArray(new String[0]));
extras.putIntArray(Intent.EXTRA_CHANGED_UID_LIST, uidsToSend.get(i).toArray()); extras.putIntArray(Intent.EXTRA_CHANGED_UID_LIST, list.getUids().toArray());
extras.putInt(Intent.EXTRA_DISTRACTION_RESTRICTIONS, distractionFlags); extras.putInt(Intent.EXTRA_DISTRACTION_RESTRICTIONS, distractionFlags);
final SparseArray<int[]> allowList = allowListsToSend.get(i).size() == 0 final SparseArray<int[]> allowList = list.getAllowList().size() == 0
? null : allowListsToSend.get(i); ? null : list.getAllowList();
handler.post(() -> mBroadcastHelper.sendPackageBroadcast( handler.post(() -> mBroadcastHelper.sendPackageBroadcast(
Intent.ACTION_DISTRACTING_PACKAGES_CHANGED, null /* pkg */, Intent.ACTION_DISTRACTING_PACKAGES_CHANGED, null /* pkg */,
extras, Intent.FLAG_RECEIVER_REGISTERED_ONLY, null /* targetPkg */, extras, Intent.FLAG_RECEIVER_REGISTERED_ONLY, null /* targetPkg */,
null /* finishedReceiver */, userIds, null /* instantUserIds */, null /* finishedReceiver */, new int[]{userId}, null /* instantUserIds */,
allowList, null /* bOptions */)); allowList, null /* bOptions */));
} }
} }

View File

@@ -51,7 +51,6 @@ import com.android.server.pm.pkg.mutate.PackageUserStateWrite;
import com.android.server.utils.WatchedArrayMap; import com.android.server.utils.WatchedArrayMap;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -588,48 +587,20 @@ public final class SuspendPackageHelper {
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE) @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
void sendPackagesSuspendedForUser(@NonNull Computer snapshot, @NonNull String intent, void sendPackagesSuspendedForUser(@NonNull Computer snapshot, @NonNull String intent,
@NonNull String[] pkgList, @NonNull int[] uidList, int userId) { @NonNull String[] pkgList, @NonNull int[] uidList, int userId) {
final List<List<String>> pkgsToSend = new ArrayList(pkgList.length); final List<BroadcastParams> lists = mBroadcastHelper.getBroadcastParams(
final List<IntArray> uidsToSend = new ArrayList(pkgList.length); snapshot, pkgList, uidList, userId);
final List<SparseArray<int[]>> allowListsToSend = new ArrayList(pkgList.length);
final int[] userIds = new int[] {userId};
// Get allow lists for the pkg in the pkgList. Merge into the existed pkgs and uids if
// allow lists are the same.
for (int i = 0; i < pkgList.length; i++) {
final String pkgName = pkgList[i];
final int uid = uidList[i];
SparseArray<int[]> allowList = mInjector.getAppsFilter().getVisibilityAllowList(
snapshot, snapshot.getPackageStateInternal(pkgName, SYSTEM_UID),
userIds, snapshot.getPackageStates());
if (allowList == null) {
allowList = new SparseArray<>(0);
}
boolean merged = false;
for (int j = 0; j < allowListsToSend.size(); j++) {
if (Arrays.equals(allowListsToSend.get(j).get(userId), allowList.get(userId))) {
pkgsToSend.get(j).add(pkgName);
uidsToSend.get(j).add(uid);
merged = true;
break;
}
}
if (!merged) {
pkgsToSend.add(new ArrayList<>(Arrays.asList(pkgName)));
uidsToSend.add(IntArray.wrap(new int[] {uid}));
allowListsToSend.add(allowList);
}
}
final Handler handler = mInjector.getHandler(); final Handler handler = mInjector.getHandler();
for (int i = 0; i < pkgsToSend.size(); i++) { for (int i = 0; i < lists.size(); i++) {
final Bundle extras = new Bundle(3); final Bundle extras = new Bundle(3);
final BroadcastParams list = lists.get(i);
extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST, extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST,
pkgsToSend.get(i).toArray(new String[pkgsToSend.get(i).size()])); list.getPackageNames().toArray(new String[0]));
extras.putIntArray(Intent.EXTRA_CHANGED_UID_LIST, uidsToSend.get(i).toArray()); extras.putIntArray(Intent.EXTRA_CHANGED_UID_LIST, list.getUids().toArray());
final SparseArray<int[]> allowList = allowListsToSend.get(i).size() == 0 final SparseArray<int[]> allowList = list.getAllowList().size() == 0
? null : allowListsToSend.get(i); ? null : list.getAllowList();
handler.post(() -> mBroadcastHelper.sendPackageBroadcast(intent, null /* pkg */, handler.post(() -> mBroadcastHelper.sendPackageBroadcast(intent, null /* pkg */,
extras, Intent.FLAG_RECEIVER_REGISTERED_ONLY, null /* targetPkg */, extras, Intent.FLAG_RECEIVER_REGISTERED_ONLY, null /* targetPkg */,
null /* finishedReceiver */, userIds, null /* instantUserIds */, null /* finishedReceiver */, new int[]{userId}, null /* instantUserIds */,
allowList, null /* bOptions */)); allowList, null /* bOptions */));
} }
} }

View File

@@ -0,0 +1,117 @@
/*
* Copyright (C) 2022 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
import com.android.server.testutils.whenever
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.Mock
import org.mockito.MockitoAnnotations
@RunWith(JUnit4::class)
class BroadcastHelperTest {
companion object {
const val TEST_PACKAGE_1 = "com.android.test.package1"
const val TEST_PACKAGE_2 = "com.android.test.package2"
const val TEST_UID_1 = 10100
const val TEST_UID_2 = 10101
const val TEST_USER_ID = 0
}
lateinit var broadcastHelper: BroadcastHelper
lateinit var packagesToChange: Array<String>
lateinit var uidsToChange: IntArray
@Mock
lateinit var snapshot: Computer
@Rule
@JvmField
val rule = MockSystemRule()
@Before
open fun setup() {
MockitoAnnotations.initMocks(this)
rule.system().stageNominalSystemState()
broadcastHelper = BroadcastHelper(rule.mocks().injector)
packagesToChange = arrayOf(TEST_PACKAGE_1, TEST_PACKAGE_2)
uidsToChange = intArrayOf(TEST_UID_1, TEST_UID_2)
}
@Test
fun getBroadcastParams_withSameVisibilityAllowList_shouldGroup() {
val allowList = intArrayOf(10001, 10002, 10003)
mockVisibilityAllowList(TEST_PACKAGE_1, allowList)
mockVisibilityAllowList(TEST_PACKAGE_2, allowList)
val broadcastParams: List<BroadcastParams> = broadcastHelper.getBroadcastParams(
snapshot, packagesToChange, uidsToChange, TEST_USER_ID)
assertThat(broadcastParams).hasSize(1)
assertThat(broadcastParams[0].packageNames).containsExactlyElementsIn(
packagesToChange.toCollection(ArrayList()))
assertThat(broadcastParams[0].uids.toArray()).asList().containsExactlyElementsIn(
uidsToChange.toCollection(ArrayList()))
}
@Test
fun getBroadcastParams_withDifferentVisibilityAllowList_shouldNotGroup() {
val allowList1 = intArrayOf(10001, 10002, 10003)
val allowList2 = intArrayOf(10001, 10002, 10007)
mockVisibilityAllowList(TEST_PACKAGE_1, allowList1)
mockVisibilityAllowList(TEST_PACKAGE_2, allowList2)
val broadcastParams: List<BroadcastParams> = broadcastHelper.getBroadcastParams(
snapshot, packagesToChange, uidsToChange, TEST_USER_ID)
assertThat(broadcastParams).hasSize(2)
broadcastParams.forEachIndexed { i, params ->
val changedPackages = params.packageNames
val changedUids = params.uids
assertThat(changedPackages[0]).isEqualTo(packagesToChange[i])
assertThat(changedUids[0]).isEqualTo(uidsToChange[i])
}
}
@Test
fun getBroadcastParams_withNullVisibilityAllowList_shouldNotGroup() {
val allowList = intArrayOf(10001, 10002, 10003)
mockVisibilityAllowList(TEST_PACKAGE_1, allowList)
mockVisibilityAllowList(TEST_PACKAGE_2, null)
val broadcastParams: List<BroadcastParams> = broadcastHelper.getBroadcastParams(
snapshot, packagesToChange, uidsToChange, TEST_USER_ID)
assertThat(broadcastParams).hasSize(2)
broadcastParams.forEachIndexed { i, params ->
val changedPackages = params.packageNames
val changedUids = params.uids
assertThat(changedPackages[0]).isEqualTo(packagesToChange[i])
assertThat(changedUids[0]).isEqualTo(uidsToChange[i])
}
}
private fun mockVisibilityAllowList(pkgName: String, list: IntArray?) {
whenever(snapshot.getVisibilityAllowList(pkgName, TEST_USER_ID))
.thenReturn(list ?: IntArray(0))
}
}

View File

@@ -196,9 +196,6 @@ class DistractingPackageHelperTest : PackageHelperTestBase() {
@Test @Test
fun sendDistractingPackagesChanged_withSameVisibilityAllowList() { fun sendDistractingPackagesChanged_withSameVisibilityAllowList() {
mockAllowList(packageSetting1, allowList(10001, 10002, 10003))
mockAllowList(packageSetting2, allowList(10001, 10002, 10003))
distractingPackageHelper.sendDistractingPackagesChanged(pms.snapshotComputer(), distractingPackageHelper.sendDistractingPackagesChanged(pms.snapshotComputer(),
packagesToChange, uidsToChange, TEST_USER_ID, packagesToChange, uidsToChange, TEST_USER_ID,
PackageManager.RESTRICTION_HIDE_NOTIFICATIONS) PackageManager.RESTRICTION_HIDE_NOTIFICATIONS)
@@ -216,8 +213,8 @@ class DistractingPackageHelperTest : PackageHelperTestBase() {
@Test @Test
fun sendDistractingPackagesChanged_withDifferentVisibilityAllowList() { fun sendDistractingPackagesChanged_withDifferentVisibilityAllowList() {
mockAllowList(packageSetting1, allowList(10001, 10002, 10003)) mockDividedSeparatedBroadcastList(
mockAllowList(packageSetting2, allowList(10001, 10002, 10004)) intArrayOf(10001, 10002, 10003), intArrayOf(10001, 10002, 10007))
distractingPackageHelper.sendDistractingPackagesChanged(pms.snapshotComputer(), distractingPackageHelper.sendDistractingPackagesChanged(pms.snapshotComputer(),
packagesToChange, uidsToChange, TEST_USER_ID, packagesToChange, uidsToChange, TEST_USER_ID,
@@ -227,20 +224,19 @@ class DistractingPackageHelperTest : PackageHelperTestBase() {
eq(Intent.ACTION_DISTRACTING_PACKAGES_CHANGED), nullable(), bundleCaptor.capture(), eq(Intent.ACTION_DISTRACTING_PACKAGES_CHANGED), nullable(), bundleCaptor.capture(),
anyInt(), nullable(), nullable(), any(), nullable(), nullable(), nullable()) anyInt(), nullable(), nullable(), any(), nullable(), nullable(), nullable())
bundleCaptor.allValues.forEach { bundleCaptor.allValues.forEachIndexed { i, it ->
var changedPackages = it.getStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST) var changedPackages = it.getStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST)
var changedUids = it.getIntArray(Intent.EXTRA_CHANGED_UID_LIST) var changedUids = it.getIntArray(Intent.EXTRA_CHANGED_UID_LIST)
assertThat(changedPackages?.size).isEqualTo(1) assertThat(changedPackages?.size).isEqualTo(1)
assertThat(changedUids?.size).isEqualTo(1) assertThat(changedUids?.size).isEqualTo(1)
assertThat(changedPackages?.get(0)).isAnyOf(TEST_PACKAGE_1, TEST_PACKAGE_2) assertThat(changedPackages?.get(0)).isEqualTo(packagesToChange[i])
assertThat(changedUids?.get(0)).isAnyOf(packageSetting1.appId, packageSetting2.appId) assertThat(changedUids?.get(0)).isEqualTo(uidsToChange[i])
} }
} }
@Test @Test
fun sendDistractingPackagesChanged_withNullVisibilityAllowList() { fun sendDistractingPackagesChanged_withNullVisibilityAllowList() {
mockAllowList(packageSetting1, allowList(10001, 10002, 10003)) mockDividedSeparatedBroadcastList(intArrayOf(10001, 10002, 10003), null)
mockAllowList(packageSetting2, null /* list */)
distractingPackageHelper.sendDistractingPackagesChanged(pms.snapshotComputer(), distractingPackageHelper.sendDistractingPackagesChanged(pms.snapshotComputer(),
packagesToChange, uidsToChange, TEST_USER_ID, packagesToChange, uidsToChange, TEST_USER_ID,
@@ -250,13 +246,13 @@ class DistractingPackageHelperTest : PackageHelperTestBase() {
eq(Intent.ACTION_DISTRACTING_PACKAGES_CHANGED), nullable(), bundleCaptor.capture(), eq(Intent.ACTION_DISTRACTING_PACKAGES_CHANGED), nullable(), bundleCaptor.capture(),
anyInt(), nullable(), nullable(), any(), nullable(), nullable(), nullable()) anyInt(), nullable(), nullable(), any(), nullable(), nullable(), nullable())
bundleCaptor.allValues.forEach { bundleCaptor.allValues.forEachIndexed { i, it ->
var changedPackages = it.getStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST) var changedPackages = it.getStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST)
var changedUids = it.getIntArray(Intent.EXTRA_CHANGED_UID_LIST) var changedUids = it.getIntArray(Intent.EXTRA_CHANGED_UID_LIST)
assertThat(changedPackages?.size).isEqualTo(1) assertThat(changedPackages?.size).isEqualTo(1)
assertThat(changedUids?.size).isEqualTo(1) assertThat(changedUids?.size).isEqualTo(1)
assertThat(changedPackages?.get(0)).isAnyOf(TEST_PACKAGE_1, TEST_PACKAGE_2) assertThat(changedPackages?.get(0)).isEqualTo(packagesToChange[i])
assertThat(changedUids?.get(0)).isAnyOf(packageSetting1.appId, packageSetting2.appId) assertThat(changedUids?.get(0)).isEqualTo(uidsToChange[i])
} }
} }
} }

View File

@@ -20,10 +20,7 @@ import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.UserHandle import android.os.UserHandle
import android.os.UserManager import android.os.UserManager
import android.util.ArrayMap
import android.util.SparseArray
import com.android.server.pm.pkg.PackageStateInternal import com.android.server.pm.pkg.PackageStateInternal
import com.android.server.pm.snapshot.PackageDataSnapshot
import com.android.server.testutils.TestHandler import com.android.server.testutils.TestHandler
import com.android.server.testutils.any import com.android.server.testutils.any
import com.android.server.testutils.eq import com.android.server.testutils.eq
@@ -31,10 +28,10 @@ import com.android.server.testutils.whenever
import org.junit.Before import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.mockito.ArgumentCaptor import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Captor import org.mockito.Captor
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito import org.mockito.Mockito
import org.mockito.Mockito.argThat
import org.mockito.Mockito.spy import org.mockito.Mockito.spy
import org.mockito.MockitoAnnotations import org.mockito.MockitoAnnotations
@@ -105,6 +102,7 @@ open class PackageHelperTestBase {
whenever(rule.mocks().userManagerService.hasUserRestriction( whenever(rule.mocks().userManagerService.hasUserRestriction(
eq(UserManager.DISALLOW_UNINSTALL_APPS), eq(TEST_USER_ID))).thenReturn(true) eq(UserManager.DISALLOW_UNINSTALL_APPS), eq(TEST_USER_ID))).thenReturn(true)
mockKnownPackages(pms) mockKnownPackages(pms)
mockUnifiedSeparatedBroadcastList()
} }
private fun mockKnownPackages(pms: PackageManagerService) { private fun mockKnownPackages(pms: PackageManagerService) {
@@ -142,15 +140,25 @@ open class PackageHelperTestBase {
return pms return pms
} }
protected fun allowList(vararg uids: Int) = SparseArray<IntArray>().apply { protected fun mockUnifiedSeparatedBroadcastList() {
this.put(TEST_USER_ID, uids) whenever(broadcastHelper.getBroadcastParams(any(Computer::class.java),
any() as Array<String>, any(IntArray::class.java), anyInt()
)).thenReturn(ArrayList<BroadcastParams>().apply {
this.add(BroadcastParams(packagesToChange[0], uidsToChange[0], IntArray(0),
TEST_USER_ID).apply {
this.addPackage(packagesToChange[1], uidsToChange[1])
})
})
} }
protected fun mockAllowList(pkgSetting: PackageStateInternal, list: SparseArray<IntArray>?) { protected fun mockDividedSeparatedBroadcastList(allowlist1: IntArray?, allowlist2: IntArray?) {
whenever(rule.mocks().appsFilter.getVisibilityAllowList( whenever(broadcastHelper.getBroadcastParams(any(Computer::class.java),
any(PackageDataSnapshot::class.java), any() as Array<String>, any(IntArray::class.java), anyInt()
argThat { it?.packageName == pkgSetting.packageName }, any(IntArray::class.java), )).thenReturn(ArrayList<BroadcastParams>().apply {
any() as ArrayMap<String, out PackageStateInternal> this.add(BroadcastParams(packagesToChange[0], uidsToChange[0],
)).thenReturn(list) allowlist1 ?: IntArray(0), TEST_USER_ID))
this.add(BroadcastParams(packagesToChange[1], uidsToChange[1],
allowlist2 ?: IntArray(0), TEST_USER_ID))
})
} }
} }

View File

@@ -298,14 +298,11 @@ class SuspendPackageHelperTest : PackageHelperTestBase() {
@Test @Test
@Throws(Exception::class) @Throws(Exception::class)
fun sendPackagesSuspendedForUser_withSameVisibilityAllowList() { fun sendPackagesSuspendedForUser_withSameVisibilityAllowList() {
mockAllowList(packageSetting1, allowList(10001, 10002, 10003))
mockAllowList(packageSetting2, allowList(10001, 10002, 10003))
suspendPackageHelper.sendPackagesSuspendedForUser(pms.snapshotComputer(), suspendPackageHelper.sendPackagesSuspendedForUser(pms.snapshotComputer(),
Intent.ACTION_PACKAGES_SUSPENDED, packagesToChange, uidsToChange, TEST_USER_ID) Intent.ACTION_PACKAGES_SUSPENDED, packagesToChange, uidsToChange, TEST_USER_ID)
testHandler.flush() testHandler.flush()
verify(broadcastHelper).sendPackageBroadcast(any(), nullable(), bundleCaptor.capture(), verify(broadcastHelper).sendPackageBroadcast(any(), nullable(), bundleCaptor.capture(),
anyInt(), nullable(), nullable(), any(), nullable(), any(), nullable()) anyInt(), nullable(), nullable(), any(), nullable(), nullable(), nullable())
var changedPackages = bundleCaptor.value.getStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST) var changedPackages = bundleCaptor.value.getStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST)
var changedUids = bundleCaptor.value.getIntArray(Intent.EXTRA_CHANGED_UID_LIST) var changedUids = bundleCaptor.value.getIntArray(Intent.EXTRA_CHANGED_UID_LIST)
@@ -317,8 +314,8 @@ class SuspendPackageHelperTest : PackageHelperTestBase() {
@Test @Test
@Throws(Exception::class) @Throws(Exception::class)
fun sendPackagesSuspendedForUser_withDifferentVisibilityAllowList() { fun sendPackagesSuspendedForUser_withDifferentVisibilityAllowList() {
mockAllowList(packageSetting1, allowList(10001, 10002, 10003)) mockDividedSeparatedBroadcastList(
mockAllowList(packageSetting2, allowList(10001, 10002, 10007)) intArrayOf(10001, 10002, 10003), intArrayOf(10001, 10002, 10007))
suspendPackageHelper.sendPackagesSuspendedForUser(pms.snapshotComputer(), suspendPackageHelper.sendPackagesSuspendedForUser(pms.snapshotComputer(),
Intent.ACTION_PACKAGES_SUSPENDED, packagesToChange, uidsToChange, TEST_USER_ID) Intent.ACTION_PACKAGES_SUSPENDED, packagesToChange, uidsToChange, TEST_USER_ID)
@@ -327,21 +324,20 @@ class SuspendPackageHelperTest : PackageHelperTestBase() {
any(), nullable(), bundleCaptor.capture(), anyInt(), nullable(), nullable(), any(), any(), nullable(), bundleCaptor.capture(), anyInt(), nullable(), nullable(), any(),
nullable(), any(), nullable()) nullable(), any(), nullable())
bundleCaptor.allValues.forEach { bundleCaptor.allValues.forEachIndexed { i, it ->
var changedPackages = it.getStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST) var changedPackages = it.getStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST)
var changedUids = it.getIntArray(Intent.EXTRA_CHANGED_UID_LIST) var changedUids = it.getIntArray(Intent.EXTRA_CHANGED_UID_LIST)
assertThat(changedPackages?.size).isEqualTo(1) assertThat(changedPackages?.size).isEqualTo(1)
assertThat(changedUids?.size).isEqualTo(1) assertThat(changedUids?.size).isEqualTo(1)
assertThat(changedPackages?.get(0)).isAnyOf(TEST_PACKAGE_1, TEST_PACKAGE_2) assertThat(changedPackages?.get(0)).isEqualTo(packagesToChange[i])
assertThat(changedUids?.get(0)).isAnyOf(packageSetting1.appId, packageSetting2.appId) assertThat(changedUids?.get(0)).isEqualTo(uidsToChange[i])
} }
} }
@Test @Test
@Throws(Exception::class) @Throws(Exception::class)
fun sendPackagesSuspendedForUser_withNullVisibilityAllowList() { fun sendPackagesSuspendedForUser_withNullVisibilityAllowList() {
mockAllowList(packageSetting1, allowList(10001, 10002, 10003)) mockDividedSeparatedBroadcastList(intArrayOf(10001, 10002, 10003), null)
mockAllowList(packageSetting2, null)
suspendPackageHelper.sendPackagesSuspendedForUser(pms.snapshotComputer(), suspendPackageHelper.sendPackagesSuspendedForUser(pms.snapshotComputer(),
Intent.ACTION_PACKAGES_SUSPENDED, packagesToChange, uidsToChange, TEST_USER_ID) Intent.ACTION_PACKAGES_SUSPENDED, packagesToChange, uidsToChange, TEST_USER_ID)
@@ -350,13 +346,13 @@ class SuspendPackageHelperTest : PackageHelperTestBase() {
any(), nullable(), bundleCaptor.capture(), anyInt(), nullable(), nullable(), any(), any(), nullable(), bundleCaptor.capture(), anyInt(), nullable(), nullable(), any(),
nullable(), nullable(), nullable()) nullable(), nullable(), nullable())
bundleCaptor.allValues.forEach { bundleCaptor.allValues.forEachIndexed { i, it ->
var changedPackages = it.getStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST) var changedPackages = it.getStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST)
var changedUids = it.getIntArray(Intent.EXTRA_CHANGED_UID_LIST) var changedUids = it.getIntArray(Intent.EXTRA_CHANGED_UID_LIST)
assertThat(changedPackages?.size).isEqualTo(1) assertThat(changedPackages?.size).isEqualTo(1)
assertThat(changedUids?.size).isEqualTo(1) assertThat(changedUids?.size).isEqualTo(1)
assertThat(changedPackages?.get(0)).isAnyOf(TEST_PACKAGE_1, TEST_PACKAGE_2) assertThat(changedPackages?.get(0)).isEqualTo(packagesToChange[i])
assertThat(changedUids?.get(0)).isAnyOf(packageSetting1.appId, packageSetting2.appId) assertThat(changedUids?.get(0)).isEqualTo(uidsToChange[i])
} }
} }