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