| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.app.admin; |
| 17 |
|
| 18 |
import android.app.admin.SystemUpdatePolicy.ValidationFailedException; |
| 19 |
import android.util.Log; |
| 20 |
import android.util.Pair; |
| 21 |
|
| 22 |
import java.time.LocalDate; |
| 23 |
import java.time.MonthDay; |
| 24 |
import java.time.format.DateTimeFormatter; |
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.List; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class FreezePeriod { |
| 40 |
private static final String TAG = "FreezePeriod"; |
| 41 |
|
| 42 |
private static final int SENTINEL_YEAR = 2001; |
| 43 |
static final int DAYS_IN_YEAR = 365; |
| 44 |
|
| 45 |
private final MonthDay mStart; |
| 46 |
private final MonthDay mEnd; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private final int mStartDay; |
| 55 |
private final int mEndDay; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public FreezePeriod(MonthDay start, MonthDay end) { |
| 62 |
mStart = start; |
| 63 |
mStartDay = mStart.atYear(SENTINEL_YEAR).getDayOfYear(); |
| 64 |
mEnd = end; |
| 65 |
mEndDay = mEnd.atYear(SENTINEL_YEAR).getDayOfYear(); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public MonthDay getStart() { |
| 72 |
return mStart; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public MonthDay getEnd() { |
| 79 |
return mEnd; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
private FreezePeriod(int startDay, int endDay) { |
| 86 |
mStartDay = startDay; |
| 87 |
mStart = dayOfYearToMonthDay(startDay); |
| 88 |
mEndDay = endDay; |
| 89 |
mEnd = dayOfYearToMonthDay(endDay); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
int getLength() { |
| 94 |
return getEffectiveEndDay() - mStartDay + 1; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
boolean isWrapped() { |
| 99 |
return mEndDay < mStartDay; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
int getEffectiveEndDay() { |
| 107 |
if (!isWrapped()) { |
| 108 |
return mEndDay; |
| 109 |
} else { |
| 110 |
return mEndDay + DAYS_IN_YEAR; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
boolean contains(LocalDate localDate) { |
| 116 |
final int daysOfYear = dayOfYearDisregardLeapYear(localDate); |
| 117 |
if (!isWrapped()) { |
| 118 |
|
| 119 |
return (mStartDay <= daysOfYear) && (daysOfYear <= mEndDay); |
| 120 |
} else { |
| 121 |
|
| 122 |
|
| 123 |
return (mStartDay <= daysOfYear) || (daysOfYear <= mEndDay); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
boolean after(LocalDate localDate) { |
| 129 |
return mStartDay > dayOfYearDisregardLeapYear(localDate); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
Pair<LocalDate, LocalDate> toCurrentOrFutureRealDates(LocalDate now) { |
| 142 |
final int nowDays = dayOfYearDisregardLeapYear(now); |
| 143 |
final int startYearAdjustment, endYearAdjustment; |
| 144 |
if (contains(now)) { |
| 145 |
|
| 146 |
if (mStartDay <= nowDays) { |
| 147 |
|
| 148 |
|
| 149 |
startYearAdjustment = 0; |
| 150 |
endYearAdjustment = isWrapped() ? 1 : 0; |
| 151 |
} else { |
| 152 |
|
| 153 |
startYearAdjustment = -1; |
| 154 |
endYearAdjustment = 0; |
| 155 |
} |
| 156 |
} else { |
| 157 |
|
| 158 |
if (mStartDay > nowDays) { |
| 159 |
|
| 160 |
|
| 161 |
startYearAdjustment = 0; |
| 162 |
endYearAdjustment = isWrapped() ? 1 : 0; |
| 163 |
} else { |
| 164 |
|
| 165 |
startYearAdjustment = 1; |
| 166 |
endYearAdjustment = 1; |
| 167 |
} |
| 168 |
} |
| 169 |
final LocalDate startDate = LocalDate.ofYearDay(SENTINEL_YEAR, mStartDay).withYear( |
| 170 |
now.getYear() + startYearAdjustment); |
| 171 |
final LocalDate endDate = LocalDate.ofYearDay(SENTINEL_YEAR, mEndDay).withYear( |
| 172 |
now.getYear() + endYearAdjustment); |
| 173 |
return new Pair<>(startDate, endDate); |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public String toString() { |
| 178 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd"); |
| 179 |
return LocalDate.ofYearDay(SENTINEL_YEAR, mStartDay).format(formatter) + " - " |
| 180 |
+ LocalDate.ofYearDay(SENTINEL_YEAR, mEndDay).format(formatter); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
private static MonthDay dayOfYearToMonthDay(int dayOfYear) { |
| 185 |
LocalDate date = LocalDate.ofYearDay(SENTINEL_YEAR, dayOfYear); |
| 186 |
return MonthDay.of(date.getMonth(), date.getDayOfMonth()); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
private static int dayOfYearDisregardLeapYear(LocalDate date) { |
| 194 |
return date.withYear(SENTINEL_YEAR).getDayOfYear(); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public static int distanceWithoutLeapYear(LocalDate first, LocalDate second) { |
| 203 |
return dayOfYearDisregardLeapYear(first) - dayOfYearDisregardLeapYear(second) |
| 204 |
+ DAYS_IN_YEAR * (first.getYear() - second.getYear()); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
static List<FreezePeriod> canonicalizePeriods(List<FreezePeriod> intervals) { |
| 221 |
boolean[] taken = new boolean[DAYS_IN_YEAR]; |
| 222 |
|
| 223 |
for (FreezePeriod interval : intervals) { |
| 224 |
for (int i = interval.mStartDay; i <= interval.getEffectiveEndDay(); i++) { |
| 225 |
taken[(i - 1) % DAYS_IN_YEAR] = true; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
List<FreezePeriod> result = new ArrayList<>(); |
| 230 |
int i = 0; |
| 231 |
while (i < DAYS_IN_YEAR) { |
| 232 |
if (!taken[i]) { |
| 233 |
i++; |
| 234 |
continue; |
| 235 |
} |
| 236 |
final int intervalStart = i + 1; |
| 237 |
while (i < DAYS_IN_YEAR && taken[i]) i++; |
| 238 |
result.add(new FreezePeriod(intervalStart, i)); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
final int lastIndex = result.size() - 1; |
| 243 |
if (lastIndex > 0 && result.get(lastIndex).mEndDay == DAYS_IN_YEAR |
| 244 |
&& result.get(0).mStartDay == 1) { |
| 245 |
FreezePeriod wrappedInterval = new FreezePeriod(result.get(lastIndex).mStartDay, |
| 246 |
result.get(0).mEndDay); |
| 247 |
result.set(lastIndex, wrappedInterval); |
| 248 |
result.remove(0); |
| 249 |
} |
| 250 |
return result; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
static void validatePeriods(List<FreezePeriod> periods) { |
| 263 |
List<FreezePeriod> allPeriods = FreezePeriod.canonicalizePeriods(periods); |
| 264 |
if (allPeriods.size() != periods.size()) { |
| 265 |
throw SystemUpdatePolicy.ValidationFailedException.duplicateOrOverlapPeriods(); |
| 266 |
} |
| 267 |
for (int i = 0; i < allPeriods.size(); i++) { |
| 268 |
FreezePeriod current = allPeriods.get(i); |
| 269 |
if (current.getLength() > SystemUpdatePolicy.FREEZE_PERIOD_MAX_LENGTH) { |
| 270 |
throw SystemUpdatePolicy.ValidationFailedException.freezePeriodTooLong("Freeze " |
| 271 |
+ "period " + current + " is too long: " + current.getLength() + " days"); |
| 272 |
} |
| 273 |
FreezePeriod previous = i > 0 ? allPeriods.get(i - 1) |
| 274 |
: allPeriods.get(allPeriods.size() - 1); |
| 275 |
if (previous != current) { |
| 276 |
final int separation; |
| 277 |
if (i == 0 && !previous.isWrapped()) { |
| 278 |
|
| 279 |
separation = current.mStartDay |
| 280 |
+ (DAYS_IN_YEAR - previous.mEndDay) - 1; |
| 281 |
} else { |
| 282 |
|
| 283 |
|
| 284 |
separation = current.mStartDay - previous.mEndDay - 1; |
| 285 |
} |
| 286 |
if (separation < SystemUpdatePolicy.FREEZE_PERIOD_MIN_SEPARATION) { |
| 287 |
throw SystemUpdatePolicy.ValidationFailedException.freezePeriodTooClose("Freeze" |
| 288 |
+ " periods " + previous + " and " + current + " are too close " |
| 289 |
+ "together: " + separation + " days apart"); |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
static void validateAgainstPreviousFreezePeriod(List<FreezePeriod> periods, |
| 303 |
LocalDate prevPeriodStart, LocalDate prevPeriodEnd, LocalDate now) { |
| 304 |
if (periods.size() == 0 || prevPeriodStart == null || prevPeriodEnd == null) { |
| 305 |
return; |
| 306 |
} |
| 307 |
if (prevPeriodStart.isAfter(now) || prevPeriodEnd.isAfter(now)) { |
| 308 |
Log.w(TAG, "Previous period (" + prevPeriodStart + "," + prevPeriodEnd + ") is after" |
| 309 |
+ " current date " + now); |
| 310 |
|
| 311 |
|
| 312 |
} |
| 313 |
List<FreezePeriod> allPeriods = FreezePeriod.canonicalizePeriods(periods); |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
FreezePeriod curOrNextFreezePeriod = allPeriods.get(0); |
| 318 |
for (FreezePeriod interval : allPeriods) { |
| 319 |
if (interval.contains(now) |
| 320 |
|| interval.mStartDay > FreezePeriod.dayOfYearDisregardLeapYear(now)) { |
| 321 |
curOrNextFreezePeriod = interval; |
| 322 |
break; |
| 323 |
} |
| 324 |
} |
| 325 |
Pair<LocalDate, LocalDate> curOrNextFreezeDates = curOrNextFreezePeriod |
| 326 |
.toCurrentOrFutureRealDates(now); |
| 327 |
if (now.isAfter(curOrNextFreezeDates.first)) { |
| 328 |
curOrNextFreezeDates = new Pair<>(now, curOrNextFreezeDates.second); |
| 329 |
} |
| 330 |
if (curOrNextFreezeDates.first.isAfter(curOrNextFreezeDates.second)) { |
| 331 |
throw new IllegalStateException("Current freeze dates inverted: " |
| 332 |
+ curOrNextFreezeDates.first + "-" + curOrNextFreezeDates.second); |
| 333 |
} |
| 334 |
|
| 335 |
final String periodsDescription = "Prev: " + prevPeriodStart + "," + prevPeriodEnd |
| 336 |
+ "; cur: " + curOrNextFreezeDates.first + "," + curOrNextFreezeDates.second; |
| 337 |
long separation = FreezePeriod.distanceWithoutLeapYear(curOrNextFreezeDates.first, |
| 338 |
prevPeriodEnd) - 1; |
| 339 |
if (separation > 0) { |
| 340 |
|
| 341 |
if (separation < SystemUpdatePolicy.FREEZE_PERIOD_MIN_SEPARATION) { |
| 342 |
throw ValidationFailedException.combinedPeriodTooClose("Previous freeze period " |
| 343 |
+ "too close to new period: " + separation + ", " + periodsDescription); |
| 344 |
} |
| 345 |
} else { |
| 346 |
|
| 347 |
long length = FreezePeriod.distanceWithoutLeapYear(curOrNextFreezeDates.second, |
| 348 |
prevPeriodStart) + 1; |
| 349 |
if (length > SystemUpdatePolicy.FREEZE_PERIOD_MAX_LENGTH) { |
| 350 |
throw ValidationFailedException.combinedPeriodTooLong("Combined freeze period " |
| 351 |
+ "exceeds maximum days: " + length + ", " + periodsDescription); |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
} |