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

Commit 71702c17 authored by Xiaomiao Zhang's avatar Xiaomiao Zhang
Browse files

Flip the stored value for Settings Secure key SEARCH_CONTENT_FILTERS_ENABLED...

Flip the stored value for Settings Secure key SEARCH_CONTENT_FILTERS_ENABLED and BROWSER_CONTENT_FILTERS_ENABLED when supervision is enabled and disabled. Update the doc for these keys.

Bug: 412658995
Test: local deployment
Test: atest SupervisionServiceTest
Flag: android.app.supervision.flags.supervision_manager_apis
Change-Id: Ie7ea5504d1eff8eabb2bf7529adac3c3deb9b079
parent 89368c69
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -8735,8 +8735,8 @@ public final class Settings {
         * Setting to indicate that content filters should be enabled on web browsers.
         *
         * <ul>
         *   <li>0 = Allow all sites
         *   <li>1 = Try to block explicit sites
         *   <li> non-positive (less or equal to 0) = Allow all sites
         *   <li> positive (greater than 0) = Try to block explicit sites
         * </ul>
         *
         * @hide
@@ -8749,8 +8749,8 @@ public final class Settings {
         * Setting to indicate that content filters should be enabled in web search engines.
         *
         * <ul>
         *   <li>0 = Off
         *   <li>1 = Filter
         *   <li> non-positive (less or equal to 0) = Off
         *   <li> positive (greater than 0) = Filter
         * </ul>
         *
         * @hide
+2 −2
Original line number Diff line number Diff line
@@ -478,8 +478,8 @@ public class SecureSettingsValidators {
        VALIDATORS.put(Secure.FINGERPRINT_APP_ENABLED,  BOOLEAN_VALIDATOR);
        VALIDATORS.put(Secure.FINGERPRINT_KEYGUARD_ENABLED, BOOLEAN_VALIDATOR);
        VALIDATORS.put(Secure.DUAL_SHADE, BOOLEAN_VALIDATOR);
        VALIDATORS.put(Secure.BROWSER_CONTENT_FILTERS_ENABLED, BOOLEAN_VALIDATOR);
        VALIDATORS.put(Secure.SEARCH_CONTENT_FILTERS_ENABLED, BOOLEAN_VALIDATOR);
        VALIDATORS.put(Secure.BROWSER_CONTENT_FILTERS_ENABLED, ANY_INTEGER_VALIDATOR);
        VALIDATORS.put(Secure.SEARCH_CONTENT_FILTERS_ENABLED, ANY_INTEGER_VALIDATOR);
        VALIDATORS.put(Secure.SPELL_CHECKER_ENABLED, BOOLEAN_VALIDATOR);
        VALIDATORS.put(Secure.SELECTED_SPELL_CHECKER, NULLABLE_COMPONENT_NAME_VALIDATOR);
        VALIDATORS.put(Secure.SELECTED_SPELL_CHECKER_SUBTYPE, ANY_INTEGER_VALIDATOR);
+41 −7
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import static android.Manifest.permission.MANAGE_ROLE_HOLDERS;
import static android.Manifest.permission.MANAGE_USERS;
import static android.Manifest.permission.QUERY_USERS;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.provider.Settings.Secure.BROWSER_CONTENT_FILTERS_ENABLED;
import static android.provider.Settings.Secure.SEARCH_CONTENT_FILTERS_ENABLED;

import static com.android.internal.util.Preconditions.checkCallAuthorization;

@@ -33,7 +35,6 @@ import android.app.admin.DevicePolicyManager;
import android.app.admin.DevicePolicyManagerInternal;
import android.app.supervision.ISupervisionAppService;
import android.app.supervision.ISupervisionManager;
import android.app.supervision.SupervisionAppService;
import android.app.supervision.SupervisionManagerInternal;
import android.app.supervision.SupervisionRecoveryInfo;
import android.app.supervision.flags.Flags;
@@ -50,6 +51,8 @@ import android.os.RemoteException;
import android.os.ResultReceiver;
import android.os.ShellCallback;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Slog;
import android.util.SparseArray;

@@ -125,6 +128,7 @@ public class SupervisionService extends ISupervisionManager.Stub {
            enforcePermission(INTERACT_ACROSS_USERS);
        }
        setSupervisionEnabledForUserInternal(userId, enabled, getSystemSupervisionPackage());
        updateWebContentFilters(userId);

        if (Flags.enableSupervisionAppService()) {
            List<AppServiceConnection> connections = getSupervisionAppServiceConnections(userId);
@@ -132,7 +136,9 @@ public class SupervisionService extends ISupervisionManager.Stub {
                String targetPackage = conn.getFinder().getTargetPackage(userId);
                ISupervisionAppService binder = (ISupervisionAppService) conn.getServiceBinder();
                if (binder == null) {
                    Slog.d(LOG_TAG, String.format(
                    Slog.d(
                            LOG_TAG,
                            TextUtils.formatSimple(
                                    "Unable to toggle supervision for package %s. Binder is null.",
                                    targetPackage));
                    continue;
@@ -144,7 +150,9 @@ public class SupervisionService extends ISupervisionManager.Stub {
                        binder.onDisabled();
                    }
                } catch (RemoteException e) {
                    Slog.d(LOG_TAG, String.format(
                    Slog.d(
                            LOG_TAG,
                            TextUtils.formatSimple(
                                    "Unable to toggle supervision for package %s. e = %s",
                                    targetPackage, e));
                }
@@ -322,6 +330,32 @@ public class SupervisionService extends ISupervisionManager.Stub {
        }
    }

    /** Updates Web Content Filters when supervision status is updated. */
    private void updateWebContentFilters(@UserIdInt int userId) {
        try {
            int browserValue =
                    Settings.Secure.getIntForUser(
                            mContext.getContentResolver(), BROWSER_CONTENT_FILTERS_ENABLED, userId);
            Settings.Secure.putInt(
                    mContext.getContentResolver(),
                    BROWSER_CONTENT_FILTERS_ENABLED,
                    browserValue * -1);
        } catch (Settings.SettingNotFoundException ignored) {
            // Ignore the exception and do not change the value as no value has been set.
        }
        try {
            int searchValue =
                    Settings.Secure.getIntForUser(
                            mContext.getContentResolver(), SEARCH_CONTENT_FILTERS_ENABLED, userId);
            Settings.Secure.putInt(
                    mContext.getContentResolver(),
                    SEARCH_CONTENT_FILTERS_ENABLED,
                    searchValue * -1);
        } catch (Settings.SettingNotFoundException ignored) {
            // Ignore the exception and do not change the value as no value has been set.
        }
    }

    /**
     * Ensures that supervision is enabled when the supervision app is the profile owner.
     *
+71 −0
Original line number Diff line number Diff line
@@ -41,6 +41,9 @@ import android.os.UserHandle.MIN_SECONDARY_USER_ID
import android.os.UserHandle.USER_SYSTEM
import android.platform.test.annotations.RequiresFlagsEnabled
import android.platform.test.flag.junit.DeviceFlagsValueProvider
import android.provider.Settings
import android.provider.Settings.Secure.BROWSER_CONTENT_FILTERS_ENABLED
import android.provider.Settings.Secure.SEARCH_CONTENT_FILTERS_ENABLED
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.android.internal.R
@@ -223,23 +226,91 @@ class SupervisionServiceTest {
    @Test
    fun setSupervisionEnabledForUser() {
        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isFalse()
        Settings.Secure.putInt(context.getContentResolver(), BROWSER_CONTENT_FILTERS_ENABLED, 1)
        Settings.Secure.putInt(context.getContentResolver(), SEARCH_CONTENT_FILTERS_ENABLED, 1)

        service.setSupervisionEnabledForUser(USER_ID, true)
        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isTrue()
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    BROWSER_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(1)
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    SEARCH_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(1)

        service.setSupervisionEnabledForUser(USER_ID, false)
        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isFalse()
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    BROWSER_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(-1)
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    SEARCH_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(-1)
    }

    @Test
    fun setSupervisionEnabledForUser_internal() {
        Settings.Secure.putInt(context.getContentResolver(), BROWSER_CONTENT_FILTERS_ENABLED, 1)
        Settings.Secure.putInt(context.getContentResolver(), SEARCH_CONTENT_FILTERS_ENABLED, 0)
        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isFalse()

        service.mInternal.setSupervisionEnabledForUser(USER_ID, true)
        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isTrue()
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    BROWSER_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(1)
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    SEARCH_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(0)

        service.mInternal.setSupervisionEnabledForUser(USER_ID, false)
        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isFalse()
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    BROWSER_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(-1)
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    SEARCH_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(0)
    }

    @Test