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.app.PendingIntent
20 import android.app.PendingIntent.FLAG_IMMUTABLE
21 import android.app.PendingIntent.FLAG_UPDATE_CURRENT
22 import android.content.Context
23 import android.content.Intent
24 import android.health.connect.HealthConnectManager
25 import android.safetycenter.SafetyEvent
26 import android.safetycenter.SafetySourceData
27 import android.safetycenter.SafetySourceData.SEVERITY_LEVEL_UNSPECIFIED
28 import android.safetycenter.SafetySourceStatus
29 import com.android.healthconnect.controller.R
30 import com.android.healthconnect.controller.utils.FeatureUtils
31 import javax.inject.Inject
32 
33 class HealthConnectSafetySource
34 @Inject
35 constructor(
36     private val featureUtils: FeatureUtils,
37     private val safetyCenterManagerWrapper: SafetyCenterManagerWrapper
38 ) {
39 
setSafetySourceDatanull40     fun setSafetySourceData(context: Context, safetyEvent: SafetyEvent) {
41         if (!safetyCenterManagerWrapper.isEnabled(context)) {
42             return
43         }
44         if (!featureUtils.isEntryPointsEnabled()) {
45             safetyCenterManagerWrapper.setSafetySourceData(
46                 context, HEALTH_CONNECT_SOURCE_ID, null, safetyEvent)
47             return
48         }
49 
50         val safetySourceData =
51             SafetySourceData.Builder()
52                 .setStatus(
53                     SafetySourceStatus.Builder(
54                             context.getString(R.string.app_label),
55                             context.getString(R.string.health_connect_summary),
56                             SEVERITY_LEVEL_UNSPECIFIED)
57                         .setPendingIntent(
58                             PendingIntent.getActivity(
59                                 context,
60                                 /* requestCode= */ 0,
61                                 Intent(HealthConnectManager.ACTION_HEALTH_HOME_SETTINGS),
62                                 FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE))
63                         .build(),
64                 )
65                 .build()
66 
67         safetyCenterManagerWrapper.setSafetySourceData(
68             context, HEALTH_CONNECT_SOURCE_ID, safetySourceData, safetyEvent)
69     }
70 
71     /** Companion object for [HealthConnectPrivacySource]. */
72     companion object {
73         /** Source id for safety center source for health connect. */
74         const val HEALTH_CONNECT_SOURCE_ID = "AndroidHealthConnect"
75     }
76 }
77