Files
packages_apps_Settings/src/com/android/settings/supervision/SupervisionSafeSitesPreference.kt
Xiaomiao Zhang 4d0be536c7 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
2025-03-17 17:55:57 +00:00

104 lines
3.6 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 androidx.preference.Preference
import com.android.settings.R
import com.android.settingslib.datastore.Permissions
import com.android.settingslib.metadata.BooleanValuePreference
import com.android.settingslib.metadata.PreferenceMetadata
import com.android.settingslib.metadata.ReadWritePermit
import com.android.settingslib.metadata.SensitivityLevel
import com.android.settingslib.preference.PreferenceBinding
import com.android.settingslib.preference.forEachRecursively
import com.android.settingslib.widget.SelectorWithWidgetPreference
/** Base class of web content filters Safe sites preferences. */
sealed class SupervisionSafeSitesPreference(
protected val dataStore: SupervisionSafeSitesDataStore
) : BooleanValuePreference, SelectorWithWidgetPreference.OnClickListener, PreferenceBinding {
override fun storage(context: Context) = dataStore
override fun getReadPermissions(context: Context) = Permissions.EMPTY
override fun getWritePermissions(context: Context) = Permissions.EMPTY
override fun getReadPermit(context: Context, callingPid: Int, callingUid: Int) =
ReadWritePermit.ALLOW
override fun getWritePermit(
context: Context,
value: Boolean?,
callingPid: Int,
callingUid: Int,
) = ReadWritePermit.DISALLOW
override val sensitivityLevel
get() = SensitivityLevel.NO_SENSITIVITY
override fun createWidget(context: Context) = SelectorWithWidgetPreference(context)
override fun onRadioButtonClicked(emiter: SelectorWithWidgetPreference) {
emiter.parent?.forEachRecursively {
if (it is SelectorWithWidgetPreference) {
it.isChecked = it == emiter
}
}
}
override fun bind(preference: Preference, metadata: PreferenceMetadata) {
super.bind(preference, metadata)
(preference as SelectorWithWidgetPreference).also {
it.isChecked = (dataStore.getBoolean(it.key) == true)
it.setOnClickListener(this)
}
}
}
/** The "Try to block explicit sites" preference. */
class SupervisionBlockExplicitSitesPreference(dataStore: SupervisionSafeSitesDataStore) :
SupervisionSafeSitesPreference(dataStore) {
override val key
get() = KEY
override val title
get() = R.string.supervision_web_content_filters_browser_block_explicit_sites_title
override val summary
get() = R.string.supervision_web_content_filters_browser_block_explicit_sites_summary
companion object {
const val KEY = "web_content_filters_browser_block_explicit_sites"
}
}
/** The "Allow all sites" preference. */
class SupervisionAllowAllSitesPreference(dataStore: SupervisionSafeSitesDataStore) :
SupervisionSafeSitesPreference(dataStore) {
override val key
get() = KEY
override val title
get() = R.string.supervision_web_content_filters_browser_allow_all_sites_title
companion object {
const val KEY = "web_content_filters_browser_allow_all_sites"
}
}