Skip to content

Commit 8c27a23

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 (cherry picked from commit 992a3d51951cbf277943d0be33abb51445a144f1)
1 parent 7155bcd commit 8c27a23

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-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
@@ -19,6 +19,7 @@
1919
import android.content.ComponentName;
2020
import android.content.pm.PackageManager.NameNotFoundException;
2121
import android.graphics.Bitmap;
22+
import android.os.Bundle;
2223
import android.os.PersistableBundle;
2324
import android.os.UserHandle;
2425
import android.os.UserManager;
@@ -299,6 +300,22 @@ void setLockTaskFeatures(int flags, @NonNull Consumer<Void> onSuccess,
299300
*/
300301
boolean isLockTaskPermitted(String packageName);
301302

303+
/**
304+
* See {@link android.app.admin.DevicePolicyManager#setApplicationRestrictions(ComponentName, String, Bundle)}.
305+
*/
306+
void setApplicationRestrictions(String packageName, Bundle settings,
307+
@NonNull Consumer<Void> onSuccess, @NonNull Consumer<Exception> onError);
308+
309+
/**
310+
* See {@link android.app.admin.DevicePolicyManager#getApplicationRestrictions(ComponentName, String)}.
311+
*/
312+
Bundle getApplicationRestrictions(String packageName);
313+
314+
/**
315+
* Gets this app's own restrictions using {@link UserManager#getApplicationRestrictions(String)}.
316+
*/
317+
Bundle getSelfRestrictions();
318+
302319
/**
303320
* Used on error callbacks to indicate a {@link android.app.admin.DevicePolicyManager} method
304321
* 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
@@ -552,6 +552,29 @@ public boolean isLockTaskPermitted(String packageName) {
552552
return mDevicePolicyManager.isLockTaskPermitted(packageName);
553553
}
554554

555+
@Override
556+
public Bundle getApplicationRestrictions(String packageName) {
557+
return mDevicePolicyManager.getApplicationRestrictions(mAdminComponentName, packageName);
558+
}
559+
560+
@Override
561+
public Bundle getSelfRestrictions() {
562+
return mUserManager.getApplicationRestrictions(mAdminComponentName.getPackageName());
563+
}
564+
565+
@Override
566+
public void setApplicationRestrictions(String packageName, Bundle settings,
567+
Consumer<Void> onSuccess, Consumer<Exception> onError) {
568+
Log.d(TAG, "setApplicationRestrictions(" + packageName + ")");
569+
try {
570+
mDevicePolicyManager.setApplicationRestrictions(mAdminComponentName, packageName,
571+
settings);
572+
onSuccess.accept(null);
573+
} catch (Exception e) {
574+
onError.accept(e);
575+
}
576+
}
577+
555578
@Override
556579
public String toString() {
557580
return "DevicePolicyManagerGatewayImpl[" + mAdminComponentName + "]";

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import android.content.pm.PackageManager.NameNotFoundException;
2121
import android.graphics.Bitmap;
2222
import android.graphics.BitmapFactory;
23+
import android.os.Build;
24+
import android.os.Bundle;
2325
import android.os.Environment;
2426
import android.os.UserHandle;
2527
import android.util.Log;
@@ -89,6 +91,8 @@ final class ShellCommand {
8991
private static final String CMD_IS_LOCK_TASK_PERMITTED = "is-lock-task-permitted";
9092
private static final String CMD_SET_LOCK_TASK_FEATURES = "set-lock-task-features";
9193
private static final String CMD_GET_LOCK_TASK_FEATURES = "get-lock-task-features";
94+
private static final String CMD_SET_APP_RESTRICTIONS = "set-app-restrictions";
95+
private static final String CMD_GET_APP_RESTRICTIONS = "get-app-restrictions";
9296

9397
private static final String ARG_FLAGS = "--flags";
9498

@@ -231,6 +235,12 @@ public void run() {
231235
case CMD_GET_LOCK_TASK_FEATURES:
232236
execute(() -> getLockTaskFeatures());
233237
break;
238+
case CMD_SET_APP_RESTRICTIONS:
239+
execute(() -> setAppRestrictions());
240+
break;
241+
case CMD_GET_APP_RESTRICTIONS:
242+
execute(() -> getAppRestrictions());
243+
break;
234244
default:
235245
mWriter.printf("Invalid command: %s\n\n", cmd);
236246
showUsage();
@@ -317,6 +327,12 @@ private void showUsage() {
317327
+ "have tasks locked\n", CMD_IS_LOCK_TASK_PERMITTED);
318328
mWriter.printf("\t%s <FLAGS> - set the lock task features\n", CMD_SET_LOCK_TASK_FEATURES);
319329
mWriter.printf("\t%s - get the lock task features\n", CMD_GET_LOCK_TASK_FEATURES);
330+
mWriter.printf("\t%s <PKG> [K1 V1] [Kn Vn] - sets the key/value (as String) application "
331+
+ "restrictions for the given app (or resets when no key/value is passed)\n",
332+
CMD_SET_APP_RESTRICTIONS);
333+
mWriter.printf("\t%s [PKG1] [PKGNn] - get the application restrictions for the given apps, "
334+
+ "or for TestDPC itself (using UserManager) when PKG is not passed\n",
335+
CMD_GET_APP_RESTRICTIONS);
320336
}
321337

322338
private void createUser() {
@@ -692,11 +708,51 @@ private void setLockTaskFeatures() {
692708

693709
private void getLockTaskFeatures() {
694710
int flags = mDevicePolicyManagerGateway.getLockTaskFeatures();
695-
String features= Util.lockTaskFeaturesToString(flags);
711+
String features = Util.lockTaskFeaturesToString(flags);
696712

697713
mWriter.printf("%s (%d)\n", features, flags);
698714
}
699715

716+
private void setAppRestrictions() {
717+
// TODO(b/171350084): check args size
718+
String packageName = mArgs[1];
719+
Bundle settings = new Bundle();
720+
for (int i = 2; i < mArgs.length; i++) {
721+
String key = mArgs[i];
722+
String value = mArgs[++i];
723+
settings.putString(key, value);
724+
}
725+
mDevicePolicyManagerGateway.setApplicationRestrictions(packageName, settings,
726+
(v) -> onSuccess("Set %d app restrictions for %s", settings.size(), packageName),
727+
(e) -> onError(e, "Error setting app restrictions for %s", packageName));
728+
}
729+
730+
private void getAppRestrictions() {
731+
if (mArgs.length == 1) {
732+
printAppRestrictions(mContext.getPackageName(),
733+
mDevicePolicyManagerGateway.getSelfRestrictions());
734+
return;
735+
}
736+
737+
for (int i = 1; i < mArgs.length; i++) {
738+
String packageName = mArgs[i];
739+
Bundle settings = mDevicePolicyManagerGateway.getApplicationRestrictions(packageName);
740+
printAppRestrictions(packageName, settings);
741+
}
742+
}
743+
744+
private void printAppRestrictions(String packageName, Bundle settings) {
745+
if (settings == null || settings.isEmpty()) {
746+
mWriter.printf("No app restrictions for %s\n", packageName);
747+
return;
748+
}
749+
mWriter.printf("%d app restrictions for %s\n", settings.size(), packageName);
750+
for (String key : settings.keySet()) {
751+
Object value = settings.get(key);
752+
mWriter.printf(" %s = %s\n", key, value);
753+
}
754+
}
755+
700756
private static String permittedToString(boolean permitted) {
701757
return permitted ? "PERMITTED" : "NOT PERMITTED";
702758
}

0 commit comments

Comments
 (0)