1 /* 2 * 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 com.android.settings.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 25 import androidx.annotation.Nullable; 26 27 import com.android.internal.annotations.VisibleForTesting; 28 29 /** A wrapper for the SafetyCenterManager system service. */ 30 public class SafetyCenterManagerWrapper { 31 32 /** 33 * Tag for logging. 34 * 35 * <p>The tag is restricted to 23 characters (the maximum allowed for Android logging). 36 */ 37 private static final String TAG = "SafetyCenterManagerWrap"; 38 39 @VisibleForTesting 40 public static SafetyCenterManagerWrapper sInstance; 41 SafetyCenterManagerWrapper()42 private SafetyCenterManagerWrapper() {} 43 44 /** Returns an instance of {@link SafetyCenterManagerWrapper}. */ get()45 public static SafetyCenterManagerWrapper get() { 46 if (sInstance == null) { 47 sInstance = new SafetyCenterManagerWrapper(); 48 } 49 return sInstance; 50 } 51 52 /** Sets the latest safety source data for Safety Center. */ setSafetySourceData(Context context, String safetySourceId, @Nullable SafetySourceData safetySourceData, SafetyEvent safetyEvent)53 public void setSafetySourceData(Context context, String safetySourceId, 54 @Nullable SafetySourceData safetySourceData, 55 SafetyEvent safetyEvent) { 56 SafetyCenterManager safetyCenterManager = 57 context.getSystemService(SafetyCenterManager.class); 58 59 if (safetyCenterManager == null) { 60 Log.e(TAG, "System service SAFETY_CENTER_SERVICE (SafetyCenterManager) is null"); 61 return; 62 } 63 64 try { 65 safetyCenterManager.setSafetySourceData( 66 safetySourceId, 67 safetySourceData, 68 safetyEvent 69 ); 70 } catch (Exception e) { 71 Log.e(TAG, "Failed to send SafetySourceData", e); 72 return; 73 } 74 } 75 76 /** Returns true is SafetyCenter page is enabled, false otherwise. */ isEnabled(Context context)77 public boolean isEnabled(Context context) { 78 if (context == null) { 79 Log.e(TAG, "Context is null at SafetyCenterManagerWrapper#isEnabled"); 80 return false; 81 } 82 SafetyCenterManager safetyCenterManager = 83 context.getSystemService(SafetyCenterManager.class); 84 if (safetyCenterManager == null) { 85 Log.w(TAG, "System service SAFETY_CENTER_SERVICE (SafetyCenterManager) is null"); 86 return false; 87 } 88 try { 89 return safetyCenterManager.isSafetyCenterEnabled(); 90 } catch (RuntimeException e) { 91 Log.e(TAG, "Calling isSafetyCenterEnabled failed.", e); 92 return false; 93 } 94 } 95 } 96