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; |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 21 | import android.app.AutomaticZenRule; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 22 | import android.content.Context; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 23 | import android.net.Uri; |
| 24 | import android.os.Parcel; |
| 25 | import android.os.Parcelable; |
| 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 | */ |
| 36 | public class Condition implements Parcelable { |
| 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 */ |
| 42 | @IntDef({STATE_FALSE, STATE_TRUE, STATE_TRUE, STATE_ERROR}) |
| 43 | @Retention(RetentionPolicy.SOURCE) |
| 44 | public @interface State {} |
| 45 | |
| 46 | /** |
| 47 | * Indicates that Do Not Disturb should be turned off. Note that all Conditions from all |
| 48 | * {@link ConditionProviderService} providers must be off for Do Not Disturb to be turned off on |
| 49 | * the device. |
| 50 | */ |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 51 | public static final int STATE_FALSE = 0; |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 52 | /** |
| 53 | * Indicates that Do Not Disturb should be turned on. |
| 54 | */ |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 55 | public static final int STATE_TRUE = 1; |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 56 | |
| 57 | @SystemApi |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 58 | public static final int STATE_UNKNOWN = 2; |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 59 | @SystemApi |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 60 | public static final int STATE_ERROR = 3; |
| 61 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 62 | @SystemApi |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 63 | public static final int FLAG_RELEVANT_NOW = 1 << 0; |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 64 | @SystemApi |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 65 | public static final int FLAG_RELEVANT_ALWAYS = 1 << 1; |
| 66 | |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 67 | /** |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 68 | * The URI representing the rule being updated. |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 69 | * See {@link android.app.AutomaticZenRule#getConditionId()}. |
| 70 | */ |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 71 | public final Uri id; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 72 | |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 73 | /** |
| 74 | * A summary of what the rule encoded in {@link #id} means when it is enabled. User visible |
| 75 | * if the state of the condition is {@link #STATE_TRUE}. |
| 76 | */ |
| 77 | public final String summary; |
| 78 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 79 | @SystemApi |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 80 | public final String line1; |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 81 | @SystemApi |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 82 | public final String line2; |
| 83 | |
| 84 | /** |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 85 | * The state of this condition. {@link #STATE_TRUE} will enable Do Not Disturb mode. |
| 86 | * {@link #STATE_FALSE} will turn Do Not Disturb off for this rule. Note that Do Not Disturb |
| 87 | * 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] | 88 | */ |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 89 | @State |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 90 | public final int state; |
| 91 | |
| 92 | @SystemApi |
| 93 | public final int flags; |
| 94 | @SystemApi |
| 95 | public final int icon; |
| 96 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 97 | /** |
| 98 | * An object representing the current state of a {@link android.app.AutomaticZenRule}. |
| 99 | * @param id the {@link android.app.AutomaticZenRule#getConditionId()} of the zen rule |
| 100 | * @param summary a user visible description of the rule state. |
| 101 | */ |
| 102 | public Condition(Uri id, String summary, int state) { |
| 103 | this(id, summary, "", "", -1, state, FLAG_RELEVANT_ALWAYS); |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @SystemApi |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 107 | public Condition(Uri id, String summary, String line1, String line2, int icon, |
| 108 | int state, int flags) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 109 | if (id == null) throw new IllegalArgumentException("id is required"); |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 110 | if (summary == null) throw new IllegalArgumentException("summary is required"); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 111 | if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 112 | this.id = id; |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 113 | this.summary = summary; |
| 114 | this.line1 = line1; |
| 115 | this.line2 = line2; |
| 116 | this.icon = icon; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 117 | this.state = state; |
| 118 | this.flags = flags; |
| 119 | } |
| 120 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 121 | public Condition(Parcel source) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 122 | this((Uri)source.readParcelable(Condition.class.getClassLoader()), |
| 123 | source.readString(), |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 124 | source.readString(), |
| 125 | source.readString(), |
| 126 | source.readInt(), |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 127 | source.readInt(), |
| 128 | source.readInt()); |
| 129 | } |
| 130 | |
| 131 | private static boolean isValidState(int state) { |
| 132 | return state >= STATE_FALSE && state <= STATE_ERROR; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void writeToParcel(Parcel dest, int flags) { |
| 137 | dest.writeParcelable(id, 0); |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 138 | dest.writeString(summary); |
| 139 | dest.writeString(line1); |
| 140 | dest.writeString(line2); |
| 141 | dest.writeInt(icon); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 142 | dest.writeInt(state); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 143 | dest.writeInt(this.flags); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public String toString() { |
| 148 | return new StringBuilder(Condition.class.getSimpleName()).append('[') |
| 149 | .append("id=").append(id) |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 150 | .append(",summary=").append(summary) |
| 151 | .append(",line1=").append(line1) |
| 152 | .append(",line2=").append(line2) |
| 153 | .append(",icon=").append(icon) |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 154 | .append(",state=").append(stateToString(state)) |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 155 | .append(",flags=").append(flags) |
| 156 | .append(']').toString(); |
| 157 | } |
| 158 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 159 | @SystemApi |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 160 | public static String stateToString(int state) { |
| 161 | if (state == STATE_FALSE) return "STATE_FALSE"; |
| 162 | if (state == STATE_TRUE) return "STATE_TRUE"; |
| 163 | if (state == STATE_UNKNOWN) return "STATE_UNKNOWN"; |
| 164 | if (state == STATE_ERROR) return "STATE_ERROR"; |
| 165 | throw new IllegalArgumentException("state is invalid: " + state); |
| 166 | } |
| 167 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 168 | @SystemApi |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 169 | public static String relevanceToString(int flags) { |
| 170 | final boolean now = (flags & FLAG_RELEVANT_NOW) != 0; |
| 171 | final boolean always = (flags & FLAG_RELEVANT_ALWAYS) != 0; |
| 172 | if (!now && !always) return "NONE"; |
| 173 | if (now && always) return "NOW, ALWAYS"; |
| 174 | return now ? "NOW" : "ALWAYS"; |
| 175 | } |
| 176 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 177 | @Override |
| 178 | public boolean equals(Object o) { |
| 179 | if (!(o instanceof Condition)) return false; |
| 180 | if (o == this) return true; |
| 181 | final Condition other = (Condition) o; |
| 182 | return Objects.equals(other.id, id) |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 183 | && Objects.equals(other.summary, summary) |
| 184 | && Objects.equals(other.line1, line1) |
| 185 | && Objects.equals(other.line2, line2) |
| 186 | && other.icon == icon |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 187 | && other.state == state |
| 188 | && other.flags == flags; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public int hashCode() { |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 193 | return Objects.hash(id, summary, line1, line2, icon, state, flags); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public int describeContents() { |
| 198 | return 0; |
| 199 | } |
| 200 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 201 | @SystemApi |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 202 | public Condition copy() { |
| 203 | final Parcel parcel = Parcel.obtain(); |
| 204 | try { |
| 205 | writeToParcel(parcel, 0); |
| 206 | parcel.setDataPosition(0); |
| 207 | return new Condition(parcel); |
| 208 | } finally { |
| 209 | parcel.recycle(); |
| 210 | } |
| 211 | } |
| 212 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 213 | @SystemApi |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 214 | public static Uri.Builder newId(Context context) { |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 215 | return new Uri.Builder() |
| 216 | .scheme(Condition.SCHEME) |
| 217 | .authority(context.getPackageName()); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 220 | @SystemApi |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 221 | public static boolean isValidId(Uri id, String pkg) { |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 222 | return id != null && SCHEME.equals(id.getScheme()) && pkg.equals(id.getAuthority()); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 223 | } |
| 224 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 225 | public static final Parcelable.Creator<Condition> CREATOR |
| 226 | = new Parcelable.Creator<Condition>() { |
| 227 | @Override |
| 228 | public Condition createFromParcel(Parcel source) { |
| 229 | return new Condition(source); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public Condition[] newArray(int size) { |
| 234 | return new Condition[size]; |
| 235 | } |
| 236 | }; |
| 237 | } |