diff --git a/services/core/java/com/android/server/display/DisplayBrightnessState.java b/services/core/java/com/android/server/display/DisplayBrightnessState.java new file mode 100644 index 0000000000000..87cbbfe1c62a0 --- /dev/null +++ b/services/core/java/com/android/server/display/DisplayBrightnessState.java @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.display; + +import com.android.server.display.brightness.BrightnessReason; + +import java.util.Objects; + +/** + * A state class representing a set of brightness related entities that are decided at runtime by + * the DisplayBrightnessModeStrategies when updating the brightness. + */ +public final class DisplayBrightnessState { + private final float mBrightness; + private final float mSdrBrightness; + private final BrightnessReason mBrightnessReason; + + private DisplayBrightnessState(Builder builder) { + this.mBrightness = builder.getBrightness(); + this.mSdrBrightness = builder.getSdrBrightness(); + this.mBrightnessReason = builder.getBrightnessReason(); + } + + /** + * Gets the brightness + */ + public float getBrightness() { + return mBrightness; + } + + /** + * Gets the sdr brightness + */ + public float getSdrBrightness() { + return mSdrBrightness; + } + + /** + * Gets the {@link BrightnessReason} + */ + public BrightnessReason getBrightnessReason() { + return mBrightnessReason; + } + + @Override + public String toString() { + StringBuilder stringBuilder = new StringBuilder("DisplayBrightnessState:"); + stringBuilder.append("\n brightness:"); + stringBuilder.append(getBrightness()); + stringBuilder.append("\n sdrBrightness:"); + stringBuilder.append(getSdrBrightness()); + stringBuilder.append("\n brightnessReason:"); + stringBuilder.append(getBrightnessReason()); + return stringBuilder.toString(); + } + + /** + * Checks whether the two objects have the same values. + */ + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + if (!(other instanceof DisplayBrightnessState)) { + return false; + } + + DisplayBrightnessState + displayBrightnessState = (DisplayBrightnessState) other; + + if (mBrightness != displayBrightnessState.getBrightness()) { + return false; + } + if (mSdrBrightness != displayBrightnessState.getSdrBrightness()) { + return false; + } + if (!mBrightnessReason.equals(displayBrightnessState.getBrightnessReason())) { + return false; + } + return true; + } + + @Override + public int hashCode() { + return Objects.hash(mBrightness, mSdrBrightness, mBrightnessReason); + } + + /** + * A DisplayBrightnessState's builder class. + */ + public static class Builder { + private float mBrightness; + private float mSdrBrightness; + private BrightnessReason mBrightnessReason = new BrightnessReason(); + + /** + * Gets the brightness + */ + public float getBrightness() { + return mBrightness; + } + + /** + * Sets the brightness + * + * @param brightness The brightness to be associated with DisplayBrightnessState's + * builder + */ + public Builder setBrightness(float brightness) { + this.mBrightness = brightness; + return this; + } + + /** + * Gets the sdr brightness + */ + public float getSdrBrightness() { + return mSdrBrightness; + } + + /** + * Sets the sdr brightness + * + * @param sdrBrightness The sdr brightness to be associated with DisplayBrightnessState's + * builder + */ + public Builder setSdrBrightness(float sdrBrightness) { + this.mSdrBrightness = sdrBrightness; + return this; + } + + /** + * Gets the {@link BrightnessReason} + */ + public BrightnessReason getBrightnessReason() { + return mBrightnessReason; + } + + /** + * Sets the {@link BrightnessReason} + * + * @param brightnessReason The brightness reason {@link BrightnessReason} to be + * associated with the builder + */ + public Builder setBrightnessReason(BrightnessReason brightnessReason) { + this.mBrightnessReason = brightnessReason; + return this; + } + + /** + * This is used to construct an immutable DisplayBrightnessState object from its builder + */ + public DisplayBrightnessState build() { + return new DisplayBrightnessState(this); + } + } +} diff --git a/services/core/java/com/android/server/display/brightness/strategy/DisplayBrightnessModeStrategy.java b/services/core/java/com/android/server/display/brightness/strategy/DisplayBrightnessModeStrategy.java new file mode 100644 index 0000000000000..3be5933cd3f12 --- /dev/null +++ b/services/core/java/com/android/server/display/brightness/strategy/DisplayBrightnessModeStrategy.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.display.brightness.strategy; + +import android.hardware.display.DisplayManagerInternal; + +import com.android.server.display.DisplayBrightnessState; + +import java.io.PrintWriter; + +/** + * An interface to define the general skeleton of how a BrightnessModeStrategy should look like + * This is responsible for deciding the DisplayBrightnessState that the display should change to, + * not taking into account clamping that might be needed + */ +public interface DisplayBrightnessModeStrategy { + /** + * Decides the DisplayBrightnessState that the system should change to. + * + * @param displayPowerRequest The request to evaluate the updated brightness + * @param displayState The target displayState to which the system should + * change to after processing the request + * @param displayBrightnessStateBuilder The DisplayBrightnessStateBuilder, consisting of + * DisplayBrightnessState that have been constructed so far + */ + DisplayBrightnessState.Builder updateBrightness( + DisplayManagerInternal.DisplayPowerRequest displayPowerRequest, int displayState, + DisplayBrightnessState.Builder displayBrightnessStateBuilder); + + /** + * Used to dump the state. + * + * @param writer The PrintWriter used to dump the state. + */ + void dump(PrintWriter writer); +} diff --git a/services/tests/mockingservicestests/src/com/android/server/display/DisplayBrightnessStateTest.java b/services/tests/mockingservicestests/src/com/android/server/display/DisplayBrightnessStateTest.java new file mode 100644 index 0000000000000..50996d7199c8c --- /dev/null +++ b/services/tests/mockingservicestests/src/com/android/server/display/DisplayBrightnessStateTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.display; + +import static org.junit.Assert.assertEquals; + +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + +import com.android.server.display.brightness.BrightnessReason; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@SmallTest +@RunWith(AndroidJUnit4.class) +public class DisplayBrightnessStateTest { + private static final float FLOAT_DELTA = 0.001f; + + private DisplayBrightnessState.Builder mDisplayBrightnessStateBuilder; + + @Before + public void before() { + mDisplayBrightnessStateBuilder = new DisplayBrightnessState.Builder(); + } + + @Test + public void validateAllDisplayBrightnessStateFieldsAreSetAsExpected() { + float brightness = 0.3f; + float sdrBrightness = 0.2f; + BrightnessReason brightnessReason = new BrightnessReason(); + brightnessReason.setReason(BrightnessReason.REASON_AUTOMATIC); + brightnessReason.setModifier(BrightnessReason.MODIFIER_DIMMED); + DisplayBrightnessState displayBrightnessState = + mDisplayBrightnessStateBuilder.setBrightness(brightness).setSdrBrightness( + sdrBrightness).setBrightnessReason(brightnessReason).build(); + + assertEquals(displayBrightnessState.getBrightness(), brightness, FLOAT_DELTA); + assertEquals(displayBrightnessState.getSdrBrightness(), sdrBrightness, FLOAT_DELTA); + assertEquals(displayBrightnessState.getBrightnessReason(), brightnessReason); + assertEquals(displayBrightnessState.toString(), getString(displayBrightnessState)); + } + + private String getString(DisplayBrightnessState displayBrightnessState) { + StringBuilder sb = new StringBuilder(); + sb.append("DisplayBrightnessState:"); + sb.append("\n brightness:" + displayBrightnessState.getBrightness()); + sb.append("\n sdrBrightness:" + displayBrightnessState.getSdrBrightness()); + sb.append("\n brightnessReason:" + displayBrightnessState.getBrightnessReason()); + return sb.toString(); + } +}