| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 33 |
|
| 34 |
public final class AutomaticZenRule implements Parcelable { |
| 35 |
|
| 36 |
private static final int ENABLED = 1; |
| 37 |
|
| 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 |
private String mPkg; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static final int MAX_STRING_LENGTH = 1000; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
@Deprecated |
| 70 |
public AutomaticZenRule(String name, ComponentName owner, Uri conditionId, |
| 71 |
int interruptionFilter, boolean enabled) { |
| 72 |
this(name, owner, null, conditionId, null, interruptionFilter, enabled); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public AutomaticZenRule(@NonNull String name, @Nullable ComponentName owner, |
| 101 |
@Nullable ComponentName configurationActivity, @NonNull Uri conditionId, |
| 102 |
@Nullable ZenPolicy policy, int interruptionFilter, boolean enabled) { |
| 103 |
this.name = getTrimmedString(name); |
| 104 |
this.owner = getTrimmedComponentName(owner); |
| 105 |
this.configurationActivity = getTrimmedComponentName(configurationActivity); |
| 106 |
this.conditionId = getTrimmedUri(conditionId); |
| 107 |
this.interruptionFilter = interruptionFilter; |
| 108 |
this.enabled = enabled; |
| 109 |
this.mZenPolicy = policy; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public AutomaticZenRule(String name, ComponentName owner, ComponentName configurationActivity, |
| 116 |
Uri conditionId, ZenPolicy policy, int interruptionFilter, boolean enabled, |
| 117 |
long creationTime) { |
| 118 |
this(name, owner, configurationActivity, conditionId, policy, interruptionFilter, enabled); |
| 119 |
this.creationTime = creationTime; |
| 120 |
} |
| 121 |
|
| 122 |
public AutomaticZenRule(Parcel source) { |
| 123 |
enabled = source.readInt() == ENABLED; |
| 124 |
if (source.readInt() == ENABLED) { |
| 125 |
name = getTrimmedString(source.readString()); |
| 126 |
} |
| 127 |
interruptionFilter = source.readInt(); |
| 128 |
conditionId = getTrimmedUri(source.readParcelable(null, android.net.Uri.class)); |
| 129 |
owner = getTrimmedComponentName( |
| 130 |
source.readParcelable(null, android.content.ComponentName.class)); |
| 131 |
configurationActivity = getTrimmedComponentName( |
| 132 |
source.readParcelable(null, android.content.ComponentName.class)); |
| 133 |
creationTime = source.readLong(); |
| 134 |
mZenPolicy = source.readParcelable(null, android.service.notification.ZenPolicy.class); |
| 135 |
mModified = source.readInt() == ENABLED; |
| 136 |
mPkg = source.readString(); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public ComponentName getOwner() { |
| 143 |
return owner; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public @Nullable ComponentName getConfigurationActivity() { |
| 151 |
return configurationActivity; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public Uri getConditionId() { |
| 158 |
return conditionId; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public int getInterruptionFilter() { |
| 165 |
return interruptionFilter; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public String getName() { |
| 172 |
return name; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public boolean isEnabled() { |
| 179 |
return enabled; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public boolean isModified() { |
| 187 |
return mModified; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public ZenPolicy getZenPolicy() { |
| 194 |
return mZenPolicy == null ? null : this.mZenPolicy.copy(); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public long getCreationTime() { |
| 201 |
return creationTime; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public void setConditionId(Uri conditionId) { |
| 208 |
this.conditionId = getTrimmedUri(conditionId); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public void setInterruptionFilter(@InterruptionFilter int interruptionFilter) { |
| 216 |
this.interruptionFilter = interruptionFilter; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public void setName(String name) { |
| 223 |
this.name = getTrimmedString(name); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public void setEnabled(boolean enabled) { |
| 230 |
this.enabled = enabled; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public void setModified(boolean modified) { |
| 238 |
this.mModified = modified; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public void setZenPolicy(ZenPolicy zenPolicy) { |
| 245 |
this.mZenPolicy = (zenPolicy == null ? null : zenPolicy.copy()); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public void setConfigurationActivity(@Nullable ComponentName componentName) { |
| 255 |
this.configurationActivity = getTrimmedComponentName(componentName); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public void setPackageName(String pkgName) { |
| 262 |
mPkg = pkgName; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public String getPackageName() { |
| 269 |
return mPkg; |
| 270 |
} |
| 271 |
|
| 272 |
@Override |
| 273 |
public int describeContents() { |
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
@Override |
| 278 |
public void writeToParcel(Parcel dest, int flags) { |
| 279 |
dest.writeInt(enabled ? ENABLED : DISABLED); |
| 280 |
if (name != null) { |
| 281 |
dest.writeInt(1); |
| 282 |
dest.writeString(name); |
| 283 |
} else { |
| 284 |
dest.writeInt(0); |
| 285 |
} |
| 286 |
dest.writeInt(interruptionFilter); |
| 287 |
dest.writeParcelable(conditionId, 0); |
| 288 |
dest.writeParcelable(owner, 0); |
| 289 |
dest.writeParcelable(configurationActivity, 0); |
| 290 |
dest.writeLong(creationTime); |
| 291 |
dest.writeParcelable(mZenPolicy, 0); |
| 292 |
dest.writeInt(mModified ? ENABLED : DISABLED); |
| 293 |
dest.writeString(mPkg); |
| 294 |
} |
| 295 |
|
| 296 |
@Override |
| 297 |
public String toString() { |
| 298 |
return new StringBuilder(AutomaticZenRule.class.getSimpleName()).append('[') |
| 299 |
.append("enabled=").append(enabled) |
| 300 |
.append(",name=").append(name) |
| 301 |
.append(",interruptionFilter=").append(interruptionFilter) |
| 302 |
.append(",pkg=").append(mPkg) |
| 303 |
.append(",conditionId=").append(conditionId) |
| 304 |
.append(",owner=").append(owner) |
| 305 |
.append(",configActivity=").append(configurationActivity) |
| 306 |
.append(",creationTime=").append(creationTime) |
| 307 |
.append(",mZenPolicy=").append(mZenPolicy) |
| 308 |
.append(']').toString(); |
| 309 |
} |
| 310 |
|
| 311 |
@Override |
| 312 |
public boolean equals(@Nullable Object o) { |
| 313 |
if (!(o instanceof AutomaticZenRule)) return false; |
| 314 |
if (o == this) return true; |
| 315 |
final AutomaticZenRule other = (AutomaticZenRule) o; |
| 316 |
return other.enabled == enabled |
| 317 |
&& other.mModified == mModified |
| 318 |
&& Objects.equals(other.name, name) |
| 319 |
&& other.interruptionFilter == interruptionFilter |
| 320 |
&& Objects.equals(other.conditionId, conditionId) |
| 321 |
&& Objects.equals(other.owner, owner) |
| 322 |
&& Objects.equals(other.mZenPolicy, mZenPolicy) |
| 323 |
&& Objects.equals(other.configurationActivity, configurationActivity) |
| 324 |
&& Objects.equals(other.mPkg, mPkg) |
| 325 |
&& other.creationTime == creationTime; |
| 326 |
} |
| 327 |
|
| 328 |
@Override |
| 329 |
public int hashCode() { |
| 330 |
return Objects.hash(enabled, name, interruptionFilter, conditionId, owner, |
| 331 |
configurationActivity, mZenPolicy, mModified, creationTime, mPkg); |
| 332 |
} |
| 333 |
|
| 334 |
public static final @android.annotation.NonNull Parcelable.Creator<AutomaticZenRule> CREATOR |
| 335 |
= new Parcelable.Creator<AutomaticZenRule>() { |
| 336 |
@Override |
| 337 |
public AutomaticZenRule createFromParcel(Parcel source) { |
| 338 |
return new AutomaticZenRule(source); |
| 339 |
} |
| 340 |
@Override |
| 341 |
public AutomaticZenRule[] newArray(int size) { |
| 342 |
return new AutomaticZenRule[size]; |
| 343 |
} |
| 344 |
}; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
private static ComponentName getTrimmedComponentName(ComponentName cn) { |
| 351 |
if (cn == null) return null; |
| 352 |
return new ComponentName(getTrimmedString(cn.getPackageName()), |
| 353 |
getTrimmedString(cn.getClassName())); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
private static String getTrimmedString(String input) { |
| 360 |
if (input != null && input.length() > MAX_STRING_LENGTH) { |
| 361 |
return input.substring(0, MAX_STRING_LENGTH); |
| 362 |
} |
| 363 |
return input; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
private static Uri getTrimmedUri(Uri input) { |
| 371 |
if (input != null && input.toString().length() > MAX_STRING_LENGTH) { |
| 372 |
return Uri.parse(getTrimmedString(input.toString())); |
| 373 |
} |
| 374 |
return input; |
| 375 |
} |
| 376 |
} |