Create data store for supervision safe sites preference.
Test: atest SupervisionWebContentFiltersScreenTest Test: atest SupervisionSafeSitesPreferenceTest Test: deployed locally to a physical device Flag: android.app.supervision.flags.enable_web_content_filters_screen Bug: 401568468 Change-Id: I7fe8a9c5932b4c8f63c4067ba6914eb73d0e2373
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (C) 2025 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.supervision
|
||||
|
||||
import android.content.Context
|
||||
import android.provider.Settings.Secure.BROWSER_CONTENT_FILTERS_ENABLED
|
||||
import com.android.settingslib.datastore.AbstractKeyedDataObservable
|
||||
import com.android.settingslib.datastore.HandlerExecutor
|
||||
import com.android.settingslib.datastore.KeyValueStore
|
||||
import com.android.settingslib.datastore.KeyedObserver
|
||||
import com.android.settingslib.datastore.SettingsSecureStore
|
||||
import com.android.settingslib.datastore.SettingsStore
|
||||
|
||||
/** Datastore of the safe sites preference. */
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class SupervisionSafeSitesDataStore(
|
||||
private val context: Context,
|
||||
private val settingsStore: SettingsStore = SettingsSecureStore.get(context),
|
||||
) : AbstractKeyedDataObservable<String>(), KeyedObserver<String>, KeyValueStore {
|
||||
|
||||
override fun contains(key: String) =
|
||||
key == SupervisionBlockExplicitSitesPreference.KEY ||
|
||||
key == SupervisionAllowAllSitesPreference.KEY
|
||||
|
||||
override fun <T : Any> getValue(key: String, valueType: Class<T>): T? {
|
||||
val settingValue = (settingsStore.getBoolean(BROWSER_CONTENT_FILTERS_ENABLED) == true)
|
||||
return when (key) {
|
||||
SupervisionAllowAllSitesPreference.KEY -> !settingValue
|
||||
|
||||
SupervisionBlockExplicitSitesPreference.KEY -> settingValue
|
||||
|
||||
else -> null
|
||||
}
|
||||
as T?
|
||||
}
|
||||
|
||||
override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
|
||||
if (value !is Boolean) return
|
||||
when (key) {
|
||||
SupervisionAllowAllSitesPreference.KEY ->
|
||||
settingsStore.setBoolean(BROWSER_CONTENT_FILTERS_ENABLED, !value)
|
||||
|
||||
SupervisionBlockExplicitSitesPreference.KEY ->
|
||||
settingsStore.setBoolean(BROWSER_CONTENT_FILTERS_ENABLED, value)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFirstObserverAdded() {
|
||||
// observe the underlying storage key
|
||||
settingsStore.addObserver(BROWSER_CONTENT_FILTERS_ENABLED, this, HandlerExecutor.main)
|
||||
}
|
||||
|
||||
override fun onKeyChanged(key: String, reason: Int) {
|
||||
// forward data change to preference hierarchy key
|
||||
notifyChange(SupervisionBlockExplicitSitesPreference.KEY, reason)
|
||||
notifyChange(SupervisionAllowAllSitesPreference.KEY, reason)
|
||||
}
|
||||
|
||||
override fun onLastObserverRemoved() {
|
||||
settingsStore.removeObserver(BROWSER_CONTENT_FILTERS_ENABLED, this)
|
||||
}
|
||||
}
|
||||
@@ -18,9 +18,7 @@ package com.android.settings.supervision
|
||||
import android.content.Context
|
||||
import androidx.preference.Preference
|
||||
import com.android.settings.R
|
||||
import com.android.settingslib.datastore.KeyValueStore
|
||||
import com.android.settingslib.datastore.Permissions
|
||||
import com.android.settingslib.datastore.SettingsSecureStore
|
||||
import com.android.settingslib.metadata.BooleanValuePreference
|
||||
import com.android.settingslib.metadata.PreferenceMetadata
|
||||
import com.android.settingslib.metadata.ReadWritePermit
|
||||
@@ -30,9 +28,10 @@ import com.android.settingslib.preference.forEachRecursively
|
||||
import com.android.settingslib.widget.SelectorWithWidgetPreference
|
||||
|
||||
/** Base class of web content filters Safe sites preferences. */
|
||||
sealed class SupervisionSafeSitesPreference :
|
||||
BooleanValuePreference, SelectorWithWidgetPreference.OnClickListener, PreferenceBinding {
|
||||
override fun storage(context: Context): KeyValueStore = SettingsSecureStore.get(context)
|
||||
sealed class SupervisionSafeSitesPreference(
|
||||
protected val dataStore: SupervisionSafeSitesDataStore
|
||||
) : BooleanValuePreference, SelectorWithWidgetPreference.OnClickListener, PreferenceBinding {
|
||||
override fun storage(context: Context) = dataStore
|
||||
|
||||
override fun getReadPermissions(context: Context) = Permissions.EMPTY
|
||||
|
||||
@@ -64,15 +63,15 @@ sealed class SupervisionSafeSitesPreference :
|
||||
override fun bind(preference: Preference, metadata: PreferenceMetadata) {
|
||||
super.bind(preference, metadata)
|
||||
(preference as SelectorWithWidgetPreference).also {
|
||||
// TODO(b/401568468): Set the isChecked value using stored values.
|
||||
it.isChecked = (it.key == SupervisionAllowAllSitesPreference.KEY)
|
||||
it.isChecked = (dataStore.getBoolean(it.key) == true)
|
||||
it.setOnClickListener(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The "Try to block explicit sites" preference. */
|
||||
class SupervisionBlockExplicitSitesPreference : SupervisionSafeSitesPreference() {
|
||||
class SupervisionBlockExplicitSitesPreference(dataStore: SupervisionSafeSitesDataStore) :
|
||||
SupervisionSafeSitesPreference(dataStore) {
|
||||
|
||||
override val key
|
||||
get() = KEY
|
||||
@@ -89,7 +88,8 @@ class SupervisionBlockExplicitSitesPreference : SupervisionSafeSitesPreference()
|
||||
}
|
||||
|
||||
/** The "Allow all sites" preference. */
|
||||
class SupervisionAllowAllSitesPreference : SupervisionSafeSitesPreference() {
|
||||
class SupervisionAllowAllSitesPreference(dataStore: SupervisionSafeSitesDataStore) :
|
||||
SupervisionSafeSitesPreference(dataStore) {
|
||||
|
||||
override val key
|
||||
get() = KEY
|
||||
|
||||
@@ -47,8 +47,9 @@ class SupervisionWebContentFiltersScreen : PreferenceScreenCreator {
|
||||
R.string.supervision_web_content_filters_browser_title,
|
||||
) +=
|
||||
{
|
||||
+SupervisionBlockExplicitSitesPreference()
|
||||
+SupervisionAllowAllSitesPreference()
|
||||
val dataStore = SupervisionSafeSitesDataStore(context)
|
||||
+SupervisionBlockExplicitSitesPreference(dataStore)
|
||||
+SupervisionAllowAllSitesPreference(dataStore)
|
||||
}
|
||||
// TODO(b/401569571) implement the SafeSearch group.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user