1010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev/**
2010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * Copyright (c) 2015, The Android Open Source Project
3010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev *
4010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * Licensed under the Apache License, Version 2.0 (the "License");
5010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * you may not use this file except in compliance with the License.
6010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * You may obtain a copy of the License at
7010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev *
8010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev *     http://www.apache.org/licenses/LICENSE-2.0
9010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev *
10010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * Unless required by applicable law or agreed to in writing, software
11010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * distributed under the License is distributed on an "AS IS" BASIS,
12010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * See the License for the specific language governing permissions and
14010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * limitations under the License.
15010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev */
16010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
17010b6a58c7d19ba2ef68295819fce00b37595decStan Ilievpackage android.app;
18010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
19010b6a58c7d19ba2ef68295819fce00b37595decStan Ilievimport android.app.NotificationManager.InterruptionFilter;
20010b6a58c7d19ba2ef68295819fce00b37595decStan Ilievimport android.content.ComponentName;
21010b6a58c7d19ba2ef68295819fce00b37595decStan Ilievimport android.net.Uri;
22010b6a58c7d19ba2ef68295819fce00b37595decStan Ilievimport android.os.Parcel;
23010b6a58c7d19ba2ef68295819fce00b37595decStan Ilievimport android.os.Parcelable;
24010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
25010b6a58c7d19ba2ef68295819fce00b37595decStan Ilievimport java.util.Objects;
26010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
27010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev/**
28010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * Rule instance information for zen mode.
29010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev */
30010b6a58c7d19ba2ef68295819fce00b37595decStan Ilievpublic final class AutomaticZenRule implements Parcelable {
31010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
32010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    private boolean enabled = false;
33010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    private String name;
34010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    private @InterruptionFilter int interruptionFilter;
35010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    private Uri conditionId;
36010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    private ComponentName owner;
37010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    private long creationTime;
38010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
39010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    /**
40010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     * Creates an automatic zen rule.
41010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     *
42010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     * @param name The name of the rule.
43010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     * @param owner The Condition Provider service that owns this rule.
44010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     * @param conditionId A representation of the state that should cause the Condition Provider
45010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     *                    service to apply the given interruption filter.
46010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     * @param interruptionFilter The interruption filter defines which notifications are allowed to
47010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     *                           interrupt the user (e.g. via sound & vibration) while this rule
48010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     *                           is active.
49010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     * @param enabled Whether the rule is enabled.
50010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     */
51010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    public AutomaticZenRule(String name, ComponentName owner, Uri conditionId,
52010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev            int interruptionFilter, boolean enabled) {
53010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        this.name = name;
54010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        this.owner = owner;
55010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        this.conditionId = conditionId;
56010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        this.interruptionFilter = interruptionFilter;
57010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        this.enabled = enabled;
58010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    }
59010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
60010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    /**
61010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     * @SystemApi
62010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     * @hide
63010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev     */
64010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    public AutomaticZenRule(String name, ComponentName owner, Uri conditionId,
65010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev            int interruptionFilter, boolean enabled, long creationTime) {
66010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        this(name, owner, conditionId, interruptionFilter, enabled);
67010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        this.creationTime = creationTime;
68010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    }
69010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
70010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    public AutomaticZenRule(Parcel source) {
71010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        enabled = source.readInt() == 1;
72010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        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