Google calendar integration - now integrated

pull/14/head
Tim Su 14 years ago
parent b333a8243c
commit 1af6202dcd

@ -6,7 +6,6 @@
<intAttribute key="ch.zork.quicklaunch.index" value="0"/>
<stringAttribute key="ch.zork.quicklaunch.mode" value="run"/>
<intAttribute key="com.android.ide.eclipse.adt.action" value="0"/>
<stringAttribute key="com.android.ide.eclipse.adt.avd" value="evo-8-google"/>
<stringAttribute key="com.android.ide.eclipse.adt.commandline" value="-scale 0.7"/>
<intAttribute key="com.android.ide.eclipse.adt.delay" value="0"/>
<booleanAttribute key="com.android.ide.eclipse.adt.nobootanim" value="true"/>

@ -7,11 +7,15 @@ import android.database.Cursor;
import android.net.Uri;
import com.timsu.astrid.R;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.astrid.utility.Preferences;
@SuppressWarnings("nls")
public class Calendars {
public static final String CALENDAR_CONTENT_CALENDARS = "calendars";
public static final String CALENDAR_CONTENT_EVENTS = "events";
private static final String ID_COLUMN_NAME = "_id";
private static final String DISPLAY_COLUMN_NAME = "displayName";
private static final String ACCES_LEVEL_COLUMN_NAME = "access_level";
@ -29,12 +33,14 @@ public class Calendars {
// --- api access
/** Return content uri for calendars */
public static Uri getCalendarContentUri() {
/** Return content uri for calendars
* @param table provider table, something like calendars, events
*/
public static Uri getCalendarContentUri(String table) {
if(android.os.Build.VERSION.SDK_INT >= 8)
return Uri.parse("content://com.android.calendar/calendars");
return Uri.parse("content://com.android.calendar/" + table);
else
return Uri.parse("content://calendar/calendars");
return Uri.parse("content://calendar/" + table);
}
/** Return calendar package name */
@ -57,8 +63,8 @@ public class Calendars {
/** calendar ids. null entry -> use default */
public String[] calendarIds;
/** selected index */
public int selectedIndex = -1;
/** default selection index */
public int defaultIndex = -1;
}
/**
@ -71,11 +77,11 @@ public class Calendars {
* @param listPreference
* preference to init
*/
public static CalendarResult getCalendars(Context context) {
public static CalendarResult getCalendars() {
Context context = ContextManager.getContext();
ContentResolver cr = context.getContentResolver();
Resources r = context.getResources();
Cursor c = cr.query(getCalendarContentUri(), CALENDARS_PROJECTION,
Cursor c = cr.query(getCalendarContentUri(CALENDAR_CONTENT_CALENDARS), CALENDARS_PROJECTION,
CALENDARS_WHERE, null, CALENDARS_SORT);
// Fetch the current setting. Invalid calendar id will
@ -90,7 +96,7 @@ public class Calendars {
result.calendars = new String[] {
r.getString(R.string.gcal_GCP_default) };
result.calendarIds = new String[] { null };
result.selectedIndex = 0;
result.defaultIndex = 0;
return result;
}
@ -112,14 +118,14 @@ public class Calendars {
// We found currently selected calendar
if (defaultSetting != null && defaultSetting.equals(id)) {
result.selectedIndex = row;
result.defaultIndex = row;
}
row++;
}
if (result.selectedIndex == -1 || result.selectedIndex >= calendarCount) {
result.selectedIndex = 0;
if (result.defaultIndex == -1 || result.defaultIndex >= calendarCount) {
result.defaultIndex = 0;
}
return result;
@ -128,4 +134,12 @@ public class Calendars {
}
}
/**
* sets the default calendar for future use
* @param defaultCalendar default calendar id
*/
public static void setDefaultCalendar(String defaultCalendar) {
Preferences.setString(R.string.gcal_p_default, defaultCalendar);
}
}

@ -1,21 +1,26 @@
package com.todoroo.astrid.gcal;
import java.util.Date;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.CompoundButton.OnCheckedChangeListener;
import com.flurry.android.FlurryAgent;
import com.timsu.astrid.R;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ExceptionService;
import com.todoroo.astrid.activity.TaskEditActivity.TaskEditControlSet;
import com.todoroo.astrid.gcal.Calendars.CalendarResult;
import com.todoroo.astrid.model.Task;
@ -33,17 +38,19 @@ public class GCalControlSet implements TaskEditControlSet {
// --- instance variables
@Autowired
private ExceptionService exceptionService;
private final Activity activity;
private final CalendarResult calendars;
private Uri calendarUri = null;
private final CalendarResult calendars;
private final CheckBox addToCalendar;
private final Spinner calendarSelector;
private final Button viewCalendarEvent;
public GCalControlSet(Activity activity, ViewGroup parent) {
public GCalControlSet(final Activity activity, ViewGroup parent) {
this.activity = activity;
LayoutInflater.from(activity).inflate(R.layout.gcal_control, parent, true);
@ -51,7 +58,7 @@ public class GCalControlSet implements TaskEditControlSet {
this.calendarSelector = (Spinner) activity.findViewById(R.id.calendars);
this.viewCalendarEvent = (Button) activity.findViewById(R.id.view_calendar_event);
calendars = Calendars.getCalendars(activity);
calendars = Calendars.getCalendars();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
android.R.layout.simple_spinner_item, calendars.calendars);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
@ -64,92 +71,80 @@ public class GCalControlSet implements TaskEditControlSet {
}
});
}
/** Take the values from the model and set the calendar start and end times
* based on these. Sets keys 'dtstart' and 'dtend'.
*
* @param preferred preferred due date or null
* @param definite definite due date or null
* @param estimatedSeconds estimated duration or null
* @param values
*/
@SuppressWarnings({ "nls", "unused" })
public void createCalendarStartEndTimes(Date preferred, Date definite,
Integer estimatedSeconds, ContentValues values) {
FlurryAgent.onEvent("create-calendar-event");
Long deadlineDate = null;
if (preferred != null && preferred.after(new Date()))
deadlineDate = preferred.getTime();
else if (definite != null)
deadlineDate = definite.getTime();
else
deadlineDate = System.currentTimeMillis() + 24*3600*1000L;
int estimatedTime = DEFAULT_CAL_TIME;
if(estimatedSeconds != null && estimatedSeconds > 0) {
estimatedTime = estimatedSeconds;
}
values.put("dtstart", deadlineDate - estimatedTime * 1000L);
values.put("dtend", deadlineDate);
}
@SuppressWarnings("unused")
protected void onPause() {
// create calendar event
/*if(addToCalendar.isChecked() && model.getCalendarUri() == null) {
Uri uri = Uri.parse("content://calendar/events");
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put("title", title.getText().toString());
values.put("calendar_id", Preferences.getDefaultCalendarIDSafe(this));
values.put("description", notes.getText().toString());
values.put("hasAlarm", 0);
values.put("transparency", 0);
values.put("visibility", 0);
createCalendarStartEndTimes(model.getPreferredDueDate(),
model.getDefiniteDueDate(), model.getEstimatedSeconds(),
values);
Uri result = null;
try{
result = cr.insert(uri, values);
model.setCalendarUri(result.toString());
} catch (IllegalArgumentException e) {
Log.e("astrid", "Error creating calendar event!", e);
viewCalendarEvent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(calendarUri == null)
return;
Intent intent = new Intent(Intent.ACTION_EDIT, calendarUri);
activity.startActivity(intent);
}
} */
// save save save
/* if(addToCalendar.isChecked() && model.getCalendarUri() != null) {
Uri result = Uri.parse(model.getCalendarUri());
Intent intent = new Intent(Intent.ACTION_EDIT, result);
ContentValues values = new ContentValues();
createCalendarStartEndTimes(model.getPreferredDueDate(),
model.getDefiniteDueDate(), model.getEstimatedSeconds(),
values);
intent.putExtra("beginTime", values.getAsLong("dtstart"));
intent.putExtra("endTime", values.getAsLong("dtend"));
startActivity(intent);
} */
});
}
@Override
public void readFromTask(Task task) {
//
String uri = task.getValue(Task.CALENDAR_URI);
if(!TextUtils.isEmpty(uri)) {
try {
calendarUri = Uri.parse(uri);
addToCalendar.setVisibility(View.GONE);
calendarSelector.setVisibility(View.GONE);
viewCalendarEvent.setVisibility(View.VISIBLE);
} catch (Exception e) {
exceptionService.reportError("unable-to-parse-calendar: " + //$NON-NLS-1$
task.getValue(Task.CALENDAR_URI), e);
}
}
}
@SuppressWarnings("nls")
@Override
public void writeToModel(Task task) {
//
if(addToCalendar.isChecked() && calendarUri == null) {
FlurryAgent.onEvent("create-calendar-event");
try{
Uri uri = Calendars.getCalendarContentUri(Calendars.CALENDAR_CONTENT_EVENTS);
ContentResolver cr = activity.getContentResolver();
ContentValues values = new ContentValues();
values.put("title", task.getValue(Task.TITLE));
String calendarId = calendars.calendarIds[calendarSelector.getSelectedItemPosition()];
Calendars.setDefaultCalendar(calendarId);
values.put("calendar_id", calendarId);
values.put("description", task.getValue(Task.NOTES));
values.put("hasAlarm", 0);
values.put("transparency", 0);
values.put("visibility", 0);
long dueDate = task.getValue(Task.DUE_DATE);
if(task.hasDueDate()) {
if(task.hasDueTime()) {
int estimatedTime = task.getValue(Task.ESTIMATED_SECONDS);
if(estimatedTime <= 0)
estimatedTime = DEFAULT_CAL_TIME;
values.put("dtstart", dueDate - estimatedTime);
values.put("dtend", dueDate);
} else {
values.put("dtstart", dueDate);
values.put("dtend", dueDate);
values.put("allDay", "1");
}
}
calendarUri = cr.insert(uri, values);
task.setValue(Task.CALENDAR_URI, calendarUri.toString());
// pop up the new event
Intent intent = new Intent(Intent.ACTION_EDIT, calendarUri);
intent.putExtra("beginTime", values.getAsLong("dtstart"));
intent.putExtra("endTime", values.getAsLong("dtend"));
activity.startActivity(intent);
} catch (Exception e) {
exceptionService.displayAndReportError(activity,
activity.getString(R.string.gcal_TEA_error), e);
}
}
}
}

@ -11,14 +11,14 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/TEA_calendar_label"
android:text="@string/gcal_TEA_calendar_label"
style="@style/TextAppearance.GEN_EditLabel" />
<CheckBox
android:id="@+id/add_to_calendar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/TEA_addToCalendar_label" />
android:text="@string/gcal_TEA_addToCalendar_label" />
<Spinner
android:id="@+id/calendars"
@ -29,7 +29,7 @@
android:id="@+id/view_calendar_event"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/TEA_showCalendar_label"
android:text="@string/gcal_TEA_showCalendar_label"
android:visibility="gone" />
</LinearLayout>

@ -289,32 +289,6 @@ to the plugin creator for fastest service.
<!-- Elapsed time label -->
<string name="TEA_elapsedDuration_label">Time Already Spent on Task</string>
<!-- Label for adding task to calendar -->
<string name="TEA_calendar_label">Calendar Integration:</string>
<!-- Label for adding task to calendar -->
<string name="TEA_addToCalendar_label">Create Calendar Event</string>
<!-- Label when calendar event already exists -->
<string name="TEA_showCalendar_label">Open Calendar Event</string>
<!-- Repeat Setting label -->
<string name="TEA_repeat_label">Repeat Every</string>
<!-- Repeat label when repeats are not set -->
<string name="TEA_repeat_value_unset">No Repeat Set</string>
<!-- dialog boxes -->
<string name="TEA_repeat_picker_title">Repeat Every (0 to disable)</string>
<string name="TEA_repeat_help_dialog_title">Help: Astrid Repeats</string>
<string name="TEA_repeat_help_dialog">
To use repeats, set at least one of the deadlines above. When you complete this task, the deadline will be automatically advanced.
\n\n
If you don\'t want to see the new task right after you complete the old one, you should use the \"Hide Until\" field, which will also be advanced automatically.
\n
</string>
<string name="TEA_repeat_help_hide">Don\'t Show Help Anymore</string>
<!-- Save button -->
<string name="TEA_save_label">Save</string>

@ -5,6 +5,19 @@
<!-- Resources for built-in tag plug-in -->
<!-- =============================================== Task Edit Controls == -->
<!-- Error message for adding to calendar -->
<string name="gcal_TEA_error">Error adding task to calendar!</string>
<!-- Label for adding task to calendar -->
<string name="gcal_TEA_calendar_label">Calendar Integration:</string>
<!-- Label for adding task to calendar -->
<string name="gcal_TEA_addToCalendar_label">Create Calendar Event</string>
<!-- Label when calendar event already exists -->
<string name="gcal_TEA_showCalendar_label">Open Calendar Event</string>
<!-- ================================================== Preference Keys == -->

@ -200,6 +200,16 @@ public class Preferences {
editor.commit();
}
/**
* Sets string preference
*/
public static void setString(int keyResource, String newValue) {
Context context = ContextManager.getContext();
Editor editor = getPrefs(context).edit();
editor.putString(context.getString(keyResource), newValue);
editor.commit();
}
/**
* Sets string preference from integer value
*/

Loading…
Cancel
Save