Merge "[BIC] Enable the creation of mock apps"

This commit is contained in:
Wenhao Wang
2023-01-31 18:46:06 +00:00
committed by Android (Google) Code Review

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;