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 | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
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; |
| 24 | |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | /** |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 28 | * 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 Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 30 | */ |
| 31 | public class Condition implements Parcelable { |
| 32 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 33 | 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 Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 40 | public static final int FLAG_RELEVANT_NOW = 1 << 0; |
| 41 | public static final int FLAG_RELEVANT_ALWAYS = 1 << 1; |
| 42 | |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 43 | /** |
| 44 | * The URI representing the condition being updated. |
| 45 | * See {@link android.app.AutomaticZenRule#getConditionId()}. |
| 46 | */ |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 47 | public final Uri id; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 48 | |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 49 | /** |
| 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 Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 84 | public Condition(Uri id, String summary, String line1, String line2, int icon, |
| 85 | int state, int flags) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 86 | if (id == null) throw new IllegalArgumentException("id is required"); |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 87 | 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 Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 90 | if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 91 | this.id = id; |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 92 | this.summary = summary; |
| 93 | this.line1 = line1; |
| 94 | this.line2 = line2; |
| 95 | this.icon = icon; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 96 | this.state = state; |
| 97 | this.flags = flags; |
| 98 | } |
| 99 | |
| 100 | private Condition(Parcel source) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 101 | this((Uri)source.readParcelable(Condition.class.getClassLoader()), |
| 102 | source.readString(), |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 103 | source.readString(), |
| 104 | source.readString(), |
| 105 | source.readInt(), |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 106 | source.readInt(), |
| 107 | source.readInt()); |
| 108 | } |
| 109 | |
| 110 | private static boolean isValidState(int state) { |
| 111 | return state >= STATE_FALSE && state <= STATE_ERROR; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void writeToParcel(Parcel dest, int flags) { |
| 116 | dest.writeParcelable(id, 0); |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 117 | dest.writeString(summary); |
| 118 | dest.writeString(line1); |
| 119 | dest.writeString(line2); |
| 120 | dest.writeInt(icon); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 121 | dest.writeInt(state); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 122 | dest.writeInt(this.flags); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public String toString() { |
| 127 | return new StringBuilder(Condition.class.getSimpleName()).append('[') |
| 128 | .append("id=").append(id) |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 129 | .append(",summary=").append(summary) |
| 130 | .append(",line1=").append(line1) |
| 131 | .append(",line2=").append(line2) |
| 132 | .append(",icon=").append(icon) |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 133 | .append(",state=").append(stateToString(state)) |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 134 | .append(",flags=").append(flags) |
| 135 | .append(']').toString(); |
| 136 | } |
| 137 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 138 | 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 Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 146 | 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 Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 154 | @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 Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 160 | && Objects.equals(other.summary, summary) |
| 161 | && Objects.equals(other.line1, line1) |
| 162 | && Objects.equals(other.line2, line2) |
| 163 | && other.icon == icon |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 164 | && other.state == state |
| 165 | && other.flags == flags; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public int hashCode() { |
John Spurlock | ef5693b | 2014-05-02 00:07:35 -0400 | [diff] [blame] | 170 | return Objects.hash(id, summary, line1, line2, icon, state, flags); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 171 | } |
| 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 Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 189 | 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 Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 194 | return id != null && SCHEME.equals(id.getScheme()) && pkg.equals(id.getAuthority()); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 195 | } |
| 196 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 197 | 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 | } |