blob: 11737c65314ed8944bd8f8ae0b2b64f2f6bf4648 [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 Reynoldsa62496d2016-01-29 15:29:16 -050019import android.annotation.SystemApi;
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;
24
25import java.util.Objects;
26
27/**
Julia Reynoldsa62496d2016-01-29 15:29:16 -050028 * Condition information from condition providers. Used to tell the system to enter Do Not Disturb
29 * mode and request that the system exit Do Not Disturb mode.
John Spurlock7340fc82014-04-24 18:50:12 -040030 */
31public class Condition implements Parcelable {
32
John Spurlocke77bb362014-04-26 10:24:59 -040033 public static final String SCHEME = "condition";
34
35 public static final int STATE_FALSE = 0;
36 public static final int STATE_TRUE = 1;
37 public static final int STATE_UNKNOWN = 2;
38 public static final int STATE_ERROR = 3;
39
John Spurlock7340fc82014-04-24 18:50:12 -040040 public static final int FLAG_RELEVANT_NOW = 1 << 0;
41 public static final int FLAG_RELEVANT_ALWAYS = 1 << 1;
42
Julia Reynoldsa62496d2016-01-29 15:29:16 -050043 /**
44 * The URI representing the condition being updated.
45 * See {@link android.app.AutomaticZenRule#getConditionId()}.
46 */
John Spurlock7340fc82014-04-24 18:50:12 -040047 public final Uri id;
John Spurlock7340fc82014-04-24 18:50:12 -040048
Julia Reynoldsa62496d2016-01-29 15:29:16 -050049 /**
50 * A summary of what the rule encoded in {@link #id} means when it is enabled. User visible
51 * if the state of the condition is {@link #STATE_TRUE}.
52 */
53 public final String summary;
54
55 /**
56 * Additional information about what the rule encoded in {@link #id} means when it is enabled.
57 * User visible if the state of the condition is {@link #STATE_TRUE}.
58 */
59 public final String line1;
60
61 /**
62 * Additional information about what the rule encoded in {@link #id} means when it is enabled.
63 * User visible if the state of the condition is {@link #STATE_TRUE}.
64 */
65 public final String line2;
66
67 /**
68 * The state of this condition. {@link #STATE_TRUE} will enable Do Not Disturb mode. Any other
69 * state will turn Do Not Disturb off for this rule. Note that Do Not Disturb might still be
70 * enabled globally if other conditions are in a {@link #STATE_TRUE} state.
71 */
72 public final int state;
73
74 @SystemApi
75 public final int flags;
76 @SystemApi
77 public final int icon;
78
79 public Condition(Uri id, String summary, String line1, String line2, int state) {
80 this(id, summary, line1, line2, -1, state, FLAG_RELEVANT_ALWAYS);
81 }
82
83 @SystemApi
John Spurlockef5693b2014-05-02 00:07:35 -040084 public Condition(Uri id, String summary, String line1, String line2, int icon,
85 int state, int flags) {
John Spurlock7340fc82014-04-24 18:50:12 -040086 if (id == null) throw new IllegalArgumentException("id is required");
John Spurlockef5693b2014-05-02 00:07:35 -040087 if (summary == null) throw new IllegalArgumentException("summary is required");
88 if (line1 == null) throw new IllegalArgumentException("line1 is required");
89 if (line2 == null) throw new IllegalArgumentException("line2 is required");
John Spurlocke77bb362014-04-26 10:24:59 -040090 if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state);
John Spurlock7340fc82014-04-24 18:50:12 -040091 this.id = id;
John Spurlockef5693b2014-05-02 00:07:35 -040092 this.summary = summary;
93 this.line1 = line1;
94 this.line2 = line2;
95 this.icon = icon;
John Spurlock7340fc82014-04-24 18:50:12 -040096 this.state = state;
97 this.flags = flags;
98 }
99
100 private Condition(Parcel source) {
John Spurlocke77bb362014-04-26 10:24:59 -0400101 this((Uri)source.readParcelable(Condition.class.getClassLoader()),
102 source.readString(),
John Spurlockef5693b2014-05-02 00:07:35 -0400103 source.readString(),
104 source.readString(),
105 source.readInt(),
John Spurlocke77bb362014-04-26 10:24:59 -0400106 source.readInt(),
107 source.readInt());
108 }
109
110 private static boolean isValidState(int state) {
111 return state >= STATE_FALSE && state <= STATE_ERROR;
John Spurlock7340fc82014-04-24 18:50:12 -0400112 }
113
114 @Override
115 public void writeToParcel(Parcel dest, int flags) {
116 dest.writeParcelable(id, 0);
John Spurlockef5693b2014-05-02 00:07:35 -0400117 dest.writeString(summary);
118 dest.writeString(line1);
119 dest.writeString(line2);
120 dest.writeInt(icon);
John Spurlocke77bb362014-04-26 10:24:59 -0400121 dest.writeInt(state);
John Spurlock3b98b3f2014-05-01 09:08:48 -0400122 dest.writeInt(this.flags);
John Spurlock7340fc82014-04-24 18:50:12 -0400123 }
124
125 @Override
126 public String toString() {
127 return new StringBuilder(Condition.class.getSimpleName()).append('[')
128 .append("id=").append(id)
John Spurlockef5693b2014-05-02 00:07:35 -0400129 .append(",summary=").append(summary)
130 .append(",line1=").append(line1)
131 .append(",line2=").append(line2)
132 .append(",icon=").append(icon)
John Spurlocke77bb362014-04-26 10:24:59 -0400133 .append(",state=").append(stateToString(state))
John Spurlock7340fc82014-04-24 18:50:12 -0400134 .append(",flags=").append(flags)
135 .append(']').toString();
136 }
137
John Spurlocke77bb362014-04-26 10:24:59 -0400138 public static String stateToString(int state) {
139 if (state == STATE_FALSE) return "STATE_FALSE";
140 if (state == STATE_TRUE) return "STATE_TRUE";
141 if (state == STATE_UNKNOWN) return "STATE_UNKNOWN";
142 if (state == STATE_ERROR) return "STATE_ERROR";
143 throw new IllegalArgumentException("state is invalid: " + state);
144 }
145
John Spurlock3b98b3f2014-05-01 09:08:48 -0400146 public static String relevanceToString(int flags) {
147 final boolean now = (flags & FLAG_RELEVANT_NOW) != 0;
148 final boolean always = (flags & FLAG_RELEVANT_ALWAYS) != 0;
149 if (!now && !always) return "NONE";
150 if (now && always) return "NOW, ALWAYS";
151 return now ? "NOW" : "ALWAYS";
152 }
153
John Spurlock7340fc82014-04-24 18:50:12 -0400154 @Override
155 public boolean equals(Object o) {
156 if (!(o instanceof Condition)) return false;
157 if (o == this) return true;
158 final Condition other = (Condition) o;
159 return Objects.equals(other.id, id)
John Spurlockef5693b2014-05-02 00:07:35 -0400160 && Objects.equals(other.summary, summary)
161 && Objects.equals(other.line1, line1)
162 && Objects.equals(other.line2, line2)
163 && other.icon == icon
John Spurlock7340fc82014-04-24 18:50:12 -0400164 && other.state == state
165 && other.flags == flags;
166 }
167
168 @Override
169 public int hashCode() {
John Spurlockef5693b2014-05-02 00:07:35 -0400170 return Objects.hash(id, summary, line1, line2, icon, state, flags);
John Spurlock7340fc82014-04-24 18:50:12 -0400171 }
172
173 @Override
174 public int describeContents() {
175 return 0;
176 }
177
178 public Condition copy() {
179 final Parcel parcel = Parcel.obtain();
180 try {
181 writeToParcel(parcel, 0);
182 parcel.setDataPosition(0);
183 return new Condition(parcel);
184 } finally {
185 parcel.recycle();
186 }
187 }
188
John Spurlocke77bb362014-04-26 10:24:59 -0400189 public static Uri.Builder newId(Context context) {
190 return new Uri.Builder().scheme(SCHEME).authority(context.getPackageName());
191 }
192
193 public static boolean isValidId(Uri id, String pkg) {
John Spurlock3e077012014-11-29 13:22:21 -0500194 return id != null && SCHEME.equals(id.getScheme()) && pkg.equals(id.getAuthority());
John Spurlocke77bb362014-04-26 10:24:59 -0400195 }
196
John Spurlock7340fc82014-04-24 18:50:12 -0400197 public static final Parcelable.Creator<Condition> CREATOR
198 = new Parcelable.Creator<Condition>() {
199 @Override
200 public Condition createFromParcel(Parcel source) {
201 return new Condition(source);
202 }
203
204 @Override
205 public Condition[] newArray(int size) {
206 return new Condition[size];
207 }
208 };
209}