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

Commit 82e25a93 authored by Matías Hernández's avatar Matías Hernández Committed by Android (Google) Code Review
Browse files

Merge "Add ZenDeviceEffects and its Builder" into main

parents f29b9048 da39c513
Loading
Loading
Loading
Loading
+25 −2
Original line number Diff line number Diff line
@@ -5316,21 +5316,23 @@ package android.app {
    method public android.net.Uri getConditionId();
    method @Nullable public android.content.ComponentName getConfigurationActivity();
    method public long getCreationTime();
    method @FlaggedApi("android.app.modes_api") @Nullable public android.service.notification.ZenDeviceEffects getDeviceEffects();
    method @FlaggedApi("android.app.modes_api") @DrawableRes public int getIconResId();
    method public int getInterruptionFilter();
    method public String getName();
    method public android.content.ComponentName getOwner();
    method @FlaggedApi("android.app.modes_api") @Nullable public String getTriggerDescription();
    method @FlaggedApi("android.app.modes_api") public int getType();
    method public android.service.notification.ZenPolicy getZenPolicy();
    method @Nullable public android.service.notification.ZenPolicy getZenPolicy();
    method public boolean isEnabled();
    method @FlaggedApi("android.app.modes_api") public boolean isManualInvocationAllowed();
    method public void setConditionId(android.net.Uri);
    method public void setConfigurationActivity(@Nullable android.content.ComponentName);
    method @FlaggedApi("android.app.modes_api") public void setDeviceEffects(@Nullable android.service.notification.ZenDeviceEffects);
    method public void setEnabled(boolean);
    method public void setInterruptionFilter(int);
    method public void setName(String);
    method public void setZenPolicy(android.service.notification.ZenPolicy);
    method public void setZenPolicy(@Nullable android.service.notification.ZenPolicy);
    method public void writeToParcel(android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.app.AutomaticZenRule> CREATOR;
    field @FlaggedApi("android.app.modes_api") public static final int TYPE_BEDTIME = 3; // 0x3
@@ -5350,6 +5352,7 @@ package android.app {
    method @NonNull public android.app.AutomaticZenRule build();
    method @NonNull public android.app.AutomaticZenRule.Builder setConditionId(@NonNull android.net.Uri);
    method @NonNull public android.app.AutomaticZenRule.Builder setConfigurationActivity(@Nullable android.content.ComponentName);
    method @NonNull public android.app.AutomaticZenRule.Builder setDeviceEffects(@Nullable android.service.notification.ZenDeviceEffects);
    method @NonNull public android.app.AutomaticZenRule.Builder setEnabled(boolean);
    method @NonNull public android.app.AutomaticZenRule.Builder setIconResId(@DrawableRes int);
    method @NonNull public android.app.AutomaticZenRule.Builder setInterruptionFilter(int);
@@ -40717,6 +40720,26 @@ package android.service.notification {
    field @NonNull public static final android.os.Parcelable.Creator<android.service.notification.StatusBarNotification> CREATOR;
  }
  @FlaggedApi("android.app.modes_api") public final class ZenDeviceEffects implements android.os.Parcelable {
    method public int describeContents();
    method public boolean shouldDimWallpaper();
    method public boolean shouldDisplayGrayscale();
    method public boolean shouldSuppressAmbientDisplay();
    method public boolean shouldUseNightMode();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.service.notification.ZenDeviceEffects> CREATOR;
  }
  @FlaggedApi("android.app.modes_api") public static final class ZenDeviceEffects.Builder {
    ctor public ZenDeviceEffects.Builder();
    ctor public ZenDeviceEffects.Builder(@NonNull android.service.notification.ZenDeviceEffects);
    method @NonNull public android.service.notification.ZenDeviceEffects build();
    method @NonNull public android.service.notification.ZenDeviceEffects.Builder setShouldDimWallpaper(boolean);
    method @NonNull public android.service.notification.ZenDeviceEffects.Builder setShouldDisplayGrayscale(boolean);
    method @NonNull public android.service.notification.ZenDeviceEffects.Builder setShouldSuppressAmbientDisplay(boolean);
    method @NonNull public android.service.notification.ZenDeviceEffects.Builder setShouldUseNightMode(boolean);
  }
  public final class ZenPolicy implements android.os.Parcelable {
    method public int describeContents();
    method public int getPriorityCallSenders();
+44 −6
Original line number Diff line number Diff line
@@ -22,12 +22,12 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.NotificationManager.InterruptionFilter;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.service.notification.Condition;
import android.service.notification.ZenDeviceEffects;
import android.service.notification.ZenPolicy;
import android.view.WindowInsetsController;

@@ -111,6 +111,7 @@ public final class AutomaticZenRule implements Parcelable {
    private ComponentName configurationActivity;
    private long creationTime;
    private ZenPolicy mZenPolicy;
    private ZenDeviceEffects mDeviceEffects;
    private boolean mModified = false;
    private String mPkg;
    private int mType = TYPE_UNKNOWN;
@@ -190,6 +191,7 @@ public final class AutomaticZenRule implements Parcelable {
    /**
     * @hide
     */
    // TODO: b/310620812 - Remove when the flag is inlined (all system callers should use Builder).
    public AutomaticZenRule(String name, ComponentName owner, ComponentName configurationActivity,
            Uri conditionId, ZenPolicy policy, int interruptionFilter, boolean enabled,
            long creationTime) {
@@ -209,10 +211,11 @@ public final class AutomaticZenRule implements Parcelable {
        configurationActivity = getTrimmedComponentName(
                source.readParcelable(null, android.content.ComponentName.class));
        creationTime = source.readLong();
        mZenPolicy = source.readParcelable(null, android.service.notification.ZenPolicy.class);
        mZenPolicy = source.readParcelable(null, ZenPolicy.class);
        mModified = source.readInt() == ENABLED;
        mPkg = source.readString();
        if (Flags.modesApi()) {
            mDeviceEffects = source.readParcelable(null, ZenDeviceEffects.class);
            mAllowManualInvocation = source.readBoolean();
            mIconResId = source.readInt();
            mTriggerDescription = getTrimmedString(source.readString(), MAX_DESC_LENGTH);
@@ -274,10 +277,18 @@ public final class AutomaticZenRule implements Parcelable {
    /**
     * Gets the zen policy.
     */
    @Nullable
    public ZenPolicy getZenPolicy() {
        return mZenPolicy == null ? null : this.mZenPolicy.copy();
    }

    /** Gets the {@link ZenDeviceEffects} of this rule. */
    @Nullable
    @FlaggedApi(Flags.FLAG_MODES_API)
    public ZenDeviceEffects getDeviceEffects() {
        return mDeviceEffects;
    }

    /**
     * Returns the time this rule was created, represented as milliseconds since the epoch.
     */
@@ -325,10 +336,20 @@ public final class AutomaticZenRule implements Parcelable {
    /**
     * Sets the zen policy.
     */
    public void setZenPolicy(ZenPolicy zenPolicy) {
    public void setZenPolicy(@Nullable ZenPolicy zenPolicy) {
        this.mZenPolicy = (zenPolicy == null ? null : zenPolicy.copy());
    }

    /**
     * Sets the {@link ZenDeviceEffects} associated to this rule. Device effects specify changes to
     * the device behavior that should apply while the rule is active, but are not directly related
     * to suppressing notifications (for example: disabling always-on display).
     */
    @FlaggedApi(Flags.FLAG_MODES_API)
    public void setDeviceEffects(@Nullable ZenDeviceEffects deviceEffects) {
        mDeviceEffects = deviceEffects;
    }

    /**
     * Sets the configuration activity - an activity that handles
     * {@link NotificationManager#ACTION_AUTOMATIC_ZEN_RULE} that shows the user more information
@@ -451,6 +472,7 @@ public final class AutomaticZenRule implements Parcelable {
        dest.writeInt(mModified ? ENABLED : DISABLED);
        dest.writeString(mPkg);
        if (Flags.modesApi()) {
            dest.writeParcelable(mDeviceEffects, 0);
            dest.writeBoolean(mAllowManualInvocation);
            dest.writeInt(mIconResId);
            dest.writeString(mTriggerDescription);
@@ -472,7 +494,8 @@ public final class AutomaticZenRule implements Parcelable {
                .append(",mZenPolicy=").append(mZenPolicy);

        if (Flags.modesApi()) {
            sb.append(",allowManualInvocation=").append(mAllowManualInvocation)
            sb.append(",deviceEffects=").append(mDeviceEffects)
                    .append(",allowManualInvocation=").append(mAllowManualInvocation)
                    .append(",iconResId=").append(mIconResId)
                    .append(",triggerDescription=").append(mTriggerDescription)
                    .append(",type=").append(mType);
@@ -498,6 +521,7 @@ public final class AutomaticZenRule implements Parcelable {
                && other.creationTime == creationTime;
        if (Flags.modesApi()) {
            return finalEquals
                    && Objects.equals(other.mDeviceEffects, mDeviceEffects)
                    && other.mAllowManualInvocation == mAllowManualInvocation
                    && other.mIconResId == mIconResId
                    && Objects.equals(other.mTriggerDescription, mTriggerDescription)
@@ -510,8 +534,8 @@ public final class AutomaticZenRule implements Parcelable {
    public int hashCode() {
        if (Flags.modesApi()) {
            return Objects.hash(enabled, name, interruptionFilter, conditionId, owner,
                    configurationActivity, mZenPolicy, mModified, creationTime, mPkg,
                    mAllowManualInvocation, mIconResId, mTriggerDescription, mType);
                    configurationActivity, mZenPolicy, mDeviceEffects, mModified, creationTime,
                    mPkg, mAllowManualInvocation, mIconResId, mTriggerDescription, mType);
        }
        return Objects.hash(enabled, name, interruptionFilter, conditionId, owner,
                configurationActivity, mZenPolicy, mModified, creationTime, mPkg);
@@ -573,6 +597,7 @@ public final class AutomaticZenRule implements Parcelable {
        private boolean mEnabled;
        private ComponentName mConfigurationActivity = null;
        private ZenPolicy mPolicy = null;
        private ZenDeviceEffects mDeviceEffects = null;
        private int mType;
        private String mDescription;
        private int mIconResId;
@@ -588,6 +613,7 @@ public final class AutomaticZenRule implements Parcelable {
            mEnabled = rule.isEnabled();
            mConfigurationActivity = rule.getConfigurationActivity();
            mPolicy = rule.getZenPolicy();
            mDeviceEffects = rule.getDeviceEffects();
            mType = rule.getType();
            mDescription = rule.getTriggerDescription();
            mIconResId = rule.getIconResId();
@@ -638,6 +664,17 @@ public final class AutomaticZenRule implements Parcelable {
            return this;
        }

        /**
         * Sets the {@link ZenDeviceEffects} associated to this rule. Device effects specify changes
         * to the device behavior that should apply while the rule is active, but are not directly
         * related to suppressing notifications (for example: disabling always-on display).
         */
        @NonNull
        public Builder setDeviceEffects(@Nullable ZenDeviceEffects deviceEffects) {
            mDeviceEffects = deviceEffects;
            return this;
        }

        /**
         * Sets the type of the rule
         */
@@ -687,6 +724,7 @@ public final class AutomaticZenRule implements Parcelable {
        public @NonNull AutomaticZenRule build() {
            AutomaticZenRule rule = new AutomaticZenRule(mName, mOwner, mConfigurationActivity,
                    mConditionId, mPolicy, mInterruptionFilter, mEnabled);
            rule.mDeviceEffects = mDeviceEffects;
            rule.creationTime = mCreationTime;
            rule.mType = mType;
            rule.mTriggerDescription = mDescription;
+360 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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 android.service.notification;

import android.annotation.FlaggedApi;
import android.annotation.NonNull;
import android.app.Flags;
import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.Objects;

/**
 * Represents the set of device effects (affecting display and device behavior in general) that
 * are applied whenever an {@link android.app.AutomaticZenRule} is active.
 */
@FlaggedApi(Flags.FLAG_MODES_API)
public final class ZenDeviceEffects implements Parcelable {

    private final boolean mGrayscale;
    private final boolean mSuppressAmbientDisplay;
    private final boolean mDimWallpaper;
    private final boolean mNightMode;

    private final boolean mDisableAutoBrightness;
    private final boolean mDisableTapToWake;
    private final boolean mDisableTiltToWake;
    private final boolean mDisableTouch;
    private final boolean mMinimizeRadioUsage;
    private final boolean mMaximizeDoze;

    private ZenDeviceEffects(boolean grayscale, boolean suppressAmbientDisplay,
            boolean dimWallpaper, boolean nightMode, boolean disableAutoBrightness,
            boolean disableTapToWake, boolean disableTiltToWake, boolean disableTouch,
            boolean minimizeRadioUsage, boolean maximizeDoze) {
        mGrayscale = grayscale;
        mSuppressAmbientDisplay = suppressAmbientDisplay;
        mDimWallpaper = dimWallpaper;
        mNightMode = nightMode;
        mDisableAutoBrightness = disableAutoBrightness;
        mDisableTapToWake = disableTapToWake;
        mDisableTiltToWake = disableTiltToWake;
        mDisableTouch = disableTouch;
        mMinimizeRadioUsage = minimizeRadioUsage;
        mMaximizeDoze = maximizeDoze;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof final ZenDeviceEffects that)) return false;
        if (obj == this) return true;

        return this.mGrayscale == that.mGrayscale
                && this.mSuppressAmbientDisplay == that.mSuppressAmbientDisplay
                && this.mDimWallpaper == that.mDimWallpaper
                && this.mNightMode == that.mNightMode
                && this.mDisableAutoBrightness == that.mDisableAutoBrightness
                && this.mDisableTapToWake == that.mDisableTapToWake
                && this.mDisableTiltToWake == that.mDisableTiltToWake
                && this.mDisableTouch == that.mDisableTouch
                && this.mMinimizeRadioUsage == that.mMinimizeRadioUsage
                && this.mMaximizeDoze == that.mMaximizeDoze;
    }

    @Override
    public int hashCode() {
        return Objects.hash(mGrayscale, mSuppressAmbientDisplay, mDimWallpaper, mNightMode,
                mDisableAutoBrightness, mDisableTapToWake, mDisableTiltToWake, mDisableTouch,
                mMinimizeRadioUsage, mMaximizeDoze);
    }

    @Override
    public String toString() {
        ArrayList<String> effects = new ArrayList<>(10);
        if (mGrayscale) effects.add("grayscale");
        if (mSuppressAmbientDisplay) effects.add("suppressAmbientDisplay");
        if (mDimWallpaper) effects.add("dimWallpaper");
        if (mNightMode) effects.add("nightMode");
        if (mDisableAutoBrightness) effects.add("disableAutoBrightness");
        if (mDisableTapToWake) effects.add("disableTapToWake");
        if (mDisableTiltToWake) effects.add("disableTiltToWake");
        if (mDisableTouch) effects.add("disableTouch");
        if (mMinimizeRadioUsage) effects.add("minimizeRadioUsage");
        if (mMaximizeDoze) effects.add("maximizeDoze");
        return "[" + String.join(", ", effects) + "]";
    }

    /**
     * Whether the level of color saturation of the display should be set to minimum, effectively
     * switching it to grayscale, while the rule is active.
     */
    public boolean shouldDisplayGrayscale() {
        return mGrayscale;
    }

    /**
     * Whether the ambient (always-on) display feature should be disabled while the rule is active.
     * This will have no effect if the device doesn't support always-on display or if it's not
     * generally enabled.
     */
    public boolean shouldSuppressAmbientDisplay() {
        return mSuppressAmbientDisplay;
    }

    /** Whether the wallpaper should be dimmed while the rule is active. */
    public boolean shouldDimWallpaper() {
        return mDimWallpaper;
    }

    /** Whether night mode (aka dark theme) should be applied while the rule is active. */
    public boolean shouldUseNightMode() {
        return mNightMode;
    }

    /**
     * Whether the display's automatic brightness adjustment should be disabled while the rule is
     * active.
     * @hide
     */
    public boolean shouldDisableAutoBrightness() {
        return mDisableAutoBrightness;
    }

    /**
     * Whether "tap to wake" should be disabled while the rule is active.
     * @hide
     */
    public boolean shouldDisableTapToWake() {
        return mDisableTapToWake;
    }

    /**
     * Whether "tilt to wake" should be disabled while the rule is active.
     * @hide
     */
    public boolean shouldDisableTiltToWake() {
        return mDisableTiltToWake;
    }

    /**
     * Whether touch interactions should be disabled while the rule is active.
     * @hide
     */
    public boolean shouldDisableTouch() {
        return mDisableTouch;
    }

    /**
     * Whether radio (wi-fi, LTE, etc) traffic, and its attendant battery consumption, should be
     * minimized while the rule is active.
     * @hide
     */
    public boolean shouldMinimizeRadioUsage() {
        return mMinimizeRadioUsage;
    }

    /**
     * Whether Doze should be enhanced (e.g. with more aggresive activation, or less frequent
     * maintenance windows) while the rule is active.
     * @hide
     */
    public boolean shouldMaximizeDoze() {
        return mMaximizeDoze;
    }

    /** {@link Parcelable.Creator} that instantiates {@link ZenDeviceEffects} objects. */
    @NonNull
    public static final Creator<ZenDeviceEffects> CREATOR = new Creator<ZenDeviceEffects>() {
        @Override
        public ZenDeviceEffects createFromParcel(Parcel in) {
            return new ZenDeviceEffects(in.readBoolean(), in.readBoolean(), in.readBoolean(),
                    in.readBoolean(), in.readBoolean(), in.readBoolean(), in.readBoolean(),
                    in.readBoolean(), in.readBoolean(), in.readBoolean());
        }

        @Override
        public ZenDeviceEffects[] newArray(int size) {
            return new ZenDeviceEffects[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeBoolean(mGrayscale);
        dest.writeBoolean(mSuppressAmbientDisplay);
        dest.writeBoolean(mDimWallpaper);
        dest.writeBoolean(mNightMode);
        dest.writeBoolean(mDisableAutoBrightness);
        dest.writeBoolean(mDisableTapToWake);
        dest.writeBoolean(mDisableTiltToWake);
        dest.writeBoolean(mDisableTouch);
        dest.writeBoolean(mMinimizeRadioUsage);
        dest.writeBoolean(mMaximizeDoze);
    }

    /** Builder class for {@link ZenDeviceEffects} objects. */
    @FlaggedApi(Flags.FLAG_MODES_API)
    public static final class Builder {

        private boolean mGrayscale;
        private boolean mSuppressAmbientDisplay;
        private boolean mDimWallpaper;
        private boolean mNightMode;
        private boolean mDisableAutoBrightness;
        private boolean mDisableTapToWake;
        private boolean mDisableTiltToWake;
        private boolean mDisableTouch;
        private boolean mMinimizeRadioUsage;
        private boolean mMaximizeDoze;

        /**
         * Instantiates a new {@link ZenPolicy.Builder} with all effects set to default (disabled).
         */
        public Builder() {
        }

        /**
         * Instantiates a new {@link ZenPolicy.Builder} with all effects set to their corresponding
         * values in the supplied {@link ZenDeviceEffects}.
         */
        public Builder(@NonNull ZenDeviceEffects zenDeviceEffects) {
            mGrayscale = zenDeviceEffects.shouldDisplayGrayscale();
            mSuppressAmbientDisplay = zenDeviceEffects.shouldSuppressAmbientDisplay();
            mDimWallpaper = zenDeviceEffects.shouldDimWallpaper();
            mNightMode = zenDeviceEffects.shouldUseNightMode();
            mDisableAutoBrightness = zenDeviceEffects.shouldDisableAutoBrightness();
            mDisableTapToWake = zenDeviceEffects.shouldDisableTapToWake();
            mDisableTiltToWake = zenDeviceEffects.shouldDisableTiltToWake();
            mDisableTouch = zenDeviceEffects.shouldDisableTouch();
            mMinimizeRadioUsage = zenDeviceEffects.shouldMinimizeRadioUsage();
            mMaximizeDoze = zenDeviceEffects.shouldMaximizeDoze();
        }

        /**
         * Sets whether the level of color saturation of the display should be set to minimum,
         * effectively switching it to grayscale, while the rule is active.
         */
        @NonNull
        public Builder setShouldDisplayGrayscale(boolean grayscale) {
            mGrayscale = grayscale;
            return this;
        }

        /**
         * Sets whether the ambient (always-on) display feature should be disabled while the rule
         * is active. This will have no effect if the device doesn't support always-on display or if
         * it's not generally enabled.
         */
        @NonNull
        public Builder setShouldSuppressAmbientDisplay(boolean suppressAmbientDisplay) {
            mSuppressAmbientDisplay = suppressAmbientDisplay;
            return this;
        }

        /** Sets whether the wallpaper should be dimmed while the rule is active. */
        @NonNull
        public Builder setShouldDimWallpaper(boolean dimWallpaper) {
            mDimWallpaper = dimWallpaper;
            return this;
        }

        /** Sets whether night mode (aka dark theme) should be applied while the rule is active. */
        @NonNull
        public Builder setShouldUseNightMode(boolean nightMode) {
            mNightMode = nightMode;
            return this;
        }

        /**
         * Sets whether the display's automatic brightness adjustment should be disabled while the
         * rule is active.
         * @hide
         */
        @NonNull
        public Builder setShouldDisableAutoBrightness(boolean disableAutoBrightness) {
            mDisableAutoBrightness = disableAutoBrightness;
            return this;
        }

        /**
         * Sets whether "tap to wake" should be disabled while the rule is active.
         * @hide
         */
        @NonNull
        public Builder setShouldDisableTapToWake(boolean disableTapToWake) {
            mDisableTapToWake = disableTapToWake;
            return this;
        }

        /**
         * Sets whether "tilt to wake" should be disabled while the rule is active.
         * @hide
         */
        @NonNull
        public Builder setShouldDisableTiltToWake(boolean disableTiltToWake) {
            mDisableTiltToWake = disableTiltToWake;
            return this;
        }

        /**
         * Sets whether touch interactions should be disabled while the rule is active.
         * @hide
         */
        @NonNull
        public Builder setShouldDisableTouch(boolean disableTouch) {
            mDisableTouch = disableTouch;
            return this;
        }

        /**
         * Sets whether radio (wi-fi, LTE, etc) traffic, and its attendant battery consumption,
         * should be minimized while the rule is active.
         * @hide
         */
        @NonNull
        public Builder setShouldMinimizeRadioUsage(boolean minimizeRadioUsage) {
            mMinimizeRadioUsage = minimizeRadioUsage;
            return this;
        }

        /**
         * Sets whether Doze should be enhanced (e.g. with more aggresive activation, or less
         * frequent maintenance windows) while the rule is active.
         * @hide
         */
        @NonNull
        public Builder setShouldMaximizeDoze(boolean maximizeDoze) {
            mMaximizeDoze = maximizeDoze;
            return this;
        }

        /** Builds a {@link ZenDeviceEffects} object based on the builder's state. */
        @NonNull
        public ZenDeviceEffects build() {
            return new ZenDeviceEffects(mGrayscale, mSuppressAmbientDisplay, mDimWallpaper,
                    mNightMode, mDisableAutoBrightness, mDisableTapToWake, mDisableTiltToWake,
                    mDisableTouch, mMinimizeRadioUsage, mMaximizeDoze);
        }
    }
}
+105 −0

File added.

Preview size limit exceeded, changes collapsed.