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 androidx.preference.Preference 20 import com.android.settings.R 21 import com.android.settingslib.datastore.Permissions 22 import com.android.settingslib.metadata.BooleanValuePreference 23 import com.android.settingslib.metadata.PreferenceMetadata 24 import com.android.settingslib.metadata.ReadWritePermit 25 import com.android.settingslib.metadata.SensitivityLevel 26 import com.android.settingslib.preference.PreferenceBinding 27 import com.android.settingslib.preference.forEachRecursively 28 import com.android.settingslib.widget.SelectorWithWidgetPreference 29 30 /** Base class of web content filters SafeSearch preferences. */ 31 sealed class SupervisionSafeSearchPreference( 32 protected val dataStore: SupervisionSafeSearchDataStore 33 ) : BooleanValuePreference, SelectorWithWidgetPreference.OnClickListener, PreferenceBinding { storagenull34 override fun storage(context: Context) = dataStore 35 36 override fun getReadPermissions(context: Context) = Permissions.EMPTY 37 38 override fun getWritePermissions(context: Context) = Permissions.EMPTY 39 40 override fun getReadPermit(context: Context, callingPid: Int, callingUid: Int) = 41 ReadWritePermit.ALLOW 42 43 override fun getWritePermit( 44 context: Context, 45 value: Boolean?, 46 callingPid: Int, 47 callingUid: Int, 48 ) = ReadWritePermit.DISALLOW 49 50 override val sensitivityLevel 51 get() = SensitivityLevel.NO_SENSITIVITY 52 53 override fun createWidget(context: Context) = SelectorWithWidgetPreference(context) 54 55 override fun onRadioButtonClicked(emiter: SelectorWithWidgetPreference) { 56 emiter.parent?.forEachRecursively { 57 if (it is SelectorWithWidgetPreference) { 58 it.isChecked = it == emiter 59 } 60 } 61 } 62 bindnull63 override fun bind(preference: Preference, metadata: PreferenceMetadata) { 64 super.bind(preference, metadata) 65 (preference as SelectorWithWidgetPreference).also { 66 it.isChecked = (dataStore.getBoolean(it.key) == true) 67 it.setOnClickListener(this) 68 } 69 } 70 } 71 72 /** The SafeSearch filter on preference. */ 73 class SupervisionSearchFilterOnPreference(dataStore: SupervisionSafeSearchDataStore) : 74 SupervisionSafeSearchPreference(dataStore) { 75 76 override val key 77 get() = KEY 78 79 override val title 80 get() = R.string.supervision_web_content_filters_search_filter_on_title 81 82 override val summary 83 get() = R.string.supervision_web_content_filters_search_filter_on_summary 84 85 companion object { 86 const val KEY = "web_content_filters_search_filter_on" 87 } 88 } 89 90 /** The SafeSearch filter off preference. */ 91 class SupervisionSearchFilterOffPreference(dataStore: SupervisionSafeSearchDataStore) : 92 SupervisionSafeSearchPreference(dataStore) { 93 94 override val key 95 get() = KEY 96 97 override val title 98 get() = R.string.supervision_web_content_filters_search_filter_off_title 99 100 override val summary 101 get() = R.string.supervision_web_content_filters_search_filter_off_summary 102 103 companion object { 104 const val KEY = "web_content_filters_search_filter_off" 105 } 106 } 107