CalendarContract.java revision 4d6f90ea503c65009b47881ed117f8c15c660cc0
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.provider;
18
19
20import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
22import android.app.Activity;
23import android.app.AlarmManager;
24import android.app.PendingIntent;
25import android.content.ContentProviderClient;
26import android.content.ContentResolver;
27import android.content.ContentUris;
28import android.content.ContentValues;
29import android.content.Context;
30import android.content.CursorEntityIterator;
31import android.content.Entity;
32import android.content.EntityIterator;
33import android.content.Intent;
34import android.database.Cursor;
35import android.database.DatabaseUtils;
36import android.net.Uri;
37import android.os.RemoteException;
38import android.text.format.DateUtils;
39import android.text.format.Time;
40import android.util.Log;
41
42/**
43 * <p>
44 * The contract between the calendar provider and applications. Contains
45 * definitions for the supported URIs and data columns.
46 * </p>
47 * <h3>Overview</h3>
48 * <p>
49 * CalendarContract defines the data model of calendar and event related
50 * information. This data is stored in a number of tables:
51 * </p>
52 * <ul>
53 * <li>The {@link Calendars} table holds the calendar specific information. Each
54 * row in this table contains the details for a single calendar, such as the
55 * name, color, sync info, etc.</li>
56 * <li>The {@link Events} table holds the event specific information. Each row
57 * in this table has the info for a single event. It contains information such
58 * as event title, location, start time, end time, etc. The event can occur
59 * one-time or can recur multiple times. Attendees, reminders, and extended
60 * properties are stored on separate tables and reference the {@link Events#_ID}
61 * to link them with the event.</li>
62 * <li>The {@link Instances} table holds the start and end time for occurrences
63 * of an event. Each row in this table represents a single occurrence. For
64 * one-time events there will be a 1:1 mapping of instances to events. For
65 * recurring events, multiple rows will automatically be generated which
66 * correspond to multiple occurrences of that event.</li>
67 * <li>The {@link Attendees} table holds the event attendee or guest
68 * information. Each row represents a single guest of an event. It specifies the
69 * type of guest they are and their attendance response for the event.</li>
70 * <li>The {@link Reminders} table holds the alert/notification data. Each row
71 * represents a single alert for an event. An event can have multiple reminders.
72 * The number of reminders per event is specified in
73 * {@link Calendars#MAX_REMINDERS} which is set by the Sync Adapter that owns
74 * the given calendar. Reminders are specified in minutes before the event and
75 * have a type.</li>
76 * <li>The {@link ExtendedProperties} table holds opaque data fields used by the
77 * sync adapter. The provider takes no action with items in this table except to
78 * delete them when their related events are deleted.</li>
79 * </ul>
80 * <p>
81 * Other tables include:
82 * </p>
83 * <ul>
84 * <li>
85 * {@link SyncState}, which contains free-form data maintained by the sync
86 * adapters</li>
87 * </ul>
88 *
89 */
90public final class CalendarContract {
91    private static final String TAG = "Calendar";
92
93    /**
94     * Broadcast Action: This is the intent that gets fired when an alarm
95     * notification needs to be posted for a reminder.
96     *
97     */
98    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
99    public static final String ACTION_EVENT_REMINDER = "android.intent.action.EVENT_REMINDER";
100
101    /**
102     * Activity Action: Display the event to the user in the custom app as
103     * specified in {@link EventsColumns#CUSTOM_APP_PACKAGE}. The custom app
104     * will be started via {@link Activity#startActivityForResult(Intent, int)}
105     * and it should call {@link Activity#setResult(int)} with
106     * {@link Activity#RESULT_OK} or {@link Activity#RESULT_CANCELED} to
107     * acknowledge whether the action was handled or not.
108     *
109     * The custom app should have an intent-filter like the following
110     * <pre>
111     * {@code
112     * <intent-filter>
113     *    <action android:name="android.provider.calendar.action.HANDLE_CUSTOM_EVENT" />
114     *    <category android:name="android.intent.category.DEFAULT" />
115     *    <data android:mimeType="vnd.android.cursor.item/event" />
116     * </intent-filter>
117     * }
118     * </pre>
119     * <p>
120     * Input: {@link Intent#getData} has the event URI. The extra
121     * {@link #EXTRA_EVENT_BEGIN_TIME} has the start time of the instance. The
122     * extra {@link #EXTRA_CUSTOM_APP_URI} will have the
123     * {@link EventsColumns#CUSTOM_APP_URI}.
124     * <p>
125     * Output: {@link Activity#RESULT_OK} if this was handled; otherwise
126     * {@link Activity#RESULT_CANCELED}
127     */
128    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
129    public static final String ACTION_HANDLE_CUSTOM_EVENT =
130        "android.provider.calendar.action.HANDLE_CUSTOM_EVENT";
131
132    /**
133     * Intent Extras key: {@link EventsColumns#CUSTOM_APP_URI} for the event in
134     * the {@link #ACTION_HANDLE_CUSTOM_EVENT} intent
135     */
136    public static final String EXTRA_CUSTOM_APP_URI = "customAppUri";
137
138    /**
139     * Intent Extras key: The start time of an event or an instance of a
140     * recurring event. (milliseconds since epoch)
141     */
142    public static final String EXTRA_EVENT_BEGIN_TIME = "beginTime";
143
144    /**
145     * Intent Extras key: The end time of an event or an instance of a recurring
146     * event. (milliseconds since epoch)
147     */
148    public static final String EXTRA_EVENT_END_TIME = "endTime";
149
150    /**
151     * Intent Extras key: When creating an event, set this to true to create an
152     * all-day event by default
153     */
154    public static final String EXTRA_EVENT_ALL_DAY = "allDay";
155
156    /**
157     * This authority is used for writing to or querying from the calendar
158     * provider. Note: This is set at first run and cannot be changed without
159     * breaking apps that access the provider.
160     */
161    public static final String AUTHORITY = "com.android.calendar";
162
163    /**
164     * The content:// style URL for the top-level calendar authority
165     */
166    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
167
168    /**
169     * An optional insert, update or delete URI parameter that allows the caller
170     * to specify that it is a sync adapter. The default value is false. If set
171     * to true, the modified row is not marked as "dirty" (needs to be synced)
172     * and when the provider calls
173     * {@link ContentResolver#notifyChange(android.net.Uri, android.database.ContentObserver, boolean)}
174     * , the third parameter "syncToNetwork" is set to false. Furthermore, if
175     * set to true, the caller must also include
176     * {@link Calendars#ACCOUNT_NAME} and {@link Calendars#ACCOUNT_TYPE} as
177     * query parameters.
178     *
179     * @see Uri.Builder#appendQueryParameter(java.lang.String, java.lang.String)
180     */
181    public static final String CALLER_IS_SYNCADAPTER = "caller_is_syncadapter";
182
183    /**
184     * A special account type for calendars not associated with any account.
185     * Normally calendars that do not match an account on the device will be
186     * removed. Setting the account_type on a calendar to this will prevent it
187     * from being wiped if it does not match an existing account.
188     *
189     * @see SyncColumns#ACCOUNT_TYPE
190     */
191    public static final String ACCOUNT_TYPE_LOCAL = "LOCAL";
192
193    /**
194     * This utility class cannot be instantiated
195     */
196    private CalendarContract() {}
197
198    /**
199     * Generic columns for use by sync adapters. The specific functions of these
200     * columns are private to the sync adapter. Other clients of the API should
201     * not attempt to either read or write this column. These columns are
202     * editable as part of the Calendars Uri, but can only be read if accessed
203     * through any other Uri.
204     */
205    protected interface CalendarSyncColumns {
206
207
208        /**
209         * Generic column for use by sync adapters. Column name.
210         * <P>Type: TEXT</P>
211         */
212        public static final String CAL_SYNC1 = "cal_sync1";
213
214        /**
215         * Generic column for use by sync adapters. Column name.
216         * <P>Type: TEXT</P>
217         */
218        public static final String CAL_SYNC2 = "cal_sync2";
219
220        /**
221         * Generic column for use by sync adapters. Column name.
222         * <P>Type: TEXT</P>
223         */
224        public static final String CAL_SYNC3 = "cal_sync3";
225
226        /**
227         * Generic column for use by sync adapters. Column name.
228         * <P>Type: TEXT</P>
229         */
230        public static final String CAL_SYNC4 = "cal_sync4";
231
232        /**
233         * Generic column for use by sync adapters. Column name.
234         * <P>Type: TEXT</P>
235         */
236        public static final String CAL_SYNC5 = "cal_sync5";
237
238        /**
239         * Generic column for use by sync adapters. Column name.
240         * <P>Type: TEXT</P>
241         */
242        public static final String CAL_SYNC6 = "cal_sync6";
243
244        /**
245         * Generic column for use by sync adapters. Column name.
246         * <P>Type: TEXT</P>
247         */
248        public static final String CAL_SYNC7 = "cal_sync7";
249
250        /**
251         * Generic column for use by sync adapters. Column name.
252         * <P>Type: TEXT</P>
253         */
254        public static final String CAL_SYNC8 = "cal_sync8";
255
256        /**
257         * Generic column for use by sync adapters. Column name.
258         * <P>Type: TEXT</P>
259         */
260        public static final String CAL_SYNC9 = "cal_sync9";
261
262        /**
263         * Generic column for use by sync adapters. Column name.
264         * <P>Type: TEXT</P>
265         */
266        public static final String CAL_SYNC10 = "cal_sync10";
267    }
268
269    /**
270     * Columns for Sync information used by Calendars and Events tables. These
271     * have specific uses which are expected to be consistent by the app and
272     * sync adapter.
273     *
274     */
275    protected interface SyncColumns extends CalendarSyncColumns {
276        /**
277         * The account that was used to sync the entry to the device. If the
278         * account_type is not {@link #ACCOUNT_TYPE_LOCAL} then the name and
279         * type must match an account on the device or the calendar will be
280         * deleted.
281         * <P>Type: TEXT</P>
282         */
283        public static final String ACCOUNT_NAME = "account_name";
284
285        /**
286         * The type of the account that was used to sync the entry to the
287         * device. A type of {@link #ACCOUNT_TYPE_LOCAL} will keep this event
288         * form being deleted if there are no matching accounts on the device.
289         * <P>Type: TEXT</P>
290         */
291        public static final String ACCOUNT_TYPE = "account_type";
292
293        /**
294         * The unique ID for a row assigned by the sync source. NULL if the row
295         * has never been synced. This is used as a reference id for exceptions
296         * along with {@link BaseColumns#_ID}.
297         * <P>Type: TEXT</P>
298         */
299        public static final String _SYNC_ID = "_sync_id";
300
301        /**
302         * Used to indicate that local, unsynced, changes are present.
303         * <P>Type: INTEGER (long)</P>
304         */
305        public static final String DIRTY = "dirty";
306
307        /**
308         * Whether the row has been deleted but not synced to the server. A
309         * deleted row should be ignored.
310         * <P>
311         * Type: INTEGER (boolean)
312         * </P>
313         */
314        public static final String DELETED = "deleted";
315
316        /**
317         * If set to 1 this causes events on this calendar to be duplicated with
318         * {@link Events#LAST_SYNCED} set to 1 whenever the event
319         * transitions from non-dirty to dirty. The duplicated event will not be
320         * expanded in the instances table and will only show up in sync adapter
321         * queries of the events table. It will also be deleted when the
322         * originating event has its dirty flag cleared by the sync adapter.
323         * <P>Type: INTEGER (boolean)</P>
324         */
325        public static final String CAN_PARTIALLY_UPDATE = "canPartiallyUpdate";
326    }
327
328    /**
329     * Columns specific to the Calendars Uri that other Uris can query.
330     */
331    protected interface CalendarColumns {
332        /**
333         * The color of the calendar. This should only be updated by the sync
334         * adapter, not other apps, as changing a calendar's color can adversely
335         * affect its display.
336         * <P>Type: INTEGER (color value)</P>
337         */
338        public static final String CALENDAR_COLOR = "calendar_color";
339
340        /**
341         * A key for looking up a color from the {@link Colors} table. NULL or
342         * an empty string are reserved for indicating that the calendar does
343         * not use a key for looking up the color. The provider will update
344         * {@link #CALENDAR_COLOR} automatically when a valid key is written to
345         * this column. The key must reference an existing row of the
346         * {@link Colors} table. @see Colors
347         * <P>
348         * Type: TEXT
349         * </P>
350         */
351        public static final String CALENDAR_COLOR_KEY = "calendar_color_index";
352
353        /**
354         * The display name of the calendar. Column name.
355         * <P>
356         * Type: TEXT
357         * </P>
358         */
359        public static final String CALENDAR_DISPLAY_NAME = "calendar_displayName";
360
361        /**
362         * The level of access that the user has for the calendar
363         * <P>Type: INTEGER (one of the values below)</P>
364         */
365        public static final String CALENDAR_ACCESS_LEVEL = "calendar_access_level";
366
367        /** Cannot access the calendar */
368        public static final int CAL_ACCESS_NONE = 0;
369        /** Can only see free/busy information about the calendar */
370        public static final int CAL_ACCESS_FREEBUSY = 100;
371        /** Can read all event details */
372        public static final int CAL_ACCESS_READ = 200;
373        /** Can reply yes/no/maybe to an event */
374        public static final int CAL_ACCESS_RESPOND = 300;
375        /** not used */
376        public static final int CAL_ACCESS_OVERRIDE = 400;
377        /** Full access to modify the calendar, but not the access control
378         * settings
379         */
380        public static final int CAL_ACCESS_CONTRIBUTOR = 500;
381        /** Full access to modify the calendar, but not the access control
382         * settings
383         */
384        public static final int CAL_ACCESS_EDITOR = 600;
385        /** Full access to the calendar */
386        public static final int CAL_ACCESS_OWNER = 700;
387        /** Domain admin */
388        public static final int CAL_ACCESS_ROOT = 800;
389
390        /**
391         * Is the calendar selected to be displayed?
392         * 0 - do not show events associated with this calendar.
393         * 1 - show events associated with this calendar
394         * <P>Type: INTEGER (boolean)</P>
395         */
396        public static final String VISIBLE = "visible";
397
398        /**
399         * The time zone the calendar is associated with.
400         * <P>Type: TEXT</P>
401         */
402        public static final String CALENDAR_TIME_ZONE = "calendar_timezone";
403
404        /**
405         * Is this calendar synced and are its events stored on the device?
406         * 0 - Do not sync this calendar or store events for this calendar.
407         * 1 - Sync down events for this calendar.
408         * <p>Type: INTEGER (boolean)</p>
409         */
410        public static final String SYNC_EVENTS = "sync_events";
411
412        /**
413         * The owner account for this calendar, based on the calendar feed.
414         * This will be different from the _SYNC_ACCOUNT for delegated calendars.
415         * Column name.
416         * <P>Type: String</P>
417         */
418        public static final String OWNER_ACCOUNT = "ownerAccount";
419
420        /**
421         * Can the organizer respond to the event?  If no, the status of the
422         * organizer should not be shown by the UI.  Defaults to 1. Column name.
423         * <P>Type: INTEGER (boolean)</P>
424         */
425        public static final String CAN_ORGANIZER_RESPOND = "canOrganizerRespond";
426
427        /**
428         * Can the organizer modify the time zone of the event? Column name.
429         * <P>Type: INTEGER (boolean)</P>
430        */
431        public static final String CAN_MODIFY_TIME_ZONE = "canModifyTimeZone";
432
433        /**
434         * The maximum number of reminders allowed for an event. Column name.
435         * <P>Type: INTEGER</P>
436         */
437        public static final String MAX_REMINDERS = "maxReminders";
438
439        /**
440         * A comma separated list of reminder methods supported for this
441         * calendar in the format "#,#,#". Valid types are
442         * {@link Reminders#METHOD_DEFAULT}, {@link Reminders#METHOD_ALERT},
443         * {@link Reminders#METHOD_EMAIL}, {@link Reminders#METHOD_SMS},
444         * {@link Reminders#METHOD_ALARM}. Column name.
445         * <P>Type: TEXT</P>
446         */
447        public static final String ALLOWED_REMINDERS = "allowedReminders";
448
449        /**
450         * A comma separated list of availability types supported for this
451         * calendar in the format "#,#,#". Valid types are
452         * {@link Events#AVAILABILITY_BUSY}, {@link Events#AVAILABILITY_FREE},
453         * {@link Events#AVAILABILITY_TENTATIVE}. Setting this field to only
454         * {@link Events#AVAILABILITY_BUSY} should be used to indicate that
455         * changing the availability is not supported.
456         *
457         */
458        public static final String ALLOWED_AVAILABILITY = "allowedAvailability";
459
460        /**
461         * A comma separated list of attendee types supported for this calendar
462         * in the format "#,#,#". Valid types are {@link Attendees#TYPE_NONE},
463         * {@link Attendees#TYPE_OPTIONAL}, {@link Attendees#TYPE_REQUIRED},
464         * {@link Attendees#TYPE_RESOURCE}. Setting this field to only
465         * {@link Attendees#TYPE_NONE} should be used to indicate that changing
466         * the attendee type is not supported.
467         *
468         */
469        public static final String ALLOWED_ATTENDEE_TYPES = "allowedAttendeeTypes";
470
471        /**
472         * Is this the primary calendar for this account. If this column is not explicitly set, the
473         * provider will return 1 if {@link Calendars#ACCOUNT_NAME} is equal to
474         * {@link Calendars#OWNER_ACCOUNT}.
475         */
476        public static final String IS_PRIMARY = "isPrimary";
477    }
478
479    /**
480     * Class that represents a Calendar Entity. There is one entry per calendar.
481     * This is a helper class to make batch operations easier.
482     */
483    public static final class CalendarEntity implements BaseColumns, SyncColumns, CalendarColumns {
484
485        /**
486         * The default Uri used when creating a new calendar EntityIterator.
487         */
488        @SuppressWarnings("hiding")
489        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY +
490                "/calendar_entities");
491
492        /**
493         * This utility class cannot be instantiated
494         */
495        private CalendarEntity() {}
496
497        /**
498         * Creates an entity iterator for the given cursor. It assumes the
499         * cursor contains a calendars query.
500         *
501         * @param cursor query on {@link #CONTENT_URI}
502         * @return an EntityIterator of calendars
503         */
504        public static EntityIterator newEntityIterator(Cursor cursor) {
505            return new EntityIteratorImpl(cursor);
506        }
507
508        private static class EntityIteratorImpl extends CursorEntityIterator {
509
510            public EntityIteratorImpl(Cursor cursor) {
511                super(cursor);
512            }
513
514            @Override
515            public Entity getEntityAndIncrementCursor(Cursor cursor) throws RemoteException {
516                // we expect the cursor is already at the row we need to read from
517                final long calendarId = cursor.getLong(cursor.getColumnIndexOrThrow(_ID));
518
519                // Create the content value
520                ContentValues cv = new ContentValues();
521                cv.put(_ID, calendarId);
522
523                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, ACCOUNT_NAME);
524                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, ACCOUNT_TYPE);
525
526                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, _SYNC_ID);
527                DatabaseUtils.cursorLongToContentValuesIfPresent(cursor, cv, DIRTY);
528
529                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC1);
530                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC2);
531                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC3);
532                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC4);
533                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC5);
534                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC6);
535                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC7);
536                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC8);
537                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC9);
538                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC10);
539
540                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, Calendars.NAME);
541                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv,
542                        Calendars.CALENDAR_DISPLAY_NAME);
543                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv,
544                        Calendars.CALENDAR_COLOR);
545                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, CALENDAR_ACCESS_LEVEL);
546                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, VISIBLE);
547                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, SYNC_EVENTS);
548                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv,
549                        Calendars.CALENDAR_LOCATION);
550                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CALENDAR_TIME_ZONE);
551                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv,
552                        Calendars.OWNER_ACCOUNT);
553                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv,
554                        Calendars.CAN_ORGANIZER_RESPOND);
555                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv,
556                        Calendars.CAN_MODIFY_TIME_ZONE);
557                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv,
558                        Calendars.MAX_REMINDERS);
559                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv,
560                        Calendars.CAN_PARTIALLY_UPDATE);
561                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv,
562                        Calendars.ALLOWED_REMINDERS);
563
564                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, DELETED);
565
566                // Create the Entity from the ContentValue
567                Entity entity = new Entity(cv);
568
569                // Set cursor to next row
570                cursor.moveToNext();
571
572                // Return the created Entity
573                return entity;
574            }
575        }
576     }
577
578    /**
579     * Constants and helpers for the Calendars table, which contains details for
580     * individual calendars. <h3>Operations</h3> All operations can be done
581     * either as an app or as a sync adapter. To perform an operation as a sync
582     * adapter {@link #CALLER_IS_SYNCADAPTER} should be set to true and
583     * {@link #ACCOUNT_NAME} and {@link #ACCOUNT_TYPE} must be set in the Uri
584     * parameters. See
585     * {@link Uri.Builder#appendQueryParameter(java.lang.String, java.lang.String)}
586     * for details on adding parameters. Sync adapters have write access to more
587     * columns but are restricted to a single account at a time. Calendars are
588     * designed to be primarily managed by a sync adapter and inserting new
589     * calendars should be done as a sync adapter. For the most part, apps
590     * should only update calendars (such as changing the color or display
591     * name). If a local calendar is required an app can do so by inserting as a
592     * sync adapter and using an {@link #ACCOUNT_TYPE} of
593     * {@link #ACCOUNT_TYPE_LOCAL} .
594     * <dl>
595     * <dt><b>Insert</b></dt>
596     * <dd>When inserting a new calendar the following fields must be included:
597     * <ul>
598     * <li>{@link #ACCOUNT_NAME}</li>
599     * <li>{@link #ACCOUNT_TYPE}</li>
600     * <li>{@link #NAME}</li>
601     * <li>{@link #CALENDAR_DISPLAY_NAME}</li>
602     * <li>{@link #CALENDAR_COLOR}</li>
603     * <li>{@link #CALENDAR_ACCESS_LEVEL}</li>
604     * <li>{@link #OWNER_ACCOUNT}</li>
605     * </ul>
606     * The following fields are not required when inserting a Calendar but are
607     * generally a good idea to include:
608     * <ul>
609     * <li>{@link #SYNC_EVENTS} set to 1</li>
610     * <li>{@link #CALENDAR_TIME_ZONE}</li>
611     * <li>{@link #ALLOWED_REMINDERS}</li>
612     * <li>{@link #ALLOWED_AVAILABILITY}</li>
613     * <li>{@link #ALLOWED_ATTENDEE_TYPES}</li>
614     * </ul>
615     * <dt><b>Update</b></dt>
616     * <dd>To perform an update on a calendar the {@link #_ID} of the calendar
617     * should be provided either as an appended id to the Uri (
618     * {@link ContentUris#withAppendedId}) or as the first selection item--the
619     * selection should start with "_id=?" and the first selectionArg should be
620     * the _id of the calendar. Calendars may also be updated using a selection
621     * without the id. In general, the {@link #ACCOUNT_NAME} and
622     * {@link #ACCOUNT_TYPE} should not be changed after a calendar is created
623     * as this can cause issues for sync adapters.
624     * <dt><b>Delete</b></dt>
625     * <dd>Calendars can be deleted either by the {@link #_ID} as an appended id
626     * on the Uri or using any standard selection. Deleting a calendar should
627     * generally be handled by a sync adapter as it will remove the calendar
628     * from the database and all associated data (aka events).</dd>
629     * <dt><b>Query</b></dt>
630     * <dd>Querying the Calendars table will get you all information about a set
631     * of calendars. There will be one row returned for each calendar that
632     * matches the query selection, or at most a single row if the {@link #_ID}
633     * is appended to the Uri.</dd>
634     * </dl>
635     * <h3>Calendar Columns</h3> The following Calendar columns are writable by
636     * both an app and a sync adapter.
637     * <ul>
638     * <li>{@link #NAME}</li>
639     * <li>{@link #CALENDAR_DISPLAY_NAME}</li>
640     * <li>{@link #VISIBLE}</li>
641     * <li>{@link #SYNC_EVENTS}</li>
642     * </ul>
643     * The following Calendars columns are writable only by a sync adapter
644     * <ul>
645     * <li>{@link #ACCOUNT_NAME}</li>
646     * <li>{@link #ACCOUNT_TYPE}</li>
647     * <li>{@link #CALENDAR_COLOR}</li>
648     * <li>{@link #_SYNC_ID}</li>
649     * <li>{@link #DIRTY}</li>
650     * <li>{@link #OWNER_ACCOUNT}</li>
651     * <li>{@link #MAX_REMINDERS}</li>
652     * <li>{@link #ALLOWED_REMINDERS}</li>
653     * <li>{@link #ALLOWED_AVAILABILITY}</li>
654     * <li>{@link #ALLOWED_ATTENDEE_TYPES}</li>
655     * <li>{@link #CAN_MODIFY_TIME_ZONE}</li>
656     * <li>{@link #CAN_ORGANIZER_RESPOND}</li>
657     * <li>{@link #CAN_PARTIALLY_UPDATE}</li>
658     * <li>{@link #CALENDAR_LOCATION}</li>
659     * <li>{@link #CALENDAR_TIME_ZONE}</li>
660     * <li>{@link #CALENDAR_ACCESS_LEVEL}</li>
661     * <li>{@link #DELETED}</li>
662     * <li>{@link #CAL_SYNC1}</li>
663     * <li>{@link #CAL_SYNC2}</li>
664     * <li>{@link #CAL_SYNC3}</li>
665     * <li>{@link #CAL_SYNC4}</li>
666     * <li>{@link #CAL_SYNC5}</li>
667     * <li>{@link #CAL_SYNC6}</li>
668     * <li>{@link #CAL_SYNC7}</li>
669     * <li>{@link #CAL_SYNC8}</li>
670     * <li>{@link #CAL_SYNC9}</li>
671     * <li>{@link #CAL_SYNC10}</li>
672     * </ul>
673     */
674    public static final class Calendars implements BaseColumns, SyncColumns, CalendarColumns {
675
676        /**
677         * This utility class cannot be instantiated
678         */
679        private Calendars() {}
680
681        /**
682         * The content:// style URL for accessing Calendars
683         */
684        @SuppressWarnings("hiding")
685        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/calendars");
686
687        /**
688         * The default sort order for this table
689         */
690        public static final String DEFAULT_SORT_ORDER = CALENDAR_DISPLAY_NAME;
691
692        /**
693         * The name of the calendar. Column name.
694         * <P>Type: TEXT</P>
695         */
696        public static final String NAME = "name";
697
698        /**
699         * The default location for the calendar. Column name.
700         * <P>Type: TEXT</P>
701         */
702        public static final String CALENDAR_LOCATION = "calendar_location";
703
704        /**
705         * These fields are only writable by a sync adapter. To modify them the
706         * caller must include {@link #CALLER_IS_SYNCADAPTER},
707         * {@link #ACCOUNT_NAME}, and {@link #ACCOUNT_TYPE} in the Uri's query
708         * parameters. TODO move to provider
709         *
710         * @hide
711         */
712        public static final String[] SYNC_WRITABLE_COLUMNS = new String[] {
713            ACCOUNT_NAME,
714            ACCOUNT_TYPE,
715            _SYNC_ID,
716            DIRTY,
717            OWNER_ACCOUNT,
718            MAX_REMINDERS,
719            ALLOWED_REMINDERS,
720            CAN_MODIFY_TIME_ZONE,
721            CAN_ORGANIZER_RESPOND,
722            CAN_PARTIALLY_UPDATE,
723            CALENDAR_LOCATION,
724            CALENDAR_TIME_ZONE,
725            CALENDAR_ACCESS_LEVEL,
726            DELETED,
727            CAL_SYNC1,
728            CAL_SYNC2,
729            CAL_SYNC3,
730            CAL_SYNC4,
731            CAL_SYNC5,
732            CAL_SYNC6,
733            CAL_SYNC7,
734            CAL_SYNC8,
735            CAL_SYNC9,
736            CAL_SYNC10,
737        };
738    }
739
740    /**
741     * Columns from the Attendees table that other tables join into themselves.
742     */
743    protected interface AttendeesColumns {
744
745        /**
746         * The id of the event. Column name.
747         * <P>Type: INTEGER</P>
748         */
749        public static final String EVENT_ID = "event_id";
750
751        /**
752         * The name of the attendee. Column name.
753         * <P>Type: STRING</P>
754         */
755        public static final String ATTENDEE_NAME = "attendeeName";
756
757        /**
758         * The email address of the attendee. Column name.
759         * <P>Type: STRING</P>
760         */
761        public static final String ATTENDEE_EMAIL = "attendeeEmail";
762
763        /**
764         * The relationship of the attendee to the user. Column name.
765         * <P>Type: INTEGER (one of {@link #RELATIONSHIP_ATTENDEE}, ...}.</P>
766         */
767        public static final String ATTENDEE_RELATIONSHIP = "attendeeRelationship";
768
769        public static final int RELATIONSHIP_NONE = 0;
770        public static final int RELATIONSHIP_ATTENDEE = 1;
771        public static final int RELATIONSHIP_ORGANIZER = 2;
772        public static final int RELATIONSHIP_PERFORMER = 3;
773        public static final int RELATIONSHIP_SPEAKER = 4;
774
775        /**
776         * The type of attendee. Column name.
777         * <P>
778         * Type: Integer (one of {@link #TYPE_NONE}, {@link #TYPE_REQUIRED},
779         * {@link #TYPE_OPTIONAL}, {@link #TYPE_RESOURCE})
780         * </P>
781         */
782        public static final String ATTENDEE_TYPE = "attendeeType";
783
784        public static final int TYPE_NONE = 0;
785        public static final int TYPE_REQUIRED = 1;
786        public static final int TYPE_OPTIONAL = 2;
787        /**
788         * This specifies that an attendee is a resource, like a room, a
789         * cabbage, or something and not an actual person.
790         */
791        public static final int TYPE_RESOURCE = 3;
792
793        /**
794         * The attendance status of the attendee. Column name.
795         * <P>Type: Integer (one of {@link #ATTENDEE_STATUS_ACCEPTED}, ...).</P>
796         */
797        public static final String ATTENDEE_STATUS = "attendeeStatus";
798
799        public static final int ATTENDEE_STATUS_NONE = 0;
800        public static final int ATTENDEE_STATUS_ACCEPTED = 1;
801        public static final int ATTENDEE_STATUS_DECLINED = 2;
802        public static final int ATTENDEE_STATUS_INVITED = 3;
803        public static final int ATTENDEE_STATUS_TENTATIVE = 4;
804
805        /**
806         * The identity of the attendee as referenced in
807         * {@link ContactsContract.CommonDataKinds.Identity#IDENTITY}.
808         * This is required only if {@link #ATTENDEE_ID_NAMESPACE} is present. Column name.
809         * <P>Type: STRING</P>
810         */
811        public static final String ATTENDEE_IDENTITY = "attendeeIdentity";
812
813        /**
814         * The identity name space of the attendee as referenced in
815         * {@link ContactsContract.CommonDataKinds.Identity#NAMESPACE}.
816         * This is required only if {@link #ATTENDEE_IDENTITY} is present. Column name.
817         * <P>Type: STRING</P>
818         */
819        public static final String ATTENDEE_ID_NAMESPACE = "attendeeIdNamespace";
820    }
821
822    /**
823     * Fields and helpers for interacting with Attendees. Each row of this table
824     * represents a single attendee or guest of an event. Calling
825     * {@link #query(ContentResolver, long, String[])} will return a list of attendees for
826     * the event with the given eventId. Both apps and sync adapters may write
827     * to this table. There are six writable fields and all of them except
828     * {@link #ATTENDEE_NAME} must be included when inserting a new attendee.
829     * They are:
830     * <ul>
831     * <li>{@link #EVENT_ID}</li>
832     * <li>{@link #ATTENDEE_NAME}</li>
833     * <li>{@link #ATTENDEE_EMAIL}</li>
834     * <li>{@link #ATTENDEE_RELATIONSHIP}</li>
835     * <li>{@link #ATTENDEE_TYPE}</li>
836     * <li>{@link #ATTENDEE_STATUS}</li>
837     * <li>{@link #ATTENDEE_IDENTITY}</li>
838     * <li>{@link #ATTENDEE_ID_NAMESPACE}</li>
839     * </ul>
840     */
841    public static final class Attendees implements BaseColumns, AttendeesColumns, EventsColumns {
842
843        /**
844         * The content:// style URL for accessing Attendees data
845         */
846        @SuppressWarnings("hiding")
847        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/attendees");
848        private static final String ATTENDEES_WHERE = Attendees.EVENT_ID + "=?";
849
850        /**
851         * This utility class cannot be instantiated
852         */
853        private Attendees() {}
854
855        /**
856         * Queries all attendees associated with the given event. This is a
857         * blocking call and should not be done on the UI thread.
858         *
859         * @param cr The content resolver to use for the query
860         * @param eventId The id of the event to retrieve attendees for
861         * @param projection the columns to return in the cursor
862         * @return A Cursor containing all attendees for the event
863         */
864        public static final Cursor query(ContentResolver cr, long eventId, String[] projection) {
865            String[] attArgs = {Long.toString(eventId)};
866            return cr.query(CONTENT_URI, projection, ATTENDEES_WHERE, attArgs /* selection args */,
867                    null /* sort order */);
868        }
869    }
870
871    /**
872     * Columns from the Events table that other tables join into themselves.
873     */
874    protected interface EventsColumns {
875
876        /**
877         * The {@link Calendars#_ID} of the calendar the event belongs to.
878         * Column name.
879         * <P>Type: INTEGER</P>
880         */
881        public static final String CALENDAR_ID = "calendar_id";
882
883        /**
884         * The title of the event. Column name.
885         * <P>Type: TEXT</P>
886         */
887        public static final String TITLE = "title";
888
889        /**
890         * The description of the event. Column name.
891         * <P>Type: TEXT</P>
892         */
893        public static final String DESCRIPTION = "description";
894
895        /**
896         * Where the event takes place. Column name.
897         * <P>Type: TEXT</P>
898         */
899        public static final String EVENT_LOCATION = "eventLocation";
900
901        /**
902         * A secondary color for the individual event. This should only be
903         * updated by the sync adapter for a given account.
904         * <P>Type: INTEGER</P>
905         */
906        public static final String EVENT_COLOR = "eventColor";
907
908        /**
909         * A secondary color key for the individual event. NULL or an empty
910         * string are reserved for indicating that the event does not use a key
911         * for looking up the color. The provider will update
912         * {@link #EVENT_COLOR} automatically when a valid key is written to
913         * this column. The key must reference an existing row of the
914         * {@link Colors} table. @see Colors
915         * <P>
916         * Type: TEXT
917         * </P>
918         */
919        public static final String EVENT_COLOR_KEY = "eventColor_index";
920
921        /**
922         * This will be {@link #EVENT_COLOR} if it is not null; otherwise, this will be
923         * {@link Calendars#CALENDAR_COLOR}.
924         * Read-only value. To modify, write to {@link #EVENT_COLOR} or
925         * {@link Calendars#CALENDAR_COLOR} directly.
926         *<P>
927         *     Type: INTEGER
928         *</P>
929         */
930        public static final String DISPLAY_COLOR = "displayColor";
931
932        /**
933         * The event status. Column name.
934         * <P>Type: INTEGER (one of {@link #STATUS_TENTATIVE}...)</P>
935         */
936        public static final String STATUS = "eventStatus";
937
938        public static final int STATUS_TENTATIVE = 0;
939        public static final int STATUS_CONFIRMED = 1;
940        public static final int STATUS_CANCELED = 2;
941
942        /**
943         * This is a copy of the attendee status for the owner of this event.
944         * This field is copied here so that we can efficiently filter out
945         * events that are declined without having to look in the Attendees
946         * table. Column name.
947         *
948         * <P>Type: INTEGER (int)</P>
949         */
950        public static final String SELF_ATTENDEE_STATUS = "selfAttendeeStatus";
951
952        /**
953         * This column is available for use by sync adapters. Column name.
954         * <P>Type: TEXT</P>
955         */
956        public static final String SYNC_DATA1 = "sync_data1";
957
958        /**
959         * This column is available for use by sync adapters. Column name.
960         * <P>Type: TEXT</P>
961         */
962        public static final String SYNC_DATA2 = "sync_data2";
963
964        /**
965         * This column is available for use by sync adapters. Column name.
966         * <P>Type: TEXT</P>
967         */
968        public static final String SYNC_DATA3 = "sync_data3";
969
970        /**
971         * This column is available for use by sync adapters. Column name.
972         * <P>Type: TEXT</P>
973         */
974        public static final String SYNC_DATA4 = "sync_data4";
975
976        /**
977         * This column is available for use by sync adapters. Column name.
978         * <P>Type: TEXT</P>
979         */
980        public static final String SYNC_DATA5 = "sync_data5";
981
982        /**
983         * This column is available for use by sync adapters. Column name.
984         * <P>Type: TEXT</P>
985         */
986        public static final String SYNC_DATA6 = "sync_data6";
987
988        /**
989         * This column is available for use by sync adapters. Column name.
990         * <P>Type: TEXT</P>
991         */
992        public static final String SYNC_DATA7 = "sync_data7";
993
994        /**
995         * This column is available for use by sync adapters. Column name.
996         * <P>Type: TEXT</P>
997         */
998        public static final String SYNC_DATA8 = "sync_data8";
999
1000        /**
1001         * This column is available for use by sync adapters. Column name.
1002         * <P>Type: TEXT</P>
1003         */
1004        public static final String SYNC_DATA9 = "sync_data9";
1005
1006        /**
1007         * This column is available for use by sync adapters. Column name.
1008         * <P>Type: TEXT</P>
1009         */
1010        public static final String SYNC_DATA10 = "sync_data10";
1011
1012        /**
1013         * Used to indicate that a row is not a real event but an original copy of a locally
1014         * modified event. A copy is made when an event changes from non-dirty to dirty and the
1015         * event is on a calendar with {@link Calendars#CAN_PARTIALLY_UPDATE} set to 1. This copy
1016         * does not get expanded in the instances table and is only visible in queries made by a
1017         * sync adapter. The copy gets removed when the event is changed back to non-dirty by a
1018         * sync adapter.
1019         * <P>Type: INTEGER (boolean)</P>
1020         */
1021        public static final String LAST_SYNCED = "lastSynced";
1022
1023        /**
1024         * The time the event starts in UTC millis since epoch. Column name.
1025         * <P>Type: INTEGER (long; millis since epoch)</P>
1026         */
1027        public static final String DTSTART = "dtstart";
1028
1029        /**
1030         * The time the event ends in UTC millis since epoch. Column name.
1031         * <P>Type: INTEGER (long; millis since epoch)</P>
1032         */
1033        public static final String DTEND = "dtend";
1034
1035        /**
1036         * The duration of the event in RFC2445 format. Column name.
1037         * <P>Type: TEXT (duration in RFC2445 format)</P>
1038         */
1039        public static final String DURATION = "duration";
1040
1041        /**
1042         * The timezone for the event. Column name.
1043         * <P>Type: TEXT</P>
1044         */
1045        public static final String EVENT_TIMEZONE = "eventTimezone";
1046
1047        /**
1048         * The timezone for the end time of the event. Column name.
1049         * <P>Type: TEXT</P>
1050         */
1051        public static final String EVENT_END_TIMEZONE = "eventEndTimezone";
1052
1053        /**
1054         * Is the event all day (time zone independent). Column name.
1055         * <P>Type: INTEGER (boolean)</P>
1056         */
1057        public static final String ALL_DAY = "allDay";
1058
1059        /**
1060         * Defines how the event shows up for others when the calendar is
1061         * shared. Column name.
1062         * <P>Type: INTEGER (One of {@link #ACCESS_DEFAULT}, ...)</P>
1063         */
1064        public static final String ACCESS_LEVEL = "accessLevel";
1065
1066        /**
1067         * Default access is controlled by the server and will be treated as
1068         * public on the device.
1069         */
1070        public static final int ACCESS_DEFAULT = 0;
1071        /**
1072         * Confidential is not used by the app.
1073         */
1074        public static final int ACCESS_CONFIDENTIAL = 1;
1075        /**
1076         * Private shares the event as a free/busy slot with no details.
1077         */
1078        public static final int ACCESS_PRIVATE = 2;
1079        /**
1080         * Public makes the contents visible to anyone with access to the
1081         * calendar.
1082         */
1083        public static final int ACCESS_PUBLIC = 3;
1084
1085        /**
1086         * If this event counts as busy time or is still free time that can be
1087         * scheduled over. Column name.
1088         * <P>
1089         * Type: INTEGER (One of {@link #AVAILABILITY_BUSY},
1090         * {@link #AVAILABILITY_FREE}, {@link #AVAILABILITY_TENTATIVE})
1091         * </P>
1092         */
1093        public static final String AVAILABILITY = "availability";
1094
1095        /**
1096         * Indicates that this event takes up time and will conflict with other
1097         * events.
1098         */
1099        public static final int AVAILABILITY_BUSY = 0;
1100        /**
1101         * Indicates that this event is free time and will not conflict with
1102         * other events.
1103         */
1104        public static final int AVAILABILITY_FREE = 1;
1105        /**
1106         * Indicates that the owner's availability may change, but should be
1107         * considered busy time that will conflict.
1108         */
1109        public static final int AVAILABILITY_TENTATIVE = 2;
1110
1111        /**
1112         * Whether the event has an alarm or not. Column name.
1113         * <P>Type: INTEGER (boolean)</P>
1114         */
1115        public static final String HAS_ALARM = "hasAlarm";
1116
1117        /**
1118         * Whether the event has extended properties or not. Column name.
1119         * <P>Type: INTEGER (boolean)</P>
1120         */
1121        public static final String HAS_EXTENDED_PROPERTIES = "hasExtendedProperties";
1122
1123        /**
1124         * The recurrence rule for the event. Column name.
1125         * <P>Type: TEXT</P>
1126         */
1127        public static final String RRULE = "rrule";
1128
1129        /**
1130         * The recurrence dates for the event. Column name.
1131         * <P>Type: TEXT</P>
1132         */
1133        public static final String RDATE = "rdate";
1134
1135        /**
1136         * The recurrence exception rule for the event. Column name.
1137         * <P>Type: TEXT</P>
1138         */
1139        public static final String EXRULE = "exrule";
1140
1141        /**
1142         * The recurrence exception dates for the event. Column name.
1143         * <P>Type: TEXT</P>
1144         */
1145        public static final String EXDATE = "exdate";
1146
1147        /**
1148         * The {@link Events#_ID} of the original recurring event for which this
1149         * event is an exception. Column name.
1150         * <P>Type: TEXT</P>
1151         */
1152        public static final String ORIGINAL_ID = "original_id";
1153
1154        /**
1155         * The _sync_id of the original recurring event for which this event is
1156         * an exception. The provider should keep the original_id in sync when
1157         * this is updated. Column name.
1158         * <P>Type: TEXT</P>
1159         */
1160        public static final String ORIGINAL_SYNC_ID = "original_sync_id";
1161
1162        /**
1163         * The original instance time of the recurring event for which this
1164         * event is an exception. Column name.
1165         * <P>Type: INTEGER (long; millis since epoch)</P>
1166         */
1167        public static final String ORIGINAL_INSTANCE_TIME = "originalInstanceTime";
1168
1169        /**
1170         * The allDay status (true or false) of the original recurring event
1171         * for which this event is an exception. Column name.
1172         * <P>Type: INTEGER (boolean)</P>
1173         */
1174        public static final String ORIGINAL_ALL_DAY = "originalAllDay";
1175
1176        /**
1177         * The last date this event repeats on, or NULL if it never ends. Column
1178         * name.
1179         * <P>Type: INTEGER (long; millis since epoch)</P>
1180         */
1181        public static final String LAST_DATE = "lastDate";
1182
1183        /**
1184         * Whether the event has attendee information.  True if the event
1185         * has full attendee data, false if the event has information about
1186         * self only. Column name.
1187         * <P>Type: INTEGER (boolean)</P>
1188         */
1189        public static final String HAS_ATTENDEE_DATA = "hasAttendeeData";
1190
1191        /**
1192         * Whether guests can modify the event. Column name.
1193         * <P>Type: INTEGER (boolean)</P>
1194         */
1195        public static final String GUESTS_CAN_MODIFY = "guestsCanModify";
1196
1197        /**
1198         * Whether guests can invite other guests. Column name.
1199         * <P>Type: INTEGER (boolean)</P>
1200         */
1201        public static final String GUESTS_CAN_INVITE_OTHERS = "guestsCanInviteOthers";
1202
1203        /**
1204         * Whether guests can see the list of attendees. Column name.
1205         * <P>Type: INTEGER (boolean)</P>
1206         */
1207        public static final String GUESTS_CAN_SEE_GUESTS = "guestsCanSeeGuests";
1208
1209        /**
1210         * Email of the organizer (owner) of the event. Column name.
1211         * <P>Type: STRING</P>
1212         */
1213        public static final String ORGANIZER = "organizer";
1214
1215        /**
1216         * Are we the organizer of this event. If this column is not explicitly set, the provider
1217         * will return 1 if {@link #ORGANIZER} is equal to {@link Calendars#OWNER_ACCOUNT}.
1218         * Column name.
1219         * <P>Type: STRING</P>
1220         */
1221        public static final String IS_ORGANIZER = "isOrganizer";
1222
1223        /**
1224         * Whether the user can invite others to the event. The
1225         * GUESTS_CAN_INVITE_OTHERS is a setting that applies to an arbitrary
1226         * guest, while CAN_INVITE_OTHERS indicates if the user can invite
1227         * others (either through GUESTS_CAN_INVITE_OTHERS or because the user
1228         * has modify access to the event). Column name.
1229         * <P>Type: INTEGER (boolean, readonly)</P>
1230         */
1231        public static final String CAN_INVITE_OTHERS = "canInviteOthers";
1232
1233        /**
1234         * The package name of the custom app that can provide a richer
1235         * experience for the event. See the ACTION TYPE
1236         * {@link CalendarContract#ACTION_HANDLE_CUSTOM_EVENT} for details.
1237         * Column name.
1238         * <P> Type: TEXT </P>
1239         */
1240        public static final String CUSTOM_APP_PACKAGE = "customAppPackage";
1241
1242        /**
1243         * The URI used by the custom app for the event. Column name.
1244         * <P>Type: TEXT</P>
1245         */
1246        public static final String CUSTOM_APP_URI = "customAppUri";
1247
1248        /**
1249         * The UID for events added from the RFC 2445 iCalendar format.
1250         * Column name.
1251         * <P>Type: TEXT</P>
1252         */
1253        public static final String UID_2445 = "uid2445";
1254    }
1255
1256    /**
1257     * Class that represents an Event Entity. There is one entry per event.
1258     * Recurring events show up as a single entry. This is a helper class to
1259     * make batch operations easier. A {@link ContentResolver} or
1260     * {@link ContentProviderClient} is required as the helper does additional
1261     * queries to add reminders and attendees to each entry.
1262     */
1263    public static final class EventsEntity implements BaseColumns, SyncColumns, EventsColumns {
1264        /**
1265         * The content:// style URL for this table
1266         */
1267        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY +
1268                "/event_entities");
1269
1270        /**
1271         * This utility class cannot be instantiated
1272         */
1273        private EventsEntity() {}
1274
1275        /**
1276         * Creates a new iterator for events
1277         *
1278         * @param cursor An event query
1279         * @param resolver For performing additional queries
1280         * @return an EntityIterator containing one entity per event in the
1281         *         cursor
1282         */
1283        public static EntityIterator newEntityIterator(Cursor cursor, ContentResolver resolver) {
1284            return new EntityIteratorImpl(cursor, resolver);
1285        }
1286
1287        /**
1288         * Creates a new iterator for events
1289         *
1290         * @param cursor An event query
1291         * @param provider For performing additional queries
1292         * @return an EntityIterator containing one entity per event in the
1293         *         cursor
1294         */
1295        public static EntityIterator newEntityIterator(Cursor cursor,
1296                ContentProviderClient provider) {
1297            return new EntityIteratorImpl(cursor, provider);
1298        }
1299
1300        private static class EntityIteratorImpl extends CursorEntityIterator {
1301            private final ContentResolver mResolver;
1302            private final ContentProviderClient mProvider;
1303
1304            private static final String[] REMINDERS_PROJECTION = new String[] {
1305                    Reminders.MINUTES,
1306                    Reminders.METHOD,
1307            };
1308            private static final int COLUMN_MINUTES = 0;
1309            private static final int COLUMN_METHOD = 1;
1310
1311            private static final String[] ATTENDEES_PROJECTION = new String[] {
1312                    Attendees.ATTENDEE_NAME,
1313                    Attendees.ATTENDEE_EMAIL,
1314                    Attendees.ATTENDEE_RELATIONSHIP,
1315                    Attendees.ATTENDEE_TYPE,
1316                    Attendees.ATTENDEE_STATUS,
1317                    Attendees.ATTENDEE_IDENTITY,
1318                    Attendees.ATTENDEE_ID_NAMESPACE
1319            };
1320            private static final int COLUMN_ATTENDEE_NAME = 0;
1321            private static final int COLUMN_ATTENDEE_EMAIL = 1;
1322            private static final int COLUMN_ATTENDEE_RELATIONSHIP = 2;
1323            private static final int COLUMN_ATTENDEE_TYPE = 3;
1324            private static final int COLUMN_ATTENDEE_STATUS = 4;
1325            private static final int COLUMN_ATTENDEE_IDENTITY = 5;
1326            private static final int COLUMN_ATTENDEE_ID_NAMESPACE = 6;
1327
1328            private static final String[] EXTENDED_PROJECTION = new String[] {
1329                    ExtendedProperties._ID,
1330                    ExtendedProperties.NAME,
1331                    ExtendedProperties.VALUE
1332            };
1333            private static final int COLUMN_ID = 0;
1334            private static final int COLUMN_NAME = 1;
1335            private static final int COLUMN_VALUE = 2;
1336
1337            private static final String WHERE_EVENT_ID = "event_id=?";
1338
1339            public EntityIteratorImpl(Cursor cursor, ContentResolver resolver) {
1340                super(cursor);
1341                mResolver = resolver;
1342                mProvider = null;
1343            }
1344
1345            public EntityIteratorImpl(Cursor cursor, ContentProviderClient provider) {
1346                super(cursor);
1347                mResolver = null;
1348                mProvider = provider;
1349            }
1350
1351            @Override
1352            public Entity getEntityAndIncrementCursor(Cursor cursor) throws RemoteException {
1353                // we expect the cursor is already at the row we need to read from
1354                final long eventId = cursor.getLong(cursor.getColumnIndexOrThrow(Events._ID));
1355                ContentValues cv = new ContentValues();
1356                cv.put(Events._ID, eventId);
1357                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, CALENDAR_ID);
1358                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, TITLE);
1359                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, DESCRIPTION);
1360                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, EVENT_LOCATION);
1361                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, STATUS);
1362                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, SELF_ATTENDEE_STATUS);
1363                DatabaseUtils.cursorLongToContentValuesIfPresent(cursor, cv, DTSTART);
1364                DatabaseUtils.cursorLongToContentValuesIfPresent(cursor, cv, DTEND);
1365                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, DURATION);
1366                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, EVENT_TIMEZONE);
1367                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, EVENT_END_TIMEZONE);
1368                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, ALL_DAY);
1369                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, ACCESS_LEVEL);
1370                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, AVAILABILITY);
1371                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, HAS_ALARM);
1372                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv,
1373                        HAS_EXTENDED_PROPERTIES);
1374                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, RRULE);
1375                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, RDATE);
1376                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, EXRULE);
1377                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, EXDATE);
1378                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, ORIGINAL_SYNC_ID);
1379                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, ORIGINAL_ID);
1380                DatabaseUtils.cursorLongToContentValuesIfPresent(cursor, cv,
1381                        ORIGINAL_INSTANCE_TIME);
1382                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, ORIGINAL_ALL_DAY);
1383                DatabaseUtils.cursorLongToContentValuesIfPresent(cursor, cv, LAST_DATE);
1384                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, HAS_ATTENDEE_DATA);
1385                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv,
1386                        GUESTS_CAN_INVITE_OTHERS);
1387                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, GUESTS_CAN_MODIFY);
1388                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, GUESTS_CAN_SEE_GUESTS);
1389                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CUSTOM_APP_PACKAGE);
1390                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CUSTOM_APP_URI);
1391                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, UID_2445);
1392                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, ORGANIZER);
1393                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, IS_ORGANIZER);
1394                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, _SYNC_ID);
1395                DatabaseUtils.cursorLongToContentValuesIfPresent(cursor, cv, DIRTY);
1396                DatabaseUtils.cursorLongToContentValuesIfPresent(cursor, cv, LAST_SYNCED);
1397                DatabaseUtils.cursorIntToContentValuesIfPresent(cursor, cv, DELETED);
1398                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, SYNC_DATA1);
1399                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, SYNC_DATA2);
1400                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, SYNC_DATA3);
1401                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, SYNC_DATA4);
1402                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, SYNC_DATA5);
1403                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, SYNC_DATA6);
1404                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, SYNC_DATA7);
1405                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, SYNC_DATA8);
1406                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, SYNC_DATA9);
1407                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, SYNC_DATA10);
1408                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC1);
1409                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC2);
1410                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC3);
1411                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC4);
1412                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC5);
1413                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC6);
1414                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC7);
1415                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC8);
1416                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC9);
1417                DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, cv, CAL_SYNC10);
1418
1419                Entity entity = new Entity(cv);
1420                Cursor subCursor;
1421                if (mResolver != null) {
1422                    subCursor = mResolver.query(Reminders.CONTENT_URI, REMINDERS_PROJECTION,
1423                            WHERE_EVENT_ID,
1424                            new String[] { Long.toString(eventId) }  /* selectionArgs */,
1425                            null /* sortOrder */);
1426                } else {
1427                    subCursor = mProvider.query(Reminders.CONTENT_URI, REMINDERS_PROJECTION,
1428                            WHERE_EVENT_ID,
1429                            new String[] { Long.toString(eventId) }  /* selectionArgs */,
1430                            null /* sortOrder */);
1431                }
1432                try {
1433                    while (subCursor.moveToNext()) {
1434                        ContentValues reminderValues = new ContentValues();
1435                        reminderValues.put(Reminders.MINUTES, subCursor.getInt(COLUMN_MINUTES));
1436                        reminderValues.put(Reminders.METHOD, subCursor.getInt(COLUMN_METHOD));
1437                        entity.addSubValue(Reminders.CONTENT_URI, reminderValues);
1438                    }
1439                } finally {
1440                    subCursor.close();
1441                }
1442
1443                if (mResolver != null) {
1444                    subCursor = mResolver.query(Attendees.CONTENT_URI, ATTENDEES_PROJECTION,
1445                            WHERE_EVENT_ID,
1446                            new String[] { Long.toString(eventId) } /* selectionArgs */,
1447                            null /* sortOrder */);
1448                } else {
1449                    subCursor = mProvider.query(Attendees.CONTENT_URI, ATTENDEES_PROJECTION,
1450                            WHERE_EVENT_ID,
1451                            new String[] { Long.toString(eventId) } /* selectionArgs */,
1452                            null /* sortOrder */);
1453                }
1454                try {
1455                    while (subCursor.moveToNext()) {
1456                        ContentValues attendeeValues = new ContentValues();
1457                        attendeeValues.put(Attendees.ATTENDEE_NAME,
1458                                subCursor.getString(COLUMN_ATTENDEE_NAME));
1459                        attendeeValues.put(Attendees.ATTENDEE_EMAIL,
1460                                subCursor.getString(COLUMN_ATTENDEE_EMAIL));
1461                        attendeeValues.put(Attendees.ATTENDEE_RELATIONSHIP,
1462                                subCursor.getInt(COLUMN_ATTENDEE_RELATIONSHIP));
1463                        attendeeValues.put(Attendees.ATTENDEE_TYPE,
1464                                subCursor.getInt(COLUMN_ATTENDEE_TYPE));
1465                        attendeeValues.put(Attendees.ATTENDEE_STATUS,
1466                                subCursor.getInt(COLUMN_ATTENDEE_STATUS));
1467                        attendeeValues.put(Attendees.ATTENDEE_IDENTITY,
1468                                subCursor.getString(COLUMN_ATTENDEE_IDENTITY));
1469                        attendeeValues.put(Attendees.ATTENDEE_ID_NAMESPACE,
1470                                subCursor.getString(COLUMN_ATTENDEE_ID_NAMESPACE));
1471                        entity.addSubValue(Attendees.CONTENT_URI, attendeeValues);
1472                    }
1473                } finally {
1474                    subCursor.close();
1475                }
1476
1477                if (mResolver != null) {
1478                    subCursor = mResolver.query(ExtendedProperties.CONTENT_URI, EXTENDED_PROJECTION,
1479                            WHERE_EVENT_ID,
1480                            new String[] { Long.toString(eventId) } /* selectionArgs */,
1481                            null /* sortOrder */);
1482                } else {
1483                    subCursor = mProvider.query(ExtendedProperties.CONTENT_URI, EXTENDED_PROJECTION,
1484                            WHERE_EVENT_ID,
1485                            new String[] { Long.toString(eventId) } /* selectionArgs */,
1486                            null /* sortOrder */);
1487                }
1488                try {
1489                    while (subCursor.moveToNext()) {
1490                        ContentValues extendedValues = new ContentValues();
1491                        extendedValues.put(ExtendedProperties._ID,
1492                                subCursor.getString(COLUMN_ID));
1493                        extendedValues.put(ExtendedProperties.NAME,
1494                                subCursor.getString(COLUMN_NAME));
1495                        extendedValues.put(ExtendedProperties.VALUE,
1496                                subCursor.getString(COLUMN_VALUE));
1497                        entity.addSubValue(ExtendedProperties.CONTENT_URI, extendedValues);
1498                    }
1499                } finally {
1500                    subCursor.close();
1501                }
1502
1503                cursor.moveToNext();
1504                return entity;
1505            }
1506        }
1507    }
1508
1509    /**
1510     * Constants and helpers for the Events table, which contains details for
1511     * individual events. <h3>Operations</h3> All operations can be done either
1512     * as an app or as a sync adapter. To perform an operation as a sync adapter
1513     * {@link #CALLER_IS_SYNCADAPTER} should be set to true and
1514     * {@link #ACCOUNT_NAME} and {@link #ACCOUNT_TYPE} must be set in the Uri
1515     * parameters. See
1516     * {@link Uri.Builder#appendQueryParameter(java.lang.String, java.lang.String)}
1517     * for details on adding parameters. Sync adapters have write access to more
1518     * columns but are restricted to a single account at a time.
1519     * <dl>
1520     * <dt><b>Insert</b></dt>
1521     * <dd>When inserting a new event the following fields must be included:
1522     * <ul>
1523     * <li>dtstart</li>
1524     * <li>dtend if the event is non-recurring</li>
1525     * <li>duration if the event is recurring</li>
1526     * <li>rrule or rdate if the event is recurring</li>
1527     * <li>eventTimezone</li>
1528     * <li>a calendar_id</li>
1529     * </ul>
1530     * There are also further requirements when inserting or updating an event.
1531     * See the section on Writing to Events.</dd>
1532     * <dt><b>Update</b></dt>
1533     * <dd>To perform an update of an Event the {@link Events#_ID} of the event
1534     * should be provided either as an appended id to the Uri (
1535     * {@link ContentUris#withAppendedId}) or as the first selection item--the
1536     * selection should start with "_id=?" and the first selectionArg should be
1537     * the _id of the event. Updates may also be done using a selection and no
1538     * id. Updating an event must respect the same rules as inserting and is
1539     * further restricted in the fields that can be written. See the section on
1540     * Writing to Events.</dd>
1541     * <dt><b>Delete</b></dt>
1542     * <dd>Events can be deleted either by the {@link Events#_ID} as an appended
1543     * id on the Uri or using any standard selection. If an appended id is used
1544     * a selection is not allowed. There are two versions of delete: as an app
1545     * and as a sync adapter. An app delete will set the deleted column on an
1546     * event and remove all instances of that event. A sync adapter delete will
1547     * remove the event from the database and all associated data.</dd>
1548     * <dt><b>Query</b></dt>
1549     * <dd>Querying the Events table will get you all information about a set of
1550     * events except their reminders, attendees, and extended properties. There
1551     * will be one row returned for each event that matches the query selection,
1552     * or at most a single row if the {@link Events#_ID} is appended to the Uri.
1553     * Recurring events will only return a single row regardless of the number
1554     * of times that event repeats.</dd>
1555     * </dl>
1556     * <h3>Writing to Events</h3> There are further restrictions on all Updates
1557     * and Inserts in the Events table:
1558     * <ul>
1559     * <li>If allDay is set to 1 eventTimezone must be {@link Time#TIMEZONE_UTC}
1560     * and the time must correspond to a midnight boundary.</li>
1561     * <li>Exceptions are not allowed to recur. If rrule or rdate is not empty,
1562     * original_id and original_sync_id must be empty.</li>
1563     * <li>In general a calendar_id should not be modified after insertion. This
1564     * is not explicitly forbidden but many sync adapters will not behave in an
1565     * expected way if the calendar_id is modified.</li>
1566     * </ul>
1567     * The following Events columns are writable by both an app and a sync
1568     * adapter.
1569     * <ul>
1570     * <li>{@link #CALENDAR_ID}</li>
1571     * <li>{@link #ORGANIZER}</li>
1572     * <li>{@link #TITLE}</li>
1573     * <li>{@link #EVENT_LOCATION}</li>
1574     * <li>{@link #DESCRIPTION}</li>
1575     * <li>{@link #EVENT_COLOR}</li>
1576     * <li>{@link #DTSTART}</li>
1577     * <li>{@link #DTEND}</li>
1578     * <li>{@link #EVENT_TIMEZONE}</li>
1579     * <li>{@link #EVENT_END_TIMEZONE}</li>
1580     * <li>{@link #DURATION}</li>
1581     * <li>{@link #ALL_DAY}</li>
1582     * <li>{@link #RRULE}</li>
1583     * <li>{@link #RDATE}</li>
1584     * <li>{@link #EXRULE}</li>
1585     * <li>{@link #EXDATE}</li>
1586     * <li>{@link #ORIGINAL_ID}</li>
1587     * <li>{@link #ORIGINAL_SYNC_ID}</li>
1588     * <li>{@link #ORIGINAL_INSTANCE_TIME}</li>
1589     * <li>{@link #ORIGINAL_ALL_DAY}</li>
1590     * <li>{@link #ACCESS_LEVEL}</li>
1591     * <li>{@link #AVAILABILITY}</li>
1592     * <li>{@link #GUESTS_CAN_MODIFY}</li>
1593     * <li>{@link #GUESTS_CAN_INVITE_OTHERS}</li>
1594     * <li>{@link #GUESTS_CAN_SEE_GUESTS}</li>
1595     * <li>{@link #CUSTOM_APP_PACKAGE}</li>
1596     * <li>{@link #CUSTOM_APP_URI}</li>
1597     * <li>{@link #UID_2445}</li>
1598     * </ul>
1599     * The following Events columns are writable only by a sync adapter
1600     * <ul>
1601     * <li>{@link #DIRTY}</li>
1602     * <li>{@link #_SYNC_ID}</li>
1603     * <li>{@link #SYNC_DATA1}</li>
1604     * <li>{@link #SYNC_DATA2}</li>
1605     * <li>{@link #SYNC_DATA3}</li>
1606     * <li>{@link #SYNC_DATA4}</li>
1607     * <li>{@link #SYNC_DATA5}</li>
1608     * <li>{@link #SYNC_DATA6}</li>
1609     * <li>{@link #SYNC_DATA7}</li>
1610     * <li>{@link #SYNC_DATA8}</li>
1611     * <li>{@link #SYNC_DATA9}</li>
1612     * <li>{@link #SYNC_DATA10}</li>
1613     * </ul>
1614     * The remaining columns are either updated by the provider only or are
1615     * views into other tables and cannot be changed through the Events table.
1616     */
1617    public static final class Events implements BaseColumns, SyncColumns, EventsColumns,
1618            CalendarColumns {
1619
1620        /**
1621         * The content:// style URL for interacting with events. Appending an
1622         * event id using {@link ContentUris#withAppendedId(Uri, long)} will
1623         * specify a single event.
1624         */
1625        @SuppressWarnings("hiding")
1626        public static final Uri CONTENT_URI =
1627                Uri.parse("content://" + AUTHORITY + "/events");
1628
1629        /**
1630         * The content:// style URI for recurring event exceptions.  Insertions require an
1631         * appended event ID.  Deletion of exceptions requires both the original event ID and
1632         * the exception event ID (see {@link Uri.Builder#appendPath}).
1633         */
1634        public static final Uri CONTENT_EXCEPTION_URI =
1635                Uri.parse("content://" + AUTHORITY + "/exception");
1636
1637        /**
1638         * This utility class cannot be instantiated
1639         */
1640        private Events() {}
1641
1642        /**
1643         * The default sort order for this table
1644         */
1645        private static final String DEFAULT_SORT_ORDER = "";
1646
1647        /**
1648         * These are columns that should only ever be updated by the provider,
1649         * either because they are views mapped to another table or because they
1650         * are used for provider only functionality. TODO move to provider
1651         *
1652         * @hide
1653         */
1654        public static String[] PROVIDER_WRITABLE_COLUMNS = new String[] {
1655                ACCOUNT_NAME,
1656                ACCOUNT_TYPE,
1657                CAL_SYNC1,
1658                CAL_SYNC2,
1659                CAL_SYNC3,
1660                CAL_SYNC4,
1661                CAL_SYNC5,
1662                CAL_SYNC6,
1663                CAL_SYNC7,
1664                CAL_SYNC8,
1665                CAL_SYNC9,
1666                CAL_SYNC10,
1667                ALLOWED_REMINDERS,
1668                ALLOWED_ATTENDEE_TYPES,
1669                ALLOWED_AVAILABILITY,
1670                CALENDAR_ACCESS_LEVEL,
1671                CALENDAR_COLOR,
1672                CALENDAR_TIME_ZONE,
1673                CAN_MODIFY_TIME_ZONE,
1674                CAN_ORGANIZER_RESPOND,
1675                CALENDAR_DISPLAY_NAME,
1676                CAN_PARTIALLY_UPDATE,
1677                SYNC_EVENTS,
1678                VISIBLE,
1679        };
1680
1681        /**
1682         * These fields are only writable by a sync adapter. To modify them the
1683         * caller must include CALLER_IS_SYNCADAPTER, _SYNC_ACCOUNT, and
1684         * _SYNC_ACCOUNT_TYPE in the query parameters. TODO move to provider.
1685         *
1686         * @hide
1687         */
1688        public static final String[] SYNC_WRITABLE_COLUMNS = new String[] {
1689            _SYNC_ID,
1690            DIRTY,
1691            SYNC_DATA1,
1692            SYNC_DATA2,
1693            SYNC_DATA3,
1694            SYNC_DATA4,
1695            SYNC_DATA5,
1696            SYNC_DATA6,
1697            SYNC_DATA7,
1698            SYNC_DATA8,
1699            SYNC_DATA9,
1700            SYNC_DATA10,
1701        };
1702    }
1703
1704    /**
1705     * Fields and helpers for interacting with Instances. An instance is a
1706     * single occurrence of an event including time zone specific start and end
1707     * days and minutes. The instances table is not writable and only provides a
1708     * way to query event occurrences.
1709     */
1710    public static final class Instances implements BaseColumns, EventsColumns, CalendarColumns {
1711
1712        private static final String WHERE_CALENDARS_SELECTED = Calendars.VISIBLE + "=?";
1713        private static final String[] WHERE_CALENDARS_ARGS = {
1714            "1"
1715        };
1716
1717        /**
1718         * This utility class cannot be instantiated
1719         */
1720        private Instances() {}
1721
1722        /**
1723         * Performs a query to return all visible instances in the given range.
1724         * This is a blocking function and should not be done on the UI thread.
1725         * This will cause an expansion of recurring events to fill this time
1726         * range if they are not already expanded and will slow down for larger
1727         * time ranges with many recurring events.
1728         *
1729         * @param cr The ContentResolver to use for the query
1730         * @param projection The columns to return
1731         * @param begin The start of the time range to query in UTC millis since
1732         *            epoch
1733         * @param end The end of the time range to query in UTC millis since
1734         *            epoch
1735         * @return A Cursor containing all instances in the given range
1736         */
1737        public static final Cursor query(ContentResolver cr, String[] projection,
1738                                         long begin, long end) {
1739            Uri.Builder builder = CONTENT_URI.buildUpon();
1740            ContentUris.appendId(builder, begin);
1741            ContentUris.appendId(builder, end);
1742            return cr.query(builder.build(), projection, WHERE_CALENDARS_SELECTED,
1743                    WHERE_CALENDARS_ARGS, DEFAULT_SORT_ORDER);
1744        }
1745
1746        /**
1747         * Performs a query to return all visible instances in the given range
1748         * that match the given query. This is a blocking function and should
1749         * not be done on the UI thread. This will cause an expansion of
1750         * recurring events to fill this time range if they are not already
1751         * expanded and will slow down for larger time ranges with many
1752         * recurring events.
1753         *
1754         * @param cr The ContentResolver to use for the query
1755         * @param projection The columns to return
1756         * @param begin The start of the time range to query in UTC millis since
1757         *            epoch
1758         * @param end The end of the time range to query in UTC millis since
1759         *            epoch
1760         * @param searchQuery A string of space separated search terms. Segments
1761         *            enclosed by double quotes will be treated as a single
1762         *            term.
1763         * @return A Cursor of instances matching the search terms in the given
1764         *         time range
1765         */
1766        public static final Cursor query(ContentResolver cr, String[] projection,
1767                                         long begin, long end, String searchQuery) {
1768            Uri.Builder builder = CONTENT_SEARCH_URI.buildUpon();
1769            ContentUris.appendId(builder, begin);
1770            ContentUris.appendId(builder, end);
1771            builder = builder.appendPath(searchQuery);
1772            return cr.query(builder.build(), projection, WHERE_CALENDARS_SELECTED,
1773                    WHERE_CALENDARS_ARGS, DEFAULT_SORT_ORDER);
1774        }
1775
1776        /**
1777         * The content:// style URL for querying an instance range. The begin
1778         * and end of the range to query should be added as path segments if
1779         * this is used directly.
1780         */
1781        @SuppressWarnings("hiding")
1782        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY +
1783                "/instances/when");
1784        /**
1785         * The content:// style URL for querying an instance range by Julian
1786         * Day. The start and end day should be added as path segments if this
1787         * is used directly.
1788         */
1789        public static final Uri CONTENT_BY_DAY_URI =
1790            Uri.parse("content://" + AUTHORITY + "/instances/whenbyday");
1791        /**
1792         * The content:// style URL for querying an instance range with a search
1793         * term. The begin, end, and search string should be appended as path
1794         * segments if this is used directly.
1795         */
1796        public static final Uri CONTENT_SEARCH_URI = Uri.parse("content://" + AUTHORITY +
1797                "/instances/search");
1798        /**
1799         * The content:// style URL for querying an instance range with a search
1800         * term. The start day, end day, and search string should be appended as
1801         * path segments if this is used directly.
1802         */
1803        public static final Uri CONTENT_SEARCH_BY_DAY_URI =
1804            Uri.parse("content://" + AUTHORITY + "/instances/searchbyday");
1805
1806        /**
1807         * The default sort order for this table.
1808         */
1809        private static final String DEFAULT_SORT_ORDER = "begin ASC";
1810
1811        /**
1812         * The beginning time of the instance, in UTC milliseconds. Column name.
1813         * <P>Type: INTEGER (long; millis since epoch)</P>
1814         */
1815        public static final String BEGIN = "begin";
1816
1817        /**
1818         * The ending time of the instance, in UTC milliseconds. Column name.
1819         * <P>Type: INTEGER (long; millis since epoch)</P>
1820         */
1821        public static final String END = "end";
1822
1823        /**
1824         * The _id of the event for this instance. Column name.
1825         * <P>Type: INTEGER (long, foreign key to the Events table)</P>
1826         */
1827        public static final String EVENT_ID = "event_id";
1828
1829        /**
1830         * The Julian start day of the instance, relative to the local time
1831         * zone. Column name.
1832         * <P>Type: INTEGER (int)</P>
1833         */
1834        public static final String START_DAY = "startDay";
1835
1836        /**
1837         * The Julian end day of the instance, relative to the local time
1838         * zone. Column name.
1839         * <P>Type: INTEGER (int)</P>
1840         */
1841        public static final String END_DAY = "endDay";
1842
1843        /**
1844         * The start minute of the instance measured from midnight in the
1845         * local time zone. Column name.
1846         * <P>Type: INTEGER (int)</P>
1847         */
1848        public static final String START_MINUTE = "startMinute";
1849
1850        /**
1851         * The end minute of the instance measured from midnight in the
1852         * local time zone. Column name.
1853         * <P>Type: INTEGER (int)</P>
1854         */
1855        public static final String END_MINUTE = "endMinute";
1856    }
1857
1858    protected interface CalendarCacheColumns {
1859        /**
1860         * The key for the setting. Keys are defined in {@link CalendarCache}.
1861         */
1862        public static final String KEY = "key";
1863
1864        /**
1865         * The value of the given setting.
1866         */
1867        public static final String VALUE = "value";
1868    }
1869
1870    /**
1871     * CalendarCache stores some settings for calendar including the current
1872     * time zone for the instances. These settings are stored using a key/value
1873     * scheme. A {@link #KEY} must be specified when updating these values.
1874     */
1875    public static final class CalendarCache implements CalendarCacheColumns {
1876        /**
1877         * The URI to use for retrieving the properties from the Calendar db.
1878         */
1879        public static final Uri URI =
1880                Uri.parse("content://" + AUTHORITY + "/properties");
1881
1882        /**
1883         * This utility class cannot be instantiated
1884         */
1885        private CalendarCache() {}
1886
1887        /**
1888         * They key for updating the use of auto/home time zones in Calendar.
1889         * Valid values are {@link #TIMEZONE_TYPE_AUTO} or
1890         * {@link #TIMEZONE_TYPE_HOME}.
1891         */
1892        public static final String KEY_TIMEZONE_TYPE = "timezoneType";
1893
1894        /**
1895         * The key for updating the time zone used by the provider when it
1896         * generates the instances table. This should only be written if the
1897         * type is set to {@link #TIMEZONE_TYPE_HOME}. A valid time zone id
1898         * should be written to this field.
1899         */
1900        public static final String KEY_TIMEZONE_INSTANCES = "timezoneInstances";
1901
1902        /**
1903         * The key for reading the last time zone set by the user. This should
1904         * only be read by apps and it will be automatically updated whenever
1905         * {@link #KEY_TIMEZONE_INSTANCES} is updated with
1906         * {@link #TIMEZONE_TYPE_HOME} set.
1907         */
1908        public static final String KEY_TIMEZONE_INSTANCES_PREVIOUS = "timezoneInstancesPrevious";
1909
1910        /**
1911         * The value to write to {@link #KEY_TIMEZONE_TYPE} if the provider
1912         * should stay in sync with the device's time zone.
1913         */
1914        public static final String TIMEZONE_TYPE_AUTO = "auto";
1915
1916        /**
1917         * The value to write to {@link #KEY_TIMEZONE_TYPE} if the provider
1918         * should use a fixed time zone set by the user.
1919         */
1920        public static final String TIMEZONE_TYPE_HOME = "home";
1921    }
1922
1923    /**
1924     * A few Calendar globals are needed in the CalendarProvider for expanding
1925     * the Instances table and these are all stored in the first (and only) row
1926     * of the CalendarMetaData table.
1927     *
1928     * @hide
1929     */
1930    protected interface CalendarMetaDataColumns {
1931        /**
1932         * The local timezone that was used for precomputing the fields
1933         * in the Instances table.
1934         */
1935        public static final String LOCAL_TIMEZONE = "localTimezone";
1936
1937        /**
1938         * The minimum time used in expanding the Instances table,
1939         * in UTC milliseconds.
1940         * <P>Type: INTEGER</P>
1941         */
1942        public static final String MIN_INSTANCE = "minInstance";
1943
1944        /**
1945         * The maximum time used in expanding the Instances table,
1946         * in UTC milliseconds.
1947         * <P>Type: INTEGER</P>
1948         */
1949        public static final String MAX_INSTANCE = "maxInstance";
1950
1951        /**
1952         * The minimum Julian day in the EventDays table.
1953         * <P>Type: INTEGER</P>
1954         */
1955        public static final String MIN_EVENTDAYS = "minEventDays";
1956
1957        /**
1958         * The maximum Julian day in the EventDays table.
1959         * <P>Type: INTEGER</P>
1960         */
1961        public static final String MAX_EVENTDAYS = "maxEventDays";
1962    }
1963
1964    /**
1965     * @hide
1966     */
1967    public static final class CalendarMetaData implements CalendarMetaDataColumns, BaseColumns {
1968
1969        /**
1970         * This utility class cannot be instantiated
1971         */
1972        private CalendarMetaData() {}
1973    }
1974
1975    protected interface EventDaysColumns {
1976        /**
1977         * The Julian starting day number. Column name.
1978         * <P>Type: INTEGER (int)</P>
1979         */
1980        public static final String STARTDAY = "startDay";
1981        /**
1982         * The Julian ending day number. Column name.
1983         * <P>Type: INTEGER (int)</P>
1984         */
1985        public static final String ENDDAY = "endDay";
1986
1987    }
1988
1989    /**
1990     * Fields and helpers for querying for a list of days that contain events.
1991     */
1992    public static final class EventDays implements EventDaysColumns {
1993        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
1994                + "/instances/groupbyday");
1995        private static final String SELECTION = "selected=1";
1996
1997        /**
1998         * This utility class cannot be instantiated
1999         */
2000        private EventDays() {}
2001
2002        /**
2003         * Retrieves the days with events for the Julian days starting at
2004         * "startDay" for "numDays". It returns a cursor containing startday and
2005         * endday representing the max range of days for all events beginning on
2006         * each startday.This is a blocking function and should not be done on
2007         * the UI thread.
2008         *
2009         * @param cr the ContentResolver
2010         * @param startDay the first Julian day in the range
2011         * @param numDays the number of days to load (must be at least 1)
2012         * @param projection the columns to return in the cursor
2013         * @return a database cursor containing a list of start and end days for
2014         *         events
2015         */
2016        public static final Cursor query(ContentResolver cr, int startDay, int numDays,
2017                String[] projection) {
2018            if (numDays < 1) {
2019                return null;
2020            }
2021            int endDay = startDay + numDays - 1;
2022            Uri.Builder builder = CONTENT_URI.buildUpon();
2023            ContentUris.appendId(builder, startDay);
2024            ContentUris.appendId(builder, endDay);
2025            return cr.query(builder.build(), projection, SELECTION,
2026                    null /* selection args */, STARTDAY);
2027        }
2028    }
2029
2030    protected interface RemindersColumns {
2031        /**
2032         * The event the reminder belongs to. Column name.
2033         * <P>Type: INTEGER (foreign key to the Events table)</P>
2034         */
2035        public static final String EVENT_ID = "event_id";
2036
2037        /**
2038         * The minutes prior to the event that the alarm should ring.  -1
2039         * specifies that we should use the default value for the system.
2040         * Column name.
2041         * <P>Type: INTEGER</P>
2042         */
2043        public static final String MINUTES = "minutes";
2044
2045        /**
2046         * Passing this as a minutes value will use the default reminder
2047         * minutes.
2048         */
2049        public static final int MINUTES_DEFAULT = -1;
2050
2051        /**
2052         * The alarm method, as set on the server. {@link #METHOD_DEFAULT},
2053         * {@link #METHOD_ALERT}, {@link #METHOD_EMAIL}, {@link #METHOD_SMS} and
2054         * {@link #METHOD_ALARM} are possible values; the device will only
2055         * process {@link #METHOD_DEFAULT} and {@link #METHOD_ALERT} reminders
2056         * (the other types are simply stored so we can send the same reminder
2057         * info back to the server when we make changes).
2058         */
2059        public static final String METHOD = "method";
2060
2061        public static final int METHOD_DEFAULT = 0;
2062        public static final int METHOD_ALERT = 1;
2063        public static final int METHOD_EMAIL = 2;
2064        public static final int METHOD_SMS = 3;
2065        public static final int METHOD_ALARM = 4;
2066    }
2067
2068    /**
2069     * Fields and helpers for accessing reminders for an event. Each row of this
2070     * table represents a single reminder for an event. Calling
2071     * {@link #query(ContentResolver, long, String[])} will return a list of reminders for
2072     * the event with the given eventId. Both apps and sync adapters may write
2073     * to this table. There are three writable fields and all of them must be
2074     * included when inserting a new reminder. They are:
2075     * <ul>
2076     * <li>{@link #EVENT_ID}</li>
2077     * <li>{@link #MINUTES}</li>
2078     * <li>{@link #METHOD}</li>
2079     * </ul>
2080     */
2081    public static final class Reminders implements BaseColumns, RemindersColumns, EventsColumns {
2082        private static final String REMINDERS_WHERE = CalendarContract.Reminders.EVENT_ID + "=?";
2083        @SuppressWarnings("hiding")
2084        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/reminders");
2085
2086        /**
2087         * This utility class cannot be instantiated
2088         */
2089        private Reminders() {}
2090
2091        /**
2092         * Queries all reminders associated with the given event. This is a
2093         * blocking call and should not be done on the UI thread.
2094         *
2095         * @param cr The content resolver to use for the query
2096         * @param eventId The id of the event to retrieve reminders for
2097         * @param projection the columns to return in the cursor
2098         * @return A Cursor containing all reminders for the event
2099         */
2100        public static final Cursor query(ContentResolver cr, long eventId, String[] projection) {
2101            String[] remArgs = {Long.toString(eventId)};
2102            return cr.query(CONTENT_URI, projection, REMINDERS_WHERE, remArgs /*selection args*/,
2103                    null /* sort order */);
2104        }
2105    }
2106
2107    protected interface CalendarAlertsColumns {
2108        /**
2109         * The event that the alert belongs to. Column name.
2110         * <P>Type: INTEGER (foreign key to the Events table)</P>
2111         */
2112        public static final String EVENT_ID = "event_id";
2113
2114        /**
2115         * The start time of the event, in UTC. Column name.
2116         * <P>Type: INTEGER (long; millis since epoch)</P>
2117         */
2118        public static final String BEGIN = "begin";
2119
2120        /**
2121         * The end time of the event, in UTC. Column name.
2122         * <P>Type: INTEGER (long; millis since epoch)</P>
2123         */
2124        public static final String END = "end";
2125
2126        /**
2127         * The alarm time of the event, in UTC. Column name.
2128         * <P>Type: INTEGER (long; millis since epoch)</P>
2129         */
2130        public static final String ALARM_TIME = "alarmTime";
2131
2132        /**
2133         * The creation time of this database entry, in UTC.
2134         * Useful for debugging missed reminders. Column name.
2135         * <P>Type: INTEGER (long; millis since epoch)</P>
2136         */
2137        public static final String CREATION_TIME = "creationTime";
2138
2139        /**
2140         * The time that the alarm broadcast was received by the Calendar app,
2141         * in UTC. Useful for debugging missed reminders. Column name.
2142         * <P>Type: INTEGER (long; millis since epoch)</P>
2143         */
2144        public static final String RECEIVED_TIME = "receivedTime";
2145
2146        /**
2147         * The time that the notification was created by the Calendar app,
2148         * in UTC. Useful for debugging missed reminders. Column name.
2149         * <P>Type: INTEGER (long; millis since epoch)</P>
2150         */
2151        public static final String NOTIFY_TIME = "notifyTime";
2152
2153        /**
2154         * The state of this alert. It starts out as {@link #STATE_SCHEDULED}, then
2155         * when the alarm goes off, it changes to {@link #STATE_FIRED}, and then when
2156         * the user dismisses the alarm it changes to {@link #STATE_DISMISSED}. Column
2157         * name.
2158         * <P>Type: INTEGER</P>
2159         */
2160        public static final String STATE = "state";
2161
2162        /**
2163         * An alert begins in this state when it is first created.
2164         */
2165        public static final int STATE_SCHEDULED = 0;
2166        /**
2167         * After a notification for an alert has been created it should be
2168         * updated to fired.
2169         */
2170        public static final int STATE_FIRED = 1;
2171        /**
2172         * Once the user has dismissed the notification the alert's state should
2173         * be set to dismissed so it is not fired again.
2174         */
2175        public static final int STATE_DISMISSED = 2;
2176
2177        /**
2178         * The number of minutes that this alarm precedes the start time. Column
2179         * name.
2180         * <P>Type: INTEGER</P>
2181         */
2182        public static final String MINUTES = "minutes";
2183
2184        /**
2185         * The default sort order for this alerts queries
2186         */
2187        public static final String DEFAULT_SORT_ORDER = "begin ASC,title ASC";
2188    }
2189
2190    /**
2191     * Fields and helpers for accessing calendar alerts information. These
2192     * fields are for tracking which alerts have been fired. Scheduled alarms
2193     * will generate an intent using {@link #ACTION_EVENT_REMINDER}. Apps that
2194     * receive this action may update the {@link #STATE} for the reminder when
2195     * they have finished handling it. Apps that have their notifications
2196     * disabled should not modify the table to ensure that they do not conflict
2197     * with another app that is generating a notification. In general, apps
2198     * should not need to write to this table directly except to update the
2199     * state of a reminder.
2200     */
2201    public static final class CalendarAlerts implements BaseColumns,
2202            CalendarAlertsColumns, EventsColumns, CalendarColumns {
2203
2204        /**
2205         * @hide
2206         */
2207        public static final String TABLE_NAME = "CalendarAlerts";
2208        /**
2209         * The Uri for querying calendar alert information
2210         */
2211        @SuppressWarnings("hiding")
2212        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY +
2213                "/calendar_alerts");
2214
2215        /**
2216         * This utility class cannot be instantiated
2217         */
2218        private CalendarAlerts() {}
2219
2220        private static final String WHERE_ALARM_EXISTS = EVENT_ID + "=?"
2221                + " AND " + BEGIN + "=?"
2222                + " AND " + ALARM_TIME + "=?";
2223
2224        private static final String WHERE_FINDNEXTALARMTIME = ALARM_TIME + ">=?";
2225        private static final String SORT_ORDER_ALARMTIME_ASC = ALARM_TIME + " ASC";
2226
2227        private static final String WHERE_RESCHEDULE_MISSED_ALARMS = STATE + "=" + STATE_SCHEDULED
2228                + " AND " + ALARM_TIME + "<?"
2229                + " AND " + ALARM_TIME + ">?"
2230                + " AND " + END + ">=?";
2231
2232        /**
2233         * This URI is for grouping the query results by event_id and begin
2234         * time.  This will return one result per instance of an event.  So
2235         * events with multiple alarms will appear just once, but multiple
2236         * instances of a repeating event will show up multiple times.
2237         */
2238        public static final Uri CONTENT_URI_BY_INSTANCE =
2239            Uri.parse("content://" + AUTHORITY + "/calendar_alerts/by_instance");
2240
2241        private static final boolean DEBUG = false;
2242
2243        /**
2244         * Helper for inserting an alarm time associated with an event TODO move
2245         * to Provider
2246         *
2247         * @hide
2248         */
2249        public static final Uri insert(ContentResolver cr, long eventId,
2250                long begin, long end, long alarmTime, int minutes) {
2251            ContentValues values = new ContentValues();
2252            values.put(CalendarAlerts.EVENT_ID, eventId);
2253            values.put(CalendarAlerts.BEGIN, begin);
2254            values.put(CalendarAlerts.END, end);
2255            values.put(CalendarAlerts.ALARM_TIME, alarmTime);
2256            long currentTime = System.currentTimeMillis();
2257            values.put(CalendarAlerts.CREATION_TIME, currentTime);
2258            values.put(CalendarAlerts.RECEIVED_TIME, 0);
2259            values.put(CalendarAlerts.NOTIFY_TIME, 0);
2260            values.put(CalendarAlerts.STATE, STATE_SCHEDULED);
2261            values.put(CalendarAlerts.MINUTES, minutes);
2262            return cr.insert(CONTENT_URI, values);
2263        }
2264
2265        /**
2266         * Finds the next alarm after (or equal to) the given time and returns
2267         * the time of that alarm or -1 if no such alarm exists. This is a
2268         * blocking call and should not be done on the UI thread. TODO move to
2269         * provider
2270         *
2271         * @param cr the ContentResolver
2272         * @param millis the time in UTC milliseconds
2273         * @return the next alarm time greater than or equal to "millis", or -1
2274         *         if no such alarm exists.
2275         * @hide
2276         */
2277        public static final long findNextAlarmTime(ContentResolver cr, long millis) {
2278            String selection = ALARM_TIME + ">=" + millis;
2279            // TODO: construct an explicit SQL query so that we can add
2280            // "LIMIT 1" to the end and get just one result.
2281            String[] projection = new String[] { ALARM_TIME };
2282            Cursor cursor = cr.query(CONTENT_URI, projection, WHERE_FINDNEXTALARMTIME,
2283                    (new String[] {
2284                        Long.toString(millis)
2285                    }), SORT_ORDER_ALARMTIME_ASC);
2286            long alarmTime = -1;
2287            try {
2288                if (cursor != null && cursor.moveToFirst()) {
2289                    alarmTime = cursor.getLong(0);
2290                }
2291            } finally {
2292                if (cursor != null) {
2293                    cursor.close();
2294                }
2295            }
2296            return alarmTime;
2297        }
2298
2299        /**
2300         * Searches the CalendarAlerts table for alarms that should have fired
2301         * but have not and then reschedules them. This method can be called at
2302         * boot time to restore alarms that may have been lost due to a phone
2303         * reboot. TODO move to provider
2304         *
2305         * @param cr the ContentResolver
2306         * @param context the Context
2307         * @param manager the AlarmManager
2308         * @hide
2309         */
2310        public static final void rescheduleMissedAlarms(ContentResolver cr,
2311                Context context, AlarmManager manager) {
2312            // Get all the alerts that have been scheduled but have not fired
2313            // and should have fired by now and are not too old.
2314            long now = System.currentTimeMillis();
2315            long ancient = now - DateUtils.DAY_IN_MILLIS;
2316            String[] projection = new String[] {
2317                    ALARM_TIME,
2318            };
2319
2320            // TODO: construct an explicit SQL query so that we can add
2321            // "GROUPBY" instead of doing a sort and de-dup
2322            Cursor cursor = cr.query(CalendarAlerts.CONTENT_URI, projection,
2323                    WHERE_RESCHEDULE_MISSED_ALARMS, (new String[] {
2324                            Long.toString(now), Long.toString(ancient), Long.toString(now)
2325                    }), SORT_ORDER_ALARMTIME_ASC);
2326            if (cursor == null) {
2327                return;
2328            }
2329
2330            if (DEBUG) {
2331                Log.d(TAG, "missed alarms found: " + cursor.getCount());
2332            }
2333
2334            try {
2335                long alarmTime = -1;
2336
2337                while (cursor.moveToNext()) {
2338                    long newAlarmTime = cursor.getLong(0);
2339                    if (alarmTime != newAlarmTime) {
2340                        if (DEBUG) {
2341                            Log.w(TAG, "rescheduling missed alarm. alarmTime: " + newAlarmTime);
2342                        }
2343                        scheduleAlarm(context, manager, newAlarmTime);
2344                        alarmTime = newAlarmTime;
2345                    }
2346                }
2347            } finally {
2348                cursor.close();
2349            }
2350        }
2351
2352        /**
2353         * Schedules an alarm intent with the system AlarmManager that will
2354         * notify listeners when a reminder should be fired. The provider will
2355         * keep scheduled reminders up to date but apps may use this to
2356         * implement snooze functionality without modifying the reminders table.
2357         * Scheduled alarms will generate an intent using
2358         * {@link #ACTION_EVENT_REMINDER}. TODO Move to provider
2359         *
2360         * @param context A context for referencing system resources
2361         * @param manager The AlarmManager to use or null
2362         * @param alarmTime The time to fire the intent in UTC millis since
2363         *            epoch
2364         * @hide
2365         */
2366        public static void scheduleAlarm(Context context, AlarmManager manager, long alarmTime) {
2367            if (DEBUG) {
2368                Time time = new Time();
2369                time.set(alarmTime);
2370                String schedTime = time.format(" %a, %b %d, %Y %I:%M%P");
2371                Log.d(TAG, "Schedule alarm at " + alarmTime + " " + schedTime);
2372            }
2373
2374            if (manager == null) {
2375                manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
2376            }
2377
2378            Intent intent = new Intent(ACTION_EVENT_REMINDER);
2379            intent.setData(ContentUris.withAppendedId(CalendarContract.CONTENT_URI, alarmTime));
2380            intent.putExtra(ALARM_TIME, alarmTime);
2381            PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
2382            manager.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
2383        }
2384
2385        /**
2386         * Searches for an entry in the CalendarAlerts table that matches the
2387         * given event id, begin time and alarm time. If one is found then this
2388         * alarm already exists and this method returns true. TODO Move to
2389         * provider
2390         *
2391         * @param cr the ContentResolver
2392         * @param eventId the event id to match
2393         * @param begin the start time of the event in UTC millis
2394         * @param alarmTime the alarm time of the event in UTC millis
2395         * @return true if there is already an alarm for the given event with
2396         *         the same start time and alarm time.
2397         * @hide
2398         */
2399        public static final boolean alarmExists(ContentResolver cr, long eventId,
2400                long begin, long alarmTime) {
2401            // TODO: construct an explicit SQL query so that we can add
2402            // "LIMIT 1" to the end and get just one result.
2403            String[] projection = new String[] { ALARM_TIME };
2404            Cursor cursor = cr.query(CONTENT_URI, projection, WHERE_ALARM_EXISTS,
2405                    (new String[] {
2406                            Long.toString(eventId), Long.toString(begin), Long.toString(alarmTime)
2407                    }), null);
2408            boolean found = false;
2409            try {
2410                if (cursor != null && cursor.getCount() > 0) {
2411                    found = true;
2412                }
2413            } finally {
2414                if (cursor != null) {
2415                    cursor.close();
2416                }
2417            }
2418            return found;
2419        }
2420    }
2421
2422    protected interface ColorsColumns extends SyncStateContract.Columns {
2423
2424        /**
2425         * The type of color, which describes how it should be used. Valid types
2426         * are {@link #TYPE_CALENDAR} and {@link #TYPE_EVENT}. Column name.
2427         * <P>
2428         * Type: INTEGER (NOT NULL)
2429         * </P>
2430         */
2431        public static final String COLOR_TYPE = "color_type";
2432
2433        /**
2434         * This indicateds a color that can be used for calendars.
2435         */
2436        public static final int TYPE_CALENDAR = 0;
2437        /**
2438         * This indicates a color that can be used for events.
2439         */
2440        public static final int TYPE_EVENT = 1;
2441
2442        /**
2443         * The key used to reference this color. This can be any non-empty
2444         * string, but must be unique for a given {@link #ACCOUNT_TYPE} and
2445         * {@link #ACCOUNT_NAME}. Column name.
2446         * <P>
2447         * Type: TEXT
2448         * </P>
2449         */
2450        public static final String COLOR_KEY = "color_index";
2451
2452        /**
2453         * The color as an 8-bit ARGB integer value. Colors should specify alpha
2454         * as fully opaque (eg 0xFF993322) as the alpha may be ignored or
2455         * modified for display. It is reccomended that colors be usable with
2456         * light (near white) text. Apps should not depend on that assumption,
2457         * however. Column name.
2458         * <P>
2459         * Type: INTEGER (NOT NULL)
2460         * </P>
2461         */
2462        public static final String COLOR = "color";
2463
2464    }
2465
2466    /**
2467     * Fields for accessing colors available for a given account. Colors are
2468     * referenced by {@link #COLOR_KEY} which must be unique for a given
2469     * account name/type. These values can only be updated by the sync
2470     * adapter. Only {@link #COLOR} may be updated after the initial insert. In
2471     * addition, a row can only be deleted once all references to that color
2472     * have been removed from the {@link Calendars} or {@link Events} tables.
2473     */
2474    public static final class Colors implements ColorsColumns {
2475        /**
2476         * @hide
2477         */
2478        public static final String TABLE_NAME = "Colors";
2479        /**
2480         * The Uri for querying color information
2481         */
2482        @SuppressWarnings("hiding")
2483        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/colors");
2484
2485        /**
2486         * This utility class cannot be instantiated
2487         */
2488        private Colors() {
2489        }
2490    }
2491
2492    protected interface ExtendedPropertiesColumns {
2493        /**
2494         * The event the extended property belongs to. Column name.
2495         * <P>Type: INTEGER (foreign key to the Events table)</P>
2496         */
2497        public static final String EVENT_ID = "event_id";
2498
2499        /**
2500         * The name of the extended property.  This is a uri of the form
2501         * {scheme}#{local-name} convention. Column name.
2502         * <P>Type: TEXT</P>
2503         */
2504        public static final String NAME = "name";
2505
2506        /**
2507         * The value of the extended property. Column name.
2508         * <P>Type: TEXT</P>
2509         */
2510        public static final String VALUE = "value";
2511    }
2512
2513    /**
2514     * Fields for accessing the Extended Properties. This is a generic set of
2515     * name/value pairs for use by sync adapters to add extra
2516     * information to events. There are three writable columns and all three
2517     * must be present when inserting a new value. They are:
2518     * <ul>
2519     * <li>{@link #EVENT_ID}</li>
2520     * <li>{@link #NAME}</li>
2521     * <li>{@link #VALUE}</li>
2522     * </ul>
2523     */
2524   public static final class ExtendedProperties implements BaseColumns,
2525            ExtendedPropertiesColumns, EventsColumns {
2526        public static final Uri CONTENT_URI =
2527                Uri.parse("content://" + AUTHORITY + "/extendedproperties");
2528
2529        /**
2530         * This utility class cannot be instantiated
2531         */
2532        private ExtendedProperties() {}
2533
2534        // TODO: fill out this class when we actually start utilizing extendedproperties
2535        // in the calendar application.
2536   }
2537
2538    /**
2539     * A table provided for sync adapters to use for storing private sync state data.
2540     *
2541     * @see SyncStateContract
2542     */
2543    public static final class SyncState implements SyncStateContract.Columns {
2544        /**
2545         * This utility class cannot be instantiated
2546         */
2547        private SyncState() {}
2548
2549        private static final String CONTENT_DIRECTORY =
2550                SyncStateContract.Constants.CONTENT_DIRECTORY;
2551
2552        /**
2553         * The content:// style URI for this table
2554         */
2555        public static final Uri CONTENT_URI =
2556                Uri.withAppendedPath(CalendarContract.CONTENT_URI, CONTENT_DIRECTORY);
2557    }
2558
2559    /**
2560     * Columns from the EventsRawTimes table
2561     *
2562     * @hide
2563     */
2564    protected interface EventsRawTimesColumns {
2565        /**
2566         * The corresponding event id. Column name.
2567         * <P>Type: INTEGER (long)</P>
2568         */
2569        public static final String EVENT_ID = "event_id";
2570
2571        /**
2572         * The RFC2445 compliant time the event starts. Column name.
2573         * <P>Type: TEXT</P>
2574         */
2575        public static final String DTSTART_2445 = "dtstart2445";
2576
2577        /**
2578         * The RFC2445 compliant time the event ends. Column name.
2579         * <P>Type: TEXT</P>
2580         */
2581        public static final String DTEND_2445 = "dtend2445";
2582
2583        /**
2584         * The RFC2445 compliant original instance time of the recurring event
2585         * for which this event is an exception. Column name.
2586         * <P>Type: TEXT</P>
2587         */
2588        public static final String ORIGINAL_INSTANCE_TIME_2445 = "originalInstanceTime2445";
2589
2590        /**
2591         * The RFC2445 compliant last date this event repeats on, or NULL if it
2592         * never ends. Column name.
2593         * <P>Type: TEXT</P>
2594         */
2595        public static final String LAST_DATE_2445 = "lastDate2445";
2596    }
2597
2598    /**
2599     * @hide
2600     */
2601    public static final class EventsRawTimes implements BaseColumns, EventsRawTimesColumns {
2602
2603        /**
2604         * This utility class cannot be instantiated
2605         */
2606        private EventsRawTimes() {}
2607    }
2608}
2609