1 /*
2  * Copyright (C) 2024 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.loadingeffect
18 
19 import android.content.Context
20 import android.graphics.BlendMode
21 import android.graphics.Canvas
22 import android.graphics.Paint
23 import android.util.AttributeSet
24 import android.view.View
25 
26 /** Custom View for drawing the [LoadingEffect] with [Canvas.drawPaint]. */
27 open class LoadingEffectView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
28 
29     private var paint: Paint? = null
30     private var blendMode: BlendMode = BlendMode.SRC_OVER
31 
onDrawnull32     override fun onDraw(canvas: Canvas) {
33         if (!canvas.isHardwareAccelerated) {
34             return
35         }
36         paint?.let { canvas.drawPaint(it) }
37     }
38 
39     /** Designed to be called on [LoadingEffect.PaintDrawCallback.onDraw]. */
drawnull40     fun draw(paint: Paint) {
41         this.paint = paint
42         this.paint!!.blendMode = blendMode
43 
44         invalidate()
45     }
46 
47     /** Sets the blend mode of the [Paint]. */
setBlendModenull48     fun setBlendMode(blendMode: BlendMode) {
49         this.blendMode = blendMode
50     }
51 }
52