From 32b7a5589f1a59f88cddc1741451ac5459fe6776 Mon Sep 17 00:00:00 2001 From: Luke De Feo Date: Thu, 22 Sep 2022 04:26:18 -0700 Subject: [PATCH] Remove layout traversal Summary: The old full traversal was not needed as the partial traversal is a superset of the functionality and it was a pain to maintain both Reviewed By: lblasa Differential Revision: D39700216 fbshipit-source-id: 0b028995a59f7c5ea4318add8b0534851ca99547 --- .../uidebugger/core/LayoutTraversal.kt | 80 ------------------- .../uidebugger/core/NativeScanScheduler.kt | 12 ++- 2 files changed, 10 insertions(+), 82 deletions(-) delete mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/core/LayoutTraversal.kt diff --git a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/core/LayoutTraversal.kt b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/core/LayoutTraversal.kt deleted file mode 100644 index c9f6f860c..000000000 --- a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/core/LayoutTraversal.kt +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.flipper.plugins.uidebugger.core - -import android.util.Log -import com.facebook.flipper.plugins.uidebugger.LogTag -import com.facebook.flipper.plugins.uidebugger.descriptors.DescriptorRegister -import com.facebook.flipper.plugins.uidebugger.descriptors.NodeDescriptor -import com.facebook.flipper.plugins.uidebugger.model.Node - -class LayoutTraversal( - private val descriptorRegister: DescriptorRegister, - val root: ApplicationRef -) { - - @Suppress("unchecked_cast") - internal fun NodeDescriptor<*>.asAny(): NodeDescriptor = this as NodeDescriptor - - /** Traverses the native android hierarchy */ - fun traverse(): List { - - val result = mutableListOf() - val stack = mutableListOf() - stack.add(this.root) - - while (stack.isNotEmpty()) { - val node = stack.removeLast() - - try { - val descriptor = descriptorRegister.descriptorForClassUnsafe(node::class.java).asAny() - val children = descriptor.getChildren(node) - val activeChild = descriptor.getActiveChild(node) - - val childrenIds = mutableListOf() - children.forEach { child -> - val childDescriptor = - descriptorRegister.descriptorForClassUnsafe(child::class.java).asAny() - // It might make sense one day to remove id from the descriptor since its always the - // hash code - childrenIds.add(childDescriptor.getId(child)) - // If there is an active child then don't traverse it - if (activeChild == null) { - stack.add(child) - } - } - - var activeChildId: String? = null - if (activeChild != null) { - stack.add(activeChild) - activeChildId = - descriptorRegister.descriptorForClassUnsafe(activeChild.javaClass).getId(activeChild) - } - - val attributes = descriptor.getData(node) - - val bounds = descriptor.getBounds(node) - val tags = descriptor.getTags(node) - - result.add( - Node( - descriptor.getId(node), - descriptor.getName(node), - attributes, - bounds, - tags, - childrenIds, - activeChildId)) - } catch (exception: Exception) { - Log.e(LogTag, "Error while processing node ${node.javaClass.name} $node", exception) - } - } - - return result - } -} diff --git a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/core/NativeScanScheduler.kt b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/core/NativeScanScheduler.kt index 1b1d20e8c..a903f698d 100644 --- a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/core/NativeScanScheduler.kt +++ b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/core/NativeScanScheduler.kt @@ -13,6 +13,8 @@ import com.facebook.flipper.plugins.uidebugger.LogTag import com.facebook.flipper.plugins.uidebugger.model.NativeScanEvent import com.facebook.flipper.plugins.uidebugger.model.Node import com.facebook.flipper.plugins.uidebugger.model.PerfStatsEvent +import com.facebook.flipper.plugins.uidebugger.observers.PartialLayoutTraversal +import com.facebook.flipper.plugins.uidebugger.observers.TreeObserverFactory import com.facebook.flipper.plugins.uidebugger.scheduler.Scheduler import kotlinx.serialization.json.Json @@ -23,13 +25,19 @@ data class ScanResult( val nodes: List ) +/** This is used to stress test the ui debugger, should not be used in production */ class NativeScanScheduler(val context: Context) : Scheduler.Task { - private val traversal = LayoutTraversal(context.descriptorRegister, context.applicationRef) + /** + * when you supply no observers the traversal will never halt and will effectively scan the entire + * hierarchy + */ + private val emptyObserverFactory = TreeObserverFactory() + private val traversal = PartialLayoutTraversal(context.descriptorRegister, emptyObserverFactory) private var txId = 0L override fun execute(): ScanResult { val start = System.currentTimeMillis() - val nodes = traversal.traverse() + val (nodes) = traversal.traverse(context.applicationRef) val scanEnd = System.currentTimeMillis() Log.d(