1 /* 2 * Copyright (C) 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.settings.supervision 17 18 import android.content.Context 19 import android.provider.Settings.Secure.BROWSER_CONTENT_FILTERS_ENABLED 20 import com.android.settingslib.datastore.AbstractKeyedDataObservable 21 import com.android.settingslib.datastore.HandlerExecutor 22 import com.android.settingslib.datastore.KeyValueStore 23 import com.android.settingslib.datastore.KeyedObserver 24 import com.android.settingslib.datastore.SettingsSecureStore 25 import com.android.settingslib.datastore.SettingsStore 26 27 /** Datastore of the safe sites preference. */ 28 @Suppress("UNCHECKED_CAST") 29 class SupervisionSafeSitesDataStore( 30 private val context: Context, 31 private val settingsStore: SettingsStore = SettingsSecureStore.get(context), 32 ) : AbstractKeyedDataObservable<String>(), KeyedObserver<String>, KeyValueStore { 33 containsnull34 override fun contains(key: String) = 35 key == SupervisionBlockExplicitSitesPreference.KEY || 36 key == SupervisionAllowAllSitesPreference.KEY 37 38 override fun <T : Any> getValue(key: String, valueType: Class<T>): T? { 39 val settingValue = (settingsStore.getBoolean(BROWSER_CONTENT_FILTERS_ENABLED) == true) 40 return when (key) { 41 SupervisionAllowAllSitesPreference.KEY -> !settingValue 42 43 SupervisionBlockExplicitSitesPreference.KEY -> settingValue 44 45 else -> null 46 } 47 as T? 48 } 49 setValuenull50 override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) { 51 if (value !is Boolean) return 52 when (key) { 53 SupervisionAllowAllSitesPreference.KEY -> 54 settingsStore.setBoolean(BROWSER_CONTENT_FILTERS_ENABLED, !value) 55 56 SupervisionBlockExplicitSitesPreference.KEY -> 57 settingsStore.setBoolean(BROWSER_CONTENT_FILTERS_ENABLED, value) 58 } 59 } 60 onFirstObserverAddednull61 override fun onFirstObserverAdded() { 62 // observe the underlying storage key 63 settingsStore.addObserver(BROWSER_CONTENT_FILTERS_ENABLED, this, HandlerExecutor.main) 64 } 65 onKeyChangednull66 override fun onKeyChanged(key: String, reason: Int) { 67 // forward data change to preference hierarchy key 68 notifyChange(SupervisionBlockExplicitSitesPreference.KEY, reason) 69 notifyChange(SupervisionAllowAllSitesPreference.KEY, reason) 70 } 71 onLastObserverRemovednull72 override fun onLastObserverRemoved() { 73 settingsStore.removeObserver(BROWSER_CONTENT_FILTERS_ENABLED, this) 74 } 75 } 76