1 /*
<lambda>null2  * 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.surfaceeffects.ripple
18 
19 import android.content.Context
20 import android.graphics.Canvas
21 import android.graphics.Paint
22 import android.util.AttributeSet
23 import android.view.View
24 import androidx.annotation.VisibleForTesting
25 
26 /**
27  * A view that allows multiple ripples to play.
28  *
29  * Use [MultiRippleController] to play ripple animations.
30  */
31 class MultiRippleView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
32 
33     @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
34     val ripples = ArrayList<RippleAnimation>()
35     private val ripplePaint = Paint()
36 
37     companion object {
38         private const val TAG = "MultiRippleView"
39     }
40 
41     override fun onDraw(canvas: Canvas) {
42         if (!canvas.isHardwareAccelerated) {
43             // Drawing with the ripple shader requires hardware acceleration, so skip if it's
44             // unsupported.
45             return
46         }
47 
48         var shouldInvalidate = false
49 
50         ripples.forEach { anim ->
51             ripplePaint.shader = anim.rippleShader
52             canvas.drawPaint(ripplePaint)
53 
54             shouldInvalidate = shouldInvalidate || anim.isPlaying()
55         }
56 
57         if (shouldInvalidate) {
58             invalidate()
59         }
60     }
61 }
62