From 54de0bddda49392a40922bc10d6be5be050513b8 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Mon, 15 Oct 2012 14:12:15 -0700 Subject: [PATCH] Fixed bugs with postponing the calendar dialogs --- .../astrid/gcal/CalendarAlarmReceiver.java | 31 ++++++++++++++----- .../astrid/gcal/CalendarAlarmScheduler.java | 6 +++- .../astrid/gcal/CalendarReminderActivity.java | 9 ++++-- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java b/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java index 13668c902..08f974b71 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java +++ b/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java @@ -21,7 +21,6 @@ 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, @@ -40,8 +39,17 @@ public class CalendarAlarmReceiver extends BroadcastReceiver { return; try { ContentResolver cr = context.getContentResolver(); - long eventId = intent.getLongExtra(TOKEN_EVENT_ID, -1); - boolean fromPostpone = intent.getBooleanExtra(CalendarReminderActivity.TOKEN_FROM_POSTPONE, false); + Uri data = intent.getData(); + if (data == null) + return; + + String uriString = data.toString(); + int pathIndex = uriString.indexOf("://"); + if (pathIndex > 0) + pathIndex += 3; + else return; + long eventId = Long.parseLong(uriString.substring(pathIndex)); + boolean fromPostpone = CalendarAlarmScheduler.URI_PREFIX_POSTPONE.equals(data.getScheme()); if (eventId > 0) { Uri eventUri = Calendars.getCalendarContentUri(Calendars.CALENDAR_CONTENT_EVENTS); @@ -61,9 +69,17 @@ public class CalendarAlarmReceiver extends BroadcastReceiver { long startTime = event.getLong(dtstartIndex); long endTime = event.getLong(dtendIndex); - long timeUntil = startTime - DateUtilities.now(); + boolean shouldShowReminder; + if (fromPostpone) { + long timeAfter = DateUtilities.now() - endTime; + shouldShowReminder = (timeAfter > DateUtilities.ONE_MINUTE * 2); + } else { + long timeUntil = startTime - DateUtilities.now(); + shouldShowReminder = (timeUntil > 0 && timeUntil < DateUtilities.ONE_MINUTE * 20); + } + - if (timeUntil > 0 && timeUntil < DateUtilities.ONE_MINUTE * 20) { + if (shouldShowReminder) { // Get attendees Cursor attendees = cr.query(Calendars.getCalendarContentUri(Calendars.CALENDAR_CONTENT_ATTENDEES), ATTENDEES_PROJECTION, @@ -93,10 +109,11 @@ public class CalendarAlarmReceiver extends BroadcastReceiver { Intent reminderActivity = new Intent(context, CalendarReminderActivity.class); reminderActivity.putStringArrayListExtra(CalendarReminderActivity.TOKEN_NAMES, names); reminderActivity.putStringArrayListExtra(CalendarReminderActivity.TOKEN_EMAILS, emails); + reminderActivity.putExtra(CalendarReminderActivity.TOKEN_EVENT_ID, eventId); reminderActivity.putExtra(CalendarReminderActivity.TOKEN_EVENT_NAME, title); reminderActivity.putExtra(CalendarReminderActivity.TOKEN_EVENT_START_TIME, startTime); reminderActivity.putExtra(CalendarReminderActivity.TOKEN_EVENT_END_TIME, endTime); - reminderActivity.putExtra(CalendarReminderActivity.TOKEN_FROM_POSTPONE, true); + reminderActivity.putExtra(CalendarReminderActivity.TOKEN_FROM_POSTPONE, fromPostpone); reminderActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); context.startActivity(reminderActivity); } @@ -109,7 +126,7 @@ public class CalendarAlarmReceiver extends BroadcastReceiver { event.close(); } } - } catch (IllegalArgumentException e) { // Some cursor read failed + } catch (IllegalArgumentException e) { // Some cursor read failed, or badly formed uri e.printStackTrace(); } } diff --git a/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmScheduler.java b/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmScheduler.java index 20865ead1..55f702923 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmScheduler.java +++ b/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarAlarmScheduler.java @@ -8,6 +8,7 @@ import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.database.Cursor; +import android.net.Uri; import android.util.Log; import com.timsu.astrid.R; @@ -20,6 +21,9 @@ public class CalendarAlarmScheduler { public static final String TAG = "calendar-alarm"; + public static final String URI_PREFIX = "cal-reminder"; + public static final String URI_PREFIX_POSTPONE = "cal-postpone"; + public static void scheduleAllCalendarAlarms(Context context) { if (!Preferences.getBoolean(R.string.p_calendar_reminders, true)) return; @@ -46,7 +50,7 @@ public class CalendarAlarmScheduler { long start = events.getLong(dtstartIndex); long id = events.getLong(idIndex); - eventAlarm.putExtra(CalendarAlarmReceiver.TOKEN_EVENT_ID, id); + eventAlarm.setData(Uri.parse(URI_PREFIX + "://" + id)); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, CalendarAlarmReceiver.REQUEST_CODE_CAL_REMINDER, eventAlarm, 0); diff --git a/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarReminderActivity.java b/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarReminderActivity.java index 8e9b260cc..52e202bde 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarReminderActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/gcal/CalendarReminderActivity.java @@ -8,7 +8,9 @@ import android.app.PendingIntent; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.net.Uri; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; @@ -29,6 +31,7 @@ import com.todoroo.astrid.service.StartupService; import com.todoroo.astrid.service.TagDataService; import com.todoroo.astrid.service.ThemeService; import com.todoroo.astrid.tags.TagFilterExposer; +import com.todoroo.astrid.utility.Constants; @SuppressWarnings("nls") public class CalendarReminderActivity extends Activity { @@ -229,9 +232,7 @@ public class CalendarReminderActivity extends Activity { Intent eventAlarm = new Intent(this, CalendarAlarmReceiver.class); eventAlarm.setAction(CalendarAlarmReceiver.BROADCAST_CALENDAR_REMINDER); - - eventAlarm.putExtra(CalendarAlarmReceiver.TOKEN_EVENT_ID, eventId); - eventAlarm.putExtra(TOKEN_FROM_POSTPONE, true); + eventAlarm.setData(Uri.parse(CalendarAlarmScheduler.URI_PREFIX_POSTPONE + "://" + eventId)); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, CalendarAlarmReceiver.REQUEST_CODE_CAL_REMINDER, eventAlarm, 0); @@ -239,6 +240,8 @@ public class CalendarReminderActivity extends Activity { am.cancel(pendingIntent); long alarmTime = endTime + DateUtilities.ONE_MINUTE * 5; + if (Constants.DEBUG) + Log.w(CalendarAlarmScheduler.TAG, "Scheduling calendar alarm for " + new Date(alarmTime)); am.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent); finish(); }