|
20 | 20 | import android.content.pm.PackageManager.NameNotFoundException;
|
21 | 21 | import android.graphics.Bitmap;
|
22 | 22 | import android.graphics.BitmapFactory;
|
| 23 | +import android.os.Build; |
| 24 | +import android.os.Bundle; |
23 | 25 | import android.os.Environment;
|
24 | 26 | import android.os.UserHandle;
|
25 | 27 | import android.util.Log;
|
@@ -89,6 +91,8 @@ final class ShellCommand {
|
89 | 91 | private static final String CMD_IS_LOCK_TASK_PERMITTED = "is-lock-task-permitted";
|
90 | 92 | private static final String CMD_SET_LOCK_TASK_FEATURES = "set-lock-task-features";
|
91 | 93 | 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"; |
92 | 96 |
|
93 | 97 | private static final String ARG_FLAGS = "--flags";
|
94 | 98 |
|
@@ -231,6 +235,12 @@ public void run() {
|
231 | 235 | case CMD_GET_LOCK_TASK_FEATURES:
|
232 | 236 | execute(() -> getLockTaskFeatures());
|
233 | 237 | break;
|
| 238 | + case CMD_SET_APP_RESTRICTIONS: |
| 239 | + execute(() -> setAppRestrictions()); |
| 240 | + break; |
| 241 | + case CMD_GET_APP_RESTRICTIONS: |
| 242 | + execute(() -> getAppRestrictions()); |
| 243 | + break; |
234 | 244 | default:
|
235 | 245 | mWriter.printf("Invalid command: %s\n\n", cmd);
|
236 | 246 | showUsage();
|
@@ -317,6 +327,12 @@ private void showUsage() {
|
317 | 327 | + "have tasks locked\n", CMD_IS_LOCK_TASK_PERMITTED);
|
318 | 328 | mWriter.printf("\t%s <FLAGS> - set the lock task features\n", CMD_SET_LOCK_TASK_FEATURES);
|
319 | 329 | 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); |
320 | 336 | }
|
321 | 337 |
|
322 | 338 | private void createUser() {
|
@@ -692,11 +708,51 @@ private void setLockTaskFeatures() {
|
692 | 708 |
|
693 | 709 | private void getLockTaskFeatures() {
|
694 | 710 | int flags = mDevicePolicyManagerGateway.getLockTaskFeatures();
|
695 |
| - String features= Util.lockTaskFeaturesToString(flags); |
| 711 | + String features = Util.lockTaskFeaturesToString(flags); |
696 | 712 |
|
697 | 713 | mWriter.printf("%s (%d)\n", features, flags);
|
698 | 714 | }
|
699 | 715 |
|
| 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 | + |
700 | 756 | private static String permittedToString(boolean permitted) {
|
701 | 757 | return permitted ? "PERMITTED" : "NOT PERMITTED";
|
702 | 758 | }
|
|
0 commit comments