blob: 70c9bc07ab92dac5e51798185b7677b7fa39408a [file] [log] [blame]
Erik79f22812010-06-23 16:55:38 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Erikfbce65e2010-08-16 12:43:13 -070017package com.android.calendar.event;
Erik79f22812010-06-23 16:55:38 -070018
Erik79f22812010-06-23 16:55:38 -070019import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Fragment;
James Kung95e95382013-03-20 12:14:33 -070022import android.app.FragmentManager;
Erik79f22812010-06-23 16:55:38 -070023import android.content.AsyncQueryHandler;
Erik5f620792010-10-27 12:42:01 -070024import android.content.ContentProviderOperation;
Erik79f22812010-06-23 16:55:38 -070025import android.content.ContentResolver;
Erikdd95df52010-08-27 09:31:18 -070026import android.content.ContentUris;
Erik5f620792010-10-27 12:42:01 -070027import android.content.ContentValues;
Daisuke Miyakawa5c0dec12010-09-22 17:21:42 -070028import android.content.Context;
Erik79f22812010-06-23 16:55:38 -070029import android.content.DialogInterface;
Michael Chand17d3482011-08-18 16:11:51 -070030import android.content.DialogInterface.OnCancelListener;
Erik79f22812010-06-23 16:55:38 -070031import android.content.DialogInterface.OnClickListener;
Michael Chanc250e2e2011-05-23 12:52:50 -070032import android.content.Intent;
Erik79f22812010-06-23 16:55:38 -070033import android.database.Cursor;
34import android.database.MatrixCursor;
35import android.net.Uri;
36import android.os.Bundle;
RoboErika7c03902011-06-14 11:06:44 -070037import android.provider.CalendarContract.Attendees;
38import android.provider.CalendarContract.Calendars;
James Kung4afba182012-12-18 23:12:08 -080039import android.provider.CalendarContract.Colors;
RoboErika7c03902011-06-14 11:06:44 -070040import android.provider.CalendarContract.Events;
41import android.provider.CalendarContract.Reminders;
Erik79f22812010-06-23 16:55:38 -070042import android.text.TextUtils;
RoboErikc8e0f212011-10-13 17:57:39 -070043import android.text.format.Time;
Erik79f22812010-06-23 16:55:38 -070044import android.util.Log;
45import android.view.LayoutInflater;
Erik5f620792010-10-27 12:42:01 -070046import android.view.Menu;
47import android.view.MenuInflater;
48import android.view.MenuItem;
Erik79f22812010-06-23 16:55:38 -070049import android.view.View;
50import android.view.ViewGroup;
Daisuke Miyakawa5c0dec12010-09-22 17:21:42 -070051import android.view.inputmethod.InputMethodManager;
Andy Huang135b2d42011-09-01 20:38:43 -070052import android.widget.LinearLayout;
Erik79f22812010-06-23 16:55:38 -070053import android.widget.Toast;
54
Michael Chanc250e2e2011-05-23 12:52:50 -070055import com.android.calendar.AsyncQueryService;
56import com.android.calendar.CalendarController;
57import com.android.calendar.CalendarController.EventHandler;
58import com.android.calendar.CalendarController.EventInfo;
59import com.android.calendar.CalendarController.EventType;
60import com.android.calendar.CalendarEventModel;
61import com.android.calendar.CalendarEventModel.Attendee;
62import com.android.calendar.CalendarEventModel.ReminderEntry;
63import com.android.calendar.DeleteEventHelper;
64import com.android.calendar.R;
65import com.android.calendar.Utils;
James Kunga8b8c0d2013-02-13 17:28:16 -080066import com.android.colorpicker.ColorPickerSwatch.OnColorSelectedListener;
67import com.android.colorpicker.HsvColorComparator;
Michael Chanc250e2e2011-05-23 12:52:50 -070068
Erikc130d9d2010-12-06 15:55:23 -080069import java.io.Serializable;
Erik5f620792010-10-27 12:42:01 -070070import java.util.ArrayList;
Andy McFadden9645d9c2011-05-16 15:28:15 -070071import java.util.Collections;
Erik5f620792010-10-27 12:42:01 -070072
James Kungb8904fb2013-02-14 14:37:42 -080073public class EditEventFragment extends Fragment implements EventHandler, OnColorSelectedListener {
Erik79f22812010-06-23 16:55:38 -070074 private static final String TAG = "EditEventActivity";
James Kung2fca0242013-04-02 16:09:50 -070075 private static final String COLOR_PICKER_DIALOG_TAG = "ColorPickerDialog";
Erik79f22812010-06-23 16:55:38 -070076
James Kungb8904fb2013-02-14 14:37:42 -080077 private static final int REQUEST_CODE_COLOR_PICKER = 0;
78
Erik59ead672010-11-29 15:47:08 -080079 private static final String BUNDLE_KEY_MODEL = "key_model";
80 private static final String BUNDLE_KEY_EDIT_STATE = "key_edit_state";
Erikc130d9d2010-12-06 15:55:23 -080081 private static final String BUNDLE_KEY_EVENT = "key_event";
Erika7694ee2010-12-07 15:46:18 -080082 private static final String BUNDLE_KEY_READ_ONLY = "key_read_only";
Mindy Pereira2d63dff2011-10-04 12:51:12 -070083 private static final String BUNDLE_KEY_EDIT_ON_LAUNCH = "key_edit_on_launch";
Erik59ead672010-11-29 15:47:08 -080084
James Kung56f42bf2013-03-29 14:59:29 -070085 private static final String BUNDLE_KEY_DATE_BUTTON_CLICKED = "date_button_clicked";
86
Erik79f22812010-06-23 16:55:38 -070087 private static final boolean DEBUG = false;
88
Michael Chan352e1a22010-09-28 05:15:08 -070089 private static final int TOKEN_EVENT = 1;
90 private static final int TOKEN_ATTENDEES = 1 << 1;
91 private static final int TOKEN_REMINDERS = 1 << 2;
92 private static final int TOKEN_CALENDARS = 1 << 3;
James Kung4afba182012-12-18 23:12:08 -080093 private static final int TOKEN_COLORS = 1 << 4;
94
Michael Chan352e1a22010-09-28 05:15:08 -070095 private static final int TOKEN_ALL = TOKEN_EVENT | TOKEN_ATTENDEES | TOKEN_REMINDERS
James Kung4afba182012-12-18 23:12:08 -080096 | TOKEN_CALENDARS | TOKEN_COLORS;
Erik5f620792010-10-27 12:42:01 -070097 private static final int TOKEN_UNITIALIZED = 1 << 31;
Michael Chan352e1a22010-09-28 05:15:08 -070098
99 /**
100 * A bitfield of TOKEN_* to keep track which query hasn't been completed
101 * yet. Once all queries have returned, the model can be applied to the
102 * view.
103 */
Erik5f620792010-10-27 12:42:01 -0700104 private int mOutstandingQueries = TOKEN_UNITIALIZED;
Erik79f22812010-06-23 16:55:38 -0700105
106 EditEventHelper mHelper;
107 CalendarEventModel mModel;
108 CalendarEventModel mOriginalModel;
Erik59ead672010-11-29 15:47:08 -0800109 CalendarEventModel mRestoreModel;
Erik79f22812010-06-23 16:55:38 -0700110 EditEventView mView;
111 QueryHandler mHandler;
112
113 private AlertDialog mModifyDialog;
114 int mModification = Utils.MODIFY_UNINITIALIZED;
115
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700116 private final EventInfo mEvent;
Erikc130d9d2010-12-06 15:55:23 -0800117 private EventBundle mEventBundle;
James Kung1d1b1e42013-03-06 19:49:23 -0800118 private ArrayList<ReminderEntry> mReminders;
119 private int mEventColor;
James Kungf56b1492013-03-07 15:10:13 -0800120 private boolean mEventColorInitialized = false;
Erik79f22812010-06-23 16:55:38 -0700121 private Uri mUri;
122 private long mBegin;
123 private long mEnd;
James Kung09fbd8e2012-11-12 11:19:46 -0800124 private long mCalendarId = -1;
Erik79f22812010-06-23 16:55:38 -0700125
James Kung2fca0242013-04-02 16:09:50 -0700126 private EventColorPickerDialog mColorPickerDialog;
James Kung4afba182012-12-18 23:12:08 -0800127
Erik79f22812010-06-23 16:55:38 -0700128 private Activity mContext;
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700129 private final Done mOnDone = new Done();
Erikdd95df52010-08-27 09:31:18 -0700130
131 private boolean mSaveOnDetach = true;
Erika7694ee2010-12-07 15:46:18 -0800132 private boolean mIsReadOnly = false;
133 public boolean mShowModifyDialogOnLaunch = false;
Erik79f22812010-06-23 16:55:38 -0700134
Sam Blitzstein7674b692013-01-29 11:45:20 -0800135 private boolean mTimeSelectedWasStartTime;
James Kung56f42bf2013-03-29 14:59:29 -0700136 private boolean mDateSelectedWasStartDate;
Sam Blitzstein7674b692013-01-29 11:45:20 -0800137
Daisuke Miyakawa5c0dec12010-09-22 17:21:42 -0700138 private InputMethodManager mInputMethodManager;
139
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700140 private final Intent mIntent;
Michael Chanc250e2e2011-05-23 12:52:50 -0700141
Andy Huang135b2d42011-09-01 20:38:43 -0700142 private boolean mUseCustomActionBar;
143
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700144 private final View.OnClickListener mActionBarListener = new View.OnClickListener() {
Andy Huang135b2d42011-09-01 20:38:43 -0700145 @Override
146 public void onClick(View v) {
147 onActionBarItemSelected(v.getId());
148 }
149 };
150
Erik364309f2010-12-23 14:26:26 -0800151 // TODO turn this into a helper function in EditEventHelper for building the
152 // model
Erik79f22812010-06-23 16:55:38 -0700153 private class QueryHandler extends AsyncQueryHandler {
154 public QueryHandler(ContentResolver cr) {
155 super(cr);
156 }
157
158 @Override
159 protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
160 // If the query didn't return a cursor for some reason return
161 if (cursor == null) {
162 return;
163 }
164
165 // If the Activity is finishing, then close the cursor.
166 // Otherwise, use the new cursor in the adapter.
Michael Chan6a7878f2010-08-17 16:46:07 -0700167 final Activity activity = EditEventFragment.this.getActivity();
168 if (activity == null || activity.isFinishing()) {
Erik79f22812010-06-23 16:55:38 -0700169 cursor.close();
170 return;
171 }
172 long eventId;
173 switch (token) {
174 case TOKEN_EVENT:
175 if (cursor.getCount() == 0) {
176 // The cursor is empty. This can happen if the event
177 // was deleted.
178 cursor.close();
Michael Chan352e1a22010-09-28 05:15:08 -0700179 mOnDone.setDoneCode(Utils.DONE_EXIT);
RoboErik16bfd172011-08-01 17:30:14 -0700180 mSaveOnDetach = false;
Michael Chan352e1a22010-09-28 05:15:08 -0700181 mOnDone.run();
Erik79f22812010-06-23 16:55:38 -0700182 return;
183 }
184 mOriginalModel = new CalendarEventModel();
185 EditEventHelper.setModelFromCursor(mOriginalModel, cursor);
186 EditEventHelper.setModelFromCursor(mModel, cursor);
187 cursor.close();
188
Erik59ead672010-11-29 15:47:08 -0800189 mOriginalModel.mUri = mUri.toString();
Erik79f22812010-06-23 16:55:38 -0700190
Erik59ead672010-11-29 15:47:08 -0800191 mModel.mUri = mUri.toString();
Erik79f22812010-06-23 16:55:38 -0700192 mModel.mOriginalStart = mBegin;
193 mModel.mOriginalEnd = mEnd;
194 mModel.mIsFirstEventInSeries = mBegin == mOriginalModel.mStart;
195 mModel.mStart = mBegin;
196 mModel.mEnd = mEnd;
James Kungf56b1492013-03-07 15:10:13 -0800197 if (mEventColorInitialized) {
198 mModel.setEventColor(mEventColor);
James Kung1d1b1e42013-03-06 19:49:23 -0800199 }
Erik79f22812010-06-23 16:55:38 -0700200 eventId = mModel.mId;
Michael Chan352e1a22010-09-28 05:15:08 -0700201
202 // TOKEN_ATTENDEES
Erik79f22812010-06-23 16:55:38 -0700203 if (mModel.mHasAttendeeData && eventId != -1) {
204 Uri attUri = Attendees.CONTENT_URI;
205 String[] whereArgs = {
206 Long.toString(eventId)
207 };
208 mHandler.startQuery(TOKEN_ATTENDEES, null, attUri,
209 EditEventHelper.ATTENDEES_PROJECTION,
Erikd845fbe2010-08-12 11:20:01 -0700210 EditEventHelper.ATTENDEES_WHERE /* selection */,
Erik79f22812010-06-23 16:55:38 -0700211 whereArgs /* selection args */, null /* sort order */);
Michael Chan352e1a22010-09-28 05:15:08 -0700212 } else {
213 setModelIfDone(TOKEN_ATTENDEES);
214 }
215
216 // TOKEN_REMINDERS
James Kung1d1b1e42013-03-06 19:49:23 -0800217 if (mModel.mHasAlarm && mReminders == null) {
Erikd845fbe2010-08-12 11:20:01 -0700218 Uri rUri = Reminders.CONTENT_URI;
219 String[] remArgs = {
Andy McFadden9645d9c2011-05-16 15:28:15 -0700220 Long.toString(eventId)
Erikd845fbe2010-08-12 11:20:01 -0700221 };
Andy McFadden9645d9c2011-05-16 15:28:15 -0700222 mHandler.startQuery(TOKEN_REMINDERS, null, rUri,
223 EditEventHelper.REMINDERS_PROJECTION,
224 EditEventHelper.REMINDERS_WHERE /* selection */,
225 remArgs /* selection args */, null /* sort order */);
Erik79f22812010-06-23 16:55:38 -0700226 } else {
Sam Blitzstein523cc482013-04-02 16:13:25 -0700227 if (mReminders == null) {
228 // mReminders should not be null.
229 mReminders = new ArrayList<ReminderEntry>();
230 } else {
231 Collections.sort(mReminders);
232 }
James Kung1d1b1e42013-03-06 19:49:23 -0800233 mOriginalModel.mReminders = mReminders;
234 mModel.mReminders =
235 (ArrayList<ReminderEntry>) mReminders.clone();
Michael Chan352e1a22010-09-28 05:15:08 -0700236 setModelIfDone(TOKEN_REMINDERS);
Erik79f22812010-06-23 16:55:38 -0700237 }
Michael Chan352e1a22010-09-28 05:15:08 -0700238
239 // TOKEN_CALENDARS
240 String[] selArgs = {
241 Long.toString(mModel.mCalendarId)
242 };
243 mHandler.startQuery(TOKEN_CALENDARS, null, Calendars.CONTENT_URI,
244 EditEventHelper.CALENDARS_PROJECTION, EditEventHelper.CALENDARS_WHERE,
245 selArgs /* selection args */, null /* sort order */);
246
247 setModelIfDone(TOKEN_EVENT);
Erik79f22812010-06-23 16:55:38 -0700248 break;
249 case TOKEN_ATTENDEES:
250 try {
Erik79f22812010-06-23 16:55:38 -0700251 while (cursor.moveToNext()) {
252 String name = cursor.getString(EditEventHelper.ATTENDEES_INDEX_NAME);
253 String email = cursor.getString(EditEventHelper.ATTENDEES_INDEX_EMAIL);
Erikd845fbe2010-08-12 11:20:01 -0700254 int status = cursor.getInt(EditEventHelper.ATTENDEES_INDEX_STATUS);
Erik79f22812010-06-23 16:55:38 -0700255 int relationship = cursor
256 .getInt(EditEventHelper.ATTENDEES_INDEX_RELATIONSHIP);
Michael Chan352e1a22010-09-28 05:15:08 -0700257 if (relationship == Attendees.RELATIONSHIP_ORGANIZER) {
258 if (email != null) {
Erik79f22812010-06-23 16:55:38 -0700259 mModel.mOrganizer = email;
Michael Chan352e1a22010-09-28 05:15:08 -0700260 mModel.mIsOrganizer = mModel.mOwnerAccount
261 .equalsIgnoreCase(email);
Erika7694ee2010-12-07 15:46:18 -0800262 mOriginalModel.mOrganizer = email;
263 mOriginalModel.mIsOrganizer = mOriginalModel.mOwnerAccount
264 .equalsIgnoreCase(email);
Erik79f22812010-06-23 16:55:38 -0700265 }
Michael Chan352e1a22010-09-28 05:15:08 -0700266
267 if (TextUtils.isEmpty(name)) {
268 mModel.mOrganizerDisplayName = mModel.mOrganizer;
Erika7694ee2010-12-07 15:46:18 -0800269 mOriginalModel.mOrganizerDisplayName =
270 mOriginalModel.mOrganizer;
Michael Chan352e1a22010-09-28 05:15:08 -0700271 } else {
272 mModel.mOrganizerDisplayName = name;
Erika7694ee2010-12-07 15:46:18 -0800273 mOriginalModel.mOrganizerDisplayName = name;
Michael Chan352e1a22010-09-28 05:15:08 -0700274 }
275 }
276
277 if (email != null) {
Erikd845fbe2010-08-12 11:20:01 -0700278 if (mModel.mOwnerAccount != null &&
279 mModel.mOwnerAccount.equalsIgnoreCase(email)) {
280 int attendeeId =
281 cursor.getInt(EditEventHelper.ATTENDEES_INDEX_ID);
282 mModel.mOwnerAttendeeId = attendeeId;
283 mModel.mSelfAttendeeStatus = status;
284 mOriginalModel.mOwnerAttendeeId = attendeeId;
285 mOriginalModel.mSelfAttendeeStatus = status;
286 continue;
287 }
Erik79f22812010-06-23 16:55:38 -0700288 }
Erikd845fbe2010-08-12 11:20:01 -0700289 Attendee attendee = new Attendee(name, email);
290 attendee.mStatus = status;
291 mModel.addAttendee(attendee);
292 mOriginalModel.addAttendee(attendee);
Erik79f22812010-06-23 16:55:38 -0700293 }
294 } finally {
295 cursor.close();
296 }
Michael Chan352e1a22010-09-28 05:15:08 -0700297
298 setModelIfDone(TOKEN_ATTENDEES);
Erik79f22812010-06-23 16:55:38 -0700299 break;
300 case TOKEN_REMINDERS:
301 try {
Erikd290a012010-08-03 15:52:57 -0700302 // Add all reminders to the models
Erik79f22812010-06-23 16:55:38 -0700303 while (cursor.moveToNext()) {
304 int minutes = cursor.getInt(EditEventHelper.REMINDERS_INDEX_MINUTES);
Andy McFaddenfea9af52011-05-13 15:40:10 -0700305 int method = cursor.getInt(EditEventHelper.REMINDERS_INDEX_METHOD);
306 ReminderEntry re = ReminderEntry.valueOf(minutes, method);
307 mModel.mReminders.add(re);
308 mOriginalModel.mReminders.add(re);
Erik79f22812010-06-23 16:55:38 -0700309 }
Andy McFadden9645d9c2011-05-16 15:28:15 -0700310
311 // Sort appropriately for display
312 Collections.sort(mModel.mReminders);
313 Collections.sort(mOriginalModel.mReminders);
Erik79f22812010-06-23 16:55:38 -0700314 } finally {
315 cursor.close();
316 }
Michael Chan352e1a22010-09-28 05:15:08 -0700317
318 setModelIfDone(TOKEN_REMINDERS);
Erik79f22812010-06-23 16:55:38 -0700319 break;
320 case TOKEN_CALENDARS:
Michael Chan352e1a22010-09-28 05:15:08 -0700321 try {
James Kung09fbd8e2012-11-12 11:19:46 -0800322 if (mModel.mId == -1) {
323 // Populate Calendar spinner only if no event id is set.
Michael Chan352e1a22010-09-28 05:15:08 -0700324 MatrixCursor matrixCursor = Utils.matrixCursorFromCursor(cursor);
325 if (DEBUG) {
326 Log.d(TAG, "onQueryComplete: setting cursor with "
327 + matrixCursor.getCount() + " calendars");
328 }
James Kung09fbd8e2012-11-12 11:19:46 -0800329 mView.setCalendarsCursor(matrixCursor, isAdded() && isResumed(),
330 mCalendarId);
Michael Chan352e1a22010-09-28 05:15:08 -0700331 } else {
332 // Populate model for an existing event
333 EditEventHelper.setModelFromCalendarCursor(mModel, cursor);
Erika7694ee2010-12-07 15:46:18 -0800334 EditEventHelper.setModelFromCalendarCursor(mOriginalModel, cursor);
Michael Chan352e1a22010-09-28 05:15:08 -0700335 }
James Kung4afba182012-12-18 23:12:08 -0800336 startQuery(TOKEN_COLORS, null, Colors.CONTENT_URI,
337 EditEventHelper.COLORS_PROJECTION,
338 Colors.COLOR_TYPE + "=" + Colors.TYPE_EVENT, null, null);
Michael Chan352e1a22010-09-28 05:15:08 -0700339 } finally {
340 cursor.close();
Erik79f22812010-06-23 16:55:38 -0700341 }
Michael Chan352e1a22010-09-28 05:15:08 -0700342 setModelIfDone(TOKEN_CALENDARS);
Erik79f22812010-06-23 16:55:38 -0700343 break;
James Kung4afba182012-12-18 23:12:08 -0800344 case TOKEN_COLORS:
345 if (cursor.moveToFirst()) {
346 EventColorCache cache = new EventColorCache();
347 do
348 {
349 int colorKey = cursor.getInt(EditEventHelper.COLORS_INDEX_COLOR_KEY);
350 int rawColor = cursor.getInt(EditEventHelper.COLORS_INDEX_COLOR);
351 int displayColor = Utils.getDisplayColorFromColor(rawColor);
352 String accountName = cursor
353 .getString(EditEventHelper.COLORS_INDEX_ACCOUNT_NAME);
354 String accountType = cursor
355 .getString(EditEventHelper.COLORS_INDEX_ACCOUNT_TYPE);
356 cache.insertColor(accountName, accountType,
357 displayColor, colorKey);
358 } while (cursor.moveToNext());
James Kunge134cd52013-02-13 11:33:49 -0800359 cache.sortPalettes(new HsvColorComparator());
James Kung4afba182012-12-18 23:12:08 -0800360
361 mModel.mEventColorCache = cache;
362 mView.mColorPickerNewEvent.setOnClickListener(mOnColorPickerClicked);
363 mView.mColorPickerExistingEvent.setOnClickListener(mOnColorPickerClicked);
James Kung4afba182012-12-18 23:12:08 -0800364 }
365 if (cursor != null) {
366 cursor.close();
367 }
James Kungb8904fb2013-02-14 14:37:42 -0800368 mView.setColorPickerButtonStates(mModel.getCalendarEventColors());
James Kung4afba182012-12-18 23:12:08 -0800369 setModelIfDone(TOKEN_COLORS);
370 break;
Isaac Katzenelsonfae6d5c2011-06-16 15:43:41 -0700371 default:
372 cursor.close();
373 break;
Erik79f22812010-06-23 16:55:38 -0700374 }
375 }
376 }
377
James Kung4afba182012-12-18 23:12:08 -0800378 private View.OnClickListener mOnColorPickerClicked = new View.OnClickListener() {
379
380 @Override
381 public void onClick(View v) {
382 int[] colors = mModel.getCalendarEventColors();
James Kung2fca0242013-04-02 16:09:50 -0700383 if (mColorPickerDialog == null) {
384 mColorPickerDialog = EventColorPickerDialog.newInstance(colors,
385 mModel.getEventColor(), mModel.getCalendarColor(), mView.mIsMultipane);
386 mColorPickerDialog.setOnColorSelectedListener(EditEventFragment.this);
James Kung4afba182012-12-18 23:12:08 -0800387 } else {
James Kung2fca0242013-04-02 16:09:50 -0700388 mColorPickerDialog.setCalendarColor(mModel.getCalendarColor());
389 mColorPickerDialog.setColors(colors, mModel.getEventColor());
James Kung4afba182012-12-18 23:12:08 -0800390 }
James Kung95e95382013-03-20 12:14:33 -0700391 final FragmentManager fragmentManager = getFragmentManager();
392 fragmentManager.executePendingTransactions();
James Kung2fca0242013-04-02 16:09:50 -0700393 if (!mColorPickerDialog.isAdded()) {
394 mColorPickerDialog.show(fragmentManager, COLOR_PICKER_DIALOG_TAG);
James Kung4afba182012-12-18 23:12:08 -0800395 }
396 }
397 };
398
Daisuke Miyakawabdcb9fc2010-10-18 13:10:56 -0700399 private void setModelIfDone(int queryType) {
Michael Chan352e1a22010-09-28 05:15:08 -0700400 synchronized (this) {
401 mOutstandingQueries &= ~queryType;
402 if (mOutstandingQueries == 0) {
Erik59ead672010-11-29 15:47:08 -0800403 if (mRestoreModel != null) {
404 mModel = mRestoreModel;
405 }
Erik1bd94532010-12-08 15:01:26 -0800406 if (mShowModifyDialogOnLaunch && mModification == Utils.MODIFY_UNINITIALIZED) {
407 if (!TextUtils.isEmpty(mModel.mRrule)) {
408 displayEditWhichDialog();
409 } else {
410 mModification = Utils.MODIFY_ALL;
411 }
412
413 }
Michael Chan352e1a22010-09-28 05:15:08 -0700414 mView.setModel(mModel);
Erik3400a2b2010-12-06 17:48:44 -0800415 mView.setModification(mModification);
Michael Chan352e1a22010-09-28 05:15:08 -0700416 }
417 }
418 }
419
Erik5f620792010-10-27 12:42:01 -0700420 public EditEventFragment() {
James Kungf56b1492013-03-07 15:10:13 -0800421 this(null, null, false, -1, false, null);
Erika7694ee2010-12-07 15:46:18 -0800422 }
423
James Kungf56b1492013-03-07 15:10:13 -0800424 public EditEventFragment(EventInfo event, ArrayList<ReminderEntry> reminders,
425 boolean eventColorInitialized, int eventColor, boolean readOnly, Intent intent) {
Erikdd95df52010-08-27 09:31:18 -0700426 mEvent = event;
Erika7694ee2010-12-07 15:46:18 -0800427 mIsReadOnly = readOnly;
Michael Chanc250e2e2011-05-23 12:52:50 -0700428 mIntent = intent;
James Kung1d1b1e42013-03-06 19:49:23 -0800429
430 mReminders = reminders;
James Kungf56b1492013-03-07 15:10:13 -0800431 mEventColorInitialized = eventColorInitialized;
432 if (eventColorInitialized) {
433 mEventColor = eventColor;
434 }
Erik5f620792010-10-27 12:42:01 -0700435 setHasOptionsMenu(true);
Erik79f22812010-06-23 16:55:38 -0700436 }
437
James Kung2fca0242013-04-02 16:09:50 -0700438 @Override
439 public void onActivityCreated(Bundle savedInstanceState) {
440 super.onActivityCreated(savedInstanceState);
441 mColorPickerDialog = (EventColorPickerDialog) getActivity().getFragmentManager()
442 .findFragmentByTag(COLOR_PICKER_DIALOG_TAG);
443 if (mColorPickerDialog != null) {
444 mColorPickerDialog.setOnColorSelectedListener(this);
445 }
446 }
447
Erik79f22812010-06-23 16:55:38 -0700448 private void startQuery() {
Erikdd95df52010-08-27 09:31:18 -0700449 mUri = null;
450 mBegin = -1;
451 mEnd = -1;
452 if (mEvent != null) {
453 if (mEvent.id != -1) {
Michael Chan352e1a22010-09-28 05:15:08 -0700454 mModel.mId = mEvent.id;
Erikdd95df52010-08-27 09:31:18 -0700455 mUri = ContentUris.withAppendedId(Events.CONTENT_URI, mEvent.id);
Michael Chanedecd9a2011-08-24 23:58:48 -0700456 } else {
457 // New event. All day?
458 mModel.mAllDay = mEvent.extraLong == CalendarController.EXTRA_CREATE_ALL_DAY;
Erikdd95df52010-08-27 09:31:18 -0700459 }
460 if (mEvent.startTime != null) {
461 mBegin = mEvent.startTime.toMillis(true);
462 }
463 if (mEvent.endTime != null) {
464 mEnd = mEvent.endTime.toMillis(true);
465 }
James Kung09fbd8e2012-11-12 11:19:46 -0800466 if (mEvent.calendarId != -1) {
467 mCalendarId = mEvent.calendarId;
468 }
Erikc130d9d2010-12-06 15:55:23 -0800469 } else if (mEventBundle != null) {
Erik3400a2b2010-12-06 17:48:44 -0800470 if (mEventBundle.id != -1) {
471 mModel.mId = mEventBundle.id;
472 mUri = ContentUris.withAppendedId(Events.CONTENT_URI, mEventBundle.id);
473 }
Erikc130d9d2010-12-06 15:55:23 -0800474 mBegin = mEventBundle.start;
475 mEnd = mEventBundle.end;
Erikdd95df52010-08-27 09:31:18 -0700476 }
Mason Tange700c162010-07-09 14:06:20 -0700477
James Kung1d1b1e42013-03-06 19:49:23 -0800478 if (mReminders != null) {
479 mModel.mReminders = mReminders;
480 }
481
James Kungf56b1492013-03-07 15:10:13 -0800482 if (mEventColorInitialized) {
483 mModel.setEventColor(mEventColor);
James Kung1d1b1e42013-03-06 19:49:23 -0800484 }
485
Mason Tange700c162010-07-09 14:06:20 -0700486 if (mBegin <= 0) {
487 // use a default value instead
488 mBegin = mHelper.constructDefaultStartTime(System.currentTimeMillis());
489 }
Mason Tange700c162010-07-09 14:06:20 -0700490 if (mEnd < mBegin) {
491 // use a default value instead
492 mEnd = mHelper.constructDefaultEndTime(mBegin);
493 }
Erik79f22812010-06-23 16:55:38 -0700494
495 // Kick off the query for the event
496 boolean newEvent = mUri == null;
497 if (!newEvent) {
RoboErikef2add92011-06-09 14:49:53 -0700498 mModel.mCalendarAccessLevel = Calendars.CAL_ACCESS_NONE;
Michael Chan352e1a22010-09-28 05:15:08 -0700499 mOutstandingQueries = TOKEN_ALL;
Erik79f22812010-06-23 16:55:38 -0700500 if (DEBUG) {
Michael Chan352e1a22010-09-28 05:15:08 -0700501 Log.d(TAG, "startQuery: uri for event is " + mUri.toString());
Erik79f22812010-06-23 16:55:38 -0700502 }
503 mHandler.startQuery(TOKEN_EVENT, null, mUri, EditEventHelper.EVENT_PROJECTION,
504 null /* selection */, null /* selection args */, null /* sort order */);
505 } else {
James Kung236429f2013-04-22 12:43:09 -0700506 mOutstandingQueries = TOKEN_CALENDARS | TOKEN_COLORS;
Erik79f22812010-06-23 16:55:38 -0700507 if (DEBUG) {
Michael Chan352e1a22010-09-28 05:15:08 -0700508 Log.d(TAG, "startQuery: Editing a new event.");
Erik79f22812010-06-23 16:55:38 -0700509 }
Michael Chanf9fa0ab2012-12-14 00:41:18 -0800510 mModel.mOriginalStart = mBegin;
511 mModel.mOriginalEnd = mEnd;
Erik79f22812010-06-23 16:55:38 -0700512 mModel.mStart = mBegin;
513 mModel.mEnd = mEnd;
James Kung09fbd8e2012-11-12 11:19:46 -0800514 mModel.mCalendarId = mCalendarId;
Erikd845fbe2010-08-12 11:20:01 -0700515 mModel.mSelfAttendeeStatus = Attendees.ATTENDEE_STATUS_ACCEPTED;
Erik79f22812010-06-23 16:55:38 -0700516
517 // Start a query in the background to read the list of calendars
518 mHandler.startQuery(TOKEN_CALENDARS, null, Calendars.CONTENT_URI,
519 EditEventHelper.CALENDARS_PROJECTION,
520 EditEventHelper.CALENDARS_WHERE_WRITEABLE_VISIBLE, null /* selection args */,
521 null /* sort order */);
Erikc07a57f2010-12-01 10:45:48 -0800522
523 mModification = Utils.MODIFY_ALL;
Erikc07a57f2010-12-01 10:45:48 -0800524 mView.setModification(mModification);
Erik79f22812010-06-23 16:55:38 -0700525 }
526 }
527
528 @Override
529 public void onAttach(Activity activity) {
530 super.onAttach(activity);
531 mContext = activity;
Erik79f22812010-06-23 16:55:38 -0700532
Erikdd95df52010-08-27 09:31:18 -0700533 mHelper = new EditEventHelper(activity, null);
Erik79f22812010-06-23 16:55:38 -0700534 mHandler = new QueryHandler(activity.getContentResolver());
Michael Chanc250e2e2011-05-23 12:52:50 -0700535 mModel = new CalendarEventModel(activity, mIntent);
Daisuke Miyakawa5c0dec12010-09-22 17:21:42 -0700536 mInputMethodManager = (InputMethodManager)
537 activity.getSystemService(Context.INPUT_METHOD_SERVICE);
Andy Huang135b2d42011-09-01 20:38:43 -0700538
Isaac Katzenelson7ef29812011-10-25 18:00:50 -0700539 mUseCustomActionBar = !Utils.getConfigBool(mContext, R.bool.multiple_pane_config);
Erik79f22812010-06-23 16:55:38 -0700540 }
541
542 @Override
543 public View onCreateView(LayoutInflater inflater, ViewGroup container,
544 Bundle savedInstanceState) {
545// mContext.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
Erika7694ee2010-12-07 15:46:18 -0800546 View view;
547 if (mIsReadOnly) {
548 view = inflater.inflate(R.layout.edit_event_single_column, null);
549 } else {
550 view = inflater.inflate(R.layout.edit_event, null);
551 }
James Kung56f42bf2013-03-29 14:59:29 -0700552 mView = new EditEventView(mContext, view, mOnDone, mTimeSelectedWasStartTime,
553 mDateSelectedWasStartDate);
Erik79f22812010-06-23 16:55:38 -0700554 startQuery();
Andy Huang135b2d42011-09-01 20:38:43 -0700555
556 if (mUseCustomActionBar) {
557 View actionBarButtons = inflater.inflate(R.layout.edit_event_custom_actionbar,
558 new LinearLayout(mContext), false);
559 View cancelActionView = actionBarButtons.findViewById(R.id.action_cancel);
560 cancelActionView.setOnClickListener(mActionBarListener);
561 View doneActionView = actionBarButtons.findViewById(R.id.action_done);
562 doneActionView.setOnClickListener(mActionBarListener);
563
564 mContext.getActionBar().setCustomView(actionBarButtons);
565 }
566
Erik79f22812010-06-23 16:55:38 -0700567 return view;
568 }
569
570 @Override
Andy Huang135b2d42011-09-01 20:38:43 -0700571 public void onDestroyView() {
572 super.onDestroyView();
573
574 if (mUseCustomActionBar) {
575 mContext.getActionBar().setCustomView(null);
576 }
577 }
578
579 @Override
Erik79f22812010-06-23 16:55:38 -0700580 public void onCreate(Bundle savedInstanceState) {
581 super.onCreate(savedInstanceState);
Erik59ead672010-11-29 15:47:08 -0800582 if (savedInstanceState != null) {
583 if (savedInstanceState.containsKey(BUNDLE_KEY_MODEL)) {
584 mRestoreModel = (CalendarEventModel) savedInstanceState.getSerializable(
585 BUNDLE_KEY_MODEL);
586 }
587 if (savedInstanceState.containsKey(BUNDLE_KEY_EDIT_STATE)) {
588 mModification = savedInstanceState.getInt(BUNDLE_KEY_EDIT_STATE);
589 }
Mindy Pereira2d63dff2011-10-04 12:51:12 -0700590 if (savedInstanceState.containsKey(BUNDLE_KEY_EDIT_ON_LAUNCH)) {
591 mShowModifyDialogOnLaunch = savedInstanceState
592 .getBoolean(BUNDLE_KEY_EDIT_ON_LAUNCH);
593 }
Erikc130d9d2010-12-06 15:55:23 -0800594 if (savedInstanceState.containsKey(BUNDLE_KEY_EVENT)) {
595 mEventBundle = (EventBundle) savedInstanceState.getSerializable(BUNDLE_KEY_EVENT);
596 }
Erika7694ee2010-12-07 15:46:18 -0800597 if (savedInstanceState.containsKey(BUNDLE_KEY_READ_ONLY)) {
598 mIsReadOnly = savedInstanceState.getBoolean(BUNDLE_KEY_READ_ONLY);
599 }
Sam Blitzstein7674b692013-01-29 11:45:20 -0800600 if (savedInstanceState.containsKey("EditEventView_timebuttonclicked")) {
601 mTimeSelectedWasStartTime = savedInstanceState.getBoolean(
602 "EditEventView_timebuttonclicked");
603 }
James Kung56f42bf2013-03-29 14:59:29 -0700604 if (savedInstanceState.containsKey(BUNDLE_KEY_DATE_BUTTON_CLICKED)) {
605 mDateSelectedWasStartDate = savedInstanceState.getBoolean(
606 BUNDLE_KEY_DATE_BUTTON_CLICKED);
607 }
Sam Blitzstein7674b692013-01-29 11:45:20 -0800608
Erik59ead672010-11-29 15:47:08 -0800609 }
Erik79f22812010-06-23 16:55:38 -0700610 }
Erik5f620792010-10-27 12:42:01 -0700611
612
613 @Override
614 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
615 super.onCreateOptionsMenu(menu, inflater);
Andy Huang135b2d42011-09-01 20:38:43 -0700616
617 if (!mUseCustomActionBar) {
618 inflater.inflate(R.menu.edit_event_title_bar, menu);
Erik5f620792010-10-27 12:42:01 -0700619 }
620 }
621
622 @Override
623 public boolean onOptionsItemSelected(MenuItem item) {
Andy Huang135b2d42011-09-01 20:38:43 -0700624 return onActionBarItemSelected(item.getItemId());
625 }
626
627 /**
628 * Handles menu item selections, whether they come from our custom action bar buttons or from
629 * the standard menu items. Depends on the menu item ids matching the custom action bar button
630 * ids.
631 *
632 * @param itemId the button or menu item id
633 * @return whether the event was handled here
634 */
635 private boolean onActionBarItemSelected(int itemId) {
Michael Chanbb6d9272012-08-17 17:32:58 -0700636 if (itemId == R.id.action_done) {
637 if (EditEventHelper.canModifyEvent(mModel) || EditEventHelper.canRespond(mModel)) {
638 if (mView != null && mView.prepareForSave()) {
639 if (mModification == Utils.MODIFY_UNINITIALIZED) {
640 mModification = Utils.MODIFY_ALL;
Erik5f620792010-10-27 12:42:01 -0700641 }
Michael Chanbb6d9272012-08-17 17:32:58 -0700642 mOnDone.setDoneCode(Utils.DONE_SAVE | Utils.DONE_EXIT);
Erik5f620792010-10-27 12:42:01 -0700643 mOnDone.run();
644 } else {
645 mOnDone.setDoneCode(Utils.DONE_REVERT);
646 mOnDone.run();
647 }
Michael Chanbb6d9272012-08-17 17:32:58 -0700648 } else if (EditEventHelper.canAddReminders(mModel) && mModel.mId != -1
649 && mOriginalModel != null && mView.prepareForSave()) {
650 saveReminders();
651 mOnDone.setDoneCode(Utils.DONE_EXIT);
652 mOnDone.run();
653 } else {
Erik5f620792010-10-27 12:42:01 -0700654 mOnDone.setDoneCode(Utils.DONE_REVERT);
655 mOnDone.run();
Michael Chanbb6d9272012-08-17 17:32:58 -0700656 }
657 } else if (itemId == R.id.action_cancel) {
658 mOnDone.setDoneCode(Utils.DONE_REVERT);
659 mOnDone.run();
Erik5f620792010-10-27 12:42:01 -0700660 }
661 return true;
662 }
663
664 private void saveReminders() {
665 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(3);
Andy McFaddenfea9af52011-05-13 15:40:10 -0700666 boolean changed = EditEventHelper.saveReminders(ops, mModel.mId, mModel.mReminders,
667 mOriginalModel.mReminders, false /* no force save */);
Erik5f620792010-10-27 12:42:01 -0700668
669 if (!changed) {
670 return;
671 }
672
673 AsyncQueryService service = new AsyncQueryService(getActivity());
674 service.startBatch(0, null, Calendars.CONTENT_URI.getAuthority(), ops, 0);
675 // Update the "hasAlarm" field for the event
676 Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, mModel.mId);
Andy McFaddenfea9af52011-05-13 15:40:10 -0700677 int len = mModel.mReminders.size();
Erik5f620792010-10-27 12:42:01 -0700678 boolean hasAlarm = len > 0;
679 if (hasAlarm != mOriginalModel.mHasAlarm) {
680 ContentValues values = new ContentValues();
681 values.put(Events.HAS_ALARM, hasAlarm ? 1 : 0);
682 service.startUpdate(0, null, uri, values, null, null, 0);
683 }
684
685 Toast.makeText(mContext, R.string.saving_event, Toast.LENGTH_SHORT).show();
686 }
687
Erik46cde5c2010-12-05 13:16:16 -0800688 protected void displayEditWhichDialog() {
Mindy Pereira2d63dff2011-10-04 12:51:12 -0700689 if (mModification == Utils.MODIFY_UNINITIALIZED) {
RoboErike0dbac62011-06-29 15:09:37 -0700690 final boolean notSynced = TextUtils.isEmpty(mModel.mSyncId);
Erik79f22812010-06-23 16:55:38 -0700691 boolean isFirstEventInSeries = mModel.mIsFirstEventInSeries;
Erik79f22812010-06-23 16:55:38 -0700692 int itemIndex = 0;
693 CharSequence[] items;
Michael Chan1ec87632010-11-18 22:16:22 -0800694
695 if (notSynced) {
696 // If this event has not been synced, then don't allow deleting
697 // or changing a single instance.
Erik79f22812010-06-23 16:55:38 -0700698 if (isFirstEventInSeries) {
699 // Still display the option so the user knows all events are
700 // changing
701 items = new CharSequence[1];
702 } else {
703 items = new CharSequence[2];
704 }
705 } else {
706 if (isFirstEventInSeries) {
707 items = new CharSequence[2];
708 } else {
709 items = new CharSequence[3];
710 }
711 items[itemIndex++] = mContext.getText(R.string.modify_event);
712 }
713 items[itemIndex++] = mContext.getText(R.string.modify_all);
714
715 // Do one more check to make sure this remains at the end of the list
716 if (!isFirstEventInSeries) {
717 items[itemIndex++] = mContext.getText(R.string.modify_all_following);
718 }
719
720 // Display the modification dialog.
721 if (mModifyDialog != null) {
722 mModifyDialog.dismiss();
723 mModifyDialog = null;
724 }
RoboErik28dab652011-07-21 15:08:39 -0700725 mModifyDialog = new AlertDialog.Builder(mContext).setTitle(R.string.edit_event_label)
726 .setItems(items, new OnClickListener() {
Michael Chanf9fa0ab2012-12-14 00:41:18 -0800727 @Override
RoboErik28dab652011-07-21 15:08:39 -0700728 public void onClick(DialogInterface dialog, int which) {
729 if (which == 0) {
730 // Update this if we start allowing exceptions
731 // to unsynced events in the app
732 mModification = notSynced ? Utils.MODIFY_ALL
733 : Utils.MODIFY_SELECTED;
734 if (mModification == Utils.MODIFY_SELECTED) {
735 mModel.mOriginalSyncId = notSynced ? null : mModel.mSyncId;
736 mModel.mOriginalId = mModel.mId;
737 }
738 } else if (which == 1) {
739 mModification = notSynced ? Utils.MODIFY_ALL_FOLLOWING
740 : Utils.MODIFY_ALL;
741 } else if (which == 2) {
742 mModification = Utils.MODIFY_ALL_FOLLOWING;
743 }
Erik79f22812010-06-23 16:55:38 -0700744
RoboErik28dab652011-07-21 15:08:39 -0700745 mView.setModification(mModification);
RoboErik28dab652011-07-21 15:08:39 -0700746 }
747 }).show();
Michael Chand17d3482011-08-18 16:11:51 -0700748
749 mModifyDialog.setOnCancelListener(new OnCancelListener() {
750 @Override
751 public void onCancel(DialogInterface dialog) {
752 Activity a = EditEventFragment.this.getActivity();
753 if (a != null) {
754 a.finish();
755 }
756 }
757 });
Erik79f22812010-06-23 16:55:38 -0700758 }
759 }
760
761 class Done implements EditEventHelper.EditDoneRunnable {
762 private int mCode = -1;
763
Michael Chanf9fa0ab2012-12-14 00:41:18 -0800764 @Override
Erik79f22812010-06-23 16:55:38 -0700765 public void setDoneCode(int code) {
766 mCode = code;
767 }
768
Michael Chanf9fa0ab2012-12-14 00:41:18 -0800769 @Override
Erik79f22812010-06-23 16:55:38 -0700770 public void run() {
Erikdd95df52010-08-27 09:31:18 -0700771 // We only want this to get called once, either because the user
772 // pressed back/home or one of the buttons on screen
773 mSaveOnDetach = false;
RoboErikaca9e032011-06-10 15:16:56 -0700774 if (mModification == Utils.MODIFY_UNINITIALIZED) {
775 // If this is uninitialized the user hit back, the only
776 // changeable item is response to default to all events.
777 mModification = Utils.MODIFY_ALL;
778 }
Erik7b92da22010-09-23 14:55:22 -0700779
Erika7694ee2010-12-07 15:46:18 -0800780 if ((mCode & Utils.DONE_SAVE) != 0 && mModel != null
781 && (EditEventHelper.canRespond(mModel)
782 || EditEventHelper.canModifyEvent(mModel))
RoboErik6a7003d2011-08-04 16:23:20 -0700783 && mView.prepareForSave()
Michael Chand0fb2dd2011-01-07 14:37:18 -0800784 && !isEmptyNewEvent()
Andy McFaddenae5bcce2011-04-14 09:28:08 -0700785 && mModel.normalizeReminders()
Michael Chan304bf0d2010-10-13 14:04:56 -0700786 && mHelper.saveEvent(mModel, mOriginalModel, mModification)) {
787 int stringResource;
Michael Chanea8ebdb2011-02-28 19:04:00 -0800788 if (!mModel.mAttendeesList.isEmpty()) {
789 if (mModel.mUri != null) {
790 stringResource = R.string.saving_event_with_guest;
791 } else {
792 stringResource = R.string.creating_event_with_guest;
793 }
Michael Chan304bf0d2010-10-13 14:04:56 -0700794 } else {
Michael Chanea8ebdb2011-02-28 19:04:00 -0800795 if (mModel.mUri != null) {
796 stringResource = R.string.saving_event;
797 } else {
798 stringResource = R.string.creating_event;
799 }
Erik7b92da22010-09-23 14:55:22 -0700800 }
Michael Chan304bf0d2010-10-13 14:04:56 -0700801 Toast.makeText(mContext, stringResource, Toast.LENGTH_SHORT).show();
RoboErik2bd5cc02011-07-28 14:19:54 -0700802 } else if ((mCode & Utils.DONE_SAVE) != 0 && mModel != null && isEmptyNewEvent()) {
803 Toast.makeText(mContext, R.string.empty_event, Toast.LENGTH_SHORT).show();
Erik79f22812010-06-23 16:55:38 -0700804 }
Erik7b92da22010-09-23 14:55:22 -0700805
RoboErik2fda2452011-02-15 17:47:45 -0800806 if ((mCode & Utils.DONE_DELETE) != 0 && mOriginalModel != null
807 && EditEventHelper.canModifyCalendar(mOriginalModel)) {
Erik7b92da22010-09-23 14:55:22 -0700808 long begin = mModel.mStart;
809 long end = mModel.mEnd;
810 int which = -1;
811 switch (mModification) {
812 case Utils.MODIFY_SELECTED:
813 which = DeleteEventHelper.DELETE_SELECTED;
814 break;
815 case Utils.MODIFY_ALL_FOLLOWING:
816 which = DeleteEventHelper.DELETE_ALL_FOLLOWING;
817 break;
818 case Utils.MODIFY_ALL:
819 which = DeleteEventHelper.DELETE_ALL;
820 break;
821 }
Erika7694ee2010-12-07 15:46:18 -0800822 DeleteEventHelper deleteHelper = new DeleteEventHelper(
823 mContext, mContext, !mIsReadOnly /* exitWhenDone */);
Erik6663f362011-01-18 10:05:55 -0800824 deleteHelper.delete(begin, end, mOriginalModel, which);
Erik7b92da22010-09-23 14:55:22 -0700825 }
826
827 if ((mCode & Utils.DONE_EXIT) != 0) {
828 // This will exit the edit event screen, should be called
829 // when we want to return to the main calendar views
Isaac Katzenelson4024dee2011-09-29 14:40:46 -0700830 if ((mCode & Utils.DONE_SAVE) != 0) {
831 if (mContext != null) {
RoboErikc8e0f212011-10-13 17:57:39 -0700832 long start = mModel.mStart;
833 long end = mModel.mEnd;
834 if (mModel.mAllDay) {
835 // For allday events we want to go to the day in the
836 // user's current tz
837 String tz = Utils.getTimeZone(mContext, null);
838 Time t = new Time(Time.TIMEZONE_UTC);
839 t.set(start);
840 t.timezone = tz;
841 start = t.toMillis(true);
842
843 t.timezone = Time.TIMEZONE_UTC;
844 t.set(end);
845 t.timezone = tz;
846 end = t.toMillis(true);
847 }
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700848 CalendarController.getInstance(mContext).launchViewEvent(-1, start, end,
849 Attendees.ATTENDEE_STATUS_NONE);
Isaac Katzenelson4024dee2011-09-29 14:40:46 -0700850 }
851 }
Michael Chan0d4a3042011-09-18 18:02:38 -0700852 Activity a = EditEventFragment.this.getActivity();
853 if (a != null) {
854 a.finish();
855 }
Erik7b92da22010-09-23 14:55:22 -0700856 }
857
Daisuke Miyakawa5c0dec12010-09-22 17:21:42 -0700858 // Hide a software keyboard so that user won't see it even after this Fragment's
859 // disappearing.
860 final View focusedView = mContext.getCurrentFocus();
861 if (focusedView != null) {
862 mInputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
863 focusedView.clearFocus();
864 }
Erik79f22812010-06-23 16:55:38 -0700865 }
866 }
867
Michael Chand0fb2dd2011-01-07 14:37:18 -0800868 boolean isEmptyNewEvent() {
869 if (mOriginalModel != null) {
870 // Not new
871 return false;
872 }
873
Michael Chanf9fa0ab2012-12-14 00:41:18 -0800874 if (mModel.mOriginalStart != mModel.mStart || mModel.mOriginalEnd != mModel.mEnd) {
875 return false;
Michael Chand0fb2dd2011-01-07 14:37:18 -0800876 }
877
Michael Chanf9fa0ab2012-12-14 00:41:18 -0800878 if (!mModel.mAttendeesList.isEmpty()) {
879 return false;
Michael Chand0fb2dd2011-01-07 14:37:18 -0800880 }
881
Michael Chanf9fa0ab2012-12-14 00:41:18 -0800882 return mModel.isEmpty();
Michael Chand0fb2dd2011-01-07 14:37:18 -0800883 }
884
Erik79f22812010-06-23 16:55:38 -0700885 @Override
Erika7694ee2010-12-07 15:46:18 -0800886 public void onPause() {
887 Activity act = getActivity();
Erik55d4c8c2010-12-22 13:59:57 -0800888 if (mSaveOnDetach && act != null && !mIsReadOnly && !act.isChangingConfigurations()
889 && mView.prepareForSave()) {
Erika7694ee2010-12-07 15:46:18 -0800890 mOnDone.setDoneCode(Utils.DONE_SAVE);
891 mOnDone.run();
892 }
893 super.onPause();
894 }
895
896 @Override
Erik79f22812010-06-23 16:55:38 -0700897 public void onDestroy() {
Dmitri Plotnikovee212202010-08-27 17:58:19 -0700898 if (mView != null) {
899 mView.setModel(null);
900 }
Erik79f22812010-06-23 16:55:38 -0700901 if (mModifyDialog != null) {
902 mModifyDialog.dismiss();
903 mModifyDialog = null;
904 }
Erik7b92da22010-09-23 14:55:22 -0700905 super.onDestroy();
Erik79f22812010-06-23 16:55:38 -0700906 }
Erikdd95df52010-08-27 09:31:18 -0700907
908 @Override
909 public void eventsChanged() {
910 // TODO Requery to see if event has changed
911 }
912
913 @Override
Erik59ead672010-11-29 15:47:08 -0800914 public void onSaveInstanceState(Bundle outState) {
915 mView.prepareForSave();
916 outState.putSerializable(BUNDLE_KEY_MODEL, mModel);
917 outState.putInt(BUNDLE_KEY_EDIT_STATE, mModification);
Erikc130d9d2010-12-06 15:55:23 -0800918 if (mEventBundle == null && mEvent != null) {
919 mEventBundle = new EventBundle();
920 mEventBundle.id = mEvent.id;
921 if (mEvent.startTime != null) {
922 mEventBundle.start = mEvent.startTime.toMillis(true);
923 }
924 if (mEvent.endTime != null) {
925 mEventBundle.end = mEvent.startTime.toMillis(true);
926 }
927 }
Mindy Pereira2d63dff2011-10-04 12:51:12 -0700928 outState.putBoolean(BUNDLE_KEY_EDIT_ON_LAUNCH, mShowModifyDialogOnLaunch);
Erikc130d9d2010-12-06 15:55:23 -0800929 outState.putSerializable(BUNDLE_KEY_EVENT, mEventBundle);
Erika7694ee2010-12-07 15:46:18 -0800930 outState.putBoolean(BUNDLE_KEY_READ_ONLY, mIsReadOnly);
Sam Blitzstein7674b692013-01-29 11:45:20 -0800931
932 outState.putBoolean("EditEventView_timebuttonclicked", mView.mTimeSelectedWasStartTime);
James Kung56f42bf2013-03-29 14:59:29 -0700933 outState.putBoolean(BUNDLE_KEY_DATE_BUTTON_CLICKED, mView.mDateSelectedWasStartDate);
Erik59ead672010-11-29 15:47:08 -0800934 }
935
936 @Override
Erikdd95df52010-08-27 09:31:18 -0700937 public long getSupportedEventTypes() {
Erik7b92da22010-09-23 14:55:22 -0700938 return EventType.USER_HOME;
Erikdd95df52010-08-27 09:31:18 -0700939 }
940
941 @Override
Erikdd95df52010-08-27 09:31:18 -0700942 public void handleEvent(EventInfo event) {
Erik7b92da22010-09-23 14:55:22 -0700943 // It's currently unclear if we want to save the event or not when home
944 // is pressed. When creating a new event we shouldn't save since we
945 // can't get the id of the new event easily.
946 if ((false && event.eventType == EventType.USER_HOME) || (event.eventType == EventType.GO_TO
947 && mSaveOnDetach)) {
948 if (mView != null && mView.prepareForSave()) {
949 mOnDone.setDoneCode(Utils.DONE_SAVE);
950 mOnDone.run();
951 }
952 }
Erikdd95df52010-08-27 09:31:18 -0700953 }
Erikc130d9d2010-12-06 15:55:23 -0800954
Erika7694ee2010-12-07 15:46:18 -0800955 private static class EventBundle implements Serializable {
Michael Chan17c0cea2011-08-19 18:00:29 -0700956 private static final long serialVersionUID = 1L;
Erikc130d9d2010-12-06 15:55:23 -0800957 long id = -1;
958 long start = -1;
959 long end = -1;
960 }
James Kungb8904fb2013-02-14 14:37:42 -0800961
962 @Override
963 public void onColorSelected(int color) {
James Kungf56b1492013-03-07 15:10:13 -0800964 if (!mModel.isEventColorInitialized() || mModel.getEventColor() != color) {
965 mModel.setEventColor(color);
James Kungb8904fb2013-02-14 14:37:42 -0800966 mView.updateHeadlineColor(mModel, color);
967 }
968 }
Erik79f22812010-06-23 16:55:38 -0700969}