• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.util.Log
24 import android.view.View
25 import androidx.annotation.VisibleForTesting
26 
27 /**
28  * A view that allows multiple ripples to play.
29  *
30  * Use [MultiRippleController] to play ripple animations.
31  */
32 class MultiRippleView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
33 
34     @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
35     val ripples = ArrayList<RippleAnimation>()
36     private val ripplePaint = Paint()
37     private var isWarningLogged = false
38 
39     companion object {
40         private const val TAG = "MultiRippleView"
41     }
42 
43     override fun onDraw(canvas: Canvas?) {
44         if (canvas == null || !canvas.isHardwareAccelerated) {
45             // Drawing with the ripple shader requires hardware acceleration, so skip
46             // if it's unsupported.
47             if (!isWarningLogged) {
48                 // Only log once to not spam.
49                 Log.w(
50                     TAG,
51                     "Can't draw ripple shader. $canvas does not support hardware acceleration."
52                 )
53                 isWarningLogged = true
54             }
55             return
56         }
57 
58         var shouldInvalidate = false
59 
60         ripples.forEach { anim ->
61             ripplePaint.shader = anim.rippleShader
62             canvas.drawPaint(ripplePaint)
63 
64             shouldInvalidate = shouldInvalidate || anim.isPlaying()
65         }
66 
67         if (shouldInvalidate) {
68             invalidate()
69         }
70     }
71 }
72