• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.app.NotificationManager.InterruptionFilter;
22 import android.content.ComponentName;
23 import android.net.Uri;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 import android.service.notification.Condition;
27 import android.service.notification.ZenPolicy;
28 
29 import java.util.Objects;
30 
31 /**
32  * Rule instance information for zen mode.
33  */
34 public final class AutomaticZenRule implements Parcelable {
35     /* @hide */
36     private static final int ENABLED = 1;
37     /* @hide */
38     private static final int DISABLED = 0;
39     private boolean enabled = false;
40     private String name;
41     private @InterruptionFilter int interruptionFilter;
42     private Uri conditionId;
43     private ComponentName owner;
44     private ComponentName configurationActivity;
45     private long creationTime;
46     private ZenPolicy mZenPolicy;
47     private boolean mModified = false;
48     private String mPkg;
49 
50     /**
51      * Creates an automatic zen rule.
52      *
53      * @param name The name of the rule.
54      * @param owner The Condition Provider service that owns this rule.
55      * @param interruptionFilter The interruption filter defines which notifications are allowed to
56      *                           interrupt the user (e.g. via sound & vibration) while this rule
57      *                           is active.
58      * @param enabled Whether the rule is enabled.
59      * @deprecated use {@link #AutomaticZenRule(String, ComponentName, ComponentName, Uri,
60      * ZenPolicy, int, boolean)}.
61      */
62     @Deprecated
AutomaticZenRule(String name, ComponentName owner, Uri conditionId, int interruptionFilter, boolean enabled)63     public AutomaticZenRule(String name, ComponentName owner, Uri conditionId,
64             int interruptionFilter, boolean enabled) {
65         this(name, owner, null, conditionId, null, interruptionFilter, enabled);
66     }
67 
68     /**
69      * Creates an automatic zen rule.
70      *
71      * @param name The name of the rule.
72      * @param owner The Condition Provider service that owns this rule. This can be null if you're
73      *              using {@link NotificationManager#setAutomaticZenRuleState(String, Condition)}
74      *              instead of {@link android.service.notification.ConditionProviderService}.
75      * @param configurationActivity An activity that handles
76      *                              {@link NotificationManager#ACTION_AUTOMATIC_ZEN_RULE} that shows
77      *                              the user
78      *                              more information about this rule and/or allows them to
79      *                              configure it. This is required if you are not using a
80      *                              {@link android.service.notification.ConditionProviderService}.
81      *                              If you are, it overrides the information specified in your
82      *                              manifest.
83      * @param conditionId A representation of the state that should cause your app to apply the
84      *                    given interruption filter.
85      * @param interruptionFilter The interruption filter defines which notifications are allowed to
86      *                           interrupt the user (e.g. via sound & vibration) while this rule
87      *                           is active.
88      * @param policy The policy defines which notifications are allowed to interrupt the user
89      *               while this rule is active. This overrides the global policy while this rule is
90      *               action ({@link Condition#STATE_TRUE}).
91      * @param enabled Whether the rule is enabled.
92      */
AutomaticZenRule(@onNull String name, @Nullable ComponentName owner, @Nullable ComponentName configurationActivity, @NonNull Uri conditionId, @Nullable ZenPolicy policy, int interruptionFilter, boolean enabled)93     public AutomaticZenRule(@NonNull String name, @Nullable ComponentName owner,
94             @Nullable ComponentName configurationActivity, @NonNull Uri conditionId,
95             @Nullable ZenPolicy policy, int interruptionFilter, boolean enabled) {
96         this.name = name;
97         this.owner = owner;
98         this.configurationActivity = configurationActivity;
99         this.conditionId = conditionId;
100         this.interruptionFilter = interruptionFilter;
101         this.enabled = enabled;
102         this.mZenPolicy = policy;
103     }
104 
105     /**
106      * @hide
107      */
AutomaticZenRule(String name, ComponentName owner, ComponentName configurationActivity, Uri conditionId, ZenPolicy policy, int interruptionFilter, boolean enabled, long creationTime)108     public AutomaticZenRule(String name, ComponentName owner, ComponentName configurationActivity,
109             Uri conditionId, ZenPolicy policy, int interruptionFilter, boolean enabled,
110             long creationTime) {
111         this(name, owner, configurationActivity, conditionId, policy, interruptionFilter, enabled);
112         this.creationTime = creationTime;
113     }
114 
AutomaticZenRule(Parcel source)115     public AutomaticZenRule(Parcel source) {
116         enabled = source.readInt() == ENABLED;
117         if (source.readInt() == ENABLED) {
118             name = source.readString();
119         }
120         interruptionFilter = source.readInt();
121         conditionId = source.readParcelable(null);
122         owner = source.readParcelable(null);
123         configurationActivity = source.readParcelable(null);
124         creationTime = source.readLong();
125         mZenPolicy = source.readParcelable(null);
126         mModified = source.readInt() == ENABLED;
127         mPkg = source.readString();
128     }
129 
130     /**
131      * Returns the {@link ComponentName} of the condition provider service that owns this rule.
132      */
getOwner()133     public ComponentName getOwner() {
134         return owner;
135     }
136 
137     /**
138      * Returns the {@link ComponentName} of the activity that shows configuration options
139      * for this rule.
140      */
getConfigurationActivity()141     public @Nullable ComponentName getConfigurationActivity() {
142         return configurationActivity;
143     }
144 
145     /**
146      * Returns the representation of the state that causes this rule to become active.
147      */
getConditionId()148     public Uri getConditionId() {
149         return conditionId;
150     }
151 
152     /**
153      * Returns the interruption filter that is applied when this rule is active.
154      */
getInterruptionFilter()155     public int getInterruptionFilter() {
156         return interruptionFilter;
157     }
158 
159     /**
160      * Returns the name of this rule.
161      */
getName()162     public String getName() {
163         return name;
164     }
165 
166     /**
167      * Returns whether this rule is enabled.
168      */
isEnabled()169     public boolean isEnabled() {
170         return enabled;
171     }
172 
173     /**
174      * Returns whether this rule's name has been modified by the user.
175      * @hide
176      */
isModified()177     public boolean isModified() {
178         return mModified;
179     }
180 
181     /**
182      * Gets the zen policy.
183      */
getZenPolicy()184     public ZenPolicy getZenPolicy() {
185         return mZenPolicy == null ? null : this.mZenPolicy.copy();
186     }
187 
188     /**
189      * Returns the time this rule was created, represented as milliseconds since the epoch.
190      */
getCreationTime()191     public long getCreationTime() {
192       return creationTime;
193     }
194 
195     /**
196      * Sets the representation of the state that causes this rule to become active.
197      */
setConditionId(Uri conditionId)198     public void setConditionId(Uri conditionId) {
199         this.conditionId = conditionId;
200     }
201 
202     /**
203      * Sets the interruption filter that is applied when this rule is active.
204      * @param interruptionFilter The do not disturb mode to enter when this rule is active.
205      */
setInterruptionFilter(@nterruptionFilter int interruptionFilter)206     public void setInterruptionFilter(@InterruptionFilter int interruptionFilter) {
207         this.interruptionFilter = interruptionFilter;
208     }
209 
210     /**
211      * Sets the name of this rule.
212      */
setName(String name)213     public void setName(String name) {
214         this.name = name;
215     }
216 
217     /**
218      * Enables this rule.
219      */
setEnabled(boolean enabled)220     public void setEnabled(boolean enabled) {
221         this.enabled = enabled;
222     }
223 
224     /**
225      * Sets modified state of this rule.
226      * @hide
227      */
setModified(boolean modified)228     public void setModified(boolean modified) {
229         this.mModified = modified;
230     }
231 
232     /**
233      * Sets the zen policy.
234      */
setZenPolicy(ZenPolicy zenPolicy)235     public void setZenPolicy(ZenPolicy zenPolicy) {
236         this.mZenPolicy = (zenPolicy == null ? null : zenPolicy.copy());
237     }
238 
239     /**
240      * Sets the configuration activity - an activity that handles
241      * {@link NotificationManager#ACTION_AUTOMATIC_ZEN_RULE} that shows the user more information
242      * about this rule and/or allows them to configure it. This is required to be non-null for rules
243      * that are not backed by {@link android.service.notification.ConditionProviderService}.
244      */
setConfigurationActivity(@ullable ComponentName componentName)245     public void setConfigurationActivity(@Nullable ComponentName componentName) {
246         this.configurationActivity = componentName;
247     }
248 
249     /**
250      * @hide
251      */
setPackageName(String pkgName)252     public void setPackageName(String pkgName) {
253         mPkg = pkgName;
254     }
255 
256     /**
257      * @hide
258      */
getPackageName()259     public String getPackageName() {
260         return mPkg;
261     }
262 
263     @Override
describeContents()264     public int describeContents() {
265         return 0;
266     }
267 
268     @Override
writeToParcel(Parcel dest, int flags)269     public void writeToParcel(Parcel dest, int flags) {
270         dest.writeInt(enabled ? ENABLED : DISABLED);
271         if (name != null) {
272             dest.writeInt(1);
273             dest.writeString(name);
274         } else {
275             dest.writeInt(0);
276         }
277         dest.writeInt(interruptionFilter);
278         dest.writeParcelable(conditionId, 0);
279         dest.writeParcelable(owner, 0);
280         dest.writeParcelable(configurationActivity, 0);
281         dest.writeLong(creationTime);
282         dest.writeParcelable(mZenPolicy, 0);
283         dest.writeInt(mModified ? ENABLED : DISABLED);
284         dest.writeString(mPkg);
285     }
286 
287     @Override
toString()288     public String toString() {
289         return new StringBuilder(AutomaticZenRule.class.getSimpleName()).append('[')
290                 .append("enabled=").append(enabled)
291                 .append(",name=").append(name)
292                 .append(",interruptionFilter=").append(interruptionFilter)
293                 .append(",pkg=").append(mPkg)
294                 .append(",conditionId=").append(conditionId)
295                 .append(",owner=").append(owner)
296                 .append(",configActivity=").append(configurationActivity)
297                 .append(",creationTime=").append(creationTime)
298                 .append(",mZenPolicy=").append(mZenPolicy)
299                 .append(']').toString();
300     }
301 
302     @Override
equals(@ullable Object o)303     public boolean equals(@Nullable Object o) {
304         if (!(o instanceof AutomaticZenRule)) return false;
305         if (o == this) return true;
306         final AutomaticZenRule other = (AutomaticZenRule) o;
307         return other.enabled == enabled
308                 && other.mModified == mModified
309                 && Objects.equals(other.name, name)
310                 && other.interruptionFilter == interruptionFilter
311                 && Objects.equals(other.conditionId, conditionId)
312                 && Objects.equals(other.owner, owner)
313                 && Objects.equals(other.mZenPolicy, mZenPolicy)
314                 && Objects.equals(other.configurationActivity, configurationActivity)
315                 && Objects.equals(other.mPkg, mPkg)
316                 && other.creationTime == creationTime;
317     }
318 
319     @Override
hashCode()320     public int hashCode() {
321         return Objects.hash(enabled, name, interruptionFilter, conditionId, owner,
322                 configurationActivity, mZenPolicy, mModified, creationTime, mPkg);
323     }
324 
325     public static final @android.annotation.NonNull Parcelable.Creator<AutomaticZenRule> CREATOR
326             = new Parcelable.Creator<AutomaticZenRule>() {
327         @Override
328         public AutomaticZenRule createFromParcel(Parcel source) {
329             return new AutomaticZenRule(source);
330         }
331         @Override
332         public AutomaticZenRule[] newArray(int size) {
333             return new AutomaticZenRule[size];
334         }
335     };
336 }
337