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

Commit 324afeef authored by Archisha Baranwal's avatar Archisha Baranwal Committed by Android (Google) Code Review
Browse files

Merge "Revert^2 "Remove main thread necessity for getting a ViewCapture...

Merge "Revert^2 "Remove main thread necessity for getting a ViewCapture instance and make"" into main
parents ef7d6b41 db5671b6
Loading
Loading
Loading
Loading
+50 −52
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
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
@@ -27,21 +26,12 @@ import android.view.WindowManager
 * Factory to create polymorphic instances of ViewCapture according to build configurations and
 * flags.
 */
class ViewCaptureFactory {
    companion object {
object ViewCaptureFactory {
    private val TAG = ViewCaptureFactory::class.java.simpleName
        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 val instance: ViewCapture by lazy { createInstance() }
    private lateinit var appContext: Context

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

    /** Returns an instance of [ViewCapture]. */
    @JvmStatic
    fun getInstance(context: Context): ViewCapture {
        if (!this::appContext.isInitialized) {
            synchronized(this) { appContext = context.applicationContext }
        }
        return instance
    }

    /** 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,
        )
    }
}
}