blob: b6c6bdc00bbe6d02cfb4faef1f8afe6af84d2dcc [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;
John Spurlocke77bb362014-04-26 10:24:59 -040021import android.content.Context;
John Spurlock7340fc82014-04-24 18:50:12 -040022import android.net.Uri;
23import android.os.Parcel;
24import android.os.Parcelable;
Kweku Adams99546332018-01-24 17:03:50 -080025import android.util.proto.ProtoOutputStream;
John Spurlock7340fc82014-04-24 18:50:12 -040026
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 */
Julia Reynoldsad41a6f2016-05-17 16:55:18 -040036public final class Condition implements Parcelable {
John Spurlock7340fc82014-04-24 18:50:12 -040037
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 */
Jeff Sharkeyce8db992017-12-13 20:05:05 -070042 @IntDef(prefix = { "STATE_" }, value = {
43 STATE_FALSE,
44 STATE_TRUE,
45 STATE_UNKNOWN,
46 STATE_ERROR
47 })
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050048 @Retention(RetentionPolicy.SOURCE)
49 public @interface State {}
50
51 /**
52 * Indicates that Do Not Disturb should be turned off. Note that all Conditions from all
53 * {@link ConditionProviderService} providers must be off for Do Not Disturb to be turned off on
54 * the device.
55 */
John Spurlocke77bb362014-04-26 10:24:59 -040056 public static final int STATE_FALSE = 0;
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050057 /**
58 * Indicates that Do Not Disturb should be turned on.
59 */
John Spurlocke77bb362014-04-26 10:24:59 -040060 public static final int STATE_TRUE = 1;
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050061
62 @SystemApi
John Spurlocke77bb362014-04-26 10:24:59 -040063 public static final int STATE_UNKNOWN = 2;
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050064 @SystemApi
John Spurlocke77bb362014-04-26 10:24:59 -040065 public static final int STATE_ERROR = 3;
66
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050067 @SystemApi
John Spurlock7340fc82014-04-24 18:50:12 -040068 public static final int FLAG_RELEVANT_NOW = 1 << 0;
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050069 @SystemApi
John Spurlock7340fc82014-04-24 18:50:12 -040070 public static final int FLAG_RELEVANT_ALWAYS = 1 << 1;
71
Julia Reynoldsa62496d2016-01-29 15:29:16 -050072 /**
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050073 * The URI representing the rule being updated.
Julia Reynoldsa62496d2016-01-29 15:29:16 -050074 * See {@link android.app.AutomaticZenRule#getConditionId()}.
75 */
John Spurlock7340fc82014-04-24 18:50:12 -040076 public final Uri id;
John Spurlock7340fc82014-04-24 18:50:12 -040077
Julia Reynoldsa62496d2016-01-29 15:29:16 -050078 /**
79 * A summary of what the rule encoded in {@link #id} means when it is enabled. User visible
80 * if the state of the condition is {@link #STATE_TRUE}.
81 */
82 public final String summary;
83
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050084 @SystemApi
Julia Reynoldsa62496d2016-01-29 15:29:16 -050085 public final String line1;
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050086 @SystemApi
Julia Reynoldsa62496d2016-01-29 15:29:16 -050087 public final String line2;
88
89 /**
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050090 * The state of this condition. {@link #STATE_TRUE} will enable Do Not Disturb mode.
91 * {@link #STATE_FALSE} will turn Do Not Disturb off for this rule. Note that Do Not Disturb
92 * might still be enabled globally if other conditions are in a {@link #STATE_TRUE} state.
Julia Reynoldsa62496d2016-01-29 15:29:16 -050093 */
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050094 @State
Julia Reynoldsa62496d2016-01-29 15:29:16 -050095 public final int state;
96
97 @SystemApi
98 public final int flags;
99 @SystemApi
100 public final int icon;
101
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500102 /**
103 * An object representing the current state of a {@link android.app.AutomaticZenRule}.
104 * @param id the {@link android.app.AutomaticZenRule#getConditionId()} of the zen rule
105 * @param summary a user visible description of the rule state.
106 */
107 public Condition(Uri id, String summary, int state) {
108 this(id, summary, "", "", -1, state, FLAG_RELEVANT_ALWAYS);
Julia Reynoldsa62496d2016-01-29 15:29:16 -0500109 }
110
111 @SystemApi
John Spurlockef5693b2014-05-02 00:07:35 -0400112 public Condition(Uri id, String summary, String line1, String line2, int icon,
113 int state, int flags) {
John Spurlock7340fc82014-04-24 18:50:12 -0400114 if (id == null) throw new IllegalArgumentException("id is required");
John Spurlockef5693b2014-05-02 00:07:35 -0400115 if (summary == null) throw new IllegalArgumentException("summary is required");
John Spurlocke77bb362014-04-26 10:24:59 -0400116 if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state);
John Spurlock7340fc82014-04-24 18:50:12 -0400117 this.id = id;
John Spurlockef5693b2014-05-02 00:07:35 -0400118 this.summary = summary;
119 this.line1 = line1;
120 this.line2 = line2;
121 this.icon = icon;
John Spurlock7340fc82014-04-24 18:50:12 -0400122 this.state = state;
123 this.flags = flags;
124 }
125
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500126 public Condition(Parcel source) {
John Spurlocke77bb362014-04-26 10:24:59 -0400127 this((Uri)source.readParcelable(Condition.class.getClassLoader()),
128 source.readString(),
John Spurlockef5693b2014-05-02 00:07:35 -0400129 source.readString(),
130 source.readString(),
131 source.readInt(),
John Spurlocke77bb362014-04-26 10:24:59 -0400132 source.readInt(),
133 source.readInt());
134 }
135
136 private static boolean isValidState(int state) {
137 return state >= STATE_FALSE && state <= STATE_ERROR;
John Spurlock7340fc82014-04-24 18:50:12 -0400138 }
139
140 @Override
141 public void writeToParcel(Parcel dest, int flags) {
142 dest.writeParcelable(id, 0);
John Spurlockef5693b2014-05-02 00:07:35 -0400143 dest.writeString(summary);
144 dest.writeString(line1);
145 dest.writeString(line2);
146 dest.writeInt(icon);
John Spurlocke77bb362014-04-26 10:24:59 -0400147 dest.writeInt(state);
John Spurlock3b98b3f2014-05-01 09:08:48 -0400148 dest.writeInt(this.flags);
John Spurlock7340fc82014-04-24 18:50:12 -0400149 }
150
151 @Override
152 public String toString() {
153 return new StringBuilder(Condition.class.getSimpleName()).append('[')
154 .append("id=").append(id)
John Spurlockef5693b2014-05-02 00:07:35 -0400155 .append(",summary=").append(summary)
156 .append(",line1=").append(line1)
157 .append(",line2=").append(line2)
158 .append(",icon=").append(icon)
John Spurlocke77bb362014-04-26 10:24:59 -0400159 .append(",state=").append(stateToString(state))
John Spurlock7340fc82014-04-24 18:50:12 -0400160 .append(",flags=").append(flags)
161 .append(']').toString();
162 }
163
Kweku Adams99546332018-01-24 17:03:50 -0800164 /** @hide */
165 public void writeToProto(ProtoOutputStream proto, long fieldId) {
166 final long token = proto.start(fieldId);
167
168 // id is guarantreed not to be null.
169 proto.write(ConditionProto.ID, id.toString());
170 proto.write(ConditionProto.SUMMARY, summary);
171 proto.write(ConditionProto.LINE_1, line1);
172 proto.write(ConditionProto.LINE_2, line2);
173 proto.write(ConditionProto.ICON, icon);
174 proto.write(ConditionProto.STATE, state);
175 proto.write(ConditionProto.FLAGS, flags);
176
177 proto.end(token);
178 }
179
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500180 @SystemApi
John Spurlocke77bb362014-04-26 10:24:59 -0400181 public static String stateToString(int state) {
182 if (state == STATE_FALSE) return "STATE_FALSE";
183 if (state == STATE_TRUE) return "STATE_TRUE";
184 if (state == STATE_UNKNOWN) return "STATE_UNKNOWN";
185 if (state == STATE_ERROR) return "STATE_ERROR";
186 throw new IllegalArgumentException("state is invalid: " + state);
187 }
188
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500189 @SystemApi
John Spurlock3b98b3f2014-05-01 09:08:48 -0400190 public static String relevanceToString(int flags) {
191 final boolean now = (flags & FLAG_RELEVANT_NOW) != 0;
192 final boolean always = (flags & FLAG_RELEVANT_ALWAYS) != 0;
193 if (!now && !always) return "NONE";
194 if (now && always) return "NOW, ALWAYS";
195 return now ? "NOW" : "ALWAYS";
196 }
197
John Spurlock7340fc82014-04-24 18:50:12 -0400198 @Override
199 public boolean equals(Object o) {
200 if (!(o instanceof Condition)) return false;
201 if (o == this) return true;
202 final Condition other = (Condition) o;
203 return Objects.equals(other.id, id)
John Spurlockef5693b2014-05-02 00:07:35 -0400204 && Objects.equals(other.summary, summary)
205 && Objects.equals(other.line1, line1)
206 && Objects.equals(other.line2, line2)
207 && other.icon == icon
John Spurlock7340fc82014-04-24 18:50:12 -0400208 && other.state == state
209 && other.flags == flags;
210 }
211
212 @Override
213 public int hashCode() {
John Spurlockef5693b2014-05-02 00:07:35 -0400214 return Objects.hash(id, summary, line1, line2, icon, state, flags);
John Spurlock7340fc82014-04-24 18:50:12 -0400215 }
216
217 @Override
218 public int describeContents() {
219 return 0;
220 }
221
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500222 @SystemApi
John Spurlock7340fc82014-04-24 18:50:12 -0400223 public Condition copy() {
224 final Parcel parcel = Parcel.obtain();
225 try {
226 writeToParcel(parcel, 0);
227 parcel.setDataPosition(0);
228 return new Condition(parcel);
229 } finally {
230 parcel.recycle();
231 }
232 }
233
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500234 @SystemApi
John Spurlocke77bb362014-04-26 10:24:59 -0400235 public static Uri.Builder newId(Context context) {
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500236 return new Uri.Builder()
237 .scheme(Condition.SCHEME)
238 .authority(context.getPackageName());
John Spurlocke77bb362014-04-26 10:24:59 -0400239 }
240
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500241 @SystemApi
John Spurlocke77bb362014-04-26 10:24:59 -0400242 public static boolean isValidId(Uri id, String pkg) {
John Spurlock3e077012014-11-29 13:22:21 -0500243 return id != null && SCHEME.equals(id.getScheme()) && pkg.equals(id.getAuthority());
John Spurlocke77bb362014-04-26 10:24:59 -0400244 }
245
John Spurlock7340fc82014-04-24 18:50:12 -0400246 public static final Parcelable.Creator<Condition> CREATOR
247 = new Parcelable.Creator<Condition>() {
248 @Override
249 public Condition createFromParcel(Parcel source) {
250 return new Condition(source);
251 }
252
253 @Override
254 public Condition[] newArray(int size) {
255 return new Condition[size];
256 }
257 };
258}