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
23 /**
24 * Get an [IndentingPrintWriter] which either is or wraps the given [PrintWriter].
25 *
26 * The original [PrintWriter] should not be used until the returned [IndentingPrintWriter] is no
27 * longer being used, to avoid inconsistent writing.
28 */
asIndentingnull29 fun PrintWriter.asIndenting(): IndentingPrintWriter =
30 (this as? IndentingPrintWriter) ?: IndentingPrintWriter(this)
31
32 /**
33 * Run some code inside a block, with [IndentingPrintWriter.increaseIndent] having been called on
34 * the given argument, and calling [IndentingPrintWriter.decreaseIndent] after completion.
35 */
36 inline fun IndentingPrintWriter.withIncreasedIndent(block: () -> Unit) {
37 this.increaseIndent()
38 try {
39 block()
40 } finally {
41 this.decreaseIndent()
42 }
43 }
44
45 /**
46 * Run some code inside a block, with [IndentingPrintWriter.increaseIndent] having been called on
47 * the given argument, and calling [IndentingPrintWriter.decreaseIndent] after completion.
48 */
withIncreasedIndentnull49 fun IndentingPrintWriter.withIncreasedIndent(runnable: Runnable) {
50 this.increaseIndent()
51 try {
52 runnable.run()
53 } finally {
54 this.decreaseIndent()
55 }
56 }
57
58 /** Print a line which is '$label=$value' */
printlnnull59 fun IndentingPrintWriter.println(label: String, value: Any) =
60 append(label).append('=').println(value)
61
62 /** Return a readable string for the visibility */
63 fun visibilityString(@View.Visibility visibility: Int): String = when (visibility) {
64 View.GONE -> "gone"
65 View.VISIBLE -> "visible"
66 View.INVISIBLE -> "invisible"
67 else -> "unknown:$visibility"
68 }
69