1 /*
<lambda>null2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.statusbar.chips.ui.viewmodel
18 
19 import com.android.systemui.dagger.SysUISingleton
20 import com.android.systemui.dagger.qualifiers.Application
21 import com.android.systemui.statusbar.chips.call.ui.viewmodel.CallChipViewModel
22 import com.android.systemui.statusbar.chips.casttootherdevice.ui.viewmodel.CastToOtherDeviceChipViewModel
23 import com.android.systemui.statusbar.chips.screenrecord.ui.viewmodel.ScreenRecordChipViewModel
24 import com.android.systemui.statusbar.chips.sharetoapp.ui.viewmodel.ShareToAppChipViewModel
25 import com.android.systemui.statusbar.chips.ui.model.OngoingActivityChipModel
26 import javax.inject.Inject
27 import kotlinx.coroutines.CoroutineScope
28 import kotlinx.coroutines.flow.SharingStarted
29 import kotlinx.coroutines.flow.StateFlow
30 import kotlinx.coroutines.flow.combine
31 import kotlinx.coroutines.flow.stateIn
32 
33 /**
34  * View model deciding which ongoing activity chip to show in the status bar.
35  *
36  * There may be multiple ongoing activities at the same time, but we can only ever show one chip at
37  * any one time (for now). This class decides which ongoing activity to show if there are multiple.
38  */
39 @SysUISingleton
40 class OngoingActivityChipsViewModel
41 @Inject
42 constructor(
43     @Application scope: CoroutineScope,
44     screenRecordChipViewModel: ScreenRecordChipViewModel,
45     shareToAppChipViewModel: ShareToAppChipViewModel,
46     castToOtherDeviceChipViewModel: CastToOtherDeviceChipViewModel,
47     callChipViewModel: CallChipViewModel,
48 ) {
49     /**
50      * A flow modeling the chip that should be shown in the status bar after accounting for possibly
51      * multiple ongoing activities.
52      *
53      * [com.android.systemui.statusbar.phone.fragment.CollapsedStatusBarFragment] is responsible for
54      * actually displaying the chip.
55      */
56     val chip: StateFlow<OngoingActivityChipModel> =
57         combine(
58                 screenRecordChipViewModel.chip,
59                 shareToAppChipViewModel.chip,
60                 castToOtherDeviceChipViewModel.chip,
61                 callChipViewModel.chip,
62             ) { screenRecord, shareToApp, castToOtherDevice, call ->
63                 // This `when` statement shows the priority order of the chips
64                 when {
65                     // Screen recording also activates the media projection APIs, so whenever the
66                     // screen recording chip is active, the media projection chip would also be
67                     // active. We want the screen-recording-specific chip shown in this case, so we
68                     // give the screen recording chip priority. See b/296461748.
69                     screenRecord is OngoingActivityChipModel.Shown -> screenRecord
70                     shareToApp is OngoingActivityChipModel.Shown -> shareToApp
71                     castToOtherDevice is OngoingActivityChipModel.Shown -> castToOtherDevice
72                     else -> call
73                 }
74             }
75             .stateIn(scope, SharingStarted.WhileSubscribed(), OngoingActivityChipModel.Hidden)
76 }
77