Add handling for main Supervision settings toggle.

This change introduces the basic behavior for the main toggle switch to
enable supervision. There is currently no PIN setup dialog when the
toggle is clicked. That will be added as a follow-up.

Intake bug: b/379312924

Change-Id: I75d6b2de1a7234c6da26fcdcf1d933eed3106a8c
Test: atest SupervisionMainSwitchPreferenceTest
Bug: 392694561
Flag: android.app.supervision.flags.enable_supervision_settings_screen
This commit is contained in:
Yvonne Jiang
2025-01-28 02:32:44 -08:00
parent 96d615ad18
commit 4f74b50b3a
2 changed files with 32 additions and 2 deletions

View File

@@ -49,7 +49,6 @@ class SupervisionMainSwitchPreference :
override val sensitivityLevel: Int override val sensitivityLevel: Int
get() = SensitivityLevel.HIGH_SENSITIVITY get() = SensitivityLevel.HIGH_SENSITIVITY
// TODO(b/390505725): Listen for changes in supervision state.
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
private class SupervisionMainSwitchStorage(private val context: Context) : private class SupervisionMainSwitchStorage(private val context: Context) :
NoOpKeyedObservable<String>(), KeyValueStore { NoOpKeyedObservable<String>(), KeyValueStore {
@@ -61,7 +60,11 @@ class SupervisionMainSwitchPreference :
as T as T
override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) { override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
// TODO(b/383402852): implement handling of main toggle. // TODO(b/392694561): add PIN protection to main toggle.
if (key == KEY && value is Boolean) {
val supervisionManager = context.getSystemService(SupervisionManager::class.java)
supervisionManager.setSupervisionEnabled(value)
}
} }
} }

View File

@@ -28,6 +28,7 @@ import org.junit.runner.RunWith
import org.mockito.kotlin.doReturn import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock import org.mockito.kotlin.mock
import org.mockito.kotlin.stub import org.mockito.kotlin.stub
import org.mockito.kotlin.verify
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class SupervisionMainSwitchPreferenceTest { class SupervisionMainSwitchPreferenceTest {
@@ -59,6 +60,32 @@ class SupervisionMainSwitchPreferenceTest {
assertThat(getMainSwitchPreference().isChecked).isFalse() assertThat(getMainSwitchPreference().isChecked).isFalse()
} }
@Test
fun toggleOn() {
setSupervisionEnabled(false)
val widget = getMainSwitchPreference()
assertThat(widget.isChecked).isFalse()
widget.performClick()
assertThat(widget.isChecked).isTrue()
verify(mockSupervisionManager).setSupervisionEnabled(true)
}
@Test
fun toggleOff() {
setSupervisionEnabled(true)
val widget = getMainSwitchPreference()
assertThat(widget.isChecked).isTrue()
widget.performClick()
assertThat(widget.isChecked).isFalse()
verify(mockSupervisionManager).setSupervisionEnabled(false)
}
private fun getMainSwitchPreference(): MainSwitchPreference = private fun getMainSwitchPreference(): MainSwitchPreference =
preference.createAndBindWidget(context) preference.createAndBindWidget(context)