Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 934601ec authored by Liana Kazanova (xWF)'s avatar Liana Kazanova (xWF) Committed by Android (Google) Code Review
Browse files

Revert "Remove main thread necessity for getting a ViewCapture instance and make"

This reverts commit ebfbc916.

Reason for revert:DroidMonitor: Potential culprit for http://b/372910511 - verifying through ABTD before revert submission. This is part of the standard investigation process, and does not mean your CL will be reverted.

Change-Id: I7e5523f95f50919d2a572bad4c1d2821c1622191
parent ebfbc916
Loading
Loading
Loading
Loading
+52 −53
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.app.viewcapture

import android.content.Context
import android.os.Looper
import android.os.Process
import android.tracing.Flags
import android.util.Log
@@ -26,12 +27,21 @@ import android.view.WindowManager
 * Factory to create polymorphic instances of ViewCapture according to build configurations and
 * flags.
 */
object ViewCaptureFactory {
class ViewCaptureFactory {
    companion object {
        private val TAG = ViewCaptureFactory::class.java.simpleName
    private val instance: ViewCapture by lazy { createInstance() }
    private lateinit var appContext: Context
        private var instance: ViewCapture? = null

        @JvmStatic
        fun getInstance(context: Context): ViewCapture {
            if (Looper.myLooper() != Looper.getMainLooper()) {
                return ViewCapture.MAIN_EXECUTOR.submit { getInstance(context) }.get()
            }

            if (instance != null) {
                return instance!!
            }

    private fun createInstance(): ViewCapture {
            return when {
                !android.os.Build.IS_DEBUGGABLE -> {
                    Log.i(TAG, "instantiating ${NoOpViewCapture::class.java.simpleName}")
@@ -40,50 +50,39 @@ object ViewCaptureFactory {
                !Flags.perfettoViewCaptureTracing() -> {
                    Log.i(TAG, "instantiating ${SettingsAwareViewCapture::class.java.simpleName}")
                    SettingsAwareViewCapture(
                    appContext,
                        context.applicationContext,
                        ViewCapture.createAndStartNewLooperExecutor(
                            "SAViewCapture",
                        Process.THREAD_PRIORITY_FOREGROUND,
                    ),
                            Process.THREAD_PRIORITY_FOREGROUND
                        )
                    )
                }
                else -> {
                    Log.i(TAG, "instantiating ${PerfettoViewCapture::class.java.simpleName}")
                    PerfettoViewCapture(
                    appContext,
                        context.applicationContext,
                        ViewCapture.createAndStartNewLooperExecutor(
                            "PerfettoViewCapture",
                        Process.THREAD_PRIORITY_FOREGROUND,
                    ),
                            Process.THREAD_PRIORITY_FOREGROUND
                        )
                    )
                }
        }
    }

    /** Returns an instance of [ViewCapture]. */
    @JvmStatic
    fun getInstance(context: Context): ViewCapture {
        synchronized(this) {
            if (this::appContext.isInitialized && appContext != context.applicationContext) {
                throw IllegalStateException("appContext is already set to a different value")
            }
            appContext = context.applicationContext
        }
        return instance
            }.also { instance = it }
        }

        /** Returns an instance of [ViewCaptureAwareWindowManager]. */
        @JvmStatic
        fun getViewCaptureAwareWindowManagerInstance(
            context: Context,
        isViewCaptureTracingEnabled: Boolean,
            isViewCaptureTracingEnabled: Boolean
        ): ViewCaptureAwareWindowManager {
            val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            val lazyViewCapture = lazy { getInstance(context) }
            return ViewCaptureAwareWindowManager(
                windowManager,
                lazyViewCapture,
            isViewCaptureTracingEnabled,
                isViewCaptureTracingEnabled
            )
        }
    }
}