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

Commit 843cc150 authored by Xiaomiao Zhang's avatar Xiaomiao Zhang
Browse files

Update SupervisionService to not disable content filters that are already...

Update SupervisionService to not disable content filters that are already enabled when users enable device supervison for the first time.

Fix: 416095714
Test: atest SupervisionServiceTest
Flag: android.app.supervision.flags.supervision_manager_apis
Change-Id: I937429584350bbcea69eedd9a7bbb77a14e4bb52
parent 4e60e3ed
Loading
Loading
Loading
Loading
+25 −13
Original line number Diff line number Diff line
@@ -331,7 +331,7 @@ public class SupervisionService extends ISupervisionManager.Stub {
        }
        final long token = Binder.clearCallingIdentity();
        try {
            updateWebContentFilters(userId);
            updateWebContentFilters(userId, enabled);

            if (Flags.enableSupervisionAppService()) {
                List<AppServiceConnection> connections =
@@ -369,17 +369,26 @@ public class SupervisionService extends ISupervisionManager.Stub {
        }
    }

    /** Updates Web Content Filters when supervision status is updated. */
    private void updateWebContentFilters(@UserIdInt int userId) {
    /**
     * Updates Web Content Filters when supervision status is updated.
     *
     * <p>Only change the content filter value if it is not in sync with the supervision state.
     * Disable the filter when disabling supervision and re-set to original value when re-enabling
     * supervision. (If the filter is already enabled when enabling supervision, do not disable it).
     */
    private void updateWebContentFilters(@UserIdInt int userId, boolean enabled) {
        try {
            int browserValue =
                    Settings.Secure.getIntForUser(
                            mContext.getContentResolver(), BROWSER_CONTENT_FILTERS_ENABLED, userId);

            if (!enabled || browserValue != 1) {
                Settings.Secure.putIntForUser(
                        mContext.getContentResolver(),
                        BROWSER_CONTENT_FILTERS_ENABLED,
                        browserValue * -1,
                        userId);
            }
        } catch (Settings.SettingNotFoundException ignored) {
            // Ignore the exception and do not change the value as no value has been set.
        }
@@ -387,11 +396,14 @@ public class SupervisionService extends ISupervisionManager.Stub {
            int searchValue =
                    Settings.Secure.getIntForUser(
                            mContext.getContentResolver(), SEARCH_CONTENT_FILTERS_ENABLED, userId);

            if (!enabled || searchValue != 1) {
                Settings.Secure.putIntForUser(
                        mContext.getContentResolver(),
                        SEARCH_CONTENT_FILTERS_ENABLED,
                        searchValue * -1,
                        userId);
            }
        } catch (Settings.SettingNotFoundException ignored) {
            // Ignore the exception and do not change the value as no value has been set.
        }
+13 −19
Original line number Diff line number Diff line
@@ -71,19 +71,13 @@ import org.mockito.kotlin.whenever
 */
@RunWith(AndroidJUnit4::class)
class SupervisionServiceTest {
    @get:Rule
    val checkFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule()
    @get:Rule
    val mocks: MockitoRule = MockitoJUnit.rule()

    @Mock
    private lateinit var mockDpmInternal: DevicePolicyManagerInternal
    @Mock
    private lateinit var mockKeyguardManager: KeyguardManager
    @Mock
    private lateinit var mockPackageManager: PackageManager
    @Mock
    private lateinit var mockUserManagerInternal: UserManagerInternal
    @get:Rule val checkFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule()
    @get:Rule val mocks: MockitoRule = MockitoJUnit.rule()

    @Mock private lateinit var mockDpmInternal: DevicePolicyManagerInternal
    @Mock private lateinit var mockKeyguardManager: KeyguardManager
    @Mock private lateinit var mockPackageManager: PackageManager
    @Mock private lateinit var mockUserManagerInternal: UserManagerInternal

    private lateinit var context: Context
    private lateinit var lifecycle: SupervisionService.Lifecycle
@@ -248,14 +242,14 @@ class SupervisionServiceTest {
        service.setSupervisionEnabledForUser(USER_ID, true)

        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isTrue()
        assertThat(getSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED)).isEqualTo(-1)
        assertThat(getSecureSetting(SEARCH_CONTENT_FILTERS_ENABLED)).isEqualTo(-1)
        assertThat(getSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED)).isEqualTo(1)
        assertThat(getSecureSetting(SEARCH_CONTENT_FILTERS_ENABLED)).isEqualTo(1)

        service.setSupervisionEnabledForUser(USER_ID, false)

        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isFalse()
        assertThat(getSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED)).isEqualTo(1)
        assertThat(getSecureSetting(SEARCH_CONTENT_FILTERS_ENABLED)).isEqualTo(1)
        assertThat(getSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED)).isEqualTo(-1)
        assertThat(getSecureSetting(SEARCH_CONTENT_FILTERS_ENABLED)).isEqualTo(-1)
    }

    @Test
@@ -267,13 +261,13 @@ class SupervisionServiceTest {
        service.mInternal.setSupervisionEnabledForUser(USER_ID, true)

        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isTrue()
        assertThat(getSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED)).isEqualTo(-1)
        assertThat(getSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED)).isEqualTo(1)
        assertThat(getSecureSetting(SEARCH_CONTENT_FILTERS_ENABLED)).isEqualTo(0)

        service.mInternal.setSupervisionEnabledForUser(USER_ID, false)

        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isFalse()
        assertThat(getSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED)).isEqualTo(1)
        assertThat(getSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED)).isEqualTo(-1)
        assertThat(getSecureSetting(SEARCH_CONTENT_FILTERS_ENABLED)).isEqualTo(0)
    }