1 /** 2 * Copyright (c) 2015, 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.app; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.app.NotificationManager.InterruptionFilter; 22 import android.content.ComponentName; 23 import android.net.Uri; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 import android.service.notification.Condition; 27 import android.service.notification.ZenPolicy; 28 29 import java.util.Objects; 30 31 /** 32 * Rule instance information for zen mode. 33 */ 34 public final class AutomaticZenRule implements Parcelable { 35 /* @hide */ 36 private static final int ENABLED = 1; 37 /* @hide */ 38 private static final int DISABLED = 0; 39 private boolean enabled = false; 40 private String name; 41 private @InterruptionFilter int interruptionFilter; 42 private Uri conditionId; 43 private ComponentName owner; 44 private ComponentName configurationActivity; 45 private long creationTime; 46 private ZenPolicy mZenPolicy; 47 private boolean mModified = false; 48 49 /** 50 * Creates an automatic zen rule. 51 * 52 * @param name The name of the rule. 53 * @param owner The Condition Provider service that owns this rule. 54 * @param interruptionFilter The interruption filter defines which notifications are allowed to 55 * interrupt the user (e.g. via sound & vibration) while this rule 56 * is active. 57 * @param enabled Whether the rule is enabled. 58 * @deprecated use {@link #AutomaticZenRule(String, ComponentName, ComponentName, Uri, 59 * ZenPolicy, int, boolean)}. 60 */ 61 @Deprecated AutomaticZenRule(String name, ComponentName owner, Uri conditionId, int interruptionFilter, boolean enabled)62 public AutomaticZenRule(String name, ComponentName owner, Uri conditionId, 63 int interruptionFilter, boolean enabled) { 64 this(name, owner, null, conditionId, null, interruptionFilter, enabled); 65 } 66 67 /** 68 * Creates an automatic zen rule. 69 * 70 * @param name The name of the rule. 71 * @param owner The Condition Provider service that owns this rule. This can be null if you're 72 * using {@link NotificationManager#setAutomaticZenRuleState(String, Condition)} 73 * instead of {@link android.service.notification.ConditionProviderService}. 74 * @param configurationActivity An activity that handles 75 * {@link NotificationManager#ACTION_AUTOMATIC_ZEN_RULE} that shows 76 * the user 77 * more information about this rule and/or allows them to 78 * configure it. This is required if you are not using a 79 * {@link android.service.notification.ConditionProviderService}. 80 * If you are, it overrides the information specified in your 81 * manifest. 82 * @param conditionId A representation of the state that should cause your app to apply the 83 * given interruption filter. 84 * @param interruptionFilter The interruption filter defines which notifications are allowed to 85 * interrupt the user (e.g. via sound & vibration) while this rule 86 * is active. 87 * @param policy The policy defines which notifications are allowed to interrupt the user 88 * while this rule is active. This overrides the global policy while this rule is 89 * action ({@link Condition#STATE_TRUE}). 90 * @param enabled Whether the rule is enabled. 91 */ AutomaticZenRule(@onNull String name, @Nullable ComponentName owner, @Nullable ComponentName configurationActivity, @NonNull Uri conditionId, @Nullable ZenPolicy policy, int interruptionFilter, boolean enabled)92 public AutomaticZenRule(@NonNull String name, @Nullable ComponentName owner, 93 @Nullable ComponentName configurationActivity, @NonNull Uri conditionId, 94 @Nullable ZenPolicy policy, int interruptionFilter, boolean enabled) { 95 this.name = name; 96 this.owner = owner; 97 this.configurationActivity = configurationActivity; 98 this.conditionId = conditionId; 99 this.interruptionFilter = interruptionFilter; 100 this.enabled = enabled; 101 this.mZenPolicy = policy; 102 } 103 104 /** 105 * @hide 106 */ AutomaticZenRule(String name, ComponentName owner, ComponentName configurationActivity, Uri conditionId, ZenPolicy policy, int interruptionFilter, boolean enabled, long creationTime)107 public AutomaticZenRule(String name, ComponentName owner, ComponentName configurationActivity, 108 Uri conditionId, ZenPolicy policy, int interruptionFilter, boolean enabled, 109 long creationTime) { 110 this(name, owner, configurationActivity, conditionId, policy, interruptionFilter, enabled); 111 this.creationTime = creationTime; 112 } 113 AutomaticZenRule(Parcel source)114 public AutomaticZenRule(Parcel source) { 115 enabled = source.readInt() == ENABLED; 116 if (source.readInt() == ENABLED) { 117 name = source.readString(); 118 } 119 interruptionFilter = source.readInt(); 120 conditionId = source.readParcelable(null); 121 owner = source.readParcelable(null); 122 configurationActivity = source.readParcelable(null); 123 creationTime = source.readLong(); 124 mZenPolicy = source.readParcelable(null); 125 mModified = source.readInt() == ENABLED; 126 } 127 128 /** 129 * Returns the {@link ComponentName} of the condition provider service that owns this rule. 130 */ getOwner()131 public ComponentName getOwner() { 132 return owner; 133 } 134 135 /** 136 * Returns the {@link ComponentName} of the activity that shows configuration options 137 * for this rule. 138 */ getConfigurationActivity()139 public @Nullable ComponentName getConfigurationActivity() { 140 return configurationActivity; 141 } 142 143 /** 144 * Returns the representation of the state that causes this rule to become active. 145 */ getConditionId()146 public Uri getConditionId() { 147 return conditionId; 148 } 149 150 /** 151 * Returns the interruption filter that is applied when this rule is active. 152 */ getInterruptionFilter()153 public int getInterruptionFilter() { 154 return interruptionFilter; 155 } 156 157 /** 158 * Returns the name of this rule. 159 */ getName()160 public String getName() { 161 return name; 162 } 163 164 /** 165 * Returns whether this rule is enabled. 166 */ isEnabled()167 public boolean isEnabled() { 168 return enabled; 169 } 170 171 /** 172 * Returns whether this rule's name has been modified by the user. 173 * @hide 174 */ isModified()175 public boolean isModified() { 176 return mModified; 177 } 178 179 /** 180 * Gets the zen policy. 181 */ getZenPolicy()182 public ZenPolicy getZenPolicy() { 183 return mZenPolicy == null ? null : this.mZenPolicy.copy(); 184 } 185 186 /** 187 * Returns the time this rule was created, represented as milliseconds since the epoch. 188 */ getCreationTime()189 public long getCreationTime() { 190 return creationTime; 191 } 192 193 /** 194 * Sets the representation of the state that causes this rule to become active. 195 */ setConditionId(Uri conditionId)196 public void setConditionId(Uri conditionId) { 197 this.conditionId = conditionId; 198 } 199 200 /** 201 * Sets the interruption filter that is applied when this rule is active. 202 * @param interruptionFilter The do not disturb mode to enter when this rule is active. 203 */ setInterruptionFilter(@nterruptionFilter int interruptionFilter)204 public void setInterruptionFilter(@InterruptionFilter int interruptionFilter) { 205 this.interruptionFilter = interruptionFilter; 206 } 207 208 /** 209 * Sets the name of this rule. 210 */ setName(String name)211 public void setName(String name) { 212 this.name = name; 213 } 214 215 /** 216 * Enables this rule. 217 */ setEnabled(boolean enabled)218 public void setEnabled(boolean enabled) { 219 this.enabled = enabled; 220 } 221 222 /** 223 * Sets modified state of this rule. 224 * @hide 225 */ setModified(boolean modified)226 public void setModified(boolean modified) { 227 this.mModified = modified; 228 } 229 230 /** 231 * Sets the zen policy. 232 */ setZenPolicy(ZenPolicy zenPolicy)233 public void setZenPolicy(ZenPolicy zenPolicy) { 234 this.mZenPolicy = (zenPolicy == null ? null : zenPolicy.copy()); 235 } 236 237 /** 238 * Sets the configuration activity - an activity that handles 239 * {@link NotificationManager#ACTION_AUTOMATIC_ZEN_RULE} that shows the user more information 240 * about this rule and/or allows them to configure it. This is required to be non-null for rules 241 * that are not backed by {@link android.service.notification.ConditionProviderService}. 242 */ setConfigurationActivity(@ullable ComponentName componentName)243 public void setConfigurationActivity(@Nullable ComponentName componentName) { 244 this.configurationActivity = componentName; 245 } 246 247 @Override describeContents()248 public int describeContents() { 249 return 0; 250 } 251 252 @Override writeToParcel(Parcel dest, int flags)253 public void writeToParcel(Parcel dest, int flags) { 254 dest.writeInt(enabled ? ENABLED : DISABLED); 255 if (name != null) { 256 dest.writeInt(1); 257 dest.writeString(name); 258 } else { 259 dest.writeInt(0); 260 } 261 dest.writeInt(interruptionFilter); 262 dest.writeParcelable(conditionId, 0); 263 dest.writeParcelable(owner, 0); 264 dest.writeParcelable(configurationActivity, 0); 265 dest.writeLong(creationTime); 266 dest.writeParcelable(mZenPolicy, 0); 267 dest.writeInt(mModified ? ENABLED : DISABLED); 268 } 269 270 @Override toString()271 public String toString() { 272 return new StringBuilder(AutomaticZenRule.class.getSimpleName()).append('[') 273 .append("enabled=").append(enabled) 274 .append(",name=").append(name) 275 .append(",interruptionFilter=").append(interruptionFilter) 276 .append(",conditionId=").append(conditionId) 277 .append(",owner=").append(owner) 278 .append(",configActivity=").append(configurationActivity) 279 .append(",creationTime=").append(creationTime) 280 .append(",mZenPolicy=").append(mZenPolicy) 281 .append(']').toString(); 282 } 283 284 @Override equals(Object o)285 public boolean equals(Object o) { 286 if (!(o instanceof AutomaticZenRule)) return false; 287 if (o == this) return true; 288 final AutomaticZenRule other = (AutomaticZenRule) o; 289 return other.enabled == enabled 290 && other.mModified == mModified 291 && Objects.equals(other.name, name) 292 && other.interruptionFilter == interruptionFilter 293 && Objects.equals(other.conditionId, conditionId) 294 && Objects.equals(other.owner, owner) 295 && Objects.equals(other.mZenPolicy, mZenPolicy) 296 && Objects.equals(other.configurationActivity, configurationActivity) 297 && other.creationTime == creationTime; 298 } 299 300 @Override hashCode()301 public int hashCode() { 302 return Objects.hash(enabled, name, interruptionFilter, conditionId, owner, 303 configurationActivity, mZenPolicy, mModified, creationTime); 304 } 305 306 public static final @android.annotation.NonNull Parcelable.Creator<AutomaticZenRule> CREATOR 307 = new Parcelable.Creator<AutomaticZenRule>() { 308 @Override 309 public AutomaticZenRule createFromParcel(Parcel source) { 310 return new AutomaticZenRule(source); 311 } 312 @Override 313 public AutomaticZenRule[] newArray(int size) { 314 return new AutomaticZenRule[size]; 315 } 316 }; 317 } 318