Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flag to use pm install to grant permissions #284

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public abstract class InstallApksCommand {
private static final Flag<ImmutableSet<String>> MODULES_FLAG = Flag.stringSet("modules");
private static final Flag<Boolean> ALLOW_DOWNGRADE_FLAG = Flag.booleanFlag("allow-downgrade");
private static final Flag<Boolean> ALLOW_TEST_ONLY_FLAG = Flag.booleanFlag("allow-test-only");
private static final Flag<Boolean> GRANT_ALL_PERMISSIONS_FLAG =
Flag.booleanFlag("grant-all-permissions");
private static final Flag<Integer> DEVICE_TIER_FLAG = Flag.nonNegativeInteger("device-tier");
private static final Flag<ImmutableSet<String>> DEVICE_GROUPS_FLAG =
Flag.stringSet("device-groups");
Expand All @@ -91,6 +93,8 @@ public abstract class InstallApksCommand {

public abstract boolean getAllowTestOnly();

public abstract boolean getGrantAllPermissions();

public abstract Optional<Integer> getDeviceTier();

public abstract Optional<ImmutableSet<String>> getDeviceGroups();
Expand All @@ -105,6 +109,7 @@ public static Builder builder() {
return new AutoValue_InstallApksCommand.Builder()
.setAllowDowngrade(false)
.setAllowTestOnly(false)
.setGrantAllPermissions(false)
.setTimeout(Device.DEFAULT_ADB_TIMEOUT);
}

Expand All @@ -126,6 +131,8 @@ public abstract static class Builder {

public abstract Builder setAllowTestOnly(boolean allowTestOnly);

public abstract Builder setGrantAllPermissions(boolean grantAllPermissions);

public abstract Builder setDeviceTier(Integer deviceTier);

public abstract Builder setDeviceGroups(ImmutableSet<String> deviceGroups);
Expand All @@ -152,6 +159,7 @@ public static InstallApksCommand fromFlags(
Optional<ImmutableSet<String>> modules = MODULES_FLAG.getValue(flags);
Optional<Boolean> allowDowngrade = ALLOW_DOWNGRADE_FLAG.getValue(flags);
Optional<Boolean> allowTestOnly = ALLOW_TEST_ONLY_FLAG.getValue(flags);
Optional<Boolean> grantAllPermissions = GRANT_ALL_PERMISSIONS_FLAG.getValue(flags);
Optional<Integer> deviceTier = DEVICE_TIER_FLAG.getValue(flags);
Optional<ImmutableSet<String>> deviceGroups = DEVICE_GROUPS_FLAG.getValue(flags);
Optional<ImmutableList<Path>> additionalLocalTestingFiles =
Expand All @@ -166,6 +174,7 @@ public static InstallApksCommand fromFlags(
modules.ifPresent(command::setModules);
allowDowngrade.ifPresent(command::setAllowDowngrade);
allowTestOnly.ifPresent(command::setAllowTestOnly);
grantAllPermissions.ifPresent(command::setGrantAllPermissions);
deviceTier.ifPresent(command::setDeviceTier);
deviceGroups.ifPresent(command::setDeviceGroups);
additionalLocalTestingFiles.ifPresent(command::setAdditionalLocalTestingFiles);
Expand Down Expand Up @@ -204,6 +213,7 @@ public void execute() {
InstallOptions.builder()
.setAllowDowngrade(getAllowDowngrade())
.setAllowTestOnly(getAllowTestOnly())
.setGrantAllPermissions(getGrantAllPermissions())
.setTimeout(getTimeout())
.build();

Expand Down Expand Up @@ -430,6 +440,13 @@ public static CommandHelp help() {
"If set, apps with 'android:testOnly=true' set in their manifest can also be"
+ " deployed")
.build())
.addFlag(
FlagDescription.builder()
.setFlagName(GRANT_ALL_PERMISSIONS_FLAG.getName())
.setOptional(true)
.setDescription(
"If set, all permissions listed in the app manifest will be granted")
.build())
.addFlag(
FlagDescription.builder()
.setFlagName(DEVICE_TIER_FLAG.getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class DdmlibDevice extends Device {
private static final int ADB_TIMEOUT_MS = 60000;
private static final String DEVICE_FEATURES_COMMAND = "pm list features";
private static final String GL_EXTENSIONS_COMMAND = "dumpsys SurfaceFlinger";
private static final int MIN_INSTALL_WITH_PERMISSIONS_API_LEVEL = 23;

private final DeviceFeaturesParser deviceFeaturesParser = new DeviceFeaturesParser();
private final GlExtensionsParser glExtensionsParser = new GlExtensionsParser();
Expand Down Expand Up @@ -169,6 +170,10 @@ public void installApks(ImmutableList<Path> apks, InstallOptions installOptions)
if (installOptions.getAllowTestOnly()) {
extraArgs.add("-t");
}
if (installOptions.getGrantAllPermissions() &&
getVersion().isGreaterOrEqualThan(MIN_INSTALL_WITH_PERMISSIONS_API_LEVEL)) {
extraArgs.add("-g");
}

try {
if (getVersion()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@ public abstract static class InstallOptions {

public abstract boolean getAllowTestOnly();

public abstract boolean getGrantAllPermissions();

public abstract Duration getTimeout();

public static Builder builder() {
return new AutoValue_Device_InstallOptions.Builder()
.setTimeout(DEFAULT_ADB_TIMEOUT)
.setAllowReinstall(true)
.setAllowDowngrade(false)
.setAllowTestOnly(false);
.setAllowTestOnly(false)
.setGrantAllPermissions(false);
}

/** Builder for {@link InstallOptions}. */
Expand All @@ -108,6 +111,8 @@ public abstract static class Builder {

public abstract Builder setAllowTestOnly(boolean allowTestOnly);

public abstract Builder setGrantAllPermissions(boolean grantAllPermissions);

public abstract InstallOptions build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,38 @@ public void allowTestOnly() throws Exception {
verify(mockDevice).installPackage(eq(APK_PATH.toString()), anyBoolean(), eq("-t"));
}

@Test
public void grantAllPermissionsPreM() throws Exception {
when(mockDevice.getVersion()).thenReturn(new AndroidVersion(VersionCodes.KITKAT));
DdmlibDevice ddmlibDevice = new DdmlibDevice(mockDevice);

ddmlibDevice.installApks(
ImmutableList.of(APK_PATH), InstallOptions.builder().setGrantAllPermissions(true).build());

verify(mockDevice).installPackage(eq(APK_PATH.toString()), anyBoolean());
}

@SuppressWarnings("unchecked")
@Test
public void grantAllPermissionsPostM() throws Exception {
when(mockDevice.getVersion()).thenReturn(new AndroidVersion(VersionCodes.M));
DdmlibDevice ddmlibDevice = new DdmlibDevice(mockDevice);

ddmlibDevice.installApks(
ImmutableList.of(APK_PATH), InstallOptions.builder().setGrantAllPermissions(true).build());

ArgumentCaptor<List<String>> extraArgsCaptor = ArgumentCaptor.forClass(List.class);
verify(mockDevice)
.installPackages(
eq(ImmutableList.of(APK_PATH.toFile())),
anyBoolean(),
extraArgsCaptor.capture(),
anyLong(),
any(TimeUnit.class));

assertThat(extraArgsCaptor.getValue()).contains("-g");
}

@Test
public void pushFiles_targetLocation() throws Exception {
String destinationPath = "/destination/path";
Expand Down