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

Commit 61538c4a authored by Garvit Narang's avatar Garvit Narang
Browse files

Introduces device effect for brightness clamping for modes

Fixes: 421045411
Test: Test: unit tests + manual flash and check
Flag: android.service.notification.apply_brightness_clamping_for_modes
Change-Id: I091807f8713b38c8c62ab8aa3411b299d63fb7e2
parent 66eaa149
Loading
Loading
Loading
Loading
+75 −2
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.service.notification;

import android.annotation.FloatRange;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
@@ -57,6 +58,7 @@ public final class ZenDeviceEffects implements Parcelable {
                FIELD_MINIMIZE_RADIO_USAGE,
                FIELD_MAXIMIZE_DOZE,
                FIELD_NIGHT_LIGHT,
                FIELD_BRIGHTNESS_CAP,
                FIELD_EXTRA_EFFECTS
            })
    @Retention(RetentionPolicy.SOURCE)
@@ -110,7 +112,12 @@ public final class ZenDeviceEffects implements Parcelable {
    /** @hide */
    public static final int FIELD_NIGHT_LIGHT = 1 << 11;

    /** @hide */
    public static final int FIELD_BRIGHTNESS_CAP = 1 << 12;

    private static final int MAX_EFFECTS_LENGTH = 2_000; // characters
    private static final float BRIGHTNESS_CAP_MIN = 0f;
    private static final float BRIGHTNESS_CAP_MAX = 100f;

    private final boolean mGrayscale;
    private final boolean mSuppressAmbientDisplay;
@@ -124,6 +131,11 @@ public final class ZenDeviceEffects implements Parcelable {
    private final boolean mMinimizeRadioUsage;
    private final boolean mMaximizeDoze;
    private final boolean mNightLight;

    @Nullable
    @FloatRange(from = BRIGHTNESS_CAP_MIN, to = BRIGHTNESS_CAP_MAX)
    private final Float mBrightnessCap;

    private final Set<String> mExtraEffects;

    private ZenDeviceEffects(
@@ -138,6 +150,7 @@ public final class ZenDeviceEffects implements Parcelable {
            boolean minimizeRadioUsage,
            boolean maximizeDoze,
            boolean nightLight,
            @Nullable Float brightnessCap,
            Set<String> extraEffects) {
        mGrayscale = grayscale;
        mSuppressAmbientDisplay = suppressAmbientDisplay;
@@ -150,6 +163,7 @@ public final class ZenDeviceEffects implements Parcelable {
        mMinimizeRadioUsage = minimizeRadioUsage;
        mMaximizeDoze = maximizeDoze;
        mNightLight = nightLight;
        mBrightnessCap = Flags.applyBrightnessClampingForModes() ? brightnessCap : null;
        mExtraEffects = Collections.unmodifiableSet(extraEffects);
    }

@@ -164,6 +178,10 @@ public final class ZenDeviceEffects implements Parcelable {
                    "Total size of extra effects must be at most " + MAX_EFFECTS_LENGTH
                            + " characters");
        }
        if (mBrightnessCap != null
                && (mBrightnessCap < BRIGHTNESS_CAP_MIN || mBrightnessCap > BRIGHTNESS_CAP_MAX)) {
            throw new IllegalArgumentException("Brightness cap must be between 0f and 100f");
        }
    }

    @Override
@@ -182,6 +200,7 @@ public final class ZenDeviceEffects implements Parcelable {
                && this.mMinimizeRadioUsage == that.mMinimizeRadioUsage
                && this.mMaximizeDoze == that.mMaximizeDoze
                && this.mNightLight == that.mNightLight
                && Objects.equals(this.mBrightnessCap, that.mBrightnessCap)
                && Objects.equals(this.mExtraEffects, that.mExtraEffects);
    }

@@ -199,6 +218,7 @@ public final class ZenDeviceEffects implements Parcelable {
                mMinimizeRadioUsage,
                mMaximizeDoze,
                mNightLight,
                mBrightnessCap,
                mExtraEffects);
    }

@@ -216,6 +236,7 @@ public final class ZenDeviceEffects implements Parcelable {
        if (mMinimizeRadioUsage) effects.add("minimizeRadioUsage");
        if (mMaximizeDoze) effects.add("maximizeDoze");
        if (mNightLight) effects.add("nightLight");
        if (mBrightnessCap != null) effects.add("brightnessCap=" + mBrightnessCap + "%");
        if (mExtraEffects.size() > 0) {
            effects.add("extraEffects=[" + String.join(",", mExtraEffects) + "]");
        }
@@ -258,6 +279,9 @@ public final class ZenDeviceEffects implements Parcelable {
        if (((bitmask) & FIELD_NIGHT_LIGHT) != 0) {
            modified.add("FIELD_NIGHT_LIGHT");
        }
        if (((bitmask) & FIELD_BRIGHTNESS_CAP) != 0) {
            modified.add("FIELD_BRIGHTNESS_CAP");
        }
        if ((bitmask & FIELD_EXTRA_EFFECTS) != 0) {
            modified.add("FIELD_EXTRA_EFFECTS");
        }
@@ -351,6 +375,18 @@ public final class ZenDeviceEffects implements Parcelable {
        return mNightLight;
    }

    /**
     * Gets the brightness cap that should be applied while the rule is active. This is reflected as
     * a percentage of the maximum brightness supported by the display.
     *
     * @hide
     */
    @Nullable
    @FloatRange(from = BRIGHTNESS_CAP_MIN, to = BRIGHTNESS_CAP_MAX)
    public Float getBrightnessPercentageCap() {
        return mBrightnessCap;
    }

    /**
     * (Immutable) set of extra effects to be applied while the rule is active. Extra effects are
     * not used in AOSP, but OEMs may add support for them by providing a custom
@@ -379,6 +415,7 @@ public final class ZenDeviceEffects implements Parcelable {
                || mMinimizeRadioUsage
                || mMaximizeDoze
                || mNightLight
                || mBrightnessCap != null
                || mExtraEffects.size() > 0;
    }

@@ -400,6 +437,7 @@ public final class ZenDeviceEffects implements Parcelable {
                            in.readBoolean(),
                            in.readBoolean(),
                            in.readBoolean(),
                            in.readBoolean() ? in.readFloat() : null,
                            Set.of(in.readArray(String.class.getClassLoader(), String.class)));
                }

@@ -427,6 +465,12 @@ public final class ZenDeviceEffects implements Parcelable {
        dest.writeBoolean(mMinimizeRadioUsage);
        dest.writeBoolean(mMaximizeDoze);
        dest.writeBoolean(mNightLight);
        if (mBrightnessCap != null) {
            dest.writeBoolean(/* val= */ true);
            dest.writeFloat(mBrightnessCap);
        } else {
            dest.writeBoolean(/* val= */ false);
        }
        dest.writeArray(mExtraEffects.toArray(new String[0]));
    }

@@ -444,6 +488,7 @@ public final class ZenDeviceEffects implements Parcelable {
        private boolean mMinimizeRadioUsage;
        private boolean mMaximizeDoze;
        private boolean mNightLight;
        @Nullable private Float mBrightnessCap = null;
        private final HashSet<String> mExtraEffects = new HashSet<>();

        /**
@@ -468,6 +513,7 @@ public final class ZenDeviceEffects implements Parcelable {
            mMinimizeRadioUsage = zenDeviceEffects.shouldMinimizeRadioUsage();
            mMaximizeDoze = zenDeviceEffects.shouldMaximizeDoze();
            mNightLight = zenDeviceEffects.shouldUseNightLight();
            mBrightnessCap = zenDeviceEffects.getBrightnessPercentageCap();
            mExtraEffects.addAll(zenDeviceEffects.getExtraEffects());
        }

@@ -581,6 +627,21 @@ public final class ZenDeviceEffects implements Parcelable {
            return this;
        }

        /**
         * Sets whether the maximum brightness of the display should be capped while the rule is
         * active. This is reflected as a percentage of the maximum brightness supported by the
         * display.
         *
         * @hide
         */
        @NonNull
        public Builder setBrightnessPercentageCap(
                @Nullable @FloatRange(from = BRIGHTNESS_CAP_MIN, to = BRIGHTNESS_CAP_MAX)
                        Float brightnessCap) {
            mBrightnessCap = Flags.applyBrightnessClampingForModes() ? brightnessCap : null;
            return this;
        }

        /**
         * Sets the extra effects to be applied while the rule is active. Extra effects are not
         * used in AOSP, but OEMs may add support for them by providing a custom
@@ -630,8 +691,12 @@ public final class ZenDeviceEffects implements Parcelable {
        }

        /**
         * Applies the effects that are {@code true} on the supplied {@link ZenDeviceEffects} to
         * this builder (essentially logically-ORing the effect set).
         * Applies the supplied {@link ZenDeviceEffects} to this builder which is consolidated on a
         * case by case basis choosing the most restrictive option. For effects tracked with a
         * boolean value, currently the structure dictates {@code true} to be most restrictive,
         * essentially logically-ORing the effect set. For {@link #getBrightnessPercentageCap()},
         * the lower range is chosen.
         *
         * @hide
         */
        @NonNull
@@ -648,6 +713,13 @@ public final class ZenDeviceEffects implements Parcelable {
            if (effects.shouldMinimizeRadioUsage()) setShouldMinimizeRadioUsage(true);
            if (effects.shouldMaximizeDoze()) setShouldMaximizeDoze(true);
            if (effects.shouldUseNightLight()) setShouldUseNightLight(true);
            if (mBrightnessCap == null) {
                setBrightnessPercentageCap(effects.getBrightnessPercentageCap());
            } else if (effects.getBrightnessPercentageCap() != null) {
                // BrightnessCap for this and other ZenDeviceEffects is non null.
                setBrightnessPercentageCap(
                        Math.min(effects.getBrightnessPercentageCap(), mBrightnessCap));
            }
            addExtraEffects(effects.getExtraEffects());
            return this;
        }
@@ -667,6 +739,7 @@ public final class ZenDeviceEffects implements Parcelable {
                    mMinimizeRadioUsage,
                    mMaximizeDoze,
                    mNightLight,
                    mBrightnessCap,
                    mExtraEffects);
        }
    }
+27 −4
Original line number Diff line number Diff line
@@ -325,6 +325,7 @@ public class ZenModeConfig implements Parcelable {
    private static final String DEVICE_EFFECT_MINIMIZE_RADIO_USAGE = "zdeMinimizeRadioUsage";
    private static final String DEVICE_EFFECT_MAXIMIZE_DOZE = "zdeMaximizeDoze";
    private static final String DEVICE_EFFECT_USE_NIGHT_LIGHT = "zdeUseNightLight";
    private static final String DEVICE_EFFECT_CAP_BRIGHTNESS = "zdeCapBrightness";
    private static final String DEVICE_EFFECT_EXTRAS = "zdeExtraEffects";
    private static final String DEVICE_EFFECT_USER_MODIFIED_FIELDS = "zdeUserModifiedFields";

@@ -1214,7 +1215,7 @@ public class ZenModeConfig implements Parcelable {

    @Nullable
    private static ZenDeviceEffects readZenDeviceEffectsXml(TypedXmlPullParser parser) {
        ZenDeviceEffects deviceEffects =
        ZenDeviceEffects.Builder builder =
                new ZenDeviceEffects.Builder()
                        .setShouldDisplayGrayscale(
                                safeBoolean(parser, DEVICE_EFFECT_DISPLAY_GRAYSCALE, false))
@@ -1238,9 +1239,11 @@ public class ZenModeConfig implements Parcelable {
                                safeBoolean(parser, DEVICE_EFFECT_MAXIMIZE_DOZE, false))
                        .setShouldUseNightLight(
                                safeBoolean(parser, DEVICE_EFFECT_USE_NIGHT_LIGHT, false))
                        .setExtraEffects(safeStringSet(parser, DEVICE_EFFECT_EXTRAS))
                        .build();

                        .setExtraEffects(safeStringSet(parser, DEVICE_EFFECT_EXTRAS));
        if (android.service.notification.Flags.applyBrightnessClampingForModes()) {
            builder.setBrightnessPercentageCap(unsafeFloat(parser, DEVICE_EFFECT_CAP_BRIGHTNESS));
        }
        ZenDeviceEffects deviceEffects = builder.build();
        return deviceEffects.hasEffects() ? deviceEffects : null;
    }

@@ -1263,6 +1266,10 @@ public class ZenModeConfig implements Parcelable {
                deviceEffects.shouldMinimizeRadioUsage());
        writeBooleanIfTrue(out, DEVICE_EFFECT_MAXIMIZE_DOZE, deviceEffects.shouldMaximizeDoze());
        writeBooleanIfTrue(out, DEVICE_EFFECT_USE_NIGHT_LIGHT, deviceEffects.shouldUseNightLight());
        if (android.service.notification.Flags.applyBrightnessClampingForModes()) {
            writeFloatIfNotNull(
                    out, DEVICE_EFFECT_CAP_BRIGHTNESS, deviceEffects.getBrightnessPercentageCap());
        }
        writeStringSet(out, DEVICE_EFFECT_EXTRAS, deviceEffects.getExtraEffects());
    }

@@ -1273,6 +1280,13 @@ public class ZenModeConfig implements Parcelable {
        }
    }

    private static void writeFloatIfNotNull(
            TypedXmlSerializer out, String att, @Nullable Float value) throws IOException {
        if (value != null) {
            out.attributeFloat(null, att, value);
        }
    }

    private static void writeStringSet(TypedXmlSerializer out, String att, Set<String> values)
            throws IOException {
        if (values.isEmpty()) {
@@ -1326,6 +1340,15 @@ public class ZenModeConfig implements Parcelable {
        return parser.getAttributeInt(null, att, defValue);
    }

    @Nullable
    private static Float unsafeFloat(TypedXmlPullParser parser, String att) {
        try {
            return parser.getAttributeFloat(null, att);
        } catch (Exception e) {
            return null;
        }
    }

    private static ComponentName safeComponentName(TypedXmlPullParser parser, String att) {
        final String val = parser.getAttributeValue(null, att);
        if (TextUtils.isEmpty(val)) return null;
+10 −0
Original line number Diff line number Diff line
@@ -611,6 +611,7 @@ public class ZenModeDiff {
        public static final String FIELD_MINIMIZE_RADIO_USAGE = "mMinimizeRadioUsage";
        public static final String FIELD_MAXIMIZE_DOZE = "mMaximizeDoze";
        public static final String FIELD_NIGHT_LIGHT = "mNightLight";
        public static final String FIELD_BRIGHTNESS_CAP = "mBrightnessCap";
        public static final String FIELD_EXTRA_EFFECTS = "mExtraEffects";
        // NOTE: new field strings must match the variable names in ZenDeviceEffects

@@ -683,6 +684,15 @@ public class ZenModeDiff {
                        FIELD_NIGHT_LIGHT,
                        new FieldDiff<>(from.shouldUseNightLight(), to.shouldUseNightLight()));
            }
            if (Flags.applyBrightnessClampingForModes()
                    && !Objects.equals(
                            from.getBrightnessPercentageCap(), to.getBrightnessPercentageCap())) {
                addField(
                        FIELD_BRIGHTNESS_CAP,
                        new FieldDiff<>(
                                from.getBrightnessPercentageCap(),
                                to.getBrightnessPercentageCap()));
            }
            if (!Objects.equals(from.getExtraEffects(), to.getExtraEffects())) {
                addField(FIELD_EXTRA_EFFECTS, new FieldDiff<>(from.getExtraEffects(),
                        to.getExtraEffects()));
+6 −0
Original line number Diff line number Diff line
@@ -82,6 +82,12 @@ flag {
  bug: "409474564"
}

flag {
  name: "apply_brightness_clamping_for_modes"
  namespace: "wear_frameworks"
  description: "Whether to apply brightness clamping for modes"
  bug: "387528442"
}



+7 −0
Original line number Diff line number Diff line
@@ -1425,6 +1425,7 @@ public class ZenModeHelper {
                            .setShouldMinimizeRadioUsage(oldEffects.shouldMinimizeRadioUsage())
                            .setShouldMaximizeDoze(oldEffects.shouldMaximizeDoze())
                            .setShouldUseNightLight(oldEffects.shouldUseNightLight())
                            .setBrightnessPercentageCap(oldEffects.getBrightnessPercentageCap())
                            .setExtraEffects(oldEffects.getExtraEffects())
                            .build();
        }
@@ -1468,6 +1469,12 @@ public class ZenModeHelper {
            if (oldEffects.shouldUseNightLight() != newEffects.shouldUseNightLight()) {
                userModifiedFields |= ZenDeviceEffects.FIELD_NIGHT_LIGHT;
            }
            if (android.service.notification.Flags.applyBrightnessClampingForModes()
                    && !Objects.equals(
                            oldEffects.getBrightnessPercentageCap(),
                            newEffects.getBrightnessPercentageCap())) {
                userModifiedFields |= ZenDeviceEffects.FIELD_BRIGHTNESS_CAP;
            }
            if (!Objects.equals(oldEffects.getExtraEffects(), newEffects.getExtraEffects())) {
                userModifiedFields |= ZenDeviceEffects.FIELD_EXTRA_EFFECTS;
            }
Loading