Skip to content

Commit 3d286c4

Browse files
committed
Added commands to get / set application restrictions.
Example: $ alias testdpc='adb shell dumpsys activity service --user 0 com.afwsamples.testdpc' $ testdpc |grep app-restriction set-app-restrictions <PKG> <K1 V1> [Kn Vn] - sets the key/value (as String) application restrictions for the given application get-app-restrictions [PKG1] [PKGNn] - get the application restrictions for the given apps, or for TestDPC itself (using UserManager) when PKG is not passed $ testdpc get-app-restrictions SERVICE com.afwsamples.testdpc/.DeviceAdminService affcbf pid=29012 user=0 Client: No app restrictions for com.afwsamples.testdpc $ testdpc set-app-restrictions com.afwsamples.testdpc dude sweet SERVICE com.afwsamples.testdpc/.DeviceAdminService affcbf pid=29012 user=0 Client: Set 1 app restrictions for com.afwsamples.testdpc $ testdpc get-app-restrictions SERVICE com.afwsamples.testdpc/.DeviceAdminService affcbf pid=29012 user=0 Client: 1 app restrictions for com.afwsamples.testdpc dude = sweet $ testdpc get-app-restrictions com.afwsamples.testdpc foo bar SERVICE com.afwsamples.testdpc/.DeviceAdminService affcbf pid=29012 user=0 Client: 1 app restrictions for com.afwsamples.testdpc dude = sweet No app restrictions for foo No app restrictions for bar Test: see above Bug: 171350084 Change-Id: I90d9be54d4bf3bffdd83369275c15fc1a267140c
1 parent 96eca39 commit 3d286c4

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.content.pm.PackageManager;
2222
import android.content.pm.PackageManager.NameNotFoundException;
2323
import android.graphics.Bitmap;
24+
import android.os.Bundle;
2425
import android.os.PersistableBundle;
2526
import android.os.UserHandle;
2627
import android.os.UserManager;
@@ -363,6 +364,22 @@ void setLockTaskFeatures(int flags, @NonNull Consumer<Void> onSuccess,
363364
*/
364365
boolean isLockTaskPermitted(String packageName);
365366

367+
/**
368+
* See {@link android.app.admin.DevicePolicyManager#setApplicationRestrictions(ComponentName, String, Bundle)}.
369+
*/
370+
void setApplicationRestrictions(String packageName, Bundle settings,
371+
@NonNull Consumer<Void> onSuccess, @NonNull Consumer<Exception> onError);
372+
373+
/**
374+
* See {@link android.app.admin.DevicePolicyManager#getApplicationRestrictions(ComponentName, String)}.
375+
*/
376+
Bundle getApplicationRestrictions(String packageName);
377+
378+
/**
379+
* Gets this app's own restrictions using {@link UserManager#getApplicationRestrictions(String)}.
380+
*/
381+
Bundle getSelfRestrictions();
382+
366383
/**
367384
* Used on error callbacks to indicate a {@link android.app.admin.DevicePolicyManager} method
368385
* 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
@@ -704,6 +704,29 @@ public boolean isLockTaskPermitted(String packageName) {
704704
return mDevicePolicyManager.isLockTaskPermitted(packageName);
705705
}
706706

707+
@Override
708+
public Bundle getApplicationRestrictions(String packageName) {
709+
return mDevicePolicyManager.getApplicationRestrictions(mAdminComponentName, packageName);
710+
}
711+
712+
@Override
713+
public Bundle getSelfRestrictions() {
714+
return mUserManager.getApplicationRestrictions(mAdminComponentName.getPackageName());
715+
}
716+
717+
@Override
718+
public void setApplicationRestrictions(String packageName, Bundle settings,
719+
Consumer<Void> onSuccess, Consumer<Exception> onError) {
720+
Log.d(TAG, "setApplicationRestrictions(" + packageName + ")");
721+
try {
722+
mDevicePolicyManager.setApplicationRestrictions(mAdminComponentName, packageName,
723+
settings);
724+
onSuccess.accept(null);
725+
} catch (Exception e) {
726+
onError.accept(e);
727+
}
728+
}
729+
707730
@Override
708731
public String toString() {
709732
return "DevicePolicyManagerGatewayImpl[" + mAdminComponentName + "]";

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

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.graphics.Bitmap;
2222
import android.graphics.BitmapFactory;
2323
import android.os.Build;
24+
import android.os.Bundle;
2425
import android.os.Environment;
2526
import android.os.UserHandle;
2627
import android.util.Log;
@@ -92,6 +93,8 @@ final class ShellCommand {
9293
private static final String CMD_IS_LOCK_TASK_PERMITTED = "is-lock-task-permitted";
9394
private static final String CMD_SET_LOCK_TASK_FEATURES = "set-lock-task-features";
9495
private static final String CMD_GET_LOCK_TASK_FEATURES = "get-lock-task-features";
96+
private static final String CMD_SET_APP_RESTRICTIONS = "set-app-restrictions";
97+
private static final String CMD_GET_APP_RESTRICTIONS = "get-app-restrictions";
9598

9699
// Commands for APIs added on Android S
97100
private static final String CMD_SET_PERMITTED_INPUT_METHODS_PARENT =
@@ -260,6 +263,12 @@ public void run() {
260263
case CMD_GET_LOCK_TASK_FEATURES:
261264
execute(() -> getLockTaskFeatures());
262265
break;
266+
case CMD_SET_APP_RESTRICTIONS:
267+
execute(() -> setAppRestrictions());
268+
break;
269+
case CMD_GET_APP_RESTRICTIONS:
270+
execute(() -> getAppRestrictions());
271+
break;
263272
default:
264273
mWriter.printf("Invalid command: %s\n\n", cmd);
265274
showUsage();
@@ -354,6 +363,12 @@ private void showUsage() {
354363
+ "have tasks locked\n", CMD_IS_LOCK_TASK_PERMITTED);
355364
mWriter.printf("\t%s <FLAGS> - set the lock task features\n", CMD_SET_LOCK_TASK_FEATURES);
356365
mWriter.printf("\t%s - get the lock task features\n", CMD_GET_LOCK_TASK_FEATURES);
366+
mWriter.printf("\t%s <PKG> [K1 V1] [Kn Vn] - sets the key/value (as String) application "
367+
+ "restrictions for the given app (or resets when no key/value is passed)\n",
368+
CMD_SET_APP_RESTRICTIONS);
369+
mWriter.printf("\t%s [PKG1] [PKGNn] - get the application restrictions for the given apps, "
370+
+ "or for TestDPC itself (using UserManager) when PKG is not passed\n",
371+
CMD_GET_APP_RESTRICTIONS);
357372

358373
if (Util.isAtLeastS()) {
359374
mWriter.printf("\t%s <true|false> - enable / disable USB data signaling\n",
@@ -793,11 +808,51 @@ private void setLockTaskFeatures() {
793808

794809
private void getLockTaskFeatures() {
795810
int flags = mDevicePolicyManagerGateway.getLockTaskFeatures();
796-
String features= Util.lockTaskFeaturesToString(flags);
811+
String features = Util.lockTaskFeaturesToString(flags);
797812

798813
mWriter.printf("%s (%d)\n", features, flags);
799814
}
800815

816+
private void setAppRestrictions() {
817+
// TODO(b/171350084): check args size
818+
String packageName = mArgs[1];
819+
Bundle settings = new Bundle();
820+
for (int i = 2; i < mArgs.length; i++) {
821+
String key = mArgs[i];
822+
String value = mArgs[++i];
823+
settings.putString(key, value);
824+
}
825+
mDevicePolicyManagerGateway.setApplicationRestrictions(packageName, settings,
826+
(v) -> onSuccess("Set %d app restrictions for %s", settings.size(), packageName),
827+
(e) -> onError(e, "Error setting app restrictions for %s", packageName));
828+
}
829+
830+
private void getAppRestrictions() {
831+
if (mArgs.length == 1) {
832+
printAppRestrictions(mContext.getPackageName(),
833+
mDevicePolicyManagerGateway.getSelfRestrictions());
834+
return;
835+
}
836+
837+
for (int i = 1; i < mArgs.length; i++) {
838+
String packageName = mArgs[i];
839+
Bundle settings = mDevicePolicyManagerGateway.getApplicationRestrictions(packageName);
840+
printAppRestrictions(packageName, settings);
841+
}
842+
}
843+
844+
private void printAppRestrictions(String packageName, Bundle settings) {
845+
if (settings == null || settings.isEmpty()) {
846+
mWriter.printf("No app restrictions for %s\n", packageName);
847+
return;
848+
}
849+
mWriter.printf("%d app restrictions for %s\n", settings.size(), packageName);
850+
for (String key : settings.keySet()) {
851+
Object value = settings.get(key);
852+
mWriter.printf(" %s = %s\n", key, value);
853+
}
854+
}
855+
801856
private static String permittedToString(boolean permitted) {
802857
return permitted ? "PERMITTED" : "NOT PERMITTED";
803858
}

0 commit comments

Comments
 (0)