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

Commit c50a1d7d authored by Ioana Alexandru's avatar Ioana Alexandru
Browse files

Prevent modes dialog from updating on theme change

This is a workaround necessary because the background of the dialog
(which we don't control) does not get updated when the theme changes.
Since the composables do update, this leads to showing things like white
text on a white background, which is especially bad since bedtime mode
by default turns on dark theme when activated.

Note that this only works if the composables use
MaterialTheme.colorScheme to get the colors, so I had to update some
components to use that instead of LocalAndroidColorScheme. However, the
documentation of AndroidColorScheme points out that we should use
MaterialTheme.colorScheme whenever possible instead, so this change
should be safe (although unflagged). The dialog itself is flagged behind
modes_ui.

Fix: 364528021
Test: manually tested that toggling a mode that changes the theme
doesn't make the dialog unreadable
Flag: android.app.modes_ui
Flag: EXEMPT trivial compose change

Change-Id: I02f9868a210b1d8d7f57f89d35d8178acccd0e47
parent 5bafeec5
Loading
Loading
Loading
Loading
+7 −17
Original line number Diff line number Diff line
@@ -28,11 +28,11 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.IconButtonColors
import androidx.compose.material3.IconButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.android.compose.theme.LocalAndroidColorScheme

@Composable
fun PlatformButton(
@@ -100,12 +100,7 @@ fun PlatformIconButton(
    @DrawableRes iconResource: Int,
    contentDescription: String?,
) {
    IconButton(
        modifier = modifier,
        onClick = onClick,
        enabled = enabled,
        colors = colors,
    ) {
    IconButton(modifier = modifier, onClick = onClick, enabled = enabled, colors = colors) {
        Icon(
            painter = painterResource(id = iconResource),
            contentDescription = contentDescription,
@@ -118,7 +113,7 @@ private val ButtonPaddings = PaddingValues(horizontal = 16.dp, vertical = 8.dp)

@Composable
private fun filledButtonColors(): ButtonColors {
    val colors = LocalAndroidColorScheme.current
    val colors = MaterialTheme.colorScheme
    return ButtonDefaults.buttonColors(
        containerColor = colors.primary,
        contentColor = colors.onPrimary,
@@ -127,27 +122,22 @@ private fun filledButtonColors(): ButtonColors {

@Composable
private fun outlineButtonColors(): ButtonColors {
    return ButtonDefaults.outlinedButtonColors(
        contentColor = LocalAndroidColorScheme.current.onSurface,
    )
    return ButtonDefaults.outlinedButtonColors(contentColor = MaterialTheme.colorScheme.onSurface)
}

@Composable
private fun iconButtonColors(): IconButtonColors {
    return IconButtonDefaults.filledIconButtonColors(
        contentColor = LocalAndroidColorScheme.current.onSurface,
        contentColor = MaterialTheme.colorScheme.onSurface
    )
}

@Composable
private fun outlineButtonBorder(): BorderStroke {
    return BorderStroke(
        width = 1.dp,
        color = LocalAndroidColorScheme.current.primary,
    )
    return BorderStroke(width = 1.dp, color = MaterialTheme.colorScheme.primary)
}

@Composable
private fun textButtonColors(): ButtonColors {
    return ButtonDefaults.textButtonColors(contentColor = LocalAndroidColorScheme.current.primary)
    return ButtonDefaults.textButtonColors(contentColor = MaterialTheme.colorScheme.primary)
}
+4 −5
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ import androidx.compose.ui.layout.Placeable
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.android.compose.theme.LocalAndroidColorScheme
import kotlin.math.roundToInt

/**
@@ -69,7 +68,7 @@ fun AlertDialogContent(
                Modifier.defaultMinSize(minWidth = defaultSize, minHeight = defaultSize),
                propagateMinConstraints = true,
            ) {
                val iconColor = LocalAndroidColorScheme.current.primary
                val iconColor = MaterialTheme.colorScheme.primary
                CompositionLocalProvider(LocalContentColor provides iconColor) { icon() }
            }

@@ -77,7 +76,7 @@ fun AlertDialogContent(
        }

        // Title.
        val titleColor = LocalAndroidColorScheme.current.onSurface
        val titleColor = MaterialTheme.colorScheme.onSurface
        CompositionLocalProvider(LocalContentColor provides titleColor) {
            ProvideTextStyle(
                MaterialTheme.typography.headlineSmall.copy(textAlign = TextAlign.Center)
@@ -88,7 +87,7 @@ fun AlertDialogContent(
        Spacer(Modifier.height(16.dp))

        // Content.
        val contentColor = LocalAndroidColorScheme.current.onSurfaceVariant
        val contentColor = MaterialTheme.colorScheme.onSurfaceVariant
        Box {
            CompositionLocalProvider(LocalContentColor provides contentColor) {
                ProvideTextStyle(
@@ -169,7 +168,7 @@ private fun AlertDialogButtons(
                                negative.width -
                                positive.width -
                                horizontalSpacing.roundToInt(),
                            0
                            0,
                        )
                    }
                }
+35 −30
Original line number Diff line number Diff line
@@ -19,8 +19,10 @@ package com.android.systemui.statusbar.policy.ui.dialog
import android.content.Intent
import android.provider.Settings
import android.util.Log
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
@@ -30,6 +32,7 @@ import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.android.compose.PlatformButton
import com.android.compose.PlatformOutlinedButton
import com.android.compose.theme.PlatformTheme
import com.android.internal.annotations.VisibleForTesting
import com.android.internal.jank.InteractionJankMonitor
import com.android.systemui.animation.DialogCuj
@@ -74,7 +77,7 @@ constructor(
            currentDialog?.dismiss()
        }

        currentDialog = sysuiDialogFactory.create() { ModesDialogContent(it) }
        currentDialog = sysuiDialogFactory.create { ModesDialogContent(it) }
        currentDialog
            ?.lifecycle
            ?.addObserver(
@@ -91,14 +94,19 @@ constructor(

    @Composable
    private fun ModesDialogContent(dialog: SystemUIDialog) {
        // TODO(b/369376884): The composable does correctly update when the theme changes
        //  while the dialog is open, but the background (which we don't control here)
        //  doesn't, which causes us to show things like white text on a white background.
        //  as a workaround, we remember the original theme and keep it on recomposition.
        val isCurrentlyInDarkTheme = isSystemInDarkTheme()
        val cachedDarkTheme = remember { isCurrentlyInDarkTheme }
        PlatformTheme(isDarkTheme = cachedDarkTheme) {
            AlertDialogContent(
            modifier = Modifier.semantics {
                testTagsAsResourceId = true
            },
                modifier = Modifier.semantics { testTagsAsResourceId = true },
                title = {
                    Text(
                        modifier = Modifier.testTag("modes_title"),
                    text = stringResource(R.string.zen_modes_dialog_title)
                        text = stringResource(R.string.zen_modes_dialog_title),
                    )
                },
                content = { ModeTileGrid(viewModel.get()) },
@@ -114,6 +122,7 @@ constructor(
                },
            )
        }
    }

    @VisibleForTesting
    fun openSettings(dialog: SystemUIDialog) {
@@ -129,7 +138,7 @@ constructor(
        activityStarter.startActivity(
            ZEN_MODE_SETTINGS_INTENT,
            true /* dismissShade */,
            animationController
            animationController,
        )
    }

@@ -163,7 +172,7 @@ constructor(
            Log.w(
                TAG,
                "Cannot launch from dialog, the dialog is not present. " +
                    "Will launch activity without animating."
                    "Will launch activity without animating.",
            )
        }

@@ -172,11 +181,7 @@ constructor(
        if (animationController == null) {
            currentDialog?.dismiss()
        }
        activityStarter.startActivity(
            intent,
            true, /* dismissShade */
            animationController,
        )
        activityStarter.startActivity(intent, true, /* dismissShade */ animationController)
    }

    companion object {