From a1ebf24ff337d5fb8445982dacf1d3aea0f41bce Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 4 Mar 2016 10:44:01 -0600 Subject: [PATCH] Inline CalendarContract fields --- .../astrid/gcal/CalendarAlarmReceiver.java | 32 ++++----- .../com/todoroo/astrid/gcal/Calendars.java | 39 ++--------- .../com/todoroo/astrid/gcal/GCalHelper.java | 66 +++++++++---------- .../astrid/gcal/GCalTaskCompleteListener.java | 3 +- .../CalendarNotificationIntentService.java | 14 ++-- .../java/org/tasks/ui/CalendarControlSet.java | 19 +++--- 6 files changed, 74 insertions(+), 99 deletions(-) diff --git a/src/main/java/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java b/src/main/java/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java index 40a18e828..283fe4b32 100644 --- a/src/main/java/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java +++ b/src/main/java/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java @@ -7,6 +7,7 @@ import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; +import android.provider.CalendarContract; import android.text.TextUtils; import com.todoroo.andlib.utility.DateUtilities; @@ -26,20 +27,22 @@ import javax.inject.Inject; import timber.log.Timber; +import static android.provider.BaseColumns._ID; + public class CalendarAlarmReceiver extends InjectingBroadcastReceiver { public static final int REQUEST_CODE_CAL_REMINDER = 100; public static final String BROADCAST_CALENDAR_REMINDER = Constants.PACKAGE + ".CALENDAR_EVENT"; private static final String[] EVENTS_PROJECTION = { - Calendars.EVENTS_DTSTART_COL, - Calendars.EVENTS_DTEND_COL, - Calendars.EVENTS_NAME_COL, + CalendarContract.Events.DTSTART, + CalendarContract.Events.DTEND, + CalendarContract.Events.TITLE, }; private static final String[] ATTENDEES_PROJECTION = { - Calendars.ATTENDEES_NAME_COL, - Calendars.ATTENDEES_EMAIL_COL, + CalendarContract.Attendees.ATTENDEE_NAME, + CalendarContract.Attendees.ATTENDEE_EMAIL, }; @Inject Preferences preferences; @@ -83,19 +86,18 @@ public class CalendarAlarmReceiver extends InjectingBroadcastReceiver { private void showCalReminder(Context context, long eventId, boolean fromPostpone) { ContentResolver cr = context.getContentResolver(); - Uri eventUri = Calendars.getCalendarContentUri(Calendars.CALENDAR_CONTENT_EVENTS); String[] eventArg = new String[] { Long.toString(eventId) }; - Cursor event = cr.query(eventUri, + Cursor event = cr.query(CalendarContract.Events.CONTENT_URI, EVENTS_PROJECTION, - Calendars.ID_COLUMN_NAME + " = ?", + _ID + " = ?", eventArg, null); try { if (event.moveToFirst()) { - int dtstartIndex = event.getColumnIndexOrThrow(Calendars.EVENTS_DTSTART_COL); - int dtendIndex = event.getColumnIndexOrThrow(Calendars.EVENTS_DTEND_COL); - int titleIndex = event.getColumnIndexOrThrow(Calendars.EVENTS_NAME_COL); + int dtstartIndex = event.getColumnIndexOrThrow(CalendarContract.Events.DTSTART); + int dtendIndex = event.getColumnIndexOrThrow(CalendarContract.Events.DTEND); + int titleIndex = event.getColumnIndexOrThrow(CalendarContract.Events.TITLE); String title = event.getString(titleIndex); long startTime = event.getLong(dtstartIndex); @@ -112,15 +114,15 @@ public class CalendarAlarmReceiver extends InjectingBroadcastReceiver { if (shouldShowReminder) { // Get attendees - Cursor attendees = cr.query(Calendars.getCalendarContentUri(Calendars.CALENDAR_CONTENT_ATTENDEES), + Cursor attendees = cr.query(CalendarContract.Attendees.CONTENT_URI, ATTENDEES_PROJECTION, - Calendars.ATTENDEES_EVENT_ID_COL + " = ? ", + CalendarContract.Attendees.EVENT_ID + " = ? ", eventArg, null); try { // Do something with attendees - int emailIndex = attendees.getColumnIndexOrThrow(Calendars.ATTENDEES_EMAIL_COL); - int nameIndex = attendees.getColumnIndexOrThrow(Calendars.ATTENDEES_NAME_COL); + int emailIndex = attendees.getColumnIndexOrThrow(CalendarContract.Attendees.ATTENDEE_EMAIL); + int nameIndex = attendees.getColumnIndexOrThrow(CalendarContract.Attendees.ATTENDEE_NAME); ArrayList names = new ArrayList<>(); ArrayList emails = new ArrayList<>(); diff --git a/src/main/java/com/todoroo/astrid/gcal/Calendars.java b/src/main/java/com/todoroo/astrid/gcal/Calendars.java index ceb45c7be..0d45c9342 100644 --- a/src/main/java/com/todoroo/astrid/gcal/Calendars.java +++ b/src/main/java/com/todoroo/astrid/gcal/Calendars.java @@ -5,52 +5,23 @@ */ package com.todoroo.astrid.gcal; -import android.net.Uri; import android.provider.CalendarContract; import javax.inject.Singleton; +import static android.provider.BaseColumns._ID; + @Singleton public class Calendars { - public static final String CALENDAR_CONTENT_CALENDARS = "calendars"; - public static final String CALENDAR_CONTENT_EVENTS = "events"; - public static final String CALENDAR_CONTENT_ATTENDEES = "attendees"; - - public static final String ID_COLUMN_NAME = "_id"; - public static final String CALENDARS_DISPLAY_COL = CalendarContract.Calendars.CALENDAR_DISPLAY_NAME; - public static final String EVENTS_DTSTART_COL = CalendarContract.Events.DTSTART; - public static final String EVENTS_DTEND_COL = CalendarContract.Events.DTEND; - public static final String EVENTS_NAME_COL = CalendarContract.Events.TITLE; - public static final String ATTENDEES_EVENT_ID_COL = CalendarContract.Attendees.EVENT_ID; - public static final String ATTENDEES_NAME_COL = CalendarContract.Attendees.ATTENDEE_NAME; - public static final String ATTENDEES_EMAIL_COL = CalendarContract.Attendees.ATTENDEE_EMAIL; - public static final String[] CALENDARS_PROJECTION = new String[] { - ID_COLUMN_NAME, - CALENDARS_DISPLAY_COL, + _ID, + CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, }; // Only show calendars that the user can modify. Access level 500 // corresponds to Calendars.CONTRIBUTOR_ACCESS public static final String CALENDARS_WHERE = CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL + ">= 500"; - public static final String CALENDARS_SORT = CALENDARS_DISPLAY_COL + " ASC"; - - // --- api access - - /** Return content uri for calendars - * @param table provider table, something like calendars, events - */ - public static Uri getCalendarContentUri(String table) { - switch (table) { - case CALENDAR_CONTENT_CALENDARS: - return CalendarContract.Calendars.CONTENT_URI; - case CALENDAR_CONTENT_EVENTS: - return CalendarContract.Events.CONTENT_URI; - case CALENDAR_CONTENT_ATTENDEES: - return CalendarContract.Attendees.CONTENT_URI; - } - return null; - } + public static final String CALENDARS_SORT = CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + " ASC"; } diff --git a/src/main/java/com/todoroo/astrid/gcal/GCalHelper.java b/src/main/java/com/todoroo/astrid/gcal/GCalHelper.java index 0571d34fd..fce08653a 100644 --- a/src/main/java/com/todoroo/astrid/gcal/GCalHelper.java +++ b/src/main/java/com/todoroo/astrid/gcal/GCalHelper.java @@ -11,6 +11,7 @@ import android.content.Context; import android.database.Cursor; import android.database.CursorIndexOutOfBoundsException; import android.net.Uri; +import android.provider.CalendarContract; import android.text.TextUtils; import android.text.format.Time; @@ -30,15 +31,13 @@ import javax.inject.Inject; import timber.log.Timber; -import static com.todoroo.astrid.gcal.Calendars.getCalendarContentUri; +import static android.provider.BaseColumns._ID; public class GCalHelper { /** If task has no estimated time, how early to set a task in calendar (seconds)*/ private static final long DEFAULT_CAL_TIME = DateUtilities.ONE_HOUR; - public static final String CALENDAR_ID_COLUMN = "calendar_id"; //$NON-NLS-1$ - private final Context context; private final TaskService taskService; private final Preferences preferences; @@ -94,22 +93,21 @@ public class GCalHelper { } try{ - Uri uri = getCalendarContentUri(Calendars.CALENDAR_CONTENT_EVENTS); - values.put("title", task.getTitle()); - values.put("description", task.getNotes()); - values.put("hasAlarm", 0); - boolean valuesContainCalendarId = (values.containsKey(CALENDAR_ID_COLUMN) && - !TextUtils.isEmpty(values.getAsString(CALENDAR_ID_COLUMN))); + values.put(CalendarContract.Events.TITLE, task.getTitle()); + values.put(CalendarContract.Events.DESCRIPTION, task.getNotes()); + values.put(CalendarContract.Events.HAS_ALARM, 0); + boolean valuesContainCalendarId = (values.containsKey(CalendarContract.Events.CALENDAR_ID) && + !TextUtils.isEmpty(values.getAsString(CalendarContract.Events.CALENDAR_ID))); if (!valuesContainCalendarId) { String calendarId = preferences.getDefaultCalendar(); if (!TextUtils.isEmpty(calendarId)) { - values.put("calendar_id", calendarId); + values.put(CalendarContract.Events.CALENDAR_ID, calendarId); } } createStartAndEndDate(task, values); - Uri eventUri = cr.insert(uri, values); + Uri eventUri = cr.insert(CalendarContract.Events.CONTENT_URI, values); cr.notifyChange(eventUri, null); return eventUri; @@ -135,14 +133,14 @@ public class GCalHelper { return; } ContentValues cv = new ContentValues(); - cv.put(CALENDAR_ID_COLUMN, calendarId); + cv.put(CalendarContract.Events.CALENDAR_ID, calendarId); Uri uri = createTaskEvent(task, cr, cv, false); task.setCalendarUri(uri.toString()); } private static String getCalendarId(Uri uri, ContentResolver cr) { - Cursor calendar = cr.query(uri, new String[]{CALENDAR_ID_COLUMN}, null, null, null); + Cursor calendar = cr.query(uri, new String[]{CalendarContract.Events.CALENDAR_ID}, null, null, null); try { calendar.moveToFirst(); return calendar.getString(0); @@ -173,7 +171,7 @@ public class GCalHelper { // try to load calendar ContentResolver cr = context.getContentResolver(); - Cursor cursor = cr.query(calendarUri, new String[] { "dtstart" }, null, null, null); //$NON-NLS-1$ + Cursor cursor = cr.query(calendarUri, new String[] { CalendarContract.Events.DTSTART }, null, null, null); //$NON-NLS-1$ try { boolean alreadydeleted = cursor.getCount() == 0; @@ -206,40 +204,40 @@ public class GCalHelper { estimatedTime = DEFAULT_CAL_TIME; } if (preferences.getBoolean(R.string.p_end_at_deadline, true)) { - values.put("dtstart", dueDate); - values.put("dtend", dueDate + estimatedTime); + values.put(CalendarContract.Events.DTSTART, dueDate); + values.put(CalendarContract.Events.DTEND, dueDate + estimatedTime); }else{ - values.put("dtstart", dueDate - estimatedTime); - values.put("dtend", dueDate); + values.put(CalendarContract.Events.DTSTART, dueDate - estimatedTime); + values.put(CalendarContract.Events.DTEND, dueDate); } // setting a duetime to a previously timeless event requires explicitly setting allDay=0 - values.put("allDay", "0"); - values.put("eventTimezone", TimeZone.getDefault().getID()); + values.put(CalendarContract.Events.ALL_DAY, "0"); + values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID()); } else { - values.put("dtstart", tzCorrectedDueDate); - values.put("dtend", tzCorrectedDueDate); - values.put("allDay", "1"); + values.put(CalendarContract.Events.DTSTART, tzCorrectedDueDate); + values.put(CalendarContract.Events.DTEND, tzCorrectedDueDate); + values.put(CalendarContract.Events.ALL_DAY, "1"); } } else { - values.put("dtstart", tzCorrectedDueDateNow); - values.put("dtend", tzCorrectedDueDateNow); - values.put("allDay", "1"); + values.put(CalendarContract.Events.DTSTART, tzCorrectedDueDateNow); + values.put(CalendarContract.Events.DTEND, tzCorrectedDueDateNow); + values.put(CalendarContract.Events.ALL_DAY, "1"); } - if ("1".equals(values.get("allDay"))) { - values.put("eventTimezone", Time.TIMEZONE_UTC); + if ("1".equals(values.get(CalendarContract.Events.ALL_DAY))) { + values.put(CalendarContract.Events.EVENT_TIMEZONE, Time.TIMEZONE_UTC); } else { - values.put("eventTimezone", TimeZone.getDefault().getID()); + values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID()); } } public AndroidCalendar getCalendar(String id) { ContentResolver cr = context.getContentResolver(); - Cursor c = cr.query(getCalendarContentUri(Calendars.CALENDAR_CONTENT_CALENDARS), Calendars.CALENDARS_PROJECTION, + Cursor c = cr.query(CalendarContract.Calendars.CONTENT_URI, Calendars.CALENDARS_PROJECTION, Calendars.CALENDARS_WHERE + " AND Calendars._id=" + id, null, Calendars.CALENDARS_SORT); try { if (c.moveToFirst()) { - int nameColumn = c.getColumnIndex(Calendars.CALENDARS_DISPLAY_COL); + int nameColumn = c.getColumnIndex(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME); String name = c.getString(nameColumn); return new AndroidCalendar(id, name); } @@ -258,7 +256,7 @@ public class GCalHelper { public List getCalendars() { ContentResolver cr = context.getContentResolver(); - Cursor c = cr.query(getCalendarContentUri(Calendars.CALENDAR_CONTENT_CALENDARS), Calendars.CALENDARS_PROJECTION, + Cursor c = cr.query(CalendarContract.Calendars.CONTENT_URI, Calendars.CALENDARS_PROJECTION, Calendars.CALENDARS_WHERE, null, Calendars.CALENDARS_SORT); try { List calendars = new ArrayList<>(); @@ -269,8 +267,8 @@ public class GCalHelper { return calendars; } - int idColumn = c.getColumnIndex(Calendars.ID_COLUMN_NAME); - int nameColumn = c.getColumnIndex(Calendars.CALENDARS_DISPLAY_COL); + int idColumn = c.getColumnIndex(_ID); + int nameColumn = c.getColumnIndex(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME); while (c.moveToNext()) { String id = c.getString(idColumn); String name = c.getString(nameColumn); diff --git a/src/main/java/com/todoroo/astrid/gcal/GCalTaskCompleteListener.java b/src/main/java/com/todoroo/astrid/gcal/GCalTaskCompleteListener.java index aa9d1b458..2228dc87e 100644 --- a/src/main/java/com/todoroo/astrid/gcal/GCalTaskCompleteListener.java +++ b/src/main/java/com/todoroo/astrid/gcal/GCalTaskCompleteListener.java @@ -10,6 +10,7 @@ import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.provider.CalendarContract; import android.text.TextUtils; import com.todoroo.astrid.api.AstridApiConstants; @@ -47,7 +48,7 @@ public class GCalTaskCompleteListener extends InjectingBroadcastReceiver { // change title of calendar event ContentResolver cr = context.getContentResolver(); ContentValues values = new ContentValues(); - values.put("title", context.getString(R.string.gcal_completed_title, + values.put(CalendarContract.Events.TITLE, context.getString(R.string.gcal_completed_title, task.getTitle())); cr.update(Uri.parse(calendarUri), values, null, null); } catch (Exception e) { diff --git a/src/main/java/org/tasks/scheduling/CalendarNotificationIntentService.java b/src/main/java/org/tasks/scheduling/CalendarNotificationIntentService.java index 890b00461..3d9bfa403 100644 --- a/src/main/java/org/tasks/scheduling/CalendarNotificationIntentService.java +++ b/src/main/java/org/tasks/scheduling/CalendarNotificationIntentService.java @@ -6,10 +6,10 @@ 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.DateUtilities; import com.todoroo.astrid.gcal.CalendarAlarmReceiver; -import com.todoroo.astrid.gcal.Calendars; import org.tasks.R; import org.tasks.injection.ForApplication; @@ -20,6 +20,8 @@ import java.util.concurrent.TimeUnit; import javax.inject.Inject; +import static android.provider.BaseColumns._ID; + public class CalendarNotificationIntentService extends RecurringIntervalIntentService { public static final String URI_PREFIX = "cal-reminder"; @@ -40,15 +42,15 @@ public class CalendarNotificationIntentService extends RecurringIntervalIntentSe 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 + " < ?", + Cursor events = cr.query(CalendarContract.Events.CONTENT_URI, + new String[] {_ID, CalendarContract.Events.DTSTART}, + CalendarContract.Events.DTSTART + " > ? AND " + CalendarContract.Events.DTSTART + " < ?", new String[] { Long.toString(now + DateUtilities.ONE_MINUTE * 15), Long.toString(now + DateUtilities.ONE_DAY) }, null); try { if (events != null && events.getCount() > 0) { - int idIndex = events.getColumnIndex(Calendars.ID_COLUMN_NAME); - int dtstartIndex = events.getColumnIndexOrThrow(Calendars.EVENTS_DTSTART_COL); + int idIndex = events.getColumnIndex(_ID); + int dtstartIndex = events.getColumnIndexOrThrow(CalendarContract.Events.DTSTART); for (events.moveToFirst(); !events.isAfterLast(); events.moveToNext()) { Intent eventAlarm = new Intent(context, CalendarAlarmReceiver.class); diff --git a/src/main/java/org/tasks/ui/CalendarControlSet.java b/src/main/java/org/tasks/ui/CalendarControlSet.java index 9fe43dc9b..9a9d3d119 100644 --- a/src/main/java/org/tasks/ui/CalendarControlSet.java +++ b/src/main/java/org/tasks/ui/CalendarControlSet.java @@ -10,6 +10,7 @@ import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; +import android.provider.CalendarContract; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; @@ -164,10 +165,10 @@ public class CalendarControlSet extends TaskEditControlFragment { // check if we need to update the item ContentValues setValues = task.getSetValues(); if(setValues.containsKey(Task.TITLE.name)) { - updateValues.put("title", task.getTitle()); + updateValues.put(CalendarContract.Events.TITLE, task.getTitle()); } if(setValues.containsKey(Task.NOTES.name)) { - updateValues.put("description", task.getNotes()); + updateValues.put(CalendarContract.Events.DESCRIPTION, task.getNotes()); } if(setValues.containsKey(Task.DUE_DATE.name) || setValues.containsKey(Task.ESTIMATED_SECONDS.name)) { gcalHelper.createStartAndEndDate(task, updateValues); @@ -181,14 +182,14 @@ public class CalendarControlSet extends TaskEditControlFragment { ContentResolver cr = context.getContentResolver(); try{ ContentValues values = new ContentValues(); - values.put("calendar_id", calendarId); + values.put(CalendarContract.Events.CALENDAR_ID, calendarId); Uri uri = gcalHelper.createTaskEvent(task, cr, values); if(uri != null) { task.setCalendarUri(uri.toString()); // pop up the new event Intent intent = new Intent(Intent.ACTION_VIEW, uri); - intent.putExtra("beginTime", values.getAsLong("dtstart")); - intent.putExtra("endTime", values.getAsLong("dtend")); + intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, values.getAsLong(CalendarContract.Events.DTSTART)); + intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, values.getAsLong(CalendarContract.Events.DTEND)); startActivity(intent); } } catch (Exception e) { @@ -247,7 +248,7 @@ public class CalendarControlSet extends TaskEditControlFragment { ContentResolver cr = getActivity().getContentResolver(); Uri uri = Uri.parse(eventUri); Intent intent = new Intent(Intent.ACTION_VIEW, uri); - Cursor cursor = cr.query(uri, new String[] { "dtstart", "dtend" }, null, null, null); + Cursor cursor = cr.query(uri, new String[] { CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND }, null, null, null); try { if(cursor.getCount() == 0) { // event no longer exists @@ -256,8 +257,8 @@ public class CalendarControlSet extends TaskEditControlFragment { refreshDisplayView(); } else { cursor.moveToFirst(); - intent.putExtra("beginTime", cursor.getLong(0)); - intent.putExtra("endTime", cursor.getLong(1)); + intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, cursor.getLong(0)); + intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, cursor.getLong(1)); startActivity(intent); } } catch (Exception e) { @@ -320,7 +321,7 @@ public class CalendarControlSet extends TaskEditControlFragment { try { Uri uri = Uri.parse(eventUri); ContentResolver contentResolver = context.getContentResolver(); - Cursor cursor = contentResolver.query(uri, new String[]{"dtstart"}, null, null, null); + Cursor cursor = contentResolver.query(uri, new String[]{CalendarContract.Events.DTSTART}, null, null, null); try { if (cursor.getCount() != 0) { return true;