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.safetycenter.SafetyEvent.SAFETY_EVENT_TYPE_SOURCE_STATE_CHANGED; 20 21 import android.app.PendingIntent; 22 import android.app.settings.SettingsEnums; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.os.UserHandle; 26 import android.os.UserManager; 27 import android.safetycenter.SafetyEvent; 28 import android.safetycenter.SafetySourceData; 29 import android.safetycenter.SafetySourceIssue; 30 import android.safetycenter.SafetySourceStatus; 31 import android.safetycenter.SafetySourceStatus.IconAction; 32 33 import com.android.settings.R; 34 import com.android.settings.security.ScreenLockPreferenceDetailsUtils; 35 import com.android.settingslib.RestrictedLockUtils; 36 import com.android.settingslib.RestrictedLockUtilsInternal; 37 38 /** Lock Screen Safety Source for Safety Center. */ 39 public final class LockScreenSafetySource { 40 41 public static final String SAFETY_SOURCE_ID = "AndroidLockScreen"; 42 public static final String NO_SCREEN_LOCK_ISSUE_ID = "NoScreenLockIssue"; 43 public static final String NO_SCREEN_LOCK_ISSUE_TYPE_ID = "NoScreenLockIssueType"; 44 public static final String SET_SCREEN_LOCK_ACTION_ID = "SetScreenLockAction"; 45 46 private static final int REQUEST_CODE_SCREEN_LOCK = 1; 47 private static final int REQUEST_CODE_SCREEN_LOCK_SETTINGS = 2; 48 LockScreenSafetySource()49 private LockScreenSafetySource() {} 50 51 /** Sets lock screen safety data for Safety Center. */ setSafetySourceData( Context context, ScreenLockPreferenceDetailsUtils screenLockPreferenceDetailsUtils, SafetyEvent safetyEvent)52 public static void setSafetySourceData( 53 Context context, 54 ScreenLockPreferenceDetailsUtils screenLockPreferenceDetailsUtils, 55 SafetyEvent safetyEvent) { 56 if (!SafetyCenterManagerWrapper.get().isEnabled(context)) { 57 return; 58 } 59 60 UserManager userManager = context.getSystemService(UserManager.class); 61 if (userManager != null && userManager.isProfile()) { 62 return; // LockScreen source only supports primary profile. 63 } 64 65 if (!screenLockPreferenceDetailsUtils.isAvailable()) { 66 SafetyCenterManagerWrapper.get() 67 .setSafetySourceData( 68 context, SAFETY_SOURCE_ID, /* safetySourceData= */ null, safetyEvent); 69 return; 70 } 71 72 final int userId = UserHandle.myUserId(); 73 final RestrictedLockUtils.EnforcedAdmin admin = 74 RestrictedLockUtilsInternal.checkIfPasswordQualityIsSet(context, userId); 75 final PendingIntent pendingIntent = 76 createPendingIntent( 77 context, 78 screenLockPreferenceDetailsUtils.getLaunchChooseLockGenericFragmentIntent( 79 SettingsEnums.SAFETY_CENTER), 80 REQUEST_CODE_SCREEN_LOCK); 81 final IconAction gearMenuIconAction = 82 createGearMenuIconAction(context, screenLockPreferenceDetailsUtils); 83 final boolean lockScreenAllowedByAdmin = 84 !screenLockPreferenceDetailsUtils.isPasswordQualityManaged(userId, admin); 85 final boolean isLockPatternSecure = screenLockPreferenceDetailsUtils.isLockPatternSecure(); 86 final int severityLevel = 87 lockScreenAllowedByAdmin 88 ? isLockPatternSecure 89 ? SafetySourceData.SEVERITY_LEVEL_INFORMATION 90 : SafetySourceData.SEVERITY_LEVEL_RECOMMENDATION 91 : SafetySourceData.SEVERITY_LEVEL_UNSPECIFIED; 92 93 final SafetySourceStatus status = 94 new SafetySourceStatus.Builder( 95 context.getString(R.string.unlock_set_unlock_launch_picker_title), 96 lockScreenAllowedByAdmin 97 ? screenLockPreferenceDetailsUtils.getSummary( 98 UserHandle.myUserId()) 99 : context.getString(R.string.disabled_by_policy_title), 100 severityLevel) 101 .setPendingIntent(lockScreenAllowedByAdmin ? pendingIntent : null) 102 .setEnabled(lockScreenAllowedByAdmin) 103 .setIconAction(lockScreenAllowedByAdmin ? gearMenuIconAction : null) 104 .build(); 105 final SafetySourceData.Builder safetySourceDataBuilder = 106 new SafetySourceData.Builder().setStatus(status); 107 if (lockScreenAllowedByAdmin && !isLockPatternSecure) { 108 safetySourceDataBuilder.addIssue(createNoScreenLockIssue(context, pendingIntent)); 109 } 110 final SafetySourceData safetySourceData = safetySourceDataBuilder.build(); 111 112 SafetyCenterManagerWrapper.get() 113 .setSafetySourceData(context, SAFETY_SOURCE_ID, safetySourceData, safetyEvent); 114 } 115 116 /** Notifies Safety Center of a change in lock screen settings. */ onLockScreenChange(Context context)117 public static void onLockScreenChange(Context context) { 118 setSafetySourceData( 119 context, 120 new ScreenLockPreferenceDetailsUtils(context), 121 new SafetyEvent.Builder(SAFETY_EVENT_TYPE_SOURCE_STATE_CHANGED).build()); 122 123 // Also send refreshed safety center data for biometrics, since changing lockscreen settings 124 // can unset biometrics. 125 BiometricsSafetySource.onBiometricsChanged(context); 126 } 127 createGearMenuIconAction( Context context, ScreenLockPreferenceDetailsUtils screenLockPreferenceDetailsUtils)128 private static IconAction createGearMenuIconAction( 129 Context context, ScreenLockPreferenceDetailsUtils screenLockPreferenceDetailsUtils) { 130 return screenLockPreferenceDetailsUtils.shouldShowGearMenu() 131 ? new IconAction( 132 IconAction.ICON_TYPE_GEAR, 133 createPendingIntent( 134 context, 135 screenLockPreferenceDetailsUtils.getLaunchScreenLockSettingsIntent( 136 SettingsEnums.SAFETY_CENTER), 137 REQUEST_CODE_SCREEN_LOCK_SETTINGS)) 138 : null; 139 } 140 createPendingIntent( Context context, Intent intent, int requestCode)141 private static PendingIntent createPendingIntent( 142 Context context, Intent intent, int requestCode) { 143 return PendingIntent.getActivity( 144 context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE); 145 } 146 createNoScreenLockIssue( Context context, PendingIntent pendingIntent)147 private static SafetySourceIssue createNoScreenLockIssue( 148 Context context, PendingIntent pendingIntent) { 149 final SafetySourceIssue.Action action = 150 new SafetySourceIssue.Action.Builder( 151 SET_SCREEN_LOCK_ACTION_ID, 152 context.getString(R.string.no_screen_lock_issue_action_label), 153 pendingIntent) 154 .build(); 155 // Custom notification deliberately has zero actions 156 final SafetySourceIssue.Notification customNotification = 157 new SafetySourceIssue.Notification.Builder( 158 context.getString(R.string.no_screen_lock_issue_notification_title), 159 context.getString(R.string.no_screen_lock_issue_notification_text)) 160 .build(); 161 return new SafetySourceIssue.Builder( 162 NO_SCREEN_LOCK_ISSUE_ID, 163 context.getString(R.string.no_screen_lock_issue_title), 164 context.getString(R.string.no_screen_lock_issue_summary), 165 SafetySourceData.SEVERITY_LEVEL_RECOMMENDATION, 166 NO_SCREEN_LOCK_ISSUE_TYPE_ID) 167 .setIssueCategory(SafetySourceIssue.ISSUE_CATEGORY_DEVICE) 168 .addAction(action) 169 .setIssueActionability(SafetySourceIssue.ISSUE_ACTIONABILITY_MANUAL) 170 .setCustomNotification(customNotification) 171 .setNotificationBehavior(SafetySourceIssue.NOTIFICATION_BEHAVIOR_DELAYED) 172 .build(); 173 } 174 } 175