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 androidx.lifecycle.DefaultLifecycleObserver
23 import androidx.lifecycle.LifecycleOwner
24 import com.android.systemui.mediaprojection.appselector.MediaProjectionAppSelectorScope
25 import com.android.systemui.mediaprojection.appselector.view.TaskPreviewSizeProvider.TaskPreviewSizeListener
26 import com.android.systemui.shared.recents.utilities.Utilities.isLargeScreen
27 import com.android.systemui.statusbar.policy.CallbackController
28 import com.android.systemui.statusbar.policy.ConfigurationController
29 import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener
30 import javax.inject.Inject
31 
32 @MediaProjectionAppSelectorScope
33 class TaskPreviewSizeProvider
34 @Inject
35 constructor(
36     private val context: Context,
37     private val windowMetricsProvider: WindowMetricsProvider,
38     private val configurationController: ConfigurationController,
39 ) : CallbackController<TaskPreviewSizeListener>, ConfigurationListener, DefaultLifecycleObserver {
40 
41     /** Returns the size of the task preview on the screen in pixels */
42     val size: Rect = calculateSize()
43 
44     private val listeners = arrayListOf<TaskPreviewSizeListener>()
45 
onCreatenull46     override fun onCreate(owner: LifecycleOwner) {
47         configurationController.addCallback(this)
48     }
49 
onDestroynull50     override fun onDestroy(owner: LifecycleOwner) {
51         configurationController.removeCallback(this)
52     }
53 
onConfigChangednull54     override fun onConfigChanged(newConfig: Configuration) {
55         val newSize = calculateSize()
56         if (newSize != size) {
57             size.set(newSize)
58             listeners.forEach { it.onTaskSizeChanged(size) }
59         }
60     }
61 
calculateSizenull62     private fun calculateSize(): Rect {
63         val maxWindowBounds = windowMetricsProvider.maximumWindowBounds
64         val maximumWindowHeight = maxWindowBounds.height()
65         val width = maxWindowBounds.width()
66         var height = maximumWindowHeight
67 
68         val isLargeScreen = isLargeScreen(context)
69         if (isLargeScreen) {
70             val taskbarSize = windowMetricsProvider.currentWindowInsets.bottom
71             height -= taskbarSize
72         }
73 
74         val previewSize = Rect(0, 0, width, height)
75         val scale = (height / maximumWindowHeight.toFloat()) / SCREEN_HEIGHT_TO_TASK_HEIGHT_RATIO
76         previewSize.scale(scale)
77 
78         return previewSize
79     }
80 
addCallbacknull81     override fun addCallback(listener: TaskPreviewSizeListener) {
82         listeners += listener
83     }
84 
removeCallbacknull85     override fun removeCallback(listener: TaskPreviewSizeListener) {
86         listeners -= listener
87     }
88 
89     interface TaskPreviewSizeListener {
onTaskSizeChangednull90         fun onTaskSizeChanged(size: Rect)
91     }
92 }
93 
94 /**
95  * How many times smaller the task preview should be on the screen comparing to the height of the
96  * screen
97  */
98 private const val SCREEN_HEIGHT_TO_TASK_HEIGHT_RATIO = 4f
99