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, String displayBrightnessStrategyName)54 public static DisplayBrightnessState constructDisplayBrightnessState( 55 int brightnessChangeReason, float brightness, String displayBrightnessStrategyName) { 56 return constructDisplayBrightnessState(brightnessChangeReason, brightness, 57 displayBrightnessStrategyName, /* slowChange= */ false); 58 } 59 60 /** 61 * A utility to construct the DisplayBrightnessState 62 */ constructDisplayBrightnessState( int brightnessChangeReason, float brightness, String displayBrightnessStrategyName, boolean slowChange)63 public static DisplayBrightnessState constructDisplayBrightnessState( 64 int brightnessChangeReason, float brightness, 65 String displayBrightnessStrategyName, boolean slowChange) { 66 BrightnessReason brightnessReason = new BrightnessReason(); 67 brightnessReason.setReason(brightnessChangeReason); 68 return new DisplayBrightnessState.Builder() 69 .setBrightness(brightness) 70 .setBrightnessReason(brightnessReason) 71 .setDisplayBrightnessStrategyName(displayBrightnessStrategyName) 72 .setIsSlowChange(slowChange) 73 .build(); 74 } 75 } 76