• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright 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.collection.coordinator
18 
19 import android.util.ArrayMap
20 import com.android.systemui.statusbar.notification.collection.BundleEntry
21 import com.android.systemui.statusbar.notification.collection.GroupEntry
22 import com.android.systemui.statusbar.notification.collection.PipelineEntry
23 import com.android.systemui.statusbar.notification.collection.NotifPipeline
24 import com.android.systemui.statusbar.notification.collection.coordinator.dagger.CoordinatorScope
25 import com.android.systemui.statusbar.notification.collection.render.NotifGroupController
26 import javax.inject.Inject
27 
28 /** A small coordinator which calculates, stores, and applies the untruncated child count. */
29 @CoordinatorScope
30 class GroupCountCoordinator @Inject constructor() : Coordinator {
31     private val untruncatedChildCounts = ArrayMap<GroupEntry, Int>()
32 
33     override fun attach(pipeline: NotifPipeline) {
34         pipeline.addOnBeforeFinalizeFilterListener(::onBeforeFinalizeFilter)
35         pipeline.addOnAfterRenderGroupListener(::onAfterRenderGroup)
36     }
37 
38     private fun onBeforeFinalizeFilter(entries: List<PipelineEntry>) {
39         // save untruncated child counts to our internal map
40         untruncatedChildCounts.clear()
41         entries.asSequence()
42             .flatMap { entry ->
43                 when (entry) {
44                     is GroupEntry -> listOf(entry)
45                     is BundleEntry -> entry.children.filterIsInstance<GroupEntry>()
46                     else -> emptyList()
47                 }
48             }
49             .forEach { groupEntry ->
50                 untruncatedChildCounts[groupEntry] = groupEntry.children.size
51             }
52     }
53 
54     private fun onAfterRenderGroup(group: GroupEntry, controller: NotifGroupController) {
55         // find the untruncated child count for a group and apply it to the controller
56         val count = untruncatedChildCounts[group]
57         checkNotNull(count) { "No untruncated child count for group: ${group.key}" }
58         controller.setUntruncatedChildCount(count)
59     }
60 }