1 /* 2 * Copyright (C) 2021 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.eventlib.premade 17 18 import android.app.admin.DeviceAdminReceiver 19 import android.content.Context 20 import android.content.Intent 21 import android.net.Uri 22 import android.os.PersistableBundle 23 import android.os.UserHandle 24 import com.android.eventlib.events.broadcastreceivers.BroadcastReceivedEvent 25 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminBugreportFailedEvent 26 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminBugreportSharedEvent 27 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminBugreportSharingDeclinedEvent 28 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminChoosePrivateKeyAliasEvent 29 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminDisableRequestedEvent 30 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminDisabledEvent 31 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminEnabledEvent 32 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminLockTaskModeEnteringEvent 33 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminLockTaskModeExitingEvent 34 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminNetworkLogsAvailableEvent 35 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminOperationSafetyStateChangedEvent 36 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminPasswordChangedEvent 37 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminPasswordExpiringEvent 38 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminPasswordFailedEvent 39 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminPasswordSucceededEvent 40 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminProfileProvisioningCompleteEvent 41 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminReadyForUserInitializationEvent 42 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminSecurityLogsAvailableEvent 43 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminSystemUpdatePendingEvent 44 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminTransferAffiliatedProfileOwnershipCompleteEvent 45 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminTransferOwnershipCompleteEvent 46 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminUserAddedEvent 47 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminUserRemovedEvent 48 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminUserStartedEvent 49 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminUserStoppedEvent 50 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminUserSwitchedEvent 51 52 /** Implementation of [DeviceAdminReceiver] which logs events in response to callbacks. */ 53 open class EventLibDeviceAdminReceiver : DeviceAdminReceiver() { 54 private var mOverrideDeviceAdminReceiverClassName: String? = null setOverrideDeviceAdminReceiverClassNamenull55 fun setOverrideDeviceAdminReceiverClassName(overrideDeviceAdminReceiverClassName: String) { 56 mOverrideDeviceAdminReceiverClassName = overrideDeviceAdminReceiverClassName 57 } 58 59 /** 60 * Get the class name for this [DeviceAdminReceiver]. 61 * 62 * 63 * This will account for the name being overridden. 64 */ classNamenull65 fun className(): String = 66 mOverrideDeviceAdminReceiverClassName ?: EventLibDeviceAdminReceiver::class.java.name 67 68 override fun onEnabled(context: Context, intent: Intent) { 69 val logger = DeviceAdminEnabledEvent.logger(this, context, intent) 70 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 71 logger.log() 72 super.onEnabled(context, intent) 73 } 74 onDisableRequestednull75 override fun onDisableRequested(context: Context, intent: Intent): CharSequence? { 76 val logger = DeviceAdminDisableRequestedEvent.logger(this, context, intent) 77 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 78 logger.log() 79 return super.onDisableRequested(context, intent) 80 } 81 onDisablednull82 override fun onDisabled(context: Context, intent: Intent) { 83 val logger = DeviceAdminDisabledEvent.logger(this, context, intent) 84 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 85 logger.log() 86 super.onDisabled(context, intent) 87 } 88 onPasswordChangednull89 override fun onPasswordChanged(context: Context, intent: Intent) { 90 val logger = DeviceAdminPasswordChangedEvent.logger(this, context, intent) 91 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 92 logger.log() 93 super.onPasswordChanged(context, intent) 94 } 95 onPasswordChangednull96 override fun onPasswordChanged(context: Context, intent: Intent, user: UserHandle) { 97 val logger = DeviceAdminPasswordChangedEvent.logger(this, context, intent) 98 logger.setUserHandle(user) 99 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 100 logger.log() 101 } 102 onPasswordFailednull103 override fun onPasswordFailed(context: Context, intent: Intent) { 104 val logger = DeviceAdminPasswordFailedEvent.logger(this, context, intent) 105 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 106 logger.log() 107 super.onPasswordFailed(context, intent) 108 } 109 onPasswordFailednull110 override fun onPasswordFailed(context: Context, intent: Intent, user: UserHandle) { 111 val logger = DeviceAdminPasswordFailedEvent.logger(this, context, intent) 112 logger.setUserHandle(user) 113 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 114 logger.log() 115 } 116 onPasswordSucceedednull117 override fun onPasswordSucceeded(context: Context, intent: Intent) { 118 val logger = DeviceAdminPasswordSucceededEvent.logger(this, context, intent) 119 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 120 logger.log() 121 super.onPasswordSucceeded(context, intent) 122 } 123 onPasswordSucceedednull124 override fun onPasswordSucceeded(context: Context, intent: Intent, user: UserHandle) { 125 val logger = DeviceAdminPasswordSucceededEvent.logger(this, context, intent) 126 logger.setUserHandle(user) 127 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 128 logger.log() 129 } 130 onPasswordExpiringnull131 override fun onPasswordExpiring(context: Context, intent: Intent) { 132 val logger = DeviceAdminPasswordExpiringEvent.logger(this, context, intent) 133 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 134 logger.log() 135 super.onPasswordExpiring(context, intent) 136 } 137 onPasswordExpiringnull138 override fun onPasswordExpiring(context: Context, intent: Intent, user: UserHandle) { 139 val logger = DeviceAdminPasswordExpiringEvent.logger(this, context, intent) 140 logger.setUserHandle(user) 141 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 142 logger.log() 143 } 144 onProfileProvisioningCompletenull145 override fun onProfileProvisioningComplete(context: Context, intent: Intent) { 146 val logger = DeviceAdminProfileProvisioningCompleteEvent.logger(this, context, intent) 147 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 148 logger.log() 149 super.onProfileProvisioningComplete(context, intent) 150 } 151 onReadyForUserInitializationnull152 override fun onReadyForUserInitialization(context: Context, intent: Intent) { 153 val logger = DeviceAdminReadyForUserInitializationEvent.logger(this, context, intent) 154 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 155 logger.log() 156 super.onReadyForUserInitialization(context, intent) 157 } 158 onLockTaskModeEnteringnull159 override fun onLockTaskModeEntering(context: Context, intent: Intent, pkg: String) { 160 val logger = DeviceAdminLockTaskModeEnteringEvent.logger(this, context, intent, pkg) 161 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 162 logger.log() 163 super.onLockTaskModeEntering(context, intent, pkg) 164 } 165 onLockTaskModeExitingnull166 override fun onLockTaskModeExiting(context: Context, intent: Intent) { 167 val logger = DeviceAdminLockTaskModeExitingEvent.logger(this, context, intent) 168 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 169 logger.log() 170 super.onLockTaskModeExiting(context, intent) 171 } 172 onChoosePrivateKeyAliasnull173 override fun onChoosePrivateKeyAlias( 174 context: Context, intent: Intent, uid: Int, uri: Uri?, 175 alias: String? 176 ): String? { 177 val logger = DeviceAdminChoosePrivateKeyAliasEvent 178 .logger(this, context, intent, uid, uri, alias) 179 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 180 logger.log() 181 182 // TODO(b/198280332) Allow TestApp to return values for methods. 183 super.onChoosePrivateKeyAlias(context, intent, uid, uri, alias) 184 return uri?.getQueryParameter("alias") 185 } 186 onSystemUpdatePendingnull187 override fun onSystemUpdatePending(context: Context, intent: Intent, receivedTime: Long) { 188 val logger = DeviceAdminSystemUpdatePendingEvent.logger(this, context, intent, receivedTime) 189 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 190 logger.log() 191 super.onSystemUpdatePending(context, intent, receivedTime) 192 } 193 onBugreportSharingDeclinednull194 override fun onBugreportSharingDeclined(context: Context, intent: Intent) { 195 val logger = DeviceAdminBugreportSharingDeclinedEvent.logger(this, context, intent) 196 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 197 logger.log() 198 super.onBugreportSharingDeclined(context, intent) 199 } 200 onBugreportSharednull201 override fun onBugreportShared(context: Context, intent: Intent, bugreportHash: String) { 202 val logger = DeviceAdminBugreportSharedEvent.logger(this, context, intent, bugreportHash) 203 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 204 logger.log() 205 super.onBugreportSharingDeclined(context, intent) 206 } 207 onBugreportFailednull208 override fun onBugreportFailed(context: Context, intent: Intent, failureCode: Int) { 209 val logger = DeviceAdminBugreportFailedEvent.logger(this, context, intent, failureCode) 210 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 211 logger.log() 212 super.onBugreportFailed(context, intent, failureCode) 213 } 214 onSecurityLogsAvailablenull215 override fun onSecurityLogsAvailable(context: Context, intent: Intent) { 216 val logger = DeviceAdminSecurityLogsAvailableEvent.logger(this, context, intent) 217 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 218 logger.log() 219 super.onSecurityLogsAvailable(context, intent) 220 } 221 onNetworkLogsAvailablenull222 override fun onNetworkLogsAvailable( 223 context: Context, intent: Intent, batchToken: Long, 224 networkLogsCount: Int 225 ) { 226 val logger = DeviceAdminNetworkLogsAvailableEvent 227 .logger(this, context, intent, batchToken, networkLogsCount) 228 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 229 logger.log() 230 super.onNetworkLogsAvailable(context, intent, batchToken, networkLogsCount) 231 } 232 onUserAddednull233 override fun onUserAdded(context: Context, intent: Intent, addedUser: UserHandle) { 234 val logger = DeviceAdminUserAddedEvent.logger(this, context, intent, addedUser) 235 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 236 logger.log() 237 super.onUserAdded(context, intent, addedUser) 238 } 239 onUserRemovednull240 override fun onUserRemoved(context: Context, intent: Intent, removedUser: UserHandle) { 241 val logger = DeviceAdminUserRemovedEvent.logger(this, context, intent, removedUser) 242 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 243 logger.log() 244 super.onUserRemoved(context, intent, removedUser) 245 } 246 onUserStartednull247 override fun onUserStarted(context: Context, intent: Intent, startedUser: UserHandle) { 248 val logger = DeviceAdminUserStartedEvent.logger(this, context, intent, startedUser) 249 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 250 logger.log() 251 super.onUserStarted(context, intent, startedUser) 252 } 253 onUserStoppednull254 override fun onUserStopped(context: Context, intent: Intent, stoppedUser: UserHandle) { 255 val logger = DeviceAdminUserStoppedEvent.logger(this, context, intent, stoppedUser) 256 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 257 logger.log() 258 super.onUserStopped(context, intent, stoppedUser) 259 } 260 onUserSwitchednull261 override fun onUserSwitched(context: Context, intent: Intent, switchedUser: UserHandle) { 262 val logger = DeviceAdminUserSwitchedEvent.logger(this, context, intent, switchedUser) 263 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 264 logger.log() 265 super.onUserRemoved(context, intent, switchedUser) 266 } 267 onTransferOwnershipCompletenull268 override fun onTransferOwnershipComplete(context: Context, bundle: PersistableBundle?) { 269 val logger = DeviceAdminTransferOwnershipCompleteEvent.logger(this, context, bundle) 270 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 271 logger.log() 272 super.onTransferOwnershipComplete(context, bundle) 273 } 274 onTransferAffiliatedProfileOwnershipCompletenull275 override fun onTransferAffiliatedProfileOwnershipComplete(context: Context, user: UserHandle) { 276 val logger = DeviceAdminTransferAffiliatedProfileOwnershipCompleteEvent 277 .logger(this, context, user) 278 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 279 logger.log() 280 super.onTransferAffiliatedProfileOwnershipComplete(context, user) 281 } 282 onOperationSafetyStateChangednull283 override fun onOperationSafetyStateChanged(context: Context, reason: Int, isSafe: Boolean) { 284 val logger = DeviceAdminOperationSafetyStateChangedEvent 285 .logger(this, context, reason, isSafe) 286 mOverrideDeviceAdminReceiverClassName?.let { logger.setDeviceAdminReceiver(it) } 287 logger.log() 288 super.onOperationSafetyStateChanged(context, reason, isSafe) 289 } 290 onReceivenull291 override fun onReceive(context: Context, intent: Intent) { 292 val logger = BroadcastReceivedEvent.logger(this, context, intent, resultCode) 293 mOverrideDeviceAdminReceiverClassName?.let { logger.setBroadcastReceiver(it) } 294 logger.log() 295 super.onReceive(context, intent) 296 } 297 } 298