Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 22bb70b4 authored by Wenhao Wang's avatar Wenhao Wang Committed by Android (Google) Code Review
Browse files

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

parents b1f12e0f cf8a36fb
Loading
Loading
Loading
Loading
+37 −1
Original line number Diff line number Diff line
@@ -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,8 +126,19 @@ public class BackgroundInstallControlService extends SystemService {
        @Override
        public ParceledListSlice<PackageInfo> getBackgroundInstalledPackages(
                @PackageManager.PackageInfoFlagsBits long flags, int 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);
            }
        }
    }

    @VisibleForTesting
@@ -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;