• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 com.android.healthconnect.controller.safetycenter
18 
19 import android.content.BroadcastReceiver
20 import android.content.Context
21 import android.content.Intent
22 import android.content.Intent.ACTION_BOOT_COMPLETED
23 import android.safetycenter.SafetyCenterManager
24 import android.safetycenter.SafetyCenterManager.ACTION_REFRESH_SAFETY_SOURCES
25 import android.safetycenter.SafetyCenterManager.EXTRA_REFRESH_SAFETY_SOURCE_IDS
26 import android.safetycenter.SafetyEvent
27 import android.safetycenter.SafetyEvent.SAFETY_EVENT_TYPE_DEVICE_REBOOTED
28 import android.safetycenter.SafetyEvent.SAFETY_EVENT_TYPE_REFRESH_REQUESTED
29 import android.util.Log
30 import com.android.healthconnect.controller.safetycenter.HealthConnectSafetySource.Companion.HEALTH_CONNECT_SOURCE_ID
31 import dagger.hilt.android.AndroidEntryPoint
32 import javax.inject.Inject
33 
34 @AndroidEntryPoint(BroadcastReceiver::class)
35 class SafetySourceBroadcastReceiver : Hilt_SafetySourceBroadcastReceiver() {
36 
37     @Inject lateinit var safetySource: HealthConnectSafetySource
38     @Inject lateinit var safetyCenterManagerWrapper: SafetyCenterManagerWrapper
39 
onReceivenull40     override fun onReceive(context: Context, intent: Intent) {
41         Log.d(TAG, "onReceive() called with: context = $context, intent = $intent")
42         super.onReceive(context, intent)
43         if (!safetyCenterManagerWrapper.isEnabled(context)) {
44             return
45         }
46         Log.i(TAG, "onReceive: ${intent.action}")
47         when (intent.action) {
48             ACTION_BOOT_COMPLETED -> refreshAllSafetySources(context)
49             ACTION_REFRESH_SAFETY_SOURCES -> {
50                 val sourceIdsExtra = intent.getStringArrayExtra(EXTRA_REFRESH_SAFETY_SOURCE_IDS)
51                 val refreshBroadcastId =
52                     intent.getStringExtra(
53                         SafetyCenterManager.EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID)
54                 Log.i(TAG, "onReceive: $sourceIdsExtra \n $refreshBroadcastId")
55                 if (sourceIdsExtra != null &&
56                     sourceIdsExtra.isNotEmpty() &&
57                     refreshBroadcastId != null) {
58                     val safetyEvent =
59                         SafetyEvent.Builder(SAFETY_EVENT_TYPE_REFRESH_REQUESTED)
60                             .setRefreshBroadcastId(refreshBroadcastId)
61                             .build()
62                     refreshSafetySources(context, sourceIdsExtra.toList(), safetyEvent)
63                 }
64                 return
65             }
66             else -> return
67         }
68     }
69 
refreshSafetySourcesnull70     private fun refreshSafetySources(
71         context: Context,
72         sourceIds: List<String>,
73         safetyEvent: SafetyEvent
74     ) {
75         if (HEALTH_CONNECT_SOURCE_ID in sourceIds) {
76             safetySource.setSafetySourceData(context, safetyEvent)
77         }
78     }
79 
refreshAllSafetySourcesnull80     private fun refreshAllSafetySources(context: Context) {
81         safetySource.setSafetySourceData(
82             context, SafetyEvent.Builder(SAFETY_EVENT_TYPE_DEVICE_REBOOTED).build())
83     }
84 
85     companion object {
86         private const val TAG = "SafetySourceBroadcastRe"
87     }
88 }
89