Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow editing calendar on existing events #1421

Merged
merged 9 commits into from
Jun 10, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class CalendarEventModel implements Serializable {
public String mDescription = null;
public String mUrl = null;
public String mRrule = null;
public String mExDate = null;
public String mOrganizer = null;
public String mOrganizerDisplayName = null;
/**
Expand Down Expand Up @@ -111,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 @@ -291,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 @@ -350,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 @@ -615,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
102 changes: 52 additions & 50 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 @@ -670,7 +670,7 @@ protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
long eventId;
switch (token) {
case TOKEN_EVENT:
if (cursor.getCount() == 0) {
if (!cursor.moveToFirst()) {
// The cursor is empty. This can happen if the event
// was deleted.
cursor.close();
Expand Down Expand Up @@ -734,13 +734,29 @@ protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
setModelIfDone(TOKEN_REMINDERS);
}

final String selection;
final String[] selectionArgs;
final boolean isRecurring = !TextUtils.isEmpty(mModel.mRrule);
if (isRecurring && Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
// recurring event AND api level < 30. disable changing calendars.
selection = EditEventHelper.CALENDARS_WHERE;
selectionArgs = new String[] { Long.toString(mModel.mCalendarId) };
} else if (isRecurring) {
// recurring event AND api level >= 30. enable changing calendars to synced calendars.
Gitsaibot marked this conversation as resolved.
Show resolved Hide resolved
selection = EditEventHelper.CALENDARS_WHERE_SYNCED_WRITEABLE_VISIBLE;
selectionArgs = null;
} else {
// non recurring event. enable changing calendars to all calendars.
selection = EditEventHelper.CALENDARS_WHERE_WRITEABLE_VISIBLE;
selectionArgs = null;
}

// 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,
selection,
selectionArgs,
null /* sort order */);

// TOKEN_COLORS
mHandler.startQuery(TOKEN_COLORS, null, Colors.CONTENT_URI,
Expand All @@ -752,17 +768,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 +820,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 +838,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