1 /*
2  * Copyright (C) 2022 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
18 
19 import com.android.systemui.statusbar.notification.collection.listbuilder.pluggable.Pluggable
20 import com.android.systemui.statusbar.notification.collection.notifcollection.NotifDismissInterceptor
21 import com.android.systemui.statusbar.notification.collection.notifcollection.NotifLifetimeExtender
22 import com.android.systemui.util.asIndenting
23 import com.android.systemui.util.withIncreasedIndent
24 import java.io.PrintWriter
25 
26 class PipelineDumper(pw: PrintWriter) {
27     private val ipw = pw.asIndenting()
28 
printlnnull29     fun println(a: Any?) = ipw.println(a)
30 
31     fun dump(label: String, value: Any?) {
32         ipw.print("$label: ")
33         dump(value)
34     }
35 
dumpnull36     private fun dump(value: Any?) = when (value) {
37         null, is String, is Int -> ipw.println(value)
38         is Collection<*> -> dumpCollection(value)
39         else -> {
40             ipw.println(value.fullPipelineName)
41             (value as? PipelineDumpable)?.let {
42                 ipw.withIncreasedIndent { it.dumpPipeline(this) }
43             }
44         }
45     }
46 
dumpCollectionnull47     private fun dumpCollection(values: Collection<Any?>) {
48         ipw.println(values.size)
49         ipw.withIncreasedIndent { values.forEach { dump(it) } }
50     }
51 }
52 
53 private val Any.bareClassName: String get() {
54     val className = javaClass.name
55     val packagePrefixLength = javaClass.`package`?.name?.length?.plus(1) ?: 0
56     return className.substring(packagePrefixLength)
57 }
58 
59 private val Any.barePipelineName: String? get() = when (this) {
60     is NotifLifetimeExtender -> name
61     is NotifDismissInterceptor -> name
62     is Pluggable<*> -> name
63     else -> null
64 }
65 
66 private val Any.fullPipelineName: String get() =
<lambda>null67     barePipelineName?.let { "\"$it\" ($bareClassName)" } ?: bareClassName
68