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

Commit 3f5ad738 authored by Winson's avatar Winson
Browse files

Remove target Q check for transient night mode in UiModeManager

The target SDK check I originally added was broken since it used
the wrong UID vs User ID type.

While diagnosing, a separate concern came up about how to handle
different apps' expectations for how night mode should
work.

Checking target is weird because the system/other apps could
also be enabling car mode, and it's unclear which target level
would take precedence if multiple apps requested car mode be
enabled.

Rather than resolving that, this makes night mode transient in Q,
regardless of target version. The use cases in the wild are
probably small, and because ag/5074744 locked night mode by
default, most apps won't be affected by this behavior.

Bug: 117346726

Test: atest UiModeManagerTest
Test: manual car/night mode test app

Change-Id: If43c2f54b93e04918eeed00d8d50536bfb083630
parent 5a95250b
Loading
Loading
Loading
Loading
+6 −31
Original line number Diff line number Diff line
@@ -30,13 +30,11 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.BatteryManager;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.PowerManager;
import android.os.PowerManager.ServiceType;
@@ -356,8 +354,8 @@ final class UiModeManagerService extends SystemService {
            try {
                synchronized (mLock) {
                    if (mNightMode != mode) {
                        // Only persist setting if not transient night mode or not in car mode
                        if (!shouldTransientNightWhenInCarMode() || !mCarModeEnabled) {
                        // Only persist setting if not in car mode
                        if (!mCarModeEnabled) {
                            Settings.Secure.putIntForUser(getContext().getContentResolver(),
                                    Settings.Secure.UI_NIGHT_MODE, mode, user);
                        }
@@ -444,34 +442,12 @@ final class UiModeManagerService extends SystemService {
        }
    }

    // Night mode settings in car mode are only persisted below Q.
    // When targeting Q, changes are not saved and night mode will be re-read
    // from settings when exiting car mode.
    private boolean shouldTransientNightWhenInCarMode() {
        int uid = Binder.getCallingUid();
        PackageManager packageManager = getContext().getPackageManager();
        String[] packagesForUid = packageManager.getPackagesForUid(uid);
        if (packagesForUid == null || packagesForUid.length == 0) {
            return false;
        }

        try {
            ApplicationInfo appInfo = packageManager.getApplicationInfoAsUser(
                    packagesForUid[0], 0, uid);

            return appInfo.targetSdkVersion >= Build.VERSION_CODES.Q;
        } catch (PackageManager.NameNotFoundException ignored) {
        }

        return false;
    }

    void setCarModeLocked(boolean enabled, int flags) {
        if (mCarModeEnabled != enabled) {
            mCarModeEnabled = enabled;

            // When transient night mode and exiting car mode, restore night mode from settings
            if (shouldTransientNightWhenInCarMode() && !mCarModeEnabled) {
            // When exiting car mode, restore night mode from settings
            if (!mCarModeEnabled) {
                Context context = getContext();
                updateNightModeFromSettings(context,
                        context.getResources(),
@@ -534,9 +510,8 @@ final class UiModeManagerService extends SystemService {
            uiMode |= mNightMode << 4;
        }

        // Override night mode in power save mode if not transient night mode or not in car mode
        boolean shouldOverrideNight = !mCarModeEnabled || !shouldTransientNightWhenInCarMode();
        if (mPowerSave && shouldOverrideNight) {
        // Override night mode in power save mode if not in car mode
        if (mPowerSave && !mCarModeEnabled) {
            uiMode &= ~Configuration.UI_MODE_NIGHT_NO;
            uiMode |= Configuration.UI_MODE_NIGHT_YES;
        }