1 /*
2 * Copyright (C) 2020 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.render
18
19 import android.view.View
20 import java.lang.RuntimeException
21 import java.lang.StringBuilder
22
23 /**
24 * A controller that represents a single unit of addable/removable view(s) in the notification
25 * shade. Some nodes are just a single view (such as a header), while some might involve many views
26 * (such as a notification row).
27 *
28 * It's possible for nodes to support having child nodes (for example, some notification rows
29 * contain other notification rows). If so, they must implement all of the child-related methods
30 * below.
31 */
32 interface NodeController {
33 /** A string that uniquely(ish) represents the node in the tree. Used for debugging. */
34 val nodeLabel: String
35
36 val view: View
37
getChildAtnull38 fun getChildAt(index: Int): View? {
39 throw RuntimeException("Not supported")
40 }
41
getChildCountnull42 fun getChildCount(): Int = 0
43
44 fun addChildAt(child: NodeController, index: Int) {
45 throw RuntimeException("Not supported")
46 }
47
moveChildTonull48 fun moveChildTo(child: NodeController, index: Int) {
49 throw RuntimeException("Not supported")
50 }
51
removeChildnull52 fun removeChild(child: NodeController, isTransfer: Boolean) {
53 throw RuntimeException("Not supported")
54 }
55 }
56
57 /**
58 * Used to specify the tree of [NodeController]s that currently make up the shade.
59 */
60 interface NodeSpec {
61 val parent: NodeSpec?
62 val controller: NodeController
63 val children: List<NodeSpec>
64 }
65
66 class NodeSpecImpl(
67 override val parent: NodeSpec?,
68 override val controller: NodeController
69 ) : NodeSpec {
70 override val children = mutableListOf<NodeSpec>()
71 }
72
73 /**
74 * Converts a tree spec to human-readable string, for dumping purposes.
75 */
treeSpecToStrnull76 fun treeSpecToStr(tree: NodeSpec): String {
77 return StringBuilder().also { treeSpecToStrHelper(tree, it, "") }.toString()
78 }
79
treeSpecToStrHelpernull80 private fun treeSpecToStrHelper(tree: NodeSpec, sb: StringBuilder, indent: String) {
81 sb.append("${indent}ns{${tree.controller.nodeLabel}")
82 if (tree.children.isNotEmpty()) {
83 val childIndent = "$indent "
84 for (child in tree.children) {
85 treeSpecToStrHelper(child, sb, childIndent)
86 }
87 }
88 }
89