1 /* 2 * Copyright (C) 2016 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 17 package com.android.cts.verifier.managedprovisioning; 18 19 import android.app.Activity; 20 import android.app.admin.DevicePolicyManager; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.graphics.BitmapFactory; 25 import android.os.Bundle; 26 import android.os.UserManager; 27 import android.util.Log; 28 29 import com.android.cts.verifier.R; 30 import com.android.cts.verifier.managedprovisioning.Utils; 31 32 import java.util.ArrayList; 33 import java.util.concurrent.TimeUnit; 34 35 public class CommandReceiverActivity extends Activity { 36 private static final String TAG = "CommandReceiverActivity"; 37 38 public static final String ACTION_EXECUTE_COMMAND = 39 "com.android.cts.verifier.managedprovisioning.action.EXECUTE_COMMAND"; 40 public static final String EXTRA_COMMAND = 41 "com.android.cts.verifier.managedprovisioning.extra.COMMAND"; 42 43 public static final String COMMAND_SET_USER_RESTRICTION = "set-user_restriction"; 44 public static final String COMMAND_DISALLOW_KEYGUARD_UNREDACTED_NOTIFICATIONS = 45 "disallow-keyguard-unredacted-notifications"; 46 public static final String COMMAND_SET_AUTO_TIME_REQUIRED = "set-auto-time-required"; 47 public static final String COMMAND_SET_GLOBAL_SETTING = 48 "set-global-setting"; 49 public static final String COMMAND_SET_MAXIMUM_TO_LOCK = "set-maximum-time-to-lock"; 50 public static final String COMMAND_SET_PASSWORD_QUALITY = "set-password-quality"; 51 public static final String COMMAND_SET_KEYGUARD_DISABLED = "set-keyguard-disabled"; 52 public static final String COMMAND_SET_LOCK_SCREEN_INFO = "set-lock-screen-info"; 53 public static final String COMMAND_SET_STATUSBAR_DISABLED = "set-statusbar-disabled"; 54 public static final String COMMAND_ALLOW_ONLY_SYSTEM_INPUT_METHODS = 55 "allow-only-system-input-methods"; 56 public static final String COMMAND_ALLOW_ONLY_SYSTEM_ACCESSIBILITY_SERVICES = 57 "allow-only-system-accessibility-services"; 58 public static final String COMMAND_DEVICE_OWNER_CLEAR_POLICIES = "do-clear-policies"; 59 public static final String COMMAND_PROFILE_OWNER_CLEAR_POLICIES = "po-clear-policies"; 60 public static final String COMMAND_REMOVE_DEVICE_OWNER = "remove-device-owner"; 61 public static final String COMMAND_REQUEST_BUGREPORT = "request-bugreport"; 62 public static final String COMMAND_SET_USER_ICON = "set-user-icon"; 63 64 public static final String EXTRA_USER_RESTRICTION = 65 "com.android.cts.verifier.managedprovisioning.extra.USER_RESTRICTION"; 66 public static final String EXTRA_SETTING = 67 "com.android.cts.verifier.managedprovisioning.extra.SETTING"; 68 // This extra can be used along with a command extra to set policy to 69 // specify if that policy is enforced or not. 70 public static final String EXTRA_ENFORCED = 71 "com.android.cts.verifier.managedprovisioning.extra.ENFORCED"; 72 public static final String EXTRA_VALUE = 73 "com.android.cts.verifier.managedprovisioning.extra.VALUE"; 74 75 private ComponentName mAdmin; 76 private DevicePolicyManager mDpm; 77 78 @Override onCreate(Bundle savedInstanceState)79 public void onCreate(Bundle savedInstanceState) { 80 super.onCreate(savedInstanceState); 81 final Intent intent = getIntent(); 82 try { 83 mDpm = (DevicePolicyManager) getSystemService( 84 Context.DEVICE_POLICY_SERVICE); 85 mAdmin = DeviceAdminTestReceiver.getReceiverComponentName(); 86 Log.i(TAG, "Command: " + intent); 87 88 final String command = getIntent().getStringExtra(EXTRA_COMMAND); 89 switch (command) { 90 case COMMAND_SET_USER_RESTRICTION: { 91 String restrictionKey = intent.getStringExtra(EXTRA_USER_RESTRICTION); 92 boolean enforced = intent.getBooleanExtra(EXTRA_ENFORCED, false); 93 if (enforced) { 94 mDpm.addUserRestriction(mAdmin, restrictionKey); 95 } else { 96 mDpm.clearUserRestriction(mAdmin, restrictionKey); 97 } 98 } break; 99 case COMMAND_DISALLOW_KEYGUARD_UNREDACTED_NOTIFICATIONS: { 100 mDpm.setKeyguardDisabledFeatures(mAdmin, 101 DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS); 102 } break; 103 case COMMAND_SET_AUTO_TIME_REQUIRED: { 104 mDpm.setAutoTimeRequired(mAdmin, 105 intent.getBooleanExtra(EXTRA_ENFORCED, false)); 106 } 107 case COMMAND_SET_LOCK_SCREEN_INFO: { 108 mDpm.setDeviceOwnerLockScreenInfo(mAdmin, intent.getStringExtra(EXTRA_VALUE)); 109 } 110 case COMMAND_SET_MAXIMUM_TO_LOCK: { 111 final long timeInSeconds = Long.parseLong(intent.getStringExtra(EXTRA_VALUE)); 112 mDpm.setMaximumTimeToLock(mAdmin, 113 TimeUnit.SECONDS.toMillis(timeInSeconds) /* in milliseconds */); 114 } break; 115 case COMMAND_SET_PASSWORD_QUALITY: { 116 int quality = intent.getIntExtra(EXTRA_VALUE, 0); 117 mDpm.setPasswordQuality(mAdmin, quality); 118 } break; 119 case COMMAND_SET_KEYGUARD_DISABLED: { 120 boolean enforced = intent.getBooleanExtra(EXTRA_ENFORCED, false); 121 if (enforced) { 122 mDpm.resetPassword(null, 0); 123 } 124 mDpm.setKeyguardDisabled(mAdmin, enforced); 125 } break; 126 case COMMAND_SET_STATUSBAR_DISABLED: { 127 boolean enforced = intent.getBooleanExtra(EXTRA_ENFORCED, false); 128 mDpm.setStatusBarDisabled(mAdmin, enforced); 129 } break; 130 case COMMAND_ALLOW_ONLY_SYSTEM_INPUT_METHODS: { 131 boolean enforced = intent.getBooleanExtra(EXTRA_ENFORCED, false); 132 mDpm.setPermittedInputMethods(mAdmin, enforced ? new ArrayList() : null); 133 } break; 134 case COMMAND_ALLOW_ONLY_SYSTEM_ACCESSIBILITY_SERVICES: { 135 boolean enforced = intent.getBooleanExtra(EXTRA_ENFORCED, false); 136 mDpm.setPermittedAccessibilityServices(mAdmin, 137 enforced ? new ArrayList() : null); 138 } break; 139 case COMMAND_SET_GLOBAL_SETTING: { 140 final String setting = intent.getStringExtra(EXTRA_SETTING); 141 final String value = intent.getStringExtra(EXTRA_VALUE); 142 mDpm.setGlobalSetting(mAdmin, setting, value); 143 } break; 144 case COMMAND_REMOVE_DEVICE_OWNER: { 145 if (!mDpm.isDeviceOwnerApp(getPackageName())) { 146 return; 147 } 148 clearAllPolicies(); 149 mDpm.clearDeviceOwnerApp(getPackageName()); 150 } break; 151 case COMMAND_REQUEST_BUGREPORT: { 152 if (!mDpm.isDeviceOwnerApp(getPackageName())) { 153 return; 154 } 155 final boolean bugreportStarted = mDpm.requestBugreport(mAdmin); 156 if (!bugreportStarted) { 157 Utils.showBugreportNotification(this, getString( 158 R.string.bugreport_already_in_progress), 159 Utils.BUGREPORT_NOTIFICATION_ID); 160 } 161 } break; 162 case COMMAND_DEVICE_OWNER_CLEAR_POLICIES: { 163 if (!mDpm.isDeviceOwnerApp(getPackageName())) { 164 return; 165 } 166 clearAllPolicies(); 167 } break; 168 case COMMAND_PROFILE_OWNER_CLEAR_POLICIES: { 169 if (!mDpm.isProfileOwnerApp(getPackageName())) { 170 return; 171 } 172 clearProfileOwnerRelatedPolicies(); 173 } break; 174 case COMMAND_SET_USER_ICON: { 175 if (!mDpm.isDeviceOwnerApp(getPackageName())) { 176 return; 177 } 178 mDpm.setUserIcon(mAdmin, BitmapFactory.decodeResource(getResources(), 179 com.android.cts.verifier.R.drawable.icon)); 180 } break; 181 } 182 } catch (Exception e) { 183 Log.e(TAG, "Failed to execute command: " + intent, e); 184 } finally { 185 finish(); 186 } 187 } 188 clearAllPolicies()189 private void clearAllPolicies() { 190 clearProfileOwnerRelatedPolicies(); 191 192 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_ADD_USER); 193 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_ADJUST_VOLUME); 194 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_CONFIG_BLUETOOTH); 195 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS); 196 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS); 197 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_CONFIG_TETHERING); 198 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_CONFIG_VPN); 199 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_CONFIG_WIFI); 200 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_DATA_ROAMING); 201 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_DEBUGGING_FEATURES); 202 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_FACTORY_RESET); 203 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_FUN); 204 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); 205 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_NETWORK_RESET); 206 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_OUTGOING_BEAM); 207 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_REMOVE_USER); 208 209 mDpm.setDeviceOwnerLockScreenInfo(mAdmin, null); 210 mDpm.setKeyguardDisabled(mAdmin, false); 211 mDpm.setAutoTimeRequired(mAdmin, false); 212 mDpm.setStatusBarDisabled(mAdmin, false); 213 } 214 clearProfileOwnerRelatedPolicies()215 private void clearProfileOwnerRelatedPolicies() { 216 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_APPS_CONTROL); 217 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_CONFIG_CREDENTIALS); 218 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_MODIFY_ACCOUNTS); 219 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_SHARE_LOCATION); 220 mDpm.clearUserRestriction(mAdmin, UserManager.DISALLOW_UNINSTALL_APPS); 221 222 mDpm.setKeyguardDisabledFeatures(mAdmin, 0); 223 mDpm.setPasswordQuality(mAdmin, 0); 224 mDpm.setMaximumTimeToLock(mAdmin, 0); 225 mDpm.setPermittedAccessibilityServices(mAdmin, null); 226 mDpm.setPermittedInputMethods(mAdmin, null); 227 } 228 } 229