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.dream.lowlight.util
18 
19 import android.view.animation.Interpolator
20 
21 /**
22  * Interpolator wrapper that shortens another interpolator from its original duration to a portion
23  * of that duration.
24  *
25  * For example, an `originalDuration` of 1000 and a `newDuration` of 200 results in an animation
26  * that when played for 200ms is the exact same as the first 200ms of a 1000ms animation if using
27  * the original interpolator.
28  *
29  * This is useful for the transition between the user dream and the low light clock as some
30  * animations are defined in the spec to be longer than the total duration of the animation. For
31  * example, the low light clock exit translation animation is defined to last >1s while the actual
32  * fade out of the low light clock is only 250ms, meaning the clock isn't visible anymore after
33  * 250ms.
34  *
35  * Since the dream framework currently only allows one dream to be visible and running, we use this
36  * interpolator to play just the first 250ms of the translation animation. Simply reducing the
37  * duration of the animation would result in the text exiting much faster than intended, so a custom
38  * interpolator is needed.
39  */
40 class TruncatedInterpolator(
41     private val baseInterpolator: Interpolator,
42     originalDuration: Float,
43     newDuration: Float
44 ) : Interpolator {
45     private val scaleFactor: Float
46 
47     init {
48         scaleFactor = newDuration / originalDuration
49     }
50 
51     override fun getInterpolation(input: Float): Float {
52         return baseInterpolator.getInterpolation(input * scaleFactor)
53     }
54 }
55