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 deleted file mode 100644 index 6223aa511..000000000 --- a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/core/NativeScanScheduler.kt +++ /dev/null @@ -1,106 +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.os.Looper -import android.util.Log -import com.facebook.flipper.plugins.uidebugger.LogTag -import com.facebook.flipper.plugins.uidebugger.descriptors.ApplicationRefDescriptor -import com.facebook.flipper.plugins.uidebugger.descriptors.MetadataRegister -import com.facebook.flipper.plugins.uidebugger.model.MetadataUpdateEvent -import com.facebook.flipper.plugins.uidebugger.model.Node -import com.facebook.flipper.plugins.uidebugger.model.PerfStatsEvent -import com.facebook.flipper.plugins.uidebugger.model.SubtreeUpdateEvent -import com.facebook.flipper.plugins.uidebugger.observers.TreeObserverFactory -import com.facebook.flipper.plugins.uidebugger.scheduler.Scheduler -import com.facebook.flipper.plugins.uidebugger.traversal.PartialLayoutTraversal -import com.facebook.flipper.plugins.uidebugger.util.MaybeDeferred -import kotlinx.serialization.json.Json - -data class ScanResult( - val txId: Long, - val scanStart: Long, - val scanEnd: Long, - val nodes: List> -) - -const val observerType = "FullScan" - -/** This is used to stress test the ui debugger, should not be used in production */ -class NativeScanScheduler(val context: Context) : Scheduler.Task { - /** - * 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 = 100000L - - override fun execute(): ScanResult { - val start = System.currentTimeMillis() - val (nodes) = traversal.traverse(context.applicationRef) - val scanEnd = System.currentTimeMillis() - - Log.d( - LogTag, - "${Thread.currentThread().name}${Looper.myLooper()} produced: ${nodes.count()} nodes") - - return ScanResult(txId++, start, scanEnd, nodes) - } - - private fun sendMetadata() { - val metadata = MetadataRegister.getPendingMetadata() - if (metadata.size > 0) { - context.connectionRef.connection?.send( - MetadataUpdateEvent.name, - Json.encodeToString(MetadataUpdateEvent.serializer(), MetadataUpdateEvent(metadata))) - } - } - - private fun sendSubtreeUpdate(input: ScanResult) { - val nodes = input.nodes.map { it.value() } - val deferredComputationComplete = System.currentTimeMillis() - - val serialized = - Json.encodeToString( - SubtreeUpdateEvent.serializer(), - SubtreeUpdateEvent( - input.txId, - observerType, - ApplicationRefDescriptor.getId(context.applicationRef), - nodes)) - val serializationEnd = System.currentTimeMillis() - - context.connectionRef.connection?.send( - SubtreeUpdateEvent.name, - serialized, - ) - val socketEnd = System.currentTimeMillis() - - context.connectionRef.connection?.send( - PerfStatsEvent.name, - Json.encodeToString( - PerfStatsEvent.serializer(), - PerfStatsEvent( - input.txId, - observerType, - input.scanStart, - input.scanStart, - input.scanEnd, - input.scanEnd, - deferredComputationComplete, - serializationEnd, - socketEnd, - input.nodes.size))) - } - - override fun process(input: ScanResult) { - sendMetadata() - sendSubtreeUpdate(input) - } -} diff --git a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/scheduler/Scheduler.kt b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/scheduler/Scheduler.kt deleted file mode 100644 index 030cc9055..000000000 --- a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/scheduler/Scheduler.kt +++ /dev/null @@ -1,110 +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.scheduler - -import android.os.Handler -import android.os.HandlerThread -import android.os.Looper -import java.util.concurrent.locks.ReentrantLock -import kotlin.concurrent.withLock - -class Scheduler(val task: Task, val rate: Long = 500L) { - interface Task { - fun execute(): T? - fun process(input: T) - } - - private val mainLooper: Handler = Handler(Looper.getMainLooper()) - - private val mainRunnable = MainThreadRunnable() - private val backgroundRunnable = BackgroundThreadRunnable() - - private var backgroundHandler: HandlerThread? = null - private var backgroundLooper: Handler? = null - - private var isRunning = false - - private val lock = ReentrantLock() - private val condition = lock.newCondition() - private val queue = mutableListOf() - - fun start() { - backgroundHandler = HandlerThread("INSPECTOR_WORKER") - backgroundHandler?.let { handlerThread -> - handlerThread.start() - backgroundLooper = Handler(handlerThread.looper) - } - - isRunning = true - mainLooper.postDelayed(mainRunnable, rate) - } - - fun stop() { - backgroundLooper?.post(CancellationRunnable()) - } - - fun execute() { - if (Looper.myLooper() == Looper.getMainLooper()) { - mainRunnable.run() - } else { - mainLooper.post(mainRunnable) - } - } - - inner class MainThreadRunnable : Runnable { - override fun run() { - if (!isRunning) { - return - } - - try { - val output = task.execute() - output?.let { output -> - lock.withLock { - queue.add(output) - condition.signal() - } - } - } catch (e: Exception) {} - - mainLooper.postDelayed(mainRunnable, rate) - backgroundLooper?.post(backgroundRunnable) - } - } - - inner class BackgroundThreadRunnable : Runnable { - override fun run() { - if (!isRunning) { - return - } - try { - var input: T? - lock.withLock { - while (queue.isEmpty()) { - condition.await() - } - input = queue.removeFirst() - } - input?.let { input -> task.process(input) } - } catch (e: Exception) {} - } - } - - inner class CancellationRunnable : Runnable { - override fun run() { - backgroundHandler?.interrupt() - - mainLooper.removeCallbacks(mainRunnable) - backgroundLooper?.removeCallbacks(backgroundRunnable) - - backgroundHandler = null - - isRunning = false - } - } -}