EditEventHelper.java revision c077ec06a91a2e5d82557de3c6edd83166ca9174
173714169d34a55fc01e14b30897a1ebbacf9934eErik/* 273714169d34a55fc01e14b30897a1ebbacf9934eErik * Copyright (C) 2010 The Android Open Source Project 373714169d34a55fc01e14b30897a1ebbacf9934eErik * 473714169d34a55fc01e14b30897a1ebbacf9934eErik * Licensed under the Apache License, Version 2.0 (the "License"); 573714169d34a55fc01e14b30897a1ebbacf9934eErik * you may not use this file except in compliance with the License. 673714169d34a55fc01e14b30897a1ebbacf9934eErik * You may obtain a copy of the License at 773714169d34a55fc01e14b30897a1ebbacf9934eErik * 873714169d34a55fc01e14b30897a1ebbacf9934eErik * http://www.apache.org/licenses/LICENSE-2.0 973714169d34a55fc01e14b30897a1ebbacf9934eErik * 1073714169d34a55fc01e14b30897a1ebbacf9934eErik * Unless required by applicable law or agreed to in writing, software 1173714169d34a55fc01e14b30897a1ebbacf9934eErik * distributed under the License is distributed on an "AS IS" BASIS, 1273714169d34a55fc01e14b30897a1ebbacf9934eErik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1373714169d34a55fc01e14b30897a1ebbacf9934eErik * See the License for the specific language governing permissions and 1473714169d34a55fc01e14b30897a1ebbacf9934eErik * limitations under the License. 1573714169d34a55fc01e14b30897a1ebbacf9934eErik */ 1673714169d34a55fc01e14b30897a1ebbacf9934eErik 17fbce65e53c7a111955f638db5bf8bee35381e5b7Erikpackage com.android.calendar.event; 1873714169d34a55fc01e14b30897a1ebbacf9934eErik 19eca82e9468c002590045338f98588ad3ce1a0bb3Erikimport android.content.ContentProviderOperation; 20eca82e9468c002590045338f98588ad3ce1a0bb3Erikimport android.content.ContentUris; 21eca82e9468c002590045338f98588ad3ce1a0bb3Erikimport android.content.ContentValues; 22dd95df57c8c5a58a85c4c0effad5652dec14f621Erikimport android.content.Context; 2373714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.database.Cursor; 24d845fbe558d5229102b58cce70a1b29fe6cb6967Erikimport android.graphics.drawable.Drawable; 2573714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.net.Uri; 26a7c0390d9c5dd4ff730de505682687fae5f5ced0RoboErikimport android.provider.CalendarContract.Attendees; 27a7c0390d9c5dd4ff730de505682687fae5f5ced0RoboErikimport android.provider.CalendarContract.Calendars; 28a7c0390d9c5dd4ff730de505682687fae5f5ced0RoboErikimport android.provider.CalendarContract.Events; 29a7c0390d9c5dd4ff730de505682687fae5f5ced0RoboErikimport android.provider.CalendarContract.Reminders; 3073714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.text.TextUtils; 3173714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.text.format.DateUtils; 3273714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.text.format.Time; 3373714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.text.util.Rfc822Token; 3473714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.text.util.Rfc822Tokenizer; 3573714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.util.Log; 36352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chanimport android.view.View; 379e196e46b238e6bca1ef86c7d673807b78ab7016Michael Chan 389e196e46b238e6bca1ef86c7d673807b78ab7016Michael Chanimport com.android.calendar.AsyncQueryService; 399e196e46b238e6bca1ef86c7d673807b78ab7016Michael Chanimport com.android.calendar.CalendarEventModel; 409e196e46b238e6bca1ef86c7d673807b78ab7016Michael Chanimport com.android.calendar.CalendarEventModel.Attendee; 419e196e46b238e6bca1ef86c7d673807b78ab7016Michael Chanimport com.android.calendar.CalendarEventModel.ReminderEntry; 429e196e46b238e6bca1ef86c7d673807b78ab7016Michael Chanimport com.android.calendar.Utils; 439e196e46b238e6bca1ef86c7d673807b78ab7016Michael Chanimport com.android.calendarcommon.DateException; 449e196e46b238e6bca1ef86c7d673807b78ab7016Michael Chanimport com.android.calendarcommon.EventRecurrence; 459e196e46b238e6bca1ef86c7d673807b78ab7016Michael Chanimport com.android.calendarcommon.RecurrenceProcessor; 469e196e46b238e6bca1ef86c7d673807b78ab7016Michael Chanimport com.android.calendarcommon.RecurrenceSet; 479e196e46b238e6bca1ef86c7d673807b78ab7016Michael Chanimport com.android.common.Rfc822Validator; 4873714169d34a55fc01e14b30897a1ebbacf9934eErik 49eca82e9468c002590045338f98588ad3ce1a0bb3Erikimport java.util.ArrayList; 50d845fbe558d5229102b58cce70a1b29fe6cb6967Erikimport java.util.HashMap; 51eca82e9468c002590045338f98588ad3ce1a0bb3Erikimport java.util.Iterator; 52eca82e9468c002590045338f98588ad3ce1a0bb3Erikimport java.util.LinkedHashSet; 53d845fbe558d5229102b58cce70a1b29fe6cb6967Erikimport java.util.LinkedList; 54eca82e9468c002590045338f98588ad3ce1a0bb3Erikimport java.util.TimeZone; 5573714169d34a55fc01e14b30897a1ebbacf9934eErik 5673714169d34a55fc01e14b30897a1ebbacf9934eErikpublic class EditEventHelper { 5773714169d34a55fc01e14b30897a1ebbacf9934eErik private static final String TAG = "EditEventHelper"; 58eca82e9468c002590045338f98588ad3ce1a0bb3Erik 5973714169d34a55fc01e14b30897a1ebbacf9934eErik private static final boolean DEBUG = false; 6073714169d34a55fc01e14b30897a1ebbacf9934eErik 61fbce65e53c7a111955f638db5bf8bee35381e5b7Erik public static final String[] EVENT_PROJECTION = new String[] { 6273714169d34a55fc01e14b30897a1ebbacf9934eErik Events._ID, // 0 6373714169d34a55fc01e14b30897a1ebbacf9934eErik Events.TITLE, // 1 6473714169d34a55fc01e14b30897a1ebbacf9934eErik Events.DESCRIPTION, // 2 6573714169d34a55fc01e14b30897a1ebbacf9934eErik Events.EVENT_LOCATION, // 3 6673714169d34a55fc01e14b30897a1ebbacf9934eErik Events.ALL_DAY, // 4 6773714169d34a55fc01e14b30897a1ebbacf9934eErik Events.HAS_ALARM, // 5 6873714169d34a55fc01e14b30897a1ebbacf9934eErik Events.CALENDAR_ID, // 6 6973714169d34a55fc01e14b30897a1ebbacf9934eErik Events.DTSTART, // 7 7073714169d34a55fc01e14b30897a1ebbacf9934eErik Events.DTEND, // 8 7173714169d34a55fc01e14b30897a1ebbacf9934eErik Events.DURATION, // 9 7273714169d34a55fc01e14b30897a1ebbacf9934eErik Events.EVENT_TIMEZONE, // 10 7373714169d34a55fc01e14b30897a1ebbacf9934eErik Events.RRULE, // 11 7473714169d34a55fc01e14b30897a1ebbacf9934eErik Events._SYNC_ID, // 12 750e1e62408b96e1532eb6f6a609ae4c817751aaf3RoboErik Events.AVAILABILITY, // 13 760e1e62408b96e1532eb6f6a609ae4c817751aaf3RoboErik Events.ACCESS_LEVEL, // 14 7773714169d34a55fc01e14b30897a1ebbacf9934eErik Events.OWNER_ACCOUNT, // 15 7873714169d34a55fc01e14b30897a1ebbacf9934eErik Events.HAS_ATTENDEE_DATA, // 16 790e1e62408b96e1532eb6f6a609ae4c817751aaf3RoboErik Events.ORIGINAL_SYNC_ID, // 17 80352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan Events.ORGANIZER, // 18 81352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan Events.GUESTS_CAN_MODIFY, // 19 8228dab653f55caccbed32f700274f5274abaee089RoboErik Events.ORIGINAL_ID, // 20 83e3f85db16cfc50b25b97c64d8ab99820d579e5deMichael Chan Events.STATUS, // 21 8473714169d34a55fc01e14b30897a1ebbacf9934eErik }; 8573714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_ID = 0; 8673714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_TITLE = 1; 8773714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_DESCRIPTION = 2; 8873714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_EVENT_LOCATION = 3; 8973714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_ALL_DAY = 4; 9073714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_HAS_ALARM = 5; 9173714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_CALENDAR_ID = 6; 9273714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_DTSTART = 7; 9373714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_DTEND = 8; 9473714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_DURATION = 9; 9573714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_TIMEZONE = 10; 9673714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_RRULE = 11; 9773714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_SYNC_ID = 12; 98ced0eb67ef49c3203f72520c296e73d9cb4f654bMichael Chan protected static final int EVENT_INDEX_AVAILABILITY = 13; 99ced0eb67ef49c3203f72520c296e73d9cb4f654bMichael Chan protected static final int EVENT_INDEX_ACCESS_LEVEL = 14; 10073714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_OWNER_ACCOUNT = 15; 10173714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_HAS_ATTENDEE_DATA = 16; 10228dab653f55caccbed32f700274f5274abaee089RoboErik protected static final int EVENT_INDEX_ORIGINAL_SYNC_ID = 17; 103352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan protected static final int EVENT_INDEX_ORGANIZER = 18; 104352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan protected static final int EVENT_INDEX_GUESTS_CAN_MODIFY = 19; 10528dab653f55caccbed32f700274f5274abaee089RoboErik protected static final int EVENT_INDEX_ORIGINAL_ID = 20; 106e3f85db16cfc50b25b97c64d8ab99820d579e5deMichael Chan protected static final int EVENT_INDEX_EVENT_STATUS = 21; 10773714169d34a55fc01e14b30897a1ebbacf9934eErik 108eca82e9468c002590045338f98588ad3ce1a0bb3Erik public static final String[] REMINDERS_PROJECTION = new String[] { 10973714169d34a55fc01e14b30897a1ebbacf9934eErik Reminders._ID, // 0 11073714169d34a55fc01e14b30897a1ebbacf9934eErik Reminders.MINUTES, // 1 111eca82e9468c002590045338f98588ad3ce1a0bb3Erik Reminders.METHOD, // 2 11273714169d34a55fc01e14b30897a1ebbacf9934eErik }; 113eca82e9468c002590045338f98588ad3ce1a0bb3Erik public static final int REMINDERS_INDEX_MINUTES = 1; 114eca82e9468c002590045338f98588ad3ce1a0bb3Erik public static final int REMINDERS_INDEX_METHOD = 2; 1159645d9c3e9b2cfd35c5fc89f41caf6e99153ba10Andy McFadden public static final String REMINDERS_WHERE = Reminders.EVENT_ID + "=?"; 116eca82e9468c002590045338f98588ad3ce1a0bb3Erik 11773714169d34a55fc01e14b30897a1ebbacf9934eErik // Visible for testing 118eca82e9468c002590045338f98588ad3ce1a0bb3Erik static final String ATTENDEES_DELETE_PREFIX = Attendees.EVENT_ID + "=? AND " 119eca82e9468c002590045338f98588ad3ce1a0bb3Erik + Attendees.ATTENDEE_EMAIL + " IN ("; 12073714169d34a55fc01e14b30897a1ebbacf9934eErik 12173714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int DOES_NOT_REPEAT = 0; 12273714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_DAILY = 1; 12373714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_EVERY_WEEKDAY = 2; 12473714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_WEEKLY_ON_DAY = 3; 12573714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_MONTHLY_ON_DAY_COUNT = 4; 12673714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_MONTHLY_ON_DAY = 5; 12773714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_YEARLY = 6; 12873714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_CUSTOM = 7; 12973714169d34a55fc01e14b30897a1ebbacf9934eErik 13073714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int MODIFY_UNINITIALIZED = 0; 13173714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int MODIFY_SELECTED = 1; 1328d69cd014d912e38d2ab37764aaf4aa7700c078bErik protected static final int MODIFY_ALL_FOLLOWING = 2; 1338d69cd014d912e38d2ab37764aaf4aa7700c078bErik protected static final int MODIFY_ALL = 3; 13473714169d34a55fc01e14b30897a1ebbacf9934eErik 13573714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int DAY_IN_SECONDS = 24 * 60 * 60; 13673714169d34a55fc01e14b30897a1ebbacf9934eErik 1376bcafcf4f28c2d1053547694bd60b3dd9c7fbaa1Isaac Katzenelson private final AsyncQueryService mService; 138eca82e9468c002590045338f98588ad3ce1a0bb3Erik 139eca82e9468c002590045338f98588ad3ce1a0bb3Erik // This allows us to flag the event if something is wrong with it, right now 140eca82e9468c002590045338f98588ad3ce1a0bb3Erik // if an uri is provided for an event that doesn't exist in the db. 14173714169d34a55fc01e14b30897a1ebbacf9934eErik protected boolean mEventOk = true; 14273714169d34a55fc01e14b30897a1ebbacf9934eErik 143d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public static final int ATTENDEE_ID_NONE = -1; 144d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public static final int[] ATTENDEE_VALUES = { 1456bcafcf4f28c2d1053547694bd60b3dd9c7fbaa1Isaac Katzenelson Attendees.ATTENDEE_STATUS_NONE, 1466bcafcf4f28c2d1053547694bd60b3dd9c7fbaa1Isaac Katzenelson Attendees.ATTENDEE_STATUS_ACCEPTED, 1476bcafcf4f28c2d1053547694bd60b3dd9c7fbaa1Isaac Katzenelson Attendees.ATTENDEE_STATUS_TENTATIVE, 1486bcafcf4f28c2d1053547694bd60b3dd9c7fbaa1Isaac Katzenelson Attendees.ATTENDEE_STATUS_DECLINED, 149d845fbe558d5229102b58cce70a1b29fe6cb6967Erik }; 150d845fbe558d5229102b58cce70a1b29fe6cb6967Erik 151eca82e9468c002590045338f98588ad3ce1a0bb3Erik /** 152eca82e9468c002590045338f98588ad3ce1a0bb3Erik * This is the symbolic name for the key used to pass in the boolean for 153eca82e9468c002590045338f98588ad3ce1a0bb3Erik * creating all-day events that is part of the extra data of the intent. 154eca82e9468c002590045338f98588ad3ce1a0bb3Erik * This is used only for creating new events and is set to true if the 155eca82e9468c002590045338f98588ad3ce1a0bb3Erik * default for the new event should be an all-day event. 156eca82e9468c002590045338f98588ad3ce1a0bb3Erik */ 157eca82e9468c002590045338f98588ad3ce1a0bb3Erik public static final String EVENT_ALL_DAY = "allDay"; 158eca82e9468c002590045338f98588ad3ce1a0bb3Erik 159eca82e9468c002590045338f98588ad3ce1a0bb3Erik static final String[] CALENDARS_PROJECTION = new String[] { 160eca82e9468c002590045338f98588ad3ce1a0bb3Erik Calendars._ID, // 0 161ef2add9145f058ea103ade2870a8ba899b9ed1c9RoboErik Calendars.CALENDAR_DISPLAY_NAME, // 1 162eca82e9468c002590045338f98588ad3ce1a0bb3Erik Calendars.OWNER_ACCOUNT, // 2 1630e1e62408b96e1532eb6f6a609ae4c817751aaf3RoboErik Calendars.CALENDAR_COLOR, // 3 164950e8a615f3dcca0b61b2eb8f8de72ecf6825d4bAndy McFadden Calendars.CAN_ORGANIZER_RESPOND, // 4 165ef2add9145f058ea103ade2870a8ba899b9ed1c9RoboErik Calendars.CALENDAR_ACCESS_LEVEL, // 5 166950e8a615f3dcca0b61b2eb8f8de72ecf6825d4bAndy McFadden Calendars.VISIBLE, // 6 1679645d9c3e9b2cfd35c5fc89f41caf6e99153ba10Andy McFadden Calendars.MAX_REMINDERS, // 7 1689645d9c3e9b2cfd35c5fc89f41caf6e99153ba10Andy McFadden Calendars.ALLOWED_REMINDERS, // 8 169c6680c5819b6ad0d1914420902746fe5f86ba403RoboErik Calendars.ALLOWED_ATTENDEE_TYPES, // 9 170c6680c5819b6ad0d1914420902746fe5f86ba403RoboErik Calendars.ALLOWED_AVAILABILITY, // 10 171eca82e9468c002590045338f98588ad3ce1a0bb3Erik }; 172eca82e9468c002590045338f98588ad3ce1a0bb3Erik static final int CALENDARS_INDEX_ID = 0; 173eca82e9468c002590045338f98588ad3ce1a0bb3Erik static final int CALENDARS_INDEX_DISPLAY_NAME = 1; 174eca82e9468c002590045338f98588ad3ce1a0bb3Erik static final int CALENDARS_INDEX_OWNER_ACCOUNT = 2; 175eca82e9468c002590045338f98588ad3ce1a0bb3Erik static final int CALENDARS_INDEX_COLOR = 3; 176950e8a615f3dcca0b61b2eb8f8de72ecf6825d4bAndy McFadden static final int CALENDARS_INDEX_CAN_ORGANIZER_RESPOND = 4; 177352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan static final int CALENDARS_INDEX_ACCESS_LEVEL = 5; 178950e8a615f3dcca0b61b2eb8f8de72ecf6825d4bAndy McFadden static final int CALENDARS_INDEX_VISIBLE = 6; 1799645d9c3e9b2cfd35c5fc89f41caf6e99153ba10Andy McFadden static final int CALENDARS_INDEX_MAX_REMINDERS = 7; 1809645d9c3e9b2cfd35c5fc89f41caf6e99153ba10Andy McFadden static final int CALENDARS_INDEX_ALLOWED_REMINDERS = 8; 181c6680c5819b6ad0d1914420902746fe5f86ba403RoboErik static final int CALENDARS_INDEX_ALLOWED_ATTENDEE_TYPES = 9; 182c6680c5819b6ad0d1914420902746fe5f86ba403RoboErik static final int CALENDARS_INDEX_ALLOWED_AVAILABILITY = 10; 183eca82e9468c002590045338f98588ad3ce1a0bb3Erik 184ef2add9145f058ea103ade2870a8ba899b9ed1c9RoboErik static final String CALENDARS_WHERE_WRITEABLE_VISIBLE = Calendars.CALENDAR_ACCESS_LEVEL + ">=" 185ef2add9145f058ea103ade2870a8ba899b9ed1c9RoboErik + Calendars.CAL_ACCESS_CONTRIBUTOR + " AND " + Calendars.VISIBLE + "=1"; 186eca82e9468c002590045338f98588ad3ce1a0bb3Erik 187352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan static final String CALENDARS_WHERE = Calendars._ID + "=?"; 188352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 189eca82e9468c002590045338f98588ad3ce1a0bb3Erik static final String[] ATTENDEES_PROJECTION = new String[] { 190d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Attendees._ID, // 0 191d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Attendees.ATTENDEE_NAME, // 1 192d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Attendees.ATTENDEE_EMAIL, // 2 193d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Attendees.ATTENDEE_RELATIONSHIP, // 3 194d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Attendees.ATTENDEE_STATUS, // 4 195eca82e9468c002590045338f98588ad3ce1a0bb3Erik }; 196d845fbe558d5229102b58cce70a1b29fe6cb6967Erik static final int ATTENDEES_INDEX_ID = 0; 197d845fbe558d5229102b58cce70a1b29fe6cb6967Erik static final int ATTENDEES_INDEX_NAME = 1; 198d845fbe558d5229102b58cce70a1b29fe6cb6967Erik static final int ATTENDEES_INDEX_EMAIL = 2; 199d845fbe558d5229102b58cce70a1b29fe6cb6967Erik static final int ATTENDEES_INDEX_RELATIONSHIP = 3; 200d845fbe558d5229102b58cce70a1b29fe6cb6967Erik static final int ATTENDEES_INDEX_STATUS = 4; 201c077ec06a91a2e5d82557de3c6edd83166ca9174Isaac Katzenelson static final String ATTENDEES_WHERE = Attendees.EVENT_ID + "=? AND attendeeEmail IS NOT NULL"; 202d845fbe558d5229102b58cce70a1b29fe6cb6967Erik 203d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public static class AttendeeItem { 204d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public boolean mRemoved; 205d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public Attendee mAttendee; 206d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public Drawable mBadge; 207d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public int mUpdateCounts; 208bdcb9fcc732d52708fafb068f535a2c93ff2356fDaisuke Miyakawa public View mView; 2098fa3111ceaab2b26a322caa873f352bb4eb3e783Daniel Lehmann public Uri mContactLookupUri; 210bdcb9fcc732d52708fafb068f535a2c93ff2356fDaisuke Miyakawa 2111784d2de8ee81c6759bef7199a902e13de3b300fRoboErik public AttendeeItem(Attendee attendee, Drawable badge) { 212bdcb9fcc732d52708fafb068f535a2c93ff2356fDaisuke Miyakawa mAttendee = attendee; 213bdcb9fcc732d52708fafb068f535a2c93ff2356fDaisuke Miyakawa mBadge = badge; 214bdcb9fcc732d52708fafb068f535a2c93ff2356fDaisuke Miyakawa } 215d845fbe558d5229102b58cce70a1b29fe6cb6967Erik } 216eca82e9468c002590045338f98588ad3ce1a0bb3Erik 217dd95df57c8c5a58a85c4c0effad5652dec14f621Erik public EditEventHelper(Context context, CalendarEventModel model) { 218dd95df57c8c5a58a85c4c0effad5652dec14f621Erik mService = new AsyncQueryService(context); 21973714169d34a55fc01e14b30897a1ebbacf9934eErik } 22073714169d34a55fc01e14b30897a1ebbacf9934eErik 22173714169d34a55fc01e14b30897a1ebbacf9934eErik /** 222eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Saves the event. Returns true if the event was successfully saved, false 223eca82e9468c002590045338f98588ad3ce1a0bb3Erik * otherwise. 22473714169d34a55fc01e14b30897a1ebbacf9934eErik * 22573714169d34a55fc01e14b30897a1ebbacf9934eErik * @param model The event model to save 22673714169d34a55fc01e14b30897a1ebbacf9934eErik * @param originalModel A model of the original event if it exists 22773714169d34a55fc01e14b30897a1ebbacf9934eErik * @param modifyWhich For recurring events which type of series modification to use 22873714169d34a55fc01e14b30897a1ebbacf9934eErik * @return true if the event was successfully queued for saving 22973714169d34a55fc01e14b30897a1ebbacf9934eErik */ 23073714169d34a55fc01e14b30897a1ebbacf9934eErik public boolean saveEvent(CalendarEventModel model, CalendarEventModel originalModel, 23173714169d34a55fc01e14b30897a1ebbacf9934eErik int modifyWhich) { 23273714169d34a55fc01e14b30897a1ebbacf9934eErik boolean forceSaveReminders = false; 23373714169d34a55fc01e14b30897a1ebbacf9934eErik 23473714169d34a55fc01e14b30897a1ebbacf9934eErik if (DEBUG) { 23573714169d34a55fc01e14b30897a1ebbacf9934eErik Log.d(TAG, "Saving event model: " + model); 23673714169d34a55fc01e14b30897a1ebbacf9934eErik } 23773714169d34a55fc01e14b30897a1ebbacf9934eErik 23873714169d34a55fc01e14b30897a1ebbacf9934eErik if (!mEventOk) { 23973714169d34a55fc01e14b30897a1ebbacf9934eErik if (DEBUG) { 24073714169d34a55fc01e14b30897a1ebbacf9934eErik Log.w(TAG, "Event no longer exists. Event was not saved."); 24173714169d34a55fc01e14b30897a1ebbacf9934eErik } 24273714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 24373714169d34a55fc01e14b30897a1ebbacf9934eErik } 24473714169d34a55fc01e14b30897a1ebbacf9934eErik 24573714169d34a55fc01e14b30897a1ebbacf9934eErik // It's a problem if we try to save a non-existent or invalid model or if we're 24673714169d34a55fc01e14b30897a1ebbacf9934eErik // modifying an existing event and we have the wrong original model 24773714169d34a55fc01e14b30897a1ebbacf9934eErik if (model == null) { 24873714169d34a55fc01e14b30897a1ebbacf9934eErik Log.e(TAG, "Attempted to save null model."); 24973714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 25073714169d34a55fc01e14b30897a1ebbacf9934eErik } 25173714169d34a55fc01e14b30897a1ebbacf9934eErik if (!model.isValid()) { 25273714169d34a55fc01e14b30897a1ebbacf9934eErik Log.e(TAG, "Attempted to save invalid model."); 25373714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 25473714169d34a55fc01e14b30897a1ebbacf9934eErik } 25573714169d34a55fc01e14b30897a1ebbacf9934eErik if (originalModel != null && !isSameEvent(model, originalModel)) { 256eca82e9468c002590045338f98588ad3ce1a0bb3Erik Log.e(TAG, "Attempted to update existing event but models didn't refer to the same " 257eca82e9468c002590045338f98588ad3ce1a0bb3Erik + "event."); 25873714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 25973714169d34a55fc01e14b30897a1ebbacf9934eErik } 26028dab653f55caccbed32f700274f5274abaee089RoboErik if (originalModel != null && model.isUnchanged(originalModel)) { 26128dab653f55caccbed32f700274f5274abaee089RoboErik return false; 26228dab653f55caccbed32f700274f5274abaee089RoboErik } 26373714169d34a55fc01e14b30897a1ebbacf9934eErik 26473714169d34a55fc01e14b30897a1ebbacf9934eErik ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 26573714169d34a55fc01e14b30897a1ebbacf9934eErik int eventIdIndex = -1; 26673714169d34a55fc01e14b30897a1ebbacf9934eErik 26773714169d34a55fc01e14b30897a1ebbacf9934eErik ContentValues values = getContentValuesFromModel(model); 26873714169d34a55fc01e14b30897a1ebbacf9934eErik 2694ebfbd26b1bc2f351d7e1657ccef261b1504b88cErik if (model.mUri != null && originalModel == null) { 27073714169d34a55fc01e14b30897a1ebbacf9934eErik Log.e(TAG, "Existing event but no originalModel provided. Aborting save."); 27173714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 27273714169d34a55fc01e14b30897a1ebbacf9934eErik } 2734ebfbd26b1bc2f351d7e1657ccef261b1504b88cErik Uri uri = null; 2744ebfbd26b1bc2f351d7e1657ccef261b1504b88cErik if (model.mUri != null) { 2754ebfbd26b1bc2f351d7e1657ccef261b1504b88cErik uri = Uri.parse(model.mUri); 2764ebfbd26b1bc2f351d7e1657ccef261b1504b88cErik } 27773714169d34a55fc01e14b30897a1ebbacf9934eErik 27873714169d34a55fc01e14b30897a1ebbacf9934eErik // Update the "hasAlarm" field for the event 279fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden ArrayList<ReminderEntry> reminders = model.mReminders; 280fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden int len = reminders.size(); 28173714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.HAS_ALARM, (len > 0) ? 1 : 0); 28273714169d34a55fc01e14b30897a1ebbacf9934eErik 28373714169d34a55fc01e14b30897a1ebbacf9934eErik if (uri == null) { 28473714169d34a55fc01e14b30897a1ebbacf9934eErik // Add hasAttendeeData for a new event 28573714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.HAS_ATTENDEE_DATA, 1); 286e3f85db16cfc50b25b97c64d8ab99820d579e5deMichael Chan values.put(Events.STATUS, Events.STATUS_CONFIRMED); 28773714169d34a55fc01e14b30897a1ebbacf9934eErik eventIdIndex = ops.size(); 288eca82e9468c002590045338f98588ad3ce1a0bb3Erik ContentProviderOperation.Builder b = ContentProviderOperation.newInsert( 289eca82e9468c002590045338f98588ad3ce1a0bb3Erik Events.CONTENT_URI).withValues(values); 29073714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 29173714169d34a55fc01e14b30897a1ebbacf9934eErik forceSaveReminders = true; 29273714169d34a55fc01e14b30897a1ebbacf9934eErik 29366b2067e0a389180f909eb0a528bea5f6becca23Michael Chan } else if (TextUtils.isEmpty(model.mRrule) && TextUtils.isEmpty(originalModel.mRrule)) { 29473714169d34a55fc01e14b30897a1ebbacf9934eErik // Simple update to a non-recurring event 29573714169d34a55fc01e14b30897a1ebbacf9934eErik checkTimeDependentFields(originalModel, model, values, modifyWhich); 29673714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build()); 29773714169d34a55fc01e14b30897a1ebbacf9934eErik 29866b2067e0a389180f909eb0a528bea5f6becca23Michael Chan } else if (TextUtils.isEmpty(originalModel.mRrule)) { 29973714169d34a55fc01e14b30897a1ebbacf9934eErik // This event was changed from a non-repeating event to a 30073714169d34a55fc01e14b30897a1ebbacf9934eErik // repeating event. 30173714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build()); 30273714169d34a55fc01e14b30897a1ebbacf9934eErik 30373714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (modifyWhich == MODIFY_SELECTED) { 30473714169d34a55fc01e14b30897a1ebbacf9934eErik // Modify contents of the current instance of repeating event 30573714169d34a55fc01e14b30897a1ebbacf9934eErik // Create a recurrence exception 30673714169d34a55fc01e14b30897a1ebbacf9934eErik long begin = model.mOriginalStart; 3070e1e62408b96e1532eb6f6a609ae4c817751aaf3RoboErik values.put(Events.ORIGINAL_SYNC_ID, originalModel.mSyncId); 30873714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.ORIGINAL_INSTANCE_TIME, begin); 30973714169d34a55fc01e14b30897a1ebbacf9934eErik boolean allDay = originalModel.mAllDay; 31073714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.ORIGINAL_ALL_DAY, allDay ? 1 : 0); 311e3f85db16cfc50b25b97c64d8ab99820d579e5deMichael Chan values.put(Events.STATUS, originalModel.mEventStatus); 31273714169d34a55fc01e14b30897a1ebbacf9934eErik 31373714169d34a55fc01e14b30897a1ebbacf9934eErik eventIdIndex = ops.size(); 314eca82e9468c002590045338f98588ad3ce1a0bb3Erik ContentProviderOperation.Builder b = ContentProviderOperation.newInsert( 315eca82e9468c002590045338f98588ad3ce1a0bb3Erik Events.CONTENT_URI).withValues(values); 31673714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 31773714169d34a55fc01e14b30897a1ebbacf9934eErik forceSaveReminders = true; 31873714169d34a55fc01e14b30897a1ebbacf9934eErik 31973714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (modifyWhich == MODIFY_ALL_FOLLOWING) { 32073714169d34a55fc01e14b30897a1ebbacf9934eErik 32166b2067e0a389180f909eb0a528bea5f6becca23Michael Chan if (TextUtils.isEmpty(model.mRrule)) { 32273714169d34a55fc01e14b30897a1ebbacf9934eErik // We've changed a recurring event to a non-recurring event. 32373714169d34a55fc01e14b30897a1ebbacf9934eErik // If the event we are editing is the first in the series, 324eca82e9468c002590045338f98588ad3ce1a0bb3Erik // then delete the whole series. Otherwise, update the series 32573714169d34a55fc01e14b30897a1ebbacf9934eErik // to end at the new start time. 32673714169d34a55fc01e14b30897a1ebbacf9934eErik if (isFirstEventInSeries(model, originalModel)) { 32773714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(ContentProviderOperation.newDelete(uri).build()); 32873714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 329152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // Update the current repeating event to end at the new start time. We 330152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // ignore the RRULE returned because the exception event doesn't want one. 33173714169d34a55fc01e14b30897a1ebbacf9934eErik updatePastEvents(ops, originalModel, model.mOriginalStart); 33273714169d34a55fc01e14b30897a1ebbacf9934eErik } 33373714169d34a55fc01e14b30897a1ebbacf9934eErik eventIdIndex = ops.size(); 334e3f85db16cfc50b25b97c64d8ab99820d579e5deMichael Chan values.put(Events.STATUS, originalModel.mEventStatus); 335eca82e9468c002590045338f98588ad3ce1a0bb3Erik ops.add(ContentProviderOperation.newInsert(Events.CONTENT_URI).withValues(values) 33673714169d34a55fc01e14b30897a1ebbacf9934eErik .build()); 33773714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 33873714169d34a55fc01e14b30897a1ebbacf9934eErik if (isFirstEventInSeries(model, originalModel)) { 33973714169d34a55fc01e14b30897a1ebbacf9934eErik checkTimeDependentFields(originalModel, model, values, modifyWhich); 340eca82e9468c002590045338f98588ad3ce1a0bb3Erik ContentProviderOperation.Builder b = ContentProviderOperation.newUpdate(uri) 34173714169d34a55fc01e14b30897a1ebbacf9934eErik .withValues(values); 34273714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 34373714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 344152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // We need to update the existing recurrence to end before the exception 345152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // event starts. If the recurrence rule has a COUNT, we need to adjust 346152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // that in the original and in the exception. This call rewrites the 347152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // original event's recurrence rule (in "ops"), and returns a new rule 348152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // for the exception. If the exception explicitly set a new rule, however, 349152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // we don't want to overwrite it. 350152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden String newRrule = updatePastEvents(ops, originalModel, model.mOriginalStart); 351152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden if (model.mRrule.equals(originalModel.mRrule)) { 352152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden values.put(Events.RRULE, newRrule); 353152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden } 35473714169d34a55fc01e14b30897a1ebbacf9934eErik 35573714169d34a55fc01e14b30897a1ebbacf9934eErik // Create a new event with the user-modified fields 35673714169d34a55fc01e14b30897a1ebbacf9934eErik eventIdIndex = ops.size(); 357e3f85db16cfc50b25b97c64d8ab99820d579e5deMichael Chan values.put(Events.STATUS, originalModel.mEventStatus); 358eca82e9468c002590045338f98588ad3ce1a0bb3Erik ops.add(ContentProviderOperation.newInsert(Events.CONTENT_URI).withValues( 359eca82e9468c002590045338f98588ad3ce1a0bb3Erik values).build()); 36073714169d34a55fc01e14b30897a1ebbacf9934eErik } 36173714169d34a55fc01e14b30897a1ebbacf9934eErik } 36273714169d34a55fc01e14b30897a1ebbacf9934eErik forceSaveReminders = true; 36373714169d34a55fc01e14b30897a1ebbacf9934eErik 36473714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (modifyWhich == MODIFY_ALL) { 36573714169d34a55fc01e14b30897a1ebbacf9934eErik 36673714169d34a55fc01e14b30897a1ebbacf9934eErik // Modify all instances of repeating event 36766b2067e0a389180f909eb0a528bea5f6becca23Michael Chan if (TextUtils.isEmpty(model.mRrule)) { 36873714169d34a55fc01e14b30897a1ebbacf9934eErik // We've changed a recurring event to a non-recurring event. 36973714169d34a55fc01e14b30897a1ebbacf9934eErik // Delete the whole series and replace it with a new 37073714169d34a55fc01e14b30897a1ebbacf9934eErik // non-recurring event. 37173714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(ContentProviderOperation.newDelete(uri).build()); 37273714169d34a55fc01e14b30897a1ebbacf9934eErik 37373714169d34a55fc01e14b30897a1ebbacf9934eErik eventIdIndex = ops.size(); 374eca82e9468c002590045338f98588ad3ce1a0bb3Erik ops.add(ContentProviderOperation.newInsert(Events.CONTENT_URI).withValues(values) 37573714169d34a55fc01e14b30897a1ebbacf9934eErik .build()); 37673714169d34a55fc01e14b30897a1ebbacf9934eErik forceSaveReminders = true; 37773714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 37873714169d34a55fc01e14b30897a1ebbacf9934eErik checkTimeDependentFields(originalModel, model, values, modifyWhich); 379eca82e9468c002590045338f98588ad3ce1a0bb3Erik ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build()); 38073714169d34a55fc01e14b30897a1ebbacf9934eErik } 38173714169d34a55fc01e14b30897a1ebbacf9934eErik } 38273714169d34a55fc01e14b30897a1ebbacf9934eErik 38373714169d34a55fc01e14b30897a1ebbacf9934eErik // New Event or New Exception to an existing event 38473714169d34a55fc01e14b30897a1ebbacf9934eErik boolean newEvent = (eventIdIndex != -1); 385fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden ArrayList<ReminderEntry> originalReminders; 38673714169d34a55fc01e14b30897a1ebbacf9934eErik if (originalModel != null) { 387fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden originalReminders = originalModel.mReminders; 38873714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 389fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden originalReminders = new ArrayList<ReminderEntry>(); 39073714169d34a55fc01e14b30897a1ebbacf9934eErik } 39173714169d34a55fc01e14b30897a1ebbacf9934eErik 39273714169d34a55fc01e14b30897a1ebbacf9934eErik if (newEvent) { 393fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden saveRemindersWithBackRef(ops, eventIdIndex, reminders, originalReminders, 394eca82e9468c002590045338f98588ad3ce1a0bb3Erik forceSaveReminders); 39573714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (uri != null) { 39673714169d34a55fc01e14b30897a1ebbacf9934eErik long eventId = ContentUris.parseId(uri); 397fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden saveReminders(ops, eventId, reminders, originalReminders, forceSaveReminders); 39873714169d34a55fc01e14b30897a1ebbacf9934eErik } 39973714169d34a55fc01e14b30897a1ebbacf9934eErik 40073714169d34a55fc01e14b30897a1ebbacf9934eErik ContentProviderOperation.Builder b; 40173714169d34a55fc01e14b30897a1ebbacf9934eErik boolean hasAttendeeData = model.mHasAttendeeData; 40273714169d34a55fc01e14b30897a1ebbacf9934eErik 403ef4530b42095069916d2bf4945a2581722b7e318Michael Chan if (hasAttendeeData && model.mOwnerAttendeeId == -1) { 404ef4530b42095069916d2bf4945a2581722b7e318Michael Chan // Organizer is not an attendee 40573714169d34a55fc01e14b30897a1ebbacf9934eErik 40673714169d34a55fc01e14b30897a1ebbacf9934eErik String ownerEmail = model.mOwnerAccount; 407ef4530b42095069916d2bf4945a2581722b7e318Michael Chan if (model.mAttendeesList.size() != 0 && Utils.isValidEmail(ownerEmail)) { 408ef4530b42095069916d2bf4945a2581722b7e318Michael Chan // Add organizer as attendee since we got some attendees 409ef4530b42095069916d2bf4945a2581722b7e318Michael Chan 410ef4530b42095069916d2bf4945a2581722b7e318Michael Chan values.clear(); 41173714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Attendees.ATTENDEE_EMAIL, ownerEmail); 412eca82e9468c002590045338f98588ad3ce1a0bb3Erik values.put(Attendees.ATTENDEE_RELATIONSHIP, Attendees.RELATIONSHIP_ORGANIZER); 413ef4530b42095069916d2bf4945a2581722b7e318Michael Chan values.put(Attendees.ATTENDEE_TYPE, Attendees.TYPE_REQUIRED); 414ef4530b42095069916d2bf4945a2581722b7e318Michael Chan values.put(Attendees.ATTENDEE_STATUS, Attendees.ATTENDEE_STATUS_ACCEPTED); 415e5149edad36737783356778819a9c66993a9cf28Michael Chan 416ef4530b42095069916d2bf4945a2581722b7e318Michael Chan if (newEvent) { 417ef4530b42095069916d2bf4945a2581722b7e318Michael Chan b = ContentProviderOperation.newInsert(Attendees.CONTENT_URI) 418ef4530b42095069916d2bf4945a2581722b7e318Michael Chan .withValues(values); 419ef4530b42095069916d2bf4945a2581722b7e318Michael Chan b.withValueBackReference(Attendees.EVENT_ID, eventIdIndex); 420ef4530b42095069916d2bf4945a2581722b7e318Michael Chan } else { 421ef4530b42095069916d2bf4945a2581722b7e318Michael Chan values.put(Attendees.EVENT_ID, model.mId); 422ef4530b42095069916d2bf4945a2581722b7e318Michael Chan b = ContentProviderOperation.newInsert(Attendees.CONTENT_URI) 423ef4530b42095069916d2bf4945a2581722b7e318Michael Chan .withValues(values); 424e5149edad36737783356778819a9c66993a9cf28Michael Chan } 42573714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 42673714169d34a55fc01e14b30897a1ebbacf9934eErik } 427d845fbe558d5229102b58cce70a1b29fe6cb6967Erik } else if (hasAttendeeData && 428d845fbe558d5229102b58cce70a1b29fe6cb6967Erik model.mSelfAttendeeStatus != originalModel.mSelfAttendeeStatus && 429d845fbe558d5229102b58cce70a1b29fe6cb6967Erik model.mOwnerAttendeeId != -1) { 430d845fbe558d5229102b58cce70a1b29fe6cb6967Erik if (DEBUG) { 431d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Log.d(TAG, "Setting attendee status to " + model.mSelfAttendeeStatus); 432d845fbe558d5229102b58cce70a1b29fe6cb6967Erik } 433d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Uri attUri = ContentUris.withAppendedId(Attendees.CONTENT_URI, model.mOwnerAttendeeId); 434d845fbe558d5229102b58cce70a1b29fe6cb6967Erik 435d845fbe558d5229102b58cce70a1b29fe6cb6967Erik values.clear(); 436d845fbe558d5229102b58cce70a1b29fe6cb6967Erik values.put(Attendees.ATTENDEE_STATUS, model.mSelfAttendeeStatus); 437d845fbe558d5229102b58cce70a1b29fe6cb6967Erik values.put(Attendees.EVENT_ID, model.mId); 438d845fbe558d5229102b58cce70a1b29fe6cb6967Erik b = ContentProviderOperation.newUpdate(attUri).withValues(values); 439d845fbe558d5229102b58cce70a1b29fe6cb6967Erik ops.add(b.build()); 44073714169d34a55fc01e14b30897a1ebbacf9934eErik } 44173714169d34a55fc01e14b30897a1ebbacf9934eErik 442eca82e9468c002590045338f98588ad3ce1a0bb3Erik // TODO: is this the right test? this currently checks if this is 443eca82e9468c002590045338f98588ad3ce1a0bb3Erik // a new event or an existing event. or is this a paranoia check? 44473714169d34a55fc01e14b30897a1ebbacf9934eErik if (hasAttendeeData && (newEvent || uri != null)) { 445d845fbe558d5229102b58cce70a1b29fe6cb6967Erik String attendees = model.getAttendeesString(); 44673714169d34a55fc01e14b30897a1ebbacf9934eErik String originalAttendeesString; 44773714169d34a55fc01e14b30897a1ebbacf9934eErik if (originalModel != null) { 448d845fbe558d5229102b58cce70a1b29fe6cb6967Erik originalAttendeesString = originalModel.getAttendeesString(); 44973714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 45073714169d34a55fc01e14b30897a1ebbacf9934eErik originalAttendeesString = ""; 45173714169d34a55fc01e14b30897a1ebbacf9934eErik } 452eca82e9468c002590045338f98588ad3ce1a0bb3Erik // Hit the content provider only if this is a new event or the user 453eca82e9468c002590045338f98588ad3ce1a0bb3Erik // has changed it 45473714169d34a55fc01e14b30897a1ebbacf9934eErik if (newEvent || !TextUtils.equals(originalAttendeesString, attendees)) { 45573714169d34a55fc01e14b30897a1ebbacf9934eErik // figure out which attendees need to be added and which ones 456eca82e9468c002590045338f98588ad3ce1a0bb3Erik // need to be deleted. use a linked hash set, so we maintain 45773714169d34a55fc01e14b30897a1ebbacf9934eErik // order (but also remove duplicates). 458d845fbe558d5229102b58cce70a1b29fe6cb6967Erik HashMap<String, Attendee> newAttendees = model.mAttendeesList; 459d845fbe558d5229102b58cce70a1b29fe6cb6967Erik LinkedList<String> removedAttendees = new LinkedList<String>(); 46073714169d34a55fc01e14b30897a1ebbacf9934eErik 46173714169d34a55fc01e14b30897a1ebbacf9934eErik // the eventId is only used if eventIdIndex is -1. 46273714169d34a55fc01e14b30897a1ebbacf9934eErik // TODO: clean up this code. 46373714169d34a55fc01e14b30897a1ebbacf9934eErik long eventId = uri != null ? ContentUris.parseId(uri) : -1; 46473714169d34a55fc01e14b30897a1ebbacf9934eErik 46573714169d34a55fc01e14b30897a1ebbacf9934eErik // only compute deltas if this is an existing event. 46673714169d34a55fc01e14b30897a1ebbacf9934eErik // new events (being inserted into the Events table) won't 46773714169d34a55fc01e14b30897a1ebbacf9934eErik // have any existing attendees. 46873714169d34a55fc01e14b30897a1ebbacf9934eErik if (!newEvent) { 469d845fbe558d5229102b58cce70a1b29fe6cb6967Erik removedAttendees.clear(); 470d845fbe558d5229102b58cce70a1b29fe6cb6967Erik HashMap<String, Attendee> originalAttendees = originalModel.mAttendeesList; 471d845fbe558d5229102b58cce70a1b29fe6cb6967Erik for (String originalEmail : originalAttendees.keySet()) { 472d845fbe558d5229102b58cce70a1b29fe6cb6967Erik if (newAttendees.containsKey(originalEmail)) { 473eca82e9468c002590045338f98588ad3ce1a0bb3Erik // existing attendee. remove from new attendees set. 474d845fbe558d5229102b58cce70a1b29fe6cb6967Erik newAttendees.remove(originalEmail); 47573714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 476eca82e9468c002590045338f98588ad3ce1a0bb3Erik // no longer in attendees. mark as removed. 477d845fbe558d5229102b58cce70a1b29fe6cb6967Erik removedAttendees.add(originalEmail); 47873714169d34a55fc01e14b30897a1ebbacf9934eErik } 47973714169d34a55fc01e14b30897a1ebbacf9934eErik } 48073714169d34a55fc01e14b30897a1ebbacf9934eErik 48173714169d34a55fc01e14b30897a1ebbacf9934eErik // delete removed attendees if necessary 48273714169d34a55fc01e14b30897a1ebbacf9934eErik if (removedAttendees.size() > 0) { 48373714169d34a55fc01e14b30897a1ebbacf9934eErik b = ContentProviderOperation.newDelete(Attendees.CONTENT_URI); 48473714169d34a55fc01e14b30897a1ebbacf9934eErik 48573714169d34a55fc01e14b30897a1ebbacf9934eErik String[] args = new String[removedAttendees.size() + 1]; 48673714169d34a55fc01e14b30897a1ebbacf9934eErik args[0] = Long.toString(eventId); 48773714169d34a55fc01e14b30897a1ebbacf9934eErik int i = 1; 48873714169d34a55fc01e14b30897a1ebbacf9934eErik StringBuilder deleteWhere = new StringBuilder(ATTENDEES_DELETE_PREFIX); 489d845fbe558d5229102b58cce70a1b29fe6cb6967Erik for (String removedAttendee : removedAttendees) { 49073714169d34a55fc01e14b30897a1ebbacf9934eErik if (i > 1) { 49173714169d34a55fc01e14b30897a1ebbacf9934eErik deleteWhere.append(","); 49273714169d34a55fc01e14b30897a1ebbacf9934eErik } 49373714169d34a55fc01e14b30897a1ebbacf9934eErik deleteWhere.append("?"); 494d845fbe558d5229102b58cce70a1b29fe6cb6967Erik args[i++] = removedAttendee; 49573714169d34a55fc01e14b30897a1ebbacf9934eErik } 49673714169d34a55fc01e14b30897a1ebbacf9934eErik deleteWhere.append(")"); 49773714169d34a55fc01e14b30897a1ebbacf9934eErik b.withSelection(deleteWhere.toString(), args); 49873714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 49973714169d34a55fc01e14b30897a1ebbacf9934eErik } 50073714169d34a55fc01e14b30897a1ebbacf9934eErik } 50173714169d34a55fc01e14b30897a1ebbacf9934eErik 50273714169d34a55fc01e14b30897a1ebbacf9934eErik if (newAttendees.size() > 0) { 50373714169d34a55fc01e14b30897a1ebbacf9934eErik // Insert the new attendees 504d845fbe558d5229102b58cce70a1b29fe6cb6967Erik for (Attendee attendee : newAttendees.values()) { 50573714169d34a55fc01e14b30897a1ebbacf9934eErik values.clear(); 506d845fbe558d5229102b58cce70a1b29fe6cb6967Erik values.put(Attendees.ATTENDEE_NAME, attendee.mName); 507d845fbe558d5229102b58cce70a1b29fe6cb6967Erik values.put(Attendees.ATTENDEE_EMAIL, attendee.mEmail); 50873714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Attendees.ATTENDEE_RELATIONSHIP, 50973714169d34a55fc01e14b30897a1ebbacf9934eErik Attendees.RELATIONSHIP_ATTENDEE); 510ef4530b42095069916d2bf4945a2581722b7e318Michael Chan values.put(Attendees.ATTENDEE_TYPE, Attendees.TYPE_REQUIRED); 511eca82e9468c002590045338f98588ad3ce1a0bb3Erik values.put(Attendees.ATTENDEE_STATUS, Attendees.ATTENDEE_STATUS_NONE); 51273714169d34a55fc01e14b30897a1ebbacf9934eErik 51373714169d34a55fc01e14b30897a1ebbacf9934eErik if (newEvent) { 514eca82e9468c002590045338f98588ad3ce1a0bb3Erik b = ContentProviderOperation.newInsert(Attendees.CONTENT_URI) 51573714169d34a55fc01e14b30897a1ebbacf9934eErik .withValues(values); 51673714169d34a55fc01e14b30897a1ebbacf9934eErik b.withValueBackReference(Attendees.EVENT_ID, eventIdIndex); 51773714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 51873714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Attendees.EVENT_ID, eventId); 519eca82e9468c002590045338f98588ad3ce1a0bb3Erik b = ContentProviderOperation.newInsert(Attendees.CONTENT_URI) 52073714169d34a55fc01e14b30897a1ebbacf9934eErik .withValues(values); 52173714169d34a55fc01e14b30897a1ebbacf9934eErik } 52273714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 52373714169d34a55fc01e14b30897a1ebbacf9934eErik } 52473714169d34a55fc01e14b30897a1ebbacf9934eErik } 52573714169d34a55fc01e14b30897a1ebbacf9934eErik } 52673714169d34a55fc01e14b30897a1ebbacf9934eErik } 52773714169d34a55fc01e14b30897a1ebbacf9934eErik 52837960c091b5912dd45beb561a85a9064ea42e616Erik 529a7c0390d9c5dd4ff730de505682687fae5f5ced0RoboErik mService.startBatch(mService.getNextToken(), null, android.provider.CalendarContract.AUTHORITY, ops, 53037960c091b5912dd45beb561a85a9064ea42e616Erik Utils.UNDO_DELAY); 53173714169d34a55fc01e14b30897a1ebbacf9934eErik 53273714169d34a55fc01e14b30897a1ebbacf9934eErik return true; 53373714169d34a55fc01e14b30897a1ebbacf9934eErik } 53473714169d34a55fc01e14b30897a1ebbacf9934eErik 535d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public static LinkedHashSet<Rfc822Token> getAddressesFromList(String list, 536d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Rfc822Validator validator) { 53773714169d34a55fc01e14b30897a1ebbacf9934eErik LinkedHashSet<Rfc822Token> addresses = new LinkedHashSet<Rfc822Token>(); 53873714169d34a55fc01e14b30897a1ebbacf9934eErik Rfc822Tokenizer.tokenize(list, addresses); 539d845fbe558d5229102b58cce70a1b29fe6cb6967Erik if (validator == null) { 540d845fbe558d5229102b58cce70a1b29fe6cb6967Erik return addresses; 541d845fbe558d5229102b58cce70a1b29fe6cb6967Erik } 54273714169d34a55fc01e14b30897a1ebbacf9934eErik 543eca82e9468c002590045338f98588ad3ce1a0bb3Erik // validate the emails, out of paranoia. they should already be 54473714169d34a55fc01e14b30897a1ebbacf9934eErik // validated on input, but drop any invalid emails just to be safe. 54573714169d34a55fc01e14b30897a1ebbacf9934eErik Iterator<Rfc822Token> addressIterator = addresses.iterator(); 54673714169d34a55fc01e14b30897a1ebbacf9934eErik while (addressIterator.hasNext()) { 54773714169d34a55fc01e14b30897a1ebbacf9934eErik Rfc822Token address = addressIterator.next(); 548d845fbe558d5229102b58cce70a1b29fe6cb6967Erik if (!validator.isValid(address.getAddress())) { 549d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Log.v(TAG, "Dropping invalid attendee email address: " + address.getAddress()); 55073714169d34a55fc01e14b30897a1ebbacf9934eErik addressIterator.remove(); 55173714169d34a55fc01e14b30897a1ebbacf9934eErik } 55273714169d34a55fc01e14b30897a1ebbacf9934eErik } 55373714169d34a55fc01e14b30897a1ebbacf9934eErik return addresses; 55473714169d34a55fc01e14b30897a1ebbacf9934eErik } 55573714169d34a55fc01e14b30897a1ebbacf9934eErik 556e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang /** 557e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * When we aren't given an explicit start time, we default to the next 558e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * upcoming half hour. So, for example, 5:01 -> 5:30, 5:30 -> 6:00, etc. 559e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * 560e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * @return a UTC time in milliseconds representing the next upcoming half 561e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * hour 562e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang */ 563e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang protected long constructDefaultStartTime(long now) { 564e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang Time defaultStart = new Time(); 565e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang defaultStart.set(now); 566e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang defaultStart.second = 0; 567e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang defaultStart.minute = 30; 568e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang long defaultStartMillis = defaultStart.toMillis(false); 569e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang if (now < defaultStartMillis) { 570e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang return defaultStartMillis; 571e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang } else { 572e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang return defaultStartMillis + 30 * DateUtils.MINUTE_IN_MILLIS; 573e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang } 574e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang } 575e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang 576e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang /** 577e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * When we aren't given an explicit end time, we default to an hour after 578e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * the start time. 579e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * @param startTime the start time 580e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * @return a default end time 581e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang */ 582e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang protected long constructDefaultEndTime(long startTime) { 583e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang return startTime + DateUtils.HOUR_IN_MILLIS; 584e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang } 585e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang 586eca82e9468c002590045338f98588ad3ce1a0bb3Erik // TODO think about how useful this is. Probably check if our event has 587eca82e9468c002590045338f98588ad3ce1a0bb3Erik // changed early on and either update all or nothing. Should still do the if 588eca82e9468c002590045338f98588ad3ce1a0bb3Erik // MODIFY_ALL bit. 589eca82e9468c002590045338f98588ad3ce1a0bb3Erik void checkTimeDependentFields(CalendarEventModel originalModel, CalendarEventModel model, 590eca82e9468c002590045338f98588ad3ce1a0bb3Erik ContentValues values, int modifyWhich) { 59173714169d34a55fc01e14b30897a1ebbacf9934eErik long oldBegin = model.mOriginalStart; 59273714169d34a55fc01e14b30897a1ebbacf9934eErik long oldEnd = model.mOriginalEnd; 59373714169d34a55fc01e14b30897a1ebbacf9934eErik boolean oldAllDay = originalModel.mAllDay; 59473714169d34a55fc01e14b30897a1ebbacf9934eErik String oldRrule = originalModel.mRrule; 59573714169d34a55fc01e14b30897a1ebbacf9934eErik String oldTimezone = originalModel.mTimezone; 59673714169d34a55fc01e14b30897a1ebbacf9934eErik 59773714169d34a55fc01e14b30897a1ebbacf9934eErik long newBegin = model.mStart; 59873714169d34a55fc01e14b30897a1ebbacf9934eErik long newEnd = model.mEnd; 59973714169d34a55fc01e14b30897a1ebbacf9934eErik boolean newAllDay = model.mAllDay; 60073714169d34a55fc01e14b30897a1ebbacf9934eErik String newRrule = model.mRrule; 60173714169d34a55fc01e14b30897a1ebbacf9934eErik String newTimezone = model.mTimezone; 60273714169d34a55fc01e14b30897a1ebbacf9934eErik 60373714169d34a55fc01e14b30897a1ebbacf9934eErik // If none of the time-dependent fields changed, then remove them. 60473714169d34a55fc01e14b30897a1ebbacf9934eErik if (oldBegin == newBegin && oldEnd == newEnd && oldAllDay == newAllDay 60573714169d34a55fc01e14b30897a1ebbacf9934eErik && TextUtils.equals(oldRrule, newRrule) 606eca82e9468c002590045338f98588ad3ce1a0bb3Erik && TextUtils.equals(oldTimezone, newTimezone)) { 60773714169d34a55fc01e14b30897a1ebbacf9934eErik values.remove(Events.DTSTART); 60873714169d34a55fc01e14b30897a1ebbacf9934eErik values.remove(Events.DTEND); 60973714169d34a55fc01e14b30897a1ebbacf9934eErik values.remove(Events.DURATION); 61073714169d34a55fc01e14b30897a1ebbacf9934eErik values.remove(Events.ALL_DAY); 61173714169d34a55fc01e14b30897a1ebbacf9934eErik values.remove(Events.RRULE); 61273714169d34a55fc01e14b30897a1ebbacf9934eErik values.remove(Events.EVENT_TIMEZONE); 61373714169d34a55fc01e14b30897a1ebbacf9934eErik return; 61473714169d34a55fc01e14b30897a1ebbacf9934eErik } 61573714169d34a55fc01e14b30897a1ebbacf9934eErik 61666b2067e0a389180f909eb0a528bea5f6becca23Michael Chan if (TextUtils.isEmpty(oldRrule) || TextUtils.isEmpty(newRrule)) { 61773714169d34a55fc01e14b30897a1ebbacf9934eErik return; 61873714169d34a55fc01e14b30897a1ebbacf9934eErik } 61973714169d34a55fc01e14b30897a1ebbacf9934eErik 62073714169d34a55fc01e14b30897a1ebbacf9934eErik // If we are modifying all events then we need to set DTSTART to the 62173714169d34a55fc01e14b30897a1ebbacf9934eErik // start time of the first event in the series, not the current 622eca82e9468c002590045338f98588ad3ce1a0bb3Erik // date and time. If the start time of the event was changed 62373714169d34a55fc01e14b30897a1ebbacf9934eErik // (from, say, 3pm to 4pm), then we want to add the time difference 62473714169d34a55fc01e14b30897a1ebbacf9934eErik // to the start time of the first event in the series (the DTSTART 625eca82e9468c002590045338f98588ad3ce1a0bb3Erik // value). If we are modifying one instance or all following instances, 62673714169d34a55fc01e14b30897a1ebbacf9934eErik // then we leave the DTSTART field alone. 62773714169d34a55fc01e14b30897a1ebbacf9934eErik if (modifyWhich == MODIFY_ALL) { 62873714169d34a55fc01e14b30897a1ebbacf9934eErik long oldStartMillis = originalModel.mStart; 62973714169d34a55fc01e14b30897a1ebbacf9934eErik if (oldBegin != newBegin) { 63073714169d34a55fc01e14b30897a1ebbacf9934eErik // The user changed the start time of this event 63173714169d34a55fc01e14b30897a1ebbacf9934eErik long offset = newBegin - oldBegin; 63273714169d34a55fc01e14b30897a1ebbacf9934eErik oldStartMillis += offset; 63373714169d34a55fc01e14b30897a1ebbacf9934eErik } 63473714169d34a55fc01e14b30897a1ebbacf9934eErik if (newAllDay) { 63573714169d34a55fc01e14b30897a1ebbacf9934eErik Time time = new Time(Time.TIMEZONE_UTC); 63673714169d34a55fc01e14b30897a1ebbacf9934eErik time.set(oldStartMillis); 63773714169d34a55fc01e14b30897a1ebbacf9934eErik time.hour = 0; 63873714169d34a55fc01e14b30897a1ebbacf9934eErik time.minute = 0; 63973714169d34a55fc01e14b30897a1ebbacf9934eErik time.second = 0; 64073714169d34a55fc01e14b30897a1ebbacf9934eErik oldStartMillis = time.toMillis(false); 64173714169d34a55fc01e14b30897a1ebbacf9934eErik } 64273714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.DTSTART, oldStartMillis); 64373714169d34a55fc01e14b30897a1ebbacf9934eErik } 64473714169d34a55fc01e14b30897a1ebbacf9934eErik } 64573714169d34a55fc01e14b30897a1ebbacf9934eErik 64673714169d34a55fc01e14b30897a1ebbacf9934eErik /** 647eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Prepares an update to the original event so it stops where the new series 648152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * begins. When we update 'this and all following' events we need to change 649eca82e9468c002590045338f98588ad3ce1a0bb3Erik * the original event to end before a new series starts. This creates an 650eca82e9468c002590045338f98588ad3ce1a0bb3Erik * update to the old event's rrule to do that. 651152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden *<p> 652152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * If the event's recurrence rule has a COUNT, we also need to reduce the count in the 653152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * RRULE for the exception event. 65473714169d34a55fc01e14b30897a1ebbacf9934eErik * 65573714169d34a55fc01e14b30897a1ebbacf9934eErik * @param ops The list of operations to add the update to 65673714169d34a55fc01e14b30897a1ebbacf9934eErik * @param originalModel The original event that we're updating 657152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * @param endTimeMillis The time before which the event must end (i.e. the start time of the 658152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * exception event instance). 659152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * @return A replacement exception recurrence rule. 66073714169d34a55fc01e14b30897a1ebbacf9934eErik */ 661152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden public String updatePastEvents(ArrayList<ContentProviderOperation> ops, 662152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden CalendarEventModel originalModel, long endTimeMillis) { 663152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden boolean origAllDay = originalModel.mAllDay; 664152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden String origRrule = originalModel.mRrule; 665152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden String newRrule = origRrule; 666eca82e9468c002590045338f98588ad3ce1a0bb3Erik 667152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden EventRecurrence origRecurrence = new EventRecurrence(); 668152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden origRecurrence.parse(origRrule); 66973714169d34a55fc01e14b30897a1ebbacf9934eErik 670152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // Get the start time of the first instance in the original recurrence. 671152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden long startTimeMillis = originalModel.mStart; 672eca82e9468c002590045338f98588ad3ce1a0bb3Erik Time dtstart = new Time(); 673eca82e9468c002590045338f98588ad3ce1a0bb3Erik dtstart.timezone = originalModel.mTimezone; 674152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden dtstart.set(startTimeMillis); 675152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden 676152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden ContentValues updateValues = new ContentValues(); 677152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden 678152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden if (origRecurrence.count > 0) { 679152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden /* 680152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * Generate the full set of instances for this recurrence, from the first to the 681152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * one just before endTimeMillis. The list should never be empty, because this method 682152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * should not be called for the first instance. All we're really interested in is 683152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * the *number* of instances found. 684152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * 685152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * TODO: the model assumes RRULE and ignores RDATE, EXRULE, and EXDATE. For the 686152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * current environment this is reasonable, but that may not hold in the future. 687152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * 688152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * TODO: if COUNT is 1, should we convert the event to non-recurring? e.g. we 689152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * do an "edit this and all future events" on the 2nd instances. 690152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden */ 691152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden RecurrenceSet recurSet = new RecurrenceSet(originalModel.mRrule, null, null, null); 692152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden RecurrenceProcessor recurProc = new RecurrenceProcessor(); 693152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden long[] recurrences; 694152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden try { 695152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden recurrences = recurProc.expand(dtstart, recurSet, startTimeMillis, endTimeMillis); 696152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden } catch (DateException de) { 697152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden throw new RuntimeException(de); 698152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden } 699152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden 700152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden if (recurrences.length == 0) { 701152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden throw new RuntimeException("can't use this method on first instance"); 702152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden } 703152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden 704152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden EventRecurrence excepRecurrence = new EventRecurrence(); 705152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden excepRecurrence.parse(origRrule); // TODO: add+use a copy constructor instead 706152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden excepRecurrence.count -= recurrences.length; 707152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden newRrule = excepRecurrence.toString(); 708152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden 709152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden origRecurrence.count = recurrences.length; 710152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden 711152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden } else { 712152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // The "until" time must be in UTC time in order for Google calendar 713152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // to display it properly. For all-day events, the "until" time string 714152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // must include just the date field, and not the time field. The 715152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // repeating events repeat up to and including the "until" time. 716152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden Time untilTime = new Time(); 717152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden untilTime.timezone = Time.TIMEZONE_UTC; 718152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden 719152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // Subtract one second from the old begin time to get the new 720152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // "until" time. 721152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden untilTime.set(endTimeMillis - 1000); // subtract one second (1000 millis) 722152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden if (origAllDay) { 723152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden untilTime.hour = 0; 724152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden untilTime.minute = 0; 725152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden untilTime.second = 0; 726152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden untilTime.allDay = true; 727152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden untilTime.normalize(false); 728152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden 729152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // This should no longer be necessary -- DTSTART should already be in the correct 730152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden // format for an all-day event. 731152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden dtstart.hour = 0; 732152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden dtstart.minute = 0; 733152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden dtstart.second = 0; 734152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden dtstart.allDay = true; 735152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden dtstart.timezone = Time.TIMEZONE_UTC; 736152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden } 737152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden origRecurrence.until = untilTime.format2445(); 73873714169d34a55fc01e14b30897a1ebbacf9934eErik } 73973714169d34a55fc01e14b30897a1ebbacf9934eErik 740152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden updateValues.put(Events.RRULE, origRecurrence.toString()); 741152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden updateValues.put(Events.DTSTART, dtstart.normalize(true)); 74259ead671b50514b49749fcd6a1d4186a270239d2Erik ContentProviderOperation.Builder b = 74359ead671b50514b49749fcd6a1d4186a270239d2Erik ContentProviderOperation.newUpdate(Uri.parse(originalModel.mUri)) 744152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden .withValues(updateValues); 74573714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 746152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden 747152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden return newRrule; 74873714169d34a55fc01e14b30897a1ebbacf9934eErik } 74973714169d34a55fc01e14b30897a1ebbacf9934eErik 75073714169d34a55fc01e14b30897a1ebbacf9934eErik /** 751eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Compares two models to ensure that they refer to the same event. This is 752eca82e9468c002590045338f98588ad3ce1a0bb3Erik * a safety check to make sure an updated event model refers to the same 753eca82e9468c002590045338f98588ad3ce1a0bb3Erik * event as the original model. If the original model is null then this is a 754eca82e9468c002590045338f98588ad3ce1a0bb3Erik * new event or we're forcing an overwrite so we return true in that case. 755eca82e9468c002590045338f98588ad3ce1a0bb3Erik * The important identifiers are the Calendar Id and the Event Id. 75673714169d34a55fc01e14b30897a1ebbacf9934eErik * 75773714169d34a55fc01e14b30897a1ebbacf9934eErik * @return 75873714169d34a55fc01e14b30897a1ebbacf9934eErik */ 759fbce65e53c7a111955f638db5bf8bee35381e5b7Erik public static boolean isSameEvent(CalendarEventModel model, CalendarEventModel originalModel) { 76073714169d34a55fc01e14b30897a1ebbacf9934eErik if (originalModel == null) { 76173714169d34a55fc01e14b30897a1ebbacf9934eErik return true; 76273714169d34a55fc01e14b30897a1ebbacf9934eErik } 76373714169d34a55fc01e14b30897a1ebbacf9934eErik 76473714169d34a55fc01e14b30897a1ebbacf9934eErik if (model.mCalendarId != originalModel.mCalendarId) { 76573714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 76673714169d34a55fc01e14b30897a1ebbacf9934eErik } 76773714169d34a55fc01e14b30897a1ebbacf9934eErik if (model.mId != originalModel.mId) { 76873714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 76973714169d34a55fc01e14b30897a1ebbacf9934eErik } 77073714169d34a55fc01e14b30897a1ebbacf9934eErik 77173714169d34a55fc01e14b30897a1ebbacf9934eErik return true; 77273714169d34a55fc01e14b30897a1ebbacf9934eErik } 77373714169d34a55fc01e14b30897a1ebbacf9934eErik 774eca82e9468c002590045338f98588ad3ce1a0bb3Erik /** 775eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Saves the reminders, if they changed. Returns true if operations to 776eca82e9468c002590045338f98588ad3ce1a0bb3Erik * update the database were added. 77773714169d34a55fc01e14b30897a1ebbacf9934eErik * 77873714169d34a55fc01e14b30897a1ebbacf9934eErik * @param ops the array of ContentProviderOperations 77973714169d34a55fc01e14b30897a1ebbacf9934eErik * @param eventId the id of the event whose reminders are being updated 780fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden * @param reminders the array of reminders set by the user 781fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden * @param originalReminders the original array of reminders 782eca82e9468c002590045338f98588ad3ce1a0bb3Erik * @param forceSave if true, then save the reminders even if they didn't change 78373714169d34a55fc01e14b30897a1ebbacf9934eErik * @return true if operations to update the database were added 78473714169d34a55fc01e14b30897a1ebbacf9934eErik */ 785fbce65e53c7a111955f638db5bf8bee35381e5b7Erik public static boolean saveReminders(ArrayList<ContentProviderOperation> ops, long eventId, 786fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden ArrayList<ReminderEntry> reminders, ArrayList<ReminderEntry> originalReminders, 78773714169d34a55fc01e14b30897a1ebbacf9934eErik boolean forceSave) { 78873714169d34a55fc01e14b30897a1ebbacf9934eErik // If the reminders have not changed, then don't update the database 789fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden if (reminders.equals(originalReminders) && !forceSave) { 79073714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 79173714169d34a55fc01e14b30897a1ebbacf9934eErik } 79273714169d34a55fc01e14b30897a1ebbacf9934eErik 79373714169d34a55fc01e14b30897a1ebbacf9934eErik // Delete all the existing reminders for this event 79473714169d34a55fc01e14b30897a1ebbacf9934eErik String where = Reminders.EVENT_ID + "=?"; 79573714169d34a55fc01e14b30897a1ebbacf9934eErik String[] args = new String[] {Long.toString(eventId)}; 796eca82e9468c002590045338f98588ad3ce1a0bb3Erik ContentProviderOperation.Builder b = ContentProviderOperation 797eca82e9468c002590045338f98588ad3ce1a0bb3Erik .newDelete(Reminders.CONTENT_URI); 79873714169d34a55fc01e14b30897a1ebbacf9934eErik b.withSelection(where, args); 79973714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 80073714169d34a55fc01e14b30897a1ebbacf9934eErik 80173714169d34a55fc01e14b30897a1ebbacf9934eErik ContentValues values = new ContentValues(); 802fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden int len = reminders.size(); 80373714169d34a55fc01e14b30897a1ebbacf9934eErik 80473714169d34a55fc01e14b30897a1ebbacf9934eErik // Insert the new reminders, if any 80573714169d34a55fc01e14b30897a1ebbacf9934eErik for (int i = 0; i < len; i++) { 806fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden ReminderEntry re = reminders.get(i); 80773714169d34a55fc01e14b30897a1ebbacf9934eErik 80873714169d34a55fc01e14b30897a1ebbacf9934eErik values.clear(); 809fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden values.put(Reminders.MINUTES, re.getMinutes()); 810fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden values.put(Reminders.METHOD, re.getMethod()); 81173714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Reminders.EVENT_ID, eventId); 81273714169d34a55fc01e14b30897a1ebbacf9934eErik b = ContentProviderOperation.newInsert(Reminders.CONTENT_URI).withValues(values); 81373714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 81473714169d34a55fc01e14b30897a1ebbacf9934eErik } 81573714169d34a55fc01e14b30897a1ebbacf9934eErik return true; 81673714169d34a55fc01e14b30897a1ebbacf9934eErik } 81773714169d34a55fc01e14b30897a1ebbacf9934eErik 81873714169d34a55fc01e14b30897a1ebbacf9934eErik /** 819eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Saves the reminders, if they changed. Returns true if operations to 820eca82e9468c002590045338f98588ad3ce1a0bb3Erik * update the database were added. Uses a reference id since an id isn't 821eca82e9468c002590045338f98588ad3ce1a0bb3Erik * created until the row is added. 82273714169d34a55fc01e14b30897a1ebbacf9934eErik * 82373714169d34a55fc01e14b30897a1ebbacf9934eErik * @param ops the array of ContentProviderOperations 82473714169d34a55fc01e14b30897a1ebbacf9934eErik * @param eventId the id of the event whose reminders are being updated 82573714169d34a55fc01e14b30897a1ebbacf9934eErik * @param reminderMinutes the array of reminders set by the user 82673714169d34a55fc01e14b30897a1ebbacf9934eErik * @param originalMinutes the original array of reminders 827eca82e9468c002590045338f98588ad3ce1a0bb3Erik * @param forceSave if true, then save the reminders even if they didn't change 82873714169d34a55fc01e14b30897a1ebbacf9934eErik * @return true if operations to update the database were added 82973714169d34a55fc01e14b30897a1ebbacf9934eErik */ 830fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden public static boolean saveRemindersWithBackRef(ArrayList<ContentProviderOperation> ops, 831fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden int eventIdIndex, ArrayList<ReminderEntry> reminders, 832fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden ArrayList<ReminderEntry> originalReminders, boolean forceSave) { 83373714169d34a55fc01e14b30897a1ebbacf9934eErik // If the reminders have not changed, then don't update the database 834fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden if (reminders.equals(originalReminders) && !forceSave) { 83573714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 83673714169d34a55fc01e14b30897a1ebbacf9934eErik } 83773714169d34a55fc01e14b30897a1ebbacf9934eErik 83873714169d34a55fc01e14b30897a1ebbacf9934eErik // Delete all the existing reminders for this event 839eca82e9468c002590045338f98588ad3ce1a0bb3Erik ContentProviderOperation.Builder b = ContentProviderOperation 840eca82e9468c002590045338f98588ad3ce1a0bb3Erik .newDelete(Reminders.CONTENT_URI); 84173714169d34a55fc01e14b30897a1ebbacf9934eErik b.withSelection(Reminders.EVENT_ID + "=?", new String[1]); 84273714169d34a55fc01e14b30897a1ebbacf9934eErik b.withSelectionBackReference(0, eventIdIndex); 84373714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 84473714169d34a55fc01e14b30897a1ebbacf9934eErik 84573714169d34a55fc01e14b30897a1ebbacf9934eErik ContentValues values = new ContentValues(); 846fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden int len = reminders.size(); 84773714169d34a55fc01e14b30897a1ebbacf9934eErik 84873714169d34a55fc01e14b30897a1ebbacf9934eErik // Insert the new reminders, if any 84973714169d34a55fc01e14b30897a1ebbacf9934eErik for (int i = 0; i < len; i++) { 850fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden ReminderEntry re = reminders.get(i); 85173714169d34a55fc01e14b30897a1ebbacf9934eErik 85273714169d34a55fc01e14b30897a1ebbacf9934eErik values.clear(); 853fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden values.put(Reminders.MINUTES, re.getMinutes()); 854fea9af5d69ad3e5687a4efd72d7d3adfb8d056c8Andy McFadden values.put(Reminders.METHOD, re.getMethod()); 85573714169d34a55fc01e14b30897a1ebbacf9934eErik b = ContentProviderOperation.newInsert(Reminders.CONTENT_URI).withValues(values); 85673714169d34a55fc01e14b30897a1ebbacf9934eErik b.withValueBackReference(Reminders.EVENT_ID, eventIdIndex); 85773714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 85873714169d34a55fc01e14b30897a1ebbacf9934eErik } 85973714169d34a55fc01e14b30897a1ebbacf9934eErik return true; 86073714169d34a55fc01e14b30897a1ebbacf9934eErik } 86173714169d34a55fc01e14b30897a1ebbacf9934eErik 862eca82e9468c002590045338f98588ad3ce1a0bb3Erik // It's the first event in the series if the start time before being 863eca82e9468c002590045338f98588ad3ce1a0bb3Erik // modified is the same as the original event's start time 86473714169d34a55fc01e14b30897a1ebbacf9934eErik static boolean isFirstEventInSeries(CalendarEventModel model, 86573714169d34a55fc01e14b30897a1ebbacf9934eErik CalendarEventModel originalModel) { 86673714169d34a55fc01e14b30897a1ebbacf9934eErik return model.mOriginalStart == originalModel.mStart; 86773714169d34a55fc01e14b30897a1ebbacf9934eErik } 86873714169d34a55fc01e14b30897a1ebbacf9934eErik 86973714169d34a55fc01e14b30897a1ebbacf9934eErik // Adds an rRule and duration to a set of content values 87073714169d34a55fc01e14b30897a1ebbacf9934eErik void addRecurrenceRule(ContentValues values, CalendarEventModel model) { 87173714169d34a55fc01e14b30897a1ebbacf9934eErik String rrule = model.mRrule; 87273714169d34a55fc01e14b30897a1ebbacf9934eErik 87373714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.RRULE, rrule); 87473714169d34a55fc01e14b30897a1ebbacf9934eErik long end = model.mEnd; 87573714169d34a55fc01e14b30897a1ebbacf9934eErik long start = model.mStart; 87673714169d34a55fc01e14b30897a1ebbacf9934eErik String duration = model.mDuration; 87773714169d34a55fc01e14b30897a1ebbacf9934eErik 87837960c091b5912dd45beb561a85a9064ea42e616Erik boolean isAllDay = model.mAllDay; 87937960c091b5912dd45beb561a85a9064ea42e616Erik if (end > start) { 88037960c091b5912dd45beb561a85a9064ea42e616Erik if (isAllDay) { 88137960c091b5912dd45beb561a85a9064ea42e616Erik // if it's all day compute the duration in days 88237960c091b5912dd45beb561a85a9064ea42e616Erik long days = (end - start + DateUtils.DAY_IN_MILLIS - 1) 88337960c091b5912dd45beb561a85a9064ea42e616Erik / DateUtils.DAY_IN_MILLIS; 88437960c091b5912dd45beb561a85a9064ea42e616Erik duration = "P" + days + "D"; 88573714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 88637960c091b5912dd45beb561a85a9064ea42e616Erik // otherwise compute the duration in seconds 88737960c091b5912dd45beb561a85a9064ea42e616Erik long seconds = (end - start) / DateUtils.SECOND_IN_MILLIS; 88837960c091b5912dd45beb561a85a9064ea42e616Erik duration = "P" + seconds + "S"; 88937960c091b5912dd45beb561a85a9064ea42e616Erik } 89037960c091b5912dd45beb561a85a9064ea42e616Erik } else if (TextUtils.isEmpty(duration)) { 89137960c091b5912dd45beb561a85a9064ea42e616Erik 89237960c091b5912dd45beb561a85a9064ea42e616Erik // If no good duration info exists assume the default 89337960c091b5912dd45beb561a85a9064ea42e616Erik if (isAllDay) { 89437960c091b5912dd45beb561a85a9064ea42e616Erik duration = "P1D"; 89537960c091b5912dd45beb561a85a9064ea42e616Erik } else { 89637960c091b5912dd45beb561a85a9064ea42e616Erik duration = "P3600S"; 89773714169d34a55fc01e14b30897a1ebbacf9934eErik } 89873714169d34a55fc01e14b30897a1ebbacf9934eErik } 89973714169d34a55fc01e14b30897a1ebbacf9934eErik // recurring events should have a duration and dtend set to null 90073714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.DURATION, duration); 901eca82e9468c002590045338f98588ad3ce1a0bb3Erik values.put(Events.DTEND, (Long) null); 90273714169d34a55fc01e14b30897a1ebbacf9934eErik } 90373714169d34a55fc01e14b30897a1ebbacf9934eErik 90473714169d34a55fc01e14b30897a1ebbacf9934eErik /** 905eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Uses the recurrence selection and the model data to build an rrule and 906eca82e9468c002590045338f98588ad3ce1a0bb3Erik * write it to the model. 90773714169d34a55fc01e14b30897a1ebbacf9934eErik * 90873714169d34a55fc01e14b30897a1ebbacf9934eErik * @param selection the type of rrule 90973714169d34a55fc01e14b30897a1ebbacf9934eErik * @param model The event to update 9108e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang * @param weekStart the week start day, specified as java.util.Calendar 9118e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang * constants 91273714169d34a55fc01e14b30897a1ebbacf9934eErik */ 9138e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang static void updateRecurrenceRule(int selection, CalendarEventModel model, 9148e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang int weekStart) { 91573714169d34a55fc01e14b30897a1ebbacf9934eErik // Make sure we don't have any leftover data from the previous setting 91673714169d34a55fc01e14b30897a1ebbacf9934eErik EventRecurrence eventRecurrence = new EventRecurrence(); 91773714169d34a55fc01e14b30897a1ebbacf9934eErik 91873714169d34a55fc01e14b30897a1ebbacf9934eErik if (selection == DOES_NOT_REPEAT) { 91973714169d34a55fc01e14b30897a1ebbacf9934eErik model.mRrule = null; 92073714169d34a55fc01e14b30897a1ebbacf9934eErik return; 92173714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_CUSTOM) { 92273714169d34a55fc01e14b30897a1ebbacf9934eErik // Keep custom recurrence as before. 92373714169d34a55fc01e14b30897a1ebbacf9934eErik return; 92473714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_DAILY) { 92573714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.freq = EventRecurrence.DAILY; 92673714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_EVERY_WEEKDAY) { 92773714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.freq = EventRecurrence.WEEKLY; 92873714169d34a55fc01e14b30897a1ebbacf9934eErik int dayCount = 5; 92973714169d34a55fc01e14b30897a1ebbacf9934eErik int[] byday = new int[dayCount]; 93073714169d34a55fc01e14b30897a1ebbacf9934eErik int[] bydayNum = new int[dayCount]; 93173714169d34a55fc01e14b30897a1ebbacf9934eErik 93273714169d34a55fc01e14b30897a1ebbacf9934eErik byday[0] = EventRecurrence.MO; 93373714169d34a55fc01e14b30897a1ebbacf9934eErik byday[1] = EventRecurrence.TU; 93473714169d34a55fc01e14b30897a1ebbacf9934eErik byday[2] = EventRecurrence.WE; 93573714169d34a55fc01e14b30897a1ebbacf9934eErik byday[3] = EventRecurrence.TH; 93673714169d34a55fc01e14b30897a1ebbacf9934eErik byday[4] = EventRecurrence.FR; 93773714169d34a55fc01e14b30897a1ebbacf9934eErik for (int day = 0; day < dayCount; day++) { 93873714169d34a55fc01e14b30897a1ebbacf9934eErik bydayNum[day] = 0; 93973714169d34a55fc01e14b30897a1ebbacf9934eErik } 94073714169d34a55fc01e14b30897a1ebbacf9934eErik 94173714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.byday = byday; 94273714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayNum = bydayNum; 94373714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayCount = dayCount; 94473714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_WEEKLY_ON_DAY) { 94573714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.freq = EventRecurrence.WEEKLY; 94673714169d34a55fc01e14b30897a1ebbacf9934eErik int[] days = new int[1]; 94773714169d34a55fc01e14b30897a1ebbacf9934eErik int dayCount = 1; 94873714169d34a55fc01e14b30897a1ebbacf9934eErik int[] dayNum = new int[dayCount]; 94973714169d34a55fc01e14b30897a1ebbacf9934eErik Time startTime = new Time(model.mTimezone); 95073714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.set(model.mStart); 95173714169d34a55fc01e14b30897a1ebbacf9934eErik 95273714169d34a55fc01e14b30897a1ebbacf9934eErik days[0] = EventRecurrence.timeDay2Day(startTime.weekDay); 95373714169d34a55fc01e14b30897a1ebbacf9934eErik // not sure why this needs to be zero, but set it for now. 95473714169d34a55fc01e14b30897a1ebbacf9934eErik dayNum[0] = 0; 95573714169d34a55fc01e14b30897a1ebbacf9934eErik 95673714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.byday = days; 95773714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayNum = dayNum; 95873714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayCount = dayCount; 95973714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_MONTHLY_ON_DAY) { 96073714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.freq = EventRecurrence.MONTHLY; 96173714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayCount = 0; 96273714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bymonthdayCount = 1; 96373714169d34a55fc01e14b30897a1ebbacf9934eErik int[] bymonthday = new int[1]; 96473714169d34a55fc01e14b30897a1ebbacf9934eErik Time startTime = new Time(model.mTimezone); 96573714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.set(model.mStart); 96673714169d34a55fc01e14b30897a1ebbacf9934eErik bymonthday[0] = startTime.monthDay; 96773714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bymonthday = bymonthday; 96873714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_MONTHLY_ON_DAY_COUNT) { 96973714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.freq = EventRecurrence.MONTHLY; 97073714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayCount = 1; 97173714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bymonthdayCount = 0; 97273714169d34a55fc01e14b30897a1ebbacf9934eErik 97373714169d34a55fc01e14b30897a1ebbacf9934eErik int[] byday = new int[1]; 97473714169d34a55fc01e14b30897a1ebbacf9934eErik int[] bydayNum = new int[1]; 97573714169d34a55fc01e14b30897a1ebbacf9934eErik Time startTime = new Time(model.mTimezone); 97673714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.set(model.mStart); 97773714169d34a55fc01e14b30897a1ebbacf9934eErik // Compute the week number (for example, the "2nd" Monday) 97873714169d34a55fc01e14b30897a1ebbacf9934eErik int dayCount = 1 + ((startTime.monthDay - 1) / 7); 97973714169d34a55fc01e14b30897a1ebbacf9934eErik if (dayCount == 5) { 98073714169d34a55fc01e14b30897a1ebbacf9934eErik dayCount = -1; 98173714169d34a55fc01e14b30897a1ebbacf9934eErik } 98273714169d34a55fc01e14b30897a1ebbacf9934eErik bydayNum[0] = dayCount; 98373714169d34a55fc01e14b30897a1ebbacf9934eErik byday[0] = EventRecurrence.timeDay2Day(startTime.weekDay); 98473714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.byday = byday; 98573714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayNum = bydayNum; 98673714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_YEARLY) { 98773714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.freq = EventRecurrence.YEARLY; 98873714169d34a55fc01e14b30897a1ebbacf9934eErik } 98973714169d34a55fc01e14b30897a1ebbacf9934eErik 99073714169d34a55fc01e14b30897a1ebbacf9934eErik // Set the week start day. 9918e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang eventRecurrence.wkst = EventRecurrence.calendarDay2Day(weekStart); 99273714169d34a55fc01e14b30897a1ebbacf9934eErik model.mRrule = eventRecurrence.toString(); 99373714169d34a55fc01e14b30897a1ebbacf9934eErik } 99473714169d34a55fc01e14b30897a1ebbacf9934eErik 99573714169d34a55fc01e14b30897a1ebbacf9934eErik /** 996eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Uses an event cursor to fill in the given model This method assumes the 997eca82e9468c002590045338f98588ad3ce1a0bb3Erik * cursor used {@link #EVENT_PROJECTION} as it's query projection. It uses 998eca82e9468c002590045338f98588ad3ce1a0bb3Erik * the cursor to fill in the given model with all the information available. 99973714169d34a55fc01e14b30897a1ebbacf9934eErik * 100073714169d34a55fc01e14b30897a1ebbacf9934eErik * @param model The model to fill in 100173714169d34a55fc01e14b30897a1ebbacf9934eErik * @param cursor An event cursor that used {@link #EVENT_PROJECTION} for the query 100273714169d34a55fc01e14b30897a1ebbacf9934eErik */ 100388c483a0189cbe7132330cb1b563f07326f3bcf9Erik public static void setModelFromCursor(CalendarEventModel model, Cursor cursor) { 100473714169d34a55fc01e14b30897a1ebbacf9934eErik if (model == null || cursor == null || cursor.getCount() != 1) { 100573714169d34a55fc01e14b30897a1ebbacf9934eErik Log.wtf(TAG, "Attempted to build non-existent model or from an incorrect query."); 100673714169d34a55fc01e14b30897a1ebbacf9934eErik return; 100773714169d34a55fc01e14b30897a1ebbacf9934eErik } 100873714169d34a55fc01e14b30897a1ebbacf9934eErik 100973714169d34a55fc01e14b30897a1ebbacf9934eErik model.clear(); 101073714169d34a55fc01e14b30897a1ebbacf9934eErik cursor.moveToFirst(); 101173714169d34a55fc01e14b30897a1ebbacf9934eErik 101273714169d34a55fc01e14b30897a1ebbacf9934eErik model.mId = cursor.getInt(EVENT_INDEX_ID); 101373714169d34a55fc01e14b30897a1ebbacf9934eErik model.mTitle = cursor.getString(EVENT_INDEX_TITLE); 101473714169d34a55fc01e14b30897a1ebbacf9934eErik model.mDescription = cursor.getString(EVENT_INDEX_DESCRIPTION); 101573714169d34a55fc01e14b30897a1ebbacf9934eErik model.mLocation = cursor.getString(EVENT_INDEX_EVENT_LOCATION); 101673714169d34a55fc01e14b30897a1ebbacf9934eErik model.mAllDay = cursor.getInt(EVENT_INDEX_ALL_DAY) != 0; 101773714169d34a55fc01e14b30897a1ebbacf9934eErik model.mHasAlarm = cursor.getInt(EVENT_INDEX_HAS_ALARM) != 0; 101873714169d34a55fc01e14b30897a1ebbacf9934eErik model.mCalendarId = cursor.getInt(EVENT_INDEX_CALENDAR_ID); 101973714169d34a55fc01e14b30897a1ebbacf9934eErik model.mStart = cursor.getLong(EVENT_INDEX_DTSTART); 102066a3c9ee58ae73a3a1ad3bdec79aa1566582465cRoboErik String tz = cursor.getString(EVENT_INDEX_TIMEZONE); 102166a3c9ee58ae73a3a1ad3bdec79aa1566582465cRoboErik if (!TextUtils.isEmpty(tz)) { 102266a3c9ee58ae73a3a1ad3bdec79aa1566582465cRoboErik model.mTimezone = tz; 102366a3c9ee58ae73a3a1ad3bdec79aa1566582465cRoboErik } 102473714169d34a55fc01e14b30897a1ebbacf9934eErik String rRule = cursor.getString(EVENT_INDEX_RRULE); 102573714169d34a55fc01e14b30897a1ebbacf9934eErik model.mRrule = rRule; 102673714169d34a55fc01e14b30897a1ebbacf9934eErik model.mSyncId = cursor.getString(EVENT_INDEX_SYNC_ID); 1027c6680c5819b6ad0d1914420902746fe5f86ba403RoboErik model.mAvailability = cursor.getInt(EVENT_INDEX_AVAILABILITY); 1028ced0eb67ef49c3203f72520c296e73d9cb4f654bMichael Chan int accessLevel = cursor.getInt(EVENT_INDEX_ACCESS_LEVEL); 102973714169d34a55fc01e14b30897a1ebbacf9934eErik model.mOwnerAccount = cursor.getString(EVENT_INDEX_OWNER_ACCOUNT); 103073714169d34a55fc01e14b30897a1ebbacf9934eErik model.mHasAttendeeData = cursor.getInt(EVENT_INDEX_HAS_ATTENDEE_DATA) != 0; 103128dab653f55caccbed32f700274f5274abaee089RoboErik model.mOriginalSyncId = cursor.getString(EVENT_INDEX_ORIGINAL_SYNC_ID); 103228dab653f55caccbed32f700274f5274abaee089RoboErik model.mOriginalId = cursor.getLong(EVENT_INDEX_ORIGINAL_ID); 1033352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan model.mOrganizer = cursor.getString(EVENT_INDEX_ORGANIZER); 1034352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan model.mIsOrganizer = model.mOwnerAccount.equalsIgnoreCase(model.mOrganizer); 1035352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan model.mGuestsCanModify = cursor.getInt(EVENT_INDEX_GUESTS_CAN_MODIFY) != 0; 103673714169d34a55fc01e14b30897a1ebbacf9934eErik 1037ced0eb67ef49c3203f72520c296e73d9cb4f654bMichael Chan if (accessLevel > 0) { 1038eca82e9468c002590045338f98588ad3ce1a0bb3Erik // For now the array contains the values 0, 2, and 3. We subtract 1039eca82e9468c002590045338f98588ad3ce1a0bb3Erik // one to make it easier to handle in code as 0,1,2. 104073714169d34a55fc01e14b30897a1ebbacf9934eErik // Default (0), Private (1), Public (2) 1041ced0eb67ef49c3203f72520c296e73d9cb4f654bMichael Chan accessLevel--; 104273714169d34a55fc01e14b30897a1ebbacf9934eErik } 1043ced0eb67ef49c3203f72520c296e73d9cb4f654bMichael Chan model.mAccessLevel = accessLevel; 1044e3f85db16cfc50b25b97c64d8ab99820d579e5deMichael Chan model.mEventStatus = cursor.getInt(EVENT_INDEX_EVENT_STATUS); 104573714169d34a55fc01e14b30897a1ebbacf9934eErik 104673714169d34a55fc01e14b30897a1ebbacf9934eErik boolean hasRRule = !TextUtils.isEmpty(rRule); 104773714169d34a55fc01e14b30897a1ebbacf9934eErik 104873714169d34a55fc01e14b30897a1ebbacf9934eErik // We expect only one of these, so ignore the other 104973714169d34a55fc01e14b30897a1ebbacf9934eErik if (hasRRule) { 105073714169d34a55fc01e14b30897a1ebbacf9934eErik model.mDuration = cursor.getString(EVENT_INDEX_DURATION); 105173714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 105273714169d34a55fc01e14b30897a1ebbacf9934eErik model.mEnd = cursor.getLong(EVENT_INDEX_DTEND); 105373714169d34a55fc01e14b30897a1ebbacf9934eErik } 1054352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1055352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan model.mModelUpdatedWithEventCursor = true; 1056352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1057352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1058352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan /** 1059352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * Uses a calendar cursor to fill in the given model This method assumes the 1060352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * cursor used {@link #CALENDARS_PROJECTION} as it's query projection. It uses 1061352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * the cursor to fill in the given model with all the information available. 1062352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * 1063352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * @param model The model to fill in 1064352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * @param cursor An event cursor that used {@link #CALENDARS_PROJECTION} for the query 1065352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * @return returns true if model was updated with the info in the cursor. 1066352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan */ 1067352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan public static boolean setModelFromCalendarCursor(CalendarEventModel model, Cursor cursor) { 1068352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (model == null || cursor == null) { 1069352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan Log.wtf(TAG, "Attempted to build non-existent model or from an incorrect query."); 1070352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1071352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1072352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1073352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (model.mCalendarId == -1) { 1074352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1075352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1076352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1077352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (!model.mModelUpdatedWithEventCursor) { 1078352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan Log.wtf(TAG, 1079352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan "Can't update model with a Calendar cursor until it has seen an Event cursor."); 1080352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1081352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1082352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1083352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan cursor.moveToPosition(-1); 1084352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan while (cursor.moveToNext()) { 1085ddbf80860566df8425f38a32190e9a64cad4e389Michael Chan if (model.mCalendarId != cursor.getInt(CALENDARS_INDEX_ID)) { 1086352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan continue; 1087352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1088352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1089950e8a615f3dcca0b61b2eb8f8de72ecf6825d4bAndy McFadden model.mOrganizerCanRespond = cursor.getInt(CALENDARS_INDEX_CAN_ORGANIZER_RESPOND) != 0; 1090352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1091352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan model.mCalendarAccessLevel = cursor.getInt(CALENDARS_INDEX_ACCESS_LEVEL); 10929d5f351e45e20ff04da6f9118031cc2cc7419905Michael Chan model.mCalendarDisplayName = cursor.getString(CALENDARS_INDEX_DISPLAY_NAME); 10933400a2bfc1e6be435ea2bb0f436a6ef338446504Erik model.mCalendarColor = cursor.getInt(CALENDARS_INDEX_COLOR); 1094352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 10959645d9c3e9b2cfd35c5fc89f41caf6e99153ba10Andy McFadden model.mCalendarMaxReminders = cursor.getInt(CALENDARS_INDEX_MAX_REMINDERS); 10969645d9c3e9b2cfd35c5fc89f41caf6e99153ba10Andy McFadden model.mCalendarAllowedReminders = cursor.getString(CALENDARS_INDEX_ALLOWED_REMINDERS); 1097c6680c5819b6ad0d1914420902746fe5f86ba403RoboErik model.mCalendarAllowedAttendeeTypes = cursor 1098c6680c5819b6ad0d1914420902746fe5f86ba403RoboErik .getString(CALENDARS_INDEX_ALLOWED_ATTENDEE_TYPES); 1099c6680c5819b6ad0d1914420902746fe5f86ba403RoboErik model.mCalendarAllowedAvailability = cursor 1100c6680c5819b6ad0d1914420902746fe5f86ba403RoboErik .getString(CALENDARS_INDEX_ALLOWED_AVAILABILITY); 11019645d9c3e9b2cfd35c5fc89f41caf6e99153ba10Andy McFadden 1102352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return true; 1103352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1104352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1105352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1106352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1107352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan public static boolean canModifyEvent(CalendarEventModel model) { 1108352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return canModifyCalendar(model) 1109352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan && (model.mIsOrganizer || model.mGuestsCanModify); 1110352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1111352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1112352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan public static boolean canModifyCalendar(CalendarEventModel model) { 1113ef2add9145f058ea103ade2870a8ba899b9ed1c9RoboErik return model.mCalendarAccessLevel >= Calendars.CAL_ACCESS_CONTRIBUTOR 1114352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan || model.mCalendarId == -1; 1115352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1116352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 11175f62079ab9daefe95ede87631766e99e8b4a2220Erik public static boolean canAddReminders(CalendarEventModel model) { 1118ef2add9145f058ea103ade2870a8ba899b9ed1c9RoboErik return model.mCalendarAccessLevel >= Calendars.CAL_ACCESS_READ; 11195f62079ab9daefe95ede87631766e99e8b4a2220Erik } 11205f62079ab9daefe95ede87631766e99e8b4a2220Erik 1121352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan public static boolean canRespond(CalendarEventModel model) { 1122352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // For non-organizers, write permission to the calendar is sufficient. 1123352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // For organizers, the user needs a) write permission to the calendar 1124352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // AND b) ownerCanRespond == true AND c) attendee data exist 1125352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // (this means num of attendees > 1, the calendar owner's and others). 1126352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // Note that mAttendeeList omits the organizer. 1127352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1128352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // (there are more cases involved to be 100% accurate, such as 1129352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // paying attention to whether or not an attendee status was 1130352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // included in the feed, but we're currently omitting those corner cases 1131352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // for simplicity). 1132352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1133352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (!canModifyCalendar(model)) { 1134352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1135352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1136352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1137352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (!model.mIsOrganizer) { 1138352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return true; 1139352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1140352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1141352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (!model.mOrganizerCanRespond) { 1142352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1143352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1144352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1145352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // This means we don't have the attendees data so we can't send 1146352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // the list of attendees and the status back to the server 1147352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (model.mHasAttendeeData && model.mAttendeesList.size() == 0) { 1148352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1149352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1150352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1151352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return true; 115273714169d34a55fc01e14b30897a1ebbacf9934eErik } 115373714169d34a55fc01e14b30897a1ebbacf9934eErik 115473714169d34a55fc01e14b30897a1ebbacf9934eErik /** 1155152cb249897e8adbc674b0d991db1c47e597e0ffAndy McFadden * Goes through an event model and fills in content values for saving. This 1156eca82e9468c002590045338f98588ad3ce1a0bb3Erik * method will perform the initial collection of values from the model and 1157eca82e9468c002590045338f98588ad3ce1a0bb3Erik * put them into a set of ContentValues. It performs some basic work such as 1158eca82e9468c002590045338f98588ad3ce1a0bb3Erik * fixing the time on allDay events and choosing whether to use an rrule or 1159eca82e9468c002590045338f98588ad3ce1a0bb3Erik * dtend. 116073714169d34a55fc01e14b30897a1ebbacf9934eErik * 116173714169d34a55fc01e14b30897a1ebbacf9934eErik * @param model The complete model of the event you want to save 116273714169d34a55fc01e14b30897a1ebbacf9934eErik * @return values 116373714169d34a55fc01e14b30897a1ebbacf9934eErik */ 116473714169d34a55fc01e14b30897a1ebbacf9934eErik ContentValues getContentValuesFromModel(CalendarEventModel model) { 116573714169d34a55fc01e14b30897a1ebbacf9934eErik String title = model.mTitle; 116673714169d34a55fc01e14b30897a1ebbacf9934eErik boolean isAllDay = model.mAllDay; 116773714169d34a55fc01e14b30897a1ebbacf9934eErik String rrule = model.mRrule; 1168eca82e9468c002590045338f98588ad3ce1a0bb3Erik String timezone = model.mTimezone; 116973714169d34a55fc01e14b30897a1ebbacf9934eErik if (timezone == null) { 117073714169d34a55fc01e14b30897a1ebbacf9934eErik timezone = TimeZone.getDefault().getID(); 117173714169d34a55fc01e14b30897a1ebbacf9934eErik } 117273714169d34a55fc01e14b30897a1ebbacf9934eErik Time startTime = new Time(timezone); 117373714169d34a55fc01e14b30897a1ebbacf9934eErik Time endTime = new Time(timezone); 117473714169d34a55fc01e14b30897a1ebbacf9934eErik 117573714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.set(model.mStart); 117673714169d34a55fc01e14b30897a1ebbacf9934eErik endTime.set(model.mEnd); 117773714169d34a55fc01e14b30897a1ebbacf9934eErik 117873714169d34a55fc01e14b30897a1ebbacf9934eErik ContentValues values = new ContentValues(); 117973714169d34a55fc01e14b30897a1ebbacf9934eErik 118073714169d34a55fc01e14b30897a1ebbacf9934eErik long startMillis; 118173714169d34a55fc01e14b30897a1ebbacf9934eErik long endMillis; 118273714169d34a55fc01e14b30897a1ebbacf9934eErik long calendarId = model.mCalendarId; 118373714169d34a55fc01e14b30897a1ebbacf9934eErik if (isAllDay) { 118473714169d34a55fc01e14b30897a1ebbacf9934eErik // Reset start and end time, ensure at least 1 day duration, and set 118573714169d34a55fc01e14b30897a1ebbacf9934eErik // the timezone to UTC, as required for all-day events. 118673714169d34a55fc01e14b30897a1ebbacf9934eErik timezone = Time.TIMEZONE_UTC; 118773714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.hour = 0; 118873714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.minute = 0; 118973714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.second = 0; 119073714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.timezone = timezone; 119173714169d34a55fc01e14b30897a1ebbacf9934eErik startMillis = startTime.normalize(true); 119273714169d34a55fc01e14b30897a1ebbacf9934eErik 119373714169d34a55fc01e14b30897a1ebbacf9934eErik endTime.hour = 0; 119473714169d34a55fc01e14b30897a1ebbacf9934eErik endTime.minute = 0; 119573714169d34a55fc01e14b30897a1ebbacf9934eErik endTime.second = 0; 119673714169d34a55fc01e14b30897a1ebbacf9934eErik endTime.timezone = timezone; 119773714169d34a55fc01e14b30897a1ebbacf9934eErik endMillis = endTime.normalize(true); 119829190975b9238dd6841f822f82f2fb83b0557f36Daisuke Miyakawa if (endMillis < startMillis + DateUtils.DAY_IN_MILLIS) { 119929190975b9238dd6841f822f82f2fb83b0557f36Daisuke Miyakawa // EditEventView#fillModelFromUI() should treat this case, but we want to ensure 120029190975b9238dd6841f822f82f2fb83b0557f36Daisuke Miyakawa // the condition anyway. 120129190975b9238dd6841f822f82f2fb83b0557f36Daisuke Miyakawa endMillis = startMillis + DateUtils.DAY_IN_MILLIS; 120229190975b9238dd6841f822f82f2fb83b0557f36Daisuke Miyakawa } 120373714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 120473714169d34a55fc01e14b30897a1ebbacf9934eErik startMillis = startTime.toMillis(true); 120573714169d34a55fc01e14b30897a1ebbacf9934eErik endMillis = endTime.toMillis(true); 120673714169d34a55fc01e14b30897a1ebbacf9934eErik } 120773714169d34a55fc01e14b30897a1ebbacf9934eErik 120873714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.CALENDAR_ID, calendarId); 120973714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.EVENT_TIMEZONE, timezone); 121073714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.TITLE, title); 121173714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.ALL_DAY, isAllDay ? 1 : 0); 121273714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.DTSTART, startMillis); 121373714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.RRULE, rrule); 121466b2067e0a389180f909eb0a528bea5f6becca23Michael Chan if (!TextUtils.isEmpty(rrule)) { 121573714169d34a55fc01e14b30897a1ebbacf9934eErik addRecurrenceRule(values, model); 121673714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 1217eca82e9468c002590045338f98588ad3ce1a0bb3Erik values.put(Events.DURATION, (String) null); 121873714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.DTEND, endMillis); 121973714169d34a55fc01e14b30897a1ebbacf9934eErik } 1220bf7c96b7adbae17d822cb8dec45fdf253a16df51Michael Chan if (model.mDescription != null) { 1221bf7c96b7adbae17d822cb8dec45fdf253a16df51Michael Chan values.put(Events.DESCRIPTION, model.mDescription.trim()); 122211bd6bec90cba2e51ed4e0e0e4ed1fdacf4fea04RoboErik } else { 122311bd6bec90cba2e51ed4e0e0e4ed1fdacf4fea04RoboErik values.put(Events.DESCRIPTION, (String) null); 1224bf7c96b7adbae17d822cb8dec45fdf253a16df51Michael Chan } 1225bf7c96b7adbae17d822cb8dec45fdf253a16df51Michael Chan if (model.mLocation != null) { 1226bf7c96b7adbae17d822cb8dec45fdf253a16df51Michael Chan values.put(Events.EVENT_LOCATION, model.mLocation.trim()); 122711bd6bec90cba2e51ed4e0e0e4ed1fdacf4fea04RoboErik } else { 122811bd6bec90cba2e51ed4e0e0e4ed1fdacf4fea04RoboErik values.put(Events.EVENT_LOCATION, (String) null); 1229bf7c96b7adbae17d822cb8dec45fdf253a16df51Michael Chan } 1230c6680c5819b6ad0d1914420902746fe5f86ba403RoboErik values.put(Events.AVAILABILITY, model.mAvailability); 123188c483a0189cbe7132330cb1b563f07326f3bcf9Erik values.put(Events.HAS_ATTENDEE_DATA, model.mHasAttendeeData ? 1 : 0); 123273714169d34a55fc01e14b30897a1ebbacf9934eErik 1233ced0eb67ef49c3203f72520c296e73d9cb4f654bMichael Chan int accessLevel = model.mAccessLevel; 1234ced0eb67ef49c3203f72520c296e73d9cb4f654bMichael Chan if (accessLevel > 0) { 123573714169d34a55fc01e14b30897a1ebbacf9934eErik // For now the array contains the values 0, 2, and 3. We add one to match. 123673714169d34a55fc01e14b30897a1ebbacf9934eErik // Default (0), Private (2), Public (3) 1237ced0eb67ef49c3203f72520c296e73d9cb4f654bMichael Chan accessLevel++; 123873714169d34a55fc01e14b30897a1ebbacf9934eErik } 1239ced0eb67ef49c3203f72520c296e73d9cb4f654bMichael Chan values.put(Events.ACCESS_LEVEL, accessLevel); 1240e3f85db16cfc50b25b97c64d8ab99820d579e5deMichael Chan values.put(Events.STATUS, model.mEventStatus); 124173714169d34a55fc01e14b30897a1ebbacf9934eErik 124273714169d34a55fc01e14b30897a1ebbacf9934eErik return values; 124373714169d34a55fc01e14b30897a1ebbacf9934eErik } 124473714169d34a55fc01e14b30897a1ebbacf9934eErik 124573714169d34a55fc01e14b30897a1ebbacf9934eErik /** 124673714169d34a55fc01e14b30897a1ebbacf9934eErik * Takes an e-mail address and returns the domain (everything after the last @) 124773714169d34a55fc01e14b30897a1ebbacf9934eErik */ 124873714169d34a55fc01e14b30897a1ebbacf9934eErik public static String extractDomain(String email) { 124973714169d34a55fc01e14b30897a1ebbacf9934eErik int separator = email.lastIndexOf('@'); 125073714169d34a55fc01e14b30897a1ebbacf9934eErik if (separator != -1 && ++separator < email.length()) { 125173714169d34a55fc01e14b30897a1ebbacf9934eErik return email.substring(separator); 125273714169d34a55fc01e14b30897a1ebbacf9934eErik } 125373714169d34a55fc01e14b30897a1ebbacf9934eErik return null; 125473714169d34a55fc01e14b30897a1ebbacf9934eErik } 125579f228124de7d98146ca526d743436f6419e2365Erik 125679f228124de7d98146ca526d743436f6419e2365Erik public interface EditDoneRunnable extends Runnable { 125779f228124de7d98146ca526d743436f6419e2365Erik public void setDoneCode(int code); 125879f228124de7d98146ca526d743436f6419e2365Erik } 125973714169d34a55fc01e14b30897a1ebbacf9934eErik} 1260