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

Commit df40b52b authored by Kean Mariotti's avatar Kean Mariotti Committed by Android (Google) Code Review
Browse files

Merge "vc tracing: refactoring prior perfetto migration" into main

parents 59ac9c8b 8d45c531
Loading
Loading
Loading
Loading
+3 −24
Original line number Diff line number Diff line
@@ -20,9 +20,7 @@ import android.content.Context
import android.content.pm.LauncherApps
import android.database.ContentObserver
import android.os.Handler
import android.os.Looper
import android.os.ParcelFileDescriptor
import android.os.Process
import android.provider.Settings
import android.util.Log
import android.window.IDumpCallback
@@ -37,10 +35,9 @@ private val TAG = SettingsAwareViewCapture::class.java.simpleName
 * WindowListeners accordingly. The Settings toggle is currently controlled by the Winscope
 * developer tile in the System developer options.
 */
class SettingsAwareViewCapture
@VisibleForTesting
internal constructor(private val context: Context, executor: Executor)
    : ViewCapture(DEFAULT_MEMORY_SIZE, DEFAULT_INIT_POOL_SIZE, executor) {
internal class SettingsAwareViewCapture
internal constructor(private val context: Context, executor: Executor) :
    ViewCapture(DEFAULT_MEMORY_SIZE, DEFAULT_INIT_POOL_SIZE, executor) {
    /** Dumps all the active view captures to the wm trace directory via LauncherAppService */
    private val mDumpCallback: IDumpCallback.Stub = object : IDumpCallback.Stub() {
        override fun onDump(out: ParcelFileDescriptor) {
@@ -83,23 +80,5 @@ internal constructor(private val context: Context, executor: Executor)

    companion object {
        @VisibleForTesting internal const val VIEW_CAPTURE_ENABLED = "view_capture_enabled"

        private var INSTANCE: ViewCapture? = null

        @JvmStatic
        fun getInstance(context: Context): ViewCapture = when {
            INSTANCE != null -> INSTANCE!!
            !android.os.Build.IS_DEBUGGABLE -> NoOpViewCapture()
            Looper.myLooper() == Looper.getMainLooper() -> SettingsAwareViewCapture(
                    context.applicationContext,
                    createAndStartNewLooperExecutor("SAViewCapture",
                    Process.THREAD_PRIORITY_FOREGROUND)).also { INSTANCE = it }
            else -> try {
                MAIN_EXECUTOR.submit { getInstance(context) }.get()
            } catch (e: Exception) {
                throw e
            }
        }

    }
}
 No newline at end of file
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.app.viewcapture

import android.content.Context
import android.os.Looper
import android.os.Process
import android.util.Log

/**
 * Factory to create polymorphic instances of ViewCapture according to build configurations and
 * flags.
 */
class ViewCaptureFactory {
    companion object {
        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!!
            }

            return when {
                !android.os.Build.IS_DEBUGGABLE -> {
                    Log.i(TAG, "instantiating ${NoOpViewCapture::class.java.simpleName}")
                    NoOpViewCapture()
                }
                else -> {
                    Log.i(TAG, "instantiating ${SettingsAwareViewCapture::class.java.simpleName}")
                    SettingsAwareViewCapture(
                        context.applicationContext,
                        ViewCapture.createAndStartNewLooperExecutor(
                            "SAViewCapture",
                            Process.THREAD_PRIORITY_FOREGROUND
                        )
                    )
                }
            }.also { instance = it }
        }
    }
}
+0 −8
Original line number Diff line number Diff line
@@ -97,12 +97,4 @@ class SettingsAwareViewCaptureTest {
            }
        }
    }

    @Test
    fun getInstance_calledTwiceInARow_returnsSameObject() {
        assertEquals(
            SettingsAwareViewCapture.getInstance(context).hashCode(),
            SettingsAwareViewCapture.getInstance(context).hashCode()
        )
    }
}