1 /*
2  * Copyright (C) 2018 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.doze.util
18 
19 import android.util.MathUtils
20 
21 private const val MILLIS_PER_MINUTES = 1000 * 60f
22 private const val BURN_IN_PREVENTION_PERIOD_Y = 521f
23 private const val BURN_IN_PREVENTION_PERIOD_X = 83f
24 
25 /**
26  * Returns the translation offset that should be used to avoid burn in at
27  * the current time (in pixels.)
28  *
29  * @param amplitude Maximum translation that will be interpolated.
30  * @param xAxis If we're moving on X or Y.
31  */
getBurnInOffsetnull32 fun getBurnInOffset(amplitude: Int, xAxis: Boolean): Int {
33     return zigzag(System.currentTimeMillis() / MILLIS_PER_MINUTES,
34             amplitude.toFloat(),
35             if (xAxis) BURN_IN_PREVENTION_PERIOD_X else BURN_IN_PREVENTION_PERIOD_Y).toInt()
36 }
37 
38 /**
39  * Implements a continuous, piecewise linear, periodic zig-zag function
40  *
41  * Can be thought of as a linear approximation of abs(sin(x)))
42  *
43  * @param period period of the function, ie. zigzag(x + period) == zigzag(x)
44  * @param amplitude maximum value of the function
45  * @return a value between 0 and amplitude
46  */
zigzagnull47 private fun zigzag(x: Float, amplitude: Float, period: Float): Float {
48     val xprime = x % period / (period / 2)
49     val interpolationAmount = if (xprime <= 1) xprime else 2 - xprime
50     return MathUtils.lerp(0f, amplitude, interpolationAmount)
51 }