From e8bd13a5930c28b6a9898a1ddf2223a08ee6fce2 Mon Sep 17 00:00:00 2001 From: Li Li Date: Fri, 29 Apr 2022 00:19:23 +0000 Subject: [PATCH] Revert "Fix stale View tracking in ShadeViewDiffer" This reverts commit 0b0791615edb53aee93aa585139828a33f423145. Reason for revert: b/230793120 Change-Id: I358fc8f0f7ee7c3c3047567f2cf460e33d1f538a --- .../collection/render/ShadeViewDiffer.kt | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/render/ShadeViewDiffer.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/render/ShadeViewDiffer.kt index 032e6784ae08c..386e2d31380c5 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/render/ShadeViewDiffer.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/render/ShadeViewDiffer.kt @@ -18,6 +18,7 @@ package com.android.systemui.statusbar.notification.collection.render import android.annotation.MainThread import android.view.View +import com.android.systemui.util.kotlin.transform import com.android.systemui.util.traceSection /** @@ -40,6 +41,7 @@ class ShadeViewDiffer( ) { private val rootNode = ShadeNode(rootController) private val nodes = mutableMapOf(rootController to rootNode) + private val views = mutableMapOf() /** * Adds and removes views from the root (and its children) until their structure matches the @@ -64,25 +66,26 @@ class ShadeViewDiffer( * * For debugging purposes. */ - fun getViewLabel(view: View): String = - nodes.values.firstOrNull { node -> node.view === view }?.label ?: view.toString() + fun getViewLabel(view: View): String = views[view]?.label ?: view.toString() - private fun detachChildren(parentNode: ShadeNode, specMap: Map) { - val views = nodes.values.asSequence().map { node -> node.view to node }.toMap() - fun detachRecursively(parentNode: ShadeNode, specMap: Map) { - val parentSpec = specMap[parentNode.controller] - for (i in parentNode.getChildCount() - 1 downTo 0) { - val childView = parentNode.getChildAt(i) - views[childView]?.let { childNode -> - val childSpec = specMap[childNode.controller] - maybeDetachChild(parentNode, parentSpec, childNode, childSpec) - if (childNode.controller.getChildCount() > 0) { - detachRecursively(childNode, specMap) - } + private fun detachChildren( + parentNode: ShadeNode, + specMap: Map + ) { + val parentSpec = specMap[parentNode.controller] + + for (i in parentNode.getChildCount() - 1 downTo 0) { + val childView = parentNode.getChildAt(i) + views[childView]?.let { childNode -> + val childSpec = specMap[childNode.controller] + + maybeDetachChild(parentNode, parentSpec, childNode, childSpec) + + if (childNode.controller.getChildCount() > 0) { + detachChildren(childNode, specMap) } } } - detachRecursively(parentNode, specMap) } private fun maybeDetachChild( @@ -91,13 +94,14 @@ class ShadeViewDiffer( childNode: ShadeNode, childSpec: NodeSpec? ) { - val newParentNode = childSpec?.parent?.let { getNode(it) } + val newParentNode = transform(childSpec?.parent) { getNode(it) } if (newParentNode != parentNode) { val childCompletelyRemoved = newParentNode == null if (childCompletelyRemoved) { nodes.remove(childNode.controller) + views.remove(childNode.controller.view) } logger.logDetachingChild( @@ -111,7 +115,10 @@ class ShadeViewDiffer( } } - private fun attachChildren(parentNode: ShadeNode, specMap: Map) { + private fun attachChildren( + parentNode: ShadeNode, + specMap: Map + ) { val parentSpec = checkNotNull(specMap[parentNode.controller]) for ((index, childSpec) in parentSpec.children.withIndex()) { @@ -153,6 +160,7 @@ class ShadeViewDiffer( if (node == null) { node = ShadeNode(spec.controller) nodes[node.controller] = node + views[node.view] = node } return node } @@ -186,9 +194,10 @@ class ShadeViewDiffer( private class DuplicateNodeException(message: String) : RuntimeException(message) -private class ShadeNode(val controller: NodeController) { - val view: View - get() = controller.view +private class ShadeNode( + val controller: NodeController +) { + val view = controller.view var parent: ShadeNode? = null