From 6356de3f9bb3a41304ec7e807933f55049adc960 Mon Sep 17 00:00:00 2001 From: Elliot Sisteron Date: Tue, 18 Apr 2023 17:14:47 +0000 Subject: [PATCH] Launch work profile entries intent in the profile parent. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The screens don't work as expected if they're launched in the work profile. The reason for this is that Settings handle the user separately with an extra inside the intent. I assume launching the actual screen in a separate user is not supported. Note that I've also: 1. Added a safeguard to make sure that the "active unlock" code path only occurs in the profile parent — it seems preferrable (see b/277877289#comment4) 2. Added the user id as an identifier on the intents lauched by the entries. The reason we do this is to make sure the user id is taken into account in the PendingIntent#equals implementation. This was automatically taken care of when launching with the profile context, but now that we launch everything in the profile parent context we have to make sure the user id is taken into account Bug: 278665241 Bug: 277877289 Test: manual Change-Id: Idcaa31cd56ed64768aa8f069d30d2adeb7269099 --- .../safetycenter/BiometricsSafetySource.java | 126 +++++++++++------- 1 file changed, 81 insertions(+), 45 deletions(-) diff --git a/src/com/android/settings/safetycenter/BiometricsSafetySource.java b/src/com/android/settings/safetycenter/BiometricsSafetySource.java index 74964ce13e1..9dc5b4e765e 100644 --- a/src/com/android/settings/safetycenter/BiometricsSafetySource.java +++ b/src/com/android/settings/safetycenter/BiometricsSafetySource.java @@ -22,7 +22,9 @@ import android.content.Intent; import android.hardware.face.FaceManager; import android.hardware.fingerprint.FingerprintManager; import android.os.Bundle; +import android.os.Process; import android.os.UserHandle; +import android.os.UserManager; import android.safetycenter.SafetyEvent; import android.safetycenter.SafetySourceData; import android.safetycenter.SafetySourceStatus; @@ -43,8 +45,7 @@ public final class BiometricsSafetySource { private static final int REQUEST_CODE_FACE_SETTING = 20; private static final int REQUEST_CODE_FINGERPRINT_SETTING = 30; - private BiometricsSafetySource() { - } + private BiometricsSafetySource() {} /** Sets biometric safety data for Safety Center. */ public static void setSafetySourceData(Context context, SafetyEvent safetyEvent) { @@ -52,41 +53,59 @@ public final class BiometricsSafetySource { return; } - final int userId = UserHandle.myUserId(); + final UserHandle userHandle = Process.myUserHandle(); + final int userId = userHandle.getIdentifier(); + final UserManager userManager = UserManager.get(context); + UserHandle profileParentUserHandle = userManager.getProfileParent(userHandle); + if (profileParentUserHandle == null) { + profileParentUserHandle = userHandle; + } + final Context profileParentContext = + context.createContextAsUser(profileParentUserHandle, 0); - final BiometricNavigationUtils biometricNavigationUtils = new BiometricNavigationUtils( - userId); + final BiometricNavigationUtils biometricNavigationUtils = + new BiometricNavigationUtils(userId); final CombinedBiometricStatusUtils combinedBiometricStatusUtils = new CombinedBiometricStatusUtils(context, userId); final ActiveUnlockStatusUtils activeUnlockStatusUtils = new ActiveUnlockStatusUtils(context); - if (activeUnlockStatusUtils.isAvailable()) { + if (!userManager.isProfile() && activeUnlockStatusUtils.isAvailable()) { final RestrictedLockUtils.EnforcedAdmin disablingAdmin = combinedBiometricStatusUtils.getDisablingAdmin(); - setBiometricSafetySourceData(context, + setBiometricSafetySourceData( + context, activeUnlockStatusUtils.getTitleForActiveUnlock(), combinedBiometricStatusUtils.getSummary(), - createPendingIntent(context, - biometricNavigationUtils.getBiometricSettingsIntent(context, + createPendingIntent( + context, + biometricNavigationUtils.getBiometricSettingsIntent( + context, combinedBiometricStatusUtils.getSettingsClassName(), - disablingAdmin, Bundle.EMPTY), + disablingAdmin, + Bundle.EMPTY), REQUEST_CODE_COMBINED_BIOMETRIC_SETTING), disablingAdmin == null /* enabled */, combinedBiometricStatusUtils.hasEnrolled(), safetyEvent); return; - } if (combinedBiometricStatusUtils.isAvailable()) { final RestrictedLockUtils.EnforcedAdmin disablingAdmin = combinedBiometricStatusUtils.getDisablingAdmin(); - setBiometricSafetySourceData(context, + setBiometricSafetySourceData( + context, combinedBiometricStatusUtils.getTitle(), combinedBiometricStatusUtils.getSummary(), - createPendingIntent(context, - biometricNavigationUtils.getBiometricSettingsIntent(context, - combinedBiometricStatusUtils.getSettingsClassNameBasedOnUser(), - disablingAdmin, Bundle.EMPTY), + createPendingIntent( + profileParentContext, + biometricNavigationUtils + .getBiometricSettingsIntent( + context, + combinedBiometricStatusUtils + .getSettingsClassNameBasedOnUser(), + disablingAdmin, + Bundle.EMPTY) + .setIdentifier(Integer.toString(userId)), REQUEST_CODE_COMBINED_BIOMETRIC_SETTING), disablingAdmin == null /* enabled */, combinedBiometricStatusUtils.hasEnrolled(), @@ -100,13 +119,19 @@ public final class BiometricsSafetySource { if (faceStatusUtils.isAvailable()) { final RestrictedLockUtils.EnforcedAdmin disablingAdmin = faceStatusUtils.getDisablingAdmin(); - setBiometricSafetySourceData(context, + setBiometricSafetySourceData( + context, faceStatusUtils.getTitle(), faceStatusUtils.getSummary(), - createPendingIntent(context, - biometricNavigationUtils.getBiometricSettingsIntent(context, - faceStatusUtils.getSettingsClassName(), disablingAdmin, - Bundle.EMPTY), + createPendingIntent( + profileParentContext, + biometricNavigationUtils + .getBiometricSettingsIntent( + context, + faceStatusUtils.getSettingsClassName(), + disablingAdmin, + Bundle.EMPTY) + .setIdentifier(Integer.toString(userId)), REQUEST_CODE_FACE_SETTING), disablingAdmin == null /* enabled */, faceStatusUtils.hasEnrolled(), @@ -116,19 +141,25 @@ public final class BiometricsSafetySource { } final FingerprintManager fingerprintManager = Utils.getFingerprintManagerOrNull(context); - final FingerprintStatusUtils fingerprintStatusUtils = new FingerprintStatusUtils(context, - fingerprintManager, userId); + final FingerprintStatusUtils fingerprintStatusUtils = + new FingerprintStatusUtils(context, fingerprintManager, userId); if (fingerprintStatusUtils.isAvailable()) { final RestrictedLockUtils.EnforcedAdmin disablingAdmin = fingerprintStatusUtils.getDisablingAdmin(); - setBiometricSafetySourceData(context, + setBiometricSafetySourceData( + context, fingerprintStatusUtils.getTitle(), fingerprintStatusUtils.getSummary(), - createPendingIntent(context, - biometricNavigationUtils.getBiometricSettingsIntent(context, - fingerprintStatusUtils.getSettingsClassName(), disablingAdmin, - Bundle.EMPTY), + createPendingIntent( + profileParentContext, + biometricNavigationUtils + .getBiometricSettingsIntent( + context, + fingerprintStatusUtils.getSettingsClassName(), + disablingAdmin, + Bundle.EMPTY) + .setIdentifier(Integer.toString(userId)), REQUEST_CODE_FINGERPRINT_SETTING), disablingAdmin == null /* enabled */, fingerprintStatusUtils.hasEnrolled(), @@ -140,33 +171,38 @@ public final class BiometricsSafetySource { public static void onBiometricsChanged(Context context) { setSafetySourceData( context, - new SafetyEvent.Builder(SafetyEvent.SAFETY_EVENT_TYPE_SOURCE_STATE_CHANGED).build() - ); + new SafetyEvent.Builder(SafetyEvent.SAFETY_EVENT_TYPE_SOURCE_STATE_CHANGED) + .build()); } - private static void setBiometricSafetySourceData(Context context, String title, String summary, - PendingIntent pendingIntent, boolean enabled, boolean hasEnrolled, + private static void setBiometricSafetySourceData( + Context context, + String title, + String summary, + PendingIntent pendingIntent, + boolean enabled, + boolean hasEnrolled, SafetyEvent safetyEvent) { final int severityLevel = - enabled && hasEnrolled ? SafetySourceData.SEVERITY_LEVEL_INFORMATION + enabled && hasEnrolled + ? SafetySourceData.SEVERITY_LEVEL_INFORMATION : SafetySourceData.SEVERITY_LEVEL_UNSPECIFIED; - final SafetySourceStatus status = new SafetySourceStatus.Builder(title, summary, - severityLevel).setPendingIntent(pendingIntent).setEnabled(enabled).build(); + final SafetySourceStatus status = + new SafetySourceStatus.Builder(title, summary, severityLevel) + .setPendingIntent(pendingIntent) + .setEnabled(enabled) + .build(); final SafetySourceData safetySourceData = new SafetySourceData.Builder().setStatus(status).build(); - SafetyCenterManagerWrapper.get().setSafetySourceData( - context, SAFETY_SOURCE_ID, safetySourceData, safetyEvent); + SafetyCenterManagerWrapper.get() + .setSafetySourceData(context, SAFETY_SOURCE_ID, safetySourceData, safetyEvent); } - private static PendingIntent createPendingIntent(Context context, Intent intent, - int requestCode) { - return PendingIntent - .getActivity( - context, - requestCode, - intent, - PendingIntent.FLAG_IMMUTABLE); + private static PendingIntent createPendingIntent( + Context context, Intent intent, int requestCode) { + return PendingIntent.getActivity( + context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE); } }