Files
app_Settings/src/com/android/settings/supervision/SupervisionSafeSearchDataStore.kt
Xiaomiao Zhang b7de2ba4a5 Create data store for safe search preference.
Test: atest SupervisionSafeSearchPreferenceTest
Test: atest SupervisionWebContentFiltersScreenTest
Test: manually tested with physical device
Flag: android.app.supervision.flags.enable_web_content_filters_screen
Bug: 401568995
Change-Id: Ia9e3f73ac6e9efbe7933dc2958443e9fdb5b4422
2025-03-19 20:59:14 +00:00

72 lines
2.9 KiB
Kotlin

/*
* 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.SEARCH_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 search preference. */
@Suppress("UNCHECKED_CAST")
class SupervisionSafeSearchDataStore(
private val context: Context,
private val settingsStore: SettingsStore = SettingsSecureStore.get(context),
) : AbstractKeyedDataObservable<String>(), KeyedObserver<String>, KeyValueStore {
override fun contains(key: String) =
key == SupervisionSearchFilterOnPreference.KEY ||
key == SupervisionSearchFilterOffPreference.KEY
override fun <T : Any> getValue(key: String, valueType: Class<T>): T? {
val settingValue = (settingsStore.getBoolean(SEARCH_CONTENT_FILTERS_ENABLED) == true)
return when (key) {
SupervisionSearchFilterOffPreference.KEY -> !settingValue
SupervisionSearchFilterOnPreference.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) {
SupervisionSearchFilterOffPreference.KEY ->
settingsStore.setBoolean(SEARCH_CONTENT_FILTERS_ENABLED, !value)
SupervisionSearchFilterOnPreference.KEY ->
settingsStore.setBoolean(SEARCH_CONTENT_FILTERS_ENABLED, value)
}
}
override fun onFirstObserverAdded() {
// observe the underlying storage key
settingsStore.addObserver(SEARCH_CONTENT_FILTERS_ENABLED, this, HandlerExecutor.main)
}
override fun onKeyChanged(key: String, reason: Int) {
// forward data change to preference hierarchy key
notifyChange(SupervisionSearchFilterOffPreference.KEY, reason)
notifyChange(SupervisionSearchFilterOnPreference.KEY, reason)
}
override fun onLastObserverRemoved() {
settingsStore.removeObserver(SEARCH_CONTENT_FILTERS_ENABLED, this)
}
}