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

Commit 4ba9929e authored by Xiaomiao Zhang's avatar Xiaomiao Zhang Committed by Android (Google) Code Review
Browse files

Merge "Update SupervisionService to not disable content filters that are...

Merge "Update SupervisionService to not disable content filters that are already enabled when users enable device supervison for the first time." into main
parents cd7550be 843cc150
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 =
@@ -374,17 +374,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.
        }
@@ -392,11 +401,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
@@ -74,19 +74,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
@@ -256,14 +250,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
@@ -289,13 +283,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)
    }