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
17package android.app;
18
19import android.app.NotificationManager.InterruptionFilter;
20import 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 */
30public final class AutomaticZenRule implements Parcelable {
31
32    private boolean enabled = false;
33    private String name;
34    private @InterruptionFilter int interruptionFilter;
35    private Uri conditionId;
36    private ComponentName owner;
37    private long creationTime;
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 given 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
60    /**
61     * @SystemApi
62     * @hide
63     */
64    public AutomaticZenRule(String name, ComponentName owner, Uri conditionId,
65            int interruptionFilter, boolean enabled, long creationTime) {
66        this(name, owner, conditionId, interruptionFilter, enabled);
67        this.creationTime = creationTime;
68    }
69
70    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);
78        creationTime = source.readLong();
79    }
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    /**
117     * Returns the time this rule was created, represented as milliseconds since the epoch.
118     */
119    public long getCreationTime() {
120      return creationTime;
121    }
122
123    /**
124     * 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.
132     * @param interruptionFilter The do not disturb mode to enter when this rule is active.
133     */
134    public void setInterruptionFilter(@InterruptionFilter int interruptionFilter) {
135        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);
169        dest.writeLong(creationTime);
170    }
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)
180                .append(",creationTime=").append(creationTime)
181                .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)
193                && Objects.equals(other.owner, owner)
194                && other.creationTime == creationTime;
195    }
196
197    @Override
198    public int hashCode() {
199        return Objects.hash(enabled, name, interruptionFilter, conditionId, owner, creationTime);
200    }
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    };
213}
214