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 17 package android.companion.cts.common 18 19 import android.companion.AssociationInfo 20 import android.companion.CompanionDeviceManager 21 import android.content.IntentSender 22 import android.util.Log 23 24 class RecordingCallback 25 private constructor( 26 container: InvocationContainer<CallbackInvocation>, 27 private val onDeviceFoundAction: ((IntentSender) -> Unit)?, 28 private val onAssociationPendingAction: ((IntentSender) -> Unit)?, 29 private val onAssociationCreatedAction: ((AssociationInfo) -> Unit)?, 30 private val onFailureAction: ((CharSequence?) -> Unit)? 31 ) : CompanionDeviceManager.Callback(), 32 InvocationTracker<RecordingCallback.CallbackInvocation> by container { 33 34 constructor( 35 onDeviceFoundAction: ((IntentSender) -> Unit)? = null, 36 onAssociationPendingAction: ((IntentSender) -> Unit)? = null, 37 onAssociationCreatedAction: ((AssociationInfo) -> Unit)? = null, 38 onFailureAction: ((CharSequence?) -> Unit)? = null 39 ) : this(InvocationContainer(), 40 onDeviceFoundAction, 41 onAssociationPendingAction, 42 onAssociationCreatedAction, 43 onFailureAction) 44 onDeviceFoundnull45 override fun onDeviceFound(intentSender: IntentSender) { 46 logAndRecordInvocation(OnDeviceFound(intentSender)) 47 onDeviceFoundAction?.invoke(intentSender) 48 } 49 onAssociationPendingnull50 override fun onAssociationPending(intentSender: IntentSender) { 51 logAndRecordInvocation(OnAssociationPending(intentSender)) 52 onAssociationPendingAction?.invoke(intentSender) 53 } 54 onAssociationCreatednull55 override fun onAssociationCreated(associationInfo: AssociationInfo) { 56 logAndRecordInvocation(OnAssociationCreated(associationInfo)) 57 onAssociationCreatedAction?.invoke(associationInfo) 58 } 59 onFailurenull60 override fun onFailure(error: CharSequence?) { 61 logAndRecordInvocation(OnFailure(error)) 62 onFailureAction?.invoke(error) 63 } 64 logAndRecordInvocationnull65 private fun logAndRecordInvocation(invocation: CallbackInvocation) { 66 Log.d(TAG, "Callback: $invocation") 67 recordInvocation(invocation) 68 } 69 70 sealed interface CallbackInvocation 71 data class OnDeviceFound(val intentSender: IntentSender) : CallbackInvocation 72 data class OnAssociationPending(val intentSender: IntentSender) : CallbackInvocation 73 data class OnAssociationCreated(val associationInfo: AssociationInfo) : CallbackInvocation 74 data class OnFailure(val error: CharSequence?) : CallbackInvocation 75 }