Partial Screen Sharing: Task Switcher - UI Layer implementation

- Defines model/state
- Implements view model
- Implements "empty" coordinator, that will later be filled with the
  code to show the actual notification

Bug: 286201261
Test: TaskSwitcherNotificationViewModelTest.kt
Change-Id: I3aae76de69e1acedc9eeb0d684c698d9d5dbce66
This commit is contained in:
Christian Göllner
2023-06-21 17:16:50 +02:00
committed by Chris Göllner
parent 6f22f3af64
commit 27f8a2266c
4 changed files with 294 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2023 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.mediaprojection.taskswitcher.ui
import android.content.Context
import android.util.Log
import android.widget.Toast
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.mediaprojection.taskswitcher.ui.model.TaskSwitcherNotificationUiState.NotShowing
import com.android.systemui.mediaprojection.taskswitcher.ui.model.TaskSwitcherNotificationUiState.Showing
import com.android.systemui.mediaprojection.taskswitcher.ui.viewmodel.TaskSwitcherNotificationViewModel
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
/** Coordinator responsible for showing/hiding the task switcher notification. */
@SysUISingleton
class TaskSwitcherNotificationCoordinator
@Inject
constructor(
private val context: Context,
@Application private val applicationScope: CoroutineScope,
@Main private val mainDispatcher: CoroutineDispatcher,
private val viewModel: TaskSwitcherNotificationViewModel,
) {
fun start() {
applicationScope.launch {
viewModel.uiState.flowOn(mainDispatcher).collect { uiState ->
Log.d(TAG, "uiState -> $uiState")
when (uiState) {
is Showing -> showNotification(uiState)
is NotShowing -> hideNotification()
}
}
}
}
private fun showNotification(uiState: Showing) {
val text =
"""
Sharing pauses when you switch apps.
Share this app instead.
Switch back.
"""
.trimIndent()
// TODO(b/286201515): Create actual notification.
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
}
private fun hideNotification() {}
companion object {
private const val TAG = "TaskSwitchNotifCoord"
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright (C) 2023 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.mediaprojection.taskswitcher.ui.model
import android.app.TaskInfo
/** Represents the UI state for the task switcher notification. */
sealed interface TaskSwitcherNotificationUiState {
/** The notification should not be shown. */
object NotShowing : TaskSwitcherNotificationUiState
/** The notification should be shown. */
data class Showing(
val projectedTask: TaskInfo,
val foregroundTask: TaskInfo,
) : TaskSwitcherNotificationUiState
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright (C) 2023 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.mediaprojection.taskswitcher.ui.viewmodel
import android.util.Log
import com.android.systemui.mediaprojection.taskswitcher.domain.interactor.TaskSwitchInteractor
import com.android.systemui.mediaprojection.taskswitcher.domain.model.TaskSwitchState
import com.android.systemui.mediaprojection.taskswitcher.ui.model.TaskSwitcherNotificationUiState
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
class TaskSwitcherNotificationViewModel @Inject constructor(interactor: TaskSwitchInteractor) {
val uiState: Flow<TaskSwitcherNotificationUiState> =
interactor.taskSwitchChanges.map { taskSwitchChange ->
Log.d(TAG, "taskSwitchChange: $taskSwitchChange")
when (taskSwitchChange) {
is TaskSwitchState.TaskSwitched -> {
TaskSwitcherNotificationUiState.Showing(
projectedTask = taskSwitchChange.projectedTask,
foregroundTask = taskSwitchChange.foregroundTask,
)
}
is TaskSwitchState.NotProjectingTask,
is TaskSwitchState.TaskUnchanged -> {
TaskSwitcherNotificationUiState.NotShowing
}
}
}
companion object {
private const val TAG = "TaskSwitchNotifVM"
}
}

View File

@@ -0,0 +1,141 @@
/*
* Copyright (C) 2023 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.mediaprojection.taskswitcher.ui.viewmodel
import android.content.Intent
import android.testing.AndroidTestingRunner
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.mediaprojection.taskswitcher.data.repository.ActivityTaskManagerTasksRepository
import com.android.systemui.mediaprojection.taskswitcher.data.repository.FakeActivityTaskManager
import com.android.systemui.mediaprojection.taskswitcher.data.repository.FakeActivityTaskManager.Companion.createTask
import com.android.systemui.mediaprojection.taskswitcher.data.repository.FakeMediaProjectionRepository
import com.android.systemui.mediaprojection.taskswitcher.domain.interactor.TaskSwitchInteractor
import com.android.systemui.mediaprojection.taskswitcher.ui.model.TaskSwitcherNotificationUiState
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidTestingRunner::class)
@SmallTest
class TaskSwitcherNotificationViewModelTest : SysuiTestCase() {
private val dispatcher = UnconfinedTestDispatcher()
private val testScope = TestScope(dispatcher)
private val fakeActivityTaskManager = FakeActivityTaskManager()
private val mediaRepo = FakeMediaProjectionRepository()
private val tasksRepo =
ActivityTaskManagerTasksRepository(
activityTaskManager = fakeActivityTaskManager.activityTaskManager,
applicationScope = testScope.backgroundScope,
backgroundDispatcher = dispatcher
)
private val interactor = TaskSwitchInteractor(mediaRepo, tasksRepo)
private val viewModel = TaskSwitcherNotificationViewModel(interactor)
@Test
fun uiState_notProjecting_emitsNotShowing() =
testScope.runTest {
mediaRepo.stopProjecting()
val uiState by collectLastValue(viewModel.uiState)
assertThat(uiState).isEqualTo(TaskSwitcherNotificationUiState.NotShowing)
}
@Test
fun uiState_notProjecting_foregroundTaskChanged_emitsNotShowing() =
testScope.runTest {
mediaRepo.stopProjecting()
val uiState by collectLastValue(viewModel.uiState)
mediaRepo.switchProjectedTask(createTask(taskId = 1))
assertThat(uiState).isEqualTo(TaskSwitcherNotificationUiState.NotShowing)
}
@Test
fun uiState_projectingEntireScreen_emitsNotShowing() =
testScope.runTest {
mediaRepo.projectEntireScreen()
val uiState by collectLastValue(viewModel.uiState)
assertThat(uiState).isEqualTo(TaskSwitcherNotificationUiState.NotShowing)
}
@Test
fun uiState_projectingEntireScreen_foregroundTaskChanged_emitsNotShowing() =
testScope.runTest {
mediaRepo.projectEntireScreen()
val uiState by collectLastValue(viewModel.uiState)
mediaRepo.switchProjectedTask(createTask(taskId = 1))
assertThat(uiState).isEqualTo(TaskSwitcherNotificationUiState.NotShowing)
}
@Test
fun uiState_projectingTask_foregroundTaskChanged_different_emitsShowing() =
testScope.runTest {
val projectedTask = createTask(taskId = 1)
val foregroundTask = createTask(taskId = 2)
mediaRepo.switchProjectedTask(projectedTask)
val uiState by collectLastValue(viewModel.uiState)
fakeActivityTaskManager.moveTaskToForeground(foregroundTask)
assertThat(uiState)
.isEqualTo(TaskSwitcherNotificationUiState.Showing(projectedTask, foregroundTask))
}
@Test
fun uiState_projectingTask_foregroundTaskChanged_same_emitsNotShowing() =
testScope.runTest {
val projectedTask = createTask(taskId = 1)
mediaRepo.switchProjectedTask(projectedTask)
val uiState by collectLastValue(viewModel.uiState)
fakeActivityTaskManager.moveTaskToForeground(projectedTask)
assertThat(uiState).isEqualTo(TaskSwitcherNotificationUiState.NotShowing)
}
@Test
fun uiState_projectingTask_foregroundTaskChanged_different_taskIsLauncher_emitsNotShowing() =
testScope.runTest {
val projectedTask = createTask(taskId = 1)
val foregroundTask = createTask(taskId = 2, baseIntent = LAUNCHER_INTENT)
mediaRepo.switchProjectedTask(projectedTask)
val uiState by collectLastValue(viewModel.uiState)
fakeActivityTaskManager.moveTaskToForeground(foregroundTask)
assertThat(uiState).isEqualTo(TaskSwitcherNotificationUiState.NotShowing)
}
companion object {
private val LAUNCHER_INTENT: Intent =
Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)
}
}