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 19 import android.annotation.IntDef; 20 import android.annotation.Nullable; 21 import android.content.Context; 22 import android.net.Uri; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 import android.util.proto.ProtoOutputStream; 26 27 import java.lang.annotation.Retention; 28 import java.lang.annotation.RetentionPolicy; 29 import java.util.Objects; 30 31 /** 32 * The current condition of an {@link android.app.AutomaticZenRule}, provided by the 33 * app 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. 35 */ 36 public final class Condition implements Parcelable { 37 38 public static final String SCHEME = "condition"; 39 40 /** @hide */ 41 @IntDef(prefix = { "STATE_" }, value = { 42 STATE_FALSE, 43 STATE_TRUE, 44 STATE_UNKNOWN, 45 STATE_ERROR 46 }) 47 @Retention(RetentionPolicy.SOURCE) 48 public @interface State {} 49 50 /** 51 * Indicates that Do Not Disturb should be turned off. Note that all Conditions from all 52 * {@link android.app.AutomaticZenRule} providers must be off for Do Not Disturb to be turned 53 * off on the device. 54 */ 55 public static final int STATE_FALSE = 0; 56 /** 57 * Indicates that Do Not Disturb should be turned on. 58 */ 59 public static final int STATE_TRUE = 1; 60 61 public static final int STATE_UNKNOWN = 2; 62 public static final int STATE_ERROR = 3; 63 64 public static final int FLAG_RELEVANT_NOW = 1 << 0; 65 public static final int FLAG_RELEVANT_ALWAYS = 1 << 1; 66 67 /** 68 * The URI representing the rule being updated. 69 * See {@link android.app.AutomaticZenRule#getConditionId()}. 70 */ 71 public final Uri id; 72 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 79 public final String line1; 80 public final String line2; 81 82 /** 83 * The state of this condition. {@link #STATE_TRUE} will enable Do Not Disturb mode. 84 * {@link #STATE_FALSE} will turn Do Not Disturb off for this rule. Note that Do Not Disturb 85 * might still be enabled globally if other conditions are in a {@link #STATE_TRUE} state. 86 */ 87 @State 88 public final int state; 89 90 public final int flags; 91 public final int icon; 92 93 /** 94 * An object representing the current state of a {@link android.app.AutomaticZenRule}. 95 * @param id the {@link android.app.AutomaticZenRule#getConditionId()} of the zen rule 96 * @param summary a user visible description of the rule state. 97 */ Condition(Uri id, String summary, int state)98 public Condition(Uri id, String summary, int state) { 99 this(id, summary, "", "", -1, state, FLAG_RELEVANT_ALWAYS); 100 } 101 Condition(Uri id, String summary, String line1, String line2, int icon, int state, int flags)102 public Condition(Uri id, String summary, String line1, String line2, int icon, 103 int state, int flags) { 104 if (id == null) throw new IllegalArgumentException("id is required"); 105 if (summary == null) throw new IllegalArgumentException("summary is required"); 106 if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state); 107 this.id = id; 108 this.summary = summary; 109 this.line1 = line1; 110 this.line2 = line2; 111 this.icon = icon; 112 this.state = state; 113 this.flags = flags; 114 } 115 Condition(Parcel source)116 public Condition(Parcel source) { 117 this((Uri)source.readParcelable(Condition.class.getClassLoader()), 118 source.readString(), 119 source.readString(), 120 source.readString(), 121 source.readInt(), 122 source.readInt(), 123 source.readInt()); 124 } 125 isValidState(int state)126 private static boolean isValidState(int state) { 127 return state >= STATE_FALSE && state <= STATE_ERROR; 128 } 129 130 @Override writeToParcel(Parcel dest, int flags)131 public void writeToParcel(Parcel dest, int flags) { 132 dest.writeParcelable(id, 0); 133 dest.writeString(summary); 134 dest.writeString(line1); 135 dest.writeString(line2); 136 dest.writeInt(icon); 137 dest.writeInt(state); 138 dest.writeInt(this.flags); 139 } 140 141 @Override toString()142 public String toString() { 143 return new StringBuilder(Condition.class.getSimpleName()).append('[') 144 .append("state=").append(stateToString(state)) 145 .append(",id=").append(id) 146 .append(",summary=").append(summary) 147 .append(",line1=").append(line1) 148 .append(",line2=").append(line2) 149 .append(",icon=").append(icon) 150 .append(",flags=").append(flags) 151 .append(']').toString(); 152 } 153 154 /** @hide */ dumpDebug(ProtoOutputStream proto, long fieldId)155 public void dumpDebug(ProtoOutputStream proto, long fieldId) { 156 final long token = proto.start(fieldId); 157 158 // id is guaranteed not to be null. 159 proto.write(ConditionProto.ID, id.toString()); 160 proto.write(ConditionProto.SUMMARY, summary); 161 proto.write(ConditionProto.LINE_1, line1); 162 proto.write(ConditionProto.LINE_2, line2); 163 proto.write(ConditionProto.ICON, icon); 164 proto.write(ConditionProto.STATE, state); 165 proto.write(ConditionProto.FLAGS, flags); 166 167 proto.end(token); 168 } 169 stateToString(int state)170 public static String stateToString(int state) { 171 if (state == STATE_FALSE) return "STATE_FALSE"; 172 if (state == STATE_TRUE) return "STATE_TRUE"; 173 if (state == STATE_UNKNOWN) return "STATE_UNKNOWN"; 174 if (state == STATE_ERROR) return "STATE_ERROR"; 175 throw new IllegalArgumentException("state is invalid: " + state); 176 } 177 relevanceToString(int flags)178 public static String relevanceToString(int flags) { 179 final boolean now = (flags & FLAG_RELEVANT_NOW) != 0; 180 final boolean always = (flags & FLAG_RELEVANT_ALWAYS) != 0; 181 if (!now && !always) return "NONE"; 182 if (now && always) return "NOW, ALWAYS"; 183 return now ? "NOW" : "ALWAYS"; 184 } 185 186 @Override equals(@ullable Object o)187 public boolean equals(@Nullable Object o) { 188 if (!(o instanceof Condition)) return false; 189 if (o == this) return true; 190 final Condition other = (Condition) o; 191 return Objects.equals(other.id, id) 192 && Objects.equals(other.summary, summary) 193 && Objects.equals(other.line1, line1) 194 && Objects.equals(other.line2, line2) 195 && other.icon == icon 196 && other.state == state 197 && other.flags == flags; 198 } 199 200 @Override hashCode()201 public int hashCode() { 202 return Objects.hash(id, summary, line1, line2, icon, state, flags); 203 } 204 205 @Override describeContents()206 public int describeContents() { 207 return 0; 208 } 209 copy()210 public Condition copy() { 211 final Parcel parcel = Parcel.obtain(); 212 try { 213 writeToParcel(parcel, 0); 214 parcel.setDataPosition(0); 215 return new Condition(parcel); 216 } finally { 217 parcel.recycle(); 218 } 219 } 220 newId(Context context)221 public static Uri.Builder newId(Context context) { 222 return new Uri.Builder() 223 .scheme(Condition.SCHEME) 224 .authority(context.getPackageName()); 225 } 226 isValidId(Uri id, String pkg)227 public static boolean isValidId(Uri id, String pkg) { 228 return id != null && SCHEME.equals(id.getScheme()) && pkg.equals(id.getAuthority()); 229 } 230 231 public static final @android.annotation.NonNull Parcelable.Creator<Condition> CREATOR 232 = new Parcelable.Creator<Condition>() { 233 @Override 234 public Condition createFromParcel(Parcel source) { 235 return new Condition(source); 236 } 237 238 @Override 239 public Condition[] newArray(int size) { 240 return new Condition[size]; 241 } 242 }; 243 } 244