• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.intentresolver
18 
19 import android.view.View
20 import android.view.animation.AlphaAnimation
21 import android.view.animation.LinearInterpolator
22 import android.view.animation.Transformation
23 import com.android.intentresolver.chooser.TargetInfo
24 
25 private const val IMAGE_FADE_IN_MILLIS = 150L
26 
27 internal class ItemRevealAnimationTracker {
28     private val iconProgress = HashMap<TargetInfo, Record>()
29     private val labelProgress = HashMap<TargetInfo, Record>()
30 
resetnull31     fun reset() {
32         iconProgress.clear()
33         labelProgress.clear()
34     }
35 
animateIconnull36     fun animateIcon(view: View, info: TargetInfo) = animateView(view, info, iconProgress)
37     fun animateLabel(view: View, info: TargetInfo) = animateView(view, info, labelProgress)
38 
39     private fun animateView(view: View, info: TargetInfo, map: MutableMap<TargetInfo, Record>) {
40         val record = map.getOrPut(info) {
41             Record()
42         }
43         if ((view.animation as? RevealAnimation)?.record === record) return
44 
45         view.clearAnimation()
46         if (record.alpha >= 1f) {
47             view.alpha = 1f
48             return
49         }
50 
51         view.startAnimation(RevealAnimation(record))
52     }
53 
54     private class Record(var alpha: Float = 0f)
55 
56     private class RevealAnimation(val record: Record) : AlphaAnimation(record.alpha, 1f) {
57         init {
58             duration = (IMAGE_FADE_IN_MILLIS * (1f - record.alpha)).toLong()
59             interpolator = LinearInterpolator()
60         }
61 
applyTransformationnull62         override fun applyTransformation(interpolatedTime: Float, t: Transformation) {
63             super.applyTransformation(interpolatedTime, t)
64             // One TargetInfo can be simultaneously bou into multiple UI grid items; make sure
65             // that the alpha value only increases. This should not affect running animations, only
66             // a starting point for a new animation when a different view is bound to this target.
67             record.alpha = minOf(1f, maxOf(record.alpha, t.alpha))
68         }
69     }
70 }
71