Add auto lock preference inside private space settings

This includes below changes
- Adds perference for Auto Lock settings inside private space settings page.
- Feaure is behind flag
android.multiuser.support_autolock_for_private_space

Screenshots:
go/ss/ZCaYGWMDdH8bQNz.png
go/ss/AxnD8DviiT5hhkW.png
go/ss/9TMYmacyiVhyexB.png
go/ss/6SzhkGfTbDYVUVE.png

Bug: 312893140
Test: atest AutoLockPreferenceControllerTest, atest AutoLockPreferenceControllerTest

Change-Id: I95beb9d71c709002e17307e612c60b2f5087290b
This commit is contained in:
josephpv
2023-12-11 23:44:27 +00:00
committed by Joseph Vincent
parent 5245ebfd4a
commit 099ae32943
10 changed files with 659 additions and 13 deletions

View File

@@ -1483,4 +1483,18 @@
<item>2</item> <item>2</item>
</string-array> </string-array>
<!-- Entries for private space auto lock option -->
<string-array name="private_space_auto_lock_options">
<item>@string/private_space_auto_lock_every_time</item>
<item>@string/private_space_auto_lock_after_inactivity</item>
<item>@string/private_space_auto_lock_never</item>
</string-array>
<!-- Values for private space auto lock option -->
<string-array name="private_space_auto_lock_options_values" translatable="false">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources> </resources>

View File

@@ -1237,10 +1237,22 @@
<string name="private_space_face_unlock_title">Face Unlock for Private Space</string> <string name="private_space_face_unlock_title">Face Unlock for Private Space</string>
<!-- Biometric category title - biometric options for unlocking the device. [CHAR LIMIT=50] --> <!-- Biometric category title - biometric options for unlocking the device. [CHAR LIMIT=50] -->
<string name="private_space_category_ways_to_unlock">Ways to unlock</string> <string name="private_space_category_ways_to_unlock">Ways to unlock</string>
<!-- Summary for one lock when device screen lock is used as private profile lock. [CHAR LIMIT=60] --> <!-- Summary for one lock when device screen lock is used as private profile lock. [CHAR LIMIT=40] -->
<string name="private_space_screen_lock_summary">Same as device screen lock</string> <string name="private_space_screen_lock_summary">Same as device screen lock</string>
<!-- Dialog message to choose a new lock for Private Space. [CHAR LIMIT=50] --> <!-- Dialog message to choose a new lock for Private Space. [CHAR LIMIT=50] -->
<string name="private_space_new_lock_title">Choose a new lock for Private Space?</string> <string name="private_space_new_lock_title">Choose a new lock for Private Space?</string>
<!-- Title for the preference for auto lock private space. [CHAR LIMIT=40] -->
<string name="private_space_auto_lock_title">Lock automatically</string>
<!-- Title for private space auto lock settings page. [CHAR LIMIT=50] -->
<string name="private_space_auto_lock_page_title">Lock private space automatically</string>
<!-- Description for private space auto lock settings page. [CHAR LIMIT=NONE] -->
<string name="private_space_auto_lock_page_summary">You can lock your private space automatically if you haven\t used your device for a period of time</string>
<!-- Configure auto lock: Value for auto lock configuration to lock private space every time device locks. [CHAR LIMIT=40] -->
<string name="private_space_auto_lock_every_time">Every time device locks</string>
<!-- Configure auto lock: Value for auto lock configuration to lock private space after 5 minutes of inactivity. [CHAR LIMIT=40] -->
<string name="private_space_auto_lock_after_inactivity">After 5 minutes of inactivity</string>
<!-- Configure auto lock: Value for auto lock configuration to never lock private space. [CHAR LIMIT=20] -->
<string name="private_space_auto_lock_never">Never</string>
<!-- Title for the preference to hide Private Space. [CHAR LIMIT=60] --> <!-- Title for the preference to hide Private Space. [CHAR LIMIT=60] -->
<string name="private_space_hide_title">Hide when locked</string> <string name="private_space_hide_title">Hide when locked</string>
<!-- Title for the hide Private Space setting. [CHAR LIMIT=60] --> <!-- Title for the hide Private Space setting. [CHAR LIMIT=60] -->

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/private_space_auto_lock_page_title"/>

View File

@@ -36,6 +36,13 @@
<PreferenceCategory <PreferenceCategory
android:title="@string/security_header"> android:title="@string/security_header">
<Preference
android:key="private_space_auto_lock"
android:title="@string/private_space_auto_lock_title"
android:fragment="com.android.settings.privatespace.autolock.AutoLockSettingsFragment"
settings:controller="com.android.settings.privatespace.autolock.AutoLockPreferenceController"
settings:searchable="false" />
<Preference <Preference
android:key="private_space_use_one_lock" android:key="private_space_use_one_lock"
android:title="@string/private_space_lock_title" android:title="@string/private_space_lock_title"

View File

@@ -18,6 +18,8 @@ package com.android.settings.privatespace;
import static android.os.UserManager.USER_TYPE_PROFILE_PRIVATE; import static android.os.UserManager.USER_TYPE_PROFILE_PRIVATE;
import static android.provider.Settings.Secure.HIDE_PRIVATESPACE_ENTRY_POINT; import static android.provider.Settings.Secure.HIDE_PRIVATESPACE_ENTRY_POINT;
import static android.provider.Settings.Secure.PRIVATE_SPACE_AUTO_LOCK;
import static android.provider.Settings.Secure.PRIVATE_SPACE_AUTO_LOCK_NEVER;
import static android.provider.Settings.Secure.USER_SETUP_COMPLETE; import static android.provider.Settings.Secure.USER_SETUP_COMPLETE;
import android.app.ActivityManager; import android.app.ActivityManager;
@@ -46,7 +48,6 @@ import java.util.List;
/** A class to help with the creation / deletion of Private Space */ /** A class to help with the creation / deletion of Private Space */
public class PrivateSpaceMaintainer { public class PrivateSpaceMaintainer {
private static final String TAG = "PrivateSpaceMaintainer"; private static final String TAG = "PrivateSpaceMaintainer";
@GuardedBy("this") @GuardedBy("this")
private static PrivateSpaceMaintainer sPrivateSpaceMaintainer; private static PrivateSpaceMaintainer sPrivateSpaceMaintainer;
@@ -59,6 +60,9 @@ public class PrivateSpaceMaintainer {
/** This is the default value for the hide private space entry point settings. */ /** This is the default value for the hide private space entry point settings. */
public static final int HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL = 0; public static final int HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL = 0;
public static final int HIDE_PRIVATE_SPACE_ENTRY_POINT_ENABLED_VAL = 1; public static final int HIDE_PRIVATE_SPACE_ENTRY_POINT_ENABLED_VAL = 1;
/** Default value for private space auto lock settings. */
@Settings.Secure.PrivateSpaceAutoLockOption
public static final int PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL = PRIVATE_SPACE_AUTO_LOCK_NEVER;
public enum ErrorDeletingPrivateSpace { public enum ErrorDeletingPrivateSpace {
DELETE_PS_ERROR_NONE, DELETE_PS_ERROR_NONE,
@@ -223,6 +227,14 @@ public class PrivateSpaceMaintainer {
Settings.Secure.putInt(mContext.getContentResolver(), HIDE_PRIVATESPACE_ENTRY_POINT, value); Settings.Secure.putInt(mContext.getContentResolver(), HIDE_PRIVATESPACE_ENTRY_POINT, value);
} }
/** Sets the setting for private space auto lock option. */
public void setPrivateSpaceAutoLockSetting(
@Settings.Secure.PrivateSpaceAutoLockOption int value) {
if (isPrivateSpaceAutoLockSupported()) {
Settings.Secure.putInt(mContext.getContentResolver(), PRIVATE_SPACE_AUTO_LOCK, value);
}
}
/** @return the setting to show PS entry point. */ /** @return the setting to show PS entry point. */
public int getHidePrivateSpaceEntryPointSetting() { public int getHidePrivateSpaceEntryPointSetting() {
return Settings.Secure.getInt( return Settings.Secure.getInt(
@@ -231,6 +243,18 @@ public class PrivateSpaceMaintainer {
HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL); HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL);
} }
/** @return the setting for PS auto lock option. */
@Settings.Secure.PrivateSpaceAutoLockOption
public int getPrivateSpaceAutoLockSetting() {
if (isPrivateSpaceAutoLockSupported()) {
return Settings.Secure.getInt(
mContext.getContentResolver(),
PRIVATE_SPACE_AUTO_LOCK,
PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL);
}
return PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL;
}
/** /**
* Returns true if private space exists and quiet mode is successfully enabled, otherwise * Returns true if private space exists and quiet mode is successfully enabled, otherwise
* returns false * returns false
@@ -264,6 +288,7 @@ public class PrivateSpaceMaintainer {
private void resetPrivateSpaceSettings() { private void resetPrivateSpaceSettings() {
setHidePrivateSpaceEntryPointSetting(HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL); setHidePrivateSpaceEntryPointSetting(HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL);
setPrivateSpaceAutoLockSetting(PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL);
} }
/** /**
@@ -275,4 +300,9 @@ public class PrivateSpaceMaintainer {
Settings.Secure.putIntForUser(mContext.getContentResolver(), USER_SETUP_COMPLETE, Settings.Secure.putIntForUser(mContext.getContentResolver(), USER_SETUP_COMPLETE,
1, mUserHandle.getIdentifier()); 1, mUserHandle.getIdentifier());
} }
private boolean isPrivateSpaceAutoLockSupported() {
return android.os.Flags.allowPrivateProfile()
&& android.multiuser.Flags.supportAutolockForPrivateSpace();
}
} }

View File

@@ -0,0 +1,58 @@
/*
* 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.autolock;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.privatespace.PrivateSpaceMaintainer;
public class AutoLockPreferenceController extends BasePreferenceController {
private static final String TAG = "AutoLockPreferenceCtrl";
private final CharSequence[] mAutoLockRadioOptions;
private final PrivateSpaceMaintainer mPrivateSpaceMaintainer;
public AutoLockPreferenceController(@NonNull Context context, @NonNull String key) {
super(context, key);
mPrivateSpaceMaintainer = PrivateSpaceMaintainer.getInstance(context);
mAutoLockRadioOptions =
context.getResources().getStringArray(R.array.private_space_auto_lock_options);
}
@Override
public int getAvailabilityStatus() {
return android.os.Flags.allowPrivateProfile()
&& android.multiuser.Flags.supportAutolockForPrivateSpace()
? AVAILABLE
: UNSUPPORTED_ON_DEVICE;
}
@NonNull
@Override
public CharSequence getSummary() {
try {
return mAutoLockRadioOptions[mPrivateSpaceMaintainer.getPrivateSpaceAutoLockSetting()];
} catch (ArrayIndexOutOfBoundsException exception) {
Log.e(TAG, "Invalid private space auto lock setting value" + exception.getMessage());
}
return mAutoLockRadioOptions[PrivateSpaceMaintainer.PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL];
}
}

View File

@@ -0,0 +1,148 @@
/*
* 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.autolock;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.privatespace.PrivateSpaceMaintainer;
import com.android.settings.widget.RadioButtonPickerFragment;
import com.android.settingslib.widget.CandidateInfo;
import com.android.settingslib.widget.TopIntroPreference;
import java.util.ArrayList;
import java.util.List;
public class AutoLockSettingsFragment extends RadioButtonPickerFragment {
private static final String TAG = "PSAutoLockSetting";
private PrivateSpaceMaintainer mPrivateSpaceMaintainer;
private CharSequence[] mAutoLockRadioOptions;
private CharSequence[] mAutoLockRadioValues;
@Override
public void onCreate(@NonNull Bundle icicle) {
if (android.os.Flags.allowPrivateProfile()
&& android.multiuser.Flags.supportAutolockForPrivateSpace()) {
super.onCreate(icicle);
}
}
@Override
public void onStart() {
super.onStart();
if (mPrivateSpaceMaintainer.isPrivateSpaceLocked()) {
finish();
}
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
mPrivateSpaceMaintainer = PrivateSpaceMaintainer.getInstance(context);
mAutoLockRadioOptions =
context.getResources().getStringArray(R.array.private_space_auto_lock_options);
mAutoLockRadioValues =
context.getResources()
.getStringArray(R.array.private_space_auto_lock_options_values);
}
@Override
protected void addStaticPreferences(PreferenceScreen screen) {
final TopIntroPreference introPreference = new TopIntroPreference(screen.getContext());
introPreference.setTitle(R.string.private_space_auto_lock_page_summary);
screen.addPreference(introPreference);
}
@Override
public int getMetricsCategory() {
return SettingsEnums.PRIVATE_SPACE_SETTINGS;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.private_space_auto_lock_settings;
}
@Override
protected List<? extends CandidateInfo> getCandidates() {
final List<CandidateInfo> candidates = new ArrayList<>();
if (mAutoLockRadioValues != null) {
for (int i = 0; i < mAutoLockRadioValues.length; ++i) {
candidates.add(
new AutoLockCandidateInfo(
mAutoLockRadioOptions[i], mAutoLockRadioValues[i].toString()));
}
} else {
Log.e(TAG, "Autolock options do not exist.");
}
return candidates;
}
@Override
protected String getDefaultKey() {
return Integer.toString(mPrivateSpaceMaintainer.getPrivateSpaceAutoLockSetting());
}
@Override
protected boolean setDefaultKey(String key) {
try {
@Settings.Secure.PrivateSpaceAutoLockOption final int value = Integer.parseInt(key);
mPrivateSpaceMaintainer.setPrivateSpaceAutoLockSetting(value);
} catch (NumberFormatException e) {
Log.e(TAG, "could not persist screen timeout setting", e);
}
return true;
}
private static class AutoLockCandidateInfo extends CandidateInfo {
private final CharSequence mLabel;
private final String mKey;
AutoLockCandidateInfo(CharSequence label, String key) {
super(true);
mLabel = label;
mKey = key;
}
@NonNull
@Override
public CharSequence loadLabel() {
return mLabel;
}
@Nullable
@Override
public Drawable loadIcon() {
return null;
}
@NonNull
@Override
public String getKey() {
return mKey;
}
}
}

View File

@@ -16,8 +16,11 @@
package com.android.settings.privatespace; package com.android.settings.privatespace;
import static android.provider.Settings.Secure.PRIVATE_SPACE_AUTO_LOCK;
import static com.android.settings.privatespace.PrivateSpaceMaintainer.HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL; import static com.android.settings.privatespace.PrivateSpaceMaintainer.HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL;
import static com.android.settings.privatespace.PrivateSpaceMaintainer.HIDE_PRIVATE_SPACE_ENTRY_POINT_ENABLED_VAL; import static com.android.settings.privatespace.PrivateSpaceMaintainer.HIDE_PRIVATE_SPACE_ENTRY_POINT_ENABLED_VAL;
import static com.android.settings.privatespace.PrivateSpaceMaintainer.PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
@@ -25,7 +28,9 @@ import android.app.ActivityManager;
import android.app.IActivityManager; import android.app.IActivityManager;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.os.Flags;
import android.os.RemoteException; import android.os.RemoteException;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings; import android.provider.Settings;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
@@ -36,6 +41,7 @@ import com.android.settings.privatespace.PrivateSpaceMaintainer.ErrorDeletingPri
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
@@ -45,6 +51,7 @@ public class PrivateSpaceMaintainerTest {
private static final String TAG = "PSMaintainerTest"; private static final String TAG = "PSMaintainerTest";
private Context mContext; private Context mContext;
private ContentResolver mContentResolver; private ContentResolver mContentResolver;
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
/** Required setup before a test. */ /** Required setup before a test. */
@Before @Before
@@ -75,8 +82,8 @@ public class PrivateSpaceMaintainerTest {
} }
/** /**
* Tests that {@link PrivateSpaceMaintainer#deletePrivateSpace()} returns error when PS does * Tests that {@link PrivateSpaceMaintainer#deletePrivateSpace()} returns error when PS does not
* not exist. * exist.
*/ */
@Test @Test
public void deletePrivateSpace_psDoesNotExist_returnsNoPSError() { public void deletePrivateSpace_psDoesNotExist_returnsNoPSError() {
@@ -100,8 +107,8 @@ public class PrivateSpaceMaintainerTest {
} }
/** /**
* Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when PS exists still * Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when PS exists still returns
* returns true. * true.
*/ */
@Test @Test
public void createPrivateSpace_psExists_returnsFalse() { public void createPrivateSpace_psExists_returnsFalse() {
@@ -114,11 +121,11 @@ public class PrivateSpaceMaintainerTest {
} }
/** /**
* Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when no PS exists resets PS * Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when no PS exists resets hide
* Settings. * PS Settings.
*/ */
@Test @Test
public void createPrivateSpace_psDoesNotExist_resetsPSSettings() { public void createPrivateSpace_psDoesNotExist_resetsHidePSSettings() {
PrivateSpaceMaintainer privateSpaceMaintainer = PrivateSpaceMaintainer privateSpaceMaintainer =
PrivateSpaceMaintainer.getInstance(mContext); PrivateSpaceMaintainer.getInstance(mContext);
Settings.Secure.putInt( Settings.Secure.putInt(
@@ -134,10 +141,10 @@ public class PrivateSpaceMaintainerTest {
/** /**
* Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when PS exist does not reset * Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when PS exist does not reset
* PS Settings. * hide PS Settings.
*/ */
@Test @Test
public void createPrivateSpace_psExists_doesNotResetPSSettings() { public void createPrivateSpace_psExists_doesNotResetHidePSSettings() {
PrivateSpaceMaintainer privateSpaceMaintainer = PrivateSpaceMaintainer privateSpaceMaintainer =
PrivateSpaceMaintainer.getInstance(mContext); PrivateSpaceMaintainer.getInstance(mContext);
privateSpaceMaintainer.createPrivateSpace(); privateSpaceMaintainer.createPrivateSpace();
@@ -212,8 +219,8 @@ public class PrivateSpaceMaintainerTest {
} }
/** /**
* Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when PS exists does not * Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when PS exists does not change
* change USER_SETUP_COMPLETE setting. * USER_SETUP_COMPLETE setting.
*/ */
@Test @Test
public void createPrivateSpace_pSExists_doesNotChangeUserSetupSetting() { public void createPrivateSpace_pSExists_doesNotChangeUserSetupSetting() {
@@ -225,6 +232,52 @@ public class PrivateSpaceMaintainerTest {
assertThat(getSecureUserSetupComplete()).isEqualTo(1); assertThat(getSecureUserSetupComplete()).isEqualTo(1);
} }
/**
* Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when no PS exists resets PS
* auto lock Settings.
*/
@Test
public void createPrivateSpace_psDoesNotExist_resetsPSAutoLockSettings() {
mSetFlagsRule.enableFlags(
Flags.FLAG_ALLOW_PRIVATE_PROFILE,
android.multiuser.Flags.FLAG_SUPPORT_AUTOLOCK_FOR_PRIVATE_SPACE);
final int autoLockOption = 2;
PrivateSpaceMaintainer privateSpaceMaintainer =
PrivateSpaceMaintainer.getInstance(mContext);
Settings.Secure.putInt(
mContentResolver, Settings.Secure.PRIVATE_SPACE_AUTO_LOCK, autoLockOption);
privateSpaceMaintainer.deletePrivateSpace();
privateSpaceMaintainer.createPrivateSpace();
assertThat(privateSpaceMaintainer.getPrivateSpaceAutoLockSetting())
.isEqualTo(PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL);
assertThat(Settings.Secure.getInt(mContentResolver, PRIVATE_SPACE_AUTO_LOCK, -1))
.isEqualTo(PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL);
}
/**
* Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when PS exist does not reset
* PS auto lock setting.
*/
@Test
public void createPrivateSpace_psExists_doesNotResetPSAutoLockSettings() {
mSetFlagsRule.enableFlags(
Flags.FLAG_ALLOW_PRIVATE_PROFILE,
android.multiuser.Flags.FLAG_SUPPORT_AUTOLOCK_FOR_PRIVATE_SPACE);
final int privateSpaceAutLockValue = 1;
PrivateSpaceMaintainer privateSpaceMaintainer =
PrivateSpaceMaintainer.getInstance(mContext);
privateSpaceMaintainer.createPrivateSpace();
Settings.Secure.putInt(
mContentResolver,
Settings.Secure.PRIVATE_SPACE_AUTO_LOCK,
privateSpaceAutLockValue);
privateSpaceMaintainer.createPrivateSpace();
assertThat(privateSpaceMaintainer.getPrivateSpaceAutoLockSetting())
.isEqualTo(privateSpaceAutLockValue);
}
private int getSecureUserSetupComplete() { private int getSecureUserSetupComplete() {
PrivateSpaceMaintainer privateSpaceMaintainer = PrivateSpaceMaintainer privateSpaceMaintainer =
PrivateSpaceMaintainer.getInstance(mContext); PrivateSpaceMaintainer.getInstance(mContext);

View File

@@ -0,0 +1,142 @@
/*
* 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.autolock;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
import static com.android.settings.privatespace.PrivateSpaceMaintainer.PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Flags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class AutoLockPreferenceControllerTest {
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private static final String KEY = "private_space_auto_lock";
private Context mContext;
private AutoLockPreferenceController mAutoLockPreferenceController;
private ContentResolver mContentResolver;
@Settings.Secure.PrivateSpaceAutoLockOption private int mOriginalAutoLockValue;
/** Required setup before a test. */
@Before
public void setUp() {
mContext = spy(ApplicationProvider.getApplicationContext());
mContentResolver = mContext.getContentResolver();
mAutoLockPreferenceController = new AutoLockPreferenceController(mContext, KEY);
mOriginalAutoLockValue =
Settings.Secure.getInt(
mContentResolver,
Settings.Secure.PRIVATE_SPACE_AUTO_LOCK,
PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL);
}
@After
public void tearDown() {
Settings.Secure.putInt(
mContentResolver, Settings.Secure.PRIVATE_SPACE_AUTO_LOCK, mOriginalAutoLockValue);
}
/**
* Tests that the controller is available when both allow private profile and auto lock support
* flags are enabled.
*/
@Test
public void getAvailabilityStatus_withAutoLockFlagEnabled_returnsAvailable() {
mSetFlagsRule.enableFlags(
Flags.FLAG_ALLOW_PRIVATE_PROFILE,
android.multiuser.Flags.FLAG_SUPPORT_AUTOLOCK_FOR_PRIVATE_SPACE);
assertThat(mAutoLockPreferenceController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
/** Tests that the controller is not available when auto lock flag is off. */
@Test
public void getAvailabilityStatus_withAutoLockFlagDisabled_returnsNull() {
mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE);
mSetFlagsRule.disableFlags(android.multiuser.Flags.FLAG_SUPPORT_AUTOLOCK_FOR_PRIVATE_SPACE);
assertThat(mAutoLockPreferenceController.getAvailabilityStatus())
.isEqualTo(UNSUPPORTED_ON_DEVICE);
}
/**
* Tests that auto lock preference displays the correct summary for option - every time device
* locks.
*/
@Test
public void getSummary_whenOptionEveryTimeDeviceLocks_returnsEveryTimeDeviceLocks() {
mSetFlagsRule.enableFlags(
Flags.FLAG_ALLOW_PRIVATE_PROFILE,
android.multiuser.Flags.FLAG_SUPPORT_AUTOLOCK_FOR_PRIVATE_SPACE);
Settings.Secure.putInt(
mContentResolver,
Settings.Secure.PRIVATE_SPACE_AUTO_LOCK,
Settings.Secure.PRIVATE_SPACE_AUTO_LOCK_ON_DEVICE_LOCK);
assertThat(mAutoLockPreferenceController.getSummary().toString())
.isEqualTo("Every time device locks");
}
/**
* Tests that auto lock preference displays the correct summary for option - lock after 5
* minutes of inactivity.
*/
@Test
public void getSummary_whenOptionAfter5MinutesOfInactivity_returnsAfter5MinutesOfInactivity() {
mSetFlagsRule.enableFlags(
Flags.FLAG_ALLOW_PRIVATE_PROFILE,
android.multiuser.Flags.FLAG_SUPPORT_AUTOLOCK_FOR_PRIVATE_SPACE);
Settings.Secure.putInt(
mContentResolver,
Settings.Secure.PRIVATE_SPACE_AUTO_LOCK,
Settings.Secure.PRIVATE_SPACE_AUTO_LOCK_AFTER_INACTIVITY);
assertThat(mAutoLockPreferenceController.getSummary().toString())
.isEqualTo("After 5 minutes of inactivity");
}
/** Tests that auto lock preference displays the correct summary for option - Never. */
@Test
public void getSummary_whenOptionNever_returnsNever() {
mSetFlagsRule.enableFlags(
Flags.FLAG_ALLOW_PRIVATE_PROFILE,
android.multiuser.Flags.FLAG_SUPPORT_AUTOLOCK_FOR_PRIVATE_SPACE);
Settings.Secure.putInt(
mContentResolver,
Settings.Secure.PRIVATE_SPACE_AUTO_LOCK,
Settings.Secure.PRIVATE_SPACE_AUTO_LOCK_NEVER);
assertThat(mAutoLockPreferenceController.getSummary().toString()).isEqualTo("Never");
}
}

View File

@@ -0,0 +1,162 @@
/*
* Copyright (C) 2024 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.autolock;
import static android.provider.Settings.Secure.PRIVATE_SPACE_AUTO_LOCK;
import static android.provider.Settings.Secure.PRIVATE_SPACE_AUTO_LOCK_AFTER_INACTIVITY;
import static android.provider.Settings.Secure.PRIVATE_SPACE_AUTO_LOCK_NEVER;
import static android.provider.Settings.Secure.PRIVATE_SPACE_AUTO_LOCK_ON_DEVICE_LOCK;
import static com.android.settings.privatespace.PrivateSpaceMaintainer.PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.app.settings.SettingsEnums;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.os.Flags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import androidx.test.annotation.UiThreadTest;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settingslib.widget.CandidateInfo;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
@RunWith(AndroidJUnit4.class)
public class AutoLockSettingsFragmentTest {
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private AutoLockSettingsFragment mFragment;
private Context mContext;
private Resources mResources;
private ContentResolver mContentResolver;
@Settings.Secure.PrivateSpaceAutoLockOption private int mOriginalAutoLockValue;
@Before
@UiThreadTest
public void setup() {
mContext = spy(ApplicationProvider.getApplicationContext());
mFragment = spy(new AutoLockSettingsFragment());
mContentResolver = mContext.getContentResolver();
mResources = spy(mContext.getResources());
mOriginalAutoLockValue =
Settings.Secure.getInt(
mContentResolver,
Settings.Secure.PRIVATE_SPACE_AUTO_LOCK,
PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL);
when(mResources.getStringArray(
com.android.settings.R.array.private_space_auto_lock_options))
.thenReturn(
new String[] {
"Every time device locks", "After 5 minutes of inactivity", "Never"
});
when(mResources.getStringArray(
com.android.settings.R.array.private_space_auto_lock_options_values))
.thenReturn(new String[] {"0", "1", "2"});
doReturn(mResources).when(mContext).getResources();
}
@After
public void tearDown() {
Settings.Secure.putInt(
mContentResolver, Settings.Secure.PRIVATE_SPACE_AUTO_LOCK, mOriginalAutoLockValue);
}
@Test
public void verifyMetricsConstant() {
mSetFlagsRule.enableFlags(
Flags.FLAG_ALLOW_PRIVATE_PROFILE,
android.multiuser.Flags.FLAG_SUPPORT_AUTOLOCK_FOR_PRIVATE_SPACE);
assertThat(mFragment.getMetricsCategory()).isEqualTo(SettingsEnums.PRIVATE_SPACE_SETTINGS);
}
@Test
@UiThreadTest
public void getCandidates_returnsCandidateInfoListWithAllKeys() {
mSetFlagsRule.enableFlags(
Flags.FLAG_ALLOW_PRIVATE_PROFILE,
android.multiuser.Flags.FLAG_SUPPORT_AUTOLOCK_FOR_PRIVATE_SPACE);
mFragment.onAttach(mContext);
final List<? extends CandidateInfo> candidates = mFragment.getCandidates();
assertThat(candidates.size()).isEqualTo(3);
assertThat(candidates.get(0).getKey()).isEqualTo("0");
assertThat(candidates.get(1).getKey()).isEqualTo("1");
assertThat(candidates.get(2).getKey()).isEqualTo("2");
}
@Test
@UiThreadTest
public void getDefaultKey_returnsStoredAutoLockOptionsValue() {
mSetFlagsRule.enableFlags(
Flags.FLAG_ALLOW_PRIVATE_PROFILE,
android.multiuser.Flags.FLAG_SUPPORT_AUTOLOCK_FOR_PRIVATE_SPACE);
mFragment.onAttach(mContext);
Settings.Secure.putInt(
mContentResolver, PRIVATE_SPACE_AUTO_LOCK, PRIVATE_SPACE_AUTO_LOCK_NEVER);
assertThat(mFragment.getDefaultKey()).isEqualTo("2");
Settings.Secure.putInt(
mContentResolver, PRIVATE_SPACE_AUTO_LOCK, PRIVATE_SPACE_AUTO_LOCK_ON_DEVICE_LOCK);
assertThat(mFragment.getDefaultKey()).isEqualTo("0");
Settings.Secure.putInt(
mContentResolver,
PRIVATE_SPACE_AUTO_LOCK,
PRIVATE_SPACE_AUTO_LOCK_AFTER_INACTIVITY);
assertThat(mFragment.getDefaultKey()).isEqualTo("1");
}
@Test
@UiThreadTest
public void setDefaultKey_storesCorrectAutoLockOptionValue() {
mSetFlagsRule.enableFlags(
Flags.FLAG_ALLOW_PRIVATE_PROFILE,
android.multiuser.Flags.FLAG_SUPPORT_AUTOLOCK_FOR_PRIVATE_SPACE);
mFragment.onAttach(mContext);
mFragment.setDefaultKey("2");
assertThat(Settings.Secure.getInt(mContentResolver, PRIVATE_SPACE_AUTO_LOCK, -1))
.isEqualTo(PRIVATE_SPACE_AUTO_LOCK_NEVER);
mFragment.setDefaultKey("1");
assertThat(Settings.Secure.getInt(mContentResolver, PRIVATE_SPACE_AUTO_LOCK, -1))
.isEqualTo(PRIVATE_SPACE_AUTO_LOCK_AFTER_INACTIVITY);
mFragment.setDefaultKey("0");
assertThat(Settings.Secure.getInt(mContentResolver, PRIVATE_SPACE_AUTO_LOCK, -1))
.isEqualTo(PRIVATE_SPACE_AUTO_LOCK_ON_DEVICE_LOCK);
}
}