Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bb8b603f authored by Xiaomiao Zhang's avatar Xiaomiao Zhang
Browse files

Add SafeSites related content filters preference.

Test: atest SupervisionSafeSitesPreferenceTest
Test: maunally tested locally
Bug: 401568993
Flag: android.app.supervision.flags.enable_web_content_filters_screen
Change-Id: I556019bdeba5ed459996102217836cda0e3c7f71
parent c847dc7e
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -14309,6 +14309,14 @@ Data usage charges may apply.</string>
    <string name="supervision_add_forgot_pin_preference_title">Forgot PIN</string>
    <!-- Title for web content filters entry [CHAR LIMIT=60] -->
    <string name="supervision_web_content_filters_title">Web content filters</string>
    <!-- Title for web content filters browser category [CHAR LIMIT=60] -->
    <string name="supervision_web_content_filters_browser_title">Google Chrome and Web</string>
    <!-- Title for web content filters browser category block explicit sites option [CHAR LIMIT=60] -->
    <string name="supervision_web_content_filters_browser_block_explicit_sites_title">Try to block explicit sites</string>
    <!-- Summary for web content filters browser category block explicit sites option [CHAR LIMIT=None] -->
    <string name="supervision_web_content_filters_browser_block_explicit_sites_summary">No filter is perfect, but this should help hide sexually explicit sites</string>
    <!-- Title for web content filters browser category allow all sites option [CHAR LIMIT=60] -->
    <string name="supervision_web_content_filters_browser_allow_all_sites_title">Allow all sites</string>
    <!-- Generic content description that is attached to the preview illustration at the top of an Accessibility feature toggle page. [CHAR LIMIT=NONE] -->
    <string name="accessibility_illustration_content_description"><xliff:g id="feature" example="Select to Speak">%1$s</xliff:g> animation</string>
</resources>
+103 −0
Original line number Diff line number Diff line
/*
 * 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.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
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 :
    BooleanValuePreference, SelectorWithWidgetPreference.OnClickListener, PreferenceBinding {
    override fun storage(context: Context): KeyValueStore = SettingsSecureStore.get(context)

    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 {
            // TODO(b/401568468): Set the isChecked value using stored values.
            it.isChecked = (it.key == SupervisionAllowAllSitesPreference.KEY)
            it.setOnClickListener(this)
        }
    }
}

/** The "Try to block explicit sites" preference. */
class SupervisionBlockExplicitSitesPreference : SupervisionSafeSitesPreference() {

    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 : SupervisionSafeSitesPreference() {

    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"
    }
}
+11 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings.supervision
import android.app.supervision.flags.Flags
import android.content.Context
import com.android.settings.R
import com.android.settingslib.metadata.PreferenceCategory
import com.android.settingslib.metadata.ProvidePreferenceScreen
import com.android.settingslib.metadata.preferenceHierarchy
import com.android.settingslib.preference.PreferenceScreenCreator
@@ -41,10 +42,19 @@ class SupervisionWebContentFiltersScreen : PreferenceScreenCreator {

    override fun getPreferenceHierarchy(context: Context) =
        preferenceHierarchy(context, this) {
            // TODO(b/395134536) implement the screen.
            +PreferenceCategory(
                BROWSER_RADIO_BUTTON_GROUP,
                R.string.supervision_web_content_filters_browser_title,
            ) +=
                {
                    +SupervisionBlockExplicitSitesPreference()
                    +SupervisionAllowAllSitesPreference()
                }
            // TODO(b/401569571) implement the SafeSearch group.
        }

    companion object {
        const val KEY = "supervision_web_content_filters"
        internal const val BROWSER_RADIO_BUTTON_GROUP = "browser_radio_button_group"
    }
}
+53 −0
Original line number Diff line number Diff line
/*
 * 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.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.R
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class SupervisionSafeSitesPreferenceTest {
    private val context: Context = ApplicationProvider.getApplicationContext()

    private val allowAllSitesPreference = SupervisionAllowAllSitesPreference()

    private val blockExplicitSitesPreference = SupervisionBlockExplicitSitesPreference()

    @Test
    fun getTitle_allowAllSites() {
        assertThat(allowAllSitesPreference.title)
            .isEqualTo(R.string.supervision_web_content_filters_browser_allow_all_sites_title)
    }

    @Test
    fun getTitle_blockExplicitSites() {
        assertThat(blockExplicitSitesPreference.title)
            .isEqualTo(R.string.supervision_web_content_filters_browser_block_explicit_sites_title)
    }

    @Test
    fun getSummary_blockExplicitSites() {
        assertThat(blockExplicitSitesPreference.summary)
            .isEqualTo(
                R.string.supervision_web_content_filters_browser_block_explicit_sites_summary
            )
    }
}