[BIC] Enable the creation of mock apps

We can populate mock background installed apps by setting the
value of "debug.transparency.bg-install-apps".
The string value is the list of names of the mock apps.

Bug: 266888785
Test: Run `adb shell setprop
      debug.transparency.bg-install-apps
      <some existing apps>`
      to set the list of mock apps.
      Run `adb shell cmd transparency get mba_info`
      to check existence of the apps.
Change-Id: I07a6106937a8217c16c31cbacf967d3c2102a2b1
This commit is contained in:
Wenhao Wang
2023-01-27 04:55:45 +00:00
parent df22180cb9
commit cf8a36fba7

View File

@@ -27,12 +27,15 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManagerInternal;
import android.content.pm.ParceledListSlice;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.ArraySet;
import android.util.AtomicFile;
import android.util.Slog;
@@ -51,6 +54,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
@@ -122,7 +126,18 @@ public class BackgroundInstallControlService extends SystemService {
@Override
public ParceledListSlice<PackageInfo> getBackgroundInstalledPackages(
@PackageManager.PackageInfoFlagsBits long flags, int userId) {
return mService.getBackgroundInstalledPackages(flags, userId);
if (!Build.IS_DEBUGGABLE) {
return mService.getBackgroundInstalledPackages(flags, userId);
}
// The debug.transparency.bg-install-apps (only works for debuggable builds)
// is used to set mock list of background installed apps for testing.
// The list of apps' names is delimited by ",".
String propertyString = SystemProperties.get("debug.transparency.bg-install-apps");
if (TextUtils.isEmpty(propertyString)) {
return mService.getBackgroundInstalledPackages(flags, userId);
} else {
return mService.getMockBackgroundInstalledPackages(propertyString);
}
}
}
@@ -145,6 +160,27 @@ public class BackgroundInstallControlService extends SystemService {
return new ParceledListSlice<>(packages);
}
/**
* Mock a list of background installed packages based on the property string.
*/
@NonNull
ParceledListSlice<PackageInfo> getMockBackgroundInstalledPackages(
@NonNull String propertyString) {
String[] mockPackageNames = propertyString.split(",");
List<PackageInfo> mockPackages = new ArrayList<>();
for (String name : mockPackageNames) {
try {
PackageInfo packageInfo = mPackageManager.getPackageInfo(name,
PackageManager.PackageInfoFlags.of(PackageManager.MATCH_ALL));
mockPackages.add(packageInfo);
} catch (PackageManager.NameNotFoundException e) {
Slog.w(TAG, "Package's PackageInfo not found " + name);
continue;
}
}
return new ParceledListSlice<>(mockPackages);
}
private static class EventHandler extends Handler {
private final BackgroundInstallControlService mService;