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