|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.devsupport |
| 9 | + |
| 10 | +import android.app.Dialog |
| 11 | +import android.content.Context |
| 12 | +import android.graphics.Color |
| 13 | +import android.graphics.Typeface |
| 14 | +import android.graphics.drawable.ColorDrawable |
| 15 | +import android.graphics.drawable.GradientDrawable |
| 16 | +import android.view.Gravity |
| 17 | +import android.view.Window |
| 18 | +import android.view.WindowManager |
| 19 | +import android.widget.LinearLayout |
| 20 | +import android.widget.TextView |
| 21 | +import androidx.core.util.Supplier |
| 22 | +import androidx.core.view.ViewCompat |
| 23 | +import androidx.core.view.WindowInsetsCompat |
| 24 | +import com.facebook.react.R |
| 25 | +import com.facebook.react.bridge.UiThreadUtil |
| 26 | +import com.facebook.react.devsupport.interfaces.PerfMonitorOverlayManager |
| 27 | +import com.facebook.react.uimanager.DisplayMetricsHolder |
| 28 | +import com.facebook.react.uimanager.PixelUtil |
| 29 | +import java.util.Locale |
| 30 | + |
| 31 | +internal class PerfMonitorOverlayViewManager( |
| 32 | + private val contextSupplier: Supplier<Context?>, |
| 33 | + private val onRequestAnalyzeTrace: () -> Unit |
| 34 | +) : PerfMonitorOverlayManager { |
| 35 | + private var initialized: Boolean = false |
| 36 | + private var enabled: Boolean = false |
| 37 | + private var hasInteractionData: Boolean = false |
| 38 | + private var interactionDialog: Dialog? = null |
| 39 | + private var buttonDialog: Dialog? = null |
| 40 | + private var interactionNameLabel: TextView? = null |
| 41 | + private var durationLabel: TextView? = null |
| 42 | + |
| 43 | + override fun enable() { |
| 44 | + UiThreadUtil.runOnUiThread { |
| 45 | + enabled = true |
| 46 | + if (hasInteractionData) { |
| 47 | + showOverlay() |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + override fun disable() { |
| 53 | + UiThreadUtil.runOnUiThread { |
| 54 | + enabled = false |
| 55 | + hideOverlay() |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + override fun reset() { |
| 60 | + UiThreadUtil.runOnUiThread { |
| 61 | + hasInteractionData = false |
| 62 | + hideOverlay() |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + override fun update(interactionName: String, durationMs: Int) { |
| 67 | + UiThreadUtil.runOnUiThread { |
| 68 | + ensureInitialized() |
| 69 | + interactionNameLabel?.text = interactionName |
| 70 | + durationLabel?.text = String.format(Locale.US, "%d ms", durationMs) |
| 71 | + hasInteractionData = true |
| 72 | + if (enabled) { |
| 73 | + showOverlay() |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private fun ensureInitialized() { |
| 79 | + if (initialized) { |
| 80 | + return |
| 81 | + } |
| 82 | + val context = contextSupplier.get() ?: return |
| 83 | + DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(context) |
| 84 | + createDialog(context) |
| 85 | + createButton(context) |
| 86 | + initialized = true |
| 87 | + } |
| 88 | + |
| 89 | + private fun showOverlay() { |
| 90 | + interactionDialog?.show() |
| 91 | + buttonDialog?.show() |
| 92 | + } |
| 93 | + |
| 94 | + private fun hideOverlay() { |
| 95 | + interactionDialog?.hide() |
| 96 | + buttonDialog?.hide() |
| 97 | + } |
| 98 | + |
| 99 | + private fun createDialog(context: Context) { |
| 100 | + val containerLayout = createInnerLayout(context) |
| 101 | + interactionNameLabel = |
| 102 | + TextView(context).apply { |
| 103 | + textSize = TEXT_SIZE_PRIMARY |
| 104 | + setTextColor(Color.WHITE) |
| 105 | + typeface = TYPEFACE_BOLD |
| 106 | + } |
| 107 | + durationLabel = |
| 108 | + TextView(context).apply { |
| 109 | + textSize = TEXT_SIZE_PRIMARY |
| 110 | + setTextColor(COLOR_TEXT_GREEN) |
| 111 | + typeface = TYPEFACE_BOLD |
| 112 | + } |
| 113 | + containerLayout.addView(interactionNameLabel) |
| 114 | + containerLayout.addView(durationLabel) |
| 115 | + |
| 116 | + val dialog = |
| 117 | + createAnchoredDialog(context, dpToPx(140f), dpToPx(16f)).apply { |
| 118 | + setContentView(containerLayout) |
| 119 | + } |
| 120 | + dialog.window?.apply { |
| 121 | + attributes = |
| 122 | + attributes?.apply { |
| 123 | + flags = |
| 124 | + flags or |
| 125 | + WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE or |
| 126 | + WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + this.interactionDialog = dialog |
| 131 | + } |
| 132 | + |
| 133 | + private fun createButton(context: Context) { |
| 134 | + val buttonInner = createInnerLayout(context) |
| 135 | + buttonInner.addView( |
| 136 | + TextView(context).apply { |
| 137 | + text = "Analyze" |
| 138 | + textSize = TEXT_SIZE_PRIMARY |
| 139 | + setTextColor(Color.WHITE) |
| 140 | + typeface = TYPEFACE_BOLD |
| 141 | + }) |
| 142 | + buttonInner.addView( |
| 143 | + TextView(context).apply { |
| 144 | + text = "cmd + A" |
| 145 | + textSize = TEXT_SIZE_ACCESSORY |
| 146 | + setTextColor(Color.WHITE) |
| 147 | + alpha = 0.7f |
| 148 | + typeface = TYPEFACE_BOLD |
| 149 | + }) |
| 150 | + val buttonView = |
| 151 | + LinearLayout(context).apply { |
| 152 | + orientation = LinearLayout.VERTICAL |
| 153 | + setPadding( |
| 154 | + dpToPx(8f).toInt(), dpToPx(16f).toInt(), dpToPx(16f).toInt(), dpToPx(8f).toInt()) |
| 155 | + addView(buttonInner) |
| 156 | + setOnClickListener { onRequestAnalyzeTrace() } |
| 157 | + } |
| 158 | + val dialog = |
| 159 | + createAnchoredDialog(context, dpToPx(0f), dpToPx(0f)).apply { setContentView(buttonView) } |
| 160 | + dialog.window?.apply { |
| 161 | + attributes = |
| 162 | + attributes?.apply { flags = flags or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE } |
| 163 | + } |
| 164 | + |
| 165 | + this.buttonDialog = dialog |
| 166 | + } |
| 167 | + |
| 168 | + private fun createAnchoredDialog(context: Context, offsetX: Float, offsetY: Float): Dialog { |
| 169 | + val dialog = |
| 170 | + Dialog(context, R.style.NoAnimationDialog).apply { |
| 171 | + requestWindowFeature(Window.FEATURE_NO_TITLE) |
| 172 | + window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) |
| 173 | + window?.setDimAmount(0f) |
| 174 | + setCancelable(false) |
| 175 | + } |
| 176 | + dialog.window?.apply { |
| 177 | + attributes = |
| 178 | + attributes?.apply { |
| 179 | + width = WindowManager.LayoutParams.WRAP_CONTENT |
| 180 | + height = WindowManager.LayoutParams.WRAP_CONTENT |
| 181 | + gravity = Gravity.TOP or Gravity.END |
| 182 | + x = offsetX.toInt() |
| 183 | + y = offsetY.toInt() |
| 184 | + } |
| 185 | + } |
| 186 | + dialog.window?.decorView?.let { decorView -> |
| 187 | + ViewCompat.setOnApplyWindowInsetsListener(decorView) { view, windowInsets -> |
| 188 | + val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) |
| 189 | + val layoutParams = (view.layoutParams as WindowManager.LayoutParams) |
| 190 | + layoutParams.y = insets.top + offsetY.toInt() |
| 191 | + dialog.window?.attributes = layoutParams |
| 192 | + WindowInsetsCompat.CONSUMED |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + return dialog |
| 197 | + } |
| 198 | + |
| 199 | + private fun createInnerLayout(context: Context): LinearLayout { |
| 200 | + return LinearLayout(context).apply { |
| 201 | + orientation = LinearLayout.HORIZONTAL |
| 202 | + gravity = Gravity.CENTER_VERTICAL |
| 203 | + val paddingHorizontal = dpToPx(14f).toInt() |
| 204 | + val paddingVertical = dpToPx(8f).toInt() |
| 205 | + setPadding(paddingHorizontal, paddingVertical, paddingHorizontal, paddingVertical) |
| 206 | + layoutParams = |
| 207 | + LinearLayout.LayoutParams( |
| 208 | + LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT) |
| 209 | + background = |
| 210 | + GradientDrawable().apply { |
| 211 | + shape = GradientDrawable.RECTANGLE |
| 212 | + setColor(Color.BLACK) |
| 213 | + cornerRadius = dpToPx(14.5f) |
| 214 | + alpha = (0.8 * 255).toInt() |
| 215 | + setStroke(dpToPx(1f).toInt(), COLOR_OVERLAY_BORDER) |
| 216 | + } |
| 217 | + showDividers = LinearLayout.SHOW_DIVIDER_MIDDLE |
| 218 | + dividerDrawable = |
| 219 | + object : ColorDrawable(Color.TRANSPARENT) { |
| 220 | + override fun getIntrinsicWidth(): Int = dpToPx(8f).toInt() |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + private fun dpToPx(dp: Float): Float = PixelUtil.toPixelFromDIP(dp) |
| 226 | + |
| 227 | + companion object { |
| 228 | + private val COLOR_TEXT_GREEN = Color.parseColor("#4AEB2F") |
| 229 | + private val COLOR_OVERLAY_BORDER = Color.parseColor("#6C6C6C") |
| 230 | + private val TEXT_SIZE_PRIMARY = 13f |
| 231 | + private val TEXT_SIZE_ACCESSORY = 9f |
| 232 | + private val TYPEFACE_BOLD = Typeface.create("sans-serif", Typeface.BOLD) |
| 233 | + } |
| 234 | +} |
0 commit comments