Search in sources :

Example 1 with CheckTransparencyCommand

use of com.android.tools.build.bundletool.commands.CheckTransparencyCommand in project bundletool by google.

the class ConnectedDeviceModeTransparencyChecker method checkTransparency.

public static TransparencyCheckResult checkTransparency(CheckTransparencyCommand command) {
    command.getAdbServer().get().init(command.getAdbPath().get());
    AdbRunner adbRunner = new AdbRunner(command.getAdbServer().get());
    Device adbDevice = getDevice(command.getAdbServer().get(), command.getDeviceId());
    // Execute a shell command to retrieve paths to all APKs for the given package name.
    AdbShellCommandTask adbShellCommandTask = new AdbShellCommandTask(adbDevice, "pm path " + command.getPackageName().get());
    ImmutableList<String> pathsToApksOnDevice = adbShellCommandTask.execute().stream().filter(path -> path.startsWith(APK_PATH_ON_DEVICE_PREFIX)).map(path -> path.substring(APK_PATH_ON_DEVICE_PREFIX.length())).collect(toImmutableList());
    if (pathsToApksOnDevice.isEmpty()) {
        throw InvalidCommandException.builder().withInternalMessage("No files found for package " + command.getPackageName().get()).build();
    }
    // Pull APKs to a temporary directory and verify code transparency.
    try (TempDirectory tempDir = new TempDirectory("connected-device-transparency-check")) {
        Path apksExtractedSubDirectory = tempDir.getPath().resolve("extracted");
        Files.createDirectory(apksExtractedSubDirectory);
        ImmutableList<FilePullParams> pullParams = createPullParams(pathsToApksOnDevice, apksExtractedSubDirectory);
        if (command.getDeviceId().isPresent()) {
            adbRunner.run(device -> device.pull(pullParams), command.getDeviceId().get());
        } else {
            adbRunner.run(device -> device.pull(pullParams));
        }
        return ApkTransparencyCheckUtils.checkTransparency(pullParams.stream().map(FilePullParams::getDestinationPath).collect(toImmutableList()));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : AdbRunner(com.android.tools.build.bundletool.device.AdbRunner) CheckTransparencyCommand(com.android.tools.build.bundletool.commands.CheckTransparencyCommand) FilePullParams(com.android.tools.build.bundletool.device.Device.FilePullParams) Files(java.nio.file.Files) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Device(com.android.tools.build.bundletool.device.Device) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) DeviceAnalyzer(com.android.tools.build.bundletool.device.DeviceAnalyzer) TempDirectory(com.android.tools.build.bundletool.io.TempDirectory) ImmutableList(com.google.common.collect.ImmutableList) Paths(java.nio.file.Paths) InvalidCommandException(com.android.tools.build.bundletool.model.exceptions.InvalidCommandException) Optional(java.util.Optional) AdbShellCommandTask(com.android.tools.build.bundletool.device.AdbShellCommandTask) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) Path(java.nio.file.Path) AdbServer(com.android.tools.build.bundletool.device.AdbServer) Path(java.nio.file.Path) TempDirectory(com.android.tools.build.bundletool.io.TempDirectory) AdbShellCommandTask(com.android.tools.build.bundletool.device.AdbShellCommandTask) AdbRunner(com.android.tools.build.bundletool.device.AdbRunner) Device(com.android.tools.build.bundletool.device.Device) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) FilePullParams(com.android.tools.build.bundletool.device.Device.FilePullParams)

Aggregations

CheckTransparencyCommand (com.android.tools.build.bundletool.commands.CheckTransparencyCommand)1 AdbRunner (com.android.tools.build.bundletool.device.AdbRunner)1 AdbServer (com.android.tools.build.bundletool.device.AdbServer)1 AdbShellCommandTask (com.android.tools.build.bundletool.device.AdbShellCommandTask)1 Device (com.android.tools.build.bundletool.device.Device)1 FilePullParams (com.android.tools.build.bundletool.device.Device.FilePullParams)1 DeviceAnalyzer (com.android.tools.build.bundletool.device.DeviceAnalyzer)1 TempDirectory (com.android.tools.build.bundletool.io.TempDirectory)1 InvalidCommandException (com.android.tools.build.bundletool.model.exceptions.InvalidCommandException)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 UncheckedTimeoutException (com.google.common.util.concurrent.UncheckedTimeoutException)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Optional (java.util.Optional)1 TimeoutException (java.util.concurrent.TimeoutException)1