Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2015, 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 android.app; |
| 18 | |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 19 | import android.app.NotificationManager.InterruptionFilter; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.net.Uri; |
| 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
| 24 | |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | /** |
| 28 | * Rule instance information for zen mode. |
| 29 | */ |
Jeff Sharkey | 50d1c04 | 2016-02-29 16:34:46 -0700 | [diff] [blame] | 30 | public final class AutomaticZenRule implements Parcelable { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 31 | |
| 32 | private boolean enabled = false; |
| 33 | private String name; |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 34 | private @InterruptionFilter int interruptionFilter; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 35 | private Uri conditionId; |
| 36 | private ComponentName owner; |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 37 | private long creationTime; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Creates an automatic zen rule. |
| 41 | * |
| 42 | * @param name The name of the rule. |
| 43 | * @param owner The Condition Provider service that owns this rule. |
| 44 | * @param conditionId A representation of the state that should cause the Condition Provider |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 45 | * service to apply the given interruption filter. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 46 | * @param interruptionFilter The interruption filter defines which notifications are allowed to |
| 47 | * interrupt the user (e.g. via sound & vibration) while this rule |
| 48 | * is active. |
| 49 | * @param enabled Whether the rule is enabled. |
| 50 | */ |
| 51 | public AutomaticZenRule(String name, ComponentName owner, Uri conditionId, |
| 52 | int interruptionFilter, boolean enabled) { |
| 53 | this.name = name; |
| 54 | this.owner = owner; |
| 55 | this.conditionId = conditionId; |
| 56 | this.interruptionFilter = interruptionFilter; |
| 57 | this.enabled = enabled; |
| 58 | } |
| 59 | |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 60 | /** |
| 61 | * @SystemApi |
| 62 | * @hide |
| 63 | */ |
| 64 | public AutomaticZenRule(String name, ComponentName owner, Uri conditionId, |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 65 | int interruptionFilter, boolean enabled, long creationTime) { |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 66 | this(name, owner, conditionId, interruptionFilter, enabled); |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 67 | this.creationTime = creationTime; |
| 68 | } |
| 69 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 70 | public AutomaticZenRule(Parcel source) { |
| 71 | enabled = source.readInt() == 1; |
| 72 | if (source.readInt() == 1) { |
| 73 | name = source.readString(); |
| 74 | } |
| 75 | interruptionFilter = source.readInt(); |
| 76 | conditionId = source.readParcelable(null); |
| 77 | owner = source.readParcelable(null); |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 78 | creationTime = source.readLong(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the {@link ComponentName} of the condition provider service that owns this rule. |
| 83 | */ |
| 84 | public ComponentName getOwner() { |
| 85 | return owner; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the representation of the state that causes this rule to become active. |
| 90 | */ |
| 91 | public Uri getConditionId() { |
| 92 | return conditionId; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the interruption filter that is applied when this rule is active. |
| 97 | */ |
| 98 | public int getInterruptionFilter() { |
| 99 | return interruptionFilter; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns the name of this rule. |
| 104 | */ |
| 105 | public String getName() { |
| 106 | return name; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns whether this rule is enabled. |
| 111 | */ |
| 112 | public boolean isEnabled() { |
| 113 | return enabled; |
| 114 | } |
| 115 | |
| 116 | /** |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 117 | * Returns the time this rule was created, represented as milliseconds since the epoch. |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 118 | */ |
| 119 | public long getCreationTime() { |
| 120 | return creationTime; |
| 121 | } |
| 122 | |
| 123 | /** |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 124 | * Sets the representation of the state that causes this rule to become active. |
| 125 | */ |
| 126 | public void setConditionId(Uri conditionId) { |
| 127 | this.conditionId = conditionId; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sets the interruption filter that is applied when this rule is active. |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 132 | * @param interruptionFilter The do not disturb mode to enter when this rule is active. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 133 | */ |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 134 | public void setInterruptionFilter(@InterruptionFilter int interruptionFilter) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 135 | this.interruptionFilter = interruptionFilter; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Sets the name of this rule. |
| 140 | */ |
| 141 | public void setName(String name) { |
| 142 | this.name = name; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Enables this rule. |
| 147 | */ |
| 148 | public void setEnabled(boolean enabled) { |
| 149 | this.enabled = enabled; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public int describeContents() { |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void writeToParcel(Parcel dest, int flags) { |
| 159 | dest.writeInt(enabled ? 1 : 0); |
| 160 | if (name != null) { |
| 161 | dest.writeInt(1); |
| 162 | dest.writeString(name); |
| 163 | } else { |
| 164 | dest.writeInt(0); |
| 165 | } |
| 166 | dest.writeInt(interruptionFilter); |
| 167 | dest.writeParcelable(conditionId, 0); |
| 168 | dest.writeParcelable(owner, 0); |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 169 | dest.writeLong(creationTime); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public String toString() { |
| 174 | return new StringBuilder(AutomaticZenRule.class.getSimpleName()).append('[') |
| 175 | .append("enabled=").append(enabled) |
| 176 | .append(",name=").append(name) |
| 177 | .append(",interruptionFilter=").append(interruptionFilter) |
| 178 | .append(",conditionId=").append(conditionId) |
| 179 | .append(",owner=").append(owner) |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 180 | .append(",creationTime=").append(creationTime) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 181 | .append(']').toString(); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public boolean equals(Object o) { |
| 186 | if (!(o instanceof AutomaticZenRule)) return false; |
| 187 | if (o == this) return true; |
| 188 | final AutomaticZenRule other = (AutomaticZenRule) o; |
| 189 | return other.enabled == enabled |
| 190 | && Objects.equals(other.name, name) |
| 191 | && other.interruptionFilter == interruptionFilter |
| 192 | && Objects.equals(other.conditionId, conditionId) |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 193 | && Objects.equals(other.owner, owner) |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 194 | && other.creationTime == creationTime; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public int hashCode() { |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 199 | return Objects.hash(enabled, name, interruptionFilter, conditionId, owner, creationTime); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | public static final Parcelable.Creator<AutomaticZenRule> CREATOR |
| 203 | = new Parcelable.Creator<AutomaticZenRule>() { |
| 204 | @Override |
| 205 | public AutomaticZenRule createFromParcel(Parcel source) { |
| 206 | return new AutomaticZenRule(source); |
| 207 | } |
| 208 | @Override |
| 209 | public AutomaticZenRule[] newArray(int size) { |
| 210 | return new AutomaticZenRule[size]; |
| 211 | } |
| 212 | }; |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 213 | } |