blob: ec2825edebe64caf1c0073be8fc8d259b496df8e [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
Beverlyb0989652019-03-04 11:37:52 -050019import android.annotation.NonNull;
20import android.annotation.Nullable;
Julia Reynolds0edb50c2016-02-26 14:08:25 -050021import android.app.NotificationManager.InterruptionFilter;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040022import android.content.ComponentName;
23import android.net.Uri;
24import android.os.Parcel;
25import android.os.Parcelable;
Julia Reynolds68062072018-08-06 15:38:21 -040026import android.service.notification.Condition;
Beverlyb0989652019-03-04 11:37:52 -050027import android.service.notification.ZenPolicy;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040028
29import java.util.Objects;
30
31/**
32 * Rule instance information for zen mode.
33 */
Jeff Sharkey50d1c042016-02-29 16:34:46 -070034public final class AutomaticZenRule implements Parcelable {
Beverly49ba9a62018-11-02 13:19:31 -040035 /* @hide */
36 private static final int ENABLED = 1;
37 /* @hide */
38 private static final int DISABLED = 0;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040039 private boolean enabled = false;
40 private String name;
Julia Reynolds0edb50c2016-02-26 14:08:25 -050041 private @InterruptionFilter int interruptionFilter;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040042 private Uri conditionId;
43 private ComponentName owner;
Julia Reynolds68062072018-08-06 15:38:21 -040044 private ComponentName configurationActivity;
Julia Reynolds56106ff2015-09-30 14:42:53 -040045 private long creationTime;
Beverly174d7412018-08-22 16:34:41 -040046 private ZenPolicy mZenPolicy;
Beverly49ba9a62018-11-02 13:19:31 -040047 private boolean mModified = false;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040048
49 /**
50 * Creates an automatic zen rule.
51 *
52 * @param name The name of the rule.
53 * @param owner The Condition Provider service that owns this rule.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040054 * @param interruptionFilter The interruption filter defines which notifications are allowed to
55 * interrupt the user (e.g. via sound & vibration) while this rule
56 * is active.
57 * @param enabled Whether the rule is enabled.
Julia Reynolds68062072018-08-06 15:38:21 -040058 * @deprecated use {@link #AutomaticZenRule(String, ComponentName, ComponentName, Uri,
59 * ZenPolicy, int, boolean)}.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040060 */
Julia Reynolds68062072018-08-06 15:38:21 -040061 @Deprecated
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040062 public AutomaticZenRule(String name, ComponentName owner, Uri conditionId,
63 int interruptionFilter, boolean enabled) {
Julia Reynolds68062072018-08-06 15:38:21 -040064 this(name, owner, null, conditionId, null, interruptionFilter, enabled);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -040065 }
66
Julia Reynolds56106ff2015-09-30 14:42:53 -040067 /**
Beverly174d7412018-08-22 16:34:41 -040068 * Creates an automatic zen rule.
69 *
70 * @param name The name of the rule.
Julia Reynolds68062072018-08-06 15:38:21 -040071 * @param owner The Condition Provider service that owns this rule. This can be null if you're
72 * using {@link NotificationManager#setAutomaticZenRuleState(String, Condition)}
73 * instead of {@link android.service.notification.ConditionProviderService}.
74 * @param configurationActivity An activity that handles
75 * {@link NotificationManager#ACTION_AUTOMATIC_ZEN_RULE} that shows
76 * the user
77 * more information about this rule and/or allows them to
78 * configure it. This is required if you are not using a
79 * {@link android.service.notification.ConditionProviderService}.
80 * If you are, it overrides the information specified in your
81 * manifest.
82 * @param conditionId A representation of the state that should cause your app to apply the
83 * given interruption filter.
84 * @param interruptionFilter The interruption filter defines which notifications are allowed to
85 * interrupt the user (e.g. via sound & vibration) while this rule
86 * is active.
Beverly174d7412018-08-22 16:34:41 -040087 * @param policy The policy defines which notifications are allowed to interrupt the user
Julia Reynolds68062072018-08-06 15:38:21 -040088 * while this rule is active. This overrides the global policy while this rule is
89 * action ({@link Condition#STATE_TRUE}).
Beverly174d7412018-08-22 16:34:41 -040090 * @param enabled Whether the rule is enabled.
91 */
Beverlyb0989652019-03-04 11:37:52 -050092 public AutomaticZenRule(@NonNull String name, @Nullable ComponentName owner,
93 @Nullable ComponentName configurationActivity, @NonNull Uri conditionId,
94 @Nullable ZenPolicy policy, int interruptionFilter, boolean enabled) {
Beverly174d7412018-08-22 16:34:41 -040095 this.name = name;
96 this.owner = owner;
Julia Reynolds68062072018-08-06 15:38:21 -040097 this.configurationActivity = configurationActivity;
Beverly174d7412018-08-22 16:34:41 -040098 this.conditionId = conditionId;
Julia Reynolds68062072018-08-06 15:38:21 -040099 this.interruptionFilter = interruptionFilter;
Beverly174d7412018-08-22 16:34:41 -0400100 this.enabled = enabled;
101 this.mZenPolicy = policy;
102 }
103
104 /**
Julia Reynolds56106ff2015-09-30 14:42:53 -0400105 * @hide
106 */
Julia Reynolds68062072018-08-06 15:38:21 -0400107 public AutomaticZenRule(String name, ComponentName owner, ComponentName configurationActivity,
108 Uri conditionId, ZenPolicy policy, int interruptionFilter, boolean enabled,
109 long creationTime) {
110 this(name, owner, configurationActivity, conditionId, policy, interruptionFilter, enabled);
Beverly174d7412018-08-22 16:34:41 -0400111 this.creationTime = creationTime;
112 }
113
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400114 public AutomaticZenRule(Parcel source) {
Beverly49ba9a62018-11-02 13:19:31 -0400115 enabled = source.readInt() == ENABLED;
116 if (source.readInt() == ENABLED) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400117 name = source.readString();
118 }
119 interruptionFilter = source.readInt();
120 conditionId = source.readParcelable(null);
121 owner = source.readParcelable(null);
Julia Reynolds68062072018-08-06 15:38:21 -0400122 configurationActivity = source.readParcelable(null);
Julia Reynolds56106ff2015-09-30 14:42:53 -0400123 creationTime = source.readLong();
Beverly174d7412018-08-22 16:34:41 -0400124 mZenPolicy = source.readParcelable(null);
Beverly49ba9a62018-11-02 13:19:31 -0400125 mModified = source.readInt() == ENABLED;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400126 }
127
128 /**
129 * Returns the {@link ComponentName} of the condition provider service that owns this rule.
130 */
131 public ComponentName getOwner() {
132 return owner;
133 }
134
135 /**
Julia Reynolds68062072018-08-06 15:38:21 -0400136 * Returns the {@link ComponentName} of the activity that shows configuration options
137 * for this rule.
138 */
Julia Reynoldscbc45e72019-03-07 12:31:52 -0500139 public @Nullable ComponentName getConfigurationActivity() {
Julia Reynolds68062072018-08-06 15:38:21 -0400140 return configurationActivity;
141 }
142
143 /**
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400144 * Returns the representation of the state that causes this rule to become active.
145 */
146 public Uri getConditionId() {
147 return conditionId;
148 }
149
150 /**
151 * Returns the interruption filter that is applied when this rule is active.
152 */
153 public int getInterruptionFilter() {
154 return interruptionFilter;
155 }
156
157 /**
158 * Returns the name of this rule.
159 */
160 public String getName() {
161 return name;
162 }
163
164 /**
165 * Returns whether this rule is enabled.
166 */
167 public boolean isEnabled() {
168 return enabled;
169 }
170
171 /**
Beverly49ba9a62018-11-02 13:19:31 -0400172 * Returns whether this rule's name has been modified by the user.
173 * @hide
174 */
175 public boolean isModified() {
176 return mModified;
177 }
178
179 /**
Beverly174d7412018-08-22 16:34:41 -0400180 * Gets the zen policy.
181 */
182 public ZenPolicy getZenPolicy() {
Beverlyff2df9b2018-10-10 16:54:10 -0400183 return mZenPolicy == null ? null : this.mZenPolicy.copy();
Beverly174d7412018-08-22 16:34:41 -0400184 }
185
186 /**
Julia Reynolds361e82d32016-02-26 18:19:49 -0500187 * Returns the time this rule was created, represented as milliseconds since the epoch.
Julia Reynolds56106ff2015-09-30 14:42:53 -0400188 */
189 public long getCreationTime() {
190 return creationTime;
191 }
192
193 /**
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400194 * Sets the representation of the state that causes this rule to become active.
195 */
196 public void setConditionId(Uri conditionId) {
197 this.conditionId = conditionId;
198 }
199
200 /**
201 * Sets the interruption filter that is applied when this rule is active.
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500202 * @param interruptionFilter The do not disturb mode to enter when this rule is active.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400203 */
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500204 public void setInterruptionFilter(@InterruptionFilter int interruptionFilter) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400205 this.interruptionFilter = interruptionFilter;
206 }
207
208 /**
209 * Sets the name of this rule.
210 */
211 public void setName(String name) {
212 this.name = name;
213 }
214
215 /**
216 * Enables this rule.
217 */
218 public void setEnabled(boolean enabled) {
219 this.enabled = enabled;
220 }
221
Beverly174d7412018-08-22 16:34:41 -0400222 /**
Beverly49ba9a62018-11-02 13:19:31 -0400223 * Sets modified state of this rule.
224 * @hide
225 */
226 public void setModified(boolean modified) {
227 this.mModified = modified;
228 }
229
230 /**
Beverly174d7412018-08-22 16:34:41 -0400231 * Sets the zen policy.
232 */
233 public void setZenPolicy(ZenPolicy zenPolicy) {
234 this.mZenPolicy = zenPolicy;
235 }
236
Julia Reynolds68062072018-08-06 15:38:21 -0400237 /**
238 * Sets the configuration activity - an activity that handles
239 * {@link NotificationManager#ACTION_AUTOMATIC_ZEN_RULE} that shows the user more information
Julia Reynoldscbc45e72019-03-07 12:31:52 -0500240 * about this rule and/or allows them to configure it. This is required to be non-null for rules
241 * that are not backed by {@link android.service.notification.ConditionProviderService}.
Julia Reynolds68062072018-08-06 15:38:21 -0400242 */
Julia Reynoldscbc45e72019-03-07 12:31:52 -0500243 public void setConfigurationActivity(@Nullable ComponentName componentName) {
Julia Reynolds68062072018-08-06 15:38:21 -0400244 this.configurationActivity = componentName;
245 }
246
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400247 @Override
248 public int describeContents() {
249 return 0;
250 }
251
252 @Override
253 public void writeToParcel(Parcel dest, int flags) {
Beverly49ba9a62018-11-02 13:19:31 -0400254 dest.writeInt(enabled ? ENABLED : DISABLED);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400255 if (name != null) {
256 dest.writeInt(1);
257 dest.writeString(name);
258 } else {
259 dest.writeInt(0);
260 }
261 dest.writeInt(interruptionFilter);
262 dest.writeParcelable(conditionId, 0);
263 dest.writeParcelable(owner, 0);
Julia Reynolds68062072018-08-06 15:38:21 -0400264 dest.writeParcelable(configurationActivity, 0);
Julia Reynolds56106ff2015-09-30 14:42:53 -0400265 dest.writeLong(creationTime);
Beverly174d7412018-08-22 16:34:41 -0400266 dest.writeParcelable(mZenPolicy, 0);
Beverly49ba9a62018-11-02 13:19:31 -0400267 dest.writeInt(mModified ? ENABLED : DISABLED);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400268 }
269
270 @Override
271 public String toString() {
272 return new StringBuilder(AutomaticZenRule.class.getSimpleName()).append('[')
273 .append("enabled=").append(enabled)
274 .append(",name=").append(name)
275 .append(",interruptionFilter=").append(interruptionFilter)
276 .append(",conditionId=").append(conditionId)
277 .append(",owner=").append(owner)
Julia Reynolds68062072018-08-06 15:38:21 -0400278 .append(",configActivity=").append(configurationActivity)
Julia Reynolds56106ff2015-09-30 14:42:53 -0400279 .append(",creationTime=").append(creationTime)
Beverly174d7412018-08-22 16:34:41 -0400280 .append(",mZenPolicy=").append(mZenPolicy)
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400281 .append(']').toString();
282 }
283
284 @Override
285 public boolean equals(Object o) {
286 if (!(o instanceof AutomaticZenRule)) return false;
287 if (o == this) return true;
288 final AutomaticZenRule other = (AutomaticZenRule) o;
289 return other.enabled == enabled
Beverly49ba9a62018-11-02 13:19:31 -0400290 && other.mModified == mModified
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400291 && Objects.equals(other.name, name)
292 && other.interruptionFilter == interruptionFilter
293 && Objects.equals(other.conditionId, conditionId)
Julia Reynolds56106ff2015-09-30 14:42:53 -0400294 && Objects.equals(other.owner, owner)
Julia Reynolds68062072018-08-06 15:38:21 -0400295 && Objects.equals(other.mZenPolicy, mZenPolicy)
296 && Objects.equals(other.configurationActivity, configurationActivity)
297 && other.creationTime == creationTime;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400298 }
299
300 @Override
301 public int hashCode() {
Julia Reynolds68062072018-08-06 15:38:21 -0400302 return Objects.hash(enabled, name, interruptionFilter, conditionId, owner,
303 configurationActivity, mZenPolicy, mModified, creationTime);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400304 }
305
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700306 public static final @android.annotation.NonNull Parcelable.Creator<AutomaticZenRule> CREATOR
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400307 = new Parcelable.Creator<AutomaticZenRule>() {
308 @Override
309 public AutomaticZenRule createFromParcel(Parcel source) {
310 return new AutomaticZenRule(source);
311 }
312 @Override
313 public AutomaticZenRule[] newArray(int size) {
314 return new AutomaticZenRule[size];
315 }
316 };
Julia Reynolds56106ff2015-09-30 14:42:53 -0400317}