Skip to content

Commit

Permalink
allow editing calendar on existing events
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-haeusler committed Oct 14, 2023
1 parent 89d9401 commit a7ecbaf
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ public class CalendarEventModel implements Serializable {
public boolean mOrganizerCanRespond = false;
public int mCalendarAccessLevel = Calendars.CAL_ACCESS_CONTRIBUTOR;
public int mEventStatus = Events.STATUS_CONFIRMED;
// The model can't be updated with a calendar cursor until it has been
// updated with an event cursor.
public boolean mModelUpdatedWithEventCursor;
public int mAccessLevel = 0;
public ArrayList<ReminderEntry> mReminders;
public ArrayList<ReminderEntry> mDefaultReminders;
Expand Down Expand Up @@ -292,7 +289,6 @@ public void clear() {
mEventStatus = Events.STATUS_CONFIRMED;
mOrganizerCanRespond = false;
mCalendarAccessLevel = Calendars.CAL_ACCESS_CONTRIBUTOR;
mModelUpdatedWithEventCursor = false;
mCalendarAllowedReminders = null;
mCalendarAllowedAttendeeTypes = null;
mCalendarAllowedAvailability = null;
Expand Down Expand Up @@ -351,7 +347,6 @@ public int hashCode() {
result = prime * result + (mGuestsCanModify ? 1231 : 1237);
result = prime * result + (mGuestsCanSeeGuests ? 1231 : 1237);
result = prime * result + (mOrganizerCanRespond ? 1231 : 1237);
result = prime * result + (mModelUpdatedWithEventCursor ? 1231 : 1237);
result = prime * result + mCalendarAccessLevel;
result = prime * result + (mHasAlarm ? 1231 : 1237);
result = prime * result + (mHasAttendeeData ? 1231 : 1237);
Expand Down Expand Up @@ -616,9 +611,6 @@ protected boolean checkOriginalModelFields(CalendarEventModel originalModel) {
if (mCalendarAccessLevel != originalModel.mCalendarAccessLevel) {
return false;
}
if (mModelUpdatedWithEventCursor != originalModel.mModelUpdatedWithEventCursor) {
return false;
}
if (mHasAlarm != originalModel.mHasAlarm) {
return false;
}
Expand Down
73 changes: 52 additions & 21 deletions app/src/main/java/com/android/calendar/DeleteEventHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,7 @@ public class DeleteEventHelper {
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int button) {
deleteStarted();
long id = mModel.mId; // mCursor.getInt(mEventIndexId);

// If this event is part of a local calendar, really remove it from the database
//
// "There are two versions of delete: as an app and as a sync adapter.
// An app delete will set the deleted column on an event and remove all instances of that event.
// A sync adapter delete will remove the event from the database and all associated data."
// from https://developer.android.com/reference/android/provider/CalendarContract.Events
boolean isLocal = mModel.mSyncAccountType.equals(CalendarContract.ACCOUNT_TYPE_LOCAL);
Uri deleteContentUri = isLocal ? CalendarRepository.asLocalCalendarSyncAdapter(mModel.mSyncAccountName, Events.CONTENT_URI) : Events.CONTENT_URI;

Uri uri = ContentUris.withAppendedId(deleteContentUri, id);
mService.startDelete(mService.getNextToken(), null, uri, null, null, Utils.UNDO_DELAY);
if (mCallback != null) {
mCallback.run();
}
if (mExitWhenDone) {
mParent.finish();
}
deleteNormalEvent();
}
};
/**
Expand Down Expand Up @@ -164,7 +146,7 @@ public void onClick(DialogInterface dialog, int button) {
}
};

public DeleteEventHelper(Context context, Activity parentActivity, boolean exitWhenDone) {
public DeleteEventHelper(Context context, Activity parentActivity, boolean exitWhenDone, boolean prompt) {
if (exitWhenDone && parentActivity == null) {
throw new IllegalArgumentException("parentActivity is required to exit when done");
}
Expand All @@ -182,12 +164,20 @@ protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
CalendarEventModel mModel = new CalendarEventModel();
EditEventHelper.setModelFromCursor(mModel, cursor, mContext);
cursor.close();
DeleteEventHelper.this.delete(mStartMillis, mEndMillis, mModel, mWhichDelete);
if (prompt) {
delete(mStartMillis, mEndMillis, mModel, mWhichDelete);
} else {
deleteUnprompted(mStartMillis, mEndMillis, mModel, mWhichDelete);
}
}
};
mExitWhenDone = exitWhenDone;
}

public DeleteEventHelper(Context context, Activity parentActivity, boolean exitWhenDone) {
this(context, parentActivity, exitWhenDone, true);
}

public void setExitWhenDone(boolean exitWhenDone) {
mExitWhenDone = exitWhenDone;
}
Expand Down Expand Up @@ -339,6 +329,47 @@ public void delete(long begin, long end, CalendarEventModel model, int which) {
}
}

private void deleteUnprompted(long begin, long end, CalendarEventModel model, int which) {
mWhichDelete = which;
mStartMillis = begin;
mEndMillis = end;
mModel = model;
mSyncId = model.mSyncId;

if (TextUtils.isEmpty(model.mRrule)) {
String originalEvent = model.mOriginalSyncId;
if (originalEvent == null) {
deleteNormalEvent();
} else {
deleteExceptionEvent();
}
} else {
deleteRepeatingEvent(which);
}
}

private void deleteNormalEvent() {
long id = mModel.mId; // mCursor.getInt(mEventIndexId);

// If this event is part of a local calendar, really remove it from the database
//
// "There are two versions of delete: as an app and as a sync adapter.
// An app delete will set the deleted column on an event and remove all instances of that event.
// A sync adapter delete will remove the event from the database and all associated data."
// from https://developer.android.com/reference/android/provider/CalendarContract.Events
boolean isLocal = mModel.mSyncAccountType.equals(CalendarContract.ACCOUNT_TYPE_LOCAL);
Uri deleteContentUri = isLocal ? CalendarRepository.asLocalCalendarSyncAdapter(mModel.mSyncAccountName, Events.CONTENT_URI) : Events.CONTENT_URI;

Uri uri = ContentUris.withAppendedId(deleteContentUri, id);
mService.startDelete(mService.getNextToken(), null, uri, null, null, Utils.UNDO_DELAY);
if (mCallback != null) {
mCallback.run();
}
if (mExitWhenDone) {
mParent.finish();
}
}

private void deleteExceptionEvent() {
long id = mModel.mId; // mCursor.getInt(mEventIndexId);

Expand Down
82 changes: 33 additions & 49 deletions app/src/main/java/com/android/calendar/event/EditEventFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = (AppCompatActivity) activity;

mHelper = new EditEventHelper(activity, null);
mHelper = new EditEventHelper(activity);
mHandler = new QueryHandler(activity.getContentResolver());
mModel = new CalendarEventModel(activity, mIntent);
mInputMethodManager = (InputMethodManager)
Expand Down Expand Up @@ -735,12 +735,10 @@ protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
}

// TOKEN_CALENDARS
String[] selArgs = {
Long.toString(mModel.mCalendarId)
};
mHandler.startQuery(TOKEN_CALENDARS, null, Calendars.CONTENT_URI,
EditEventHelper.CALENDARS_PROJECTION, EditEventHelper.CALENDARS_WHERE,
selArgs /* selection args */, null /* sort order */);
EditEventHelper.CALENDARS_PROJECTION,
EditEventHelper.CALENDARS_WHERE_WRITEABLE_VISIBLE,
null /* selection args */, null /* sort order */);

// TOKEN_COLORS
mHandler.startQuery(TOKEN_COLORS, null, Colors.CONTENT_URI,
Expand All @@ -752,17 +750,15 @@ protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
mModel.mCalendarAccountName,
mModel.mCalendarAccountType
);
selArgs = new String[]{
Long.toString(eventId)
};
String[] selArgs = new String[]{ Long.toString(eventId) };
mHandler.startQuery(TOKEN_EXTENDED, null, extendedPropUri,
EditEventHelper.EXTENDED_PROJECTION,
EditEventHelper.EXTENDED_WHERE_EVENT, selArgs, null);

setModelIfDone(TOKEN_EVENT);
break;
case TOKEN_ATTENDEES:
try {
try (cursor) {
while (cursor.moveToNext()) {
String name = cursor.getString(EditEventHelper.ATTENDEES_INDEX_NAME);
String email = cursor.getString(EditEventHelper.ATTENDEES_INDEX_EMAIL);
Expand Down Expand Up @@ -806,14 +802,12 @@ protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
mModel.addAttendee(attendee);
mOriginalModel.addAttendee(attendee);
}
} finally {
cursor.close();
}

setModelIfDone(TOKEN_ATTENDEES);
break;
case TOKEN_REMINDERS:
try {
try (cursor) {
// Add all reminders to the models
while (cursor.moveToNext()) {
int minutes = cursor.getInt(EditEventHelper.REMINDERS_INDEX_MINUTES);
Expand All @@ -826,55 +820,45 @@ protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
// Sort appropriately for display
Collections.sort(mModel.mReminders);
Collections.sort(mOriginalModel.mReminders);
} finally {
cursor.close();
}

setModelIfDone(TOKEN_REMINDERS);
break;
case TOKEN_CALENDARS:
try {
if (mModel.mId == -1) {
// Populate Calendar spinner only if no event id is set.
MatrixCursor matrixCursor = Utils.matrixCursorFromCursor(cursor);
if (DEBUG) {
Log.d(TAG, "onQueryComplete: setting cursor with "
+ matrixCursor.getCount() + " calendars");
}
mView.setCalendarsCursor(matrixCursor, isAdded() && isResumed(),
mCalendarId);
} else {
try (cursor) {
MatrixCursor matrixCursor = Utils.matrixCursorFromCursor(cursor);
if (DEBUG) {
Log.d(TAG, "onQueryComplete: setting cursor with " + matrixCursor.getCount() + " calendars");
}
if (mModel.mId != -1) {
// Populate model for an existing event
EditEventHelper.setModelFromCalendarCursor(mModel, cursor, activity);
EditEventHelper.setModelFromCalendarCursor(mOriginalModel, cursor, activity);
}
} finally {
cursor.close();
mView.setCalendarsCursor(matrixCursor, isAdded() && isResumed(), mModel.mCalendarId);
}
setModelIfDone(TOKEN_CALENDARS);
break;
case TOKEN_COLORS:
if (cursor.moveToFirst()) {
EventColorCache cache = new EventColorCache();
do {
String colorKey = cursor.getString(EditEventHelper.COLORS_INDEX_COLOR_KEY);
int rawColor = cursor.getInt(EditEventHelper.COLORS_INDEX_COLOR);
int displayColor = Utils.getDisplayColorFromColor(activity, rawColor);
String accountName = cursor
.getString(EditEventHelper.COLORS_INDEX_ACCOUNT_NAME);
String accountType = cursor
.getString(EditEventHelper.COLORS_INDEX_ACCOUNT_TYPE);
cache.insertColor(accountName, accountType,
displayColor, colorKey);
} while (cursor.moveToNext());
cache.sortPalettes(new HsvColorComparator());

mModel.mEventColorCache = cache;
mView.mColorPickerNewEvent.setOnClickListener(mOnColorPickerClicked);
mView.mColorPickerExistingEvent.setOnClickListener(mOnColorPickerClicked);
}
if (cursor != null) {
cursor.close();
try (cursor) {
if (cursor.moveToFirst()) {
EventColorCache cache = new EventColorCache();
do {
String colorKey = cursor.getString(EditEventHelper.COLORS_INDEX_COLOR_KEY);
int rawColor = cursor.getInt(EditEventHelper.COLORS_INDEX_COLOR);
int displayColor = Utils.getDisplayColorFromColor(activity, rawColor);
String accountName = cursor
.getString(EditEventHelper.COLORS_INDEX_ACCOUNT_NAME);
String accountType = cursor
.getString(EditEventHelper.COLORS_INDEX_ACCOUNT_TYPE);
cache.insertColor(accountName, accountType,
displayColor, colorKey);
} while (cursor.moveToNext());
cache.sortPalettes(new HsvColorComparator());

mModel.mEventColorCache = cache;
mView.mColorPicker.setOnClickListener(mOnColorPickerClicked);
}
}

// If the account name/type is null, the calendar event colors cannot be
Expand Down
Loading

0 comments on commit a7ecbaf

Please sign in to comment.