1 /* 2 * 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.systemui.screenshot 18 19 import android.content.pm.PackageManager 20 import android.content.pm.PackageManager.ComponentInfoFlags 21 import android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS 22 import android.view.Display 23 import android.view.IWindowManager 24 import android.view.ViewGroup 25 import android.view.WindowManager 26 import android.widget.TextView 27 import com.android.systemui.R 28 import javax.inject.Inject 29 30 class ScreenshotDetectionController 31 @Inject 32 constructor( 33 private val windowManager: IWindowManager, 34 private val packageManager: PackageManager, 35 ) { 36 /** 37 * Notify potentially listening apps of the screenshot. Return a list of the names of the apps 38 * notified. 39 */ 40 fun maybeNotifyOfScreenshot(data: ScreenshotData): List<CharSequence> { 41 // No notification for screenshots from overview. 42 if (data.source == WindowManager.ScreenshotSource.SCREENSHOT_OVERVIEW) return listOf() 43 44 // Notify listeners, retrieve a list of listening components. 45 val components = windowManager.notifyScreenshotListeners(Display.DEFAULT_DISPLAY) 46 47 // Convert component names to app names. 48 return components.map { 49 packageManager 50 .getActivityInfo(it, ComponentInfoFlags.of(MATCH_DISABLED_COMPONENTS.toLong())) 51 .loadLabel(packageManager) 52 } 53 } 54 55 fun populateView(view: ViewGroup, appNames: List<CharSequence>) { 56 assert(appNames.isNotEmpty()) 57 58 val textView: TextView = view.requireViewById(R.id.screenshot_detection_notice_text) 59 if (appNames.size == 1) { 60 textView.text = 61 view.resources.getString(R.string.screenshot_detected_template, appNames[0]) 62 } else { 63 textView.text = 64 view.resources.getString( 65 R.string.screenshot_detected_multiple_template, 66 appNames[0] 67 ) 68 } 69 } 70 } 71