• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.WindowInsets.Type
23 import android.view.WindowManager
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 windowManager: WindowManager,
38     configurationController: ConfigurationController
39 ) : CallbackController<TaskPreviewSizeListener>, ConfigurationListener {
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 
46     init {
47         configurationController.addCallback(this)
48     }
49 
onConfigChangednull50     override fun onConfigChanged(newConfig: Configuration) {
51         val newSize = calculateSize()
52         if (newSize != size) {
53             size.set(newSize)
54             listeners.forEach { it.onTaskSizeChanged(size) }
55         }
56     }
57 
calculateSizenull58     private fun calculateSize(): Rect {
59         val windowMetrics = windowManager.maximumWindowMetrics
60         val maximumWindowHeight = windowMetrics.bounds.height()
61         val width = windowMetrics.bounds.width()
62         var height = maximumWindowHeight
63 
64         val isLargeScreen = isLargeScreen(context)
65         if (isLargeScreen) {
66             val taskbarSize =
67                 windowManager.currentWindowMetrics.windowInsets
68                     .getInsets(Type.tappableElement())
69                     .bottom
70             height -= taskbarSize
71         }
72 
73         val previewSize = Rect(0, 0, width, height)
74         val scale = (height / maximumWindowHeight.toFloat()) / SCREEN_HEIGHT_TO_TASK_HEIGHT_RATIO
75         previewSize.scale(scale)
76 
77         return previewSize
78     }
79 
addCallbacknull80     override fun addCallback(listener: TaskPreviewSizeListener) {
81         listeners += listener
82     }
83 
removeCallbacknull84     override fun removeCallback(listener: TaskPreviewSizeListener) {
85         listeners -= listener
86     }
87 
88     interface TaskPreviewSizeListener {
onTaskSizeChangednull89         fun onTaskSizeChanged(size: Rect)
90     }
91 }
92 
93 /**
94  * How many times smaller the task preview should be on the screen comparing to the height of the
95  * screen
96  */
97 private const val SCREEN_HEIGHT_TO_TASK_HEIGHT_RATIO = 4f
98