use of android.text.format.Time in project Etar-Calendar by Etar-Group.
the class AgendaWindowAdapter method doQuery.
private void doQuery(QuerySpec queryData) {
if (!mAdapterInfos.isEmpty()) {
int start = mAdapterInfos.getFirst().start;
int end = mAdapterInfos.getLast().end;
int queryDuration = calculateQueryDuration(start, end);
switch(queryData.queryType) {
case QUERY_TYPE_OLDER:
queryData.end = start - 1;
queryData.start = queryData.end - queryDuration;
break;
case QUERY_TYPE_NEWER:
queryData.start = end + 1;
queryData.end = queryData.start + queryDuration;
break;
}
// b/5311977
if (mRowCount < 20 && queryData.queryType != QUERY_TYPE_CLEAN) {
if (DEBUGLOG) {
Log.e(TAG, "Compacting cursor: mRowCount=" + mRowCount + " totalStart:" + start + " totalEnd:" + end + " query.start:" + queryData.start + " query.end:" + queryData.end);
}
queryData.queryType = QUERY_TYPE_CLEAN;
if (queryData.start > start) {
queryData.start = start;
}
if (queryData.end < end) {
queryData.end = end;
}
}
}
if (BASICLOG) {
Time time = new Time(mTimeZone);
time.setJulianDay(queryData.start);
Time time2 = new Time(mTimeZone);
time2.setJulianDay(queryData.end);
Log.v(TAG, "startQuery: " + time.toString() + " to " + time2.toString() + " then go to " + queryData.goToTime);
}
mQueryHandler.cancelOperation(0);
if (BASICLOG)
queryData.queryStartMillis = System.nanoTime();
Uri queryUri = buildQueryUri(queryData.start, queryData.end, queryData.searchQuery);
mQueryHandler.startQuery(0, queryData, queryUri, PROJECTION, buildQuerySelection(), null, AGENDA_SORT_ORDER);
}
use of android.text.format.Time in project Etar-Calendar by Etar-Group.
the class AgendaWindowAdapter method getAdapterInfoByTime.
private DayAdapterInfo getAdapterInfoByTime(Time time) {
if (DEBUGLOG)
Log.e(TAG, "getAdapterInfoByTime " + time.toString());
Time tmpTime = new Time(time);
long timeInMillis = tmpTime.normalize(true);
int day = Time.getJulianDay(timeInMillis, tmpTime.gmtoff);
synchronized (mAdapterInfos) {
for (DayAdapterInfo info : mAdapterInfos) {
if (info.start <= day && day <= info.end) {
return info;
}
}
}
return null;
}
use of android.text.format.Time in project Etar-Calendar by Etar-Group.
the class AlarmScheduler method queryUpcomingEvents.
/**
* Queries events starting within a fixed interval from now.
*/
private static Cursor queryUpcomingEvents(Context context, ContentResolver contentResolver, long currentMillis) {
Time time = new Time();
time.normalize(false);
long localOffset = time.gmtoff * 1000;
final long localStartMin = currentMillis;
final long localStartMax = localStartMin + EVENT_LOOKAHEAD_WINDOW_MS;
final long utcStartMin = localStartMin - localOffset;
final long utcStartMax = utcStartMin + EVENT_LOOKAHEAD_WINDOW_MS;
if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
//If permission is not granted then just return.
Log.d(TAG, "Manifest.permission.READ_CALENDAR is not granted");
return null;
}
// Expand Instances table range by a day on either end to account for
// all-day events.
Uri.Builder uriBuilder = Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(uriBuilder, localStartMin - DateUtils.DAY_IN_MILLIS);
ContentUris.appendId(uriBuilder, localStartMax + DateUtils.DAY_IN_MILLIS);
// Build query for all events starting within the fixed interval.
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("(");
queryBuilder.append(INSTANCES_WHERE);
queryBuilder.append(") OR (");
queryBuilder.append(INSTANCES_WHERE);
queryBuilder.append(")");
String[] queryArgs = new String[] { // allday selection
"1", /* visible = ? */
String.valueOf(utcStartMin), /* begin >= ? */
String.valueOf(utcStartMax), /* begin <= ? */
"1", // non-allday selection
"1", /* visible = ? */
String.valueOf(localStartMin), /* begin >= ? */
String.valueOf(localStartMax), /* begin <= ? */
"0" };
Cursor cursor = contentResolver.query(uriBuilder.build(), INSTANCES_PROJECTION, queryBuilder.toString(), queryArgs, null);
return cursor;
}
use of android.text.format.Time in project Etar-Calendar by Etar-Group.
the class AlarmScheduler method queryNextReminderAndSchedule.
/**
* Queries for all the reminders of the events in the instancesCursor, and schedules
* the alarm for the next upcoming reminder.
*/
private static void queryNextReminderAndSchedule(Cursor instancesCursor, Context context, ContentResolver contentResolver, AlarmManagerInterface alarmManager, int batchSize, long currentMillis) {
if (AlertService.DEBUG) {
int eventCount = instancesCursor.getCount();
if (eventCount == 0) {
Log.d(TAG, "No events found starting within 1 week.");
} else {
Log.d(TAG, "Query result count for events starting within 1 week: " + eventCount);
}
}
// Put query results of all events starting within some interval into map of event ID to
// local start time.
Map<Integer, List<Long>> eventMap = new HashMap<Integer, List<Long>>();
Time timeObj = new Time();
long nextAlarmTime = Long.MAX_VALUE;
int nextAlarmEventId = 0;
instancesCursor.moveToPosition(-1);
while (!instancesCursor.isAfterLast()) {
int index = 0;
eventMap.clear();
StringBuilder eventIdsForQuery = new StringBuilder();
eventIdsForQuery.append('(');
while (index++ < batchSize && instancesCursor.moveToNext()) {
int eventId = instancesCursor.getInt(INSTANCES_INDEX_EVENTID);
long begin = instancesCursor.getLong(INSTANCES_INDEX_BEGIN);
boolean allday = instancesCursor.getInt(INSTANCES_INDEX_ALL_DAY) != 0;
long localStartTime;
if (allday) {
// Adjust allday to local time.
localStartTime = Utils.convertAlldayUtcToLocal(timeObj, begin, Time.getCurrentTimezone());
} else {
localStartTime = begin;
}
List<Long> startTimes = eventMap.get(eventId);
if (startTimes == null) {
startTimes = new ArrayList<Long>();
eventMap.put(eventId, startTimes);
eventIdsForQuery.append(eventId);
eventIdsForQuery.append(",");
}
startTimes.add(localStartTime);
// Log for debugging.
if (Log.isLoggable(TAG, Log.DEBUG)) {
timeObj.set(localStartTime);
StringBuilder msg = new StringBuilder();
msg.append("Events cursor result -- eventId:").append(eventId);
msg.append(", allDay:").append(allday);
msg.append(", start:").append(localStartTime);
msg.append(" (").append(timeObj.format("%a, %b %d, %Y %I:%M%P")).append(")");
Log.d(TAG, msg.toString());
}
}
if (eventIdsForQuery.charAt(eventIdsForQuery.length() - 1) == ',') {
eventIdsForQuery.deleteCharAt(eventIdsForQuery.length() - 1);
}
eventIdsForQuery.append(')');
// Query the reminders table for the events found.
Cursor cursor = null;
try {
cursor = contentResolver.query(Reminders.CONTENT_URI, REMINDERS_PROJECTION, REMINDERS_WHERE + eventIdsForQuery, null, null);
// Process the reminders query results to find the next reminder time.
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
int eventId = cursor.getInt(REMINDERS_INDEX_EVENT_ID);
int reminderMinutes = cursor.getInt(REMINDERS_INDEX_MINUTES);
List<Long> startTimes = eventMap.get(eventId);
if (startTimes != null) {
for (Long startTime : startTimes) {
long alarmTime = startTime - reminderMinutes * DateUtils.MINUTE_IN_MILLIS;
if (alarmTime > currentMillis && alarmTime < nextAlarmTime) {
nextAlarmTime = alarmTime;
nextAlarmEventId = eventId;
}
if (Log.isLoggable(TAG, Log.DEBUG)) {
timeObj.set(alarmTime);
StringBuilder msg = new StringBuilder();
msg.append("Reminders cursor result -- eventId:").append(eventId);
msg.append(", startTime:").append(startTime);
msg.append(", minutes:").append(reminderMinutes);
msg.append(", alarmTime:").append(alarmTime);
msg.append(" (").append(timeObj.format("%a, %b %d, %Y %I:%M%P")).append(")");
Log.d(TAG, msg.toString());
}
}
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
// Schedule the alarm for the next reminder time.
if (nextAlarmTime < Long.MAX_VALUE) {
scheduleAlarm(context, nextAlarmEventId, nextAlarmTime, currentMillis, alarmManager);
}
}
use of android.text.format.Time in project Etar-Calendar by Etar-Group.
the class AlarmScheduler method scheduleAlarm.
/**
* Schedules an alarm for the EVENT_REMINDER_APP broadcast, for the specified
* alarm time with a slight delay (to account for the possible duplicate broadcast
* from the provider).
*/
private static void scheduleAlarm(Context context, long eventId, long alarmTime, long currentMillis, AlarmManagerInterface alarmManager) {
// Max out the alarm time to 1 day out, so an alert for an event far in the future
// (not present in our event query results for a limited range) can only be at
// most 1 day late.
long maxAlarmTime = currentMillis + MAX_ALARM_ELAPSED_MS;
if (alarmTime > maxAlarmTime) {
alarmTime = maxAlarmTime;
}
// Add a slight delay (see comments on the member var).
alarmTime += ALARM_DELAY_MS;
if (AlertService.DEBUG) {
Time time = new Time();
time.set(alarmTime);
String schedTime = time.format("%a, %b %d, %Y %I:%M%P");
Log.d(TAG, "Scheduling alarm for EVENT_REMINDER_APP broadcast for event " + eventId + " at " + alarmTime + " (" + schedTime + ")");
}
// Schedule an EVENT_REMINDER_APP broadcast with AlarmManager. The extra is
// only used by AlertService for logging. It is ignored by Intent.filterEquals,
// so this scheduling will still overwrite the alarm that was previously pending.
// Note that the 'setClass' is required, because otherwise it seems the broadcast
// can be eaten by other apps and we somehow may never receive it.
Intent intent = new Intent(AlertReceiver.EVENT_REMINDER_APP_ACTION);
intent.setClass(context, AlertReceiver.class);
intent.putExtra(CalendarContract.CalendarAlerts.ALARM_TIME, alarmTime);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}
Aggregations