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.Intent; 21 import android.content.pm.PackageManager.NameNotFoundException; 22 import android.os.Bundle; 23 import android.os.Process; 24 import android.util.Log; 25 26 import com.android.bedstead.dpmwrapper.TestAppSystemServiceFactory; 27 28 import java.util.ArrayList; 29 import java.util.Arrays; 30 import java.util.List; 31 32 /** 33 * Simple activity that sets or unsets a policy depending on the value of the extras. 34 */ 35 public class SetPolicyActivity extends Activity { 36 37 private static final String TAG = SetPolicyActivity.class.getSimpleName(); 38 39 private static final String EXTRA_RESTRICTION_KEY = "extra-restriction-key"; 40 private static final String EXTRA_COMMAND = "extra-command"; 41 private static final String EXTRA_ACCOUNT_TYPE = "extra-account-type"; 42 private static final String EXTRA_PACKAGE_NAME = "extra-package-name"; 43 private static final String EXTRA_SCOPES_LIST = "extra-scopes-list"; 44 private static final String EXTRA_ADMIN_TYPE = "extra-admin-type"; 45 46 private static final String COMMAND_ADD_USER_RESTRICTION = "add-restriction"; 47 private static final String COMMAND_CLEAR_USER_RESTRICTION = "clear-restriction"; 48 private static final String COMMAND_BLOCK_ACCOUNT_TYPE = "block-accounttype"; 49 private static final String COMMAND_UNBLOCK_ACCOUNT_TYPE = "unblock-accounttype"; 50 private static final String COMMAND_SET_APP_RESTRICTIONS_MANAGER = 51 "set-app-restrictions-manager"; 52 private static final String COMMAND_SET_DELEGATED_SCOPES = "set-delegated-scopes"; 53 54 @Override onCreate(Bundle savedInstanceState)55 public void onCreate(Bundle savedInstanceState) { 56 Log.v(TAG, "onCreate(): uid=" + Process.myUid()); 57 super.onCreate(savedInstanceState); 58 handleIntent(getIntent()); 59 } 60 61 // Overriding this method in case another intent is sent to this activity before finish() 62 @Override onNewIntent(Intent intent)63 public void onNewIntent(Intent intent) { 64 super.onNewIntent(intent); 65 handleIntent(intent); 66 } 67 handleIntent(Intent intent)68 private void handleIntent(Intent intent) { 69 DevicePolicyManager dpm = getSystemService(DevicePolicyManager.class); 70 String command = intent.getStringExtra(EXTRA_COMMAND); 71 Log.i(TAG, "Command: " + command + " UID: " + Process.myUid() + " DPM: " + dpm); 72 73 if (COMMAND_ADD_USER_RESTRICTION.equals(command)) { 74 String restrictionKey = intent.getStringExtra(EXTRA_RESTRICTION_KEY); 75 dpm.addUserRestriction(BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT, restrictionKey); 76 Log.i(TAG, "Added user restriction " + restrictionKey 77 + " for user " + Process.myUserHandle()); 78 } else if (COMMAND_CLEAR_USER_RESTRICTION.equals(command)) { 79 String restrictionKey = intent.getStringExtra(EXTRA_RESTRICTION_KEY); 80 dpm.clearUserRestriction( 81 BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT, restrictionKey); 82 Log.i(TAG, "Cleared user restriction " + restrictionKey 83 + " for user " + Process.myUserHandle()); 84 } else if (COMMAND_BLOCK_ACCOUNT_TYPE.equals(command)) { 85 String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE); 86 dpm.setAccountManagementDisabled(BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT, 87 accountType, true); 88 Log.i(TAG, "Blocking account management for account type: " + accountType 89 + " for user " + Process.myUserHandle()); 90 } else if (COMMAND_UNBLOCK_ACCOUNT_TYPE.equals(command)) { 91 String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE); 92 dpm.setAccountManagementDisabled(BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT, 93 accountType, false); 94 Log.i(TAG, "Unblocking account management for account type: " + accountType 95 + " for user " + Process.myUserHandle()); 96 } else if (COMMAND_SET_APP_RESTRICTIONS_MANAGER.equals(command)) { 97 String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME); 98 try { 99 dpm.setApplicationRestrictionsManagingPackage( 100 BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT, packageName); 101 } catch (NameNotFoundException e) { 102 throw new IllegalArgumentException(e); 103 } 104 Log.i(TAG, "Setting the application restrictions managing package to " + packageName); 105 } else if (COMMAND_SET_DELEGATED_SCOPES.equals(command)) { 106 String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME); 107 String scopeArray[] = intent.getStringArrayExtra(EXTRA_SCOPES_LIST); 108 List<String> scopes = scopeArray == null ? new ArrayList<>() 109 : Arrays.asList(scopeArray); 110 Log.i(TAG, "Setting delegated scopes for package: " + packageName + " " + scopes); 111 dpm.setDelegatedScopes(BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT, 112 packageName, scopes); 113 } else { 114 Log.e(TAG, "Invalid command: " + command); 115 } 116 } 117 } 118 119