ScheduleCalendar.java revision 7f73308f50791740beb380fecc17903301054ad3
1/** 2 * Copyright (c) 2014, 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 17package com.android.server.notification; 18 19import android.service.notification.ZenModeConfig.ScheduleInfo; 20import android.util.ArraySet; 21 22import java.util.Calendar; 23import java.util.Objects; 24import java.util.TimeZone; 25 26public class ScheduleCalendar { 27 private final ArraySet<Integer> mDays = new ArraySet<Integer>(); 28 private final Calendar mCalendar = Calendar.getInstance(); 29 30 private ScheduleInfo mSchedule; 31 32 @Override 33 public String toString() { 34 return "ScheduleCalendar[mDays=" + mDays + ", mSchedule=" + mSchedule + "]"; 35 } 36 37 public void setSchedule(ScheduleInfo schedule) { 38 if (Objects.equals(mSchedule, schedule)) return; 39 mSchedule = schedule; 40 updateDays(); 41 } 42 43 public void maybeSetNextAlarm(long now, long nextAlarm) { 44 if (mSchedule != null) { 45 if (mSchedule.exitAtAlarm && now > mSchedule.nextAlarm) { 46 mSchedule.nextAlarm = nextAlarm; 47 } 48 } 49 } 50 51 public void setTimeZone(TimeZone tz) { 52 mCalendar.setTimeZone(tz); 53 } 54 55 public long getNextChangeTime(long now) { 56 if (mSchedule == null) return 0; 57 final long nextStart = getNextTime(now, mSchedule.startHour, mSchedule.startMinute); 58 final long nextEnd = getNextTime(now, mSchedule.endHour, mSchedule.endMinute); 59 long nextScheduleTime = Math.min(nextStart, nextEnd); 60 61 if (mSchedule.exitAtAlarm && mSchedule.nextAlarm > now) { 62 return Math.min(nextScheduleTime, mSchedule.nextAlarm); 63 } else { 64 return nextScheduleTime; 65 } 66 } 67 68 private long getNextTime(long now, int hr, int min) { 69 final long time = getTime(now, hr, min); 70 return time <= now ? addDays(time, 1) : time; 71 } 72 73 private long getTime(long millis, int hour, int min) { 74 mCalendar.setTimeInMillis(millis); 75 mCalendar.set(Calendar.HOUR_OF_DAY, hour); 76 mCalendar.set(Calendar.MINUTE, min); 77 mCalendar.set(Calendar.SECOND, 0); 78 mCalendar.set(Calendar.MILLISECOND, 0); 79 return mCalendar.getTimeInMillis(); 80 } 81 82 public boolean isInSchedule(long time) { 83 if (mSchedule == null || mDays.size() == 0) return false; 84 final long start = getTime(time, mSchedule.startHour, mSchedule.startMinute); 85 long end = getTime(time, mSchedule.endHour, mSchedule.endMinute); 86 if (end <= start) { 87 end = addDays(end, 1); 88 } 89 boolean isInSchedule = 90 isInSchedule(-1, time, start, end) || isInSchedule(0, time, start, end); 91 if (isInSchedule && mSchedule.exitAtAlarm 92 && mSchedule.nextAlarm != 0 93 && time >= mSchedule.nextAlarm) { 94 return false; 95 } else { 96 return isInSchedule; 97 } 98 } 99 100 private boolean isInSchedule(int daysOffset, long time, long start, long end) { 101 final int n = Calendar.SATURDAY; 102 final int day = ((getDayOfWeek(time) - 1) + (daysOffset % n) + n) % n + 1; 103 start = addDays(start, daysOffset); 104 end = addDays(end, daysOffset); 105 return mDays.contains(day) && time >= start && time < end; 106 } 107 108 private int getDayOfWeek(long time) { 109 mCalendar.setTimeInMillis(time); 110 return mCalendar.get(Calendar.DAY_OF_WEEK); 111 } 112 113 private void updateDays() { 114 mDays.clear(); 115 if (mSchedule != null && mSchedule.days != null) { 116 for (int i = 0; i < mSchedule.days.length; i++) { 117 mDays.add(mSchedule.days[i]); 118 } 119 } 120 } 121 122 private long addDays(long time, int days) { 123 mCalendar.setTimeInMillis(time); 124 mCalendar.add(Calendar.DATE, days); 125 return mCalendar.getTimeInMillis(); 126 } 127}