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

Commit 7bddda48 authored by Jay Aliomer's avatar Jay Aliomer
Browse files

Rememeber the last dark mode before shutdown when in auto mode

Twilight mode is not available when the phone starts. So the last state
is rememebered until the phone screen turns off

Fixes: 160842085
Test: manual testing
Change-Id: I78a490c0f083e92022e1995b3e19dc7dd567a7c3
parent afded3d0
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -7953,6 +7953,12 @@ public final class Settings {
         */
        public static final String UI_NIGHT_MODE_OVERRIDE_ON = "ui_night_mode_override_on";
        /**
         * The last computed night mode bool the last time the phone was on
         * @hide
         */
        public static final String UI_NIGHT_MODE_LAST_COMPUTED = "ui_night_mode_last_computed";
        /**
         * The current night mode that has been overridden to turn off by the system.  Owned
         * and controlled by UiModeManagerService.  Constants are as per
+29 −18
Original line number Diff line number Diff line
@@ -72,7 +72,6 @@ import com.android.internal.app.DisableCarModeActivity;
import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
import com.android.internal.notification.SystemNotificationChannels;
import com.android.internal.util.DumpUtils;
import com.android.server.SystemService.TargetUser;
import com.android.server.twilight.TwilightListener;
import com.android.server.twilight.TwilightManager;
import com.android.server.twilight.TwilightState;
@@ -327,8 +326,16 @@ final class UiModeManagerService extends SystemService {
    @Override
    public void onUserSwitching(@Nullable TargetUser from, @NonNull TargetUser to) {
        mCurrentUser = to.getUserIdentifier();
        if (mNightMode == MODE_NIGHT_AUTO) persistComputedNightMode(from.getUserIdentifier());
        getContext().getContentResolver().unregisterContentObserver(mSetupWizardObserver);
        verifySetupWizardCompleted();
        synchronized (mLock) {
            // only update if the value is actually changed
            if (updateNightModeFromSettingsLocked(getContext(), getContext().getResources(),
                    to.getUserIdentifier())) {
                updateLocked(0, 0);
            }
        }
    }

    @Override
@@ -356,11 +363,10 @@ final class UiModeManagerService extends SystemService {
                        new IntentFilter(Intent.ACTION_DOCK_EVENT));
                IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
                context.registerReceiver(mBatteryReceiver, batteryFilter);
                IntentFilter filter = new IntentFilter();
                filter.addAction(Intent.ACTION_USER_SWITCHED);
                context.registerReceiver(mSettingsRestored,
                        new IntentFilter(Intent.ACTION_SETTING_RESTORED));
                context.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler);
                context.registerReceiver(mOnShutdown,
                        new IntentFilter(Intent.ACTION_SHUTDOWN));
                updateConfigurationLocked();
                applyConfigurationExternallyLocked();
            }
@@ -407,6 +413,21 @@ final class UiModeManagerService extends SystemService {
        publishLocalService(UiModeManagerInternal.class, mLocalService);
    }

    private final BroadcastReceiver mOnShutdown = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (mNightMode == MODE_NIGHT_AUTO) {
                persistComputedNightMode(mCurrentUser);
            }
        }
    };

    private void persistComputedNightMode(int userId) {
        Secure.putIntForUser(getContext().getContentResolver(),
                Secure.UI_NIGHT_MODE_LAST_COMPUTED, mComputedNightMode ? 1 : 0,
                userId);
    }

    private final BroadcastReceiver mSettingsRestored = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
@@ -508,6 +529,10 @@ final class UiModeManagerService extends SystemService {
                    Secure.getLongForUser(context.getContentResolver(),
                            Secure.DARK_THEME_CUSTOM_END_TIME,
                            DEFAULT_CUSTOM_NIGHT_END_TIME.toNanoOfDay() / 1000L, userId) * 1000);
            if (mNightMode == MODE_NIGHT_AUTO) {
                mComputedNightMode = Secure.getIntForUser(context.getContentResolver(),
                        Secure.UI_NIGHT_MODE_LAST_COMPUTED, 0, userId) != 0;
            }
        }

        return oldNightMode != mNightMode;
@@ -1630,18 +1655,4 @@ final class UiModeManagerService extends SystemService {
            }
        }
    }

    private final class UserSwitchedReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            synchronized (mLock) {
                final int currentId = intent.getIntExtra(
                        Intent.EXTRA_USER_HANDLE, USER_SYSTEM);
                // only update if the value is actually changed
                if (updateNightModeFromSettingsLocked(context, context.getResources(), currentId)) {
                    updateLocked(0, 0);
                }
            }
        }
    }
}