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.Context 20 import android.safetycenter.SafetyCenterManager 21 import android.safetycenter.SafetyEvent 22 import android.safetycenter.SafetySourceData 23 import android.util.Log 24 import javax.inject.Inject 25 26 /** A wrapper for the SafetyCenterManager system service. */ 27 class SafetyCenterManagerWrapper @Inject constructor() { 28 29 /** Sets the latest safety source data for Safety Center. */ setSafetySourceDatanull30 fun setSafetySourceData( 31 context: Context, 32 safetySourceId: String, 33 safetySourceData: SafetySourceData?, 34 safetyEvent: SafetyEvent 35 ) { 36 val safetyCenterManager = context.getSystemService(SafetyCenterManager::class.java) 37 if (safetyCenterManager == null) { 38 Log.e(TAG, "System service SAFETY_CENTER_SERVICE (SafetyCenterManager) is null") 39 return 40 } 41 try { 42 safetyCenterManager.setSafetySourceData(safetySourceId, safetySourceData, safetyEvent) 43 } catch (e: Exception) { 44 Log.e(TAG, "Failed to send SafetySourceData", e) 45 return 46 } 47 } 48 49 /** Returns true is SafetyCenter page is enabled, false otherwise. */ isEnablednull50 fun isEnabled(context: Context?): Boolean { 51 if (context == null) { 52 Log.e(TAG, "Context is null at SafetyCenterManagerWrapper#isEnabled") 53 return false 54 } 55 val safetyCenterManager = context.getSystemService(SafetyCenterManager::class.java) 56 if (safetyCenterManager == null) { 57 Log.w(TAG, "System service SAFETY_CENTER_SERVICE (SafetyCenterManager) is null") 58 return false 59 } 60 return try { 61 safetyCenterManager.isSafetyCenterEnabled 62 } catch (e: RuntimeException) { 63 Log.e(TAG, "Calling isSafetyCenterEnabled failed.", e) 64 false 65 } 66 } 67 68 companion object { 69 /** 70 * Tag for logging. 71 * 72 * The tag is restricted to 23 characters (the maximum allowed for Android logging). 73 */ 74 private const val TAG = "SafetyCenterManagerWrap" 75 } 76 } 77