Add CalendarProvider

pull/384/head
Alex Baker 9 years ago
parent a1ebf24ff3
commit da9530ba7c

@ -10,14 +10,12 @@ import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.Preference;
import com.todoroo.astrid.gcal.AndroidCalendar;
import com.todoroo.astrid.gcal.GCalHelper;
import org.tasks.R;
import org.tasks.activities.CalendarSelectionActivity;
import org.tasks.calendars.AndroidCalendar;
import org.tasks.calendars.CalendarProvider;
import org.tasks.injection.InjectingPreferenceActivity;
import org.tasks.preferences.ActivityPermissionRequestor;
import org.tasks.preferences.PermissionChecker;
import org.tasks.preferences.PermissionRequestor;
import org.tasks.preferences.Preferences;
@ -36,8 +34,7 @@ public class DefaultsPreferences extends InjectingPreferenceActivity {
private static final int REQUEST_CALENDAR_SELECTION = 10412;
@Inject Preferences preferences;
@Inject GCalHelper calendarHelper;
@Inject PermissionChecker permissionChecker;
@Inject CalendarProvider calendarProvider;
@Inject ActivityPermissionRequestor permissionRequester;
private Preference defaultCalendarPref;
@ -61,16 +58,10 @@ public class DefaultsPreferences extends InjectingPreferenceActivity {
}
private void setCalendarSummary(String calendarId) {
if (permissionChecker.canAccessCalendars()) {
List<AndroidCalendar> calendars = calendarHelper.getCalendars();
for (AndroidCalendar calendar : calendars) {
if (calendar.getId().equals(calendarId)) {
defaultCalendarPref.setSummary(calendar.getName());
return;
}
}
}
defaultCalendarPref.setSummary(getString(R.string.none));
AndroidCalendar calendar = calendarProvider.getCalendar(calendarId);
defaultCalendarPref.setSummary(calendar == null
? getString(R.string.none)
: calendar.getName());
}
private void startCalendarSelectionActivity() {

@ -8,8 +8,6 @@ package com.todoroo.astrid.gcal;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.net.Uri;
import android.provider.CalendarContract;
import android.text.TextUtils;
@ -20,19 +18,17 @@ import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.service.TaskService;
import org.tasks.R;
import org.tasks.calendars.AndroidCalendar;
import org.tasks.calendars.CalendarProvider;
import org.tasks.injection.ForApplication;
import org.tasks.preferences.Preferences;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import javax.inject.Inject;
import timber.log.Timber;
import static android.provider.BaseColumns._ID;
public class GCalHelper {
/** If task has no estimated time, how early to set a task in calendar (seconds)*/
@ -41,12 +37,15 @@ public class GCalHelper {
private final Context context;
private final TaskService taskService;
private final Preferences preferences;
private final CalendarProvider calendarProvider;
@Inject
public GCalHelper(@ForApplication Context context, TaskService taskService, Preferences preferences) {
public GCalHelper(@ForApplication Context context, TaskService taskService, Preferences preferences,
CalendarProvider calendarProvider) {
this.context = context;
this.taskService = taskService;
this.preferences = preferences;
this.calendarProvider = calendarProvider;
}
public String getTaskEventUri(Task task) {
@ -127,29 +126,16 @@ public class GCalHelper {
}
Uri eventUri = Uri.parse(taskUri);
String calendarId = getCalendarId(eventUri, cr);
if (calendarId == null) { // Bail out, no calendar id
AndroidCalendar calendar = calendarProvider.getCalendar(eventUri);
if (calendar == null) { // Bail out, no calendar id
task.setCalendarUri(""); //$NON-NLS-1$
return;
}
} else {
ContentValues cv = new ContentValues();
cv.put(CalendarContract.Events.CALENDAR_ID, calendarId);
cv.put(CalendarContract.Events.CALENDAR_ID, calendar.getId());
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[]{CalendarContract.Events.CALENDAR_ID}, null, null, null);
try {
calendar.moveToFirst();
return calendar.getString(0);
} catch (CursorIndexOutOfBoundsException e) {
Timber.e(e, e.getMessage());
return null;
} finally {
calendar.close();
}
}
public boolean deleteTaskEvent(Task task) {
@ -168,22 +154,11 @@ public class GCalHelper {
if(!TextUtils.isEmpty(uri)) {
try {
Uri calendarUri = Uri.parse(uri);
// try to load calendar
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(calendarUri, new String[] { CalendarContract.Events.DTSTART }, null, null, null); //$NON-NLS-1$
try {
boolean alreadydeleted = cursor.getCount() == 0;
if (!alreadydeleted) {
cr.delete(calendarUri, null, null);
if (calendarProvider.getCalendar(calendarUri) != null) {
context.getContentResolver().delete(calendarUri, null, null);
eventDeleted = true;
}
} finally {
cursor.close();
}
task.setCalendarUri( "");
task.setCalendarUri("");
} catch (Exception e) {
Timber.e(e, e.getMessage());
}
@ -229,56 +204,4 @@ public class GCalHelper {
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
}
}
public AndroidCalendar getCalendar(String id) {
ContentResolver cr = context.getContentResolver();
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(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME);
String name = c.getString(nameColumn);
return new AndroidCalendar(id, name);
}
} finally {
if(c != null) {
c.close();
}
}
return null;
}
/**
* Appends all user-modifiable calendars to listPreference.
*/
public List<AndroidCalendar> getCalendars() {
ContentResolver cr = context.getContentResolver();
Cursor c = cr.query(CalendarContract.Calendars.CONTENT_URI, Calendars.CALENDARS_PROJECTION,
Calendars.CALENDARS_WHERE, null, Calendars.CALENDARS_SORT);
try {
List<AndroidCalendar> calendars = new ArrayList<>();
if (c == null || c.getCount() == 0) {
// Something went wrong when querying calendars. Only offer them
// the system default choice
return calendars;
}
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);
calendars.add(new AndroidCalendar(id, name));
}
return calendars;
} finally {
if(c != null) {
c.close();
}
}
}
}

@ -5,7 +5,7 @@ import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import com.todoroo.astrid.gcal.AndroidCalendar;
import org.tasks.calendars.AndroidCalendar;
import org.tasks.injection.InjectingAppCompatActivity;
import org.tasks.preferences.ActivityPermissionRequestor;

@ -8,10 +8,10 @@ import android.support.v7.app.AlertDialog;
import android.widget.ArrayAdapter;
import com.google.common.base.Function;
import com.todoroo.astrid.gcal.AndroidCalendar;
import com.todoroo.astrid.gcal.GCalHelper;
import org.tasks.R;
import org.tasks.calendars.AndroidCalendar;
import org.tasks.calendars.CalendarProvider;
import org.tasks.dialogs.DialogBuilder;
import org.tasks.injection.InjectingDialogFragment;
@ -33,14 +33,14 @@ public class CalendarSelectionDialog extends InjectingDialogFragment {
}
@Inject DialogBuilder dialogBuilder;
@Inject GCalHelper gCalHelper;
@Inject CalendarProvider calendarProvider;
private CalendarSelectionHandler handler;
private boolean enableNone;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final List<AndroidCalendar> calendars = gCalHelper.getCalendars();
final List<AndroidCalendar> calendars = calendarProvider.getCalendars();
List<String> calendarNames = transform(calendars, new Function<AndroidCalendar, String>() {
@Override
public String apply(AndroidCalendar androidCalendar) {

@ -1,4 +1,4 @@
package com.todoroo.astrid.gcal;
package org.tasks.calendars;
public class AndroidCalendar {
private final String id;
@ -18,4 +18,12 @@ public class AndroidCalendar {
public String getName() {
return name;
}
@Override
public String toString() {
return "AndroidCalendar{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}

@ -0,0 +1,85 @@
package org.tasks.calendars;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;
import android.support.annotation.Nullable;
import org.tasks.injection.ForApplication;
import org.tasks.preferences.PermissionChecker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import timber.log.Timber;
import static android.provider.BaseColumns._ID;
public class CalendarProvider {
private static final String CAN_MODIFY = CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL + ">= " + CalendarContract.Calendars.CAL_ACCESS_CONTRIBUTOR;
private static final String SORT = CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + " ASC";
private static final String[] COLUMNS = {
_ID,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME
};
private final PermissionChecker permissionChecker;
private final ContentResolver contentResolver;
@Inject
public CalendarProvider(@ForApplication Context context, PermissionChecker permissionChecker) {
this.permissionChecker = permissionChecker;
contentResolver = context.getContentResolver();
}
public List<AndroidCalendar> getCalendars() {
return getCalendars(CalendarContract.Calendars.CONTENT_URI, CAN_MODIFY);
}
@Nullable
public AndroidCalendar getCalendar(String id) {
return getCalendar(CalendarContract.Calendars.CONTENT_URI, CAN_MODIFY + " AND Calendars._id=" + id);
}
@Nullable
public AndroidCalendar getCalendar(Uri uri) {
return getCalendar(uri, CAN_MODIFY);
}
private AndroidCalendar getCalendar(Uri uri, String selection) {
List<AndroidCalendar> calendars = getCalendars(uri, selection);
return calendars.isEmpty() ? null : calendars.get(0);
}
private List<AndroidCalendar> getCalendars(Uri uri, String selection) {
if (!permissionChecker.canAccessCalendars()) {
return Collections.emptyList();
}
List<AndroidCalendar> calendars = new ArrayList<>();
Cursor cursor = null;
try {
cursor = contentResolver.query(uri, COLUMNS, selection, null, SORT);
if (cursor != null && cursor.getCount() > 0) {
int idColumn = cursor.getColumnIndex(_ID);
int nameColumn = cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME);
while (cursor.moveToNext()) {
calendars.add(new AndroidCalendar(cursor.getString(idColumn), cursor.getString(nameColumn)));
}
}
} catch (Exception e) {
Timber.e(e, e.getMessage());
} finally {
if (cursor != null) {
cursor.close();
}
}
return calendars;
}
}

@ -20,12 +20,13 @@ import android.widget.Toast;
import com.google.common.base.Strings;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.gcal.AndroidCalendar;
import org.tasks.calendars.AndroidCalendar;
import com.todoroo.astrid.gcal.GCalHelper;
import org.tasks.R;
import org.tasks.activities.CalendarSelectionActivity;
import org.tasks.analytics.Tracker;
import org.tasks.calendars.CalendarProvider;
import org.tasks.dialogs.DialogBuilder;
import org.tasks.injection.ForActivity;
import org.tasks.preferences.FragmentPermissionRequestor;
@ -56,6 +57,7 @@ public class CalendarControlSet extends TaskEditControlFragment {
@Bind(R.id.calendar_display_which) TextView calendar;
@Inject GCalHelper gcalHelper;
@Inject CalendarProvider calendarProvider;
@Inject Preferences preferences;
@Inject @ForActivity Context context;
@Inject PermissionChecker permissionChecker;
@ -81,7 +83,7 @@ public class CalendarControlSet extends TaskEditControlFragment {
calendarId = preferences.getDefaultCalendar();
if (!Strings.isNullOrEmpty(calendarId)) {
try {
AndroidCalendar defaultCalendar = gcalHelper.getCalendar(calendarId);
AndroidCalendar defaultCalendar = calendarProvider.getCalendar(calendarId);
if (defaultCalendar == null) {
calendarId = null;
} else {

Loading…
Cancel
Save