blob: 30d98045f3f40ed9299d9b81480d8e257eb2a689 [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;
John Spurlocke77bb362014-04-26 10:24:59 -040020import android.content.Context;
John Spurlock7340fc82014-04-24 18:50:12 -040021import android.net.Uri;
22import android.os.Parcel;
23import android.os.Parcelable;
Kweku Adams99546332018-01-24 17:03:50 -080024import android.util.proto.ProtoOutputStream;
John Spurlock7340fc82014-04-24 18:50:12 -040025
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050026import java.lang.annotation.Retention;
27import java.lang.annotation.RetentionPolicy;
John Spurlock7340fc82014-04-24 18:50:12 -040028import java.util.Objects;
29
30/**
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050031 * The current condition of an {@link android.app.AutomaticZenRule}, provided by the
Julia Reynolds68062072018-08-06 15:38:21 -040032 * app that owns the rule. Used to tell the system to enter Do Not
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050033 * Disturb mode and request that the system exit Do Not Disturb mode.
John Spurlock7340fc82014-04-24 18:50:12 -040034 */
Julia Reynoldsad41a6f2016-05-17 16:55:18 -040035public final class Condition implements Parcelable {
John Spurlock7340fc82014-04-24 18:50:12 -040036
John Spurlocke77bb362014-04-26 10:24:59 -040037 public static final String SCHEME = "condition";
38
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050039 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -070040 @IntDef(prefix = { "STATE_" }, value = {
41 STATE_FALSE,
42 STATE_TRUE,
43 STATE_UNKNOWN,
44 STATE_ERROR
45 })
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050046 @Retention(RetentionPolicy.SOURCE)
47 public @interface State {}
48
49 /**
50 * Indicates that Do Not Disturb should be turned off. Note that all Conditions from all
Julia Reynolds68062072018-08-06 15:38:21 -040051 * {@link android.app.AutomaticZenRule} providers must be off for Do Not Disturb to be turned
52 * off on the device.
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050053 */
John Spurlocke77bb362014-04-26 10:24:59 -040054 public static final int STATE_FALSE = 0;
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050055 /**
56 * Indicates that Do Not Disturb should be turned on.
57 */
John Spurlocke77bb362014-04-26 10:24:59 -040058 public static final int STATE_TRUE = 1;
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050059
John Spurlocke77bb362014-04-26 10:24:59 -040060 public static final int STATE_UNKNOWN = 2;
61 public static final int STATE_ERROR = 3;
62
John Spurlock7340fc82014-04-24 18:50:12 -040063 public static final int FLAG_RELEVANT_NOW = 1 << 0;
64 public static final int FLAG_RELEVANT_ALWAYS = 1 << 1;
65
Julia Reynoldsa62496d2016-01-29 15:29:16 -050066 /**
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050067 * The URI representing the rule being updated.
Julia Reynoldsa62496d2016-01-29 15:29:16 -050068 * See {@link android.app.AutomaticZenRule#getConditionId()}.
69 */
John Spurlock7340fc82014-04-24 18:50:12 -040070 public final Uri id;
John Spurlock7340fc82014-04-24 18:50:12 -040071
Julia Reynoldsa62496d2016-01-29 15:29:16 -050072 /**
73 * A summary of what the rule encoded in {@link #id} means when it is enabled. User visible
74 * if the state of the condition is {@link #STATE_TRUE}.
75 */
76 public final String summary;
77
Julia Reynoldsa62496d2016-01-29 15:29:16 -050078 public final String line1;
Julia Reynoldsa62496d2016-01-29 15:29:16 -050079 public final String line2;
80
81 /**
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050082 * The state of this condition. {@link #STATE_TRUE} will enable Do Not Disturb mode.
83 * {@link #STATE_FALSE} will turn Do Not Disturb off for this rule. Note that Do Not Disturb
84 * might still be enabled globally if other conditions are in a {@link #STATE_TRUE} state.
Julia Reynoldsa62496d2016-01-29 15:29:16 -050085 */
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050086 @State
Julia Reynoldsa62496d2016-01-29 15:29:16 -050087 public final int state;
88
Julia Reynoldsa62496d2016-01-29 15:29:16 -050089 public final int flags;
Julia Reynoldsa62496d2016-01-29 15:29:16 -050090 public final int icon;
91
Julia Reynolds1d6d16d2016-03-07 13:51:02 -050092 /**
93 * An object representing the current state of a {@link android.app.AutomaticZenRule}.
94 * @param id the {@link android.app.AutomaticZenRule#getConditionId()} of the zen rule
95 * @param summary a user visible description of the rule state.
96 */
97 public Condition(Uri id, String summary, int state) {
98 this(id, summary, "", "", -1, state, FLAG_RELEVANT_ALWAYS);
Julia Reynoldsa62496d2016-01-29 15:29:16 -050099 }
100
John Spurlockef5693b2014-05-02 00:07:35 -0400101 public Condition(Uri id, String summary, String line1, String line2, int icon,
102 int state, int flags) {
John Spurlock7340fc82014-04-24 18:50:12 -0400103 if (id == null) throw new IllegalArgumentException("id is required");
John Spurlockef5693b2014-05-02 00:07:35 -0400104 if (summary == null) throw new IllegalArgumentException("summary is required");
John Spurlocke77bb362014-04-26 10:24:59 -0400105 if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state);
John Spurlock7340fc82014-04-24 18:50:12 -0400106 this.id = id;
John Spurlockef5693b2014-05-02 00:07:35 -0400107 this.summary = summary;
108 this.line1 = line1;
109 this.line2 = line2;
110 this.icon = icon;
John Spurlock7340fc82014-04-24 18:50:12 -0400111 this.state = state;
112 this.flags = flags;
113 }
114
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500115 public Condition(Parcel source) {
John Spurlocke77bb362014-04-26 10:24:59 -0400116 this((Uri)source.readParcelable(Condition.class.getClassLoader()),
117 source.readString(),
John Spurlockef5693b2014-05-02 00:07:35 -0400118 source.readString(),
119 source.readString(),
120 source.readInt(),
John Spurlocke77bb362014-04-26 10:24:59 -0400121 source.readInt(),
122 source.readInt());
123 }
124
125 private static boolean isValidState(int state) {
126 return state >= STATE_FALSE && state <= STATE_ERROR;
John Spurlock7340fc82014-04-24 18:50:12 -0400127 }
128
129 @Override
130 public void writeToParcel(Parcel dest, int flags) {
131 dest.writeParcelable(id, 0);
John Spurlockef5693b2014-05-02 00:07:35 -0400132 dest.writeString(summary);
133 dest.writeString(line1);
134 dest.writeString(line2);
135 dest.writeInt(icon);
John Spurlocke77bb362014-04-26 10:24:59 -0400136 dest.writeInt(state);
John Spurlock3b98b3f2014-05-01 09:08:48 -0400137 dest.writeInt(this.flags);
John Spurlock7340fc82014-04-24 18:50:12 -0400138 }
139
140 @Override
141 public String toString() {
142 return new StringBuilder(Condition.class.getSimpleName()).append('[')
Julia Reynolds8f056002018-07-13 15:12:29 -0400143 .append("state=").append(stateToString(state))
144 .append(",id=").append(id)
145 .append(",summary=").append(summary)
146 .append(",line1=").append(line1)
147 .append(",line2=").append(line2)
148 .append(",icon=").append(icon)
149 .append(",flags=").append(flags)
150 .append(']').toString();
John Spurlock7340fc82014-04-24 18:50:12 -0400151 }
152
Kweku Adams99546332018-01-24 17:03:50 -0800153 /** @hide */
154 public void writeToProto(ProtoOutputStream proto, long fieldId) {
155 final long token = proto.start(fieldId);
156
Julia Reynolds68062072018-08-06 15:38:21 -0400157 // id is guaranteed not to be null.
Kweku Adams99546332018-01-24 17:03:50 -0800158 proto.write(ConditionProto.ID, id.toString());
159 proto.write(ConditionProto.SUMMARY, summary);
160 proto.write(ConditionProto.LINE_1, line1);
161 proto.write(ConditionProto.LINE_2, line2);
162 proto.write(ConditionProto.ICON, icon);
163 proto.write(ConditionProto.STATE, state);
164 proto.write(ConditionProto.FLAGS, flags);
165
166 proto.end(token);
167 }
168
John Spurlocke77bb362014-04-26 10:24:59 -0400169 public static String stateToString(int state) {
170 if (state == STATE_FALSE) return "STATE_FALSE";
171 if (state == STATE_TRUE) return "STATE_TRUE";
172 if (state == STATE_UNKNOWN) return "STATE_UNKNOWN";
173 if (state == STATE_ERROR) return "STATE_ERROR";
174 throw new IllegalArgumentException("state is invalid: " + state);
175 }
176
John Spurlock3b98b3f2014-05-01 09:08:48 -0400177 public static String relevanceToString(int flags) {
178 final boolean now = (flags & FLAG_RELEVANT_NOW) != 0;
179 final boolean always = (flags & FLAG_RELEVANT_ALWAYS) != 0;
180 if (!now && !always) return "NONE";
181 if (now && always) return "NOW, ALWAYS";
182 return now ? "NOW" : "ALWAYS";
183 }
184
John Spurlock7340fc82014-04-24 18:50:12 -0400185 @Override
186 public boolean equals(Object o) {
187 if (!(o instanceof Condition)) return false;
188 if (o == this) return true;
189 final Condition other = (Condition) o;
190 return Objects.equals(other.id, id)
John Spurlockef5693b2014-05-02 00:07:35 -0400191 && Objects.equals(other.summary, summary)
192 && Objects.equals(other.line1, line1)
193 && Objects.equals(other.line2, line2)
194 && other.icon == icon
John Spurlock7340fc82014-04-24 18:50:12 -0400195 && other.state == state
196 && other.flags == flags;
197 }
198
199 @Override
200 public int hashCode() {
John Spurlockef5693b2014-05-02 00:07:35 -0400201 return Objects.hash(id, summary, line1, line2, icon, state, flags);
John Spurlock7340fc82014-04-24 18:50:12 -0400202 }
203
204 @Override
205 public int describeContents() {
206 return 0;
207 }
208
209 public Condition copy() {
210 final Parcel parcel = Parcel.obtain();
211 try {
212 writeToParcel(parcel, 0);
213 parcel.setDataPosition(0);
214 return new Condition(parcel);
215 } finally {
216 parcel.recycle();
217 }
218 }
219
John Spurlocke77bb362014-04-26 10:24:59 -0400220 public static Uri.Builder newId(Context context) {
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500221 return new Uri.Builder()
222 .scheme(Condition.SCHEME)
223 .authority(context.getPackageName());
John Spurlocke77bb362014-04-26 10:24:59 -0400224 }
225
226 public static boolean isValidId(Uri id, String pkg) {
John Spurlock3e077012014-11-29 13:22:21 -0500227 return id != null && SCHEME.equals(id.getScheme()) && pkg.equals(id.getAuthority());
John Spurlocke77bb362014-04-26 10:24:59 -0400228 }
229
John Spurlock7340fc82014-04-24 18:50:12 -0400230 public static final Parcelable.Creator<Condition> CREATOR
231 = new Parcelable.Creator<Condition>() {
232 @Override
233 public Condition createFromParcel(Parcel source) {
234 return new Condition(source);
235 }
236
237 @Override
238 public Condition[] newArray(int size) {
239 return new Condition[size];
240 }
241 };
242}