From 6adf1d666fd04fc6956a581119794af4d3e3104c Mon Sep 17 00:00:00 2001 From: Lorenzo Blasa Date: Fri, 19 Aug 2022 08:47:58 -0700 Subject: [PATCH] Descriptors Summary: Introducing descriptors. Taken from the existing inspector, refreshed with a few things from Stetho, and translated to Kotlin. Note: doesn't use FlipperObject or FlipperArray. This is a very rough draft for them all, but can be used as basis for experimentation. Reviewed By: LukeDefeo Differential Revision: D38860763 fbshipit-source-id: 9f6bf4ad0e61fc40b0a773dfb8bfa80b1f397b3a --- .../descriptors/AbstractChainedDescriptor.kt | 73 ++++++ .../descriptors/ActivityDescriptor.kt | 47 ++++ .../descriptors/ApplicationDescriptor.kt | 52 ++++ .../descriptors/ButtonDescriptor.kt | 26 ++ .../descriptors/ChainedDescriptor.kt | 42 ++++ .../uidebugger/descriptors/Descriptor.kt | 56 +++++ .../descriptors/DescriptorRegister.kt | 67 +++++ .../uidebugger/descriptors/NodeDescriptor.kt | 34 +++ .../descriptors/ObjectDescriptor.kt | 24 ++ .../descriptors/TextViewDescriptor.kt | 26 ++ .../uidebugger/descriptors/ViewDescriptor.kt | 237 ++++++++++++++++++ .../descriptors/ViewGroupDescriptor.kt | 82 ++++++ .../descriptors/WindowDescriptor.kt | 28 +++ 13 files changed, 794 insertions(+) create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/AbstractChainedDescriptor.kt create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ActivityDescriptor.kt create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ApplicationDescriptor.kt create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ButtonDescriptor.kt create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ChainedDescriptor.kt create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/Descriptor.kt create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/DescriptorRegister.kt create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/NodeDescriptor.kt create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ObjectDescriptor.kt create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/TextViewDescriptor.kt create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ViewDescriptor.kt create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ViewGroupDescriptor.kt create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/WindowDescriptor.kt diff --git a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/AbstractChainedDescriptor.kt b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/AbstractChainedDescriptor.kt new file mode 100644 index 00000000000..cb1ad1bf346 --- /dev/null +++ b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/AbstractChainedDescriptor.kt @@ -0,0 +1,73 @@ +/* + * 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.descriptors + +/** + * This class derives from Descriptor and provides a canonical implementation of ChainedDescriptor}. + */ +abstract class AbstractChainedDescriptor : Descriptor(), ChainedDescriptor { + private var mSuper: Descriptor? = null + + override fun setSuper(superDescriptor: Descriptor) { + if (superDescriptor !== mSuper) { + check(mSuper == null) + mSuper = superDescriptor + } + } + + fun getSuper(): Descriptor? { + return mSuper + } + + /** Initialize a descriptor. */ + override fun init() { + mSuper?.init() + onInit() + } + + open fun onInit() {} + + /** + * A globally unique ID used to identify a node in a hierarchy. If your node does not have a + * globally unique ID it is fine to rely on [System.identityHashCode]. + */ + override fun getId(node: T): String? { + return onGetId(node) + } + + abstract fun onGetId(node: T): String + + /** + * The name used to identify this node in the inspector. Does not need to be unique. A good + * default is to use the class name of the node. + */ + override fun getName(node: T): String? { + return onGetName(node) + } + + abstract fun onGetName(node: T): String + + /** The children this node exposes in the inspector. */ + override fun getChildren(node: T, children: MutableList) { + mSuper?.getChildren(node, children) + onGetChildren(node, children) + } + + open fun onGetChildren(node: T, children: MutableList) {} + + /** + * Get the data to show for this node in the sidebar of the inspector. The object will be showen + * in order and with a header matching the given name. + */ + override fun getData(node: T, builder: MutableMap) { + mSuper?.getData(node, builder) + onGetData(node, builder) + } + + open fun onGetData(node: T, builder: MutableMap) {} +} diff --git a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ActivityDescriptor.kt b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ActivityDescriptor.kt new file mode 100644 index 00000000000..e3accf52c1d --- /dev/null +++ b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ActivityDescriptor.kt @@ -0,0 +1,47 @@ +/* + * 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.descriptors + +import android.app.Activity +import com.facebook.flipper.plugins.uidebugger.stetho.FragmentCompat + +class ActivityDescriptor : AbstractChainedDescriptor() { + override fun onInit() {} + + override fun onGetId(activity: Activity): String { + return Integer.toString(System.identityHashCode(activity)) + } + + override fun onGetName(activity: Activity): String { + return activity.javaClass.simpleName + } + + override fun onGetChildren(activity: Activity, children: MutableList) { + activity.window?.let { window -> children.add(activity.window) } + + var fragments = getFragments(FragmentCompat.supportInstance, activity) + for (fragment in fragments) { + children.add(fragment) + } + + fragments = getFragments(FragmentCompat.frameworkInstance, activity) + for (fragment in fragments) { + children.add(fragment) + } + } + + override fun onGetData(activity: Activity, builder: MutableMap) {} + + private fun getFragments(compat: FragmentCompat<*, *, *, *>?, activity: Activity): List { + if (compat == null) { + return emptyList() + } + + return compat?.getFragments(activity) + } +} diff --git a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ApplicationDescriptor.kt b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ApplicationDescriptor.kt new file mode 100644 index 00000000000..b21d806b1fa --- /dev/null +++ b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ApplicationDescriptor.kt @@ -0,0 +1,52 @@ +/* + * 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.descriptors + +import android.app.Activity +import com.facebook.flipper.plugins.uidebugger.core.ApplicationRef +import com.facebook.flipper.plugins.uidebugger.core.RootViewResolver + +class ApplicationDescriptor : AbstractChainedDescriptor() { + val rootResolver = RootViewResolver() + + override fun onInit() {} + + override fun onGetId(applicationRef: ApplicationRef): String { + return applicationRef.application.packageName + } + + override fun onGetName(applicationRef: ApplicationRef): String { + val applicationInfo = applicationRef.application.getApplicationInfo() + val stringId = applicationInfo.labelRes + return if (stringId == 0) applicationInfo.nonLocalizedLabel.toString() + else applicationRef.application.getString(stringId) + } + + override fun onGetChildren(applicationRef: ApplicationRef, children: MutableList) { + val activeRoots = rootResolver.listActiveRootViews() + + activeRoots?.let { roots -> + for (root: RootViewResolver.RootView in roots) { + var added = false + for (activity: Activity in applicationRef.activitiesStack) { + if (activity.window.decorView == root.view) { + children.add(activity) + added = true + + break + } + } + if (!added) { + children.add(root.view) + } + } + } + } + + override fun onGetData(applicationRef: ApplicationRef, builder: MutableMap) {} +} diff --git a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ButtonDescriptor.kt b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ButtonDescriptor.kt new file mode 100644 index 00000000000..f87b4e51583 --- /dev/null +++ b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/descriptors/ButtonDescriptor.kt @@ -0,0 +1,26 @@ +/* + * 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.descriptors + +import android.widget.Button + +class ButtonDescriptor : AbstractChainedDescriptor