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

Commit 5948319e authored by Jay Aliomer's avatar Jay Aliomer
Browse files

Dark theme helper

Since adding custom and auto night mode, applications have been
making mistakes in checking if the device is in dark theme.

The API for checking dark theme is not very clear. They use
UiModeManager.getNightMode and checking if it is a yes or no thinking
that it refers to Dark theme. Where it is the service scheduler modes.

The most famous example of such mishap is the gboard app (b/145820343) and the dialer
(b/148142709).

We had to test all google apps that implement dark theme on android.

Test: atest UiModeManagerTest and Config tests
Fixes: 148606412
Change-Id: I45dbd9daa057b14d5dd2e88115f666db99d19145
parent 429b6896
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -12552,6 +12552,7 @@ package android.content.res {
    method public int getLayoutDirection();
    method @NonNull public android.os.LocaleList getLocales();
    method public boolean isLayoutSizeAtLeast(int);
    method public boolean isNightModeActive();
    method public boolean isScreenHdr();
    method public boolean isScreenRound();
    method public boolean isScreenWideColorGamut();
+10 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.TestApi;
import android.app.UiModeManager;
import android.app.WindowConfiguration;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.LocaleProto;
@@ -1975,6 +1976,15 @@ public final class Configuration implements Parcelable, Comparable<Configuration
        readFromParcel(source);
    }


    /**
     * Retuns whether the configuration is in night mode
     * @return true if night mode is active and false otherwise
     */
    public boolean isNightModeActive() {
        return (uiMode & UI_MODE_NIGHT_MASK) == UI_MODE_NIGHT_YES;
    }

    public int compareTo(Configuration that) {
        int n;
        float a = this.fontScale;
+9 −0
Original line number Diff line number Diff line
@@ -148,6 +148,15 @@ public class ConfigurationTest extends TestCase {
        assertEquals(SMALLEST_SCREEN_WIDTH_DP_UNDEFINED, config.smallestScreenWidthDp);
    }

    @Test
    public void testNightModeHelper() {
        Configuration config = new Configuration();
        config.uiMode = Configuration.UI_MODE_NIGHT_YES;
        assertTrue(config.isNightModeActive());
        config.uiMode = Configuration.UI_MODE_NIGHT_NO;
        assertFalse(config.isNightModeActive());
    }

    private void dumpDebug(File f, Configuration config) throws Exception {
        final AtomicFile af = new AtomicFile(f);
        FileOutputStream fos = af.startWrite();