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.car.carlauncher
18 
19 import android.animation.Animator
20 import android.animation.AnimatorListenerAdapter
21 import android.animation.AnimatorSet
22 import android.animation.ObjectAnimator
23 import android.animation.ValueAnimator
24 import android.view.View
25 import android.widget.LinearLayout
26 
27 /**
28  * Animation Helper for animating elements on the appGrid
29  */
30 class BackgroundAnimationHelper(
31         private val appsGridBackground: LinearLayout,
32         private val banner: Banner
33 ) {
34     private var bannerAnimatorSet = AnimatorSet()
35 
showBannernull36     fun showBanner() {
37         banner.visibility = View.VISIBLE
38         val bannerTranslateAnimator = ValueAnimator.ofInt(-banner.measuredHeight, 0)
39         val bannerAlphaAnimator = ObjectAnimator.ofFloat(banner, "alpha", 1f)
40         val appsGridTranslateAnimator = ValueAnimator.ofInt(0, banner.measuredHeight)
41 
42         animateBanner(bannerTranslateAnimator, bannerAlphaAnimator, appsGridTranslateAnimator)
43     }
hideBannernull44     fun hideBanner() {
45         val bannerTranslateAnimator = ValueAnimator.ofInt(0, -banner.measuredHeight)
46         val bannerAlphaAnimator = ObjectAnimator.ofFloat(banner, "alpha", 0f)
47         val appsGridTranslateAnimator = ValueAnimator.ofInt(banner.measuredHeight, 0)
48 
49         bannerTranslateAnimator.addListener(object : AnimatorListenerAdapter() {
50             override fun onAnimationEnd(animation: Animator) {
51                 super.onAnimationEnd(animation)
52                 banner.visibility = View.GONE
53             }
54         })
55 
56         animateBanner(bannerTranslateAnimator, bannerAlphaAnimator, appsGridTranslateAnimator)
57     }
58 
animateBannernull59     private fun animateBanner(
60             bannerTranslateAnimator: ValueAnimator,
61             bannerAlphaAnimator: ObjectAnimator,
62             appsGridTranslateAnimator: ValueAnimator
63     ) {
64         bannerTranslateAnimator.addUpdateListener { banner.y = (it.animatedValue as Int).toFloat() }
65         appsGridTranslateAnimator
66             .addUpdateListener { appsGridBackground.y = (it.animatedValue as Int).toFloat() }
67 
68         bannerAnimatorSet.cancel()
69         bannerAnimatorSet = AnimatorSet().apply {
70             duration = BANNER_ANIMATION_DURATION_MS
71             startDelay = BANNER_ANIMATION_START_DELAY_MS
72             playTogether(
73                 bannerTranslateAnimator,
74                 bannerAlphaAnimator,
75                 appsGridTranslateAnimator
76             )
77         }
78         bannerAnimatorSet.start()
79     }
80 
81     private companion object {
82         const val BANNER_ANIMATION_DURATION_MS = 300L
83         const val BANNER_ANIMATION_START_DELAY_MS = 400L
84     }
85 }
86