1 /* 2 * 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.media.controls.ui.animation 18 19 import android.animation.Animator 20 import android.animation.AnimatorListenerAdapter 21 22 /** 23 * MetadataAnimationHandler controls the current state of the MediaControlPanel's transition motion. 24 * 25 * It checks for a changed data object (artist & title from MediaControlPanel) and runs the 26 * animation if necessary. When the motion has fully transitioned the elements out, it runs the 27 * update callback to modify the view data, before the enter animation runs. 28 */ 29 internal open class MetadataAnimationHandler( 30 private val exitAnimator: Animator, 31 private val enterAnimator: Animator 32 ) : AnimatorListenerAdapter() { 33 34 private var postExitUpdate: (() -> Unit)? = null 35 private var postEnterUpdate: (() -> Unit)? = null 36 private var targetData: Any? = null 37 38 val isRunning: Boolean 39 get() = enterAnimator.isRunning || exitAnimator.isRunning 40 setNextnull41 fun setNext(targetData: Any, postExit: () -> Unit, postEnter: () -> Unit): Boolean { 42 if (targetData != this.targetData) { 43 this.targetData = targetData 44 postExitUpdate = postExit 45 postEnterUpdate = postEnter 46 if (!isRunning) { 47 exitAnimator.start() 48 } 49 return true 50 } 51 return false 52 } 53 onAnimationEndnull54 override fun onAnimationEnd(anim: Animator) { 55 if (anim === exitAnimator) { 56 postExitUpdate?.let { it() } 57 postExitUpdate = null 58 enterAnimator.start() 59 } 60 61 if (anim === enterAnimator) { 62 // Another new update appeared while entering 63 if (postExitUpdate != null) { 64 exitAnimator.start() 65 } else { 66 postEnterUpdate?.let { it() } 67 postEnterUpdate = null 68 } 69 } 70 } 71 72 init { 73 exitAnimator.addListener(this) 74 enterAnimator.addListener(this) 75 } 76 } 77