• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     private val onFailureCodeAction: ((Int, CharSequence?) -> Unit)?
32 ) : CompanionDeviceManager.Callback(),
33     InvocationTracker<RecordingCallback.CallbackInvocation> by container {
34 
35     constructor(
36         onDeviceFoundAction: ((IntentSender) -> Unit)? = null,
37         onAssociationPendingAction: ((IntentSender) -> Unit)? = null,
38         onAssociationCreatedAction: ((AssociationInfo) -> Unit)? = null,
39         onFailureAction: ((CharSequence?) -> Unit)? = null,
40         onFailureCodeAction: ((Int, CharSequence?) -> Unit)? = null
41     ) : this(
42         InvocationContainer(),
43             onDeviceFoundAction,
44             onAssociationPendingAction,
45             onAssociationCreatedAction,
46             onFailureAction,
47             onFailureCodeAction
48         )
49 
onDeviceFoundnull50     override fun onDeviceFound(intentSender: IntentSender) {
51         logAndRecordInvocation(OnDeviceFound(intentSender))
52         onDeviceFoundAction?.invoke(intentSender)
53     }
54 
onAssociationPendingnull55     override fun onAssociationPending(intentSender: IntentSender) {
56         logAndRecordInvocation(OnAssociationPending(intentSender))
57         onAssociationPendingAction?.invoke(intentSender)
58     }
59 
onAssociationCreatednull60     override fun onAssociationCreated(associationInfo: AssociationInfo) {
61         logAndRecordInvocation(OnAssociationCreated(associationInfo))
62         onAssociationCreatedAction?.invoke(associationInfo)
63     }
64 
onFailurenull65     override fun onFailure(error: CharSequence?) {
66         logAndRecordInvocation(OnFailure(error))
67         onFailureAction?.invoke(error)
68     }
69 
onFailurenull70     override fun onFailure(resultCode: Int, error: CharSequence?) {
71         logAndRecordInvocation(OnFailureCode(resultCode, error))
72         onFailureCodeAction?.invoke(resultCode, error)
73     }
74 
logAndRecordInvocationnull75     private fun logAndRecordInvocation(invocation: CallbackInvocation) {
76         Log.d(TAG, "Callback: $invocation")
77         recordInvocation(invocation)
78     }
79 
80     sealed interface CallbackInvocation
81     data class OnDeviceFound(val intentSender: IntentSender) : CallbackInvocation
82     data class OnAssociationPending(val intentSender: IntentSender) : CallbackInvocation
83     data class OnAssociationCreated(val associationInfo: AssociationInfo) : CallbackInvocation
84     data class OnFailure(val error: CharSequence?) : CallbackInvocation
85     data class OnFailureCode(val resultCode: Int, val char: CharSequence?) : CallbackInvocation
86 }
87