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

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

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

parents a638fb69 934601ec
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
            )
        }
    }
}