• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.cts.deviceandprofileowner;
17 
18 import android.app.Activity;
19 import android.app.admin.DevicePolicyManager;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager.NameNotFoundException;
23 import android.os.Bundle;
24 import android.os.Process;
25 import android.util.Log;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29 
30 /**
31  * Simple activity that sets or unsets a policy depending on the value of the extras.
32  */
33 public class SetPolicyActivity extends Activity {
34 
35     private static final String TAG = SetPolicyActivity.class.getName();
36 
37     private static final String EXTRA_RESTRICTION_KEY = "extra-restriction-key";
38     private static final String EXTRA_COMMAND = "extra-command";
39     private static final String EXTRA_ACCOUNT_TYPE = "extra-account-type";
40     private static final String EXTRA_PACKAGE_NAME = "extra-package-name";
41     private static final String EXTRA_SCOPES_LIST = "extra-scopes-list";
42 
43     private static final String COMMAND_ADD_USER_RESTRICTION = "add-restriction";
44     private static final String COMMAND_CLEAR_USER_RESTRICTION = "clear-restriction";
45     private static final String COMMAND_BLOCK_ACCOUNT_TYPE = "block-accounttype";
46     private static final String COMMAND_UNBLOCK_ACCOUNT_TYPE = "unblock-accounttype";
47     private static final String COMMAND_SET_APP_RESTRICTIONS_MANAGER =
48             "set-app-restrictions-manager";
49     private static final String COMMAND_SET_DELEGATED_SCOPES = "set-delegated-scopes";
50 
51     @Override
onCreate(Bundle savedInstanceState)52     public void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         handleIntent(getIntent());
55     }
56 
57     // Overriding this method in case another intent is sent to this activity before finish()
58     @Override
onNewIntent(Intent intent)59     public void onNewIntent(Intent intent) {
60         super.onNewIntent(intent);
61         handleIntent(intent);
62     }
63 
64     @Override
onPause()65     public void onPause() {
66         super.onPause();
67         // Calling finish() here because doing it in onCreate(), onStart() or onResume() makes
68         // "adb shell am start" timeout if using the -W option.
69         finish();
70     }
71 
handleIntent(Intent intent)72     private void handleIntent(Intent intent) {
73         DevicePolicyManager dpm = (DevicePolicyManager)
74                 getSystemService(Context.DEVICE_POLICY_SERVICE);
75         String command = intent.getStringExtra(EXTRA_COMMAND);
76         Log.i(TAG, "Command: " + command);
77 
78         if (COMMAND_ADD_USER_RESTRICTION.equals(command)) {
79             String restrictionKey = intent.getStringExtra(EXTRA_RESTRICTION_KEY);
80             dpm.addUserRestriction(BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT, restrictionKey);
81             Log.i(TAG, "Added user restriction " + restrictionKey
82                     + " for user " + Process.myUserHandle());
83         } else if (COMMAND_CLEAR_USER_RESTRICTION.equals(command)) {
84             String restrictionKey = intent.getStringExtra(EXTRA_RESTRICTION_KEY);
85             dpm.clearUserRestriction(
86                     BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT, restrictionKey);
87             Log.i(TAG, "Cleared user restriction " + restrictionKey
88                     + " for user " + Process.myUserHandle());
89         } else if (COMMAND_BLOCK_ACCOUNT_TYPE.equals(command)) {
90             String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE);
91             dpm.setAccountManagementDisabled(BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT,
92                     accountType, true);
93             Log.i(TAG, "Blocking account management for account type: " + accountType
94                     + " for user " + Process.myUserHandle());
95         } else if (COMMAND_UNBLOCK_ACCOUNT_TYPE.equals(command)) {
96             String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE);
97             dpm.setAccountManagementDisabled(BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT,
98                     accountType, false);
99             Log.i(TAG, "Unblocking account management for account type: " + accountType
100                     + " for user " + Process.myUserHandle());
101         } else if (COMMAND_SET_APP_RESTRICTIONS_MANAGER.equals(command)) {
102             String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME);
103             try {
104                 dpm.setApplicationRestrictionsManagingPackage(
105                         BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT, packageName);
106             } catch (NameNotFoundException e) {
107                 throw new IllegalArgumentException(e);
108             }
109             Log.i(TAG, "Setting the application restrictions managing package to " + packageName);
110         } else if (COMMAND_SET_DELEGATED_SCOPES.equals(command)) {
111             String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME);
112             String scopeArray[] = intent.getStringArrayExtra(EXTRA_SCOPES_LIST);
113             List<String> scopes = scopeArray == null ? new ArrayList() : Arrays.asList(scopeArray);
114             dpm.setDelegatedScopes(BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT,
115                     packageName, scopes);
116             Log.i(TAG, "Setting delegated scopes for package: " + packageName + " "
117                     + Arrays.toString(scopeArray));
118         } else {
119             Log.e(TAG, "Invalid command: " + command);
120         }
121     }
122 }
123 
124