diff --git a/astrid/AndroidManifest.xml b/astrid/AndroidManifest.xml index b3c66a517..6a66d3d49 100644 --- a/astrid/AndroidManifest.xml +++ b/astrid/AndroidManifest.xml @@ -227,6 +227,8 @@ + + diff --git a/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java b/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java new file mode 100644 index 000000000..aae43b165 --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java @@ -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(); + } + } + +} diff --git a/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmReciever.java b/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmReciever.java deleted file mode 100644 index a393a53b7..000000000 --- a/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmReciever.java +++ /dev/null @@ -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(); - } - } - -} diff --git a/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmScheduler.java b/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmScheduler.java new file mode 100644 index 000000000..77dcc1597 --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmScheduler.java @@ -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(); + } + + } + +} diff --git a/astrid/plugin-src/com/todoroo/astrid/gcal/Calendars.java b/astrid/plugin-src/com/todoroo/astrid/gcal/Calendars.java index bba4fc6ec..ab3e1b7e4 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gcal/Calendars.java +++ b/astrid/plugin-src/com/todoroo/astrid/gcal/Calendars.java @@ -29,22 +29,27 @@ public class Calendars { public static final String CALENDAR_CONTENT_EVENTS = "events"; public static final String CALENDAR_CONTENT_ATTENDEES = "attendees"; - private static final String ID_COLUMN_NAME = "_id"; private static final boolean USE_ICS_NAMES = AndroidUtilities.getSdkVersion() >= 14; - private static final String DISPLAY_COLUMN_NAME = (USE_ICS_NAMES ? CalendarContract.Calendars.CALENDAR_DISPLAY_NAME : "displayName"); - private static final String ACCES_LEVEL_COLUMN_NAME = (USE_ICS_NAMES ? CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL : "access_level"); + + public static final String ID_COLUMN_NAME = "_id"; + public static final String CALENDARS_DISPLAY_COL = (USE_ICS_NAMES ? CalendarContract.Calendars.CALENDAR_DISPLAY_NAME : "displayName"); + public static final String CALENDARS_ACCESS_LEVEL_COL = (USE_ICS_NAMES ? CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL : "access_level"); + public static final String EVENTS_DTSTART_COL = (USE_ICS_NAMES ? CalendarContract.Events.DTSTART : "dtstart"); + public static final String ATTENDEES_EVENT_ID_COL = (USE_ICS_NAMES ? CalendarContract.Attendees.EVENT_ID : "event_id"); + public static final String ATTENDEES_NAME_COL = (USE_ICS_NAMES ? CalendarContract.Attendees.ATTENDEE_NAME : "attendeeName"); + public static final String ATTENDEES_EMAIL_COL = (USE_ICS_NAMES ? CalendarContract.Attendees.ATTENDEE_EMAIL: "attendeeEmail"); private static final String[] CALENDARS_PROJECTION = new String[] { ID_COLUMN_NAME, - DISPLAY_COLUMN_NAME, + CALENDARS_DISPLAY_COL, }; // Only show calendars that the user can modify. Access level 500 // corresponds to Calendars.CONTRIBUTOR_ACCESS - private static final String CALENDARS_WHERE = ACCES_LEVEL_COLUMN_NAME + ">= 500"; + private static final String CALENDARS_WHERE = CALENDARS_ACCESS_LEVEL_COL + ">= 500"; - private static final String CALENDARS_SORT = DISPLAY_COLUMN_NAME + " ASC"; + private static final String CALENDARS_SORT = CALENDARS_DISPLAY_COL + " ASC"; // --- api access @@ -136,7 +141,7 @@ public class Calendars { // Iterate calendars one by one, and fill up the list preference int row = 0; int idColumn = c.getColumnIndex(ID_COLUMN_NAME); - int nameColumn = c.getColumnIndex(DISPLAY_COLUMN_NAME); + int nameColumn = c.getColumnIndex(CALENDARS_DISPLAY_COL); while (c.moveToNext()) { String id = c.getString(idColumn); String name = c.getString(nameColumn);