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 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.net.Uri; |
| 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | /** |
| 27 | * Rule instance information for zen mode. |
| 28 | */ |
| 29 | public class AutomaticZenRule implements Parcelable { |
| 30 | |
| 31 | private boolean enabled = false; |
| 32 | private String name; |
| 33 | private int interruptionFilter; |
| 34 | private Uri conditionId; |
| 35 | private ComponentName owner; |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 36 | private String id; |
| 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 |
| 45 | * service to apply the interruption filter. |
| 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, |
| 65 | int interruptionFilter, boolean enabled, String id, long creationTime) { |
| 66 | this(name, owner, conditionId, interruptionFilter, enabled); |
| 67 | this.id = id; |
| 68 | this.creationTime = creationTime; |
| 69 | } |
| 70 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 71 | public AutomaticZenRule(Parcel source) { |
| 72 | enabled = source.readInt() == 1; |
| 73 | if (source.readInt() == 1) { |
| 74 | name = source.readString(); |
| 75 | } |
| 76 | interruptionFilter = source.readInt(); |
| 77 | conditionId = source.readParcelable(null); |
| 78 | owner = source.readParcelable(null); |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 79 | if (source.readInt() == 1) { |
| 80 | id = source.readString(); |
| 81 | } |
| 82 | creationTime = source.readLong(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the {@link ComponentName} of the condition provider service that owns this rule. |
| 87 | */ |
| 88 | public ComponentName getOwner() { |
| 89 | return owner; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns the representation of the state that causes this rule to become active. |
| 94 | */ |
| 95 | public Uri getConditionId() { |
| 96 | return conditionId; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns the interruption filter that is applied when this rule is active. |
| 101 | */ |
| 102 | public int getInterruptionFilter() { |
| 103 | return interruptionFilter; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns the name of this rule. |
| 108 | */ |
| 109 | public String getName() { |
| 110 | return name; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns whether this rule is enabled. |
| 115 | */ |
| 116 | public boolean isEnabled() { |
| 117 | return enabled; |
| 118 | } |
| 119 | |
| 120 | /** |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 121 | * Returns the wall time in milliseconds when this rule was created, if known. |
| 122 | */ |
| 123 | public long getCreationTime() { |
| 124 | return creationTime; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns the unique identifier for this rule. |
| 129 | */ |
| 130 | public String getId() { |
| 131 | return id; |
| 132 | } |
| 133 | |
| 134 | /** |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 135 | * Sets the representation of the state that causes this rule to become active. |
| 136 | */ |
| 137 | public void setConditionId(Uri conditionId) { |
| 138 | this.conditionId = conditionId; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Sets the interruption filter that is applied when this rule is active. |
| 143 | * @param interruptionFilter One of the INTERRUPTION_FILTER_ constants in NotificationManager. |
| 144 | */ |
| 145 | public void setInterruptionFilter(int interruptionFilter) { |
| 146 | this.interruptionFilter = interruptionFilter; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Sets the name of this rule. |
| 151 | */ |
| 152 | public void setName(String name) { |
| 153 | this.name = name; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Enables this rule. |
| 158 | */ |
| 159 | public void setEnabled(boolean enabled) { |
| 160 | this.enabled = enabled; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public int describeContents() { |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void writeToParcel(Parcel dest, int flags) { |
| 170 | dest.writeInt(enabled ? 1 : 0); |
| 171 | if (name != null) { |
| 172 | dest.writeInt(1); |
| 173 | dest.writeString(name); |
| 174 | } else { |
| 175 | dest.writeInt(0); |
| 176 | } |
| 177 | dest.writeInt(interruptionFilter); |
| 178 | dest.writeParcelable(conditionId, 0); |
| 179 | dest.writeParcelable(owner, 0); |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 180 | if (id != null) { |
| 181 | dest.writeInt(1); |
| 182 | dest.writeString(id); |
| 183 | } else { |
| 184 | dest.writeInt(0); |
| 185 | } |
| 186 | dest.writeLong(creationTime); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public String toString() { |
| 191 | return new StringBuilder(AutomaticZenRule.class.getSimpleName()).append('[') |
| 192 | .append("enabled=").append(enabled) |
| 193 | .append(",name=").append(name) |
| 194 | .append(",interruptionFilter=").append(interruptionFilter) |
| 195 | .append(",conditionId=").append(conditionId) |
| 196 | .append(",owner=").append(owner) |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 197 | .append(",id=").append(id) |
| 198 | .append(",creationTime=").append(creationTime) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 199 | .append(']').toString(); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public boolean equals(Object o) { |
| 204 | if (!(o instanceof AutomaticZenRule)) return false; |
| 205 | if (o == this) return true; |
| 206 | final AutomaticZenRule other = (AutomaticZenRule) o; |
| 207 | return other.enabled == enabled |
| 208 | && Objects.equals(other.name, name) |
| 209 | && other.interruptionFilter == interruptionFilter |
| 210 | && Objects.equals(other.conditionId, conditionId) |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 211 | && Objects.equals(other.owner, owner) |
| 212 | && Objects.equals(other.id, id) |
| 213 | && other.creationTime == creationTime; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public int hashCode() { |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 218 | return Objects.hash(enabled, name, interruptionFilter, conditionId, owner, id, creationTime); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | public static final Parcelable.Creator<AutomaticZenRule> CREATOR |
| 222 | = new Parcelable.Creator<AutomaticZenRule>() { |
| 223 | @Override |
| 224 | public AutomaticZenRule createFromParcel(Parcel source) { |
| 225 | return new AutomaticZenRule(source); |
| 226 | } |
| 227 | @Override |
| 228 | public AutomaticZenRule[] newArray(int size) { |
| 229 | return new AutomaticZenRule[size]; |
| 230 | } |
| 231 | }; |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 232 | } |