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