blob: 0163b47f2ba7fef4013538a6cd2e9bb09fee7752 [file] [log] [blame]
John Spurlock7340fc82014-04-24 18:50:12 -04001/**
2 * Copyright (c) 2014, 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.service.notification;
18
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050019import android.annotation.IntDef;
Julia Reynoldsa62496d2016-01-29 15:29:16 -050020import android.annotation.SystemApi;
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050021import android.app.AutomaticZenRule;
John Spurlocke77bb362014-04-26 10:24:59 -040022import android.content.Context;
John Spurlock7340fc82014-04-24 18:50:12 -040023import android.net.Uri;
24import android.os.Parcel;
25import android.os.Parcelable;
26
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050027import java.lang.annotation.Retention;
28import java.lang.annotation.RetentionPolicy;
John Spurlock7340fc82014-04-24 18:50:12 -040029import java.util.Objects;
30
31/**
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050032 * The current condition of an {@link android.app.AutomaticZenRule}, provided by the
33 * {@link ConditionProviderService} that owns the rule. Used to tell the system to enter Do Not
34 * Disturb mode and request that the system exit Do Not Disturb mode.
John Spurlock7340fc82014-04-24 18:50:12 -040035 */
36public class Condition implements Parcelable {
37
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050038 @SystemApi
John Spurlocke77bb362014-04-26 10:24:59 -040039 public static final String SCHEME = "condition";
40
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050041 /** @hide */
42 @IntDef({STATE_FALSE, STATE_TRUE, STATE_TRUE, STATE_ERROR})
43 @Retention(RetentionPolicy.SOURCE)
44 public @interface State {}
45
46 /**
47 * Indicates that Do Not Disturb should be turned off. Note that all Conditions from all
48 * {@link ConditionProviderService} providers must be off for Do Not Disturb to be turned off on
49 * the device.
50 */
John Spurlocke77bb362014-04-26 10:24:59 -040051 public static final int STATE_FALSE = 0;
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050052 /**
53 * Indicates that Do Not Disturb should be turned on.
54 */
John Spurlocke77bb362014-04-26 10:24:59 -040055 public static final int STATE_TRUE = 1;
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050056
57 @SystemApi
John Spurlocke77bb362014-04-26 10:24:59 -040058 public static final int STATE_UNKNOWN = 2;
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050059 @SystemApi
John Spurlocke77bb362014-04-26 10:24:59 -040060 public static final int STATE_ERROR = 3;
61
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050062 @SystemApi
John Spurlock7340fc82014-04-24 18:50:12 -040063 public static final int FLAG_RELEVANT_NOW = 1 << 0;
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050064 @SystemApi
John Spurlock7340fc82014-04-24 18:50:12 -040065 public static final int FLAG_RELEVANT_ALWAYS = 1 << 1;
66
Julia Reynoldsa62496d2016-01-29 15:29:16 -050067 /**
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050068 * The URI representing the rule being updated.
Julia Reynoldsa62496d2016-01-29 15:29:16 -050069 * See {@link android.app.AutomaticZenRule#getConditionId()}.
70 */
John Spurlock7340fc82014-04-24 18:50:12 -040071 public final Uri id;
John Spurlock7340fc82014-04-24 18:50:12 -040072
Julia Reynoldsa62496d2016-01-29 15:29:16 -050073 /**
74 * A summary of what the rule encoded in {@link #id} means when it is enabled. User visible
75 * if the state of the condition is {@link #STATE_TRUE}.
76 */
77 public final String summary;
78
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050079 @SystemApi
Julia Reynoldsa62496d2016-01-29 15:29:16 -050080 public final String line1;
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050081 @SystemApi
Julia Reynoldsa62496d2016-01-29 15:29:16 -050082 public final String line2;
83
84 /**
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050085 * The state of this condition. {@link #STATE_TRUE} will enable Do Not Disturb mode.
86 * {@link #STATE_FALSE} will turn Do Not Disturb off for this rule. Note that Do Not Disturb
87 * might still be enabled globally if other conditions are in a {@link #STATE_TRUE} state.
Julia Reynoldsa62496d2016-01-29 15:29:16 -050088 */
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050089 @State
Julia Reynoldsa62496d2016-01-29 15:29:16 -050090 public final int state;
91
92 @SystemApi
93 public final int flags;
94 @SystemApi
95 public final int icon;
96
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050097 /**
98 * An object representing the current state of a {@link android.app.AutomaticZenRule}.
99 * @param id the {@link android.app.AutomaticZenRule#getConditionId()} of the zen rule
100 * @param summary a user visible description of the rule state.
101 */
102 public Condition(Uri id, String summary, int state) {
103 this(id, summary, "", "", -1, state, FLAG_RELEVANT_ALWAYS);
Julia Reynoldsa62496d2016-01-29 15:29:16 -0500104 }
105
106 @SystemApi
John Spurlockef5693b2014-05-02 00:07:35 -0400107 public Condition(Uri id, String summary, String line1, String line2, int icon,
108 int state, int flags) {
John Spurlock7340fc82014-04-24 18:50:12 -0400109 if (id == null) throw new IllegalArgumentException("id is required");
John Spurlockef5693b2014-05-02 00:07:35 -0400110 if (summary == null) throw new IllegalArgumentException("summary is required");
John Spurlocke77bb362014-04-26 10:24:59 -0400111 if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state);
John Spurlock7340fc82014-04-24 18:50:12 -0400112 this.id = id;
John Spurlockef5693b2014-05-02 00:07:35 -0400113 this.summary = summary;
114 this.line1 = line1;
115 this.line2 = line2;
116 this.icon = icon;
John Spurlock7340fc82014-04-24 18:50:12 -0400117 this.state = state;
118 this.flags = flags;
119 }
120
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500121 public Condition(Parcel source) {
John Spurlocke77bb362014-04-26 10:24:59 -0400122 this((Uri)source.readParcelable(Condition.class.getClassLoader()),
123 source.readString(),
John Spurlockef5693b2014-05-02 00:07:35 -0400124 source.readString(),
125 source.readString(),
126 source.readInt(),
John Spurlocke77bb362014-04-26 10:24:59 -0400127 source.readInt(),
128 source.readInt());
129 }
130
131 private static boolean isValidState(int state) {
132 return state >= STATE_FALSE && state <= STATE_ERROR;
John Spurlock7340fc82014-04-24 18:50:12 -0400133 }
134
135 @Override
136 public void writeToParcel(Parcel dest, int flags) {
137 dest.writeParcelable(id, 0);
John Spurlockef5693b2014-05-02 00:07:35 -0400138 dest.writeString(summary);
139 dest.writeString(line1);
140 dest.writeString(line2);
141 dest.writeInt(icon);
John Spurlocke77bb362014-04-26 10:24:59 -0400142 dest.writeInt(state);
John Spurlock3b98b3f2014-05-01 09:08:48 -0400143 dest.writeInt(this.flags);
John Spurlock7340fc82014-04-24 18:50:12 -0400144 }
145
146 @Override
147 public String toString() {
148 return new StringBuilder(Condition.class.getSimpleName()).append('[')
149 .append("id=").append(id)
John Spurlockef5693b2014-05-02 00:07:35 -0400150 .append(",summary=").append(summary)
151 .append(",line1=").append(line1)
152 .append(",line2=").append(line2)
153 .append(",icon=").append(icon)
John Spurlocke77bb362014-04-26 10:24:59 -0400154 .append(",state=").append(stateToString(state))
John Spurlock7340fc82014-04-24 18:50:12 -0400155 .append(",flags=").append(flags)
156 .append(']').toString();
157 }
158
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500159 @SystemApi
John Spurlocke77bb362014-04-26 10:24:59 -0400160 public static String stateToString(int state) {
161 if (state == STATE_FALSE) return "STATE_FALSE";
162 if (state == STATE_TRUE) return "STATE_TRUE";
163 if (state == STATE_UNKNOWN) return "STATE_UNKNOWN";
164 if (state == STATE_ERROR) return "STATE_ERROR";
165 throw new IllegalArgumentException("state is invalid: " + state);
166 }
167
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500168 @SystemApi
John Spurlock3b98b3f2014-05-01 09:08:48 -0400169 public static String relevanceToString(int flags) {
170 final boolean now = (flags & FLAG_RELEVANT_NOW) != 0;
171 final boolean always = (flags & FLAG_RELEVANT_ALWAYS) != 0;
172 if (!now && !always) return "NONE";
173 if (now && always) return "NOW, ALWAYS";
174 return now ? "NOW" : "ALWAYS";
175 }
176
John Spurlock7340fc82014-04-24 18:50:12 -0400177 @Override
178 public boolean equals(Object o) {
179 if (!(o instanceof Condition)) return false;
180 if (o == this) return true;
181 final Condition other = (Condition) o;
182 return Objects.equals(other.id, id)
John Spurlockef5693b2014-05-02 00:07:35 -0400183 && Objects.equals(other.summary, summary)
184 && Objects.equals(other.line1, line1)
185 && Objects.equals(other.line2, line2)
186 && other.icon == icon
John Spurlock7340fc82014-04-24 18:50:12 -0400187 && other.state == state
188 && other.flags == flags;
189 }
190
191 @Override
192 public int hashCode() {
John Spurlockef5693b2014-05-02 00:07:35 -0400193 return Objects.hash(id, summary, line1, line2, icon, state, flags);
John Spurlock7340fc82014-04-24 18:50:12 -0400194 }
195
196 @Override
197 public int describeContents() {
198 return 0;
199 }
200
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500201 @SystemApi
John Spurlock7340fc82014-04-24 18:50:12 -0400202 public Condition copy() {
203 final Parcel parcel = Parcel.obtain();
204 try {
205 writeToParcel(parcel, 0);
206 parcel.setDataPosition(0);
207 return new Condition(parcel);
208 } finally {
209 parcel.recycle();
210 }
211 }
212
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500213 @SystemApi
John Spurlocke77bb362014-04-26 10:24:59 -0400214 public static Uri.Builder newId(Context context) {
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500215 return new Uri.Builder()
216 .scheme(Condition.SCHEME)
217 .authority(context.getPackageName());
John Spurlocke77bb362014-04-26 10:24:59 -0400218 }
219
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500220 @SystemApi
John Spurlocke77bb362014-04-26 10:24:59 -0400221 public static boolean isValidId(Uri id, String pkg) {
John Spurlock3e077012014-11-29 13:22:21 -0500222 return id != null && SCHEME.equals(id.getScheme()) && pkg.equals(id.getAuthority());
John Spurlocke77bb362014-04-26 10:24:59 -0400223 }
224
John Spurlock7340fc82014-04-24 18:50:12 -0400225 public static final Parcelable.Creator<Condition> CREATOR
226 = new Parcelable.Creator<Condition>() {
227 @Override
228 public Condition createFromParcel(Parcel source) {
229 return new Condition(source);
230 }
231
232 @Override
233 public Condition[] newArray(int size) {
234 return new Condition[size];
235 }
236 };
237}