1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.annotation.Dimension
import android.content.Context
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.Path
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.Region
import android.util.AttributeSet
import android.view.Display
import android.view.DisplayCutout
import android.view.DisplayInfo
import android.view.Surface
import android.view.View
import androidx.annotation.VisibleForTesting
import com.android.systemui.RegionInterceptingFrameLayout.RegionInterceptableView
import com.android.systemui.animation.Interpolators
/**
* A class that handles common actions of display cutout view.
* - Draws cutouts.
* - Handles camera protection.
* - Intercepts touches on cutout areas.
*/
open class DisplayCutoutBaseView : View, RegionInterceptableView {
private var shouldDrawCutout: Boolean = DisplayCutout.getFillBuiltInDisplayCutout(
context.resources, context.display?.uniqueId
)
private var displayUniqueId: String? = null
private var displayMode: Display.Mode? = null
protected val location = IntArray(2)
protected var displayRotation = 0
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
@JvmField val displayInfo = DisplayInfo()
@JvmField protected var pendingConfigChange = false
@JvmField protected val paint = Paint()
@JvmField protected val cutoutPath = Path()
@JvmField protected var showProtection = false
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
@JvmField val protectionRect: RectF = RectF()
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
@JvmField val protectionPath: Path = Path()
private val protectionRectOrig: RectF = RectF()
private val protectionPathOrig: Path = Path()
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
var cameraProtectionProgress: Float = HIDDEN_CAMERA_PROTECTION_SCALE
private var cameraProtectionAnimator: ValueAnimator? = null
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
super(context, attrs, defStyleAttr)
override fun onAttachedToWindow() {
super.onAttachedToWindow()
displayUniqueId = context.display?.uniqueId
updateCutout()
updateProtectionBoundingPath()
onUpdate()
}
fun updateConfiguration(newDisplayUniqueId: String?) {
val info = DisplayInfo()
context.display?.getDisplayInfo(info)
val oldMode: Display.Mode? = displayMode
displayMode = info.mode
updateDisplayUniqueId(info.uniqueId)
// Skip if display mode or cutout hasn't changed.
if (!displayModeChanged(oldMode, displayMode) &&
displayInfo.displayCutout == info.displayCutout &&
displayRotation == info.rotation) {
return
}
if (newDisplayUniqueId == info.uniqueId) {
displayRotation = info.rotation
updateCutout()
updateProtectionBoundingPath()
onUpdate()
}
}
open fun updateDisplayUniqueId(newDisplayUniqueId: String?) {
if (displayUniqueId != newDisplayUniqueId) {
displayUniqueId = newDisplayUniqueId
shouldDrawCutout = DisplayCutout.getFillBuiltInDisplayCutout(
context.resources, displayUniqueId
)
invalidate()
}
}
open fun updateRotation(rotation: Int) {
displayRotation = rotation
updateCutout()
updateProtectionBoundingPath()
onUpdate()
}
// Called after the cutout and protection bounding path change. Subclasses
// should make any changes that need to happen based on the change.
open fun onUpdate() = Unit
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
public override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (!shouldDrawCutout) {
return
}
canvas.save()
getLocationOnScreen(location)
canvas.translate(-location[0].toFloat(), -location[1].toFloat())
drawCutouts(canvas)
drawCutoutProtection(canvas)
canvas.restore()
}
override fun shouldInterceptTouch(): Boolean {
return displayInfo.displayCutout != null && visibility == VISIBLE && shouldDrawCutout
}
override fun getInterceptRegion(): Region? {
displayInfo.displayCutout ?: return null
val cutoutBounds: Region = rectsToRegion(displayInfo.displayCutout?.boundingRects)
// Transform to window's coordinate space
rootView.getLocationOnScreen(location)
cutoutBounds.translate(-location[0], -location[1])
// Intersect with window's frame
cutoutBounds.op(
rootView.left, rootView.top, rootView.right, rootView.bottom, Region.Op.INTERSECT
)
return cutoutBounds
}
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
open fun updateCutout() {
if (pendingConfigChange) {
return
}
cutoutPath.reset()
context.display?.getDisplayInfo(displayInfo)
displayInfo.displayCutout?.cutoutPath?.let { path -> cutoutPath.set(path) }
invalidate()
}
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
open fun drawCutouts(canvas: Canvas) {
displayInfo.displayCutout?.cutoutPath ?: return
canvas.drawPath(cutoutPath, paint)
}
protected open fun drawCutoutProtection(canvas: Canvas) {
if (cameraProtectionProgress > HIDDEN_CAMERA_PROTECTION_SCALE &&
!protectionRect.isEmpty
) {
canvas.scale(
cameraProtectionProgress, cameraProtectionProgress, protectionRect.centerX(),
protectionRect.centerY()
)
canvas.drawPath(protectionPath, paint)
}
}
/**
* Converts a set of [Rect]s into a [Region]
*/
fun rectsToRegion(rects: List<Rect?>?): Region {
val result = Region.obtain()
if (rects != null) {
for (r in rects) {
if (r != null && !r.isEmpty) {
result.op(r, Region.Op.UNION)
}
}
}
return result
}
open fun enableShowProtection(show: Boolean) {
if (showProtection == show) {
return
}
showProtection = show
updateProtectionBoundingPath()
// Delay the relayout until the end of the animation when hiding the cutout,
// otherwise we'd clip it.
if (showProtection) {
requestLayout()
}
cameraProtectionAnimator?.cancel()
cameraProtectionAnimator = ValueAnimator.ofFloat(
cameraProtectionProgress,
if (showProtection) 1.0f else HIDDEN_CAMERA_PROTECTION_SCALE
).setDuration(750)
cameraProtectionAnimator?.interpolator = Interpolators.DECELERATE_QUINT
cameraProtectionAnimator?.addUpdateListener(
ValueAnimator.AnimatorUpdateListener { animation: ValueAnimator ->
cameraProtectionProgress = animation.animatedValue as Float
invalidate()
}
)
cameraProtectionAnimator?.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
cameraProtectionAnimator = null
if (!showProtection) {
requestLayout()
}
}
})
cameraProtectionAnimator?.start()
}
open fun setProtection(path: Path, pathBounds: Rect) {
protectionPathOrig.reset()
protectionPathOrig.set(path)
protectionPath.reset()
protectionRectOrig.setEmpty()
protectionRectOrig.set(pathBounds)
protectionRect.setEmpty()
}
protected open fun updateProtectionBoundingPath() {
if (pendingConfigChange) {
return
}
val m = Matrix()
// Apply display ratio.
val physicalPixelDisplaySizeRatio = getPhysicalPixelDisplaySizeRatio()
m.postScale(physicalPixelDisplaySizeRatio, physicalPixelDisplaySizeRatio)
// Apply rotation.
val lw: Int = displayInfo.logicalWidth
val lh: Int = displayInfo.logicalHeight
val flipped = (
displayInfo.rotation == Surface.ROTATION_90 ||
displayInfo.rotation == Surface.ROTATION_270
)
val dw = if (flipped) lh else lw
val dh = if (flipped) lw else lh
transformPhysicalToLogicalCoordinates(displayInfo.rotation, dw, dh, m)
if (!protectionPathOrig.isEmpty) {
// Reset the protection path so we don't aggregate rotations
protectionPath.set(protectionPathOrig)
protectionPath.transform(m)
m.mapRect(protectionRect, protectionRectOrig)
}
}
@VisibleForTesting
open fun getPhysicalPixelDisplaySizeRatio(): Float {
displayInfo.displayCutout?.let {
return it.cutoutPathParserInfo.physicalPixelDisplaySizeRatio
}
return 1f
}
private fun displayModeChanged(oldMode: Display.Mode?, newMode: Display.Mode?): Boolean {
if (oldMode == null) {
return true
}
// We purposely ignore refresh rate and id changes here, because we don't need to
// invalidate for those, and they can trigger the refresh rate to increase
return oldMode?.physicalHeight != newMode?.physicalHeight ||
oldMode?.physicalWidth != newMode?.physicalWidth
}
companion object {
const val HIDDEN_CAMERA_PROTECTION_SCALE = 0.5f
@JvmStatic protected fun transformPhysicalToLogicalCoordinates(
@Surface.Rotation rotation: Int,
@Dimension physicalWidth: Int,
@Dimension physicalHeight: Int,
out: Matrix
) {
when (rotation) {
Surface.ROTATION_0 -> return
Surface.ROTATION_90 -> {
out.postRotate(270f)
out.postTranslate(0f, physicalWidth.toFloat())
}
Surface.ROTATION_180 -> {
out.postRotate(180f)
out.postTranslate(physicalWidth.toFloat(), physicalHeight.toFloat())
}
Surface.ROTATION_270 -> {
out.postRotate(90f)
out.postTranslate(physicalHeight.toFloat(), 0f)
}
else -> throw IllegalArgumentException("Unknown rotation: $rotation")
}
}
}
}
|