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

Commit 2f792ef8 authored by Salvador Martinez's avatar Salvador Martinez
Browse files

Fix user setting for dark mode not being applied

There was nothing making it so that night mode would be read
from settings global when the user was switched. Additionally
the settings was not being explicitly saved for the user. This
change makes it so we save the setting per user and registers
a receiver to update the UI when the user changes.

Test: Manual verification in settings by toggling
Change-Id: I4ddbc6bbcc51c81cb0271525a71b37440581feab
Fixes: 116001869
parent 54756a66
Loading
Loading
Loading
Loading
+41 −6
Original line number Original line Diff line number Diff line
@@ -230,10 +230,7 @@ final class UiModeManagerService extends SystemService {
                || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
                || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
        mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
        mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);


        final int defaultNightMode = res.getInteger(
        updateNightModeFromSettings(context, res, UserHandle.getCallingUserId());
                com.android.internal.R.integer.config_defaultNightMode);
        mNightMode = Settings.Secure.getInt(context.getContentResolver(),
                Settings.Secure.UI_NIGHT_MODE, defaultNightMode);


        // Update the initial, static configurations.
        // Update the initial, static configurations.
        SystemServerInitThreadPool.get().submit(() -> {
        SystemServerInitThreadPool.get().submit(() -> {
@@ -245,6 +242,29 @@ final class UiModeManagerService extends SystemService {
        }, TAG + ".onStart");
        }, TAG + ".onStart");
        publishBinderService(Context.UI_MODE_SERVICE, mService);
        publishBinderService(Context.UI_MODE_SERVICE, mService);
        publishLocalService(UiModeManagerInternal.class, mLocalService);
        publishLocalService(UiModeManagerInternal.class, mLocalService);

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_USER_SWITCHED);
        context.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler);
    }

    /**
     * Updates the night mode setting in Settings.Global and returns if the value was successfully
     * changed.
     * @param context A valid context
     * @param res A valid resource object
     * @param userId The user to update the setting for
     * @return True if the new value is different from the old value. False otherwise.
     */
    private boolean updateNightModeFromSettings(Context context, Resources res, int userId) {
        final int defaultNightMode = res.getInteger(
                com.android.internal.R.integer.config_defaultNightMode);
        int oldNightMode = mNightMode;
        mNightMode = Settings.Secure.getIntForUser(context.getContentResolver(),
                Settings.Secure.UI_NIGHT_MODE, defaultNightMode, userId);

        // false if night mode stayed the same, true otherwise.
        return !(oldNightMode == mNightMode);
    }
    }


    private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
    private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
@@ -315,12 +335,13 @@ final class UiModeManagerService extends SystemService {
                    throw new IllegalArgumentException("Unknown mode: " + mode);
                    throw new IllegalArgumentException("Unknown mode: " + mode);
            }
            }


            final int user = UserHandle.getCallingUserId();
            final long ident = Binder.clearCallingIdentity();
            final long ident = Binder.clearCallingIdentity();
            try {
            try {
                synchronized (mLock) {
                synchronized (mLock) {
                    if (mNightMode != mode) {
                    if (mNightMode != mode) {
                        Settings.Secure.putInt(getContext().getContentResolver(),
                        Settings.Secure.putIntForUser(getContext().getContentResolver(),
                                Settings.Secure.UI_NIGHT_MODE, mode);
                                Settings.Secure.UI_NIGHT_MODE, mode, user);
                        mNightMode = mode;
                        mNightMode = mode;
                        updateLocked(0, 0);
                        updateLocked(0, 0);
                    }
                    }
@@ -860,4 +881,18 @@ 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, UserHandle.USER_SYSTEM);
                // only update if the value is actually changed
                if (updateNightModeFromSettings(context, context.getResources(), currentId)) {
                    updateLocked(0, 0);
                }
            }
        }
    }
}
}