|
21 | 21 | import android.graphics.Bitmap;
|
22 | 22 | import android.graphics.BitmapFactory;
|
23 | 23 | import android.os.Build;
|
| 24 | +import android.os.Bundle; |
24 | 25 | import android.os.Environment;
|
25 | 26 | import android.os.UserHandle;
|
26 | 27 | import android.util.Log;
|
@@ -92,6 +93,8 @@ final class ShellCommand {
|
92 | 93 | private static final String CMD_IS_LOCK_TASK_PERMITTED = "is-lock-task-permitted";
|
93 | 94 | private static final String CMD_SET_LOCK_TASK_FEATURES = "set-lock-task-features";
|
94 | 95 | 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"; |
95 | 98 |
|
96 | 99 | // Commands for APIs added on Android S
|
97 | 100 | private static final String CMD_SET_PERMITTED_INPUT_METHODS_PARENT =
|
@@ -260,6 +263,12 @@ public void run() {
|
260 | 263 | case CMD_GET_LOCK_TASK_FEATURES:
|
261 | 264 | execute(() -> getLockTaskFeatures());
|
262 | 265 | break;
|
| 266 | + case CMD_SET_APP_RESTRICTIONS: |
| 267 | + execute(() -> setAppRestrictions()); |
| 268 | + break; |
| 269 | + case CMD_GET_APP_RESTRICTIONS: |
| 270 | + execute(() -> getAppRestrictions()); |
| 271 | + break; |
263 | 272 | default:
|
264 | 273 | mWriter.printf("Invalid command: %s\n\n", cmd);
|
265 | 274 | showUsage();
|
@@ -354,6 +363,12 @@ private void showUsage() {
|
354 | 363 | + "have tasks locked\n", CMD_IS_LOCK_TASK_PERMITTED);
|
355 | 364 | mWriter.printf("\t%s <FLAGS> - set the lock task features\n", CMD_SET_LOCK_TASK_FEATURES);
|
356 | 365 | 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); |
357 | 372 |
|
358 | 373 | if (Util.isAtLeastS()) {
|
359 | 374 | mWriter.printf("\t%s <true|false> - enable / disable USB data signaling\n",
|
@@ -793,11 +808,51 @@ private void setLockTaskFeatures() {
|
793 | 808 |
|
794 | 809 | private void getLockTaskFeatures() {
|
795 | 810 | int flags = mDevicePolicyManagerGateway.getLockTaskFeatures();
|
796 |
| - String features= Util.lockTaskFeaturesToString(flags); |
| 811 | + String features = Util.lockTaskFeaturesToString(flags); |
797 | 812 |
|
798 | 813 | mWriter.printf("%s (%d)\n", features, flags);
|
799 | 814 | }
|
800 | 815 |
|
| 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 | + |
801 | 856 | private static String permittedToString(boolean permitted) {
|
802 | 857 | return permitted ? "PERMITTED" : "NOT PERMITTED";
|
803 | 858 | }
|
|
0 commit comments