blob: e5fa02b248f470d9f1a5630629a062490214f5de [file] [log] [blame]
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001/**
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
17package android.app;
18
19import android.content.ComponentName;
20import android.net.Uri;
21import android.os.Parcel;
22import android.os.Parcelable;
23
24import java.util.Objects;
25
26/**
27 * Rule instance information for zen mode.
28 */
29public 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 Reynolds56106ff2015-09-30 14:42:53 -040036 private String id;
37 private long creationTime;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040038
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 Reynolds56106ff2015-09-30 14:42:53 -040060 /**
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 Reynoldsa47a27f2015-08-24 08:31:47 -040071 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 Reynolds56106ff2015-09-30 14:42:53 -040079 if (source.readInt() == 1) {
80 id = source.readString();
81 }
82 creationTime = source.readLong();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040083 }
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 Reynolds56106ff2015-09-30 14:42:53 -0400121 * 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 Reynoldsa47a27f2015-08-24 08:31:47 -0400135 * 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 Reynolds56106ff2015-09-30 14:42:53 -0400180 if (id != null) {
181 dest.writeInt(1);
182 dest.writeString(id);
183 } else {
184 dest.writeInt(0);
185 }
186 dest.writeLong(creationTime);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400187 }
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 Reynolds56106ff2015-09-30 14:42:53 -0400197 .append(",id=").append(id)
198 .append(",creationTime=").append(creationTime)
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400199 .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 Reynolds56106ff2015-09-30 14:42:53 -0400211 && Objects.equals(other.owner, owner)
212 && Objects.equals(other.id, id)
213 && other.creationTime == creationTime;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400214 }
215
216 @Override
217 public int hashCode() {
Julia Reynolds56106ff2015-09-30 14:42:53 -0400218 return Objects.hash(enabled, name, interruptionFilter, conditionId, owner, id, creationTime);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400219 }
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 Reynolds56106ff2015-09-30 14:42:53 -0400232}