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.util
18 
19 import android.util.IndentingPrintWriter
20 import android.view.View
21 import java.io.PrintWriter
22 import java.util.function.Consumer
23 
24 /**
25  * Run some code that will print to an [IndentingPrintWriter] that wraps the given [PrintWriter].
26  *
27  * If the given [PrintWriter] is an [IndentingPrintWriter], the block will be passed that same
28  * instance with [IndentingPrintWriter.increaseIndent] having been called, and calling
29  * [IndentingPrintWriter.decreaseIndent] after completion of the block, so the passed [PrintWriter]
30  * should not be used before the block completes.
31  */
32 inline fun PrintWriter.withIndenting(block: (IndentingPrintWriter) -> Unit) {
33     if (this is IndentingPrintWriter) {
34         this.withIncreasedIndent { block(this) }
35     } else {
36         block(IndentingPrintWriter(this))
37     }
38 }
39 
40 /**
41  * Run some code that will print to an [IndentingPrintWriter] that wraps the given [PrintWriter].
42  *
43  * If the given [PrintWriter] is an [IndentingPrintWriter], the block will be passed that same
44  * instance with [IndentingPrintWriter.increaseIndent] having been called, and calling
45  * [IndentingPrintWriter.decreaseIndent] after completion of the block, so the passed [PrintWriter]
46  * should not be used before the block completes.
47  */
48 fun PrintWriter.withIndenting(consumer: Consumer<IndentingPrintWriter>) {
49     if (this is IndentingPrintWriter) {
50         this.withIncreasedIndent { consumer.accept(this) }
51     } else {
52         consumer.accept(IndentingPrintWriter(this))
53     }
54 }
55 
56 /**
57  * Run some code inside a block, with [IndentingPrintWriter.increaseIndent] having been called on
58  * the given argument, and calling [IndentingPrintWriter.decreaseIndent] after completion.
59  */
60 inline fun IndentingPrintWriter.withIncreasedIndent(block: () -> Unit) {
61     this.increaseIndent()
62     try {
63         block()
64     } finally {
65         this.decreaseIndent()
66     }
67 }
68 
69 /** Return a readable string for the visibility */
70 fun visibilityString(@View.Visibility visibility: Int): String = when (visibility) {
71     View.GONE -> "gone"
72     View.VISIBLE -> "visible"
73     View.INVISIBLE -> "invisible"
74     else -> "unknown:$visibility"
75 }
76