Provide data to LockScreenSafetySource

Test: atest SettingsUnitTests

Bug: 215515298
Change-Id: I59640e9f691754a2225870d40f5db3e6fd8557f8
This commit is contained in:
Yuri Ufimtsev
2022-02-18 14:33:47 +00:00
parent 3b3c6bc564
commit fc9b13e865
5 changed files with 297 additions and 49 deletions

View File

@@ -17,51 +17,76 @@
package com.android.settings.safetycenter;
import android.app.PendingIntent;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.os.UserHandle;
import android.safetycenter.SafetySourceData;
import android.safetycenter.SafetySourceStatus;
import android.safetycenter.SafetySourceStatus.IconAction;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.password.ChooseLockGeneric;
import com.android.settingslib.transition.SettingsTransitionHelper;
import com.android.settings.R;
import com.android.settings.security.ScreenLockPreferenceDetailsUtils;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
/** Lock Screen Safety Source for Safety Center. */
public final class LockScreenSafetySource {
public static final String SAFETY_SOURCE_ID = "LockScreenSafetySource";
private LockScreenSafetySource() {}
private LockScreenSafetySource() {
}
/** Sends lock screen safety data to Safety Center. */
public static void sendSafetyData(Context context) {
public static void sendSafetyData(Context context,
ScreenLockPreferenceDetailsUtils screenLockPreferenceDetailsUtils) {
if (!SafetyCenterStatusHolder.get().isEnabled(context)) {
return;
}
// TODO(b/215515298): Replace placeholder SafetySourceData with real data.
// TODO(b/217409995): Replace SECURITY_ALTERNATIVE with Safety Center metrics category.
Intent clickIntent = new SubSettingLauncher(context)
.setDestination(ChooseLockGeneric.ChooseLockGenericFragment.class.getName())
.setSourceMetricsCategory(SettingsEnums.SECURITY_ALTERNATIVE)
.setTransitionType(SettingsTransitionHelper.TransitionType.TRANSITION_SLIDE)
.toIntent();
PendingIntent pendingIntent = PendingIntent
.getActivity(
context,
0 /* requestCode */,
clickIntent,
PendingIntent.FLAG_IMMUTABLE);
SafetySourceData safetySourceData =
new SafetySourceData.Builder(SAFETY_SOURCE_ID).setStatus(
new SafetySourceStatus.Builder(
"Lock Screen",
"Lock screen settings",
SafetySourceStatus.STATUS_LEVEL_OK,
pendingIntent).build()
).build();
if (!screenLockPreferenceDetailsUtils.isAvailable()) {
return;
}
final int userId = UserHandle.myUserId();
final RestrictedLockUtils.EnforcedAdmin admin = RestrictedLockUtilsInternal
.checkIfPasswordQualityIsSet(context, userId);
final PendingIntent pendingIntent = createPendingIntent(context,
screenLockPreferenceDetailsUtils.getLaunchChooseLockGenericFragmentIntent());
final IconAction gearMenuIconAction = createGearMenuIconAction(context,
screenLockPreferenceDetailsUtils);
final SafetySourceStatus status = new SafetySourceStatus.Builder(
context.getString(R.string.unlock_set_unlock_launch_picker_title_profile),
screenLockPreferenceDetailsUtils.getSummary(UserHandle.myUserId()),
screenLockPreferenceDetailsUtils.isLockPatternSecure()
? SafetySourceStatus.STATUS_LEVEL_OK
: SafetySourceStatus.STATUS_LEVEL_RECOMMENDATION,
pendingIntent)
.setEnabled(
!screenLockPreferenceDetailsUtils.isPasswordQualityManaged(userId, admin))
.setIconAction(gearMenuIconAction).build();
final SafetySourceData safetySourceData = new SafetySourceData.Builder(
SAFETY_SOURCE_ID).setStatus(status).build();
SafetyCenterManagerWrapper.get().sendSafetyCenterUpdate(context, safetySourceData);
}
private static IconAction createGearMenuIconAction(Context context,
ScreenLockPreferenceDetailsUtils screenLockPreferenceDetailsUtils) {
return screenLockPreferenceDetailsUtils.shouldShowGearMenu() ? new IconAction(
IconAction.ICON_TYPE_GEAR,
createPendingIntent(context,
screenLockPreferenceDetailsUtils.getLaunchScreenLockSettingsIntent()))
: null;
}
private static PendingIntent createPendingIntent(Context context, Intent intent) {
return PendingIntent
.getActivity(
context,
0 /* requestCode */,
intent,
PendingIntent.FLAG_IMMUTABLE);
}
}

View File

@@ -20,10 +20,13 @@ import static android.content.Intent.ACTION_BOOT_COMPLETED;
import static android.safetycenter.SafetyCenterManager.ACTION_REFRESH_SAFETY_SOURCES;
import static android.safetycenter.SafetyCenterManager.EXTRA_REFRESH_SAFETY_SOURCE_IDS;
import android.app.settings.SettingsEnums;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.android.settings.security.ScreenLockPreferenceDetailsUtils;
import com.google.common.collect.ImmutableList;
import java.util.List;
@@ -54,7 +57,8 @@ public class SafetySourceBroadcastReceiver extends BroadcastReceiver {
private static void refreshSafetySources(Context context, List<String> sourceIds) {
if (sourceIds.contains(LockScreenSafetySource.SAFETY_SOURCE_ID)) {
LockScreenSafetySource.sendSafetyData(context);
LockScreenSafetySource.sendSafetyData(context,
new ScreenLockPreferenceDetailsUtils(context, SettingsEnums.SAFETY_CENTER));
}
if (sourceIds.contains(BiometricsSafetySource.SAFETY_SOURCE_ID)) {
@@ -63,7 +67,8 @@ public class SafetySourceBroadcastReceiver extends BroadcastReceiver {
}
private static void refreshAllSafetySources(Context context) {
LockScreenSafetySource.sendSafetyData(context);
LockScreenSafetySource.sendSafetyData(context,
new ScreenLockPreferenceDetailsUtils(context, SettingsEnums.SAFETY_CENTER));
BiometricsSafetySource.sendSafetyData(context);
}
}

View File

@@ -18,12 +18,14 @@ package com.android.settings.security;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.Intent;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.storage.StorageManager;
import androidx.annotation.StringRes;
import com.android.internal.app.UnlaunchableAppActivity;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.R;
import com.android.settings.Utils;
@@ -81,21 +83,35 @@ public class ScreenLockPreferenceDetailsUtils {
== DevicePolicyManager.PASSWORD_QUALITY_MANAGED;
}
/**
* Returns whether the lock pattern is secure.
*/
public boolean isLockPatternSecure() {
return mLockPatternUtils.isSecure(mUserId);
}
/**
* Returns whether the Gear Menu should be shown.
*/
public boolean shouldShowGearMenu() {
return mLockPatternUtils.isSecure(mUserId);
return isLockPatternSecure();
}
/**
* Launches the {@link ScreenLockSettings}.
*/
public void openScreenLockSettings() {
new SubSettingLauncher(mContext)
mContext.startActivity(getLaunchScreenLockSettingsIntent());
}
/**
* Returns {@link Intent} to launch the {@link ScreenLockSettings}.
*/
public Intent getLaunchScreenLockSettingsIntent() {
return new SubSettingLauncher(mContext)
.setDestination(ScreenLockSettings.class.getName())
.setSourceMetricsCategory(mSourceMetricsCategory)
.launch();
.toIntent();
}
/**
@@ -105,6 +121,29 @@ public class ScreenLockPreferenceDetailsUtils {
* @return true if the {@link ChooseLockGenericFragment} is launching.
*/
public boolean openChooseLockGenericFragment() {
final Intent quietModeDialogIntent = getQuietModeDialogIntent();
if (quietModeDialogIntent != null) {
mContext.startActivity(quietModeDialogIntent);
return false;
}
mContext.startActivity(getChooseLockGenericFragmentIntent());
return true;
}
/**
* Returns {@link Intent} to launch an appropriate Settings screen.
*
* <p>If Quiet Mode is enabled for managed profile, returns {@link Intent} to launch a dialog
* to disable the Quiet Mode, otherwise returns {@link Intent} to launch
* {@link ChooseLockGenericFragment}.
*/
public Intent getLaunchChooseLockGenericFragmentIntent() {
final Intent quietModeDialogIntent = getQuietModeDialogIntent();
return quietModeDialogIntent != null ? quietModeDialogIntent
: getChooseLockGenericFragmentIntent();
}
private Intent getQuietModeDialogIntent() {
// TODO(b/35930129): Remove once existing password can be passed into vold directly.
// Currently we need this logic to ensure that the QUIET_MODE is off for any work
// profile with unified challenge on FBE-enabled devices. Otherwise, vold would not be
@@ -112,17 +151,20 @@ public class ScreenLockPreferenceDetailsUtils {
if (mProfileChallengeUserId != UserHandle.USER_NULL
&& !mLockPatternUtils.isSeparateProfileChallengeEnabled(mProfileChallengeUserId)
&& StorageManager.isFileEncryptedNativeOnly()) {
if (Utils.startQuietModeDialogIfNecessary(mContext, mUm, mProfileChallengeUserId)) {
return false;
if (mUm.isQuietModeEnabled(UserHandle.of(mProfileChallengeUserId))) {
return UnlaunchableAppActivity.createInQuietModeDialogIntent(
mProfileChallengeUserId);
}
}
return null;
}
new SubSettingLauncher(mContext)
private Intent getChooseLockGenericFragmentIntent() {
return new SubSettingLauncher(mContext)
.setDestination(ChooseLockGenericFragment.class.getName())
.setSourceMetricsCategory(mSourceMetricsCategory)
.setTransitionType(SettingsTransitionHelper.TransitionType.TRANSITION_SLIDE)
.launch();
return true;
.toIntent();
}
@StringRes

View File

@@ -19,19 +19,22 @@ package com.android.settings.safetycenter;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.safetycenter.SafetySourceData;
import android.safetycenter.SafetySourceStatus;
import android.safetycenter.SafetySourceStatus.IconAction;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.SettingsActivity;
import com.android.settings.password.ChooseLockGeneric;
import com.android.settings.security.ScreenLockPreferenceDetailsUtils;
import com.android.settings.testutils.ResourcesUtils;
import org.junit.After;
import org.junit.Before;
@@ -44,6 +47,10 @@ import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class LockScreenSafetySourceTest {
private static final String SUMMARY = "summary";
private static final String FAKE_ACTION_CHOOSE_LOCK_GENERIC_FRAGMENT = "choose_lock_generic";
private static final String FAKE_ACTION_SCREEN_LOCK_SETTINGS = "screen_lock_settings";
private Context mApplicationContext;
@Mock
@@ -52,6 +59,9 @@ public class LockScreenSafetySourceTest {
@Mock
private SafetyCenterStatusHolder mSafetyCenterStatusHolder;
@Mock
private ScreenLockPreferenceDetailsUtils mScreenLockPreferenceDetailsUtils;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
@@ -70,30 +80,160 @@ public class LockScreenSafetySourceTest {
public void sendSafetyData_whenSafetyCenterIsDisabled_sendsNoData() {
when(mSafetyCenterStatusHolder.isEnabled(mApplicationContext)).thenReturn(false);
LockScreenSafetySource.sendSafetyData(mApplicationContext);
LockScreenSafetySource.sendSafetyData(mApplicationContext,
mScreenLockPreferenceDetailsUtils);
verify(mSafetyCenterManagerWrapper, never()).sendSafetyCenterUpdate(any(), any());
}
@Test
public void sendSafetyData_whenSafetyCenterIsEnabled_sendsPlaceholderData() {
public void sendSafetyData_whenScreenLockIsDisabled_sendsNoData() {
when(mSafetyCenterStatusHolder.isEnabled(mApplicationContext)).thenReturn(true);
when(mScreenLockPreferenceDetailsUtils.isAvailable()).thenReturn(false);
LockScreenSafetySource.sendSafetyData(mApplicationContext,
mScreenLockPreferenceDetailsUtils);
verify(mSafetyCenterManagerWrapper, never()).sendSafetyCenterUpdate(any(), any());
}
@Test
public void sendSafetyData_whenScreenLockIsEnabled_sendsData() {
whenScreenLockIsEnabled();
LockScreenSafetySource.sendSafetyData(mApplicationContext,
mScreenLockPreferenceDetailsUtils);
LockScreenSafetySource.sendSafetyData(mApplicationContext);
ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class);
verify(mSafetyCenterManagerWrapper).sendSafetyCenterUpdate(any(), captor.capture());
SafetySourceData safetySourceData = captor.getValue();
SafetySourceStatus safetySourceStatus = safetySourceData.getStatus();
assertThat(safetySourceData.getId()).isEqualTo(LockScreenSafetySource.SAFETY_SOURCE_ID);
assertThat(safetySourceStatus.getTitle().toString()).isEqualTo("Lock Screen");
assertThat(safetySourceStatus.getTitle().toString())
.isEqualTo(ResourcesUtils.getResourcesString(
mApplicationContext,
"unlock_set_unlock_launch_picker_title_profile"));
assertThat(safetySourceStatus.getSummary().toString())
.isEqualTo("Lock screen settings");
.isEqualTo(SUMMARY);
assertThat(safetySourceStatus.getPendingIntent().getIntent()).isNotNull();
assertThat(safetySourceStatus.getPendingIntent().getIntent().getAction())
.isEqualTo(FAKE_ACTION_CHOOSE_LOCK_GENERIC_FRAGMENT);
}
@Test
public void sendSafetyData_whenLockPatternIsSecure_sendsStatusLevelOk() {
whenScreenLockIsEnabled();
when(mScreenLockPreferenceDetailsUtils.isLockPatternSecure()).thenReturn(true);
LockScreenSafetySource.sendSafetyData(mApplicationContext,
mScreenLockPreferenceDetailsUtils);
ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class);
verify(mSafetyCenterManagerWrapper).sendSafetyCenterUpdate(any(), captor.capture());
SafetySourceData safetySourceData = captor.getValue();
SafetySourceStatus safetySourceStatus = safetySourceData.getStatus();
assertThat(safetySourceStatus.getStatusLevel())
.isEqualTo(SafetySourceStatus.STATUS_LEVEL_OK);
assertThat(safetySourceStatus.getPendingIntent()).isNotNull();
assertThat(safetySourceStatus.getPendingIntent().getIntent().getStringExtra(
SettingsActivity.EXTRA_SHOW_FRAGMENT))
.isEqualTo(ChooseLockGeneric.ChooseLockGenericFragment.class.getName());
}
@Test
public void sendSafetyData_whenLockPatternIsNotSecure_sendsStatusLevelRecommendation() {
whenScreenLockIsEnabled();
when(mScreenLockPreferenceDetailsUtils.isLockPatternSecure()).thenReturn(false);
LockScreenSafetySource.sendSafetyData(mApplicationContext,
mScreenLockPreferenceDetailsUtils);
ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class);
verify(mSafetyCenterManagerWrapper).sendSafetyCenterUpdate(any(), captor.capture());
SafetySourceData safetySourceData = captor.getValue();
SafetySourceStatus safetySourceStatus = safetySourceData.getStatus();
assertThat(safetySourceStatus.getStatusLevel())
.isEqualTo(SafetySourceStatus.STATUS_LEVEL_RECOMMENDATION);
}
@Test
public void sendSafetyData_whenPasswordQualityIsManaged_sendsDisabled() {
whenScreenLockIsEnabled();
when(mScreenLockPreferenceDetailsUtils.isPasswordQualityManaged(anyInt(), any()))
.thenReturn(true);
LockScreenSafetySource.sendSafetyData(mApplicationContext,
mScreenLockPreferenceDetailsUtils);
ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class);
verify(mSafetyCenterManagerWrapper).sendSafetyCenterUpdate(any(), captor.capture());
SafetySourceData safetySourceData = captor.getValue();
SafetySourceStatus safetySourceStatus = safetySourceData.getStatus();
assertThat(safetySourceStatus.isEnabled()).isFalse();
}
@Test
public void sendSafetyData_whenPasswordQualityIsNotManaged_sendsEnabled() {
whenScreenLockIsEnabled();
when(mScreenLockPreferenceDetailsUtils.isPasswordQualityManaged(anyInt(), any()))
.thenReturn(false);
LockScreenSafetySource.sendSafetyData(mApplicationContext,
mScreenLockPreferenceDetailsUtils);
ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class);
verify(mSafetyCenterManagerWrapper).sendSafetyCenterUpdate(any(), captor.capture());
SafetySourceData safetySourceData = captor.getValue();
SafetySourceStatus safetySourceStatus = safetySourceData.getStatus();
assertThat(safetySourceStatus.isEnabled()).isTrue();
}
@Test
public void sendSafetyData_whenShouldShowGearMenu_sendsGearMenuActionIcon() {
whenScreenLockIsEnabled();
final Intent launchScreenLockSettings = new Intent(FAKE_ACTION_SCREEN_LOCK_SETTINGS);
when(mScreenLockPreferenceDetailsUtils.getLaunchScreenLockSettingsIntent())
.thenReturn(launchScreenLockSettings);
when(mScreenLockPreferenceDetailsUtils.shouldShowGearMenu()).thenReturn(true);
LockScreenSafetySource.sendSafetyData(mApplicationContext,
mScreenLockPreferenceDetailsUtils);
final ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(
SafetySourceData.class);
verify(mSafetyCenterManagerWrapper).sendSafetyCenterUpdate(any(), captor.capture());
final IconAction iconAction = captor.getValue().getStatus().getIconAction();
assertThat(iconAction.getIconType()).isEqualTo(IconAction.ICON_TYPE_GEAR);
assertThat(iconAction.getPendingIntent().getIntent().getAction())
.isEqualTo(FAKE_ACTION_SCREEN_LOCK_SETTINGS);
}
@Test
public void sendSafetyData_whenShouldNotShowGearMenu_sendsNoGearMenuActionIcon() {
whenScreenLockIsEnabled();
when(mScreenLockPreferenceDetailsUtils.shouldShowGearMenu()).thenReturn(false);
LockScreenSafetySource.sendSafetyData(mApplicationContext,
mScreenLockPreferenceDetailsUtils);
ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class);
verify(mSafetyCenterManagerWrapper).sendSafetyCenterUpdate(any(), captor.capture());
SafetySourceData safetySourceData = captor.getValue();
SafetySourceStatus safetySourceStatus = safetySourceData.getStatus();
assertThat(safetySourceStatus.getIconAction()).isNull();
}
private void whenScreenLockIsEnabled() {
when(mSafetyCenterStatusHolder.isEnabled(mApplicationContext)).thenReturn(true);
when(mScreenLockPreferenceDetailsUtils.isAvailable()).thenReturn(true);
when(mScreenLockPreferenceDetailsUtils.getSummary(anyInt())).thenReturn(SUMMARY);
Intent launchChooseLockGenericFragment = new Intent(
FAKE_ACTION_CHOOSE_LOCK_GENERIC_FRAGMENT);
when(mScreenLockPreferenceDetailsUtils.getLaunchChooseLockGenericFragmentIntent())
.thenReturn(launchChooseLockGenericFragment);
}
}

View File

@@ -253,6 +253,20 @@ public class ScreenLockPreferenceDetailsUtilsTest {
.isFalse();
}
@Test
public void isLockPatternSecure_patternIsSecure_shouldReturnTrue() {
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
assertThat(mScreenLockPreferenceDetailsUtils.isLockPatternSecure()).isTrue();
}
@Test
public void isLockPatternSecure_patternIsNotSecure_shouldReturnFalse() {
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false);
assertThat(mScreenLockPreferenceDetailsUtils.isLockPatternSecure()).isFalse();
}
@Test
public void shouldShowGearMenu_patternIsSecure_shouldReturnTrue() {
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
@@ -274,6 +288,13 @@ public class ScreenLockPreferenceDetailsUtilsTest {
assertFragmentLaunchRequested(ScreenLockSettings.class.getName());
}
@Test
public void getLaunchScreenLockSettingsIntent_returnsIntent() {
final Intent intent = mScreenLockPreferenceDetailsUtils.getLaunchScreenLockSettingsIntent();
assertFragmentLaunchIntent(intent, ScreenLockSettings.class.getName());
}
@Test
public void openChooseLockGenericFragment_noQuietMode_shouldSendIntent_shouldReturnTrue() {
when(mUserManager.isQuietModeEnabled(any())).thenReturn(false);
@@ -282,6 +303,17 @@ public class ScreenLockPreferenceDetailsUtilsTest {
assertFragmentLaunchRequested(ChooseLockGeneric.ChooseLockGenericFragment.class.getName());
}
@Test
public void getLaunchChooseLockGenericFragmentIntent_noQuietMode_returnsIntent() {
when(mUserManager.isQuietModeEnabled(any())).thenReturn(false);
final Intent intent =
mScreenLockPreferenceDetailsUtils.getLaunchChooseLockGenericFragmentIntent();
assertFragmentLaunchIntent(intent,
ChooseLockGeneric.ChooseLockGenericFragment.class.getName());
}
private void whenConfigShowUnlockSetOrChangeIsEnabled(boolean enabled) {
final int resId = ResourcesUtils.getResourcesId(
ApplicationProvider.getApplicationContext(), "bool",
@@ -301,6 +333,10 @@ public class ScreenLockPreferenceDetailsUtilsTest {
verify(mContext).startActivity(intentCaptor.capture());
Intent intent = intentCaptor.getValue();
assertFragmentLaunchIntent(intent, fragmentClassName);
}
private void assertFragmentLaunchIntent(Intent intent, String fragmentClassName) {
assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
.isEqualTo(fragmentClassName);
assertThat(intent.getIntExtra(