1 /*
2  * Copyright (C) 2017 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.deviceowner;
17 
18 import android.app.Activity;
19 import android.app.admin.DevicePolicyManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.os.Process;
25 import android.util.Log;
26 
27 /**
28  * Simple activity that adds or clears a user restriction depending on the value of the extras.
29  */
30 public class SetPolicyActivity extends Activity {
31 
32     private static final String TAG = SetPolicyActivity.class.getName();
33 
34     private static final String EXTRA_RESTRICTION_KEY = "extra-restriction-key";
35     private static final String EXTRA_COMMAND = "extra-command";
36 
37     private static final String ADD_RESTRICTION_COMMAND = "add-restriction";
38     private static final String CLEAR_RESTRICTION_COMMAND = "clear-restriction";
39 
40     @Override
onCreate(Bundle savedInstanceState)41     public void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         handleIntent(getIntent());
44     }
45 
46     // Overriding this method in case another intent is sent to this activity before finish()
47     @Override
onNewIntent(Intent intent)48     public void onNewIntent(Intent intent) {
49         super.onNewIntent(intent);
50         handleIntent(intent);
51     }
52 
53     @Override
onPause()54     public void onPause() {
55         super.onPause();
56         // Calling finish() here because doing it in onCreate(), onStart() or onResume() makes
57         // "adb shell am start" timeout if using the -W option.
58         finish();
59     }
60 
handleIntent(Intent intent)61     private void handleIntent(Intent intent) {
62         DevicePolicyManager dpm = (DevicePolicyManager)
63                 getSystemService(Context.DEVICE_POLICY_SERVICE);
64         String command = intent.getStringExtra(EXTRA_COMMAND);
65         Log.i(TAG, "Command: \"" + command);
66         ComponentName admin = BaseDeviceOwnerTest.getWho();
67         if (ADD_RESTRICTION_COMMAND.equals(command)) {
68             String restrictionKey = intent.getStringExtra(EXTRA_RESTRICTION_KEY);
69             dpm.addUserRestriction(admin, restrictionKey);
70             Log.i(TAG, "Added user restriction " + restrictionKey
71                     + " for user " + Process.myUserHandle());
72         } else if (CLEAR_RESTRICTION_COMMAND.equals(command)) {
73             String restrictionKey = intent.getStringExtra(EXTRA_RESTRICTION_KEY);
74             dpm.clearUserRestriction(admin, restrictionKey);
75             Log.i(TAG, "Cleared user restriction " + restrictionKey
76                     + " for user " + Process.myUserHandle());
77         } else {
78             Log.e(TAG, "Invalid command: " + command);
79         }
80     }
81 
82 }
83