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.server.display.brightness; 18 19 import android.os.PowerManager; 20 import android.util.MathUtils; 21 22 import com.android.server.display.DisplayBrightnessState; 23 24 /** 25 * A helper class for eualuating brightness utilities 26 */ 27 public final class BrightnessUtils { 28 /** 29 * Checks whether the brightness is within the valid brightness range, not including off. 30 */ isValidBrightnessValue(float brightness)31 public static boolean isValidBrightnessValue(float brightness) { 32 return !Float.isNaN(brightness) && brightness >= PowerManager.BRIGHTNESS_MIN 33 && brightness <= PowerManager.BRIGHTNESS_MAX; 34 } 35 36 /** 37 * Clamps the brightness value in the maximum and the minimum brightness range 38 */ clampAbsoluteBrightness(float value)39 public static float clampAbsoluteBrightness(float value) { 40 return MathUtils.constrain(value, PowerManager.BRIGHTNESS_MIN, 41 PowerManager.BRIGHTNESS_MAX); 42 } 43 44 /** 45 * Clamps the brightness value in the maximum and the minimum brightness adjustment range 46 */ clampBrightnessAdjustment(float value)47 public static float clampBrightnessAdjustment(float value) { 48 return MathUtils.constrain(value, -1.0f, 1.0f); 49 } 50 51 /** 52 * A utility to construct the DisplayBrightnessState 53 */ constructDisplayBrightnessState( int brightnessChangeReason, float brightness, float sdrBrightness, String displayBrightnessStrategyName)54 public static DisplayBrightnessState constructDisplayBrightnessState( 55 int brightnessChangeReason, float brightness, float sdrBrightness, 56 String displayBrightnessStrategyName) { 57 return constructDisplayBrightnessState(brightnessChangeReason, brightness, sdrBrightness, 58 displayBrightnessStrategyName, /* slowChange= */ false); 59 } 60 61 /** 62 * A utility to construct the DisplayBrightnessState 63 */ constructDisplayBrightnessState( int brightnessChangeReason, float brightness, float sdrBrightness, String displayBrightnessStrategyName, boolean slowChange)64 public static DisplayBrightnessState constructDisplayBrightnessState( 65 int brightnessChangeReason, float brightness, float sdrBrightness, 66 String displayBrightnessStrategyName, boolean slowChange) { 67 BrightnessReason brightnessReason = new BrightnessReason(); 68 brightnessReason.setReason(brightnessChangeReason); 69 return new DisplayBrightnessState.Builder() 70 .setBrightness(brightness) 71 .setSdrBrightness(sdrBrightness) 72 .setBrightnessReason(brightnessReason) 73 .setDisplayBrightnessStrategyName(displayBrightnessStrategyName) 74 .setIsSlowChange(slowChange) 75 .build(); 76 } 77 } 78