summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/toast/ToastDefaultAnimation.kt
blob: 8187956962b6a76e6da4458eae2d9494b6ed5816 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.toast

import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.LinearInterpolator
import android.view.animation.PathInterpolator
import android.animation.AnimatorSet

class ToastDefaultAnimation {
    /**
     * sum of the in and out animation durations cannot exceed
     * [com.android.server.policy.PhoneWindowManager.TOAST_WINDOW_ANIM_BUFFER] to prevent the toast
     * window from being removed before animations are completed
     */
    companion object {
        // total duration shouldn't exceed NotificationManagerService's delay for "in" animation
        fun toastIn(view: View): AnimatorSet? {
            val icon: View? = view.findViewById(com.android.systemui.R.id.icon)
            val text: View? = view.findViewById(com.android.systemui.R.id.text)
            if (icon == null || text == null) {
                return null
            }
            val linearInterp = LinearInterpolator()
            val scaleInterp = PathInterpolator(0f, 0f, 0f, 1f)
            val sX = ObjectAnimator.ofFloat(view, "scaleX", 0.9f, 1f).apply {
                interpolator = scaleInterp
                duration = 333
            }
            val sY = ObjectAnimator.ofFloat(view, "scaleY", 0.9f, 1f).apply {
                interpolator = scaleInterp
                duration = 333
            }
            val vA = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f).apply {
                interpolator = linearInterp
                duration = 66
            }
            text.alpha = 0f // Set now otherwise won't apply until start delay
            val tA = ObjectAnimator.ofFloat(text, "alpha", 0f, 1f).apply {
                interpolator = linearInterp
                duration = 283
                startDelay = 50
            }
            icon.alpha = 0f // Set now otherwise won't apply until start delay
            val iA = ObjectAnimator.ofFloat(icon, "alpha", 0f, 1f).apply {
                interpolator = linearInterp
                duration = 283
                startDelay = 50
            }
            return AnimatorSet().apply {
                playTogether(sX, sY, vA, tA, iA)
            }
        }

        fun toastOut(view: View): AnimatorSet? {
            // total duration shouldn't exceed NotificationManagerService's delay for "out" anim
            val icon: View? = view.findViewById(com.android.systemui.R.id.icon)
            val text: View? = view.findViewById(com.android.systemui.R.id.text)
            if (icon == null || text == null) {
                return null
            }
            val linearInterp = LinearInterpolator()
            val scaleInterp = PathInterpolator(0.3f, 0f, 1f, 1f)
            val viewScaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0.9f).apply {
                interpolator = scaleInterp
                duration = 250
            }
            val viewScaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0.9f).apply {
                interpolator = scaleInterp
                duration = 250
            }
            val viewElevation = ObjectAnimator.ofFloat(view, "elevation",
                view.elevation, 0f).apply {
                interpolator = linearInterp
                duration = 40
                startDelay = 150
            }
            val viewAlpha = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f).apply {
                interpolator = linearInterp
                duration = 100
                startDelay = 150
            }
            val textAlpha = ObjectAnimator.ofFloat(text, "alpha", 1f, 0f).apply {
                interpolator = linearInterp
                duration = 166
            }
            val iconAlpha = ObjectAnimator.ofFloat(icon, "alpha", 1f, 0f).apply {
                interpolator = linearInterp
                duration = 166
            }
            return AnimatorSet().apply {
                playTogether(
                    viewScaleX,
                    viewScaleY,
                    viewElevation,
                    viewAlpha,
                    textAlpha,
                    iconAlpha)
            }
        }
    }
}