1 /* 2 * Copyright (C) 2025 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 com.android.settings.biometrics.combination.BiometricsSettingsBase.ACTIVE_UNLOCK_REQUEST; 20 21 import android.app.PendingIntent; 22 import android.content.Context; 23 import android.os.UserManager; 24 import android.safetycenter.SafetyEvent; 25 26 import androidx.annotation.NonNull; 27 import androidx.annotation.Nullable; 28 29 import com.android.internal.annotations.VisibleForTesting; 30 import com.android.settings.biometrics.activeunlock.ActiveUnlockStatusUtils; 31 import com.android.settings.flags.Flags; 32 33 /** Wear Safety Source for Safety Center. */ 34 public final class WearSafetySource { 35 36 private static final String TAG = "WearSafetySource"; 37 public static final String SAFETY_SOURCE_ID = "AndroidWearUnlock"; 38 private static boolean sIsTestingEnv = false; 39 private static String sSummaryForTesting = ""; 40 private static boolean sHasEnrolledForTesting; 41 WearSafetySource()42 private WearSafetySource() {} 43 44 /** Sets test value for summary. */ 45 @VisibleForTesting setSummaryForTesting(@onNull String summary)46 public static void setSummaryForTesting(@NonNull String summary) { 47 sIsTestingEnv = true; 48 sSummaryForTesting = summary; 49 } 50 51 /** Sets test value for hasEnrolled. */ 52 @VisibleForTesting setHasEnrolledForTesting(boolean hasEnrolled)53 public static void setHasEnrolledForTesting(boolean hasEnrolled) { 54 sIsTestingEnv = true; 55 sHasEnrolledForTesting = hasEnrolled; 56 } 57 58 /** Sets biometric safety data for Safety Center. */ setSafetySourceData( @onNull Context context, @NonNull SafetyEvent safetyEvent)59 public static void setSafetySourceData( 60 @NonNull Context context, @NonNull SafetyEvent safetyEvent) { 61 if (!SafetyCenterManagerWrapper.get().isEnabled(context)) { 62 return; 63 } 64 if (!Flags.biometricsOnboardingEducation()) { // this source is effectively turned off 65 sendNullData(context, safetyEvent); 66 return; 67 } 68 69 // Handle private profile case. 70 UserManager userManager = UserManager.get(context); 71 if (android.os.Flags.allowPrivateProfile() 72 && android.multiuser.Flags.enablePrivateSpaceFeatures() 73 && userManager.isPrivateProfile()) { 74 // SC always expects a response from the source if the broadcast has been sent for this 75 // source, therefore, we need to send a null SafetySourceData. 76 sendNullData(context, safetyEvent); 77 return; 78 } 79 80 ActiveUnlockStatusUtils activeUnlockStatusUtils = new ActiveUnlockStatusUtils(context); 81 if (!userManager.isProfile() && activeUnlockStatusUtils.isAvailable()) { 82 boolean hasEnrolled = false; 83 String summary = ""; 84 85 if (sIsTestingEnv) { 86 hasEnrolled = sHasEnrolledForTesting; 87 summary = sSummaryForTesting; 88 } else { 89 String authority = new ActiveUnlockStatusUtils(context).getAuthority(); 90 hasEnrolled = getHasEnrolledFromContentProvider(context, authority); 91 summary = getSummaryFromContentProvider(context, authority); 92 } 93 94 BiometricSourcesUtils.setBiometricSafetySourceData( 95 SAFETY_SOURCE_ID, 96 context, 97 activeUnlockStatusUtils.getTitleForActiveUnlockOnly(), 98 summary, 99 PendingIntent.getActivity(context, ACTIVE_UNLOCK_REQUEST, 100 activeUnlockStatusUtils.getIntent(), 101 PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT), 102 /* enabled= */ true, 103 hasEnrolled, 104 safetyEvent); 105 return; 106 } 107 108 sendNullData(context, safetyEvent); 109 } 110 sendNullData(Context context, SafetyEvent safetyEvent)111 private static void sendNullData(Context context, SafetyEvent safetyEvent) { 112 SafetyCenterManagerWrapper.get() 113 .setSafetySourceData( 114 context, SAFETY_SOURCE_ID, /* safetySourceData= */ null, safetyEvent); 115 } 116 117 /** Notifies Safety Center of a change in wear biometrics settings. */ onBiometricsChanged(@onNull Context context)118 public static void onBiometricsChanged(@NonNull Context context) { 119 setSafetySourceData( 120 context, 121 new SafetyEvent.Builder(SafetyEvent.SAFETY_EVENT_TYPE_SOURCE_STATE_CHANGED) 122 .build()); 123 } 124 getHasEnrolledFromContentProvider( @onNull Context context, @Nullable String authority)125 private static boolean getHasEnrolledFromContentProvider( 126 @NonNull Context context, @Nullable String authority) { 127 if (authority == null) { 128 return false; 129 } 130 return ActiveUnlockStatusUtils.getDeviceNameFromContentProvider(context, authority, TAG) 131 != null; 132 } 133 getSummaryFromContentProvider( @onNull Context context, @Nullable String authority)134 private static String getSummaryFromContentProvider( 135 @NonNull Context context, @Nullable String authority) { 136 if (authority == null) { 137 return ""; 138 } 139 String summary = ActiveUnlockStatusUtils.getSummaryFromContentProvider( 140 context, authority, TAG); 141 if (summary == null) { 142 return ""; 143 } 144 return summary; 145 } 146 147 } 148