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

Commit e81d5f63 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
Merged-In: I78a490c0f083e92022e1995b3e19dc7dd567a7c3
Change-Id: I78a490c0f083e92022e1995b3e19dc7dd567a7c3
(cherry picked from commit 7bddda48)
parent 8c662c34
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -7896,6 +7896,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
+28 −17
Original line number Diff line number Diff line
@@ -327,6 +327,13 @@ final class UiModeManagerService extends SystemService {
      	mCurrentUser = userHandle;
        getContext().getContentResolver().unregisterContentObserver(mSetupWizardObserver);
        verifySetupWizardCompleted();
        synchronized (mLock) {
            // only update if the value is actually changed
            if (updateNightModeFromSettingsLocked(getContext(), getContext().getResources(),
                   mCurrentUser)) {
                updateLocked(0, 0);
            }
        }
    }

    @Override
@@ -354,11 +361,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();
            }
@@ -405,6 +411,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) {
@@ -506,6 +527,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;
@@ -1596,18 +1621,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);
                }
            }
        }
    }
}