1 /* 2 * Copyright (C) 2014 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.managedprofile; 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.os.Bundle; 23 import android.os.Process; 24 import android.util.Log; 25 26 import com.android.cts.managedprofile.BaseManagedProfileTest; 27 28 /** 29 * Simple activity that adds or clears a user restriction depending on the value of the extras. 30 */ 31 public class SetPolicyActivity extends Activity { 32 33 private static final String TAG = SetPolicyActivity.class.getName(); 34 35 private static final String EXTRA_RESTRICTION_KEY = "extra-restriction-key"; 36 private static final String EXTRA_PACKAGE_NAME = "extra-package-name"; 37 private static final String EXTRA_COMMAND = "extra-command"; 38 39 private static final String ADD_RESTRICTION_COMMAND = "add-restriction"; 40 private static final String CLEAR_RESTRICTION_COMMAND = "clear-restriction"; 41 private static final String ADD_CROSS_PROFILE_WIDGET_COMMAND = "add-cross-profile-widget"; 42 private static final String REMOVE_CROSS_PROFILE_WIDGET_COMMAND = "remove-cross-profile-widget"; 43 44 @Override 45 public void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 handleIntent(getIntent()); 48 } 49 50 // Overriding this method in case another intent is sent to this activity before finish() 51 @Override 52 public void onNewIntent(Intent intent) { 53 super.onNewIntent(intent); 54 handleIntent(intent); 55 } 56 57 @Override 58 public void onPause() { 59 super.onPause(); 60 // Calling finish() here because doing it in onCreate(), onStart() or onResume() makes 61 // "adb shell am start" timeout if using the -W option. 62 finish(); 63 } 64 65 private void handleIntent(Intent intent) { 66 DevicePolicyManager dpm = (DevicePolicyManager) 67 getSystemService(Context.DEVICE_POLICY_SERVICE); 68 String command = intent.getStringExtra(EXTRA_COMMAND); 69 Log.i(TAG, "Command: \"" + command); 70 71 if (ADD_RESTRICTION_COMMAND.equals(command)) { 72 String restrictionKey = intent.getStringExtra(EXTRA_RESTRICTION_KEY); 73 dpm.addUserRestriction(BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT, restrictionKey); 74 Log.i(TAG, "Added user restriction " + restrictionKey 75 + " for user " + Process.myUserHandle()); 76 } else if (CLEAR_RESTRICTION_COMMAND.equals(command)) { 77 String restrictionKey = intent.getStringExtra(EXTRA_RESTRICTION_KEY); 78 dpm.clearUserRestriction( 79 BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT, restrictionKey); 80 Log.i(TAG, "Cleared user restriction " + restrictionKey 81 + " for user " + Process.myUserHandle()); 82 } else if (ADD_CROSS_PROFILE_WIDGET_COMMAND.equals(command)) { 83 String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME); 84 dpm.addCrossProfileWidgetProvider(BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT, 85 packageName); 86 Log.i(TAG, "Added cross-profile widget provider for package:" + packageName 87 + " for user " + Process.myUserHandle()); 88 } else if (REMOVE_CROSS_PROFILE_WIDGET_COMMAND.equals(command)) { 89 String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME); 90 dpm.removeCrossProfileWidgetProvider(BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT, 91 packageName); 92 Log.i(TAG, "Removed cross-profile widget provider for package:" + packageName 93 + " for user " + Process.myUserHandle()); 94 } else { 95 Log.e(TAG, "Invalid command: " + command); 96 } 97 } 98 99 }