Merge of default-calendar branch in launchpad from Sami Salonen. (lp:~ssalonen/astrid/default-calendar)

Tim Su 16 years ago
parent f96f29171f
commit 3be69a1d6b

@ -51,6 +51,9 @@
<string name="p_backup">backup</string>
<string name="prefs_defaultCalendar">default_calendar_id</string>
<string name="prefs_defaultCalendar_default">1</string>
<string name="prefs_titleVisible">titleVisible</string>
<string name="prefs_titleVisible_default">true</string>
<string name="prefs_deadlineVisible">deadlineVisible</string>

@ -475,6 +475,11 @@ Skipped %d tasks.\n
<!-- backup success message (%s -> date) -->
<string name="prefs_backup_desc_success">Latest backup was on %s</string>
<string name="prefs_defaultCalendar_title">Calendar</string>
<string name="prefs_defaultCalendar_desc">Calendar to store the events.</string>
<string name="prefs_defaultCalendar_astrid_default">Astrid default</string>
<string name="displayedFields_PrefScreen_Title">Displayed Fields</string>
<string name="displayedFields_PrefScreen_Desc">Select the fields to show in task list</string>

@ -113,5 +113,9 @@
android:title="@string/prefs_backup_title"
android:summary="@string/prefs_backup_desc"
android:defaultValue="true" />
<ListPreference
android:key="@string/prefs_defaultCalendar"
android:title="@string/prefs_defaultCalendar_title"
android:summary="@string/prefs_defaultCalendar_desc" />
</PreferenceCategory>
</PreferenceScreen>

@ -20,11 +20,13 @@
package com.timsu.astrid.activities;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import com.flurry.android.FlurryAgent;
import com.timsu.astrid.R;
import com.timsu.astrid.utilities.Calendars;
import com.timsu.astrid.utilities.Constants;
import com.timsu.astrid.utilities.Preferences;
@ -45,6 +47,9 @@ public class EditPreferences extends PreferenceActivity {
String backupSummary = Preferences.getBackupSummary(this);
if(backupSummary != null && backupPreference != null)
backupPreference.setSummary(backupSummary);
ListPreference defaultCalendarPreference = (ListPreference) findPreference(getString(R.string.prefs_defaultCalendar));
Calendars.initCalendarsPreference(this, defaultCalendarPreference);
}
@Override

@ -766,12 +766,13 @@ public class TaskEdit extends TaskModificationTabbedActivity<TaskModelForEdit> {
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", name.getText().toString());
values.put("calendar_id", 1);
values.put("calendar_id", Preferences.getDefaultCalendarIDSafe(this));
values.put("description", notes.getText().toString());
values.put("hasAlarm", 0);
values.put("transparency", 0);
@ -781,11 +782,13 @@ public class TaskEdit extends TaskModificationTabbedActivity<TaskModelForEdit> {
model.getDefiniteDueDate(), model.getEstimatedSeconds(),
values);
Uri result = cr.insert(uri, values);
if(result != null)
model.setCalendarUri(result.toString());
else
Log.e("astrid", "Error creating calendar event!");
Uri result = null;
try{
result = cr.insert(uri, values);
model.setCalendarUri(result.toString());
} catch (IllegalArgumentException e) {
Log.e("astrid", "Error creating calendar event!", e);
}
}
if(shouldSaveState)

@ -0,0 +1,179 @@
package com.timsu.astrid.utilities;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.util.Log;
import com.timsu.astrid.R;
public class Calendars {
// Private SDK
private static final Uri CALENDAR_CONTENT_URI = Uri
.parse("content://calendar/calendars"); // Calendars.CONTENT_URI
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";
private static final String[] CALENDARS_PROJECTION = new String[] {
ID_COLUMN_NAME, // Calendars._ID,
DISPLAY_COLUMN_NAME // Calendars.DISPLAY_NAME
};
// Only show calendars that the user can modify. Access level 500
// corresponds to Calendars.CONTRIBUTOR_ACCESS
private static final String CALENDARS_WHERE = ACCES_LEVEL_COLUMN_NAME + " >= 500";
private static final String CALENDARS_WHERE_ID = ACCES_LEVEL_COLUMN_NAME+" >= 500 AND" + ID_COLUMN_NAME +"=?";
private static final String CALENDARS_SORT = "displayName ASC";
/**
* Ensures that the default calendar preference is pointing to
* user-modifiable calendar that exists. If the calendar does not exist
* anymore, the preference is reset to default value.
*
* @param context
* Context
*/
public static void ensureValidDefaultCalendarPreference(Context context) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
Resources r = context.getResources();
Editor editor = prefs.edit();
// We default the 'defaultCalendar' setting when it is undefined
// or when the calendar does not exist anymore
if (!prefs.contains(r.getString(R.string.prefs_defaultCalendar))
|| !Calendars.isCalendarPresent(context, prefs.getString(r
.getString(R.string.prefs_defaultCalendar), null))) {
editor.putString(r.getString(R.string.prefs_defaultCalendar), r
.getString(R.string.prefs_defaultCalendar_default));
editor.commit();
}
}
/**
* Appends all user-modifiable calendars to listPreference. Always includes
* entry called "Astrid default" with calendar id of
* prefs_defaultCalendar_default.
*
* @param context
* context
* @param listPreference
* preference to init
*/
public static void initCalendarsPreference(Context context,
ListPreference listPreference) {
ContentResolver cr = context.getContentResolver();
Resources r = context.getResources();
Cursor c = cr.query(CALENDAR_CONTENT_URI, CALENDARS_PROJECTION,
CALENDARS_WHERE, null, CALENDARS_SORT);
// Fetch the current setting. Invalid calendar id will
// be changed to default value.
String currentSetting = String.valueOf(Preferences
.getDefaultCalendarIDSafe(context));
int currentSettingIndex = -1;
if (c == null) {
// Something went wrong when querying calendars
// Let's keep the "Astrid default" only.
listPreference
.setEntries(new String[] { r
.getString(R.string.prefs_defaultCalendar_astrid_default) });
listPreference.setEntryValues(new String[] { r
.getString(R.string.prefs_defaultCalendar_default) });
listPreference.setValueIndex(0);
listPreference.setEnabled(true);
return;
}
int calendarCount = c.getCount();
String[] entries = new String[calendarCount];
String[] entryValues = new String[calendarCount];
// Iterate calendars one by one, and fill up the list preference
try {
int row = 0;
int idColumn = c.getColumnIndex(ID_COLUMN_NAME);
int nameColumn = c.getColumnIndex(DISPLAY_COLUMN_NAME);
while (c.moveToNext()) {
String id = c.getString(idColumn);
String name = c.getString(nameColumn);
entries[row] = name;
entryValues[row] = id;
// We found currently selected calendar
if (currentSetting.equals(id)) {
currentSettingIndex = row;
}
row++;
}
if (currentSettingIndex == -1) {
// Should not happen!
// Let's keep the "Astrid default" only.
Log.d("astrid", "initCalendarsPreference: Unknown calendar.");
listPreference
.setEntries(new String[] { r
.getString(R.string.prefs_defaultCalendar_astrid_default) });
listPreference.setEntryValues(new String[] { r
.getString(R.string.prefs_defaultCalendar_default) });
listPreference.setValueIndex(0);
listPreference.setEnabled(true);
}
listPreference.setEntries(entries);
listPreference.setEntryValues(entryValues);
listPreference.setValueIndex(currentSettingIndex);
listPreference.setEnabled(true);
} finally {
c.deactivate();
}
}
/**
* Checks whether user-modifiable calendar is present with a given id.
*
* @param context
* Context
* @param id
* Calendar ID to search for
* @return true, if user-modifiable calendar with the given id exists; false
* otherwise.
*/
private static boolean isCalendarPresent(Context context, String id) {
if (id == null)
return false;
ContentResolver cr = context.getContentResolver();
Cursor c = null;
try {
c = cr.query(CALENDAR_CONTENT_URI, CALENDARS_PROJECTION,
CALENDARS_WHERE_ID, new String[] { id }, CALENDARS_SORT);
} finally {
if (c != null) {
c.deactivate();
}
}
return (c != null) && (c.getCount() != 0);
}
}

@ -68,6 +68,8 @@ public class Preferences {
editor.putString(P_BACKUP_ERROR, null);
}
Calendars.ensureValidDefaultCalendarPreference(context);
setVisibilityPreferences(prefs, editor, r);
editor.commit();
@ -110,7 +112,7 @@ public class Preferences {
// --- system preferences
/** CurrentVersion: the currently installed version of Astrid */
/** CurrentVersion: the currently installed version of Astrid */
public static int getCurrentVersion(Context context) {
return getPrefs(context).getInt(P_CURRENT_VERSION, 0);
}
@ -444,6 +446,28 @@ public class Preferences {
editor.commit();
}
/** Get default calendar id. */
public static String getDefaultCalendarID(Context context) {
Resources r = context.getResources();
return getPrefs(context).getString(
r.getString(R.string.prefs_defaultCalendar),
r.getString(R.string.prefs_defaultCalendar_default));
}
/** Get default calendar id. Returns default value if the calendar does not exist anymore.*/
public static String getDefaultCalendarIDSafe(Context context) {
Calendars.ensureValidDefaultCalendarPreference(context);
return getDefaultCalendarID(context);
}
/** Set default calendar id */
public static void setDefaultCalendarID(Context context, String value) {
Resources r = context.getResources();
Editor editor = getPrefs(context).edit();
editor.putString(r.getString(R.string.prefs_defaultCalendar), value);
editor.commit();
}
// --- helper methods
/** Clear the given preference */

Loading…
Cancel
Save