Add Private Space settings page

This involves:
1. Adding a new page for PS settings under Security & Privacy
2. Integrating the PS safety source with SafetyCenter.

Also, add the capability to create and delete PS from the page.
Creation is temporary to help with prototyping, and will be removed
finally.

Screenshots:
Private Space Entry point in Security & Privacy Settings:
https://screenshot.googleplex.com/4VHxNekjhxZHJwF

Private Space Settings page:
https://screenshot.googleplex.com/3Raw4wuymTHTgtM

Creating Private Space:
https://screenshot.googleplex.com/3dvzAH6V4kQmuYf

Private Space created:
https://screenshot.googleplex.com/Aj7nnF9uuUCa9Q5

Design doc:
https://docs.google.com/document/d/1CdjUjAE84-5ZEPRIfw5KYFjLVHtEZxc_sF0w95su8DA/edit?usp=sharing&resourcekey=0-dAACT9HRexY1IyoxMmkVlw

Bug: 286539356
Bug: 293569406
Test: manual
Test: atest DeletePrivateSpaceControllerTest
Test: atest UseOneLockControllerTest
Test: atest HidePrivateSpaceControllerTest
Change-Id: I9caf8e04e7fb2df36e60f607225e2931988ee692
This commit is contained in:
Manish Singh
2023-07-31 14:41:00 +01:00
parent c192039fe5
commit 27c3987860
15 changed files with 1101 additions and 7 deletions

View File

@@ -0,0 +1,108 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.privatespace;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.privatespace.PrivateSpaceMaintainer.ErrorDeletingPrivateSpace.DELETE_PS_ERROR_INTERNAL;
import static com.android.settings.privatespace.PrivateSpaceMaintainer.ErrorDeletingPrivateSpace.DELETE_PS_ERROR_NONE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import android.content.Context;
import androidx.preference.Preference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class DeletePrivateSpaceControllerTest {
@Mock private PrivateSpaceMaintainer mPrivateSpaceMaintainer;
@Mock private Context mContext;
private Preference mPreference;
private DeletePrivateSpaceController mDeletePrivateSpaceController;
/** Required setup before a test. */
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
final String preferenceKey = "private_space_delete";
mPreference = new Preference(ApplicationProvider.getApplicationContext());
mPreference.setKey(preferenceKey);
mDeletePrivateSpaceController =
new DeletePrivateSpaceController(
mContext,
preferenceKey,
new DeletePrivateSpaceController.Injector() {
@Override
PrivateSpaceMaintainer injectPrivateSpaceMaintainer(Context context) {
return mPrivateSpaceMaintainer;
}
});
}
/** Tests that the controller is always available. */
@Test
public void getAvailabilityStatus_returnsAvailable() {
assertThat(mDeletePrivateSpaceController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
/** Tests that on click it attempts to delete the PS. */
@Test
public void handlePreferenceTreeClick_attemptsToDeletePrivateSpace() {
doReturn(DELETE_PS_ERROR_NONE).when(mPrivateSpaceMaintainer).deletePrivateSpace();
DeletePrivateSpaceController spy = Mockito.spy(mDeletePrivateSpaceController);
doNothing().when(spy).showSuccessfulDeletionToast();
spy.handlePreferenceTreeClick(mPreference);
verify(mPrivateSpaceMaintainer).deletePrivateSpace();
}
/** Tests that on deletion of PS relevant toast is shown. */
@Test
public void handlePreferenceTreeClick_onDeletion_showsDeletedToast() {
doReturn(DELETE_PS_ERROR_NONE).when(mPrivateSpaceMaintainer).deletePrivateSpace();
DeletePrivateSpaceController spy = Mockito.spy(mDeletePrivateSpaceController);
doNothing().when(spy).showSuccessfulDeletionToast();
spy.handlePreferenceTreeClick(mPreference);
verify(spy).showSuccessfulDeletionToast();
}
/** Tests that on failing to delete the PS relevant toast is shown. */
@Test
public void handlePreferenceTreeClick_onDeletionError_showsDeletionFailedToast() {
doReturn(DELETE_PS_ERROR_INTERNAL).when(mPrivateSpaceMaintainer).deletePrivateSpace();
DeletePrivateSpaceController spy = Mockito.spy(mDeletePrivateSpaceController);
doNothing().when(spy).showDeletionInternalErrorToast();
spy.handlePreferenceTreeClick(mPreference);
verify(spy).showDeletionInternalErrorToast();
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.privatespace;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@RunWith(AndroidJUnit4.class)
public class HidePrivateSpaceControllerTest {
@Mock private Context mContext;
private HidePrivateSpaceController mHidePrivateSpaceController;
/** Required setup before a test. */
@Before
public void setUp() {
mContext = ApplicationProvider.getApplicationContext();
final String preferenceKey = "private_space_hidden";
mHidePrivateSpaceController = new HidePrivateSpaceController(mContext, preferenceKey);
}
/** Tests that the controller is always available. */
@Test
public void getAvailabilityStatus_returnsAvailable() {
assertThat(mHidePrivateSpaceController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
}

View File

@@ -0,0 +1,143 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.privatespace;
import static android.safetycenter.SafetyEvent.SAFETY_EVENT_TYPE_DEVICE_REBOOTED;
import static com.android.settings.privatespace.PrivateSpaceSafetySource.SAFETY_SOURCE_ID;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.safetycenter.SafetyEvent;
import android.safetycenter.SafetySourceData;
import android.safetycenter.SafetySourceStatus;
import android.util.FeatureFlagUtils;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.safetycenter.SafetyCenterManagerWrapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class PrivateSpaceSafetySourceTest {
private static final SafetyEvent EVENT_TYPE_DEVICE_REBOOTED =
new SafetyEvent.Builder(SAFETY_EVENT_TYPE_DEVICE_REBOOTED).build();
private Context mContext = ApplicationProvider.getApplicationContext();
@Mock private SafetyCenterManagerWrapper mSafetyCenterManagerWrapper;
/** Required setup before a test. */
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
SafetyCenterManagerWrapper.sInstance = mSafetyCenterManagerWrapper;
FeatureFlagUtils
.setEnabled(mContext, FeatureFlagUtils.SETTINGS_PRIVATE_SPACE_SETTINGS, true);
}
/** Required setup after a test. */
@After
public void tearDown() {
SafetyCenterManagerWrapper.sInstance = null;
}
/** Tests that when SC is disabled we don't set any data. */
@Test
public void onDeviceRebootedEvent_whenSafetyCenterDisabled_doesNotSetData() {
when(mSafetyCenterManagerWrapper.isEnabled(mContext)).thenReturn(false);
PrivateSpaceSafetySource.setSafetySourceData(mContext, EVENT_TYPE_DEVICE_REBOOTED);
verify(mSafetyCenterManagerWrapper, never()).setSafetySourceData(
any(), any(), any(), any());
}
/** Tests that when SC is enabled we set data. */
@Test
public void onDeviceRebootedEvent_whenSafetyCenterEnabled_setsData() {
when(mSafetyCenterManagerWrapper.isEnabled(mContext)).thenReturn(true);
PrivateSpaceSafetySource.setSafetySourceData(mContext, EVENT_TYPE_DEVICE_REBOOTED);
verify(mSafetyCenterManagerWrapper).setSafetySourceData(
any(), eq(SAFETY_SOURCE_ID), any(), eq(EVENT_TYPE_DEVICE_REBOOTED));
}
// TODO(b/295516544): Modify this test for the new trunk stable flag instead when available.
/** Tests that when the feature is disabled null data is set. */
@Test
public void setSafetySourceData_whenFeatureDisabled_setsNullData() {
when(mSafetyCenterManagerWrapper.isEnabled(mContext)).thenReturn(true);
FeatureFlagUtils
.setEnabled(mContext, FeatureFlagUtils.SETTINGS_PRIVATE_SPACE_SETTINGS, false);
PrivateSpaceSafetySource.setSafetySourceData(mContext, EVENT_TYPE_DEVICE_REBOOTED);
ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class);
verify(mSafetyCenterManagerWrapper).setSafetySourceData(
any(), eq(SAFETY_SOURCE_ID), captor.capture(), eq(EVENT_TYPE_DEVICE_REBOOTED));
SafetySourceData safetySourceData = captor.getValue();
assertThat(safetySourceData).isNull();
FeatureFlagUtils
.setEnabled(mContext, FeatureFlagUtils.SETTINGS_PRIVATE_SPACE_SETTINGS, true);
}
/** Tests that setSafetySourceData sets the source status enabled. */
@Test
public void setSafetySourceData_setsEnabled() {
when(mSafetyCenterManagerWrapper.isEnabled(mContext)).thenReturn(true);
PrivateSpaceSafetySource.setSafetySourceData(mContext, EVENT_TYPE_DEVICE_REBOOTED);
ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class);
verify(mSafetyCenterManagerWrapper).setSafetySourceData(
any(), eq(SAFETY_SOURCE_ID), captor.capture(), eq(EVENT_TYPE_DEVICE_REBOOTED));
SafetySourceData safetySourceData = captor.getValue();
SafetySourceStatus safetySourceStatus = safetySourceData.getStatus();
assertThat(safetySourceStatus.isEnabled()).isTrue();
}
/** Tests that setSafetySourceData sets the PS settings page intent. */
@Test
public void setSafetySourceData_setsPsIntent() {
when(mSafetyCenterManagerWrapper.isEnabled(mContext)).thenReturn(true);
PrivateSpaceSafetySource.setSafetySourceData(mContext, EVENT_TYPE_DEVICE_REBOOTED);
ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class);
verify(mSafetyCenterManagerWrapper).setSafetySourceData(
any(), eq(SAFETY_SOURCE_ID), captor.capture(), eq(EVENT_TYPE_DEVICE_REBOOTED));
SafetySourceData safetySourceData = captor.getValue();
SafetySourceStatus safetySourceStatus = safetySourceData.getStatus();
assertThat(safetySourceStatus.getPendingIntent().getIntent().getIdentifier())
.isEqualTo(SAFETY_SOURCE_ID);
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.privatespace;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@RunWith(AndroidJUnit4.class)
public class UseOneLockControllerTest {
@Mock private Context mContext;
private UseOneLockController mUseOneLockController;
/** Required setup before a test. */
@Before
public void setUp() {
mContext = ApplicationProvider.getApplicationContext();
final String preferenceKey = "private_space_use_one_lock";
mUseOneLockController = new UseOneLockController(mContext, preferenceKey);
}
/** Tests that the controller is always available. */
@Test
public void getAvailabilityStatus_returnsAvailable() {
assertThat(mUseOneLockController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
}

View File

@@ -21,9 +21,7 @@ import static android.safetycenter.SafetyCenterManager.EXTRA_REFRESH_SAFETY_SOUR
import static android.safetycenter.SafetyCenterManager.EXTRA_REFRESH_SAFETY_SOURCE_IDS;
import static android.safetycenter.SafetyEvent.SAFETY_EVENT_TYPE_DEVICE_REBOOTED;
import static android.safetycenter.SafetyEvent.SAFETY_EVENT_TYPE_REFRESH_REQUESTED;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
@@ -33,11 +31,14 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.safetycenter.SafetyEvent;
import android.safetycenter.SafetySourceData;
import android.util.FeatureFlagUtils;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.privatespace.PrivateSpaceSafetySource;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.After;
@@ -216,6 +217,62 @@ public class SafetySourceBroadcastReceiverTest {
assertThat(captor.getValue()).isEqualTo(BiometricsSafetySource.SAFETY_SOURCE_ID);
}
/**
* Tests that on receiving the refresh broadcast request with the PS source id, the PS data
* is set.
*/
@Test
public void onReceive_onRefresh_withPrivateSpaceSourceId_setsPrivateSpaceData() {
when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true);
Intent intent =
new Intent()
.setAction(ACTION_REFRESH_SAFETY_SOURCES)
.putExtra(
EXTRA_REFRESH_SAFETY_SOURCE_IDS,
new String[] {PrivateSpaceSafetySource.SAFETY_SOURCE_ID})
.putExtra(EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID, REFRESH_BROADCAST_ID);
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(mSafetyCenterManagerWrapper, times(1))
.setSafetySourceData(any(), captor.capture(), any(), any());
assertThat(captor.getValue()).isEqualTo(PrivateSpaceSafetySource.SAFETY_SOURCE_ID);
}
/** Tests that the PS source sets null data when it's disabled. */
// TODO(b/295516544): Modify this test for the new trunk stable flag instead when available.
@Test
public void onReceive_onRefresh_withPrivateSpaceFeatureDisabled_setsNullData() {
when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true);
FeatureFlagUtils
.setEnabled(
mApplicationContext,
FeatureFlagUtils.SETTINGS_PRIVATE_SPACE_SETTINGS,
false);
Intent intent =
new Intent()
.setAction(ACTION_REFRESH_SAFETY_SOURCES)
.putExtra(
EXTRA_REFRESH_SAFETY_SOURCE_IDS,
new String[] {PrivateSpaceSafetySource.SAFETY_SOURCE_ID})
.putExtra(EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID, REFRESH_BROADCAST_ID);
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class);
verify(mSafetyCenterManagerWrapper, times(1))
.setSafetySourceData(any(), any(), captor.capture(), any());
assertThat(captor.getValue()).isEqualTo(null);
FeatureFlagUtils
.setEnabled(
mApplicationContext,
FeatureFlagUtils.SETTINGS_PRIVATE_SPACE_SETTINGS,
true);
}
@Test
public void onReceive_onBootCompleted_setsBootCompleteEvent() {
when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true);
@@ -223,22 +280,22 @@ public class SafetySourceBroadcastReceiverTest {
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
ArgumentCaptor<SafetyEvent> captor = ArgumentCaptor.forClass(SafetyEvent.class);
verify(mSafetyCenterManagerWrapper, times(2))
verify(mSafetyCenterManagerWrapper, times(3))
.setSafetySourceData(any(), any(), any(), captor.capture());
SafetyEvent bootEvent = new SafetyEvent.Builder(SAFETY_EVENT_TYPE_DEVICE_REBOOTED).build();
assertThat(captor.getAllValues())
.containsExactlyElementsIn(Arrays.asList(bootEvent, bootEvent));
.containsExactlyElementsIn(Arrays.asList(bootEvent, bootEvent, bootEvent));
}
@Test
public void onReceive_onBootCompleted_sendsBiometricAndLockscreenData() {
public void onReceive_onBootCompleted_sendsAllSafetySourcesData() {
when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true);
Intent intent = new Intent().setAction(Intent.ACTION_BOOT_COMPLETED);
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(mSafetyCenterManagerWrapper, times(2))
verify(mSafetyCenterManagerWrapper, times(3))
.setSafetySourceData(any(), captor.capture(), any(), any());
List<String> safetySourceIdList = captor.getAllValues();
@@ -246,5 +303,7 @@ public class SafetySourceBroadcastReceiverTest {
id -> id.equals(LockScreenSafetySource.SAFETY_SOURCE_ID))).isTrue();
assertThat(safetySourceIdList.stream().anyMatch(
id -> id.equals(BiometricsSafetySource.SAFETY_SOURCE_ID))).isTrue();
assertThat(safetySourceIdList.stream().anyMatch(
id -> id.equals(PrivateSpaceSafetySource.SAFETY_SOURCE_ID))).isTrue();
}
}