blob: cd4ace669b6ce57b48c7cc3f0631667a035c8a08 [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
Julia Reynolds0edb50c2016-02-26 14:08:25 -050019import android.app.NotificationManager.InterruptionFilter;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040020import android.content.ComponentName;
21import android.net.Uri;
22import android.os.Parcel;
23import android.os.Parcelable;
24
25import java.util.Objects;
26
27/**
28 * Rule instance information for zen mode.
29 */
Jeff Sharkey50d1c042016-02-29 16:34:46 -070030public final class AutomaticZenRule implements Parcelable {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040031
32 private boolean enabled = false;
33 private String name;
Julia Reynolds0edb50c2016-02-26 14:08:25 -050034 private @InterruptionFilter int interruptionFilter;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040035 private Uri conditionId;
36 private ComponentName owner;
Julia Reynolds56106ff2015-09-30 14:42:53 -040037 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
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050045 * service to apply the given interruption filter.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040046 * @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,
Julia Reynolds361e82d32016-02-26 18:19:49 -050065 int interruptionFilter, boolean enabled, long creationTime) {
Julia Reynolds56106ff2015-09-30 14:42:53 -040066 this(name, owner, conditionId, interruptionFilter, enabled);
Julia Reynolds56106ff2015-09-30 14:42:53 -040067 this.creationTime = creationTime;
68 }
69
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040070 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 Reynolds56106ff2015-09-30 14:42:53 -040078 creationTime = source.readLong();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040079 }
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 Reynolds361e82d32016-02-26 18:19:49 -0500117 * Returns the time this rule was created, represented as milliseconds since the epoch.
Julia Reynolds56106ff2015-09-30 14:42:53 -0400118 */
119 public long getCreationTime() {
120 return creationTime;
121 }
122
123 /**
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400124 * 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 Reynolds0edb50c2016-02-26 14:08:25 -0500132 * @param interruptionFilter The do not disturb mode to enter when this rule is active.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400133 */
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500134 public void setInterruptionFilter(@InterruptionFilter int interruptionFilter) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400135 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 Reynolds56106ff2015-09-30 14:42:53 -0400169 dest.writeLong(creationTime);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400170 }
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 Reynolds56106ff2015-09-30 14:42:53 -0400180 .append(",creationTime=").append(creationTime)
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400181 .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 Reynolds56106ff2015-09-30 14:42:53 -0400193 && Objects.equals(other.owner, owner)
Julia Reynolds56106ff2015-09-30 14:42:53 -0400194 && other.creationTime == creationTime;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400195 }
196
197 @Override
198 public int hashCode() {
Julia Reynolds361e82d32016-02-26 18:19:49 -0500199 return Objects.hash(enabled, name, interruptionFilter, conditionId, owner, creationTime);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400200 }
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 Reynolds56106ff2015-09-30 14:42:53 -0400213}