Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 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 | |
Beverly | b098965 | 2019-03-04 11:37:52 -0500 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 21 | import android.app.NotificationManager.InterruptionFilter; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 22 | import android.content.ComponentName; |
| 23 | import android.net.Uri; |
| 24 | import android.os.Parcel; |
| 25 | import android.os.Parcelable; |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 26 | import android.service.notification.Condition; |
Beverly | b098965 | 2019-03-04 11:37:52 -0500 | [diff] [blame] | 27 | import android.service.notification.ZenPolicy; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 28 | |
| 29 | import java.util.Objects; |
| 30 | |
| 31 | /** |
| 32 | * Rule instance information for zen mode. |
| 33 | */ |
Jeff Sharkey | 50d1c04 | 2016-02-29 16:34:46 -0700 | [diff] [blame] | 34 | public final class AutomaticZenRule implements Parcelable { |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 35 | /* @hide */ |
| 36 | private static final int ENABLED = 1; |
| 37 | /* @hide */ |
| 38 | private static final int DISABLED = 0; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 39 | private boolean enabled = false; |
| 40 | private String name; |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 41 | private @InterruptionFilter int interruptionFilter; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 42 | private Uri conditionId; |
| 43 | private ComponentName owner; |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 44 | private ComponentName configurationActivity; |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 45 | private long creationTime; |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 46 | private ZenPolicy mZenPolicy; |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 47 | private boolean mModified = false; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 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. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 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. |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 58 | * @deprecated use {@link #AutomaticZenRule(String, ComponentName, ComponentName, Uri, |
| 59 | * ZenPolicy, int, boolean)}. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 60 | */ |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 61 | @Deprecated |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 62 | public AutomaticZenRule(String name, ComponentName owner, Uri conditionId, |
| 63 | int interruptionFilter, boolean enabled) { |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 64 | this(name, owner, null, conditionId, null, interruptionFilter, enabled); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 67 | /** |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 68 | * Creates an automatic zen rule. |
| 69 | * |
| 70 | * @param name The name of the rule. |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 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. |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 87 | * @param policy The policy defines which notifications are allowed to interrupt the user |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 88 | * while this rule is active. This overrides the global policy while this rule is |
| 89 | * action ({@link Condition#STATE_TRUE}). |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 90 | * @param enabled Whether the rule is enabled. |
| 91 | */ |
Beverly | b098965 | 2019-03-04 11:37:52 -0500 | [diff] [blame] | 92 | public AutomaticZenRule(@NonNull String name, @Nullable ComponentName owner, |
| 93 | @Nullable ComponentName configurationActivity, @NonNull Uri conditionId, |
| 94 | @Nullable ZenPolicy policy, int interruptionFilter, boolean enabled) { |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 95 | this.name = name; |
| 96 | this.owner = owner; |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 97 | this.configurationActivity = configurationActivity; |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 98 | this.conditionId = conditionId; |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 99 | this.interruptionFilter = interruptionFilter; |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 100 | this.enabled = enabled; |
| 101 | this.mZenPolicy = policy; |
| 102 | } |
| 103 | |
| 104 | /** |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 105 | * @hide |
| 106 | */ |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 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); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 111 | this.creationTime = creationTime; |
| 112 | } |
| 113 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 114 | public AutomaticZenRule(Parcel source) { |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 115 | enabled = source.readInt() == ENABLED; |
| 116 | if (source.readInt() == ENABLED) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 117 | name = source.readString(); |
| 118 | } |
| 119 | interruptionFilter = source.readInt(); |
| 120 | conditionId = source.readParcelable(null); |
| 121 | owner = source.readParcelable(null); |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 122 | configurationActivity = source.readParcelable(null); |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 123 | creationTime = source.readLong(); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 124 | mZenPolicy = source.readParcelable(null); |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 125 | mModified = source.readInt() == ENABLED; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns the {@link ComponentName} of the condition provider service that owns this rule. |
| 130 | */ |
| 131 | public ComponentName getOwner() { |
| 132 | return owner; |
| 133 | } |
| 134 | |
| 135 | /** |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 136 | * Returns the {@link ComponentName} of the activity that shows configuration options |
| 137 | * for this rule. |
| 138 | */ |
Julia Reynolds | cbc45e7 | 2019-03-07 12:31:52 -0500 | [diff] [blame] | 139 | public @Nullable ComponentName getConfigurationActivity() { |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 140 | return configurationActivity; |
| 141 | } |
| 142 | |
| 143 | /** |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 144 | * Returns the representation of the state that causes this rule to become active. |
| 145 | */ |
| 146 | public Uri getConditionId() { |
| 147 | return conditionId; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns the interruption filter that is applied when this rule is active. |
| 152 | */ |
| 153 | public int getInterruptionFilter() { |
| 154 | return interruptionFilter; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Returns the name of this rule. |
| 159 | */ |
| 160 | public String getName() { |
| 161 | return name; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns whether this rule is enabled. |
| 166 | */ |
| 167 | public boolean isEnabled() { |
| 168 | return enabled; |
| 169 | } |
| 170 | |
| 171 | /** |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 172 | * Returns whether this rule's name has been modified by the user. |
| 173 | * @hide |
| 174 | */ |
| 175 | public boolean isModified() { |
| 176 | return mModified; |
| 177 | } |
| 178 | |
| 179 | /** |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 180 | * Gets the zen policy. |
| 181 | */ |
| 182 | public ZenPolicy getZenPolicy() { |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 183 | return mZenPolicy == null ? null : this.mZenPolicy.copy(); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 187 | * Returns the time this rule was created, represented as milliseconds since the epoch. |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 188 | */ |
| 189 | public long getCreationTime() { |
| 190 | return creationTime; |
| 191 | } |
| 192 | |
| 193 | /** |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 194 | * Sets the representation of the state that causes this rule to become active. |
| 195 | */ |
| 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. |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 202 | * @param interruptionFilter The do not disturb mode to enter when this rule is active. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 203 | */ |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 204 | public void setInterruptionFilter(@InterruptionFilter int interruptionFilter) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 205 | this.interruptionFilter = interruptionFilter; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Sets the name of this rule. |
| 210 | */ |
| 211 | public void setName(String name) { |
| 212 | this.name = name; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Enables this rule. |
| 217 | */ |
| 218 | public void setEnabled(boolean enabled) { |
| 219 | this.enabled = enabled; |
| 220 | } |
| 221 | |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 222 | /** |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 223 | * Sets modified state of this rule. |
| 224 | * @hide |
| 225 | */ |
| 226 | public void setModified(boolean modified) { |
| 227 | this.mModified = modified; |
| 228 | } |
| 229 | |
| 230 | /** |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 231 | * Sets the zen policy. |
| 232 | */ |
| 233 | public void setZenPolicy(ZenPolicy zenPolicy) { |
| 234 | this.mZenPolicy = zenPolicy; |
| 235 | } |
| 236 | |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 237 | /** |
| 238 | * Sets the configuration activity - an activity that handles |
| 239 | * {@link NotificationManager#ACTION_AUTOMATIC_ZEN_RULE} that shows the user more information |
Julia Reynolds | cbc45e7 | 2019-03-07 12:31:52 -0500 | [diff] [blame] | 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}. |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 242 | */ |
Julia Reynolds | cbc45e7 | 2019-03-07 12:31:52 -0500 | [diff] [blame] | 243 | public void setConfigurationActivity(@Nullable ComponentName componentName) { |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 244 | this.configurationActivity = componentName; |
| 245 | } |
| 246 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 247 | @Override |
| 248 | public int describeContents() { |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void writeToParcel(Parcel dest, int flags) { |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 254 | dest.writeInt(enabled ? ENABLED : DISABLED); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 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); |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 264 | dest.writeParcelable(configurationActivity, 0); |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 265 | dest.writeLong(creationTime); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 266 | dest.writeParcelable(mZenPolicy, 0); |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 267 | dest.writeInt(mModified ? ENABLED : DISABLED); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @Override |
| 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) |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 278 | .append(",configActivity=").append(configurationActivity) |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 279 | .append(",creationTime=").append(creationTime) |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 280 | .append(",mZenPolicy=").append(mZenPolicy) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 281 | .append(']').toString(); |
| 282 | } |
| 283 | |
| 284 | @Override |
| 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 |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 290 | && other.mModified == mModified |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 291 | && Objects.equals(other.name, name) |
| 292 | && other.interruptionFilter == interruptionFilter |
| 293 | && Objects.equals(other.conditionId, conditionId) |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 294 | && Objects.equals(other.owner, owner) |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 295 | && Objects.equals(other.mZenPolicy, mZenPolicy) |
| 296 | && Objects.equals(other.configurationActivity, configurationActivity) |
| 297 | && other.creationTime == creationTime; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public int hashCode() { |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 302 | return Objects.hash(enabled, name, interruptionFilter, conditionId, owner, |
| 303 | configurationActivity, mZenPolicy, mModified, creationTime); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 304 | } |
| 305 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 306 | public static final @android.annotation.NonNull Parcelable.Creator<AutomaticZenRule> CREATOR |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 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 | }; |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 317 | } |