| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.notification; |
| 18 |
|
| 19 |
import android.service.notification.ZenModeConfig.ScheduleInfo; |
| 20 |
import android.util.ArraySet; |
| 21 |
import android.util.Log; |
| 22 |
|
| 23 |
import com.android.internal.annotations.VisibleForTesting; |
| 24 |
|
| 25 |
import java.util.Calendar; |
| 26 |
import java.util.Objects; |
| 27 |
import java.util.TimeZone; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class ScheduleCalendar { |
| 33 |
public static final String TAG = "ScheduleCalendar"; |
| 34 |
public static final boolean DEBUG = Log.isLoggable("ConditionProviders", Log.DEBUG); |
| 35 |
private final ArraySet<Integer> mDays = new ArraySet<Integer>(); |
| 36 |
private final Calendar mCalendar = Calendar.getInstance(); |
| 37 |
|
| 38 |
private ScheduleInfo mSchedule; |
| 39 |
|
| 40 |
@Override |
| 41 |
public String toString() { |
| 42 |
return "ScheduleCalendar[mDays=" + mDays + ", mSchedule=" + mSchedule + "]"; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public boolean exitAtAlarm() { |
| 49 |
return mSchedule.exitAtAlarm; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public void setSchedule(ScheduleInfo schedule) { |
| 56 |
if (Objects.equals(mSchedule, schedule)) return; |
| 57 |
mSchedule = schedule; |
| 58 |
updateDays(); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public void maybeSetNextAlarm(long now, long nextAlarm) { |
| 67 |
if (mSchedule != null && mSchedule.exitAtAlarm) { |
| 68 |
if (nextAlarm == 0) { |
| 69 |
|
| 70 |
mSchedule.nextAlarm = 0; |
| 71 |
} else if (nextAlarm > now) { |
| 72 |
|
| 73 |
mSchedule.nextAlarm = nextAlarm; |
| 74 |
} else if (mSchedule.nextAlarm < now) { |
| 75 |
if (DEBUG) { |
| 76 |
Log.d(TAG, "All alarms are in the past " + mSchedule.nextAlarm); |
| 77 |
} |
| 78 |
mSchedule.nextAlarm = 0; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public void setTimeZone(TimeZone tz) { |
| 88 |
mCalendar.setTimeZone(tz); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public long getNextChangeTime(long now) { |
| 96 |
if (mSchedule == null) return 0; |
| 97 |
final long nextStart = getNextTime(now, mSchedule.startHour, mSchedule.startMinute, true); |
| 98 |
final long nextEnd = getNextTime(now, mSchedule.endHour, mSchedule.endMinute, false); |
| 99 |
long nextScheduleTime = Math.min(nextStart, nextEnd); |
| 100 |
|
| 101 |
return nextScheduleTime; |
| 102 |
} |
| 103 |
|
| 104 |
private long getNextTime(long now, int hr, int min, boolean adjust) { |
| 105 |
|
| 106 |
|
| 107 |
final long time = adjust ? getClosestActualTime(now, hr, min) : getTime(now, hr, min); |
| 108 |
if (time <= now) { |
| 109 |
final long tomorrow = addDays(time, 1); |
| 110 |
return adjust ? getClosestActualTime(tomorrow, hr, min) : getTime(tomorrow, hr, min); |
| 111 |
} |
| 112 |
return time; |
| 113 |
} |
| 114 |
|
| 115 |
private long getTime(long millis, int hour, int min) { |
| 116 |
mCalendar.setTimeInMillis(millis); |
| 117 |
mCalendar.set(Calendar.HOUR_OF_DAY, hour); |
| 118 |
mCalendar.set(Calendar.MINUTE, min); |
| 119 |
mCalendar.set(Calendar.SECOND, 0); |
| 120 |
mCalendar.set(Calendar.MILLISECOND, 0); |
| 121 |
return mCalendar.getTimeInMillis(); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public boolean isInSchedule(long time) { |
| 129 |
if (mSchedule == null || mDays.size() == 0) return false; |
| 130 |
final long start = getClosestActualTime(time, mSchedule.startHour, mSchedule.startMinute); |
| 131 |
long end = getTime(time, mSchedule.endHour, mSchedule.endMinute); |
| 132 |
if (end <= start) { |
| 133 |
end = addDays(end, 1); |
| 134 |
} |
| 135 |
return isInSchedule(-1, time, start, end) || isInSchedule(0, time, start, end); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public boolean isAlarmInSchedule(long alarm, long now) { |
| 144 |
if (mSchedule == null || mDays.size() == 0) return false; |
| 145 |
final long start = getClosestActualTime(alarm, mSchedule.startHour, mSchedule.startMinute); |
| 146 |
long end = getTime(alarm, mSchedule.endHour, mSchedule.endMinute); |
| 147 |
if (end <= start) { |
| 148 |
end = addDays(end, 1); |
| 149 |
} |
| 150 |
return (isInSchedule(-1, alarm, start, end) |
| 151 |
&& isInSchedule(-1, now, start, end)) |
| 152 |
|| (isInSchedule(0, alarm, start, end) |
| 153 |
&& isInSchedule(0, now, start, end)); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public boolean shouldExitForAlarm(long time) { |
| 161 |
if (mSchedule == null) { |
| 162 |
return false; |
| 163 |
} |
| 164 |
return mSchedule.exitAtAlarm |
| 165 |
&& mSchedule.nextAlarm != 0 |
| 166 |
&& time >= mSchedule.nextAlarm |
| 167 |
&& isAlarmInSchedule(mSchedule.nextAlarm, time); |
| 168 |
} |
| 169 |
|
| 170 |
private boolean isInSchedule(int daysOffset, long time, long start, long end) { |
| 171 |
final int n = Calendar.SATURDAY; |
| 172 |
final int day = ((getDayOfWeek(time) - 1) + (daysOffset % n) + n) % n + 1; |
| 173 |
start = addDays(start, daysOffset); |
| 174 |
end = addDays(end, daysOffset); |
| 175 |
return mDays.contains(day) && time >= start && time < end; |
| 176 |
} |
| 177 |
|
| 178 |
private int getDayOfWeek(long time) { |
| 179 |
mCalendar.setTimeInMillis(time); |
| 180 |
return mCalendar.get(Calendar.DAY_OF_WEEK); |
| 181 |
} |
| 182 |
|
| 183 |
private void updateDays() { |
| 184 |
mDays.clear(); |
| 185 |
if (mSchedule != null && mSchedule.days != null) { |
| 186 |
for (int i = 0; i < mSchedule.days.length; i++) { |
| 187 |
mDays.add(mSchedule.days[i]); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
private long addDays(long time, int days) { |
| 193 |
mCalendar.setTimeInMillis(time); |
| 194 |
mCalendar.add(Calendar.DATE, days); |
| 195 |
return mCalendar.getTimeInMillis(); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
@VisibleForTesting |
| 214 |
public long getClosestActualTime(long refTime, int hour, int min) { |
| 215 |
long resTime = getTime(refTime, hour, min); |
| 216 |
if (!mCalendar.getTimeZone().observesDaylightTime()) { |
| 217 |
|
| 218 |
return resTime; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
mCalendar.setTimeInMillis(resTime); |
| 227 |
int resHr = mCalendar.get(Calendar.HOUR_OF_DAY); |
| 228 |
int resMin = mCalendar.get(Calendar.MINUTE); |
| 229 |
if (resHr == hour + 1 && resMin == min) { |
| 230 |
return getTime(refTime, resHr, 0); |
| 231 |
} |
| 232 |
return resTime; |
| 233 |
} |
| 234 |
} |