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 private const val BURN_IN_PREVENTION_PERIOD_SCALE = 181f 25 private const val BURN_IN_PREVENTION_PERIOD_PROGRESS = 89f 26 27 /** 28 * Returns the translation offset that should be used to avoid burn in at 29 * the current time (in pixels.) 30 * 31 * @param amplitude Maximum translation that will be interpolated. 32 * @param xAxis If we're moving on X or Y. 33 */ 34 fun getBurnInOffset(amplitude: Int, xAxis: Boolean): Int { 35 return zigzag(System.currentTimeMillis() / MILLIS_PER_MINUTES, 36 amplitude.toFloat(), 37 if (xAxis) BURN_IN_PREVENTION_PERIOD_X else BURN_IN_PREVENTION_PERIOD_Y).toInt() 38 } 39 40 /** 41 * Returns a progress offset (between 0f and 1.0f) that should be used to avoid burn in at 42 * the current time. 43 */ 44 fun getBurnInProgressOffset(): Float { 45 return zigzag(System.currentTimeMillis() / MILLIS_PER_MINUTES, 46 1f, BURN_IN_PREVENTION_PERIOD_PROGRESS) 47 } 48 49 /** 50 * Returns a value to scale a view in order to avoid burn in. 51 */ 52 fun getBurnInScale(): Float { 53 return 0.8f + zigzag(System.currentTimeMillis() / MILLIS_PER_MINUTES, 54 0.2f, BURN_IN_PREVENTION_PERIOD_SCALE) 55 } 56 57 /** 58 * Implements a continuous, piecewise linear, periodic zig-zag function 59 * 60 * Can be thought of as a linear approximation of abs(sin(x))) 61 * 62 * @param period period of the function, ie. zigzag(x + period) == zigzag(x) 63 * @param amplitude maximum value of the function 64 * @return a value between 0 and amplitude 65 */ 66 private fun zigzag(x: Float, amplitude: Float, period: Float): Float { 67 val xprime = x % period / (period / 2) 68 val interpolationAmount = if (xprime <= 1) xprime else 2 - xprime 69 return MathUtils.lerp(0f, amplitude, interpolationAmount) 70 }