1 /* <lambda>null2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.app.viewcapture 18 19 import android.content.Context 20 import android.content.pm.LauncherApps 21 import android.database.ContentObserver 22 import android.os.Handler 23 import android.os.Looper 24 import android.os.ParcelFileDescriptor 25 import android.os.Process 26 import android.provider.Settings 27 import android.util.Log 28 import android.window.IDumpCallback 29 import androidx.annotation.AnyThread 30 import androidx.annotation.VisibleForTesting 31 import java.util.concurrent.Executor 32 33 private val TAG = SettingsAwareViewCapture::class.java.simpleName 34 35 /** 36 * ViewCapture that listens to system updates and enables / disables attached ViewCapture 37 * WindowListeners accordingly. The Settings toggle is currently controlled by the Winscope 38 * developer tile in the System developer options. 39 */ 40 class SettingsAwareViewCapture 41 @VisibleForTesting 42 internal constructor(private val context: Context, executor: Executor) 43 : ViewCapture(DEFAULT_MEMORY_SIZE, DEFAULT_INIT_POOL_SIZE, executor) { 44 /** Dumps all the active view captures to the wm trace directory via LauncherAppService */ 45 private val mDumpCallback: IDumpCallback.Stub = object : IDumpCallback.Stub() { 46 override fun onDump(out: ParcelFileDescriptor) { 47 try { 48 ParcelFileDescriptor.AutoCloseOutputStream(out).use { os -> dumpTo(os, context) } 49 } catch (e: Exception) { 50 Log.e(TAG, "failed to dump data to wm trace", e) 51 } 52 } 53 } 54 55 init { 56 enableOrDisableWindowListeners() 57 context.contentResolver.registerContentObserver( 58 Settings.Global.getUriFor(VIEW_CAPTURE_ENABLED), 59 false, 60 object : ContentObserver(Handler()) { 61 override fun onChange(selfChange: Boolean) { 62 enableOrDisableWindowListeners() 63 } 64 }) 65 } 66 67 @AnyThread 68 private fun enableOrDisableWindowListeners() { 69 mBgExecutor.execute { 70 val isEnabled = Settings.Global.getInt(context.contentResolver, VIEW_CAPTURE_ENABLED, 71 0) != 0 72 MAIN_EXECUTOR.execute { 73 enableOrDisableWindowListeners(isEnabled) 74 } 75 val launcherApps = context.getSystemService(LauncherApps::class.java) 76 if (isEnabled) { 77 launcherApps?.registerDumpCallback(mDumpCallback) 78 } else { 79 launcherApps?.unRegisterDumpCallback(mDumpCallback) 80 } 81 } 82 } 83 84 companion object { 85 @VisibleForTesting internal const val VIEW_CAPTURE_ENABLED = "view_capture_enabled" 86 87 private var INSTANCE: ViewCapture? = null 88 89 @JvmStatic 90 fun getInstance(context: Context): ViewCapture = when { 91 INSTANCE != null -> INSTANCE!! 92 Looper.myLooper() == Looper.getMainLooper() -> SettingsAwareViewCapture( 93 context.applicationContext, 94 createAndStartNewLooperExecutor("SAViewCapture", 95 Process.THREAD_PRIORITY_FOREGROUND)).also { INSTANCE = it } 96 else -> try { 97 MAIN_EXECUTOR.submit { getInstance(context) }.get() 98 } catch (e: Exception) { 99 throw e 100 } 101 } 102 103 } 104 }