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 = intent.getStringArrayExtra(EXTRA_REFRESH_SAFETY_SOURCE_IDS); 52 final String refreshBroadcastId = 53 intent.getStringExtra( 54 SafetyCenterManager.EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID); 55 if (sourceIdsExtra != null && sourceIdsExtra.length > 0 && refreshBroadcastId != null) { 56 final SafetyEvent safetyEvent = 57 new SafetyEvent.Builder(SAFETY_EVENT_TYPE_REFRESH_REQUESTED) 58 .setRefreshBroadcastId(refreshBroadcastId) 59 .build(); 60 refreshSafetySources(context, ImmutableList.copyOf(sourceIdsExtra), safetyEvent); 61 } 62 return; 63 } 64 65 if (ACTION_BOOT_COMPLETED.equals(intent.getAction())) { 66 refreshAllSafetySources(context, EVENT_DEVICE_REBOOTED); 67 } 68 } 69 refreshSafetySources( Context context, List<String> sourceIds, SafetyEvent safetyEvent)70 private static void refreshSafetySources( 71 Context context, List<String> sourceIds, SafetyEvent safetyEvent) { 72 if (sourceIds.contains(LockScreenSafetySource.SAFETY_SOURCE_ID)) { 73 LockScreenSafetySource.setSafetySourceData( 74 context, new ScreenLockPreferenceDetailsUtils(context), safetyEvent); 75 } 76 77 if (sourceIds.contains(BiometricsSafetySource.SAFETY_SOURCE_ID)) { 78 BiometricsSafetySource.setSafetySourceData(context, safetyEvent); 79 } 80 if (sourceIds.contains(PrivateSpaceSafetySource.SAFETY_SOURCE_ID)) { 81 PrivateSpaceSafetySource.setSafetySourceData(context, safetyEvent); 82 } 83 if (sourceIds.contains(FaceSafetySource.SAFETY_SOURCE_ID)) { 84 FaceSafetySource.setSafetySourceData(context, safetyEvent); 85 } 86 if (sourceIds.contains(FingerprintSafetySource.SAFETY_SOURCE_ID)) { 87 FingerprintSafetySource.setSafetySourceData(context, safetyEvent); 88 } 89 if (sourceIds.contains(WearSafetySource.SAFETY_SOURCE_ID)) { 90 WearSafetySource.setSafetySourceData(context, safetyEvent); 91 } 92 } 93 refreshAllSafetySources(Context context, SafetyEvent safetyEvent)94 private static void refreshAllSafetySources(Context context, SafetyEvent safetyEvent) { 95 LockScreenSafetySource.setSafetySourceData( 96 context, new ScreenLockPreferenceDetailsUtils(context), safetyEvent); 97 BiometricsSafetySource.setSafetySourceData(context, safetyEvent); 98 PrivateSpaceSafetySource.setSafetySourceData(context, safetyEvent); 99 FaceSafetySource.setSafetySourceData(context, safetyEvent); 100 FingerprintSafetySource.setSafetySourceData(context, safetyEvent); 101 WearSafetySource.setSafetySourceData(context, safetyEvent); 102 } 103 } 104