1 /*
2  * Copyright (C) 2023 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.notification.shelf.ui.viewbinder
18 
19 import com.android.app.tracing.traceSection
20 import com.android.systemui.plugins.FalsingManager
21 import com.android.systemui.statusbar.NotificationShelf
22 import com.android.systemui.statusbar.notification.icon.ui.viewbinder.NotificationIconContainerShelfViewBinder
23 import com.android.systemui.statusbar.notification.row.ui.viewbinder.ActivatableNotificationViewBinder
24 import com.android.systemui.statusbar.notification.shared.NotificationIconContainerRefactor
25 import com.android.systemui.statusbar.notification.shelf.ui.viewmodel.NotificationShelfViewModel
26 import com.android.systemui.statusbar.phone.NotificationIconAreaController
27 import kotlinx.coroutines.awaitCancellation
28 import kotlinx.coroutines.coroutineScope
29 import kotlinx.coroutines.launch
30 
31 /** Binds a [NotificationShelf] to its [view model][NotificationShelfViewModel]. */
32 object NotificationShelfViewBinder {
bindnull33     suspend fun bind(
34         shelf: NotificationShelf,
35         viewModel: NotificationShelfViewModel,
36         falsingManager: FalsingManager,
37         nicBinder: NotificationIconContainerShelfViewBinder,
38         notificationIconAreaController: NotificationIconAreaController,
39     ): Unit = coroutineScope {
40         ActivatableNotificationViewBinder.bind(viewModel, shelf, falsingManager)
41         shelf.apply {
42             traceSection("NotifShelf#bindShelfIcons") {
43                 if (NotificationIconContainerRefactor.isEnabled) {
44                     launch { nicBinder.bind(shelfIcons) }
45                 } else {
46                     notificationIconAreaController.setShelfIcons(shelfIcons)
47                 }
48             }
49             launch {
50                 viewModel.canModifyColorOfNotifications.collect(::setCanModifyColorOfNotifications)
51             }
52             launch { viewModel.isClickable.collect(::setCanInteract) }
53             registerViewListenersWhileAttached(shelf, viewModel)
54         }
55     }
56 
registerViewListenersWhileAttachednull57     private suspend fun registerViewListenersWhileAttached(
58         shelf: NotificationShelf,
59         viewModel: NotificationShelfViewModel,
60     ) {
61         try {
62             shelf.setOnClickListener { viewModel.onShelfClicked() }
63             awaitCancellation()
64         } finally {
65             shelf.setOnClickListener(null)
66         }
67     }
68 }
69