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

Commit b2be7ebe authored by Anton Potapov's avatar Anton Potapov Committed by Android (Google) Code Review
Browse files

Merge changes Iad89f93d,I6bcc80e1,I43e2245d into main

* changes:
  Add tests for the dialog showing logic
  Add showing the new Volume Dialog based on the VolumeDialogController callback
  Add window configuration for the new Volume Dialog
parents fa5e4152 584582dd
Loading
Loading
Loading
Loading
+150 −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.volume.dialog.domain.interactor

import android.app.ActivityManager
import android.testing.TestableLooper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.testScope
import com.android.systemui.plugins.fakeVolumeDialogController
import com.android.systemui.testKosmos
import com.android.systemui.volume.Events
import com.android.systemui.volume.dialog.domain.model.VolumeDialogVisibilityModel
import com.google.common.truth.Truth.assertThat
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

private val dialogTimeoutDuration = 3.seconds

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(AndroidJUnit4::class)
@TestableLooper.RunWithLooper()
class VolumeDialogVisibilityInteractorTest : SysuiTestCase() {

    private val kosmos: Kosmos = testKosmos()

    private lateinit var underTest: VolumeDialogVisibilityInteractor

    @Before
    fun setUp() {
        underTest = kosmos.volumeDialogVisibilityInteractor
    }

    @Test
    fun testShowRequest_visible() =
        with(kosmos) {
            testScope.runTest {
                runCurrent()
                val visibilityModel by collectLastValue(underTest.dialogVisibility)
                fakeVolumeDialogController.onShowRequested(
                    Events.SHOW_REASON_VOLUME_CHANGED,
                    false,
                    ActivityManager.LOCK_TASK_MODE_LOCKED,
                )
                runCurrent()

                assertThat(visibilityModel!!)
                    .isEqualTo(
                        VolumeDialogVisibilityModel.Visible(
                            Events.SHOW_REASON_VOLUME_CHANGED,
                            false,
                            ActivityManager.LOCK_TASK_MODE_LOCKED,
                        )
                    )
            }
        }

    @Test
    fun testDismissRequest_dismissed() =
        with(kosmos) {
            testScope.runTest {
                runCurrent()
                val visibilityModel by collectLastValue(underTest.dialogVisibility)
                fakeVolumeDialogController.onShowRequested(
                    Events.SHOW_REASON_VOLUME_CHANGED,
                    false,
                    ActivityManager.LOCK_TASK_MODE_LOCKED,
                )
                runCurrent()

                fakeVolumeDialogController.onDismissRequested(Events.DISMISS_REASON_SCREEN_OFF)

                assertThat(visibilityModel!!)
                    .isEqualTo(
                        VolumeDialogVisibilityModel.Dismissed(Events.DISMISS_REASON_SCREEN_OFF)
                    )
            }
        }

    @Test
    fun testTimeout_dismissed() =
        with(kosmos) {
            testScope.runTest {
                runCurrent()
                underTest.resetDismissTimeout()
                val visibilityModel by collectLastValue(underTest.dialogVisibility)
                fakeVolumeDialogController.onShowRequested(
                    Events.SHOW_REASON_VOLUME_CHANGED,
                    false,
                    ActivityManager.LOCK_TASK_MODE_LOCKED,
                )
                runCurrent()

                advanceTimeBy(1.days)

                assertThat(visibilityModel!!)
                    .isEqualTo(VolumeDialogVisibilityModel.Dismissed(Events.DISMISS_REASON_TIMEOUT))
            }
        }

    @Test
    fun testResetTimeoutInterruptsEvents() =
        with(kosmos) {
            testScope.runTest {
                runCurrent()
                underTest.resetDismissTimeout()
                val visibilityModel by collectLastValue(underTest.dialogVisibility)
                fakeVolumeDialogController.onShowRequested(
                    Events.SHOW_REASON_VOLUME_CHANGED,
                    false,
                    ActivityManager.LOCK_TASK_MODE_LOCKED,
                )
                runCurrent()

                advanceTimeBy(dialogTimeoutDuration / 2)
                underTest.resetDismissTimeout()
                advanceTimeBy(dialogTimeoutDuration / 2)
                underTest.resetDismissTimeout()
                advanceTimeBy(dialogTimeoutDuration / 2)

                assertThat(visibilityModel)
                    .isInstanceOf(VolumeDialogVisibilityModel.Visible::class.java)
            }
        }
}
+1 −0
Original line number Diff line number Diff line
@@ -540,6 +540,7 @@
    <!-- Overridden by values-television/styles.xml with tv-specific settings -->
    <style name="volume_dialog_theme" parent="Theme.SystemUI">
        <item name="android:windowIsFloating">true</item>
        <item name="android:showWhenLocked">true</item>
    </style>

    <style name="Theme.SystemUI.DayNightDialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog"/>
+33 −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.statusbar.policy

import com.android.systemui.statusbar.policy.DevicePostureController.DevicePostureInt
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onStart

/** [DevicePostureController.getDevicePosture] as a [Flow]. */
@DevicePostureInt
fun DevicePostureController.devicePosture(): Flow<Int> =
    conflatedCallbackFlow {
            val callback = DevicePostureController.Callback { posture -> trySend(posture) }
            addCallback(callback)
            awaitClose { removeCallback(callback) }
        }
        .onStart { emit(devicePosture) }
+0 −67
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.volume.dialog

import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.plugins.VolumeDialog
import com.android.systemui.volume.dialog.dagger.VolumeDialogComponent
import com.android.systemui.volume.dialog.dagger.VolumeDialogPluginComponent
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

class NewVolumeDialogPlugin
@Inject
constructor(
    @Application private val applicationCoroutineScope: CoroutineScope,
    private val volumeDialogPluginComponentFactory: VolumeDialogPluginComponent.Factory,
) : VolumeDialog {

    private var volumeDialogPluginComponent: VolumeDialogPluginComponent? = null
    private var job: Job? = null

    override fun init(windowType: Int, callback: VolumeDialog.Callback?) {
        job =
            applicationCoroutineScope.launch {
                coroutineScope {
                    volumeDialogPluginComponent = volumeDialogPluginComponentFactory.create(this)
                }
            }
    }

    private fun showDialog() {
        val volumeDialogPluginComponent =
            volumeDialogPluginComponent ?: error("Creating dialog before init was called")
        volumeDialogPluginComponent.coroutineScope().launch {
            coroutineScope {
                val volumeDialogComponent: VolumeDialogComponent =
                    volumeDialogPluginComponent.volumeDialogComponentFactory().create(this)
                with(volumeDialogComponent.volumeDialog()) {
                    setOnDismissListener { volumeDialogComponent.coroutineScope().cancel() }
                    show()
                }
            }
        }
    }

    override fun destroy() {
        job?.cancel()
    }
}
+27 −3
Original line number Diff line number Diff line
@@ -20,15 +20,39 @@ import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.ContextThemeWrapper
import android.view.MotionEvent
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.res.R
import com.android.systemui.volume.Events
import com.android.systemui.volume.dialog.domain.interactor.VolumeDialogVisibilityInteractor
import com.android.systemui.volume.dialog.ui.binder.VolumeDialogBinder
import javax.inject.Inject

class VolumeDialog @Inject constructor(@Application context: Context) :
    Dialog(ContextThemeWrapper(context, R.style.volume_dialog_theme)) {
class VolumeDialog
@Inject
constructor(
    @Application context: Context,
    private val dialogBinder: VolumeDialogBinder,
    private val visibilityInteractor: VolumeDialogVisibilityInteractor,
) : Dialog(ContextThemeWrapper(context, R.style.volume_dialog_theme)) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.volume_dialog)
        dialogBinder.bind(this)
    }

    /**
     * NOTE: This will be called with ACTION_OUTSIDE MotionEvents for touches that occur outside of
     * the touchable region of the volume dialog (as returned by [.onComputeInternalInsets]) even if
     * those touches occurred within the bounds of the volume dialog.
     */
    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (isShowing) {
            if (event.action == MotionEvent.ACTION_OUTSIDE) {
                visibilityInteractor.dismissDialog(Events.DISMISS_REASON_TOUCH_OUTSIDE)
                return true
            }
        }
        return false
    }
}
Loading