• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2022 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.nearby.multidevices.fastpair.seeker.data
18 
19 import android.content.BroadcastReceiver
20 import android.content.Context
21 import android.content.Intent
22 import android.content.IntentFilter
23 import android.nearby.fastpair.seeker.ACTION_RESET_TEST_DATA_CACHE
24 import android.nearby.fastpair.seeker.ACTION_SEND_ACCOUNT_KEY_DEVICE_METADATA
25 import android.nearby.fastpair.seeker.ACTION_SEND_ANTISPOOF_KEY_DEVICE_METADATA
26 import android.nearby.fastpair.seeker.ACTION_WRITE_ACCOUNT_KEY_DEVICE_METADATA
27 import android.nearby.fastpair.seeker.DATA_JSON_STRING_KEY
28 import android.nearby.fastpair.seeker.DATA_MODEL_ID_STRING_KEY
29 import android.nearby.fastpair.seeker.FastPairTestDataCache
30 import android.util.Log
31 
32 /** Manage local FastPairTestDataCache and send to/sync from the remote cache in data provider. */
33 class FastPairTestDataManager(private val context: Context) : BroadcastReceiver() {
34     val testDataCache = FastPairTestDataCache()
35     var listener: EventListener? = null
36 
37     /** Puts a model id to FastPairAntispoofKeyDeviceMetadata pair into local and remote cache.
38      *
39      * @param modelId a string of model id to be associated with.
40      * @param json a string of FastPairAntispoofKeyDeviceMetadata JSON object.
41      */
42     fun sendAntispoofKeyDeviceMetadata(modelId: String, json: String) {
43         Intent().also { intent ->
44             intent.action = ACTION_SEND_ANTISPOOF_KEY_DEVICE_METADATA
45             intent.putExtra(DATA_MODEL_ID_STRING_KEY, modelId)
46             intent.putExtra(DATA_JSON_STRING_KEY, json)
47             context.sendBroadcast(intent)
48         }
49         testDataCache.putAntispoofKeyDeviceMetadata(modelId, json)
50     }
51 
52     /** Puts account key device metadata array to local and remote cache.
53      *
54      * @param json a string of FastPairAccountKeyDeviceMetadata JSON array.
55      */
56     fun sendAccountKeyDeviceMetadataJsonArray(json: String) {
57         Intent().also { intent ->
58             intent.action = ACTION_SEND_ACCOUNT_KEY_DEVICE_METADATA
59             intent.putExtra(DATA_JSON_STRING_KEY, json)
60             context.sendBroadcast(intent)
61         }
62         testDataCache.putAccountKeyDeviceMetadataJsonArray(json)
63     }
64 
65     /** Clears local and remote cache. */
66     fun sendResetCache() {
67         context.sendBroadcast(Intent(ACTION_RESET_TEST_DATA_CACHE))
68         testDataCache.reset()
69     }
70 
71     /**
72      * Callback method for receiving Intent broadcast from FastPairTestDataProvider.
73      *
74      * See [BroadcastReceiver#onReceive].
75      *
76      * @param context the Context in which the receiver is running.
77      * @param intent the Intent being received.
78      */
79     override fun onReceive(context: Context, intent: Intent) {
80         when (intent.action) {
81             ACTION_WRITE_ACCOUNT_KEY_DEVICE_METADATA -> {
82                 Log.d(TAG, "ACTION_WRITE_ACCOUNT_KEY_DEVICE_METADATA received!")
83                 val json = intent.getStringExtra(DATA_JSON_STRING_KEY)!!
84                 testDataCache.putAccountKeyDeviceMetadataJsonObject(json)
85                 listener?.onManageFastPairAccountDevice(json)
86             }
87             else -> Log.d(TAG, "Unknown action received!")
88         }
89     }
90 
91     fun registerDataReceiveListener(listener: EventListener) {
92         this.listener = listener
93         val bondStateFilter = IntentFilter(ACTION_WRITE_ACCOUNT_KEY_DEVICE_METADATA)
94         context.registerReceiver(this, bondStateFilter)
95     }
96 
97     fun unregisterDataReceiveListener() {
98         this.listener = null
99         context.unregisterReceiver(this)
100     }
101 
102     /** Interface for listening the data receive from the remote cache in data provider. */
103     interface EventListener {
104         /** Reports a FastPairAccountKeyDeviceMetadata write into the cache.
105          *
106          * @param json the FastPairAccountKeyDeviceMetadata as JSON object string.
107          */
108         fun onManageFastPairAccountDevice(json: String)
109     }
110 
111     companion object {
112         private const val TAG = "FastPairTestDataManager"
113     }
114 }