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

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

Dynamically set summary of web content filters entry.

Test: atest SupervisionWebContentFiltersScreenTest
Test: local deployment
Flag: android.app.supervision.flags.enable_web_content_filters_screen
Bug: 406297683
Change-Id: I108252a4a5da8394873dac47f8e8e4f267eca541
parent 6f0d1c3b
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -14470,6 +14470,14 @@ Data usage charges may apply.</string>
    <string name="supervision_add_pin_recovery_title">Tap to add recovery</string>
    <!-- Title for web content filters entry [CHAR LIMIT=60] -->
    <string name="supervision_web_content_filters_title">Web content filters</string>
    <!-- Summary for web content filters entry both on case [CHAR LIMIT=60] -->
    <string name="supervision_web_content_filters_summary_both_on">ON / Chrome &amp; Search filters set</string>
    <!-- Summary for web content filters entry chrome on [CHAR LIMIT=60] -->
    <string name="supervision_web_content_filters_summary_chrome_on">ON / Chrome filter set</string>
    <!-- Summary for web content filters entry chrome on [CHAR LIMIT=60] -->
    <string name="supervision_web_content_filters_summary_search_on">ON / Search filter set</string>
    <!-- Summary for web content filters entry chrome on [CHAR LIMIT=60] -->
    <string name="supervision_web_content_filters_summary_both_off">No filters set</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] -->
+20 −2
Original line number Diff line number Diff line
@@ -17,15 +17,19 @@ package com.android.settings.supervision

import android.app.supervision.flags.Flags
import android.content.Context
import android.provider.Settings.Secure.BROWSER_CONTENT_FILTERS_ENABLED
import android.provider.Settings.Secure.SEARCH_CONTENT_FILTERS_ENABLED
import com.android.settings.R
import com.android.settingslib.datastore.SettingsSecureStore
import com.android.settingslib.metadata.PreferenceCategory
import com.android.settingslib.metadata.PreferenceSummaryProvider
import com.android.settingslib.metadata.ProvidePreferenceScreen
import com.android.settingslib.metadata.preferenceHierarchy
import com.android.settingslib.preference.PreferenceScreenCreator

/** Web content filters landing page (Settings > Supervision > Web content filters). */
@ProvidePreferenceScreen(SupervisionWebContentFiltersScreen.KEY)
class SupervisionWebContentFiltersScreen : PreferenceScreenCreator {
class SupervisionWebContentFiltersScreen : PreferenceScreenCreator, PreferenceSummaryProvider {
    override fun isFlagEnabled(context: Context) = Flags.enableWebContentFiltersScreen()

    override val key: String
@@ -34,10 +38,24 @@ class SupervisionWebContentFiltersScreen : PreferenceScreenCreator {
    override val title: Int
        get() = R.string.supervision_web_content_filters_title

    // TODO(b/395134536) update the summary once the string is finalized.
    override val icon: Int
        get() = R.drawable.ic_globe

    override fun getSummary(context: Context): CharSequence? {
        val dataStore = SettingsSecureStore.get(context)
        return if (dataStore.getBoolean(BROWSER_CONTENT_FILTERS_ENABLED) == true) {
            if (dataStore.getBoolean(SEARCH_CONTENT_FILTERS_ENABLED) == true) {
                context.getString(R.string.supervision_web_content_filters_summary_both_on)
            } else {
                context.getString(R.string.supervision_web_content_filters_summary_chrome_on)
            }
        } else if (dataStore.getBoolean(SEARCH_CONTENT_FILTERS_ENABLED) == true) {
            context.getString(R.string.supervision_web_content_filters_summary_search_on)
        } else {
            context.getString(R.string.supervision_web_content_filters_summary_both_off)
        }
    }

    override fun fragmentClass() = SupervisionWebContentFiltersFragment::class.java

    override fun getPreferenceHierarchy(context: Context) =
+33 −0
Original line number Diff line number Diff line
@@ -23,6 +23,9 @@ import android.content.IntentFilter
import android.platform.test.annotations.DisableFlags
import android.platform.test.annotations.EnableFlags
import android.platform.test.flag.junit.SetFlagsRule
import android.provider.Settings
import android.provider.Settings.Secure.BROWSER_CONTENT_FILTERS_ENABLED
import android.provider.Settings.Secure.SEARCH_CONTENT_FILTERS_ENABLED
import androidx.fragment.app.testing.FragmentScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
@@ -71,6 +74,36 @@ class SupervisionWebContentFiltersScreenTest {
            .isEqualTo(R.string.supervision_web_content_filters_title)
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_WEB_CONTENT_FILTERS_SCREEN)
    fun getSummary() {
        // Filters are default off.
        assertThat(supervisionWebContentFiltersScreen.getSummary(context))
            .isEqualTo(context.getString(R.string.supervision_web_content_filters_summary_both_off))

        // Enable browser filters.
        Settings.Secure.putInt(context.getContentResolver(), BROWSER_CONTENT_FILTERS_ENABLED, 1)

        assertThat(supervisionWebContentFiltersScreen.getSummary(context))
            .isEqualTo(
                context.getString(R.string.supervision_web_content_filters_summary_chrome_on)
            )

        // Enable search filters.
        Settings.Secure.putInt(context.getContentResolver(), SEARCH_CONTENT_FILTERS_ENABLED, 1)

        assertThat(supervisionWebContentFiltersScreen.getSummary(context))
            .isEqualTo(context.getString(R.string.supervision_web_content_filters_summary_both_on))

        // Disable browser filters.
        Settings.Secure.putInt(context.getContentResolver(), BROWSER_CONTENT_FILTERS_ENABLED, 0)

        assertThat(supervisionWebContentFiltersScreen.getSummary(context))
            .isEqualTo(
                context.getString(R.string.supervision_web_content_filters_summary_search_on)
            )
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_WEB_CONTENT_FILTERS_SCREEN)
    fun flagEnabled() {