Compress the known package IDs

The numbers of KnownPackage should be continuous and the string
representation should not be unknown.

Fix: 170356374
Test: atest PackageManagerServiceTest
Change-Id: I2d96ca74a49b8f24ff87f8e21981d8cc0d43a469
This commit is contained in:
Jackal Guo
2020-10-08 13:50:38 +08:00
parent da2e66369b
commit 7e7c256b3d
3 changed files with 88 additions and 26 deletions

View File

@@ -60,6 +60,27 @@ import java.util.function.Consumer;
* @hide Only for use within the system server.
*/
public abstract class PackageManagerInternal {
@IntDef(prefix = "PACKAGE_", value = {
PACKAGE_SYSTEM,
PACKAGE_SETUP_WIZARD,
PACKAGE_INSTALLER,
PACKAGE_VERIFIER,
PACKAGE_BROWSER,
PACKAGE_SYSTEM_TEXT_CLASSIFIER,
PACKAGE_PERMISSION_CONTROLLER,
PACKAGE_WELLBEING,
PACKAGE_DOCUMENTER,
PACKAGE_CONFIGURATOR,
PACKAGE_INCIDENT_REPORT_APPROVER,
PACKAGE_APP_PREDICTOR,
PACKAGE_OVERLAY_CONFIG_SIGNATURE,
PACKAGE_WIFI,
PACKAGE_COMPANION,
PACKAGE_RETAIL_DEMO,
})
@Retention(RetentionPolicy.SOURCE)
public @interface KnownPackage {}
public static final int PACKAGE_SYSTEM = 0;
public static final int PACKAGE_SETUP_WIZARD = 1;
public static final int PACKAGE_INSTALLER = 2;
@@ -72,11 +93,13 @@ public abstract class PackageManagerInternal {
public static final int PACKAGE_CONFIGURATOR = 9;
public static final int PACKAGE_INCIDENT_REPORT_APPROVER = 10;
public static final int PACKAGE_APP_PREDICTOR = 11;
public static final int PACKAGE_OVERLAY_CONFIG_SIGNATURE = 12;
public static final int PACKAGE_WIFI = 13;
public static final int PACKAGE_COMPANION = 14;
public static final int PACKAGE_RETAIL_DEMO = 15;
public static final int PACKAGE_OVERLAY_CONFIG_SIGNATURE = 16;
public static final int LAST_KNOWN_PACKAGE = PACKAGE_OVERLAY_CONFIG_SIGNATURE;
// Integer value of the last known package ID. Increases as new ID is added to KnownPackage.
// Please note the numbers should be continuous.
public static final int LAST_KNOWN_PACKAGE = PACKAGE_RETAIL_DEMO;
@IntDef(flag = true, prefix = "RESOLVE_", value = {
RESOLVE_NON_BROWSER_ONLY,
@@ -118,26 +141,6 @@ public abstract class PackageManagerInternal {
*/
public static final int INTEGRITY_VERIFICATION_REJECT = 0;
@IntDef(value = {
PACKAGE_SYSTEM,
PACKAGE_SETUP_WIZARD,
PACKAGE_INSTALLER,
PACKAGE_VERIFIER,
PACKAGE_BROWSER,
PACKAGE_SYSTEM_TEXT_CLASSIFIER,
PACKAGE_PERMISSION_CONTROLLER,
PACKAGE_WELLBEING,
PACKAGE_DOCUMENTER,
PACKAGE_CONFIGURATOR,
PACKAGE_INCIDENT_REPORT_APPROVER,
PACKAGE_APP_PREDICTOR,
PACKAGE_WIFI,
PACKAGE_COMPANION,
PACKAGE_RETAIL_DEMO,
})
@Retention(RetentionPolicy.SOURCE)
public @interface KnownPackage {}
/** Observer called whenever the list of packages changes */
public interface PackageListObserver {
/** A package was added to the system. */

View File

@@ -22259,11 +22259,8 @@ public class PackageManagerService extends IPackageManager.Stub
final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ", 120);
ipw.println("Known Packages:");
ipw.increaseIndent();
for (int i = 0; i < LAST_KNOWN_PACKAGE; i++) {
for (int i = 0; i <= LAST_KNOWN_PACKAGE; i++) {
final String knownPackage = mPmInternal.knownPackageToString(i);
if ("Unknown".equals(knownPackage)) {
continue;
}
ipw.print(knownPackage);
ipw.println(":");
final String[] pkgNames = mPmInternal.getKnownPackageNames(i,

View File

@@ -16,12 +16,21 @@
package com.android.server.pm;
import static com.google.common.truth.Truth.assertWithMessage;
import static java.lang.reflect.Modifier.isFinal;
import static java.lang.reflect.Modifier.isPublic;
import static java.lang.reflect.Modifier.isStatic;
import android.content.IIntentReceiver;
import android.content.pm.PackageManagerInternal;
import android.os.Bundle;
import android.util.SparseArray;
import androidx.test.runner.AndroidJUnit4;
import com.google.android.collect.Lists;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@@ -29,6 +38,12 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
// runtest -c com.android.server.pm.PackageManagerServiceTest frameworks-services
// bit FrameworksServicesTests:com.android.server.pm.PackageManagerServiceTest
@@ -133,4 +148,51 @@ public class PackageManagerServiceTest {
}
}
}
@Test
public void testKnownPackageToString_shouldNotGetUnknown() {
final List<String> packageNames = new ArrayList<>();
for (int i = 0; i <= PackageManagerInternal.LAST_KNOWN_PACKAGE; i++) {
packageNames.add(PackageManagerInternal.knownPackageToString(i));
}
assertWithMessage(
"The Ids of KnownPackage should be continuous and the string representation "
+ "should not be unknown.").that(
packageNames).containsNoneIn(Lists.newArrayList("Unknown"));
}
@Test
public void testKnownPackage_lastKnownPackageIsTheLast() throws Exception {
final List<Integer> knownPackageIds = getKnownPackageIdsList();
assertWithMessage(
"The last KnownPackage Id should be assigned to PackageManagerInternal"
+ ".LAST_KNOWN_PACKAGE.").that(
knownPackageIds.get(knownPackageIds.size() - 1)).isEqualTo(
PackageManagerInternal.LAST_KNOWN_PACKAGE);
}
@Test
public void testKnownPackage_IdsShouldBeUniqueAndContinuous() throws Exception {
final List<Integer> knownPackageIds = getKnownPackageIdsList();
for (int i = 0, size = knownPackageIds.size(); i < size - 1; i++) {
assertWithMessage(
"The KnownPackage Ids should be unique and continuous. KnownPackageIds = "
+ Arrays.toString(knownPackageIds.toArray())).that(
knownPackageIds.get(i) + 1).isEqualTo(knownPackageIds.get(i + 1));
}
}
private List<Integer> getKnownPackageIdsList() throws IllegalAccessException {
final ArrayList<Integer> knownPackageIds = new ArrayList<>();
final Field[] allFields = PackageManagerInternal.class.getDeclaredFields();
for (Field field : allFields) {
final int modifier = field.getModifiers();
if (isPublic(modifier) && isStatic(modifier) && isFinal(modifier)
&& Pattern.matches("PACKAGE(_[A-Z]+)+", field.getName())) {
knownPackageIds.add(field.getInt(null));
}
}
Collections.sort(knownPackageIds);
return knownPackageIds;
}
}