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; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 20 | import android.content.Context; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 21 | import android.net.Uri; |
| 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Kweku Adams | 9954633 | 2018-01-24 17:03:50 -0800 | [diff] [blame] | 24 | import android.util.proto.ProtoOutputStream; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 25 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 26 | import java.lang.annotation.Retention; |
| 27 | import java.lang.annotation.RetentionPolicy; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 28 | import java.util.Objects; |
| 29 | |
| 30 | /** |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 31 | * The current condition of an {@link android.app.AutomaticZenRule}, provided by the |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 32 | * app that owns the rule. Used to tell the system to enter Do Not |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 33 | * Disturb mode and request that the system exit Do Not Disturb mode. |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 34 | */ |
Julia Reynolds | ad41a6f | 2016-05-17 16:55:18 -0400 | [diff] [blame] | 35 | public final class Condition implements Parcelable { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 36 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 37 | public static final String SCHEME = "condition"; |
| 38 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 39 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 40 | @IntDef(prefix = { "STATE_" }, value = { |
| 41 | STATE_FALSE, |
| 42 | STATE_TRUE, |
| 43 | STATE_UNKNOWN, |
| 44 | STATE_ERROR |
| 45 | }) |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 46 | @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 Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 51 | * {@link android.app.AutomaticZenRule} providers must be off for Do Not Disturb to be turned |
| 52 | * off on the device. |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 53 | */ |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 54 | public static final int STATE_FALSE = 0; |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 55 | /** |
| 56 | * Indicates that Do Not Disturb should be turned on. |
| 57 | */ |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 58 | public static final int STATE_TRUE = 1; |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 59 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 60 | public static final int STATE_UNKNOWN = 2; |
| 61 | public static final int STATE_ERROR = 3; |
| 62 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 63 | public static final int FLAG_RELEVANT_NOW = 1 << 0; |
| 64 | public static final int FLAG_RELEVANT_ALWAYS = 1 << 1; |
| 65 | |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 66 | /** |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 67 | * The URI representing the rule being updated. |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 68 | * See {@link android.app.AutomaticZenRule#getConditionId()}. |
| 69 | */ |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 70 | public final Uri id; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 71 | |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 72 | /** |
| 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 Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 78 | public final String line1; |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 79 | public final String line2; |
| 80 | |
| 81 | /** |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 82 | * 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 Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 85 | */ |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 86 | @State |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 87 | public final int state; |
| 88 | |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 89 | public final int flags; |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 90 | public final int icon; |
| 91 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 92 | /** |
| 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 Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 99 | } |
| 100 | |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 101 | public Condition(Uri id, String summary, String line1, String line2, int icon, |
| 102 | int state, int flags) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 103 | if (id == null) throw new IllegalArgumentException("id is required"); |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 104 | if (summary == null) throw new IllegalArgumentException("summary is required"); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 105 | if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 106 | this.id = id; |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 107 | this.summary = summary; |
| 108 | this.line1 = line1; |
| 109 | this.line2 = line2; |
| 110 | this.icon = icon; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 111 | this.state = state; |
| 112 | this.flags = flags; |
| 113 | } |
| 114 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 115 | public Condition(Parcel source) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 116 | this((Uri)source.readParcelable(Condition.class.getClassLoader()), |
| 117 | source.readString(), |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 118 | source.readString(), |
| 119 | source.readString(), |
| 120 | source.readInt(), |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 121 | source.readInt(), |
| 122 | source.readInt()); |
| 123 | } |
| 124 | |
| 125 | private static boolean isValidState(int state) { |
| 126 | return state >= STATE_FALSE && state <= STATE_ERROR; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void writeToParcel(Parcel dest, int flags) { |
| 131 | dest.writeParcelable(id, 0); |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 132 | dest.writeString(summary); |
| 133 | dest.writeString(line1); |
| 134 | dest.writeString(line2); |
| 135 | dest.writeInt(icon); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 136 | dest.writeInt(state); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 137 | dest.writeInt(this.flags); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public String toString() { |
| 142 | return new StringBuilder(Condition.class.getSimpleName()).append('[') |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 143 | .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 Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 151 | } |
| 152 | |
Kweku Adams | 9954633 | 2018-01-24 17:03:50 -0800 | [diff] [blame] | 153 | /** @hide */ |
| 154 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 155 | final long token = proto.start(fieldId); |
| 156 | |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 157 | // id is guaranteed not to be null. |
Kweku Adams | 9954633 | 2018-01-24 17:03:50 -0800 | [diff] [blame] | 158 | 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 Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 169 | 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 Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 177 | 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 Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 185 | @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 Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 191 | && Objects.equals(other.summary, summary) |
| 192 | && Objects.equals(other.line1, line1) |
| 193 | && Objects.equals(other.line2, line2) |
| 194 | && other.icon == icon |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 195 | && other.state == state |
| 196 | && other.flags == flags; |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public int hashCode() { |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 201 | return Objects.hash(id, summary, line1, line2, icon, state, flags); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 202 | } |
| 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 Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 220 | public static Uri.Builder newId(Context context) { |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 221 | return new Uri.Builder() |
| 222 | .scheme(Condition.SCHEME) |
| 223 | .authority(context.getPackageName()); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | public static boolean isValidId(Uri id, String pkg) { |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 227 | return id != null && SCHEME.equals(id.getScheme()) && pkg.equals(id.getAuthority()); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 230 | public static final @android.annotation.NonNull Parcelable.Creator<Condition> CREATOR |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 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 | } |