1 /*
2  * Copyright (C) 2021 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.collection.provider
18 
19 import com.android.internal.statusbar.NotificationVisibility
20 import com.android.systemui.dagger.SysUISingleton
21 import com.android.systemui.statusbar.notification.collection.NotifLiveDataStore
22 import com.android.systemui.statusbar.notification.collection.NotificationEntry
23 import com.android.systemui.statusbar.notification.collection.notifcollection.CommonNotifCollection
24 import com.android.systemui.statusbar.notification.collection.render.NotificationVisibilityProvider
25 import com.android.systemui.statusbar.notification.logging.NotificationLogger
26 import javax.inject.Inject
27 
28 /** pipeline-agnostic implementation for getting [NotificationVisibility]. */
29 @SysUISingleton
30 class NotificationVisibilityProviderImpl @Inject constructor(
31     private val notifDataStore: NotifLiveDataStore,
32     private val notifCollection: CommonNotifCollection
33 ) : NotificationVisibilityProvider {
34 
35     override fun obtain(entry: NotificationEntry, visible: Boolean): NotificationVisibility {
36         val count: Int = getCount()
37         val rank = entry.ranking.rank
38         val hasRow = entry.row != null
39         val location = NotificationLogger.getNotificationLocation(entry)
40         return NotificationVisibility.obtain(entry.key, rank, count, visible && hasRow, location)
41     }
42 
43     override fun obtain(key: String, visible: Boolean): NotificationVisibility =
44         notifCollection.getEntry(key)?.let { return obtain(it, visible) }
45             ?: NotificationVisibility.obtain(key, -1, getCount(), false)
46 
47     override fun getLocation(key: String): NotificationVisibility.NotificationLocation =
48         NotificationLogger.getNotificationLocation(notifCollection.getEntry(key))
49 
50     private fun getCount() = notifDataStore.activeNotifCount.value
51 }
52