1 /* 2 * Copyright (C) 2022 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.mediaprojection.appselector.view 18 19 import android.content.Context 20 import android.content.res.Configuration 21 import android.graphics.Rect 22 import android.view.WindowManager 23 import androidx.lifecycle.DefaultLifecycleObserver 24 import androidx.lifecycle.LifecycleOwner 25 import com.android.systemui.mediaprojection.appselector.MediaProjectionAppSelectorScope 26 import com.android.systemui.mediaprojection.appselector.view.TaskPreviewSizeProvider.TaskPreviewSizeListener 27 import com.android.systemui.shared.recents.utilities.Utilities.isLargeScreen 28 import com.android.systemui.statusbar.policy.CallbackController 29 import com.android.systemui.statusbar.policy.ConfigurationController 30 import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener 31 import javax.inject.Inject 32 33 @MediaProjectionAppSelectorScope 34 class TaskPreviewSizeProvider 35 @Inject 36 constructor( 37 private val context: Context, 38 private val windowMetricsProvider: WindowMetricsProvider, 39 private val configurationController: ConfigurationController, 40 private val windowManager: WindowManager, 41 ) : CallbackController<TaskPreviewSizeListener>, ConfigurationListener, DefaultLifecycleObserver { 42 43 /** Returns the size of the task preview on the screen in pixels */ 44 val size: Rect = calculateSize() 45 46 private val listeners = arrayListOf<TaskPreviewSizeListener>() 47 onCreatenull48 override fun onCreate(owner: LifecycleOwner) { 49 configurationController.addCallback(this) 50 } 51 onDestroynull52 override fun onDestroy(owner: LifecycleOwner) { 53 configurationController.removeCallback(this) 54 } 55 onConfigChangednull56 override fun onConfigChanged(newConfig: Configuration) { 57 val newSize = calculateSize() 58 if (newSize != size) { 59 size.set(newSize) 60 listeners.forEach { it.onTaskSizeChanged(size) } 61 } 62 } 63 calculateSizenull64 private fun calculateSize(): Rect { 65 val maxWindowBounds = windowMetricsProvider.maximumWindowBounds 66 val maximumWindowHeight = maxWindowBounds.height() 67 val width = maxWindowBounds.width() 68 var height = maximumWindowHeight 69 70 val isLargeScreen = isLargeScreen(windowManager, context.resources) 71 if (isLargeScreen) { 72 val taskbarSize = windowMetricsProvider.currentWindowInsets.bottom 73 height -= taskbarSize 74 } 75 76 val previewSize = Rect(0, 0, width, height) 77 val scale = (height / maximumWindowHeight.toFloat()) / SCREEN_HEIGHT_TO_TASK_HEIGHT_RATIO 78 previewSize.scale(scale) 79 80 return previewSize 81 } 82 addCallbacknull83 override fun addCallback(listener: TaskPreviewSizeListener) { 84 listeners += listener 85 } 86 removeCallbacknull87 override fun removeCallback(listener: TaskPreviewSizeListener) { 88 listeners -= listener 89 } 90 91 interface TaskPreviewSizeListener { onTaskSizeChangednull92 fun onTaskSizeChanged(size: Rect) 93 } 94 } 95 96 /** 97 * How many times smaller the task preview should be on the screen comparing to the height of the 98 * screen 99 */ 100 private const val SCREEN_HEIGHT_TO_TASK_HEIGHT_RATIO = 4f 101