Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ced499e5 authored by Rupesh Bansal's avatar Rupesh Bansal Committed by Android (Google) Code Review
Browse files

Merge "Introducing the concept of DisplayBrightnessModeStrategy."

parents 1a593ae7 c18b4b92
Loading
Loading
Loading
Loading
+173 −0
Original line number Diff line number Diff line
/*
 * 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);
        }
    }
}
+50 −0
Original line number Diff line number Diff line
/*
 * 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);
}
+67 −0
Original line number Diff line number Diff line
/*
 * 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();
    }
}