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

Commit fc92ee10 authored by Will Leshner's avatar Will Leshner Committed by William Xiao
Browse files

Add a button to glanceable hub to swap to dream.

Bug: 378174118
Test: atest CommunalViewModelTest
Test: Manually by tapping button and verifying the swap to dream.
Flag: com.android.systemui.glanceable_hub_v2
Change-Id: I63776346607d2b7f1666440aaaa8d7c9a4b043c4
parent e01df808
Loading
Loading
Loading
Loading
+30 −3
Original line number Diff line number Diff line
@@ -23,12 +23,17 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import com.android.compose.animation.scene.SceneScope
import com.android.systemui.communal.smartspace.SmartspaceInteractionHandler
import com.android.systemui.communal.ui.compose.section.AmbientStatusBarSection
import com.android.systemui.communal.ui.compose.section.CommunalPopupSection
import com.android.systemui.communal.ui.compose.section.CommunalToDreamButtonSection
import com.android.systemui.communal.ui.view.layout.sections.CommunalAppWidgetSection
import com.android.systemui.communal.ui.viewmodel.CommunalViewModel
import com.android.systemui.keyguard.ui.composable.blueprint.BlueprintAlignmentLines
@@ -49,6 +54,7 @@ constructor(
    private val ambientStatusBarSection: AmbientStatusBarSection,
    private val communalPopupSection: CommunalPopupSection,
    private val widgetSection: CommunalAppWidgetSection,
    private val communalToDreamButtonSection: CommunalToDreamButtonSection,
) {

    @Composable
@@ -82,11 +88,13 @@ constructor(
                            Modifier.element(Communal.Elements.IndicationArea).fillMaxWidth()
                        )
                    }
                    with(communalToDreamButtonSection) { Button() }
                },
            ) { measurables, constraints ->
                val communalGridMeasurable = measurables[0]
                val lockIconMeasurable = measurables[1]
                val bottomAreaMeasurable = measurables[2]
                val screensaverButtonMeasurable: Measurable? = measurables.getOrNull(3)

                val noMinConstraints = constraints.copy(minWidth = 0, minHeight = 0)

@@ -101,6 +109,15 @@ constructor(

                val bottomAreaPlaceable = bottomAreaMeasurable.measure(noMinConstraints)

                val screensaverButtonSizeInt = screensaverButtonSize.roundToPx()
                val screensaverButtonPlaceable =
                    screensaverButtonMeasurable?.measure(
                        Constraints.fixed(
                            width = screensaverButtonSizeInt,
                            height = screensaverButtonSizeInt,
                        )
                    )

                val communalGridPlaceable =
                    communalGridMeasurable.measure(
                        noMinConstraints.copy(maxHeight = lockIconBounds.top)
@@ -109,12 +126,22 @@ constructor(
                layout(constraints.maxWidth, constraints.maxHeight) {
                    communalGridPlaceable.place(x = 0, y = 0)
                    lockIconPlaceable.place(x = lockIconBounds.left, y = lockIconBounds.top)
                    bottomAreaPlaceable.place(
                        x = 0,
                        y = constraints.maxHeight - bottomAreaPlaceable.height,

                    val bottomAreaTop = constraints.maxHeight - bottomAreaPlaceable.height
                    bottomAreaPlaceable.place(x = 0, y = bottomAreaTop)
                    screensaverButtonPlaceable?.place(
                        x =
                            constraints.maxWidth -
                                screensaverButtonSizeInt -
                                Dimensions.ItemSpacing.roundToPx(),
                        y = lockIconBounds.top,
                    )
                }
            }
        }
    }

    companion object {
        val screensaverButtonSize: Dp = 64.dp
    }
}
+65 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.communal.ui.compose.section

import androidx.compose.material3.IconButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.android.compose.PlatformIconButton
import com.android.systemui.communal.domain.interactor.CommunalSettingsInteractor
import com.android.systemui.communal.ui.viewmodel.CommunalToDreamButtonViewModel
import com.android.systemui.lifecycle.rememberViewModel
import com.android.systemui.res.R
import javax.inject.Inject

class CommunalToDreamButtonSection
@Inject
constructor(
    private val communalSettingsInteractor: CommunalSettingsInteractor,
    private val viewModelFactory: CommunalToDreamButtonViewModel.Factory,
) {
    @Composable
    fun Button() {
        if (!communalSettingsInteractor.isV2FlagEnabled()) {
            return
        }

        val viewModel =
            rememberViewModel("CommunalToDreamButtonSection") { viewModelFactory.create() }
        val shouldShowDreamButtonOnHub by
            viewModel.shouldShowDreamButtonOnHub.collectAsStateWithLifecycle(false)

        if (!shouldShowDreamButtonOnHub) {
            return
        }

        PlatformIconButton(
            onClick = { viewModel.onShowDreamButtonTap() },
            iconResource = R.drawable.ic_screensaver_auto,
            contentDescription =
                stringResource(R.string.accessibility_glanceable_hub_to_dream_button),
            colors =
                IconButtonDefaults.filledIconButtonColors(
                    contentColor = MaterialTheme.colorScheme.onPrimaryContainer,
                    containerColor = MaterialTheme.colorScheme.primaryContainer,
                ),
        )
    }
}
+104 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.communal.ui.viewmodel

import android.platform.test.annotations.EnableFlags
import android.service.dream.dreamManager
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.Flags.FLAG_GLANCEABLE_HUB_V2
import com.android.systemui.SysuiTestCase
import com.android.systemui.flags.Flags.COMMUNAL_SERVICE_ENABLED
import com.android.systemui.flags.fakeFeatureFlagsClassic
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runCurrent
import com.android.systemui.kosmos.runTest
import com.android.systemui.kosmos.testScope
import com.android.systemui.lifecycle.activateIn
import com.android.systemui.statusbar.policy.batteryController
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.verify
import org.mockito.kotlin.any
import org.mockito.kotlin.whenever

@SmallTest
@EnableFlags(FLAG_GLANCEABLE_HUB_V2)
@RunWith(AndroidJUnit4::class)
class CommunalToDreamButtonViewModelTest : SysuiTestCase() {
    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope
    private val underTest: CommunalToDreamButtonViewModel by lazy {
        kosmos.communalToDreamButtonViewModel
    }

    @Before
    fun setUp() {
        kosmos.fakeFeatureFlagsClassic.set(COMMUNAL_SERVICE_ENABLED, true)
        underTest.activateIn(testScope)
    }

    @Test
    fun shouldShowDreamButtonOnHub_trueWhenCanDream() =
        with(kosmos) {
            runTest {
                whenever(dreamManager.canStartDreaming(any())).thenReturn(true)
                whenever(batteryController.isPluggedIn()).thenReturn(true)

                val shouldShowButton by collectLastValue(underTest.shouldShowDreamButtonOnHub)
                assertThat(shouldShowButton).isTrue()
            }
        }

    @Test
    fun shouldShowDreamButtonOnHub_falseWhenCannotDream() =
        with(kosmos) {
            runTest {
                whenever(dreamManager.canStartDreaming(any())).thenReturn(false)
                whenever(batteryController.isPluggedIn()).thenReturn(true)

                val shouldShowButton by collectLastValue(underTest.shouldShowDreamButtonOnHub)
                assertThat(shouldShowButton).isFalse()
            }
        }

    @Test
    fun shouldShowDreamButtonOnHub_falseWhenNotPluggedIn() =
        with(kosmos) {
            runTest {
                whenever(dreamManager.canStartDreaming(any())).thenReturn(true)
                whenever(batteryController.isPluggedIn()).thenReturn(false)

                val shouldShowButton by collectLastValue(underTest.shouldShowDreamButtonOnHub)
                assertThat(shouldShowButton).isFalse()
            }
        }

    @Test
    fun onShowDreamButtonTap_startsDream() =
        with(kosmos) {
            runTest {
                underTest.onShowDreamButtonTap()
                runCurrent()

                verify(dreamManager).startDream()
            }
        }
}
+26 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?><!--
  ~ Copyright (C) 2024 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="960"
    android:viewportHeight="960"
    android:tint="?attr/colorControlNormal">
    <path
        android:fillColor="@android:color/white"
        android:pathData="M160,240Q160,240 160,240Q160,240 160,240L160,720Q160,720 160,720Q160,720 160,720L160,720Q160,720 160,720Q160,720 160,720L160,240Q160,240 160,240Q160,240 160,240L160,240Q160,240 160,240Q160,240 160,240Q160,240 160,240Q160,240 160,240ZM160,800Q127,800 103.5,776.5Q80,753 80,720L80,240Q80,207 103.5,183.5Q127,160 160,160L440,160Q440,179 440,199Q440,219 440,240L160,240Q160,240 160,240Q160,240 160,240L160,720Q160,720 160,720Q160,720 160,720L800,720Q800,720 800,720Q800,720 800,720L800,480Q823,480 843,480Q863,480 880,480L880,720Q880,753 856.5,776.5Q833,800 800,800L160,800ZM700,480Q700,388 636,324Q572,260 480,260Q572,260 636,196Q700,132 700,40Q700,132 764,196Q828,260 920,260Q828,260 764,324Q700,388 700,480Z"/>
</vector>
+2 −0
Original line number Diff line number Diff line
@@ -1342,6 +1342,8 @@
    <string name="glanceable_hub_lockscreen_affordance_disabled_text">To add the \"Widgets\" shortcut, make sure \"Show widgets on lock screen\" is enabled in settings.</string>
    <!-- Label for a button used to open Settings in order to enable showing widgets on the lock screen. [CHAR LIMIT=NONE] -->
    <string name="glanceable_hub_lockscreen_affordance_action_button_label">Settings</string>
    <!-- Content description for a "show screensaver" button on glanceable hub. [CHAR LIMIT=NONE] -->
    <string name="accessibility_glanceable_hub_to_dream_button">Show screensaver button</string>

    <!-- Related to user switcher --><skip/>

Loading