| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.admin; |
| 18 |
|
| 19 |
import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
| 20 |
import static org.xmlpull.v1.XmlPullParser.END_TAG; |
| 21 |
import static org.xmlpull.v1.XmlPullParser.TEXT; |
| 22 |
|
| 23 |
import android.annotation.IntDef; |
| 24 |
import android.annotation.SystemApi; |
| 25 |
import android.os.Parcel; |
| 26 |
import android.os.Parcelable; |
| 27 |
import android.util.Log; |
| 28 |
import android.util.Pair; |
| 29 |
import android.util.TypedXmlPullParser; |
| 30 |
import android.util.TypedXmlSerializer; |
| 31 |
|
| 32 |
import org.xmlpull.v1.XmlPullParserException; |
| 33 |
|
| 34 |
import java.io.IOException; |
| 35 |
import java.lang.annotation.Retention; |
| 36 |
import java.lang.annotation.RetentionPolicy; |
| 37 |
import java.time.Instant; |
| 38 |
import java.time.LocalDate; |
| 39 |
import java.time.LocalDateTime; |
| 40 |
import java.time.LocalTime; |
| 41 |
import java.time.MonthDay; |
| 42 |
import java.time.ZoneId; |
| 43 |
import java.util.ArrayList; |
| 44 |
import java.util.Calendar; |
| 45 |
import java.util.Collections; |
| 46 |
import java.util.List; |
| 47 |
import java.util.concurrent.TimeUnit; |
| 48 |
import java.util.stream.Collectors; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
ng |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public final class SystemUpdatePolicy implements Parcelable { |
| 85 |
private static final String TAG = "SystemUpdatePolicy"; |
| 86 |
|
| 87 |
|
| 88 |
@IntDef(prefix = { "TYPE_" }, value = { |
| 89 |
TYPE_INSTALL_AUTOMATIC, |
| 90 |
TYPE_INSTALL_WINDOWED, |
| 91 |
TYPE_POSTPONE |
| 92 |
}) |
| 93 |
@Retention(RetentionPolicy.SOURCE) |
| 94 |
@interface SystemUpdatePolicyType {} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
private static final int TYPE_UNKNOWN = -1; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public static final int TYPE_INSTALL_AUTOMATIC = 1; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public static final int TYPE_INSTALL_WINDOWED = 2; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
nd |
| 127 |
em |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public static final int TYPE_POSTPONE = 3; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
@SystemApi |
| 147 |
public static final int TYPE_PAUSE = 4; |
| 148 |
|
| 149 |
private static final String KEY_POLICY_TYPE = "policy_type"; |
| 150 |
private static final String KEY_INSTALL_WINDOW_START = "install_window_start"; |
| 151 |
private static final String KEY_INSTALL_WINDOW_END = "install_window_end"; |
| 152 |
private static final String KEY_FREEZE_TAG = "freeze"; |
| 153 |
private static final String KEY_FREEZE_START = "start"; |
| 154 |
private static final String KEY_FREEZE_END = "end"; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
private static final int WINDOW_BOUNDARY = 24 * 60; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
static final int FREEZE_PERIOD_MAX_LENGTH = 90; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
static final int FREEZE_PERIOD_MIN_SEPARATION = 60; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public static final class ValidationFailedException extends IllegalArgumentException |
| 179 |
implements Parcelable { |
| 180 |
|
| 181 |
|
| 182 |
@IntDef(prefix = { "ERROR_" }, value = { |
| 183 |
ERROR_NONE, |
| 184 |
ERROR_DUPLICATE_OR_OVERLAP, |
| 185 |
ERROR_NEW_FREEZE_PERIOD_TOO_LONG, |
| 186 |
ERROR_NEW_FREEZE_PERIOD_TOO_CLOSE, |
| 187 |
ERROR_COMBINED_FREEZE_PERIOD_TOO_LONG, |
| 188 |
ERROR_COMBINED_FREEZE_PERIOD_TOO_CLOSE, |
| 189 |
ERROR_UNKNOWN, |
| 190 |
}) |
| 191 |
@Retention(RetentionPolicy.SOURCE) |
| 192 |
@interface ValidationFailureType {} |
| 193 |
|
| 194 |
|
| 195 |
public static final int ERROR_NONE = 0; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public static final int ERROR_UNKNOWN = 1; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public static final int ERROR_DUPLICATE_OR_OVERLAP = 2; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public static final int ERROR_NEW_FREEZE_PERIOD_TOO_LONG = 3; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public static final int ERROR_NEW_FREEZE_PERIOD_TOO_CLOSE = 4; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public static final int ERROR_COMBINED_FREEZE_PERIOD_TOO_LONG = 5; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public static final int ERROR_COMBINED_FREEZE_PERIOD_TOO_CLOSE = 6; |
| 230 |
|
| 231 |
@ValidationFailureType |
| 232 |
private final int mErrorCode; |
| 233 |
|
| 234 |
private ValidationFailedException(int errorCode, String message) { |
| 235 |
super(message); |
| 236 |
mErrorCode = errorCode; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public @ValidationFailureType int getErrorCode() { |
| 243 |
return mErrorCode; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
public static ValidationFailedException duplicateOrOverlapPeriods() { |
| 248 |
return new ValidationFailedException(ERROR_DUPLICATE_OR_OVERLAP, |
| 249 |
"Found duplicate or overlapping periods"); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
public static ValidationFailedException freezePeriodTooLong(String message) { |
| 254 |
return new ValidationFailedException(ERROR_NEW_FREEZE_PERIOD_TOO_LONG, message); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
public static ValidationFailedException freezePeriodTooClose(String message) { |
| 259 |
return new ValidationFailedException(ERROR_NEW_FREEZE_PERIOD_TOO_CLOSE, message); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
public static ValidationFailedException combinedPeriodTooLong(String message) { |
| 264 |
return new ValidationFailedException(ERROR_COMBINED_FREEZE_PERIOD_TOO_LONG, message); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
public static ValidationFailedException combinedPeriodTooClose(String message) { |
| 269 |
return new ValidationFailedException(ERROR_COMBINED_FREEZE_PERIOD_TOO_CLOSE, message); |
| 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(mErrorCode); |
| 280 |
dest.writeString(getMessage()); |
| 281 |
} |
| 282 |
|
| 283 |
public static final @android.annotation.NonNull Parcelable.Creator<ValidationFailedException> CREATOR = |
| 284 |
new Parcelable.Creator<ValidationFailedException>() { |
| 285 |
@Override |
| 286 |
public ValidationFailedException createFromParcel(Parcel source) { |
| 287 |
return new ValidationFailedException(source.readInt(), source.readString()); |
| 288 |
} |
| 289 |
|
| 290 |
@Override |
| 291 |
public ValidationFailedException[] newArray(int size) { |
| 292 |
return new ValidationFailedException[size]; |
| 293 |
} |
| 294 |
|
| 295 |
}; |
| 296 |
} |
| 297 |
|
| 298 |
@SystemUpdatePolicyType |
| 299 |
private int mPolicyType; |
| 300 |
|
| 301 |
private int mMaintenanceWindowStart; |
| 302 |
private int mMaintenanceWindowEnd; |
| 303 |
|
| 304 |
private final ArrayList<FreezePeriod> mFreezePeriods; |
| 305 |
|
| 306 |
private SystemUpdatePolicy() { |
| 307 |
mPolicyType = TYPE_UNKNOWN; |
| 308 |
mFreezePeriods = new ArrayList<>(); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
public static SystemUpdatePolicy createAutomaticInstallPolicy() { |
| 318 |
SystemUpdatePolicy policy = new SystemUpdatePolicy(); |
| 319 |
policy.mPolicyType = TYPE_INSTALL_AUTOMATIC; |
| 320 |
return policy; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public static SystemUpdatePolicy createWindowedInstallPolicy(int startTime, int endTime) { |
| 344 |
if (startTime < 0 || startTime >= WINDOW_BOUNDARY |
| 345 |
|| endTime < 0 || endTime >= WINDOW_BOUNDARY) { |
| 346 |
throw new IllegalArgumentException("startTime and endTime must be inside [0, 1440)"); |
| 347 |
} |
| 348 |
SystemUpdatePolicy policy = new SystemUpdatePolicy(); |
| 349 |
policy.mPolicyType = TYPE_INSTALL_WINDOWED; |
| 350 |
policy.mMaintenanceWindowStart = startTime; |
| 351 |
policy.mMaintenanceWindowEnd = endTime; |
| 352 |
return policy; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public static SystemUpdatePolicy createPostponeInstallPolicy() { |
| 365 |
SystemUpdatePolicy policy = new SystemUpdatePolicy(); |
| 366 |
policy.mPolicyType = TYPE_POSTPONE; |
| 367 |
return policy; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
@SystemUpdatePolicyType |
| 376 |
public int getPolicyType() { |
| 377 |
return mPolicyType; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
public int getInstallWindowStart() { |
| 387 |
if (mPolicyType == TYPE_INSTALL_WINDOWED) { |
| 388 |
return mMaintenanceWindowStart; |
| 389 |
} else { |
| 390 |
return -1; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public int getInstallWindowEnd() { |
| 401 |
if (mPolicyType == TYPE_INSTALL_WINDOWED) { |
| 402 |
return mMaintenanceWindowEnd; |
| 403 |
} else { |
| 404 |
return -1; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public boolean isValid() { |
| 416 |
try { |
| 417 |
validateType(); |
| 418 |
validateFreezePeriods(); |
| 419 |
return true; |
| 420 |
} catch (IllegalArgumentException e) { |
| 421 |
return false; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public void validateType() { |
| 431 |
if (mPolicyType == TYPE_INSTALL_AUTOMATIC || mPolicyType == TYPE_POSTPONE) { |
| 432 |
return; |
| 433 |
} else if (mPolicyType == TYPE_INSTALL_WINDOWED) { |
| 434 |
if (!(mMaintenanceWindowStart >= 0 && mMaintenanceWindowStart < WINDOW_BOUNDARY |
| 435 |
&& mMaintenanceWindowEnd >= 0 && mMaintenanceWindowEnd < WINDOW_BOUNDARY)) { |
| 436 |
throw new IllegalArgumentException("Invalid maintenance window"); |
| 437 |
} |
| 438 |
} else { |
| 439 |
throw new IllegalArgumentException("Invalid system update policy type."); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public SystemUpdatePolicy setFreezePeriods(List<FreezePeriod> freezePeriods) { |
| 471 |
FreezePeriod.validatePeriods(freezePeriods); |
| 472 |
mFreezePeriods.clear(); |
| 473 |
mFreezePeriods.addAll(freezePeriods); |
| 474 |
return this; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
public List<FreezePeriod> getFreezePeriods() { |
| 483 |
return Collections.unmodifiableList(mFreezePeriods); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
public Pair<LocalDate, LocalDate> getCurrentFreezePeriod(LocalDate now) { |
| 492 |
for (FreezePeriod interval : mFreezePeriods) { |
| 493 |
if (interval.contains(now)) { |
| 494 |
return interval.toCurrentOrFutureRealDates(now); |
| 495 |
} |
| 496 |
} |
| 497 |
return null; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
private long timeUntilNextFreezePeriod(long now) { |
| 505 |
List<FreezePeriod> sortedPeriods = FreezePeriod.canonicalizePeriods(mFreezePeriods); |
| 506 |
LocalDate nowDate = millisToDate(now); |
| 507 |
LocalDate nextFreezeStart = null; |
| 508 |
for (FreezePeriod interval : sortedPeriods) { |
| 509 |
if (interval.after(nowDate)) { |
| 510 |
nextFreezeStart = interval.toCurrentOrFutureRealDates(nowDate).first; |
| 511 |
break; |
| 512 |
} else if (interval.contains(nowDate)) { |
| 513 |
throw new IllegalArgumentException("Given date is inside a freeze period"); |
| 514 |
} |
| 515 |
} |
| 516 |
if (nextFreezeStart == null) { |
| 517 |
|
| 518 |
|
| 519 |
nextFreezeStart = sortedPeriods.get(0).toCurrentOrFutureRealDates(nowDate).first; |
| 520 |
} |
| 521 |
return dateToMillis(nextFreezeStart) - now; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
public void validateFreezePeriods() { |
| 526 |
FreezePeriod.validatePeriods(mFreezePeriods); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
public void validateAgainstPreviousFreezePeriod(LocalDate prevPeriodStart, |
| 531 |
LocalDate prevPeriodEnd, LocalDate now) { |
| 532 |
FreezePeriod.validateAgainstPreviousFreezePeriod(mFreezePeriods, prevPeriodStart, |
| 533 |
prevPeriodEnd, now); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
@SystemApi |
| 554 |
public static class InstallationOption { |
| 555 |
|
| 556 |
@IntDef(prefix = { "TYPE_" }, value = { |
| 557 |
TYPE_INSTALL_AUTOMATIC, |
| 558 |
TYPE_PAUSE, |
| 559 |
TYPE_POSTPONE |
| 560 |
}) |
| 561 |
@Retention(RetentionPolicy.SOURCE) |
| 562 |
@interface InstallationOptionType {} |
| 563 |
|
| 564 |
@InstallationOptionType |
| 565 |
private final int mType; |
| 566 |
private long mEffectiveTime; |
| 567 |
|
| 568 |
InstallationOption(@InstallationOptionType int type, long effectiveTime) { |
| 569 |
this.mType = type; |
| 570 |
this.mEffectiveTime = effectiveTime; |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
public @InstallationOptionType int getType() { |
| 579 |
return mType; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
public long getEffectiveTime() { |
| 588 |
return mEffectiveTime; |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
protected void limitEffectiveTime(long otherTime) { |
| 593 |
mEffectiveTime = Long.min(mEffectiveTime, otherTime); |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
@SystemApi |
| 615 |
public InstallationOption getInstallationOptionAt(long when) { |
| 616 |
LocalDate whenDate = millisToDate(when); |
| 617 |
Pair<LocalDate, LocalDate> current = getCurrentFreezePeriod(whenDate); |
| 618 |
if (current != null) { |
| 619 |
return new InstallationOption(TYPE_PAUSE, |
| 620 |
dateToMillis(roundUpLeapDay(current.second).plusDays(1)) - when); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
InstallationOption option = getInstallationOptionRegardlessFreezeAt(when); |
| 626 |
if (mFreezePeriods.size() > 0) { |
| 627 |
option.limitEffectiveTime(timeUntilNextFreezePeriod(when)); |
| 628 |
} |
| 629 |
return option; |
| 630 |
} |
| 631 |
|
| 632 |
private InstallationOption getInstallationOptionRegardlessFreezeAt(long when) { |
| 633 |
if (mPolicyType == TYPE_INSTALL_AUTOMATIC || mPolicyType == TYPE_POSTPONE) { |
| 634 |
return new InstallationOption(mPolicyType, Long.MAX_VALUE); |
| 635 |
} else if (mPolicyType == TYPE_INSTALL_WINDOWED) { |
| 636 |
Calendar query = Calendar.getInstance(); |
| 637 |
query.setTimeInMillis(when); |
| 638 |
|
| 639 |
long whenMillis = TimeUnit.HOURS.toMillis(query.get(Calendar.HOUR_OF_DAY)) |
| 640 |
+ TimeUnit.MINUTES.toMillis(query.get(Calendar.MINUTE)) |
| 641 |
+ TimeUnit.SECONDS.toMillis(query.get(Calendar.SECOND)) |
| 642 |
+ query.get(Calendar.MILLISECOND); |
| 643 |
long windowStartMillis = TimeUnit.MINUTES.toMillis(mMaintenanceWindowStart); |
| 644 |
long windowEndMillis = TimeUnit.MINUTES.toMillis(mMaintenanceWindowEnd); |
| 645 |
final long dayInMillis = TimeUnit.DAYS.toMillis(1); |
| 646 |
|
| 647 |
if ((windowStartMillis <= whenMillis && whenMillis <= windowEndMillis) |
| 648 |
|| ((windowStartMillis > windowEndMillis) |
| 649 |
&& (windowStartMillis <= whenMillis || whenMillis <= windowEndMillis))) { |
| 650 |
return new InstallationOption(TYPE_INSTALL_AUTOMATIC, |
| 651 |
(windowEndMillis - whenMillis + dayInMillis) % dayInMillis); |
| 652 |
} else { |
| 653 |
return new InstallationOption(TYPE_PAUSE, |
| 654 |
(windowStartMillis - whenMillis + dayInMillis) % dayInMillis); |
| 655 |
} |
| 656 |
} else { |
| 657 |
throw new RuntimeException("Unknown policy type"); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
private static LocalDate roundUpLeapDay(LocalDate date) { |
| 662 |
if (date.isLeapYear() && date.getMonthValue() == 2 && date.getDayOfMonth() == 28) { |
| 663 |
return date.plusDays(1); |
| 664 |
} else { |
| 665 |
return date; |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
private static LocalDate millisToDate(long when) { |
| 673 |
return Instant.ofEpochMilli(when).atZone(ZoneId.systemDefault()).toLocalDate(); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
private static long dateToMillis(LocalDate when) { |
| 680 |
return LocalDateTime.of(when, LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant() |
| 681 |
.toEpochMilli(); |
| 682 |
} |
| 683 |
|
| 684 |
@Override |
| 685 |
public String toString() { |
| 686 |
return String.format("SystemUpdatePolicy (type: %d, windowStart: %d, windowEnd: %d, " |
| 687 |
+ "freezes: [%s])", |
| 688 |
mPolicyType, mMaintenanceWindowStart, mMaintenanceWindowEnd, |
| 689 |
mFreezePeriods.stream().map(n -> n.toString()).collect(Collectors.joining(","))); |
| 690 |
} |
| 691 |
|
| 692 |
@Override |
| 693 |
public int describeContents() { |
| 694 |
return 0; |
| 695 |
} |
| 696 |
|
| 697 |
@Override |
| 698 |
public void writeToParcel(Parcel dest, int flags) { |
| 699 |
dest.writeInt(mPolicyType); |
| 700 |
dest.writeInt(mMaintenanceWindowStart); |
| 701 |
dest.writeInt(mMaintenanceWindowEnd); |
| 702 |
int freezeCount = mFreezePeriods.size(); |
| 703 |
dest.writeInt(freezeCount); |
| 704 |
for (int i = 0; i < freezeCount; i++) { |
| 705 |
FreezePeriod interval = mFreezePeriods.get(i); |
| 706 |
dest.writeInt(interval.getStart().getMonthValue()); |
| 707 |
dest.writeInt(interval.getStart().getDayOfMonth()); |
| 708 |
dest.writeInt(interval.getEnd().getMonthValue()); |
| 709 |
dest.writeInt(interval.getEnd().getDayOfMonth()); |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
public static final @android.annotation.NonNull Parcelable.Creator<SystemUpdatePolicy> CREATOR = |
| 714 |
new Parcelable.Creator<SystemUpdatePolicy>() { |
| 715 |
|
| 716 |
@Override |
| 717 |
public SystemUpdatePolicy createFromParcel(Parcel source) { |
| 718 |
SystemUpdatePolicy policy = new SystemUpdatePolicy(); |
| 719 |
policy.mPolicyType = source.readInt(); |
| 720 |
policy.mMaintenanceWindowStart = source.readInt(); |
| 721 |
policy.mMaintenanceWindowEnd = source.readInt(); |
| 722 |
int freezeCount = source.readInt(); |
| 723 |
policy.mFreezePeriods.ensureCapacity(freezeCount); |
| 724 |
for (int i = 0; i < freezeCount; i++) { |
| 725 |
MonthDay start = MonthDay.of(source.readInt(), source.readInt()); |
| 726 |
MonthDay end = MonthDay.of(source.readInt(), source.readInt()); |
| 727 |
policy.mFreezePeriods.add(new FreezePeriod(start, end)); |
| 728 |
} |
| 729 |
return policy; |
| 730 |
} |
| 731 |
|
| 732 |
@Override |
| 733 |
public SystemUpdatePolicy[] newArray(int size) { |
| 734 |
return new SystemUpdatePolicy[size]; |
| 735 |
} |
| 736 |
}; |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
public static SystemUpdatePolicy restoreFromXml(TypedXmlPullParser parser) { |
| 745 |
try { |
| 746 |
SystemUpdatePolicy policy = new SystemUpdatePolicy(); |
| 747 |
policy.mPolicyType = |
| 748 |
parser.getAttributeInt(null, KEY_POLICY_TYPE, TYPE_UNKNOWN); |
| 749 |
policy.mMaintenanceWindowStart = |
| 750 |
parser.getAttributeInt(null, KEY_INSTALL_WINDOW_START, 0); |
| 751 |
policy.mMaintenanceWindowEnd = |
| 752 |
parser.getAttributeInt(null, KEY_INSTALL_WINDOW_END, 0); |
| 753 |
|
| 754 |
int outerDepth = parser.getDepth(); |
| 755 |
int type; |
| 756 |
while ((type = parser.next()) != END_DOCUMENT |
| 757 |
&& (type != END_TAG || parser.getDepth() > outerDepth)) { |
| 758 |
if (type == END_TAG || type == TEXT) { |
| 759 |
continue; |
| 760 |
} |
| 761 |
if (!parser.getName().equals(KEY_FREEZE_TAG)) { |
| 762 |
continue; |
| 763 |
} |
| 764 |
policy.mFreezePeriods.add(new FreezePeriod( |
| 765 |
MonthDay.parse(parser.getAttributeValue(null, KEY_FREEZE_START)), |
| 766 |
MonthDay.parse(parser.getAttributeValue(null, KEY_FREEZE_END)))); |
| 767 |
} |
| 768 |
return policy; |
| 769 |
} catch (NumberFormatException | XmlPullParserException | IOException e) { |
| 770 |
|
| 771 |
Log.w(TAG, "Load xml failed", e); |
| 772 |
} |
| 773 |
return null; |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
public void saveToXml(TypedXmlSerializer out) throws IOException { |
| 780 |
out.attributeInt(null, KEY_POLICY_TYPE, mPolicyType); |
| 781 |
out.attributeInt(null, KEY_INSTALL_WINDOW_START, mMaintenanceWindowStart); |
| 782 |
out.attributeInt(null, KEY_INSTALL_WINDOW_END, mMaintenanceWindowEnd); |
| 783 |
for (int i = 0; i < mFreezePeriods.size(); i++) { |
| 784 |
FreezePeriod interval = mFreezePeriods.get(i); |
| 785 |
out.startTag(null, KEY_FREEZE_TAG); |
| 786 |
out.attribute(null, KEY_FREEZE_START, interval.getStart().toString()); |
| 787 |
out.attribute(null, KEY_FREEZE_END, interval.getEnd().toString()); |
| 788 |
out.endTag(null, KEY_FREEZE_TAG); |
| 789 |
} |
| 790 |
} |
| 791 |
} |
| 792 |
|