|
|
|
@ -5,24 +5,14 @@
|
|
|
|
|
*/
|
|
|
|
|
package com.todoroo.astrid.gcal;
|
|
|
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.res.Resources;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.preference.ListPreference;
|
|
|
|
|
import android.provider.CalendarContract;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import com.todoroo.andlib.service.ContextManager;
|
|
|
|
|
import com.todoroo.andlib.utility.AndroidUtilities;
|
|
|
|
|
import com.todoroo.andlib.utility.Preferences;
|
|
|
|
|
|
|
|
|
|
import org.tasks.R;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import javax.inject.Singleton;
|
|
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
|
public class Calendars {
|
|
|
|
|
|
|
|
|
|
public static final String CALENDAR_CONTENT_CALENDARS = "calendars";
|
|
|
|
@ -41,19 +31,18 @@ public class Calendars {
|
|
|
|
|
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[] {
|
|
|
|
|
public static final String[] CALENDARS_PROJECTION = new String[] {
|
|
|
|
|
ID_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 = CALENDARS_ACCESS_LEVEL_COL + ">= 500";
|
|
|
|
|
public static final String CALENDARS_WHERE = CALENDARS_ACCESS_LEVEL_COL + ">= 500";
|
|
|
|
|
|
|
|
|
|
private static final String CALENDARS_SORT = CALENDARS_DISPLAY_COL + " ASC";
|
|
|
|
|
public static final String CALENDARS_SORT = CALENDARS_DISPLAY_COL + " ASC";
|
|
|
|
|
|
|
|
|
|
// --- api access
|
|
|
|
|
// --- api access
|
|
|
|
|
|
|
|
|
|
/** Return content uri for calendars
|
|
|
|
|
* @param table provider table, something like calendars, events
|
|
|
|
@ -81,153 +70,4 @@ public class Calendars {
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- helper data structure
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper class for working with the results of getCalendars
|
|
|
|
|
*/
|
|
|
|
|
public static class CalendarResult {
|
|
|
|
|
/** calendar names */
|
|
|
|
|
public String[] calendars;
|
|
|
|
|
|
|
|
|
|
/** calendar ids. null entry -> use default */
|
|
|
|
|
public String[] calendarIds;
|
|
|
|
|
|
|
|
|
|
/** default selection index */
|
|
|
|
|
public int defaultIndex = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Appends all user-modifiable calendars to listPreference.
|
|
|
|
|
*/
|
|
|
|
|
public static CalendarResult getCalendars() {
|
|
|
|
|
Context context = ContextManager.getContext();
|
|
|
|
|
ContentResolver cr = context.getContentResolver();
|
|
|
|
|
Resources r = context.getResources();
|
|
|
|
|
|
|
|
|
|
Cursor c = cr.query(getCalendarContentUri(CALENDAR_CONTENT_CALENDARS), CALENDARS_PROJECTION,
|
|
|
|
|
CALENDARS_WHERE, null, CALENDARS_SORT);
|
|
|
|
|
try {
|
|
|
|
|
// Fetch the current setting. Invalid calendar id will
|
|
|
|
|
// be changed to default value.
|
|
|
|
|
String defaultSetting = Preferences.getStringValue(R.string.gcal_p_default);
|
|
|
|
|
|
|
|
|
|
CalendarResult result = new CalendarResult();
|
|
|
|
|
|
|
|
|
|
if (c == null || c.getCount() == 0) {
|
|
|
|
|
// Something went wrong when querying calendars. Only offer them
|
|
|
|
|
// the system default choice
|
|
|
|
|
result.calendars = new String[] {
|
|
|
|
|
r.getString(R.string.gcal_GCP_default) };
|
|
|
|
|
result.calendarIds = new String[] { null };
|
|
|
|
|
result.defaultIndex = 0;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int calendarCount = c.getCount();
|
|
|
|
|
|
|
|
|
|
result.calendars = new String[calendarCount];
|
|
|
|
|
result.calendarIds = new String[calendarCount];
|
|
|
|
|
|
|
|
|
|
// 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(CALENDARS_DISPLAY_COL);
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
String id = c.getString(idColumn);
|
|
|
|
|
String name = c.getString(nameColumn);
|
|
|
|
|
result.calendars[row] = name;
|
|
|
|
|
result.calendarIds[row] = id;
|
|
|
|
|
|
|
|
|
|
// We found currently selected calendar
|
|
|
|
|
if (defaultSetting != null && defaultSetting.equals(id)) {
|
|
|
|
|
result.defaultIndex = row;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
row++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.defaultIndex >= calendarCount) {
|
|
|
|
|
result.defaultIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
} finally {
|
|
|
|
|
if(c != null) {
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Appends all user-modifiable calendars to listPreference.
|
|
|
|
|
*
|
|
|
|
|
* @param context
|
|
|
|
|
* context
|
|
|
|
|
* @param listPreference
|
|
|
|
|
* preference to init
|
|
|
|
|
*/
|
|
|
|
|
public static void initCalendarsPreference(Context context,
|
|
|
|
|
ListPreference listPreference) {
|
|
|
|
|
|
|
|
|
|
Resources r = context.getResources();
|
|
|
|
|
CalendarResult calendars = getCalendars();
|
|
|
|
|
|
|
|
|
|
// Fetch the current setting. Invalid calendar id will
|
|
|
|
|
// be changed to default value.
|
|
|
|
|
String currentSetting = Preferences.getStringValue(R.string.gcal_p_default);
|
|
|
|
|
|
|
|
|
|
int currentSettingIndex = -1;
|
|
|
|
|
|
|
|
|
|
ArrayList<CharSequence> entries = new ArrayList<>();
|
|
|
|
|
entries.addAll(Arrays.asList(r.getStringArray(R.array.EPr_default_addtocalendar)));
|
|
|
|
|
entries.addAll(Arrays.asList(calendars.calendars));
|
|
|
|
|
|
|
|
|
|
ArrayList<CharSequence> entryValues = new ArrayList<>();
|
|
|
|
|
entryValues.addAll(Arrays.asList(r.getStringArray(R.array.EPr_default_addtocalendar_values)));
|
|
|
|
|
entryValues.addAll(Arrays.asList(calendars.calendarIds));
|
|
|
|
|
|
|
|
|
|
listPreference.setEntries(entries.toArray(new CharSequence[entries.size()]));
|
|
|
|
|
listPreference.setEntryValues(entryValues.toArray(new CharSequence[entryValues.size()]));
|
|
|
|
|
|
|
|
|
|
listPreference.setValueIndex(0);
|
|
|
|
|
listPreference.setEnabled(true);
|
|
|
|
|
|
|
|
|
|
if (calendars.calendarIds.length == 0 || calendars.calendars.length == 0) {
|
|
|
|
|
// Something went wrong when querying calendars
|
|
|
|
|
// Leave the preference at disabled.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Iterate calendars one by one, and fill up the list preference
|
|
|
|
|
if (currentSetting != null) {
|
|
|
|
|
for (int i=0; i<calendars.calendarIds.length; i++) {
|
|
|
|
|
// We found currently selected calendar
|
|
|
|
|
if (currentSetting.equals(calendars.calendarIds[i])) {
|
|
|
|
|
currentSettingIndex = i+1; // +1 correction for disabled-entry
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(currentSettingIndex == -1 || currentSettingIndex > calendars.calendarIds.length+1) {
|
|
|
|
|
// Should not happen!
|
|
|
|
|
// Leave the preference at disabled.
|
|
|
|
|
Log.d("astrid", "initCalendarsPreference: Unknown calendar.");
|
|
|
|
|
currentSettingIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listPreference.setValueIndex(currentSettingIndex);
|
|
|
|
|
listPreference.setEnabled(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gets the default calendar for future use
|
|
|
|
|
* @return the calendar id for use with the contentresolver
|
|
|
|
|
*/
|
|
|
|
|
public static String getDefaultCalendar() {
|
|
|
|
|
return Preferences.getStringValue(R.string.gcal_p_default);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|