John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1 | /** |
| 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 | |
| 17 | package android.service.notification; |
| 18 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 20 | import android.annotation.SystemApi; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 21 | import android.content.Context; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 22 | import android.net.Uri; |
| 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Kweku Adams | 9954633 | 2018-01-24 17:03:50 -0800 | [diff] [blame] | 25 | import android.util.proto.ProtoOutputStream; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 26 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 27 | import java.lang.annotation.Retention; |
| 28 | import java.lang.annotation.RetentionPolicy; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 29 | import java.util.Objects; |
| 30 | |
| 31 | /** |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 32 | * 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 Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 35 | */ |
Julia Reynolds | ad41a6f | 2016-05-17 16:55:18 -0400 | [diff] [blame] | 36 | public final class Condition implements Parcelable { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 37 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 38 | @SystemApi |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 39 | public static final String SCHEME = "condition"; |
| 40 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 41 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 42 | @IntDef(prefix = { "STATE_" }, value = { |
| 43 | STATE_FALSE, |
| 44 | STATE_TRUE, |
| 45 | STATE_UNKNOWN, |
| 46 | STATE_ERROR |
| 47 | }) |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 48 | @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 Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 56 | public static final int STATE_FALSE = 0; |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 57 | /** |
| 58 | * Indicates that Do Not Disturb should be turned on. |
| 59 | */ |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 60 | public static final int STATE_TRUE = 1; |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 61 | |
| 62 | @SystemApi |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 63 | public static final int STATE_UNKNOWN = 2; |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 64 | @SystemApi |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 65 | public static final int STATE_ERROR = 3; |
| 66 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 67 | @SystemApi |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 68 | public static final int FLAG_RELEVANT_NOW = 1 << 0; |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 69 | @SystemApi |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 70 | public static final int FLAG_RELEVANT_ALWAYS = 1 << 1; |
| 71 | |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 72 | /** |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 73 | * The URI representing the rule being updated. |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 74 | * See {@link android.app.AutomaticZenRule#getConditionId()}. |
| 75 | */ |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 76 | public final Uri id; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 77 | |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 78 | /** |
| 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 Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 84 | @SystemApi |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 85 | public final String line1; |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 86 | @SystemApi |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 87 | public final String line2; |
| 88 | |
| 89 | /** |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 90 | * 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 Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 93 | */ |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 94 | @State |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 95 | public final int state; |
| 96 | |
| 97 | @SystemApi |
| 98 | public final int flags; |
| 99 | @SystemApi |
| 100 | public final int icon; |
| 101 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 102 | /** |
| 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 Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @SystemApi |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 112 | public Condition(Uri id, String summary, String line1, String line2, int icon, |
| 113 | int state, int flags) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 114 | if (id == null) throw new IllegalArgumentException("id is required"); |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 115 | if (summary == null) throw new IllegalArgumentException("summary is required"); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 116 | if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 117 | this.id = id; |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 118 | this.summary = summary; |
| 119 | this.line1 = line1; |
| 120 | this.line2 = line2; |
| 121 | this.icon = icon; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 122 | this.state = state; |
| 123 | this.flags = flags; |
| 124 | } |
| 125 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 126 | public Condition(Parcel source) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 127 | this((Uri)source.readParcelable(Condition.class.getClassLoader()), |
| 128 | source.readString(), |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 129 | source.readString(), |
| 130 | source.readString(), |
| 131 | source.readInt(), |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 132 | source.readInt(), |
| 133 | source.readInt()); |
| 134 | } |
| 135 | |
| 136 | private static boolean isValidState(int state) { |
| 137 | return state >= STATE_FALSE && state <= STATE_ERROR; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void writeToParcel(Parcel dest, int flags) { |
| 142 | dest.writeParcelable(id, 0); |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 143 | dest.writeString(summary); |
| 144 | dest.writeString(line1); |
| 145 | dest.writeString(line2); |
| 146 | dest.writeInt(icon); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 147 | dest.writeInt(state); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 148 | dest.writeInt(this.flags); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public String toString() { |
| 153 | return new StringBuilder(Condition.class.getSimpleName()).append('[') |
| 154 | .append("id=").append(id) |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 155 | .append(",summary=").append(summary) |
| 156 | .append(",line1=").append(line1) |
| 157 | .append(",line2=").append(line2) |
| 158 | .append(",icon=").append(icon) |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 159 | .append(",state=").append(stateToString(state)) |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 160 | .append(",flags=").append(flags) |
| 161 | .append(']').toString(); |
| 162 | } |
| 163 | |
Kweku Adams | 9954633 | 2018-01-24 17:03:50 -0800 | [diff] [blame] | 164 | /** @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 Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 180 | @SystemApi |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 181 | 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 Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 189 | @SystemApi |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 190 | 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 Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 198 | @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 Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 204 | && Objects.equals(other.summary, summary) |
| 205 | && Objects.equals(other.line1, line1) |
| 206 | && Objects.equals(other.line2, line2) |
| 207 | && other.icon == icon |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 208 | && other.state == state |
| 209 | && other.flags == flags; |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public int hashCode() { |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 214 | return Objects.hash(id, summary, line1, line2, icon, state, flags); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public int describeContents() { |
| 219 | return 0; |
| 220 | } |
| 221 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 222 | @SystemApi |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 223 | 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 Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 234 | @SystemApi |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 235 | public static Uri.Builder newId(Context context) { |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 236 | return new Uri.Builder() |
| 237 | .scheme(Condition.SCHEME) |
| 238 | .authority(context.getPackageName()); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 241 | @SystemApi |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 242 | public static boolean isValidId(Uri id, String pkg) { |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 243 | return id != null && SCHEME.equals(id.getScheme()) && pkg.equals(id.getAuthority()); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 244 | } |
| 245 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 246 | 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 | } |