EditEventHelper.java revision 352e1a2f834460bd54e03ce94cadd36a46debacb
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 19fbce65e53c7a111955f638db5bf8bee35381e5b7Erikimport com.android.calendar.AsyncQueryService; 20fbce65e53c7a111955f638db5bf8bee35381e5b7Erikimport com.android.calendar.CalendarEventModel; 21d845fbe558d5229102b58cce70a1b29fe6cb6967Erikimport com.android.calendar.CalendarEventModel.Attendee; 22fbce65e53c7a111955f638db5bf8bee35381e5b7Erikimport com.android.calendar.R; 23fbce65e53c7a111955f638db5bf8bee35381e5b7Erikimport com.android.calendar.Utils; 2473714169d34a55fc01e14b30897a1ebbacf9934eErikimport com.android.common.Rfc822Validator; 2573714169d34a55fc01e14b30897a1ebbacf9934eErik 26eca82e9468c002590045338f98588ad3ce1a0bb3Erikimport android.content.ContentProviderOperation; 27eca82e9468c002590045338f98588ad3ce1a0bb3Erikimport android.content.ContentUris; 28eca82e9468c002590045338f98588ad3ce1a0bb3Erikimport android.content.ContentValues; 29dd95df57c8c5a58a85c4c0effad5652dec14f621Erikimport android.content.Context; 3073714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.content.res.Resources; 3173714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.database.Cursor; 32d845fbe558d5229102b58cce70a1b29fe6cb6967Erikimport android.graphics.drawable.Drawable; 3373714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.net.Uri; 3473714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.pim.EventRecurrence; 3573714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.provider.Calendar.Attendees; 36eca82e9468c002590045338f98588ad3ce1a0bb3Erikimport android.provider.Calendar.Calendars; 3773714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.provider.Calendar.Events; 3873714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.provider.Calendar.Reminders; 3973714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.text.TextUtils; 4073714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.text.format.DateUtils; 4173714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.text.format.Time; 4273714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.text.util.Rfc822Token; 4373714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.text.util.Rfc822Tokenizer; 4473714169d34a55fc01e14b30897a1ebbacf9934eErikimport android.util.Log; 45352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chanimport android.view.View; 46d845fbe558d5229102b58cce70a1b29fe6cb6967Erikimport android.widget.ImageView; 47d845fbe558d5229102b58cce70a1b29fe6cb6967Erikimport android.widget.QuickContactBadge; 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 61eca82e9468c002590045338f98588ad3ce1a0bb3Erik public static final int MAX_REMINDERS = 5; 62eca82e9468c002590045338f98588ad3ce1a0bb3Erik 63fbce65e53c7a111955f638db5bf8bee35381e5b7Erik public static final String[] EVENT_PROJECTION = new String[] { 6473714169d34a55fc01e14b30897a1ebbacf9934eErik Events._ID, // 0 6573714169d34a55fc01e14b30897a1ebbacf9934eErik Events.TITLE, // 1 6673714169d34a55fc01e14b30897a1ebbacf9934eErik Events.DESCRIPTION, // 2 6773714169d34a55fc01e14b30897a1ebbacf9934eErik Events.EVENT_LOCATION, // 3 6873714169d34a55fc01e14b30897a1ebbacf9934eErik Events.ALL_DAY, // 4 6973714169d34a55fc01e14b30897a1ebbacf9934eErik Events.HAS_ALARM, // 5 7073714169d34a55fc01e14b30897a1ebbacf9934eErik Events.CALENDAR_ID, // 6 7173714169d34a55fc01e14b30897a1ebbacf9934eErik Events.DTSTART, // 7 7273714169d34a55fc01e14b30897a1ebbacf9934eErik Events.DTEND, // 8 7373714169d34a55fc01e14b30897a1ebbacf9934eErik Events.DURATION, // 9 7473714169d34a55fc01e14b30897a1ebbacf9934eErik Events.EVENT_TIMEZONE, // 10 7573714169d34a55fc01e14b30897a1ebbacf9934eErik Events.RRULE, // 11 7673714169d34a55fc01e14b30897a1ebbacf9934eErik Events._SYNC_ID, // 12 7773714169d34a55fc01e14b30897a1ebbacf9934eErik Events.TRANSPARENCY, // 13 7873714169d34a55fc01e14b30897a1ebbacf9934eErik Events.VISIBILITY, // 14 7973714169d34a55fc01e14b30897a1ebbacf9934eErik Events.OWNER_ACCOUNT, // 15 8073714169d34a55fc01e14b30897a1ebbacf9934eErik Events.HAS_ATTENDEE_DATA, // 16 81eca82e9468c002590045338f98588ad3ce1a0bb3Erik Events.ORIGINAL_EVENT, // 17 82352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan Events.ORGANIZER, // 18 83352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan Events.GUESTS_CAN_MODIFY, // 19 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; 9873714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_TRANSPARENCY = 13; 9973714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_VISIBILITY = 14; 10073714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_OWNER_ACCOUNT = 15; 10173714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int EVENT_INDEX_HAS_ATTENDEE_DATA = 16; 102eca82e9468c002590045338f98588ad3ce1a0bb3Erik protected static final int EVENT_INDEX_ORIGINAL_EVENT = 17; 103352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan protected static final int EVENT_INDEX_ORGANIZER = 18; 104352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan protected static final int EVENT_INDEX_GUESTS_CAN_MODIFY = 19; 10573714169d34a55fc01e14b30897a1ebbacf9934eErik 106eca82e9468c002590045338f98588ad3ce1a0bb3Erik public static final String[] REMINDERS_PROJECTION = new String[] { 10773714169d34a55fc01e14b30897a1ebbacf9934eErik Reminders._ID, // 0 10873714169d34a55fc01e14b30897a1ebbacf9934eErik Reminders.MINUTES, // 1 109eca82e9468c002590045338f98588ad3ce1a0bb3Erik Reminders.METHOD, // 2 11073714169d34a55fc01e14b30897a1ebbacf9934eErik }; 111eca82e9468c002590045338f98588ad3ce1a0bb3Erik public static final int REMINDERS_INDEX_MINUTES = 1; 112eca82e9468c002590045338f98588ad3ce1a0bb3Erik public static final int REMINDERS_INDEX_METHOD = 2; 113eca82e9468c002590045338f98588ad3ce1a0bb3Erik public static final String REMINDERS_WHERE = Reminders.EVENT_ID + "=? AND (" + Reminders.METHOD 114eca82e9468c002590045338f98588ad3ce1a0bb3Erik + "=?" + " OR " + Reminders.METHOD + "=?" + ")"; 115eca82e9468c002590045338f98588ad3ce1a0bb3Erik 11673714169d34a55fc01e14b30897a1ebbacf9934eErik // Visible for testing 117eca82e9468c002590045338f98588ad3ce1a0bb3Erik static final String ATTENDEES_DELETE_PREFIX = Attendees.EVENT_ID + "=? AND " 118eca82e9468c002590045338f98588ad3ce1a0bb3Erik + Attendees.ATTENDEE_EMAIL + " IN ("; 11973714169d34a55fc01e14b30897a1ebbacf9934eErik 12073714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int DOES_NOT_REPEAT = 0; 12173714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_DAILY = 1; 12273714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_EVERY_WEEKDAY = 2; 12373714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_WEEKLY_ON_DAY = 3; 12473714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_MONTHLY_ON_DAY_COUNT = 4; 12573714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_MONTHLY_ON_DAY = 5; 12673714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_YEARLY = 6; 12773714169d34a55fc01e14b30897a1ebbacf9934eErik public static final int REPEATS_CUSTOM = 7; 12873714169d34a55fc01e14b30897a1ebbacf9934eErik 12973714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int MODIFY_UNINITIALIZED = 0; 13073714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int MODIFY_SELECTED = 1; 1318d69cd014d912e38d2ab37764aaf4aa7700c078bErik protected static final int MODIFY_ALL_FOLLOWING = 2; 1328d69cd014d912e38d2ab37764aaf4aa7700c078bErik protected static final int MODIFY_ALL = 3; 13373714169d34a55fc01e14b30897a1ebbacf9934eErik 13473714169d34a55fc01e14b30897a1ebbacf9934eErik protected static final int DAY_IN_SECONDS = 24 * 60 * 60; 13573714169d34a55fc01e14b30897a1ebbacf9934eErik 136dd95df57c8c5a58a85c4c0effad5652dec14f621Erik private Context mContext; 13737960c091b5912dd45beb561a85a9064ea42e616Erik private AsyncQueryService mService; 138eca82e9468c002590045338f98588ad3ce1a0bb3Erik 139eca82e9468c002590045338f98588ad3ce1a0bb3Erik // public int mModification; 14073714169d34a55fc01e14b30897a1ebbacf9934eErik private Rfc822Validator mEmailValidator; 141eca82e9468c002590045338f98588ad3ce1a0bb3Erik 142eca82e9468c002590045338f98588ad3ce1a0bb3Erik // This allows us to flag the event if something is wrong with it, right now 143eca82e9468c002590045338f98588ad3ce1a0bb3Erik // if an uri is provided for an event that doesn't exist in the db. 14473714169d34a55fc01e14b30897a1ebbacf9934eErik protected boolean mEventOk = true; 14573714169d34a55fc01e14b30897a1ebbacf9934eErik 146d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public static final int ATTENDEE_NO_RESPONSE = -1; 147d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public static final int ATTENDEE_ID_NONE = -1; 148d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public static final int[] ATTENDEE_VALUES = { 149d845fbe558d5229102b58cce70a1b29fe6cb6967Erik ATTENDEE_NO_RESPONSE, 150d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Attendees.ATTENDEE_STATUS_ACCEPTED, 151d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Attendees.ATTENDEE_STATUS_TENTATIVE, 152d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Attendees.ATTENDEE_STATUS_DECLINED, 153d845fbe558d5229102b58cce70a1b29fe6cb6967Erik }; 154d845fbe558d5229102b58cce70a1b29fe6cb6967Erik 155eca82e9468c002590045338f98588ad3ce1a0bb3Erik /** 156eca82e9468c002590045338f98588ad3ce1a0bb3Erik * This is the symbolic name for the key used to pass in the boolean for 157eca82e9468c002590045338f98588ad3ce1a0bb3Erik * creating all-day events that is part of the extra data of the intent. 158eca82e9468c002590045338f98588ad3ce1a0bb3Erik * This is used only for creating new events and is set to true if the 159eca82e9468c002590045338f98588ad3ce1a0bb3Erik * default for the new event should be an all-day event. 160eca82e9468c002590045338f98588ad3ce1a0bb3Erik */ 161eca82e9468c002590045338f98588ad3ce1a0bb3Erik public static final String EVENT_ALL_DAY = "allDay"; 162eca82e9468c002590045338f98588ad3ce1a0bb3Erik 163eca82e9468c002590045338f98588ad3ce1a0bb3Erik static final String[] CALENDARS_PROJECTION = new String[] { 164eca82e9468c002590045338f98588ad3ce1a0bb3Erik Calendars._ID, // 0 165eca82e9468c002590045338f98588ad3ce1a0bb3Erik Calendars.DISPLAY_NAME, // 1 166eca82e9468c002590045338f98588ad3ce1a0bb3Erik Calendars.OWNER_ACCOUNT, // 2 167eca82e9468c002590045338f98588ad3ce1a0bb3Erik Calendars.COLOR, // 3 168352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan Calendars.ORGANIZER_CAN_RESPOND, // 4 169352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan Calendars.ACCESS_LEVEL, // 5 170352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan Calendars.SELECTED, // 6 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; 176352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan static final int CALENDARS_INDEX_ORGANIZER_CAN_RESPOND = 4; 177352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan static final int CALENDARS_INDEX_ACCESS_LEVEL = 5; 178352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan static final int CALENDARS_INDEX_SELECTED = 6; 179eca82e9468c002590045338f98588ad3ce1a0bb3Erik 180eca82e9468c002590045338f98588ad3ce1a0bb3Erik static final String CALENDARS_WHERE_WRITEABLE_VISIBLE = Calendars.ACCESS_LEVEL + ">=" 181eca82e9468c002590045338f98588ad3ce1a0bb3Erik + Calendars.CONTRIBUTOR_ACCESS + " AND " + Calendars.SELECTED + "=1"; 182eca82e9468c002590045338f98588ad3ce1a0bb3Erik 183352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan static final String CALENDARS_WHERE = Calendars._ID + "=?"; 184352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 185eca82e9468c002590045338f98588ad3ce1a0bb3Erik static final String[] ATTENDEES_PROJECTION = new String[] { 186d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Attendees._ID, // 0 187d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Attendees.ATTENDEE_NAME, // 1 188d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Attendees.ATTENDEE_EMAIL, // 2 189d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Attendees.ATTENDEE_RELATIONSHIP, // 3 190d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Attendees.ATTENDEE_STATUS, // 4 191eca82e9468c002590045338f98588ad3ce1a0bb3Erik }; 192d845fbe558d5229102b58cce70a1b29fe6cb6967Erik static final int ATTENDEES_INDEX_ID = 0; 193d845fbe558d5229102b58cce70a1b29fe6cb6967Erik static final int ATTENDEES_INDEX_NAME = 1; 194d845fbe558d5229102b58cce70a1b29fe6cb6967Erik static final int ATTENDEES_INDEX_EMAIL = 2; 195d845fbe558d5229102b58cce70a1b29fe6cb6967Erik static final int ATTENDEES_INDEX_RELATIONSHIP = 3; 196d845fbe558d5229102b58cce70a1b29fe6cb6967Erik static final int ATTENDEES_INDEX_STATUS = 4; 197eca82e9468c002590045338f98588ad3ce1a0bb3Erik static final String ATTENDEES_WHERE_NOT_ORGANIZER = Attendees.EVENT_ID + "=? AND " 198eca82e9468c002590045338f98588ad3ce1a0bb3Erik + Attendees.ATTENDEE_RELATIONSHIP + "<>" + Attendees.RELATIONSHIP_ORGANIZER; 199d845fbe558d5229102b58cce70a1b29fe6cb6967Erik static final String ATTENDEES_WHERE = Attendees.EVENT_ID + "=?"; 200d845fbe558d5229102b58cce70a1b29fe6cb6967Erik 201d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public static class ContactViewHolder { 202d845fbe558d5229102b58cce70a1b29fe6cb6967Erik QuickContactBadge badge; 203d845fbe558d5229102b58cce70a1b29fe6cb6967Erik ImageView presence; 204d845fbe558d5229102b58cce70a1b29fe6cb6967Erik int updateCounts; 205d845fbe558d5229102b58cce70a1b29fe6cb6967Erik } 206d845fbe558d5229102b58cce70a1b29fe6cb6967Erik 207d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public static class AttendeeItem { 208d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public boolean mRemoved; 209d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public boolean mDivider; 210d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public String mDividerLabel; 211d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public Attendee mAttendee; 212d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public Drawable mBadge; 213d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public int mPresence; 214d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public int mUpdateCounts; 215d845fbe558d5229102b58cce70a1b29fe6cb6967Erik } 216eca82e9468c002590045338f98588ad3ce1a0bb3Erik 217dd95df57c8c5a58a85c4c0effad5652dec14f621Erik public EditEventHelper(Context context, CalendarEventModel model) { 218dd95df57c8c5a58a85c4c0effad5652dec14f621Erik mContext = context; 219dd95df57c8c5a58a85c4c0effad5652dec14f621Erik mService = new AsyncQueryService(context); 2206ae7d69b90f22fd51ab677036ba987dbdc35a62dErik setDomainFromModel(model); 2216ae7d69b90f22fd51ab677036ba987dbdc35a62dErik } 2226ae7d69b90f22fd51ab677036ba987dbdc35a62dErik 223eca82e9468c002590045338f98588ad3ce1a0bb3Erik // Sets up the email validator for the given model 2246ae7d69b90f22fd51ab677036ba987dbdc35a62dErik public void setDomainFromModel(CalendarEventModel model) { 22573714169d34a55fc01e14b30897a1ebbacf9934eErik String domain = "gmail.com"; 226eca82e9468c002590045338f98588ad3ce1a0bb3Erik if (model != null) { 22773714169d34a55fc01e14b30897a1ebbacf9934eErik String ownerAccount = model.mOwnerAccount; 22873714169d34a55fc01e14b30897a1ebbacf9934eErik if (!TextUtils.isEmpty(ownerAccount)) { 22973714169d34a55fc01e14b30897a1ebbacf9934eErik String ownerDomain = extractDomain(ownerAccount); 23073714169d34a55fc01e14b30897a1ebbacf9934eErik if (!TextUtils.isEmpty(ownerDomain)) { 23173714169d34a55fc01e14b30897a1ebbacf9934eErik domain = ownerDomain; 23273714169d34a55fc01e14b30897a1ebbacf9934eErik } 23373714169d34a55fc01e14b30897a1ebbacf9934eErik } 23473714169d34a55fc01e14b30897a1ebbacf9934eErik } 23573714169d34a55fc01e14b30897a1ebbacf9934eErik mEmailValidator = new Rfc822Validator(domain); 23673714169d34a55fc01e14b30897a1ebbacf9934eErik } 23773714169d34a55fc01e14b30897a1ebbacf9934eErik 23873714169d34a55fc01e14b30897a1ebbacf9934eErik /** 239eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Saves the event. Returns true if the event was successfully saved, false 240eca82e9468c002590045338f98588ad3ce1a0bb3Erik * otherwise. 24173714169d34a55fc01e14b30897a1ebbacf9934eErik * 24273714169d34a55fc01e14b30897a1ebbacf9934eErik * @param model The event model to save 24373714169d34a55fc01e14b30897a1ebbacf9934eErik * @param originalModel A model of the original event if it exists 24473714169d34a55fc01e14b30897a1ebbacf9934eErik * @param modifyWhich For recurring events which type of series modification to use 24573714169d34a55fc01e14b30897a1ebbacf9934eErik * @return true if the event was successfully queued for saving 24673714169d34a55fc01e14b30897a1ebbacf9934eErik */ 24773714169d34a55fc01e14b30897a1ebbacf9934eErik public boolean saveEvent(CalendarEventModel model, CalendarEventModel originalModel, 24873714169d34a55fc01e14b30897a1ebbacf9934eErik int modifyWhich) { 24973714169d34a55fc01e14b30897a1ebbacf9934eErik boolean forceSaveReminders = false; 25073714169d34a55fc01e14b30897a1ebbacf9934eErik 25173714169d34a55fc01e14b30897a1ebbacf9934eErik if (DEBUG) { 25273714169d34a55fc01e14b30897a1ebbacf9934eErik Log.d(TAG, "Saving event model: " + model); 25373714169d34a55fc01e14b30897a1ebbacf9934eErik } 25473714169d34a55fc01e14b30897a1ebbacf9934eErik 25573714169d34a55fc01e14b30897a1ebbacf9934eErik if (!mEventOk) { 25673714169d34a55fc01e14b30897a1ebbacf9934eErik if (DEBUG) { 25773714169d34a55fc01e14b30897a1ebbacf9934eErik Log.w(TAG, "Event no longer exists. Event was not saved."); 25873714169d34a55fc01e14b30897a1ebbacf9934eErik } 25973714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 26073714169d34a55fc01e14b30897a1ebbacf9934eErik } 26173714169d34a55fc01e14b30897a1ebbacf9934eErik 26273714169d34a55fc01e14b30897a1ebbacf9934eErik // It's a problem if we try to save a non-existent or invalid model or if we're 26373714169d34a55fc01e14b30897a1ebbacf9934eErik // modifying an existing event and we have the wrong original model 26473714169d34a55fc01e14b30897a1ebbacf9934eErik if (model == null) { 26573714169d34a55fc01e14b30897a1ebbacf9934eErik Log.e(TAG, "Attempted to save null model."); 26673714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 26773714169d34a55fc01e14b30897a1ebbacf9934eErik } 26873714169d34a55fc01e14b30897a1ebbacf9934eErik if (!model.isValid()) { 26973714169d34a55fc01e14b30897a1ebbacf9934eErik Log.e(TAG, "Attempted to save invalid model."); 27073714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 27173714169d34a55fc01e14b30897a1ebbacf9934eErik } 27273714169d34a55fc01e14b30897a1ebbacf9934eErik if (originalModel != null && !isSameEvent(model, originalModel)) { 273eca82e9468c002590045338f98588ad3ce1a0bb3Erik Log.e(TAG, "Attempted to update existing event but models didn't refer to the same " 274eca82e9468c002590045338f98588ad3ce1a0bb3Erik + "event."); 27573714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 27673714169d34a55fc01e14b30897a1ebbacf9934eErik } 27773714169d34a55fc01e14b30897a1ebbacf9934eErik 278eca82e9468c002590045338f98588ad3ce1a0bb3Erik // TODO Check if anything has been changed and return false if it 279eca82e9468c002590045338f98588ad3ce1a0bb3Erik // hasn't. 28073714169d34a55fc01e14b30897a1ebbacf9934eErik 28173714169d34a55fc01e14b30897a1ebbacf9934eErik ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 28273714169d34a55fc01e14b30897a1ebbacf9934eErik int eventIdIndex = -1; 28373714169d34a55fc01e14b30897a1ebbacf9934eErik 28473714169d34a55fc01e14b30897a1ebbacf9934eErik ContentValues values = getContentValuesFromModel(model); 28573714169d34a55fc01e14b30897a1ebbacf9934eErik Uri uri = model.mUri; 28673714169d34a55fc01e14b30897a1ebbacf9934eErik 28773714169d34a55fc01e14b30897a1ebbacf9934eErik if (uri != null && originalModel == null) { 28873714169d34a55fc01e14b30897a1ebbacf9934eErik Log.e(TAG, "Existing event but no originalModel provided. Aborting save."); 28973714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 29073714169d34a55fc01e14b30897a1ebbacf9934eErik } 29173714169d34a55fc01e14b30897a1ebbacf9934eErik 29273714169d34a55fc01e14b30897a1ebbacf9934eErik // Update the "hasAlarm" field for the event 29373714169d34a55fc01e14b30897a1ebbacf9934eErik ArrayList<Integer> reminderMinutes = model.mReminderMinutes; 29473714169d34a55fc01e14b30897a1ebbacf9934eErik int len = reminderMinutes.size(); 29573714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.HAS_ALARM, (len > 0) ? 1 : 0); 29673714169d34a55fc01e14b30897a1ebbacf9934eErik 29773714169d34a55fc01e14b30897a1ebbacf9934eErik if (uri == null) { 29873714169d34a55fc01e14b30897a1ebbacf9934eErik // Add hasAttendeeData for a new event 29973714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.HAS_ATTENDEE_DATA, 1); 30073714169d34a55fc01e14b30897a1ebbacf9934eErik eventIdIndex = ops.size(); 301eca82e9468c002590045338f98588ad3ce1a0bb3Erik ContentProviderOperation.Builder b = ContentProviderOperation.newInsert( 302eca82e9468c002590045338f98588ad3ce1a0bb3Erik Events.CONTENT_URI).withValues(values); 30373714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 30473714169d34a55fc01e14b30897a1ebbacf9934eErik forceSaveReminders = true; 30573714169d34a55fc01e14b30897a1ebbacf9934eErik 30673714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (model.mRrule == null && originalModel.mRrule == null) { 30773714169d34a55fc01e14b30897a1ebbacf9934eErik // Simple update to a non-recurring event 30873714169d34a55fc01e14b30897a1ebbacf9934eErik checkTimeDependentFields(originalModel, model, values, modifyWhich); 30973714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build()); 31073714169d34a55fc01e14b30897a1ebbacf9934eErik 31173714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (originalModel.mRrule == null) { 31273714169d34a55fc01e14b30897a1ebbacf9934eErik // This event was changed from a non-repeating event to a 31373714169d34a55fc01e14b30897a1ebbacf9934eErik // repeating event. 31473714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build()); 31573714169d34a55fc01e14b30897a1ebbacf9934eErik 31673714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (modifyWhich == MODIFY_SELECTED) { 31773714169d34a55fc01e14b30897a1ebbacf9934eErik // Modify contents of the current instance of repeating event 31873714169d34a55fc01e14b30897a1ebbacf9934eErik // Create a recurrence exception 31973714169d34a55fc01e14b30897a1ebbacf9934eErik long begin = model.mOriginalStart; 32073714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.ORIGINAL_EVENT, originalModel.mSyncId); 32173714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.ORIGINAL_INSTANCE_TIME, begin); 32273714169d34a55fc01e14b30897a1ebbacf9934eErik boolean allDay = originalModel.mAllDay; 32373714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.ORIGINAL_ALL_DAY, allDay ? 1 : 0); 32473714169d34a55fc01e14b30897a1ebbacf9934eErik 32573714169d34a55fc01e14b30897a1ebbacf9934eErik eventIdIndex = ops.size(); 326eca82e9468c002590045338f98588ad3ce1a0bb3Erik ContentProviderOperation.Builder b = ContentProviderOperation.newInsert( 327eca82e9468c002590045338f98588ad3ce1a0bb3Erik Events.CONTENT_URI).withValues(values); 32873714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 32973714169d34a55fc01e14b30897a1ebbacf9934eErik forceSaveReminders = true; 33073714169d34a55fc01e14b30897a1ebbacf9934eErik 33173714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (modifyWhich == MODIFY_ALL_FOLLOWING) { 33273714169d34a55fc01e14b30897a1ebbacf9934eErik 33373714169d34a55fc01e14b30897a1ebbacf9934eErik if (model.mRrule == null) { 33473714169d34a55fc01e14b30897a1ebbacf9934eErik // We've changed a recurring event to a non-recurring event. 33573714169d34a55fc01e14b30897a1ebbacf9934eErik // If the event we are editing is the first in the series, 336eca82e9468c002590045338f98588ad3ce1a0bb3Erik // then delete the whole series. Otherwise, update the series 33773714169d34a55fc01e14b30897a1ebbacf9934eErik // to end at the new start time. 33873714169d34a55fc01e14b30897a1ebbacf9934eErik if (isFirstEventInSeries(model, originalModel)) { 33973714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(ContentProviderOperation.newDelete(uri).build()); 34073714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 34173714169d34a55fc01e14b30897a1ebbacf9934eErik // Update the current repeating event to end at the new 34273714169d34a55fc01e14b30897a1ebbacf9934eErik // start time. 34373714169d34a55fc01e14b30897a1ebbacf9934eErik updatePastEvents(ops, originalModel, model.mOriginalStart); 34473714169d34a55fc01e14b30897a1ebbacf9934eErik } 34573714169d34a55fc01e14b30897a1ebbacf9934eErik eventIdIndex = ops.size(); 346eca82e9468c002590045338f98588ad3ce1a0bb3Erik ops.add(ContentProviderOperation.newInsert(Events.CONTENT_URI).withValues(values) 34773714169d34a55fc01e14b30897a1ebbacf9934eErik .build()); 34873714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 34973714169d34a55fc01e14b30897a1ebbacf9934eErik if (isFirstEventInSeries(model, originalModel)) { 35073714169d34a55fc01e14b30897a1ebbacf9934eErik checkTimeDependentFields(originalModel, model, values, modifyWhich); 351eca82e9468c002590045338f98588ad3ce1a0bb3Erik ContentProviderOperation.Builder b = ContentProviderOperation.newUpdate(uri) 35273714169d34a55fc01e14b30897a1ebbacf9934eErik .withValues(values); 35373714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 35473714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 35573714169d34a55fc01e14b30897a1ebbacf9934eErik // Update the current repeating event to end at the new 35673714169d34a55fc01e14b30897a1ebbacf9934eErik // start time. 35773714169d34a55fc01e14b30897a1ebbacf9934eErik updatePastEvents(ops, originalModel, model.mOriginalStart); 35873714169d34a55fc01e14b30897a1ebbacf9934eErik 35973714169d34a55fc01e14b30897a1ebbacf9934eErik // Create a new event with the user-modified fields 36073714169d34a55fc01e14b30897a1ebbacf9934eErik eventIdIndex = ops.size(); 361eca82e9468c002590045338f98588ad3ce1a0bb3Erik ops.add(ContentProviderOperation.newInsert(Events.CONTENT_URI).withValues( 362eca82e9468c002590045338f98588ad3ce1a0bb3Erik values).build()); 36373714169d34a55fc01e14b30897a1ebbacf9934eErik } 36473714169d34a55fc01e14b30897a1ebbacf9934eErik } 36573714169d34a55fc01e14b30897a1ebbacf9934eErik forceSaveReminders = true; 36673714169d34a55fc01e14b30897a1ebbacf9934eErik 36773714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (modifyWhich == MODIFY_ALL) { 36873714169d34a55fc01e14b30897a1ebbacf9934eErik 36973714169d34a55fc01e14b30897a1ebbacf9934eErik // Modify all instances of repeating event 37073714169d34a55fc01e14b30897a1ebbacf9934eErik if (model.mRrule == null) { 37173714169d34a55fc01e14b30897a1ebbacf9934eErik // We've changed a recurring event to a non-recurring event. 37273714169d34a55fc01e14b30897a1ebbacf9934eErik // Delete the whole series and replace it with a new 37373714169d34a55fc01e14b30897a1ebbacf9934eErik // non-recurring event. 37473714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(ContentProviderOperation.newDelete(uri).build()); 37573714169d34a55fc01e14b30897a1ebbacf9934eErik 37673714169d34a55fc01e14b30897a1ebbacf9934eErik eventIdIndex = ops.size(); 377eca82e9468c002590045338f98588ad3ce1a0bb3Erik ops.add(ContentProviderOperation.newInsert(Events.CONTENT_URI).withValues(values) 37873714169d34a55fc01e14b30897a1ebbacf9934eErik .build()); 37973714169d34a55fc01e14b30897a1ebbacf9934eErik forceSaveReminders = true; 38073714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 38173714169d34a55fc01e14b30897a1ebbacf9934eErik checkTimeDependentFields(originalModel, model, values, modifyWhich); 382eca82e9468c002590045338f98588ad3ce1a0bb3Erik ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build()); 38373714169d34a55fc01e14b30897a1ebbacf9934eErik } 38473714169d34a55fc01e14b30897a1ebbacf9934eErik } 38573714169d34a55fc01e14b30897a1ebbacf9934eErik 38673714169d34a55fc01e14b30897a1ebbacf9934eErik // New Event or New Exception to an existing event 38773714169d34a55fc01e14b30897a1ebbacf9934eErik boolean newEvent = (eventIdIndex != -1); 38873714169d34a55fc01e14b30897a1ebbacf9934eErik ArrayList<Integer> originalMinutes; 38973714169d34a55fc01e14b30897a1ebbacf9934eErik if (originalModel != null) { 39073714169d34a55fc01e14b30897a1ebbacf9934eErik originalMinutes = originalModel.mReminderMinutes; 39173714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 39273714169d34a55fc01e14b30897a1ebbacf9934eErik originalMinutes = new ArrayList<Integer>(); 39373714169d34a55fc01e14b30897a1ebbacf9934eErik } 39473714169d34a55fc01e14b30897a1ebbacf9934eErik 39573714169d34a55fc01e14b30897a1ebbacf9934eErik if (newEvent) { 396eca82e9468c002590045338f98588ad3ce1a0bb3Erik saveRemindersWithBackRef(ops, eventIdIndex, reminderMinutes, originalMinutes, 397eca82e9468c002590045338f98588ad3ce1a0bb3Erik forceSaveReminders); 39873714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (uri != null) { 39973714169d34a55fc01e14b30897a1ebbacf9934eErik long eventId = ContentUris.parseId(uri); 400eca82e9468c002590045338f98588ad3ce1a0bb3Erik saveReminders(ops, eventId, reminderMinutes, originalMinutes, forceSaveReminders); 40173714169d34a55fc01e14b30897a1ebbacf9934eErik } 40273714169d34a55fc01e14b30897a1ebbacf9934eErik 40373714169d34a55fc01e14b30897a1ebbacf9934eErik ContentProviderOperation.Builder b; 40473714169d34a55fc01e14b30897a1ebbacf9934eErik boolean hasAttendeeData = model.mHasAttendeeData; 40573714169d34a55fc01e14b30897a1ebbacf9934eErik 40673714169d34a55fc01e14b30897a1ebbacf9934eErik // New event/instance - Set Organizer's response as yes 40773714169d34a55fc01e14b30897a1ebbacf9934eErik if (hasAttendeeData && newEvent) { 40873714169d34a55fc01e14b30897a1ebbacf9934eErik values.clear(); 40973714169d34a55fc01e14b30897a1ebbacf9934eErik 41073714169d34a55fc01e14b30897a1ebbacf9934eErik String ownerEmail = model.mOwnerAccount; 41173714169d34a55fc01e14b30897a1ebbacf9934eErik if (ownerEmail != null) { 41273714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Attendees.ATTENDEE_EMAIL, ownerEmail); 413eca82e9468c002590045338f98588ad3ce1a0bb3Erik values.put(Attendees.ATTENDEE_RELATIONSHIP, Attendees.RELATIONSHIP_ORGANIZER); 414eca82e9468c002590045338f98588ad3ce1a0bb3Erik values.put(Attendees.ATTENDEE_TYPE, Attendees.TYPE_NONE); 415d845fbe558d5229102b58cce70a1b29fe6cb6967Erik values.put(Attendees.ATTENDEE_STATUS, model.mSelfAttendeeStatus); 416eca82e9468c002590045338f98588ad3ce1a0bb3Erik 417eca82e9468c002590045338f98588ad3ce1a0bb3Erik b = ContentProviderOperation.newInsert(Attendees.CONTENT_URI).withValues(values); 418d845fbe558d5229102b58cce70a1b29fe6cb6967Erik b.withValueBackReference(Attendees.EVENT_ID, eventIdIndex); 41973714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 42073714169d34a55fc01e14b30897a1ebbacf9934eErik } 421d845fbe558d5229102b58cce70a1b29fe6cb6967Erik } else if (hasAttendeeData && 422d845fbe558d5229102b58cce70a1b29fe6cb6967Erik model.mSelfAttendeeStatus != originalModel.mSelfAttendeeStatus && 423d845fbe558d5229102b58cce70a1b29fe6cb6967Erik model.mOwnerAttendeeId != -1) { 424d845fbe558d5229102b58cce70a1b29fe6cb6967Erik if (DEBUG) { 425d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Log.d(TAG, "Setting attendee status to " + model.mSelfAttendeeStatus); 426d845fbe558d5229102b58cce70a1b29fe6cb6967Erik } 427d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Uri attUri = ContentUris.withAppendedId(Attendees.CONTENT_URI, model.mOwnerAttendeeId); 428d845fbe558d5229102b58cce70a1b29fe6cb6967Erik 429d845fbe558d5229102b58cce70a1b29fe6cb6967Erik values.clear(); 430d845fbe558d5229102b58cce70a1b29fe6cb6967Erik values.put(Attendees.ATTENDEE_STATUS, model.mSelfAttendeeStatus); 431d845fbe558d5229102b58cce70a1b29fe6cb6967Erik values.put(Attendees.EVENT_ID, model.mId); 432d845fbe558d5229102b58cce70a1b29fe6cb6967Erik b = ContentProviderOperation.newUpdate(attUri).withValues(values); 433d845fbe558d5229102b58cce70a1b29fe6cb6967Erik ops.add(b.build()); 43473714169d34a55fc01e14b30897a1ebbacf9934eErik } 43573714169d34a55fc01e14b30897a1ebbacf9934eErik 436eca82e9468c002590045338f98588ad3ce1a0bb3Erik // TODO: is this the right test? this currently checks if this is 437eca82e9468c002590045338f98588ad3ce1a0bb3Erik // a new event or an existing event. or is this a paranoia check? 43873714169d34a55fc01e14b30897a1ebbacf9934eErik if (hasAttendeeData && (newEvent || uri != null)) { 439d845fbe558d5229102b58cce70a1b29fe6cb6967Erik String attendees = model.getAttendeesString(); 44073714169d34a55fc01e14b30897a1ebbacf9934eErik String originalAttendeesString; 44173714169d34a55fc01e14b30897a1ebbacf9934eErik if (originalModel != null) { 442d845fbe558d5229102b58cce70a1b29fe6cb6967Erik originalAttendeesString = originalModel.getAttendeesString(); 44373714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 44473714169d34a55fc01e14b30897a1ebbacf9934eErik originalAttendeesString = ""; 44573714169d34a55fc01e14b30897a1ebbacf9934eErik } 446eca82e9468c002590045338f98588ad3ce1a0bb3Erik // Hit the content provider only if this is a new event or the user 447eca82e9468c002590045338f98588ad3ce1a0bb3Erik // has changed it 44873714169d34a55fc01e14b30897a1ebbacf9934eErik if (newEvent || !TextUtils.equals(originalAttendeesString, attendees)) { 44973714169d34a55fc01e14b30897a1ebbacf9934eErik // figure out which attendees need to be added and which ones 450eca82e9468c002590045338f98588ad3ce1a0bb3Erik // need to be deleted. use a linked hash set, so we maintain 45173714169d34a55fc01e14b30897a1ebbacf9934eErik // order (but also remove duplicates). 452eca82e9468c002590045338f98588ad3ce1a0bb3Erik setDomainFromModel(model); 453d845fbe558d5229102b58cce70a1b29fe6cb6967Erik HashMap<String, Attendee> newAttendees = model.mAttendeesList; 454d845fbe558d5229102b58cce70a1b29fe6cb6967Erik LinkedList<String> removedAttendees = new LinkedList<String>(); 45573714169d34a55fc01e14b30897a1ebbacf9934eErik 45673714169d34a55fc01e14b30897a1ebbacf9934eErik // the eventId is only used if eventIdIndex is -1. 45773714169d34a55fc01e14b30897a1ebbacf9934eErik // TODO: clean up this code. 45873714169d34a55fc01e14b30897a1ebbacf9934eErik long eventId = uri != null ? ContentUris.parseId(uri) : -1; 45973714169d34a55fc01e14b30897a1ebbacf9934eErik 46073714169d34a55fc01e14b30897a1ebbacf9934eErik // only compute deltas if this is an existing event. 46173714169d34a55fc01e14b30897a1ebbacf9934eErik // new events (being inserted into the Events table) won't 46273714169d34a55fc01e14b30897a1ebbacf9934eErik // have any existing attendees. 46373714169d34a55fc01e14b30897a1ebbacf9934eErik if (!newEvent) { 464d845fbe558d5229102b58cce70a1b29fe6cb6967Erik removedAttendees.clear(); 465d845fbe558d5229102b58cce70a1b29fe6cb6967Erik HashMap<String, Attendee> originalAttendees = originalModel.mAttendeesList; 466d845fbe558d5229102b58cce70a1b29fe6cb6967Erik for (String originalEmail : originalAttendees.keySet()) { 467d845fbe558d5229102b58cce70a1b29fe6cb6967Erik if (newAttendees.containsKey(originalEmail)) { 468eca82e9468c002590045338f98588ad3ce1a0bb3Erik // existing attendee. remove from new attendees set. 469d845fbe558d5229102b58cce70a1b29fe6cb6967Erik newAttendees.remove(originalEmail); 47073714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 471eca82e9468c002590045338f98588ad3ce1a0bb3Erik // no longer in attendees. mark as removed. 472d845fbe558d5229102b58cce70a1b29fe6cb6967Erik removedAttendees.add(originalEmail); 47373714169d34a55fc01e14b30897a1ebbacf9934eErik } 47473714169d34a55fc01e14b30897a1ebbacf9934eErik } 47573714169d34a55fc01e14b30897a1ebbacf9934eErik 47673714169d34a55fc01e14b30897a1ebbacf9934eErik // delete removed attendees if necessary 47773714169d34a55fc01e14b30897a1ebbacf9934eErik if (removedAttendees.size() > 0) { 47873714169d34a55fc01e14b30897a1ebbacf9934eErik b = ContentProviderOperation.newDelete(Attendees.CONTENT_URI); 47973714169d34a55fc01e14b30897a1ebbacf9934eErik 48073714169d34a55fc01e14b30897a1ebbacf9934eErik String[] args = new String[removedAttendees.size() + 1]; 48173714169d34a55fc01e14b30897a1ebbacf9934eErik args[0] = Long.toString(eventId); 48273714169d34a55fc01e14b30897a1ebbacf9934eErik int i = 1; 48373714169d34a55fc01e14b30897a1ebbacf9934eErik StringBuilder deleteWhere = new StringBuilder(ATTENDEES_DELETE_PREFIX); 484d845fbe558d5229102b58cce70a1b29fe6cb6967Erik for (String removedAttendee : removedAttendees) { 48573714169d34a55fc01e14b30897a1ebbacf9934eErik if (i > 1) { 48673714169d34a55fc01e14b30897a1ebbacf9934eErik deleteWhere.append(","); 48773714169d34a55fc01e14b30897a1ebbacf9934eErik } 48873714169d34a55fc01e14b30897a1ebbacf9934eErik deleteWhere.append("?"); 489d845fbe558d5229102b58cce70a1b29fe6cb6967Erik args[i++] = removedAttendee; 49073714169d34a55fc01e14b30897a1ebbacf9934eErik } 49173714169d34a55fc01e14b30897a1ebbacf9934eErik deleteWhere.append(")"); 49273714169d34a55fc01e14b30897a1ebbacf9934eErik b.withSelection(deleteWhere.toString(), args); 49373714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 49473714169d34a55fc01e14b30897a1ebbacf9934eErik } 49573714169d34a55fc01e14b30897a1ebbacf9934eErik } 49673714169d34a55fc01e14b30897a1ebbacf9934eErik 49773714169d34a55fc01e14b30897a1ebbacf9934eErik if (newAttendees.size() > 0) { 49873714169d34a55fc01e14b30897a1ebbacf9934eErik // Insert the new attendees 499d845fbe558d5229102b58cce70a1b29fe6cb6967Erik for (Attendee attendee : newAttendees.values()) { 50073714169d34a55fc01e14b30897a1ebbacf9934eErik values.clear(); 501d845fbe558d5229102b58cce70a1b29fe6cb6967Erik values.put(Attendees.ATTENDEE_NAME, attendee.mName); 502d845fbe558d5229102b58cce70a1b29fe6cb6967Erik values.put(Attendees.ATTENDEE_EMAIL, attendee.mEmail); 50373714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Attendees.ATTENDEE_RELATIONSHIP, 50473714169d34a55fc01e14b30897a1ebbacf9934eErik Attendees.RELATIONSHIP_ATTENDEE); 505eca82e9468c002590045338f98588ad3ce1a0bb3Erik values.put(Attendees.ATTENDEE_TYPE, Attendees.TYPE_NONE); 506eca82e9468c002590045338f98588ad3ce1a0bb3Erik values.put(Attendees.ATTENDEE_STATUS, Attendees.ATTENDEE_STATUS_NONE); 50773714169d34a55fc01e14b30897a1ebbacf9934eErik 50873714169d34a55fc01e14b30897a1ebbacf9934eErik if (newEvent) { 509eca82e9468c002590045338f98588ad3ce1a0bb3Erik b = ContentProviderOperation.newInsert(Attendees.CONTENT_URI) 51073714169d34a55fc01e14b30897a1ebbacf9934eErik .withValues(values); 51173714169d34a55fc01e14b30897a1ebbacf9934eErik b.withValueBackReference(Attendees.EVENT_ID, eventIdIndex); 51273714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 51373714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Attendees.EVENT_ID, eventId); 514eca82e9468c002590045338f98588ad3ce1a0bb3Erik b = ContentProviderOperation.newInsert(Attendees.CONTENT_URI) 51573714169d34a55fc01e14b30897a1ebbacf9934eErik .withValues(values); 51673714169d34a55fc01e14b30897a1ebbacf9934eErik } 51773714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 51873714169d34a55fc01e14b30897a1ebbacf9934eErik } 51973714169d34a55fc01e14b30897a1ebbacf9934eErik } 52073714169d34a55fc01e14b30897a1ebbacf9934eErik } 52173714169d34a55fc01e14b30897a1ebbacf9934eErik } 52273714169d34a55fc01e14b30897a1ebbacf9934eErik 52337960c091b5912dd45beb561a85a9064ea42e616Erik 52437960c091b5912dd45beb561a85a9064ea42e616Erik mService.startBatch(mService.getNextToken(), null, android.provider.Calendar.AUTHORITY, ops, 52537960c091b5912dd45beb561a85a9064ea42e616Erik Utils.UNDO_DELAY); 52673714169d34a55fc01e14b30897a1ebbacf9934eErik 52773714169d34a55fc01e14b30897a1ebbacf9934eErik return true; 52873714169d34a55fc01e14b30897a1ebbacf9934eErik } 52973714169d34a55fc01e14b30897a1ebbacf9934eErik 530d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public static LinkedHashSet<Rfc822Token> getAddressesFromList(String list, 531d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Rfc822Validator validator) { 53273714169d34a55fc01e14b30897a1ebbacf9934eErik LinkedHashSet<Rfc822Token> addresses = new LinkedHashSet<Rfc822Token>(); 53373714169d34a55fc01e14b30897a1ebbacf9934eErik Rfc822Tokenizer.tokenize(list, addresses); 534d845fbe558d5229102b58cce70a1b29fe6cb6967Erik if (validator == null) { 535d845fbe558d5229102b58cce70a1b29fe6cb6967Erik return addresses; 536d845fbe558d5229102b58cce70a1b29fe6cb6967Erik } 53773714169d34a55fc01e14b30897a1ebbacf9934eErik 538eca82e9468c002590045338f98588ad3ce1a0bb3Erik // validate the emails, out of paranoia. they should already be 53973714169d34a55fc01e14b30897a1ebbacf9934eErik // validated on input, but drop any invalid emails just to be safe. 54073714169d34a55fc01e14b30897a1ebbacf9934eErik Iterator<Rfc822Token> addressIterator = addresses.iterator(); 54173714169d34a55fc01e14b30897a1ebbacf9934eErik while (addressIterator.hasNext()) { 54273714169d34a55fc01e14b30897a1ebbacf9934eErik Rfc822Token address = addressIterator.next(); 543d845fbe558d5229102b58cce70a1b29fe6cb6967Erik if (!validator.isValid(address.getAddress())) { 544d845fbe558d5229102b58cce70a1b29fe6cb6967Erik Log.v(TAG, "Dropping invalid attendee email address: " + address.getAddress()); 54573714169d34a55fc01e14b30897a1ebbacf9934eErik addressIterator.remove(); 54673714169d34a55fc01e14b30897a1ebbacf9934eErik } 54773714169d34a55fc01e14b30897a1ebbacf9934eErik } 54873714169d34a55fc01e14b30897a1ebbacf9934eErik return addresses; 54973714169d34a55fc01e14b30897a1ebbacf9934eErik } 55073714169d34a55fc01e14b30897a1ebbacf9934eErik 551e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang /** 552e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * When we aren't given an explicit start time, we default to the next 553e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * upcoming half hour. So, for example, 5:01 -> 5:30, 5:30 -> 6:00, etc. 554e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * 555e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * @return a UTC time in milliseconds representing the next upcoming half 556e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * hour 557e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang */ 558e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang protected long constructDefaultStartTime(long now) { 559e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang Time defaultStart = new Time(); 560e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang defaultStart.set(now); 561e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang defaultStart.second = 0; 562e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang defaultStart.minute = 30; 563e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang long defaultStartMillis = defaultStart.toMillis(false); 564e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang if (now < defaultStartMillis) { 565e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang return defaultStartMillis; 566e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang } else { 567e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang return defaultStartMillis + 30 * DateUtils.MINUTE_IN_MILLIS; 568e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang } 569e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang } 570e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang 571e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang /** 572e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * When we aren't given an explicit end time, we default to an hour after 573e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * the start time. 574e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * @param startTime the start time 575e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang * @return a default end time 576e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang */ 577e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang protected long constructDefaultEndTime(long startTime) { 578e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang return startTime + DateUtils.HOUR_IN_MILLIS; 579e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang } 580e700c16ec2464cbba86f91f8f757ae59cbed34b0Mason Tang 581eca82e9468c002590045338f98588ad3ce1a0bb3Erik // TODO think about how useful this is. Probably check if our event has 582eca82e9468c002590045338f98588ad3ce1a0bb3Erik // changed early on and either update all or nothing. Should still do the if 583eca82e9468c002590045338f98588ad3ce1a0bb3Erik // MODIFY_ALL bit. 584eca82e9468c002590045338f98588ad3ce1a0bb3Erik void checkTimeDependentFields(CalendarEventModel originalModel, CalendarEventModel model, 585eca82e9468c002590045338f98588ad3ce1a0bb3Erik ContentValues values, int modifyWhich) { 58673714169d34a55fc01e14b30897a1ebbacf9934eErik long oldBegin = model.mOriginalStart; 58773714169d34a55fc01e14b30897a1ebbacf9934eErik long oldEnd = model.mOriginalEnd; 58873714169d34a55fc01e14b30897a1ebbacf9934eErik boolean oldAllDay = originalModel.mAllDay; 58973714169d34a55fc01e14b30897a1ebbacf9934eErik String oldRrule = originalModel.mRrule; 59073714169d34a55fc01e14b30897a1ebbacf9934eErik String oldTimezone = originalModel.mTimezone; 59173714169d34a55fc01e14b30897a1ebbacf9934eErik 59273714169d34a55fc01e14b30897a1ebbacf9934eErik long newBegin = model.mStart; 59373714169d34a55fc01e14b30897a1ebbacf9934eErik long newEnd = model.mEnd; 59473714169d34a55fc01e14b30897a1ebbacf9934eErik boolean newAllDay = model.mAllDay; 59573714169d34a55fc01e14b30897a1ebbacf9934eErik String newRrule = model.mRrule; 59673714169d34a55fc01e14b30897a1ebbacf9934eErik String newTimezone = model.mTimezone; 59773714169d34a55fc01e14b30897a1ebbacf9934eErik 59873714169d34a55fc01e14b30897a1ebbacf9934eErik // If none of the time-dependent fields changed, then remove them. 59973714169d34a55fc01e14b30897a1ebbacf9934eErik if (oldBegin == newBegin && oldEnd == newEnd && oldAllDay == newAllDay 60073714169d34a55fc01e14b30897a1ebbacf9934eErik && TextUtils.equals(oldRrule, newRrule) 601eca82e9468c002590045338f98588ad3ce1a0bb3Erik && TextUtils.equals(oldTimezone, newTimezone)) { 60273714169d34a55fc01e14b30897a1ebbacf9934eErik values.remove(Events.DTSTART); 60373714169d34a55fc01e14b30897a1ebbacf9934eErik values.remove(Events.DTEND); 60473714169d34a55fc01e14b30897a1ebbacf9934eErik values.remove(Events.DURATION); 60573714169d34a55fc01e14b30897a1ebbacf9934eErik values.remove(Events.ALL_DAY); 60673714169d34a55fc01e14b30897a1ebbacf9934eErik values.remove(Events.RRULE); 60773714169d34a55fc01e14b30897a1ebbacf9934eErik values.remove(Events.EVENT_TIMEZONE); 60873714169d34a55fc01e14b30897a1ebbacf9934eErik return; 60973714169d34a55fc01e14b30897a1ebbacf9934eErik } 61073714169d34a55fc01e14b30897a1ebbacf9934eErik 61173714169d34a55fc01e14b30897a1ebbacf9934eErik if (oldRrule == null || newRrule == null) { 61273714169d34a55fc01e14b30897a1ebbacf9934eErik return; 61373714169d34a55fc01e14b30897a1ebbacf9934eErik } 61473714169d34a55fc01e14b30897a1ebbacf9934eErik 61573714169d34a55fc01e14b30897a1ebbacf9934eErik // If we are modifying all events then we need to set DTSTART to the 61673714169d34a55fc01e14b30897a1ebbacf9934eErik // start time of the first event in the series, not the current 617eca82e9468c002590045338f98588ad3ce1a0bb3Erik // date and time. If the start time of the event was changed 61873714169d34a55fc01e14b30897a1ebbacf9934eErik // (from, say, 3pm to 4pm), then we want to add the time difference 61973714169d34a55fc01e14b30897a1ebbacf9934eErik // to the start time of the first event in the series (the DTSTART 620eca82e9468c002590045338f98588ad3ce1a0bb3Erik // value). If we are modifying one instance or all following instances, 62173714169d34a55fc01e14b30897a1ebbacf9934eErik // then we leave the DTSTART field alone. 62273714169d34a55fc01e14b30897a1ebbacf9934eErik if (modifyWhich == MODIFY_ALL) { 62373714169d34a55fc01e14b30897a1ebbacf9934eErik long oldStartMillis = originalModel.mStart; 62473714169d34a55fc01e14b30897a1ebbacf9934eErik long oldStartMillis2 = oldStartMillis; 62573714169d34a55fc01e14b30897a1ebbacf9934eErik if (oldBegin != newBegin) { 62673714169d34a55fc01e14b30897a1ebbacf9934eErik // The user changed the start time of this event 62773714169d34a55fc01e14b30897a1ebbacf9934eErik long offset = newBegin - oldBegin; 62873714169d34a55fc01e14b30897a1ebbacf9934eErik oldStartMillis += offset; 62973714169d34a55fc01e14b30897a1ebbacf9934eErik } 63073714169d34a55fc01e14b30897a1ebbacf9934eErik if (newAllDay) { 63173714169d34a55fc01e14b30897a1ebbacf9934eErik Time time = new Time(Time.TIMEZONE_UTC); 63273714169d34a55fc01e14b30897a1ebbacf9934eErik time.set(oldStartMillis); 63373714169d34a55fc01e14b30897a1ebbacf9934eErik time.hour = 0; 63473714169d34a55fc01e14b30897a1ebbacf9934eErik time.minute = 0; 63573714169d34a55fc01e14b30897a1ebbacf9934eErik time.second = 0; 63673714169d34a55fc01e14b30897a1ebbacf9934eErik oldStartMillis = time.toMillis(false); 63773714169d34a55fc01e14b30897a1ebbacf9934eErik } 63873714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.DTSTART, oldStartMillis); 63973714169d34a55fc01e14b30897a1ebbacf9934eErik } 64073714169d34a55fc01e14b30897a1ebbacf9934eErik } 64173714169d34a55fc01e14b30897a1ebbacf9934eErik 64273714169d34a55fc01e14b30897a1ebbacf9934eErik /** 643eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Prepares an update to the original event so it stops where the new series 644eca82e9468c002590045338f98588ad3ce1a0bb3Erik * begins When we update 'this and all following' events we need to change 645eca82e9468c002590045338f98588ad3ce1a0bb3Erik * the original event to end before a new series starts. This creates an 646eca82e9468c002590045338f98588ad3ce1a0bb3Erik * update to the old event's rrule to do that. 64773714169d34a55fc01e14b30897a1ebbacf9934eErik * 64873714169d34a55fc01e14b30897a1ebbacf9934eErik * @param ops The list of operations to add the update to 64973714169d34a55fc01e14b30897a1ebbacf9934eErik * @param originalModel The original event that we're updating 65073714169d34a55fc01e14b30897a1ebbacf9934eErik * @param initialBeginTime The original start time for the exception 65173714169d34a55fc01e14b30897a1ebbacf9934eErik */ 652fbce65e53c7a111955f638db5bf8bee35381e5b7Erik public void updatePastEvents(ArrayList<ContentProviderOperation> ops, 65373714169d34a55fc01e14b30897a1ebbacf9934eErik CalendarEventModel originalModel, long initialBeginTime) { 65473714169d34a55fc01e14b30897a1ebbacf9934eErik boolean allDay = originalModel.mAllDay; 65573714169d34a55fc01e14b30897a1ebbacf9934eErik String oldRrule = originalModel.mRrule; 656eca82e9468c002590045338f98588ad3ce1a0bb3Erik 65773714169d34a55fc01e14b30897a1ebbacf9934eErik EventRecurrence eventRecurrence = new EventRecurrence(); 65873714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.parse(oldRrule); 65973714169d34a55fc01e14b30897a1ebbacf9934eErik 66073714169d34a55fc01e14b30897a1ebbacf9934eErik Time untilTime = new Time(); 661eca82e9468c002590045338f98588ad3ce1a0bb3Erik Time dtstart = new Time(); 66273714169d34a55fc01e14b30897a1ebbacf9934eErik long begin = initialBeginTime; 66373714169d34a55fc01e14b30897a1ebbacf9934eErik ContentValues oldValues = new ContentValues(); 66473714169d34a55fc01e14b30897a1ebbacf9934eErik 66573714169d34a55fc01e14b30897a1ebbacf9934eErik // The "until" time must be in UTC time in order for Google calendar 666eca82e9468c002590045338f98588ad3ce1a0bb3Erik // to display it properly. For all-day events, the "until" time string 667eca82e9468c002590045338f98588ad3ce1a0bb3Erik // must include just the date field, and not the time field. The 66873714169d34a55fc01e14b30897a1ebbacf9934eErik // repeating events repeat up to and including the "until" time. 66973714169d34a55fc01e14b30897a1ebbacf9934eErik untilTime.timezone = Time.TIMEZONE_UTC; 670eca82e9468c002590045338f98588ad3ce1a0bb3Erik dtstart.timezone = originalModel.mTimezone; 671eca82e9468c002590045338f98588ad3ce1a0bb3Erik dtstart.set(originalModel.mStart); 67273714169d34a55fc01e14b30897a1ebbacf9934eErik 67373714169d34a55fc01e14b30897a1ebbacf9934eErik // Subtract one second from the old begin time to get the new 67473714169d34a55fc01e14b30897a1ebbacf9934eErik // "until" time. 675eca82e9468c002590045338f98588ad3ce1a0bb3Erik untilTime.set(begin - 1000); // subtract one second (1000 millis) 67673714169d34a55fc01e14b30897a1ebbacf9934eErik if (allDay) { 67773714169d34a55fc01e14b30897a1ebbacf9934eErik untilTime.hour = 0; 67873714169d34a55fc01e14b30897a1ebbacf9934eErik untilTime.minute = 0; 67973714169d34a55fc01e14b30897a1ebbacf9934eErik untilTime.second = 0; 68073714169d34a55fc01e14b30897a1ebbacf9934eErik untilTime.allDay = true; 68173714169d34a55fc01e14b30897a1ebbacf9934eErik untilTime.normalize(false); 682eca82e9468c002590045338f98588ad3ce1a0bb3Erik 683eca82e9468c002590045338f98588ad3ce1a0bb3Erik dtstart.hour = 0; 684eca82e9468c002590045338f98588ad3ce1a0bb3Erik dtstart.minute = 0; 685eca82e9468c002590045338f98588ad3ce1a0bb3Erik dtstart.second = 0; 686eca82e9468c002590045338f98588ad3ce1a0bb3Erik dtstart.allDay = true; 687eca82e9468c002590045338f98588ad3ce1a0bb3Erik dtstart.timezone = Time.TIMEZONE_UTC; 68873714169d34a55fc01e14b30897a1ebbacf9934eErik } 68973714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.until = untilTime.format2445(); 69073714169d34a55fc01e14b30897a1ebbacf9934eErik 69173714169d34a55fc01e14b30897a1ebbacf9934eErik oldValues.put(Events.RRULE, eventRecurrence.toString()); 692eca82e9468c002590045338f98588ad3ce1a0bb3Erik oldValues.put(Events.DTSTART, dtstart.normalize(true)); 693eca82e9468c002590045338f98588ad3ce1a0bb3Erik ContentProviderOperation.Builder b = ContentProviderOperation.newUpdate(originalModel.mUri) 694eca82e9468c002590045338f98588ad3ce1a0bb3Erik .withValues(oldValues); 69573714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 69673714169d34a55fc01e14b30897a1ebbacf9934eErik } 69773714169d34a55fc01e14b30897a1ebbacf9934eErik 698eca82e9468c002590045338f98588ad3ce1a0bb3Erik // Constructs a label given an arbitrary number of minutes. For example, 69973714169d34a55fc01e14b30897a1ebbacf9934eErik // if the given minutes is 63, then this returns the string "63 minutes". 70073714169d34a55fc01e14b30897a1ebbacf9934eErik // As another example, if the given minutes is 120, then this returns 70173714169d34a55fc01e14b30897a1ebbacf9934eErik // "2 hours". 702fbce65e53c7a111955f638db5bf8bee35381e5b7Erik public String constructReminderLabel(int minutes, boolean abbrev) { 703dd95df57c8c5a58a85c4c0effad5652dec14f621Erik Resources resources = mContext.getResources(); 70473714169d34a55fc01e14b30897a1ebbacf9934eErik int value, resId; 70573714169d34a55fc01e14b30897a1ebbacf9934eErik 70673714169d34a55fc01e14b30897a1ebbacf9934eErik if (minutes % 60 != 0) { 70773714169d34a55fc01e14b30897a1ebbacf9934eErik value = minutes; 70873714169d34a55fc01e14b30897a1ebbacf9934eErik if (abbrev) { 70973714169d34a55fc01e14b30897a1ebbacf9934eErik resId = R.plurals.Nmins; 71073714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 71173714169d34a55fc01e14b30897a1ebbacf9934eErik resId = R.plurals.Nminutes; 71273714169d34a55fc01e14b30897a1ebbacf9934eErik } 71373714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (minutes % (24 * 60) != 0) { 71473714169d34a55fc01e14b30897a1ebbacf9934eErik value = minutes / 60; 71573714169d34a55fc01e14b30897a1ebbacf9934eErik resId = R.plurals.Nhours; 71673714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 717eca82e9468c002590045338f98588ad3ce1a0bb3Erik value = minutes / (24 * 60); 71873714169d34a55fc01e14b30897a1ebbacf9934eErik resId = R.plurals.Ndays; 71973714169d34a55fc01e14b30897a1ebbacf9934eErik } 72073714169d34a55fc01e14b30897a1ebbacf9934eErik 72173714169d34a55fc01e14b30897a1ebbacf9934eErik String format = resources.getQuantityString(resId, value); 72273714169d34a55fc01e14b30897a1ebbacf9934eErik return String.format(format, value); 72373714169d34a55fc01e14b30897a1ebbacf9934eErik } 72473714169d34a55fc01e14b30897a1ebbacf9934eErik 72573714169d34a55fc01e14b30897a1ebbacf9934eErik /** 726eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Compares two models to ensure that they refer to the same event. This is 727eca82e9468c002590045338f98588ad3ce1a0bb3Erik * a safety check to make sure an updated event model refers to the same 728eca82e9468c002590045338f98588ad3ce1a0bb3Erik * event as the original model. If the original model is null then this is a 729eca82e9468c002590045338f98588ad3ce1a0bb3Erik * new event or we're forcing an overwrite so we return true in that case. 730eca82e9468c002590045338f98588ad3ce1a0bb3Erik * The important identifiers are the Calendar Id and the Event Id. 73173714169d34a55fc01e14b30897a1ebbacf9934eErik * 73273714169d34a55fc01e14b30897a1ebbacf9934eErik * @return 73373714169d34a55fc01e14b30897a1ebbacf9934eErik */ 734fbce65e53c7a111955f638db5bf8bee35381e5b7Erik public static boolean isSameEvent(CalendarEventModel model, CalendarEventModel originalModel) { 73573714169d34a55fc01e14b30897a1ebbacf9934eErik if (originalModel == null) { 73673714169d34a55fc01e14b30897a1ebbacf9934eErik return true; 73773714169d34a55fc01e14b30897a1ebbacf9934eErik } 73873714169d34a55fc01e14b30897a1ebbacf9934eErik 73973714169d34a55fc01e14b30897a1ebbacf9934eErik if (model.mCalendarId != originalModel.mCalendarId) { 74073714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 74173714169d34a55fc01e14b30897a1ebbacf9934eErik } 74273714169d34a55fc01e14b30897a1ebbacf9934eErik if (model.mId != originalModel.mId) { 74373714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 74473714169d34a55fc01e14b30897a1ebbacf9934eErik } 74573714169d34a55fc01e14b30897a1ebbacf9934eErik 74673714169d34a55fc01e14b30897a1ebbacf9934eErik return true; 74773714169d34a55fc01e14b30897a1ebbacf9934eErik } 74873714169d34a55fc01e14b30897a1ebbacf9934eErik 749eca82e9468c002590045338f98588ad3ce1a0bb3Erik /** 750eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Saves the reminders, if they changed. Returns true if operations to 751eca82e9468c002590045338f98588ad3ce1a0bb3Erik * update the database were added. 75273714169d34a55fc01e14b30897a1ebbacf9934eErik * 75373714169d34a55fc01e14b30897a1ebbacf9934eErik * @param ops the array of ContentProviderOperations 75473714169d34a55fc01e14b30897a1ebbacf9934eErik * @param eventId the id of the event whose reminders are being updated 75573714169d34a55fc01e14b30897a1ebbacf9934eErik * @param reminderMinutes the array of reminders set by the user 75673714169d34a55fc01e14b30897a1ebbacf9934eErik * @param originalMinutes the original array of reminders 757eca82e9468c002590045338f98588ad3ce1a0bb3Erik * @param forceSave if true, then save the reminders even if they didn't change 75873714169d34a55fc01e14b30897a1ebbacf9934eErik * @return true if operations to update the database were added 75973714169d34a55fc01e14b30897a1ebbacf9934eErik */ 760fbce65e53c7a111955f638db5bf8bee35381e5b7Erik public static boolean saveReminders(ArrayList<ContentProviderOperation> ops, long eventId, 76173714169d34a55fc01e14b30897a1ebbacf9934eErik ArrayList<Integer> reminderMinutes, ArrayList<Integer> originalMinutes, 76273714169d34a55fc01e14b30897a1ebbacf9934eErik boolean forceSave) { 76373714169d34a55fc01e14b30897a1ebbacf9934eErik // If the reminders have not changed, then don't update the database 76473714169d34a55fc01e14b30897a1ebbacf9934eErik if (reminderMinutes.equals(originalMinutes) && !forceSave) { 76573714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 76673714169d34a55fc01e14b30897a1ebbacf9934eErik } 76773714169d34a55fc01e14b30897a1ebbacf9934eErik 76873714169d34a55fc01e14b30897a1ebbacf9934eErik // Delete all the existing reminders for this event 76973714169d34a55fc01e14b30897a1ebbacf9934eErik String where = Reminders.EVENT_ID + "=?"; 77073714169d34a55fc01e14b30897a1ebbacf9934eErik String[] args = new String[] {Long.toString(eventId)}; 771eca82e9468c002590045338f98588ad3ce1a0bb3Erik ContentProviderOperation.Builder b = ContentProviderOperation 772eca82e9468c002590045338f98588ad3ce1a0bb3Erik .newDelete(Reminders.CONTENT_URI); 77373714169d34a55fc01e14b30897a1ebbacf9934eErik b.withSelection(where, args); 77473714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 77573714169d34a55fc01e14b30897a1ebbacf9934eErik 77673714169d34a55fc01e14b30897a1ebbacf9934eErik ContentValues values = new ContentValues(); 77773714169d34a55fc01e14b30897a1ebbacf9934eErik int len = reminderMinutes.size(); 77873714169d34a55fc01e14b30897a1ebbacf9934eErik 77973714169d34a55fc01e14b30897a1ebbacf9934eErik // Insert the new reminders, if any 78073714169d34a55fc01e14b30897a1ebbacf9934eErik for (int i = 0; i < len; i++) { 78173714169d34a55fc01e14b30897a1ebbacf9934eErik int minutes = reminderMinutes.get(i); 78273714169d34a55fc01e14b30897a1ebbacf9934eErik 78373714169d34a55fc01e14b30897a1ebbacf9934eErik values.clear(); 78473714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Reminders.MINUTES, minutes); 78573714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Reminders.METHOD, Reminders.METHOD_ALERT); 78673714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Reminders.EVENT_ID, eventId); 78773714169d34a55fc01e14b30897a1ebbacf9934eErik b = ContentProviderOperation.newInsert(Reminders.CONTENT_URI).withValues(values); 78873714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 78973714169d34a55fc01e14b30897a1ebbacf9934eErik } 79073714169d34a55fc01e14b30897a1ebbacf9934eErik return true; 79173714169d34a55fc01e14b30897a1ebbacf9934eErik } 79273714169d34a55fc01e14b30897a1ebbacf9934eErik 79373714169d34a55fc01e14b30897a1ebbacf9934eErik /** 794eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Saves the reminders, if they changed. Returns true if operations to 795eca82e9468c002590045338f98588ad3ce1a0bb3Erik * update the database were added. Uses a reference id since an id isn't 796eca82e9468c002590045338f98588ad3ce1a0bb3Erik * created until the row is added. 79773714169d34a55fc01e14b30897a1ebbacf9934eErik * 79873714169d34a55fc01e14b30897a1ebbacf9934eErik * @param ops the array of ContentProviderOperations 79973714169d34a55fc01e14b30897a1ebbacf9934eErik * @param eventId the id of the event whose reminders are being updated 80073714169d34a55fc01e14b30897a1ebbacf9934eErik * @param reminderMinutes the array of reminders set by the user 80173714169d34a55fc01e14b30897a1ebbacf9934eErik * @param originalMinutes the original array of reminders 802eca82e9468c002590045338f98588ad3ce1a0bb3Erik * @param forceSave if true, then save the reminders even if they didn't change 80373714169d34a55fc01e14b30897a1ebbacf9934eErik * @return true if operations to update the database were added 80473714169d34a55fc01e14b30897a1ebbacf9934eErik */ 805d845fbe558d5229102b58cce70a1b29fe6cb6967Erik public boolean saveRemindersWithBackRef(ArrayList<ContentProviderOperation> ops, 806d845fbe558d5229102b58cce70a1b29fe6cb6967Erik int eventIdIndex, ArrayList<Integer> reminderMinutes, 807d845fbe558d5229102b58cce70a1b29fe6cb6967Erik ArrayList<Integer> originalMinutes, boolean forceSave) { 80873714169d34a55fc01e14b30897a1ebbacf9934eErik // If the reminders have not changed, then don't update the database 80973714169d34a55fc01e14b30897a1ebbacf9934eErik if (reminderMinutes.equals(originalMinutes) && !forceSave) { 81073714169d34a55fc01e14b30897a1ebbacf9934eErik return false; 81173714169d34a55fc01e14b30897a1ebbacf9934eErik } 81273714169d34a55fc01e14b30897a1ebbacf9934eErik 81373714169d34a55fc01e14b30897a1ebbacf9934eErik // Delete all the existing reminders for this event 814eca82e9468c002590045338f98588ad3ce1a0bb3Erik ContentProviderOperation.Builder b = ContentProviderOperation 815eca82e9468c002590045338f98588ad3ce1a0bb3Erik .newDelete(Reminders.CONTENT_URI); 81673714169d34a55fc01e14b30897a1ebbacf9934eErik b.withSelection(Reminders.EVENT_ID + "=?", new String[1]); 81773714169d34a55fc01e14b30897a1ebbacf9934eErik b.withSelectionBackReference(0, eventIdIndex); 81873714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 81973714169d34a55fc01e14b30897a1ebbacf9934eErik 82073714169d34a55fc01e14b30897a1ebbacf9934eErik ContentValues values = new ContentValues(); 82173714169d34a55fc01e14b30897a1ebbacf9934eErik int len = reminderMinutes.size(); 82273714169d34a55fc01e14b30897a1ebbacf9934eErik 82373714169d34a55fc01e14b30897a1ebbacf9934eErik // Insert the new reminders, if any 82473714169d34a55fc01e14b30897a1ebbacf9934eErik for (int i = 0; i < len; i++) { 82573714169d34a55fc01e14b30897a1ebbacf9934eErik int minutes = reminderMinutes.get(i); 82673714169d34a55fc01e14b30897a1ebbacf9934eErik 82773714169d34a55fc01e14b30897a1ebbacf9934eErik values.clear(); 82873714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Reminders.MINUTES, minutes); 82973714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Reminders.METHOD, Reminders.METHOD_ALERT); 83073714169d34a55fc01e14b30897a1ebbacf9934eErik b = ContentProviderOperation.newInsert(Reminders.CONTENT_URI).withValues(values); 83173714169d34a55fc01e14b30897a1ebbacf9934eErik b.withValueBackReference(Reminders.EVENT_ID, eventIdIndex); 83273714169d34a55fc01e14b30897a1ebbacf9934eErik ops.add(b.build()); 83373714169d34a55fc01e14b30897a1ebbacf9934eErik } 83473714169d34a55fc01e14b30897a1ebbacf9934eErik return true; 83573714169d34a55fc01e14b30897a1ebbacf9934eErik } 83673714169d34a55fc01e14b30897a1ebbacf9934eErik 837eca82e9468c002590045338f98588ad3ce1a0bb3Erik // It's the first event in the series if the start time before being 838eca82e9468c002590045338f98588ad3ce1a0bb3Erik // modified is the same as the original event's start time 83973714169d34a55fc01e14b30897a1ebbacf9934eErik static boolean isFirstEventInSeries(CalendarEventModel model, 84073714169d34a55fc01e14b30897a1ebbacf9934eErik CalendarEventModel originalModel) { 84173714169d34a55fc01e14b30897a1ebbacf9934eErik return model.mOriginalStart == originalModel.mStart; 84273714169d34a55fc01e14b30897a1ebbacf9934eErik } 84373714169d34a55fc01e14b30897a1ebbacf9934eErik 84473714169d34a55fc01e14b30897a1ebbacf9934eErik // Adds an rRule and duration to a set of content values 84573714169d34a55fc01e14b30897a1ebbacf9934eErik void addRecurrenceRule(ContentValues values, CalendarEventModel model) { 84673714169d34a55fc01e14b30897a1ebbacf9934eErik String rrule = model.mRrule; 84773714169d34a55fc01e14b30897a1ebbacf9934eErik 84873714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.RRULE, rrule); 84973714169d34a55fc01e14b30897a1ebbacf9934eErik long end = model.mEnd; 85073714169d34a55fc01e14b30897a1ebbacf9934eErik long start = model.mStart; 85173714169d34a55fc01e14b30897a1ebbacf9934eErik String duration = model.mDuration; 85273714169d34a55fc01e14b30897a1ebbacf9934eErik 85337960c091b5912dd45beb561a85a9064ea42e616Erik boolean isAllDay = model.mAllDay; 85437960c091b5912dd45beb561a85a9064ea42e616Erik if (end > start) { 85537960c091b5912dd45beb561a85a9064ea42e616Erik if (isAllDay) { 85637960c091b5912dd45beb561a85a9064ea42e616Erik // if it's all day compute the duration in days 85737960c091b5912dd45beb561a85a9064ea42e616Erik long days = (end - start + DateUtils.DAY_IN_MILLIS - 1) 85837960c091b5912dd45beb561a85a9064ea42e616Erik / DateUtils.DAY_IN_MILLIS; 85937960c091b5912dd45beb561a85a9064ea42e616Erik duration = "P" + days + "D"; 86073714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 86137960c091b5912dd45beb561a85a9064ea42e616Erik // otherwise compute the duration in seconds 86237960c091b5912dd45beb561a85a9064ea42e616Erik long seconds = (end - start) / DateUtils.SECOND_IN_MILLIS; 86337960c091b5912dd45beb561a85a9064ea42e616Erik duration = "P" + seconds + "S"; 86437960c091b5912dd45beb561a85a9064ea42e616Erik } 86537960c091b5912dd45beb561a85a9064ea42e616Erik } else if (TextUtils.isEmpty(duration)) { 86637960c091b5912dd45beb561a85a9064ea42e616Erik 86737960c091b5912dd45beb561a85a9064ea42e616Erik // If no good duration info exists assume the default 86837960c091b5912dd45beb561a85a9064ea42e616Erik if (isAllDay) { 86937960c091b5912dd45beb561a85a9064ea42e616Erik duration = "P1D"; 87037960c091b5912dd45beb561a85a9064ea42e616Erik } else { 87137960c091b5912dd45beb561a85a9064ea42e616Erik duration = "P3600S"; 87273714169d34a55fc01e14b30897a1ebbacf9934eErik } 87373714169d34a55fc01e14b30897a1ebbacf9934eErik } 87473714169d34a55fc01e14b30897a1ebbacf9934eErik // recurring events should have a duration and dtend set to null 87573714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.DURATION, duration); 876eca82e9468c002590045338f98588ad3ce1a0bb3Erik values.put(Events.DTEND, (Long) null); 87773714169d34a55fc01e14b30897a1ebbacf9934eErik } 87873714169d34a55fc01e14b30897a1ebbacf9934eErik 87973714169d34a55fc01e14b30897a1ebbacf9934eErik /** 880eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Uses the recurrence selection and the model data to build an rrule and 881eca82e9468c002590045338f98588ad3ce1a0bb3Erik * write it to the model. 88273714169d34a55fc01e14b30897a1ebbacf9934eErik * 88373714169d34a55fc01e14b30897a1ebbacf9934eErik * @param selection the type of rrule 88473714169d34a55fc01e14b30897a1ebbacf9934eErik * @param model The event to update 8858e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang * @param weekStart the week start day, specified as java.util.Calendar 8868e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang * constants 88773714169d34a55fc01e14b30897a1ebbacf9934eErik */ 8888e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang static void updateRecurrenceRule(int selection, CalendarEventModel model, 8898e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang int weekStart) { 89073714169d34a55fc01e14b30897a1ebbacf9934eErik // Make sure we don't have any leftover data from the previous setting 89173714169d34a55fc01e14b30897a1ebbacf9934eErik EventRecurrence eventRecurrence = new EventRecurrence(); 89273714169d34a55fc01e14b30897a1ebbacf9934eErik 89373714169d34a55fc01e14b30897a1ebbacf9934eErik if (selection == DOES_NOT_REPEAT) { 89473714169d34a55fc01e14b30897a1ebbacf9934eErik model.mRrule = null; 89573714169d34a55fc01e14b30897a1ebbacf9934eErik return; 89673714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_CUSTOM) { 89773714169d34a55fc01e14b30897a1ebbacf9934eErik // Keep custom recurrence as before. 89873714169d34a55fc01e14b30897a1ebbacf9934eErik return; 89973714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_DAILY) { 90073714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.freq = EventRecurrence.DAILY; 90173714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_EVERY_WEEKDAY) { 90273714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.freq = EventRecurrence.WEEKLY; 90373714169d34a55fc01e14b30897a1ebbacf9934eErik int dayCount = 5; 90473714169d34a55fc01e14b30897a1ebbacf9934eErik int[] byday = new int[dayCount]; 90573714169d34a55fc01e14b30897a1ebbacf9934eErik int[] bydayNum = new int[dayCount]; 90673714169d34a55fc01e14b30897a1ebbacf9934eErik 90773714169d34a55fc01e14b30897a1ebbacf9934eErik byday[0] = EventRecurrence.MO; 90873714169d34a55fc01e14b30897a1ebbacf9934eErik byday[1] = EventRecurrence.TU; 90973714169d34a55fc01e14b30897a1ebbacf9934eErik byday[2] = EventRecurrence.WE; 91073714169d34a55fc01e14b30897a1ebbacf9934eErik byday[3] = EventRecurrence.TH; 91173714169d34a55fc01e14b30897a1ebbacf9934eErik byday[4] = EventRecurrence.FR; 91273714169d34a55fc01e14b30897a1ebbacf9934eErik for (int day = 0; day < dayCount; day++) { 91373714169d34a55fc01e14b30897a1ebbacf9934eErik bydayNum[day] = 0; 91473714169d34a55fc01e14b30897a1ebbacf9934eErik } 91573714169d34a55fc01e14b30897a1ebbacf9934eErik 91673714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.byday = byday; 91773714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayNum = bydayNum; 91873714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayCount = dayCount; 91973714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_WEEKLY_ON_DAY) { 92073714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.freq = EventRecurrence.WEEKLY; 92173714169d34a55fc01e14b30897a1ebbacf9934eErik int[] days = new int[1]; 92273714169d34a55fc01e14b30897a1ebbacf9934eErik int dayCount = 1; 92373714169d34a55fc01e14b30897a1ebbacf9934eErik int[] dayNum = new int[dayCount]; 92473714169d34a55fc01e14b30897a1ebbacf9934eErik Time startTime = new Time(model.mTimezone); 92573714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.set(model.mStart); 92673714169d34a55fc01e14b30897a1ebbacf9934eErik 92773714169d34a55fc01e14b30897a1ebbacf9934eErik days[0] = EventRecurrence.timeDay2Day(startTime.weekDay); 92873714169d34a55fc01e14b30897a1ebbacf9934eErik // not sure why this needs to be zero, but set it for now. 92973714169d34a55fc01e14b30897a1ebbacf9934eErik dayNum[0] = 0; 93073714169d34a55fc01e14b30897a1ebbacf9934eErik 93173714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.byday = days; 93273714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayNum = dayNum; 93373714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayCount = dayCount; 93473714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_MONTHLY_ON_DAY) { 93573714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.freq = EventRecurrence.MONTHLY; 93673714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayCount = 0; 93773714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bymonthdayCount = 1; 93873714169d34a55fc01e14b30897a1ebbacf9934eErik int[] bymonthday = new int[1]; 93973714169d34a55fc01e14b30897a1ebbacf9934eErik Time startTime = new Time(model.mTimezone); 94073714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.set(model.mStart); 94173714169d34a55fc01e14b30897a1ebbacf9934eErik bymonthday[0] = startTime.monthDay; 94273714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bymonthday = bymonthday; 94373714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_MONTHLY_ON_DAY_COUNT) { 94473714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.freq = EventRecurrence.MONTHLY; 94573714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayCount = 1; 94673714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bymonthdayCount = 0; 94773714169d34a55fc01e14b30897a1ebbacf9934eErik 94873714169d34a55fc01e14b30897a1ebbacf9934eErik int[] byday = new int[1]; 94973714169d34a55fc01e14b30897a1ebbacf9934eErik int[] bydayNum = new int[1]; 95073714169d34a55fc01e14b30897a1ebbacf9934eErik Time startTime = new Time(model.mTimezone); 95173714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.set(model.mStart); 95273714169d34a55fc01e14b30897a1ebbacf9934eErik // Compute the week number (for example, the "2nd" Monday) 95373714169d34a55fc01e14b30897a1ebbacf9934eErik int dayCount = 1 + ((startTime.monthDay - 1) / 7); 95473714169d34a55fc01e14b30897a1ebbacf9934eErik if (dayCount == 5) { 95573714169d34a55fc01e14b30897a1ebbacf9934eErik dayCount = -1; 95673714169d34a55fc01e14b30897a1ebbacf9934eErik } 95773714169d34a55fc01e14b30897a1ebbacf9934eErik bydayNum[0] = dayCount; 95873714169d34a55fc01e14b30897a1ebbacf9934eErik byday[0] = EventRecurrence.timeDay2Day(startTime.weekDay); 95973714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.byday = byday; 96073714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.bydayNum = bydayNum; 96173714169d34a55fc01e14b30897a1ebbacf9934eErik } else if (selection == REPEATS_YEARLY) { 96273714169d34a55fc01e14b30897a1ebbacf9934eErik eventRecurrence.freq = EventRecurrence.YEARLY; 96373714169d34a55fc01e14b30897a1ebbacf9934eErik } 96473714169d34a55fc01e14b30897a1ebbacf9934eErik 96573714169d34a55fc01e14b30897a1ebbacf9934eErik // Set the week start day. 9668e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang eventRecurrence.wkst = EventRecurrence.calendarDay2Day(weekStart); 96773714169d34a55fc01e14b30897a1ebbacf9934eErik model.mRrule = eventRecurrence.toString(); 96873714169d34a55fc01e14b30897a1ebbacf9934eErik } 96973714169d34a55fc01e14b30897a1ebbacf9934eErik 97073714169d34a55fc01e14b30897a1ebbacf9934eErik /** 971eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Uses an event cursor to fill in the given model This method assumes the 972eca82e9468c002590045338f98588ad3ce1a0bb3Erik * cursor used {@link #EVENT_PROJECTION} as it's query projection. It uses 973eca82e9468c002590045338f98588ad3ce1a0bb3Erik * the cursor to fill in the given model with all the information available. 97473714169d34a55fc01e14b30897a1ebbacf9934eErik * 97573714169d34a55fc01e14b30897a1ebbacf9934eErik * @param model The model to fill in 97673714169d34a55fc01e14b30897a1ebbacf9934eErik * @param cursor An event cursor that used {@link #EVENT_PROJECTION} for the query 97773714169d34a55fc01e14b30897a1ebbacf9934eErik */ 97888c483a0189cbe7132330cb1b563f07326f3bcf9Erik public static void setModelFromCursor(CalendarEventModel model, Cursor cursor) { 97973714169d34a55fc01e14b30897a1ebbacf9934eErik if (model == null || cursor == null || cursor.getCount() != 1) { 98073714169d34a55fc01e14b30897a1ebbacf9934eErik Log.wtf(TAG, "Attempted to build non-existent model or from an incorrect query."); 98173714169d34a55fc01e14b30897a1ebbacf9934eErik return; 98273714169d34a55fc01e14b30897a1ebbacf9934eErik } 98373714169d34a55fc01e14b30897a1ebbacf9934eErik 98473714169d34a55fc01e14b30897a1ebbacf9934eErik model.clear(); 98573714169d34a55fc01e14b30897a1ebbacf9934eErik cursor.moveToFirst(); 98673714169d34a55fc01e14b30897a1ebbacf9934eErik 98773714169d34a55fc01e14b30897a1ebbacf9934eErik model.mId = cursor.getInt(EVENT_INDEX_ID); 98873714169d34a55fc01e14b30897a1ebbacf9934eErik model.mTitle = cursor.getString(EVENT_INDEX_TITLE); 98973714169d34a55fc01e14b30897a1ebbacf9934eErik model.mDescription = cursor.getString(EVENT_INDEX_DESCRIPTION); 99073714169d34a55fc01e14b30897a1ebbacf9934eErik model.mLocation = cursor.getString(EVENT_INDEX_EVENT_LOCATION); 99173714169d34a55fc01e14b30897a1ebbacf9934eErik model.mAllDay = cursor.getInt(EVENT_INDEX_ALL_DAY) != 0; 99273714169d34a55fc01e14b30897a1ebbacf9934eErik model.mHasAlarm = cursor.getInt(EVENT_INDEX_HAS_ALARM) != 0; 99373714169d34a55fc01e14b30897a1ebbacf9934eErik model.mCalendarId = cursor.getInt(EVENT_INDEX_CALENDAR_ID); 99473714169d34a55fc01e14b30897a1ebbacf9934eErik model.mStart = cursor.getLong(EVENT_INDEX_DTSTART); 99573714169d34a55fc01e14b30897a1ebbacf9934eErik model.mTimezone = cursor.getString(EVENT_INDEX_TIMEZONE); 99673714169d34a55fc01e14b30897a1ebbacf9934eErik String rRule = cursor.getString(EVENT_INDEX_RRULE); 99773714169d34a55fc01e14b30897a1ebbacf9934eErik model.mRrule = rRule; 99873714169d34a55fc01e14b30897a1ebbacf9934eErik model.mSyncId = cursor.getString(EVENT_INDEX_SYNC_ID); 99973714169d34a55fc01e14b30897a1ebbacf9934eErik model.mTransparency = cursor.getInt(EVENT_INDEX_TRANSPARENCY) != 0; 100073714169d34a55fc01e14b30897a1ebbacf9934eErik int visibility = cursor.getInt(EVENT_INDEX_VISIBILITY); 100173714169d34a55fc01e14b30897a1ebbacf9934eErik model.mOwnerAccount = cursor.getString(EVENT_INDEX_OWNER_ACCOUNT); 100273714169d34a55fc01e14b30897a1ebbacf9934eErik model.mHasAttendeeData = cursor.getInt(EVENT_INDEX_HAS_ATTENDEE_DATA) != 0; 1003eca82e9468c002590045338f98588ad3ce1a0bb3Erik model.mOriginalEvent = cursor.getString(EVENT_INDEX_ORIGINAL_EVENT); 1004352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan model.mOrganizer = cursor.getString(EVENT_INDEX_ORGANIZER); 1005352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan model.mIsOrganizer = model.mOwnerAccount.equalsIgnoreCase(model.mOrganizer); 1006352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan model.mGuestsCanModify = cursor.getInt(EVENT_INDEX_GUESTS_CAN_MODIFY) != 0; 100773714169d34a55fc01e14b30897a1ebbacf9934eErik 100873714169d34a55fc01e14b30897a1ebbacf9934eErik if (visibility > 0) { 1009eca82e9468c002590045338f98588ad3ce1a0bb3Erik // For now the array contains the values 0, 2, and 3. We subtract 1010eca82e9468c002590045338f98588ad3ce1a0bb3Erik // one to make it easier to handle in code as 0,1,2. 101173714169d34a55fc01e14b30897a1ebbacf9934eErik // Default (0), Private (1), Public (2) 101273714169d34a55fc01e14b30897a1ebbacf9934eErik visibility--; 101373714169d34a55fc01e14b30897a1ebbacf9934eErik } 101473714169d34a55fc01e14b30897a1ebbacf9934eErik model.mVisibility = visibility; 101573714169d34a55fc01e14b30897a1ebbacf9934eErik 101673714169d34a55fc01e14b30897a1ebbacf9934eErik boolean hasRRule = !TextUtils.isEmpty(rRule); 101773714169d34a55fc01e14b30897a1ebbacf9934eErik 101873714169d34a55fc01e14b30897a1ebbacf9934eErik // We expect only one of these, so ignore the other 101973714169d34a55fc01e14b30897a1ebbacf9934eErik if (hasRRule) { 102073714169d34a55fc01e14b30897a1ebbacf9934eErik model.mDuration = cursor.getString(EVENT_INDEX_DURATION); 102173714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 102273714169d34a55fc01e14b30897a1ebbacf9934eErik model.mEnd = cursor.getLong(EVENT_INDEX_DTEND); 102373714169d34a55fc01e14b30897a1ebbacf9934eErik } 1024352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1025352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan model.mModelUpdatedWithEventCursor = true; 1026352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1027352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1028352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan /** 1029352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * Uses a calendar cursor to fill in the given model This method assumes the 1030352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * cursor used {@link #CALENDARS_PROJECTION} as it's query projection. It uses 1031352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * the cursor to fill in the given model with all the information available. 1032352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * 1033352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * @param model The model to fill in 1034352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * @param cursor An event cursor that used {@link #CALENDARS_PROJECTION} for the query 1035352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan * @return returns true if model was updated with the info in the cursor. 1036352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan */ 1037352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan public static boolean setModelFromCalendarCursor(CalendarEventModel model, Cursor cursor) { 1038352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (model == null || cursor == null) { 1039352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan Log.wtf(TAG, "Attempted to build non-existent model or from an incorrect query."); 1040352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1041352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1042352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1043352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (model.mCalendarId == -1) { 1044352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1045352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1046352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1047352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (!model.mModelUpdatedWithEventCursor) { 1048352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan Log.wtf(TAG, 1049352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan "Can't update model with a Calendar cursor until it has seen an Event cursor."); 1050352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1051352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1052352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1053352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan cursor.moveToPosition(-1); 1054352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan while (cursor.moveToNext()) { 1055352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (model.mCalendarId != cursor.getInt(EVENT_INDEX_CALENDAR_ID)) { 1056352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan continue; 1057352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1058352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1059352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan model.mOrganizerCanRespond = cursor.getInt(CALENDARS_INDEX_ORGANIZER_CAN_RESPOND) != 0; 1060352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1061352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan model.mCalendarAccessLevel = cursor.getInt(CALENDARS_INDEX_ACCESS_LEVEL); 1062352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1063352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return true; 1064352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1065352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1066352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1067352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1068352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan public static boolean canModifyEvent(CalendarEventModel model) { 1069352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return canModifyCalendar(model) 1070352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan && (model.mIsOrganizer || model.mGuestsCanModify); 1071352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1072352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1073352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan public static boolean canModifyCalendar(CalendarEventModel model) { 1074352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return model.mCalendarAccessLevel >= Calendars.CONTRIBUTOR_ACCESS 1075352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan || model.mCalendarId == -1; 1076352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1077352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1078352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan public static boolean canRespond(CalendarEventModel model) { 1079352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // For non-organizers, write permission to the calendar is sufficient. 1080352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // For organizers, the user needs a) write permission to the calendar 1081352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // AND b) ownerCanRespond == true AND c) attendee data exist 1082352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // (this means num of attendees > 1, the calendar owner's and others). 1083352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // Note that mAttendeeList omits the organizer. 1084352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1085352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // (there are more cases involved to be 100% accurate, such as 1086352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // paying attention to whether or not an attendee status was 1087352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // included in the feed, but we're currently omitting those corner cases 1088352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // for simplicity). 1089352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1090352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (!canModifyCalendar(model)) { 1091352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1092352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1093352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1094352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (!model.mIsOrganizer) { 1095352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return true; 1096352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1097352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1098352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (!model.mOrganizerCanRespond) { 1099352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1100352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1101352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1102352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // This means we don't have the attendees data so we can't send 1103352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan // the list of attendees and the status back to the server 1104352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan if (model.mHasAttendeeData && model.mAttendeesList.size() == 0) { 1105352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return false; 1106352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan } 1107352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan 1108352e1a2f834460bd54e03ce94cadd36a46debacbMichael Chan return true; 110973714169d34a55fc01e14b30897a1ebbacf9934eErik } 111073714169d34a55fc01e14b30897a1ebbacf9934eErik 111173714169d34a55fc01e14b30897a1ebbacf9934eErik /** 1112eca82e9468c002590045338f98588ad3ce1a0bb3Erik * Goes through an event model and fills in content values for saving This 1113eca82e9468c002590045338f98588ad3ce1a0bb3Erik * method will perform the initial collection of values from the model and 1114eca82e9468c002590045338f98588ad3ce1a0bb3Erik * put them into a set of ContentValues. It performs some basic work such as 1115eca82e9468c002590045338f98588ad3ce1a0bb3Erik * fixing the time on allDay events and choosing whether to use an rrule or 1116eca82e9468c002590045338f98588ad3ce1a0bb3Erik * dtend. 111773714169d34a55fc01e14b30897a1ebbacf9934eErik * 111873714169d34a55fc01e14b30897a1ebbacf9934eErik * @param model The complete model of the event you want to save 111973714169d34a55fc01e14b30897a1ebbacf9934eErik * @return values 112073714169d34a55fc01e14b30897a1ebbacf9934eErik */ 112173714169d34a55fc01e14b30897a1ebbacf9934eErik ContentValues getContentValuesFromModel(CalendarEventModel model) { 112273714169d34a55fc01e14b30897a1ebbacf9934eErik String title = model.mTitle; 112373714169d34a55fc01e14b30897a1ebbacf9934eErik boolean isAllDay = model.mAllDay; 112473714169d34a55fc01e14b30897a1ebbacf9934eErik String location = model.mLocation.trim(); 112573714169d34a55fc01e14b30897a1ebbacf9934eErik String description = model.mDescription.trim(); 112673714169d34a55fc01e14b30897a1ebbacf9934eErik String rrule = model.mRrule; 1127eca82e9468c002590045338f98588ad3ce1a0bb3Erik String timezone = model.mTimezone; 112873714169d34a55fc01e14b30897a1ebbacf9934eErik if (timezone == null) { 112973714169d34a55fc01e14b30897a1ebbacf9934eErik timezone = TimeZone.getDefault().getID(); 113073714169d34a55fc01e14b30897a1ebbacf9934eErik } 113173714169d34a55fc01e14b30897a1ebbacf9934eErik Time startTime = new Time(timezone); 113273714169d34a55fc01e14b30897a1ebbacf9934eErik Time endTime = new Time(timezone); 113373714169d34a55fc01e14b30897a1ebbacf9934eErik 113473714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.set(model.mStart); 113573714169d34a55fc01e14b30897a1ebbacf9934eErik endTime.set(model.mEnd); 113673714169d34a55fc01e14b30897a1ebbacf9934eErik 113773714169d34a55fc01e14b30897a1ebbacf9934eErik ContentValues values = new ContentValues(); 113873714169d34a55fc01e14b30897a1ebbacf9934eErik 113973714169d34a55fc01e14b30897a1ebbacf9934eErik long startMillis; 114073714169d34a55fc01e14b30897a1ebbacf9934eErik long endMillis; 114173714169d34a55fc01e14b30897a1ebbacf9934eErik long calendarId = model.mCalendarId; 114273714169d34a55fc01e14b30897a1ebbacf9934eErik if (isAllDay) { 114373714169d34a55fc01e14b30897a1ebbacf9934eErik // Reset start and end time, ensure at least 1 day duration, and set 114473714169d34a55fc01e14b30897a1ebbacf9934eErik // the timezone to UTC, as required for all-day events. 114573714169d34a55fc01e14b30897a1ebbacf9934eErik timezone = Time.TIMEZONE_UTC; 114673714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.hour = 0; 114773714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.minute = 0; 114873714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.second = 0; 114973714169d34a55fc01e14b30897a1ebbacf9934eErik startTime.timezone = timezone; 115073714169d34a55fc01e14b30897a1ebbacf9934eErik startMillis = startTime.normalize(true); 115173714169d34a55fc01e14b30897a1ebbacf9934eErik 115273714169d34a55fc01e14b30897a1ebbacf9934eErik endTime.hour = 0; 115373714169d34a55fc01e14b30897a1ebbacf9934eErik endTime.minute = 0; 115473714169d34a55fc01e14b30897a1ebbacf9934eErik endTime.second = 0; 115573714169d34a55fc01e14b30897a1ebbacf9934eErik if (endTime.monthDay == startTime.monthDay) { 115673714169d34a55fc01e14b30897a1ebbacf9934eErik endTime.monthDay++; 115773714169d34a55fc01e14b30897a1ebbacf9934eErik } 115873714169d34a55fc01e14b30897a1ebbacf9934eErik endTime.timezone = timezone; 115973714169d34a55fc01e14b30897a1ebbacf9934eErik endMillis = endTime.normalize(true); 116073714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 116173714169d34a55fc01e14b30897a1ebbacf9934eErik startMillis = startTime.toMillis(true); 116273714169d34a55fc01e14b30897a1ebbacf9934eErik endMillis = endTime.toMillis(true); 116373714169d34a55fc01e14b30897a1ebbacf9934eErik } 116473714169d34a55fc01e14b30897a1ebbacf9934eErik 116573714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.CALENDAR_ID, calendarId); 116673714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.EVENT_TIMEZONE, timezone); 116773714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.TITLE, title); 116873714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.ALL_DAY, isAllDay ? 1 : 0); 116973714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.DTSTART, startMillis); 117073714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.RRULE, rrule); 117173714169d34a55fc01e14b30897a1ebbacf9934eErik if (rrule != null) { 117273714169d34a55fc01e14b30897a1ebbacf9934eErik addRecurrenceRule(values, model); 117373714169d34a55fc01e14b30897a1ebbacf9934eErik } else { 1174eca82e9468c002590045338f98588ad3ce1a0bb3Erik values.put(Events.DURATION, (String) null); 117573714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.DTEND, endMillis); 117673714169d34a55fc01e14b30897a1ebbacf9934eErik } 117773714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.DESCRIPTION, description); 117873714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.EVENT_LOCATION, location); 117973714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.TRANSPARENCY, model.mTransparency ? 1 : 0); 118088c483a0189cbe7132330cb1b563f07326f3bcf9Erik values.put(Events.HAS_ATTENDEE_DATA, model.mHasAttendeeData ? 1 : 0); 118173714169d34a55fc01e14b30897a1ebbacf9934eErik 118273714169d34a55fc01e14b30897a1ebbacf9934eErik int visibility = model.mVisibility; 118373714169d34a55fc01e14b30897a1ebbacf9934eErik if (visibility > 0) { 118473714169d34a55fc01e14b30897a1ebbacf9934eErik // For now the array contains the values 0, 2, and 3. We add one to match. 118573714169d34a55fc01e14b30897a1ebbacf9934eErik // Default (0), Private (2), Public (3) 118673714169d34a55fc01e14b30897a1ebbacf9934eErik visibility++; 118773714169d34a55fc01e14b30897a1ebbacf9934eErik } 118873714169d34a55fc01e14b30897a1ebbacf9934eErik values.put(Events.VISIBILITY, visibility); 118973714169d34a55fc01e14b30897a1ebbacf9934eErik 119073714169d34a55fc01e14b30897a1ebbacf9934eErik return values; 119173714169d34a55fc01e14b30897a1ebbacf9934eErik } 119273714169d34a55fc01e14b30897a1ebbacf9934eErik 119373714169d34a55fc01e14b30897a1ebbacf9934eErik /** 119473714169d34a55fc01e14b30897a1ebbacf9934eErik * Takes an e-mail address and returns the domain (everything after the last @) 119573714169d34a55fc01e14b30897a1ebbacf9934eErik */ 119673714169d34a55fc01e14b30897a1ebbacf9934eErik public static String extractDomain(String email) { 119773714169d34a55fc01e14b30897a1ebbacf9934eErik int separator = email.lastIndexOf('@'); 119873714169d34a55fc01e14b30897a1ebbacf9934eErik if (separator != -1 && ++separator < email.length()) { 119973714169d34a55fc01e14b30897a1ebbacf9934eErik return email.substring(separator); 120073714169d34a55fc01e14b30897a1ebbacf9934eErik } 120173714169d34a55fc01e14b30897a1ebbacf9934eErik return null; 120273714169d34a55fc01e14b30897a1ebbacf9934eErik } 120379f228124de7d98146ca526d743436f6419e2365Erik 120479f228124de7d98146ca526d743436f6419e2365Erik public interface EditDoneRunnable extends Runnable { 120579f228124de7d98146ca526d743436f6419e2365Erik public void setDoneCode(int code); 120679f228124de7d98146ca526d743436f6419e2365Erik } 120773714169d34a55fc01e14b30897a1ebbacf9934eErik} 1208