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.notification.stack.ui.viewbinder 18 19 import com.android.systemui.common.coroutine.ConflatedCallbackFlow 20 import com.android.systemui.statusbar.notification.stack.NotificationStackScrollLayout 21 import com.android.systemui.statusbar.notification.stack.ui.view.NotificationStatsLogger 22 import com.android.systemui.statusbar.notification.stack.ui.viewmodel.NotificationLoggerViewModel 23 import com.android.systemui.util.kotlin.Utils 24 import com.android.systemui.util.kotlin.sample 25 import com.android.systemui.util.kotlin.throttle 26 import kotlinx.coroutines.channels.awaitClose 27 import kotlinx.coroutines.flow.collectLatest 28 import kotlinx.coroutines.flow.combine 29 30 /** 31 * Binds a [NotificationStatsLogger] to its [NotificationLoggerViewModel], and wires in 32 * [NotificationStackScrollLayout.OnNotificationLocationsChangedListener] updates to it. 33 */ 34 object NotificationStatsLoggerBinder { 35 36 /** minimum delay in ms between Notification location updates */ 37 private const val NOTIFICATION_UPDATE_PERIOD_MS = 500L 38 39 suspend fun bindLogger( 40 view: NotificationStackScrollLayout, 41 logger: NotificationStatsLogger, 42 viewModel: NotificationLoggerViewModel, 43 ) { 44 // Updates the logger about whether the Notification panel, and the individual Notifications 45 // are visible to the user. 46 viewModel.isLockscreenOrShadeInteractive 47 .sample( 48 combine(viewModel.isOnLockScreen, viewModel.activeNotifications, ::Pair), 49 Utils.Companion::toTriple 50 ) 51 .collectLatest { (isPanelInteractive, isOnLockScreen, notifications) -> 52 if (isPanelInteractive) { 53 logger.onLockscreenOrShadeInteractive( 54 isOnLockScreen = isOnLockScreen, 55 activeNotifications = notifications, 56 ) 57 view.onNotificationLocationsUpdated 58 // Delay the updates with [NOTIFICATION_UPDATE_PERIOD_MS]. If the original 59 // flow emits more than once during this period, only the latest value is 60 // emitted, meaning that we won't log the intermediate Notification states. 61 .throttle(NOTIFICATION_UPDATE_PERIOD_MS) 62 .sample(viewModel.activeNotificationRanks, ::Pair) 63 .collect { (locationsProvider, ranks) -> 64 logger.onNotificationLocationsChanged(locationsProvider, ranks) 65 } 66 } else { 67 logger.onLockscreenOrShadeNotInteractive( 68 activeNotifications = notifications, 69 ) 70 } 71 } 72 } 73 } 74 75 private val NotificationStackScrollLayout.onNotificationLocationsUpdated 76 get() = <lambda>null77 ConflatedCallbackFlow.conflatedCallbackFlow { 78 val callback = 79 NotificationStackScrollLayout.OnNotificationLocationsChangedListener { callable -> 80 trySend(callable) 81 } 82 setNotificationLocationsChangedListener(callback) 83 awaitClose { setNotificationLocationsChangedListener(null) } 84 } 85