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.util.animation
18 
19 import kotlin.math.roundToLong
20 
21 /** A generic util class for animations in SysUI. */
22 class AnimationUtil {
23     companion object {
24         /**
25          * Returns the number of milliseconds there are in [numFrames] for a 60 fps device.
26          *
27          * Note that this method can be used on any device, not just 60 fps devices. Animation
28          * lengths are typically specified in terms of number of frames for a 60 fps device, and
29          * the value "5 frames" is often more meaningful than "83ms". This method allows us to
30          * write animation code in terms of the more meaningful "5" number.
31          *
32          * @param numFrames must be >= 0.
33          */
getMsForFramesnull34         fun getMsForFrames(numFrames: Int): Long {
35             if (numFrames < 0) {
36                 throw IllegalArgumentException("numFrames must be >= 0")
37             }
38             return (numFrames * 1000f / 60f).roundToLong()
39         }
40 
41         /**
42          * Convenience extension function for [getMsForFrames], so that we can write `23.frames`
43          */
44         val Int.frames: Long
45             get() = getMsForFrames(this)
46     }
47 }
48