mirror of https://github.com/tasks/tasks
First pass at calendar alarm scheduler, some refactoring
parent
c943fd1f12
commit
1d891a8c01
@ -0,0 +1,72 @@
|
||||
package com.todoroo.astrid.gcal;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
|
||||
import com.todoroo.andlib.utility.DateUtilities;
|
||||
import com.todoroo.astrid.utility.Constants;
|
||||
|
||||
@SuppressWarnings("nls")
|
||||
public class CalendarAlarmReceiver extends BroadcastReceiver {
|
||||
|
||||
public static final int REQUEST_CODE_CAL_REMINDER = 100;
|
||||
public static final String BROADCAST_CALENDAR_REMINDER = Constants.PACKAGE + ".CALENDAR_EVENT";
|
||||
public static final String TOKEN_EVENT_ID = "eventId";
|
||||
|
||||
private static final String[] EVENTS_PROJECTION = {
|
||||
Calendars.EVENTS_DTSTART_COL,
|
||||
};
|
||||
|
||||
private static final String[] ATTENDEES_PROJECTION = {
|
||||
Calendars.ATTENDEES_NAME_COL,
|
||||
Calendars.ATTENDEES_EMAIL_COL,
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
try {
|
||||
ContentResolver cr = context.getContentResolver();
|
||||
long eventId = intent.getLongExtra(TOKEN_EVENT_ID, -1);
|
||||
if (eventId > 0) {
|
||||
Uri eventUri = Calendars.getCalendarContentUri(Calendars.CALENDAR_CONTENT_EVENTS);
|
||||
|
||||
String[] eventArg = new String[] { Long.toString(eventId) };
|
||||
Cursor event = cr.query(eventUri,
|
||||
EVENTS_PROJECTION,
|
||||
Calendars.ID_COLUMN_NAME + " = ?",
|
||||
eventArg,
|
||||
null);
|
||||
try {
|
||||
if (event.moveToFirst()) {
|
||||
int timeIndex = event.getColumnIndexOrThrow(Calendars.EVENTS_DTSTART_COL);
|
||||
long startTime = event.getLong(timeIndex);
|
||||
long timeUntil = startTime - DateUtilities.now();
|
||||
|
||||
if (timeUntil > 0 && timeUntil < DateUtilities.ONE_MINUTE * 20) {
|
||||
// Get attendees
|
||||
Cursor attendees = cr.query(Calendars.getCalendarContentUri(Calendars.CALENDAR_CONTENT_ATTENDEES),
|
||||
ATTENDEES_PROJECTION,
|
||||
Calendars.ATTENDEES_EVENT_ID_COL + " = ? ",
|
||||
eventArg,
|
||||
null);
|
||||
try {
|
||||
// Do something with attendees
|
||||
} finally {
|
||||
attendees.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
event.close();
|
||||
}
|
||||
}
|
||||
} catch (IllegalArgumentException e) { // Some cursor read failed
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,77 +0,0 @@
|
||||
package com.todoroo.astrid.gcal;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.provider.CalendarContract;
|
||||
|
||||
import com.todoroo.andlib.utility.AndroidUtilities;
|
||||
import com.todoroo.andlib.utility.DateUtilities;
|
||||
|
||||
@SuppressWarnings("nls")
|
||||
public class CalendarAlarmReciever extends BroadcastReceiver {
|
||||
|
||||
public static final String TOKEN_EVENT_ID = "eventId";
|
||||
|
||||
private static final String ID_COLUMN_NAME = "_id";
|
||||
private static final boolean USE_ICS_NAMES = AndroidUtilities.getSdkVersion() >= 14;
|
||||
private static final String EVENT_START_COLUMN_NAME = (USE_ICS_NAMES ? CalendarContract.Events.DTSTART : "dtstart");
|
||||
|
||||
private static final String[] EVENTS_PROJECTION = {
|
||||
EVENT_START_COLUMN_NAME,
|
||||
};
|
||||
|
||||
private static final String ATTENDEES_EVENT_ID_COL = (USE_ICS_NAMES ? CalendarContract.Attendees.EVENT_ID : "event_id");
|
||||
private static final String ATTENDEES_NAME_COL = (USE_ICS_NAMES ? CalendarContract.Attendees.ATTENDEE_NAME : "attendeeName");
|
||||
private static final String ATTENDEES_EMAIL_COL = (USE_ICS_NAMES ? CalendarContract.Attendees.ATTENDEE_EMAIL: "attendeeEmail");
|
||||
|
||||
private static final String[] ATTENDEES_PROJECTION = {
|
||||
ATTENDEES_NAME_COL,
|
||||
ATTENDEES_EMAIL_COL,
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
try {
|
||||
ContentResolver cr = context.getContentResolver();
|
||||
long eventId = intent.getLongExtra(TOKEN_EVENT_ID, -1);
|
||||
if (eventId > 0) {
|
||||
Uri eventUri = Calendars.getCalendarContentUri(Calendars.CALENDAR_CONTENT_EVENTS);
|
||||
|
||||
String[] eventArg = new String[] { Long.toString(eventId) };
|
||||
Cursor event = cr.query(eventUri,
|
||||
EVENTS_PROJECTION,
|
||||
ID_COLUMN_NAME + " = ?",
|
||||
eventArg,
|
||||
null);
|
||||
try {
|
||||
int timeIndex = event.getColumnIndexOrThrow(EVENT_START_COLUMN_NAME);
|
||||
long startTime = event.getLong(timeIndex);
|
||||
long timeUntil = startTime - DateUtilities.now();
|
||||
|
||||
if (timeUntil > 0 && timeUntil < DateUtilities.ONE_MINUTE * 20) {
|
||||
// Get attendees
|
||||
Cursor attendees = cr.query(Calendars.getCalendarContentUri(Calendars.CALENDAR_CONTENT_ATTENDEES),
|
||||
ATTENDEES_PROJECTION,
|
||||
ATTENDEES_EVENT_ID_COL + " = ? ",
|
||||
eventArg,
|
||||
null);
|
||||
try {
|
||||
// Do something with attendees
|
||||
} finally {
|
||||
attendees.close();
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
event.close();
|
||||
}
|
||||
}
|
||||
} catch (IllegalArgumentException e) { // Some cursor read failed
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.todoroo.astrid.gcal;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
|
||||
import com.todoroo.andlib.utility.DateUtilities;
|
||||
|
||||
@SuppressWarnings("nls")
|
||||
public class CalendarAlarmScheduler {
|
||||
|
||||
public static void scheduleCalendarAlarms(Context context) {
|
||||
ContentResolver cr = context.getContentResolver();
|
||||
|
||||
long now = DateUtilities.now();
|
||||
|
||||
Cursor events = cr.query(Calendars.getCalendarContentUri(Calendars.CALENDAR_CONTENT_EVENTS),
|
||||
new String[] { Calendars.ID_COLUMN_NAME, Calendars.EVENTS_DTSTART_COL },
|
||||
Calendars.EVENTS_DTSTART_COL + " > ? AND " + Calendars.EVENTS_DTSTART_COL + " < ?",
|
||||
new String[] { Long.toString(now + DateUtilities.ONE_MINUTE * 20), Long.toString(now + DateUtilities.ONE_DAY) },
|
||||
null);
|
||||
try {
|
||||
if (events.moveToFirst()) {
|
||||
int idIndex = events.getColumnIndex(Calendars.ID_COLUMN_NAME);
|
||||
int timeIndex = events.getColumnIndexOrThrow(Calendars.EVENTS_DTSTART_COL);
|
||||
|
||||
long start = events.getLong(timeIndex);
|
||||
long id = events.getLong(idIndex);
|
||||
|
||||
long alarmTime = start - DateUtilities.ONE_MINUTE * 15;
|
||||
Intent eventAlarm = new Intent(context, CalendarAlarmReceiver.class);
|
||||
eventAlarm.setAction(CalendarAlarmReceiver.BROADCAST_CALENDAR_REMINDER);
|
||||
eventAlarm.putExtra(CalendarAlarmReceiver.TOKEN_EVENT_ID, id);
|
||||
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
|
||||
CalendarAlarmReceiver.REQUEST_CODE_CAL_REMINDER, eventAlarm, 0);
|
||||
|
||||
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
|
||||
am.cancel(pendingIntent);
|
||||
|
||||
am.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
|
||||
}
|
||||
} finally {
|
||||
events.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue