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