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 androidx.annotation.VisibleForTesting 20 21 /** Controller that handles playing [RippleAnimation]. */ 22 class MultiRippleController(private val multipleRippleView: MultiRippleView) { 23 24 private val ripplesFinishedListeners = ArrayList<RipplesFinishedListener>() 25 26 companion object { 27 /** Max number of ripple animations at a time. */ 28 @VisibleForTesting const val MAX_RIPPLE_NUMBER = 10 29 30 interface RipplesFinishedListener { 31 /** Triggered when all the ripples finish running. */ 32 fun onRipplesFinish() 33 } 34 } 35 36 fun addRipplesFinishedListener(listener: RipplesFinishedListener) { 37 ripplesFinishedListeners.add(listener) 38 } 39 40 /** Updates all the ripple colors during the animation. */ 41 fun updateColor(color: Int) { 42 multipleRippleView.ripples.forEach { anim -> anim.updateColor(color) } 43 } 44 45 fun play(rippleAnimation: RippleAnimation) { 46 if (multipleRippleView.ripples.size >= MAX_RIPPLE_NUMBER) { 47 return 48 } 49 50 multipleRippleView.ripples.add(rippleAnimation) 51 52 rippleAnimation.play { 53 // Remove ripple once the animation is done 54 multipleRippleView.ripples.remove(rippleAnimation) 55 if (multipleRippleView.ripples.isEmpty()) { 56 ripplesFinishedListeners.forEach { listener -> listener.onRipplesFinish() } 57 } 58 } 59 60 // Trigger drawing 61 multipleRippleView.invalidate() 62 } 63 } 64