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

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

Merge "Remove main thread necessity for getting a ViewCapture instance and...

Merge "Remove main thread necessity for getting a ViewCapture instance and make call to getInstance method thread safe." into main
parents 41f9fbd8 ebfbc916
Loading
Loading
Loading
Loading
+53 −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,50 @@ 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 {
        synchronized(this) {
            if (this::appContext.isInitialized && appContext != context.applicationContext) {
                throw IllegalStateException("appContext is already set to a different value")
            }
            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,
        )
    }
}
}