diff --git a/app/src/main/java/com/android/calendar/DeleteEventHelper.java b/app/src/main/java/com/android/calendar/DeleteEventHelper.java index ac4495b392dc1b2c318c8d93c3e0677111dafe6d..f129c01ac76c5b585684c475fad92838910bd642 100644 --- a/app/src/main/java/com/android/calendar/DeleteEventHelper.java +++ b/app/src/main/java/com/android/calendar/DeleteEventHelper.java @@ -32,6 +32,8 @@ import android.text.TextUtils; import android.widget.ArrayAdapter; import android.widget.Button; +import androidx.annotation.NonNull; + import com.android.calendar.event.EditEventHelper; import com.android.calendar.persistence.CalendarRepository; import com.android.calendarcommon2.EventRecurrence; @@ -120,6 +122,7 @@ public class DeleteEventHelper { } } }; + /** * This callback is used when an exception to an event is deleted */ @@ -136,6 +139,7 @@ public class DeleteEventHelper { } } }; + /** * This callback is used when a list item for a repeating event is selected */ @@ -163,7 +167,6 @@ public class DeleteEventHelper { } } }; - public DeleteEventHelper(Context context, Activity parentActivity, boolean exitWhenDone) { if (exitWhenDone && parentActivity == null) { throw new IllegalArgumentException("parentActivity is required to exit when done"); @@ -339,6 +342,20 @@ public class DeleteEventHelper { } } + public void delete(@NonNull CalendarEventModel model) { + deleteStarted(); + long id = model.mId; + + boolean isLocal = model.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(); + } + } + private void deleteExceptionEvent() { long id = mModel.mId; // mCursor.getInt(mEventIndexId); diff --git a/app/src/main/java/com/android/calendar/event/CreateEventDialogFragment.java b/app/src/main/java/com/android/calendar/event/CreateEventDialogFragment.java index 430326201861e205f9493b8abdb2ecbd391b4643..7a51652aec4b154c1b07316fed6bc4a3abbf65fe 100644 --- a/app/src/main/java/com/android/calendar/event/CreateEventDialogFragment.java +++ b/app/src/main/java/com/android/calendar/event/CreateEventDialogFragment.java @@ -205,7 +205,7 @@ public class CreateEventDialogFragment extends DialogFragment implements TextWat mModel.mCalendarId = mCalendarId; mModel.mOwnerAccount = mCalendarOwner; - if (mEditEventHelper.saveEvent(mModel, null, 0)) { + if (mEditEventHelper.saveEvent(mModel, null, 0, null)) { Toast.makeText(getActivity(), R.string.creating_event, Toast.LENGTH_SHORT).show(); } } diff --git a/app/src/main/java/com/android/calendar/event/EditEventFragment.java b/app/src/main/java/com/android/calendar/event/EditEventFragment.java index 04f1091bb74698e7e278ddf5f2f6659a0f410dee..8f00a76e0bbe445fe67145567734f347849ebc48 100644 --- a/app/src/main/java/com/android/calendar/event/EditEventFragment.java +++ b/app/src/main/java/com/android/calendar/event/EditEventFragment.java @@ -753,12 +753,10 @@ public class EditEventFragment extends Fragment implements EventHandler, OnColor } // 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, @@ -770,7 +768,7 @@ public class EditEventFragment extends Fragment implements EventHandler, OnColor mModel.mCalendarAccountName, mModel.mCalendarAccountType ); - selArgs = new String[]{ + String[] selArgs = new String[]{ Long.toString(eventId) }; mHandler.startQuery(TOKEN_EXTENDED, null, extendedPropUri, @@ -852,16 +850,17 @@ public class EditEventFragment extends Fragment implements EventHandler, OnColor 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 { + MatrixCursor matrixCursor = Utils.matrixCursorFromCursor(cursor); + if (DEBUG) { + Log.d(TAG, "onQueryComplete: setting cursor with " + + matrixCursor.getCount() + " calendars"); + } + + long selectedCalendarId = mModel.mCalendarId == -1 ? mCalendarId : mModel.mCalendarId; + mView.setCalendarsCursor(matrixCursor, isAdded() && isResumed(), + selectedCalendarId); + + if (mModel.mId != -1) { // Populate model for an existing event EditEventHelper.setModelFromCalendarCursor(mModel, cursor, activity); EditEventHelper.setModelFromCalendarCursor(mOriginalModel, cursor, activity); @@ -960,7 +959,7 @@ public class EditEventFragment extends Fragment implements EventHandler, OnColor && mView.prepareForSave() && !isEmptyNewEvent() && mModel.normalizeReminders() - && mHelper.saveEvent(mModel, mOriginalModel, mModification)) { + && mHelper.saveEvent(mModel, mOriginalModel, mModification, requireActivity())) { int stringResource; if (!mModel.mAttendeesList.isEmpty()) { if (mModel.mUri != null) { diff --git a/app/src/main/java/com/android/calendar/event/EditEventHelper.java b/app/src/main/java/com/android/calendar/event/EditEventHelper.java index 39fdf576a173dbc61180ba9c6fbd1ecf580ffaec..2b68bfdff07c16041289963219a5d7c27dcc7885 100644 --- a/app/src/main/java/com/android/calendar/event/EditEventHelper.java +++ b/app/src/main/java/com/android/calendar/event/EditEventHelper.java @@ -16,6 +16,7 @@ package com.android.calendar.event; +import android.app.Activity; import android.content.ContentProviderOperation; import android.content.ContentUris; import android.content.ContentValues; @@ -23,7 +24,6 @@ import android.content.Context; import android.database.Cursor; import android.graphics.drawable.Drawable; import android.net.Uri; -import android.provider.CalendarContract; import android.provider.CalendarContract.Attendees; import android.provider.CalendarContract.Calendars; import android.provider.CalendarContract.Colors; @@ -281,10 +281,13 @@ public class EditEventHelper { * @param model The event model to save * @param originalModel A model of the original event if it exists * @param modifyWhich For recurring events which type of series modification to use + * @param activity is used to handle moving any event to a different calendar * @return true if the event was successfully queued for saving */ - public boolean saveEvent(CalendarEventModel model, CalendarEventModel originalModel, - int modifyWhich) { + public boolean saveEvent(CalendarEventModel model, + CalendarEventModel originalModel, + int modifyWhich, + Activity activity) { boolean forceSaveReminders = false; if (DEBUG) { @@ -317,6 +320,8 @@ public class EditEventHelper { return false; } + MoveEventHelper.handleMoveEvent(originalModel, model, activity); + ArrayList ops = new ArrayList(); int eventIdIndex = -1; @@ -841,9 +846,6 @@ public class EditEventHelper { return true; } - if (model.mCalendarId != originalModel.mCalendarId) { - return false; - } if (model.mId != originalModel.mId) { return false; } diff --git a/app/src/main/java/com/android/calendar/event/EditEventView.java b/app/src/main/java/com/android/calendar/event/EditEventView.java index 779ab6f4304a08e406e22efbd134d3dfeb24739d..c001c8b9685c23ee467b93490087eeb0c4f2640f 100644 --- a/app/src/main/java/com/android/calendar/event/EditEventView.java +++ b/app/src/main/java/com/android/calendar/event/EditEventView.java @@ -16,7 +16,6 @@ package com.android.calendar.event; -import android.app.Activity; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.ProgressDialog; @@ -640,9 +639,11 @@ public class EditEventView implements View.OnClickListener, DialogInterface.OnCa mEmailValidator.setRemoveInvalid(false); } - // If this was a new event we need to fill in the Calendar information - if (mModel.mUri == null) { - mModel.mCalendarId = mCalendarsSpinner.getSelectedItemId(); + long oldCalendarId = mModel.mCalendarId; + long selectedCalendar = mCalendarsSpinner.getSelectedItemId(); + // If calendar is changed, we need to fill in the Calendar information + if (oldCalendarId != selectedCalendar) { + mModel.mCalendarId = selectedCalendar; int calendarCursorPosition = mCalendarsSpinner.getSelectedItemPosition(); if (mCalendarsCursor.moveToPosition(calendarCursorPosition)) { String calendarOwner = mCalendarsCursor.getString( @@ -1000,21 +1001,6 @@ public class EditEventView implements View.OnClickListener, DialogInterface.OnCa mResponseGroup.setVisibility(View.GONE); } - if (model.mUri != null) { - // This is an existing event so hide the calendar spinner - // since we can't change the calendar. - View calendarGroup = mView.findViewById(R.id.calendar_selector_group); - calendarGroup.setVisibility(View.VISIBLE); - TextView tv = (TextView) mView.findViewById(R.id.calendar_textview); - tv.setText(model.mCalendarDisplayName); - tv = (TextView) mView.findViewById(R.id.calendar_textview_secondary); - if (tv != null) { - tv.setText(model.mOwnerAccount); - } - } else { - View calendarGroup = mView.findViewById(R.id.calendar_group); - calendarGroup.setVisibility(View.GONE); - } if (model.isEventColorInitialized()) { updateHeadlineColor(model.getEventColor()); } @@ -1208,8 +1194,8 @@ public class EditEventView implements View.OnClickListener, DialogInterface.OnCa v.setEnabled(false); v.setBackgroundDrawable(null); } - mCalendarSelectorGroup.setVisibility(View.GONE); - mCalendarStaticGroup.setVisibility(View.VISIBLE); + mCalendarSelectorGroup.setVisibility(View.VISIBLE); + mCalendarStaticGroup.setVisibility(View.GONE); mRruleButton.setEnabled(false); if (EditEventHelper.canAddReminders(mModel)) { mRemindersGroup.setVisibility(View.VISIBLE); @@ -1240,13 +1226,10 @@ public class EditEventView implements View.OnClickListener, DialogInterface.OnCa mOriginalPadding[3]); } } - if (mModel.mUri == null) { - mCalendarSelectorGroup.setVisibility(View.VISIBLE); - mCalendarStaticGroup.setVisibility(View.GONE); - } else { - mCalendarSelectorGroup.setVisibility(View.GONE); - mCalendarStaticGroup.setVisibility(View.VISIBLE); - } + + mCalendarSelectorGroup.setVisibility(View.VISIBLE); + mCalendarStaticGroup.setVisibility(View.GONE); + if (mModel.mOriginalSyncId == null) { mRruleButton.setEnabled(true); } else { diff --git a/app/src/main/java/com/android/calendar/event/MoveEventHelper.java b/app/src/main/java/com/android/calendar/event/MoveEventHelper.java new file mode 100644 index 0000000000000000000000000000000000000000..4f852f831174abfc10867bd4545cbeb109e03526 --- /dev/null +++ b/app/src/main/java/com/android/calendar/event/MoveEventHelper.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2024 MURENA SAS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package com.android.calendar.event; + +import android.app.Activity; +import android.util.Log; + +import com.android.calendar.CalendarEventModel; +import com.android.calendar.DeleteEventHelper; + +public class MoveEventHelper { + private static final String TAG = MoveEventHelper.class.getName(); + + public static void handleMoveEvent(CalendarEventModel originalModel, CalendarEventModel model, Activity activity) { + if (model.mUri != null && originalModel.mCalendarId != model.mCalendarId) { + DeleteEventHelper deleteHelper = + new DeleteEventHelper(activity, activity, true /* exitWhenDone */); + deleteHelper.setDeleteNotificationListener(null); + deleteHelper.delete(model); + Log.d(TAG, "Event deleted"); + + resetCurrentObject(model); + } + } + + private static void resetCurrentObject(CalendarEventModel model) { + model.mUri = null; + model.mId = -1; + model.mUrl = null; + model.mSyncId = null; + model.mOriginalSyncId = null; + } +}