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.admin.DeviceAdminReceiver;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.UserHandle;
23 import android.util.Log;
24 
25 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
26 
27 import com.android.bedstead.dpmwrapper.DeviceOwnerHelper;
28 import com.android.cts.devicepolicy.OperationSafetyChangedCallback;
29 import com.android.cts.devicepolicy.OperationSafetyChangedEvent;
30 
31 public class BasicAdminReceiver extends DeviceAdminReceiver {
32 
33     private static final String TAG = BasicAdminReceiver.class.getSimpleName();
34 
35     final static String ACTION_USER_ADDED = "com.android.cts.deviceowner.action.USER_ADDED";
36     final static String ACTION_USER_REMOVED = "com.android.cts.deviceowner.action.USER_REMOVED";
37     final static String ACTION_USER_STARTED = "com.android.cts.deviceowner.action.USER_STARTED";
38     final static String ACTION_USER_STOPPED = "com.android.cts.deviceowner.action.USER_STOPPED";
39     final static String ACTION_USER_SWITCHED = "com.android.cts.deviceowner.action.USER_SWITCHED";
40     final static String EXTRA_USER_HANDLE = "com.android.cts.deviceowner.extra.USER_HANDLE";
41     final static String ACTION_NETWORK_LOGS_AVAILABLE =
42             "com.android.cts.deviceowner.action.ACTION_NETWORK_LOGS_AVAILABLE";
43     final static String EXTRA_NETWORK_LOGS_BATCH_TOKEN =
44             "com.android.cts.deviceowner.extra.NETWORK_LOGS_BATCH_TOKEN";
45 
getComponentName(Context context)46     public static ComponentName getComponentName(Context context) {
47         return new ComponentName(context, BasicAdminReceiver.class);
48     }
49 
50     @Override
onReceive(Context context, Intent intent)51     public void onReceive(Context context, Intent intent) {
52         if (DeviceOwnerHelper.runManagerMethod(this, context, intent)) return;
53 
54         String action = intent.getAction();
55         Log.d(TAG, "onReceive(userId=" + context.getUserId() + "): " + action);
56         super.onReceive(context, intent);
57     }
58 
59     @Override
onUserAdded(Context context, Intent intent, UserHandle userHandle)60     public void onUserAdded(Context context, Intent intent, UserHandle userHandle) {
61         sendUserBroadcast(context, ACTION_USER_ADDED, userHandle);
62     }
63 
64     @Override
onUserRemoved(Context context, Intent intent, UserHandle userHandle)65     public void onUserRemoved(Context context, Intent intent, UserHandle userHandle) {
66         sendUserBroadcast(context, ACTION_USER_REMOVED, userHandle);
67     }
68 
69     @Override
onUserStarted(Context context, Intent intent, UserHandle userHandle)70     public void onUserStarted(Context context, Intent intent, UserHandle userHandle) {
71         sendUserBroadcast(context, ACTION_USER_STARTED, userHandle);
72     }
73 
74     @Override
onUserStopped(Context context, Intent intent, UserHandle userHandle)75     public void onUserStopped(Context context, Intent intent, UserHandle userHandle) {
76         sendUserBroadcast(context, ACTION_USER_STOPPED, userHandle);
77     }
78 
79     @Override
onUserSwitched(Context context, Intent intent, UserHandle userHandle)80     public void onUserSwitched(Context context, Intent intent, UserHandle userHandle) {
81         sendUserBroadcast(context, ACTION_USER_SWITCHED, userHandle);
82     }
83 
84     @Override
onNetworkLogsAvailable(Context context, Intent intent, long batchToken, int networkLogsCount)85     public void onNetworkLogsAvailable(Context context, Intent intent, long batchToken,
86             int networkLogsCount) {
87         Log.d(TAG, "onNetworkLogsAvailable() on user " + context.getUserId()
88                 + ": token=" + batchToken + ", count=" + networkLogsCount);
89         super.onNetworkLogsAvailable(context, intent, batchToken, networkLogsCount);
90         // send the broadcast, the rest of the test happens in NetworkLoggingTest
91         Intent batchIntent = new Intent(ACTION_NETWORK_LOGS_AVAILABLE);
92         batchIntent.putExtra(EXTRA_NETWORK_LOGS_BATCH_TOKEN, batchToken);
93 
94         DeviceOwnerHelper.sendBroadcastToTestAppReceivers(context, batchIntent);
95     }
96 
97     @Override
onOperationSafetyStateChanged(Context context, int reason, boolean isSafe)98     public void onOperationSafetyStateChanged(Context context, int reason, boolean isSafe) {
99         OperationSafetyChangedEvent event = new OperationSafetyChangedEvent(reason, isSafe);
100         Log.d(TAG, "onOperationSafetyStateChanged() on user " + context.getUserId() + ": " + event);
101 
102         Intent intent = OperationSafetyChangedCallback.intentFor(event);
103 
104         DeviceOwnerHelper.sendBroadcastToTestAppReceivers(context, intent);
105     }
106 
sendUserBroadcast(Context context, String action, UserHandle userHandle)107     private void sendUserBroadcast(Context context, String action, UserHandle userHandle) {
108         Log.d(TAG, "sendUserBroadcast(): action=" + action + ", user=" + userHandle);
109         Intent intent = new Intent(action).putExtra(EXTRA_USER_HANDLE, userHandle);
110 
111         // NOTE: broadcast locally as user-related tests on headless system user always run on
112         // system user, as current user is stopped on switch.
113         LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
114     }
115 }
116