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

Commit 9f6eaf86 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Fix flicker in Dark theme

Currently, when schedule sets to "Turns on at bedtime", the footer will
show a slid up animation when entering the page, this is because the
"Start time" & "End time" preferences are hidden in onResume().

This is because these 2 preferences always return AVAILABLE in
getAvailabilityStatus(), and manually update visibility in
refreshSummary(), which is called each time updateState() is called.

Usually the controller not set the visibility explicitly, but return
CONDITIONALLY_UNAVAILABLE in getAvailabilityStatus() when they want to
hide the preference.

Because getAvailabilityStatus() is called in onCreate(), by using this,
we can fix the flicker.

Fix: 234399017
Test: visual & robo test
Change-Id: I4cb7dd95d2985bd1ca4c8cb30aaebdc21a5415f8
parent 610538e1
Loading
Loading
Loading
Loading
+4 −10
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ import androidx.preference.Preference;
import com.android.settings.core.BasePreferenceController;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * Controller for custom mode night mode time settings
@@ -35,7 +34,6 @@ import java.time.format.DateTimeFormatter;
public class DarkModeCustomPreferenceController extends BasePreferenceController {
    private static final String START_TIME_KEY = "dark_theme_start_time";
    private static final String END_TIME_KEY = "dark_theme_end_time";
    public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");
    private final UiModeManager mUiModeManager;
    private TimeFormatter mFormat;
    private DarkModeSettingsFragment mFragmet;
@@ -63,7 +61,10 @@ public class DarkModeCustomPreferenceController extends BasePreferenceController

    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
        return mUiModeManager.getNightMode() == MODE_NIGHT_CUSTOM
                && mUiModeManager.getNightModeCustomType()
                == UiModeManager.MODE_NIGHT_CUSTOM_TYPE_SCHEDULE
                ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
    }

    public TimePickerDialog getDialog() {
@@ -88,13 +89,6 @@ public class DarkModeCustomPreferenceController extends BasePreferenceController

    @Override
    protected void refreshSummary(Preference preference) {
        if (mUiModeManager.getNightMode() != MODE_NIGHT_CUSTOM
                || mUiModeManager.getNightModeCustomType()
                        != UiModeManager.MODE_NIGHT_CUSTOM_TYPE_SCHEDULE) {
            preference.setVisible(false);
            return;
        }
        preference.setVisible(true);
        final LocalTime time;
        if (TextUtils.equals(getPreferenceKey(), START_TIME_KEY)) {
            time = mUiModeManager.getCustomNightModeStart();
+7 −4
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.os.Bundle;
import android.os.PowerManager;

import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
@@ -43,9 +44,6 @@ public class DarkModeSettingsFragment extends DashboardFragment {
    private DarkModeObserver mContentObserver;
    private DarkModeCustomPreferenceController mCustomStartController;
    private DarkModeCustomPreferenceController mCustomEndController;
    private Runnable mCallback = () -> {
        updatePreferenceStates();
    };
    private static final int DIALOG_START_TIME = 0;
    private static final int DIALOG_END_TIME = 1;

@@ -60,7 +58,12 @@ public class DarkModeSettingsFragment extends DashboardFragment {
    public void onStart() {
        super.onStart();
        // Listen for changes only while visible.
        mContentObserver.subscribe(mCallback);
        mContentObserver.subscribe(() -> {
            PreferenceScreen preferenceScreen = getPreferenceScreen();
            mCustomStartController.displayPreference(preferenceScreen);
            mCustomEndController.displayPreference(preferenceScreen);
            updatePreferenceStates();
        });
    }

    @Override
+17 −25
Original line number Diff line number Diff line
@@ -15,6 +15,11 @@

package com.android.settings.display.darkmode;

import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
@@ -71,68 +76,55 @@ public class DarkModeCustomPreferenceControllerTest {
    }

    @Test
    public void nightMode_manualOn_hidePreference() {
    public void getAvailabilityStatus_nightModeManualOn_unavailable() {
        when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_YES);
        mConfig.uiMode = Configuration.UI_MODE_NIGHT_YES;

        mController.refreshSummary(mPreference);

        verify(mPreference).setVisible(eq(false));
        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    public void nightMode_manualOff_hidePreference() {
    public void getAvailabilityStatus_nightModeManualOff_unavailable() {
        when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_NO);
        mConfig.uiMode = Configuration.UI_MODE_NIGHT_NO;

        mController.refreshSummary(mPreference);

        verify(mPreference).setVisible(eq(false));
        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
    }


    @Test
    public void nightMode_customOn_showPreference() {
    public void getAvailabilityStatus_nightModeCustomOn_available() {
        when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_CUSTOM);
        mConfig.uiMode = Configuration.UI_MODE_NIGHT_YES;

        mController.refreshSummary(mPreference);

        verify(mPreference).setVisible(eq(true));
        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    }

    @Test
    public void nightMode_customOff_showPreference() {
    public void getAvailabilityStatus_nightModeCustomOff_available() {
        when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_CUSTOM);
        mConfig.uiMode = Configuration.UI_MODE_NIGHT_NO;

        mController.refreshSummary(mPreference);

        verify(mPreference).setVisible(eq(true));
        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    }

    @Test
    public void nightMode_customBedtimeOn_hidePreference() {
    public void getAvailabilityStatus_nightModeCustomBedtimeOn_unavailable() {
        when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_CUSTOM);
        when(mService.getNightModeCustomType())
                .thenReturn(UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME);
        mConfig.uiMode = Configuration.UI_MODE_NIGHT_YES;

        mController.refreshSummary(mPreference);

        verify(mPreference).setVisible(eq(false));
        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    public void nightMode_customBedtimeOff_hidePreference() {
    public void getAvailabilityStatus_nightModeCustomBedtimeOff_unavailable() {
        when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_CUSTOM);
        when(mService.getNightModeCustomType())
                .thenReturn(UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME);
        mConfig.uiMode = Configuration.UI_MODE_NIGHT_NO;

        mController.refreshSummary(mPreference);

        verify(mPreference).setVisible(eq(false));
        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
    }

    @Test