Skip to content

Commit f537981

Browse files
TreeHugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "Added Shell commands to manage permission grants." into ub-testdpc-sc
2 parents e6f1bac + 0379b94 commit f537981

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

app/src/main/java/com/afwsamples/testdpc/DevicePolicyManagerGateway.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,17 @@ void setApplicationRestrictions(String packageName, Bundle settings,
380380
*/
381381
Bundle getSelfRestrictions();
382382

383+
/**
384+
* See {@link android.app.admin.DevicePolicyManager#setPermissionGrantState(ComponentName, String, String, int)}.
385+
*/
386+
void setPermissionGrantState(String packageName, String permission, int grantState,
387+
@NonNull Consumer<Boolean> onSuccess, @NonNull Consumer<Exception> onError);
388+
389+
/**
390+
* See {@link android.app.admin.DevicePolicyManager#getPermissionGrantState(ComponentName, String, String)}.
391+
*/
392+
int getPermissionGrantState(String packageName, String permission);
393+
383394
/**
384395
* Used on error callbacks to indicate a {@link android.app.admin.DevicePolicyManager} method
385396
* call failed.

app/src/main/java/com/afwsamples/testdpc/DevicePolicyManagerGatewayImpl.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,29 @@ public void setApplicationRestrictions(String packageName, Bundle settings,
727727
}
728728
}
729729

730+
@Override
731+
public void setPermissionGrantState(String packageName, String permission, int grantState,
732+
Consumer<Boolean> onSuccess, Consumer<Exception> onError) {
733+
Log.d(TAG, "setPermissionGrantState(" + packageName + ", " + permission + "): "
734+
+ Util.grantStateToString(grantState));
735+
try {
736+
boolean result = mDevicePolicyManager.setPermissionGrantState(mAdminComponentName,
737+
packageName, permission, grantState);
738+
onSuccess.accept(result);
739+
} catch (Exception e) {
740+
onError.accept(e);
741+
}
742+
}
743+
744+
@Override
745+
public int getPermissionGrantState(String packageName, String permission) {
746+
int grantState = mDevicePolicyManager.getPermissionGrantState(mAdminComponentName,
747+
packageName, permission);
748+
Log.d(TAG, "getPermissionGrantState(" + packageName + ", " + permission + "): "
749+
+ Util.grantStateToString(grantState));
750+
return grantState;
751+
}
752+
730753
@Override
731754
public String toString() {
732755
return "DevicePolicyManagerGatewayImpl[" + mAdminComponentName + "]";

app/src/main/java/com/afwsamples/testdpc/ShellCommand.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ final class ShellCommand {
9595
private static final String CMD_GET_LOCK_TASK_FEATURES = "get-lock-task-features";
9696
private static final String CMD_SET_APP_RESTRICTIONS = "set-app-restrictions";
9797
private static final String CMD_GET_APP_RESTRICTIONS = "get-app-restrictions";
98+
private static final String CMD_SET_PERMISSION_GRANT_STATE = "set-permission-grant-state";
99+
private static final String CMD_GET_PERMISSION_GRANT_STATE = "get-permission-grant-state";
98100

99101
// Commands for APIs added on Android S
100102
private static final String CMD_SET_PERMITTED_INPUT_METHODS_PARENT =
@@ -269,6 +271,12 @@ public void run() {
269271
case CMD_GET_APP_RESTRICTIONS:
270272
execute(() -> getAppRestrictions());
271273
break;
274+
case CMD_SET_PERMISSION_GRANT_STATE:
275+
execute(() -> setPermissionGrantState());
276+
break;
277+
case CMD_GET_PERMISSION_GRANT_STATE:
278+
execute(() -> getPermissionGrantState());
279+
break;
272280
default:
273281
mWriter.printf("Invalid command: %s\n\n", cmd);
274282
showUsage();
@@ -369,6 +377,13 @@ private void showUsage() {
369377
mWriter.printf("\t%s [PKG1] [PKGNn] - get the application restrictions for the given apps, "
370378
+ "or for TestDPC itself (using UserManager) when PKG is not passed\n",
371379
CMD_GET_APP_RESTRICTIONS);
380+
mWriter.printf("\t%s <PKG> <PERMISSION> <STATE> - set the grant state for the given "
381+
+ "package / permission\n",
382+
CMD_SET_PERMISSION_GRANT_STATE);
383+
mWriter.printf("\t%s <PKG> <PERMISSION> - get the grant state for the given permission / "
384+
+ "package\n", CMD_GET_PERMISSION_GRANT_STATE);
385+
386+
// Separator for S / pre-S commands - do NOT remove line to avoid cherry-pick conflicts
372387

373388
if (Util.isAtLeastS()) {
374389
mWriter.printf("\t%s <true|false> - enable / disable USB data signaling\n",
@@ -853,6 +868,29 @@ private void printAppRestrictions(String packageName, Bundle settings) {
853868
}
854869
}
855870

871+
private void setPermissionGrantState() {
872+
// TODO(b/171350084): check args
873+
String packageName = mArgs[1];
874+
String permission = mArgs[2];
875+
int grantState = getIntArg(3);
876+
String grantName = Util.grantStateToString(grantState);
877+
878+
mDevicePolicyManagerGateway.setPermissionGrantState(packageName, permission, grantState,
879+
(v) -> onSuccess("Set %s state on %s to %s", permission, packageName, grantName),
880+
(e) -> onError(e, "Error setting %s state on %s to %s", packageName, permission,
881+
grantName));
882+
}
883+
884+
private void getPermissionGrantState() {
885+
// TODO(b/171350084): check args
886+
String packageName = mArgs[1];
887+
String permission = mArgs[2];
888+
int grantState = mDevicePolicyManagerGateway.getPermissionGrantState(packageName,
889+
permission);
890+
mWriter.printf("%s state for %s: %s\n", permission, packageName,
891+
Util.grantStateToString(grantState));
892+
}
893+
856894
private static String permittedToString(boolean permitted) {
857895
return permitted ? "PERMITTED" : "NOT PERMITTED";
858896
}

app/src/main/java/com/afwsamples/testdpc/common/Util.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ public static String personalAppsSuspensionReasonToString(int reasons) {
268268
return flagsToString(DevicePolicyManager.class, "PERSONAL_APPS_", reasons);
269269
}
270270

271+
public static String grantStateToString(int grantState) {
272+
return constantToString(DevicePolicyManager.class, "PERMISSION_GRANT_STATE_", grantState);
273+
}
274+
271275
public static void onSuccessLog(String tag, String template, Object... args) {
272276
Log.d(tag, String.format(template, args) + " succeeded");
273277
}
@@ -310,6 +314,22 @@ public static String flagsToString(Class<?> clazz, String prefix, int flags) {
310314
return res.toString();
311315
}
312316

317+
// Copied from DebugUtils
318+
public static String constantToString(Class<?> clazz, String prefix, int value) {
319+
for (Field field : clazz.getDeclaredFields()) {
320+
final int modifiers = field.getModifiers();
321+
try {
322+
if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)
323+
&& field.getType().equals(int.class) && field.getName().startsWith(prefix)
324+
&& field.getInt(null) == value) {
325+
return constNameWithoutPrefix(prefix, field);
326+
}
327+
} catch (IllegalAccessException ignored) {
328+
}
329+
}
330+
return prefix + Integer.toString(value);
331+
}
332+
313333
// Copied from DebugUtils
314334
private static String constNameWithoutPrefix(String prefix, Field field) {
315335
return field.getName().substring(prefix.length());

0 commit comments

Comments
 (0)