Home | History | Annotate | Download | only in deviceowner
      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 androidx.localbroadcastmanager.content.LocalBroadcastManager;
     24 
     25 public class BasicAdminReceiver extends DeviceAdminReceiver {
     26 
     27     final static String ACTION_USER_ADDED = "com.android.cts.deviceowner.action.USER_ADDED";
     28     final static String ACTION_USER_REMOVED = "com.android.cts.deviceowner.action.USER_REMOVED";
     29     final static String ACTION_USER_STARTED = "com.android.cts.deviceowner.action.USER_STARTED";
     30     final static String ACTION_USER_STOPPED = "com.android.cts.deviceowner.action.USER_STOPPED";
     31     final static String ACTION_USER_SWITCHED = "com.android.cts.deviceowner.action.USER_SWITCHED";
     32     final static String EXTRA_USER_HANDLE = "com.android.cts.deviceowner.extra.USER_HANDLE";
     33     final static String ACTION_NETWORK_LOGS_AVAILABLE =
     34             "com.android.cts.deviceowner.action.ACTION_NETWORK_LOGS_AVAILABLE";
     35     final static String EXTRA_NETWORK_LOGS_BATCH_TOKEN =
     36             "com.android.cts.deviceowner.extra.NETWORK_LOGS_BATCH_TOKEN";
     37 
     38     public static ComponentName getComponentName(Context context) {
     39         return new ComponentName(context, BasicAdminReceiver.class);
     40     }
     41 
     42     @Override
     43     public void onUserAdded(Context context, Intent intent, UserHandle userHandle) {
     44         super.onUserAdded(context, intent, userHandle);
     45         sendUserBroadcast(context, ACTION_USER_ADDED, userHandle);
     46     }
     47 
     48     @Override
     49     public void onUserRemoved(Context context, Intent intent, UserHandle userHandle) {
     50         super.onUserRemoved(context, intent, userHandle);
     51         sendUserBroadcast(context, ACTION_USER_REMOVED, userHandle);
     52     }
     53 
     54     @Override
     55     public void onUserStarted(Context context, Intent intent, UserHandle userHandle) {
     56         super.onUserStarted(context, intent, userHandle);
     57         sendUserBroadcast(context, ACTION_USER_STARTED, userHandle);
     58     }
     59 
     60     @Override
     61     public void onUserStopped(Context context, Intent intent, UserHandle userHandle) {
     62         super.onUserStopped(context, intent, userHandle);
     63         sendUserBroadcast(context, ACTION_USER_STOPPED, userHandle);
     64     }
     65 
     66     @Override
     67     public void onUserSwitched(Context context, Intent intent, UserHandle userHandle) {
     68         super.onUserSwitched(context, intent, userHandle);
     69         sendUserBroadcast(context, ACTION_USER_SWITCHED, userHandle);
     70     }
     71 
     72     @Override
     73     public void onNetworkLogsAvailable(Context context, Intent intent, long batchToken,
     74             int networkLogsCount) {
     75         super.onNetworkLogsAvailable(context, intent, batchToken, networkLogsCount);
     76         // send the broadcast, the rest of the test happens in NetworkLoggingTest
     77         Intent batchIntent = new Intent(ACTION_NETWORK_LOGS_AVAILABLE);
     78         batchIntent.putExtra(EXTRA_NETWORK_LOGS_BATCH_TOKEN, batchToken);
     79         LocalBroadcastManager.getInstance(context).sendBroadcast(batchIntent);
     80     }
     81 
     82     private void sendUserBroadcast(Context context, String action,
     83             UserHandle userHandle) {
     84         Intent intent = new Intent(action);
     85         intent.putExtra(EXTRA_USER_HANDLE, userHandle);
     86         LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
     87     }
     88 }
     89