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 static android.content.Intent.ACTION_BOOT_COMPLETED;
20 import static android.safetycenter.SafetyCenterManager.ACTION_REFRESH_SAFETY_SOURCES;
21 import static android.safetycenter.SafetyCenterManager.EXTRA_REFRESH_SAFETY_SOURCE_IDS;
22 import static android.safetycenter.SafetyEvent.SAFETY_EVENT_TYPE_DEVICE_REBOOTED;
23 import static android.safetycenter.SafetyEvent.SAFETY_EVENT_TYPE_REFRESH_REQUESTED;
24 
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.safetycenter.SafetyCenterManager;
29 import android.safetycenter.SafetyEvent;
30 
31 import com.android.settings.privatespace.PrivateSpaceSafetySource;
32 import com.android.settings.security.ScreenLockPreferenceDetailsUtils;
33 
34 import com.google.common.collect.ImmutableList;
35 
36 import java.util.List;
37 
38 /** Broadcast receiver for handling requests from Safety Center for fresh data. */
39 public class SafetySourceBroadcastReceiver extends BroadcastReceiver {
40 
41     private static final SafetyEvent EVENT_DEVICE_REBOOTED =
42             new SafetyEvent.Builder(SAFETY_EVENT_TYPE_DEVICE_REBOOTED).build();
43 
44     @Override
onReceive(Context context, Intent intent)45     public void onReceive(Context context, Intent intent) {
46         if (!SafetyCenterManagerWrapper.get().isEnabled(context)) {
47             return;
48         }
49 
50         if (ACTION_REFRESH_SAFETY_SOURCES.equals(intent.getAction())) {
51             String[] sourceIdsExtra =
52                     intent.getStringArrayExtra(EXTRA_REFRESH_SAFETY_SOURCE_IDS);
53             final String refreshBroadcastId = intent.getStringExtra(
54                     SafetyCenterManager.EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID);
55             if (sourceIdsExtra != null && sourceIdsExtra.length > 0 && refreshBroadcastId != null) {
56                 final SafetyEvent safetyEvent = new SafetyEvent.Builder(
57                         SAFETY_EVENT_TYPE_REFRESH_REQUESTED)
58                         .setRefreshBroadcastId(refreshBroadcastId).build();
59                 refreshSafetySources(
60                         context,
61                         ImmutableList.copyOf(sourceIdsExtra),
62                         safetyEvent);
63             }
64             return;
65         }
66 
67 
68         if (ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
69             refreshAllSafetySources(context, EVENT_DEVICE_REBOOTED);
70         }
71     }
72 
refreshSafetySources(Context context, List<String> sourceIds, SafetyEvent safetyEvent)73     private static void refreshSafetySources(Context context, List<String> sourceIds,
74             SafetyEvent safetyEvent) {
75         if (sourceIds.contains(LockScreenSafetySource.SAFETY_SOURCE_ID)) {
76             LockScreenSafetySource.setSafetySourceData(context,
77                     new ScreenLockPreferenceDetailsUtils(context), safetyEvent);
78         }
79 
80         if (sourceIds.contains(BiometricsSafetySource.SAFETY_SOURCE_ID)) {
81             BiometricsSafetySource.setSafetySourceData(context, safetyEvent);
82         }
83 
84         if (sourceIds.contains(PrivateSpaceSafetySource.SAFETY_SOURCE_ID)) {
85             PrivateSpaceSafetySource.setSafetySourceData(context, safetyEvent);
86         }
87     }
88 
refreshAllSafetySources(Context context, SafetyEvent safetyEvent)89     private static void refreshAllSafetySources(Context context, SafetyEvent safetyEvent) {
90         LockScreenSafetySource.setSafetySourceData(context,
91                 new ScreenLockPreferenceDetailsUtils(context), safetyEvent);
92         BiometricsSafetySource.setSafetySourceData(context, safetyEvent);
93         PrivateSpaceSafetySource.setSafetySourceData(context, safetyEvent);
94     }
95 }
96