2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.calendar;
19 import static android.provider.Calendar.EVENT_BEGIN_TIME;
20 import static android.provider.Calendar.EVENT_END_TIME;
21 import static android.provider.Calendar.AttendeesColumns.ATTENDEE_STATUS;
23 import android.app.Activity;
24 import android.content.ActivityNotFoundException;
25 import android.content.AsyncQueryHandler;
26 import android.content.ContentProviderOperation;
27 import android.content.ContentResolver;
28 import android.content.ContentUris;
29 import android.content.ContentValues;
30 import android.content.Context;
31 import android.content.Intent;
32 import android.content.OperationApplicationException;
33 import android.content.SharedPreferences;
34 import android.content.res.Resources;
35 import android.database.Cursor;
36 import android.graphics.PorterDuff;
37 import android.graphics.Rect;
38 import android.net.Uri;
39 import android.os.Bundle;
40 import android.os.RemoteException;
41 import android.pim.ContactsAsyncHelper;
42 import android.pim.EventRecurrence;
43 import android.preference.PreferenceManager;
44 import android.provider.Calendar;
45 import android.provider.ContactsContract;
46 import android.provider.Calendar.Attendees;
47 import android.provider.Calendar.Calendars;
48 import android.provider.Calendar.Events;
49 import android.provider.Calendar.Reminders;
50 import android.provider.Contacts.Presence;
51 import android.provider.ContactsContract.Contacts;
52 import android.provider.ContactsContract.Intents;
53 import android.provider.ContactsContract.RawContacts;
54 import android.provider.ContactsContract.CommonDataKinds.Email;
55 import android.text.format.DateFormat;
56 import android.text.format.DateUtils;
57 import android.text.format.Time;
58 import android.text.util.Linkify;
59 import android.text.util.Rfc822Token;
60 import android.util.Log;
61 import android.view.KeyEvent;
62 import android.view.LayoutInflater;
63 import android.view.Menu;
64 import android.view.MenuItem;
65 import android.view.MotionEvent;
66 import android.view.View;
67 import android.view.View.OnClickListener;
68 import android.view.View.OnTouchListener;
69 import android.widget.AdapterView;
70 import android.widget.ArrayAdapter;
71 import android.widget.ImageButton;
72 import android.widget.ImageView;
73 import android.widget.LinearLayout;
74 import android.widget.Spinner;
75 import android.widget.TextView;
76 import android.widget.Toast;
78 import java.util.ArrayList;
79 import java.util.Arrays;
80 import java.util.HashMap;
81 import java.util.TimeZone;
82 import java.util.regex.Pattern;
84 public class EventInfoActivity extends Activity implements View.OnClickListener,
85 AdapterView.OnItemSelectedListener {
86 private static final int MAX_REMINDERS = 5;
89 * These are the corresponding indices into the array of strings
90 * "R.array.change_response_labels" in the resource file.
92 static final int UPDATE_SINGLE = 0;
93 static final int UPDATE_ALL = 1;
95 private static final String[] EVENT_PROJECTION = new String[] {
96 Events._ID, // 0 do not remove; used in DeleteEventHelper
97 Events.TITLE, // 1 do not remove; used in DeleteEventHelper
98 Events.RRULE, // 2 do not remove; used in DeleteEventHelper
99 Events.ALL_DAY, // 3 do not remove; used in DeleteEventHelper
100 Events.CALENDAR_ID, // 4 do not remove; used in DeleteEventHelper
101 Events.DTSTART, // 5 do not remove; used in DeleteEventHelper
102 Events._SYNC_ID, // 6 do not remove; used in DeleteEventHelper
103 Events.EVENT_TIMEZONE, // 7 do not remove; used in DeleteEventHelper
104 Events.DESCRIPTION, // 8
105 Events.EVENT_LOCATION, // 9
106 Events.HAS_ALARM, // 10
107 Events.ACCESS_LEVEL, // 11
110 private static final int EVENT_INDEX_ID = 0;
111 private static final int EVENT_INDEX_TITLE = 1;
112 private static final int EVENT_INDEX_RRULE = 2;
113 private static final int EVENT_INDEX_ALL_DAY = 3;
114 private static final int EVENT_INDEX_CALENDAR_ID = 4;
115 private static final int EVENT_INDEX_SYNC_ID = 6;
116 private static final int EVENT_INDEX_EVENT_TIMEZONE = 7;
117 private static final int EVENT_INDEX_DESCRIPTION = 8;
118 private static final int EVENT_INDEX_EVENT_LOCATION = 9;
119 private static final int EVENT_INDEX_HAS_ALARM = 10;
120 private static final int EVENT_INDEX_ACCESS_LEVEL = 11;
121 private static final int EVENT_INDEX_COLOR = 12;
123 private static final String[] ATTENDEES_PROJECTION = new String[] {
125 Attendees.ATTENDEE_NAME, // 1
126 Attendees.ATTENDEE_EMAIL, // 2
127 Attendees.ATTENDEE_RELATIONSHIP, // 3
128 Attendees.ATTENDEE_STATUS, // 4
130 private static final int ATTENDEES_INDEX_ID = 0;
131 private static final int ATTENDEES_INDEX_NAME = 1;
132 private static final int ATTENDEES_INDEX_EMAIL = 2;
133 private static final int ATTENDEES_INDEX_RELATIONSHIP = 3;
134 private static final int ATTENDEES_INDEX_STATUS = 4;
136 private static final String ATTENDEES_WHERE = Attendees.EVENT_ID + "=%d";
138 private static final String ATTENDEES_SORT_ORDER = Attendees.ATTENDEE_NAME + " ASC, "
139 + Attendees.ATTENDEE_EMAIL + " ASC";
141 static final String[] CALENDARS_PROJECTION = new String[] {
143 Calendars.DISPLAY_NAME, // 1
144 Calendars.OWNER_ACCOUNT, // 2
146 static final int CALENDARS_INDEX_DISPLAY_NAME = 1;
147 static final int CALENDARS_INDEX_OWNER_ACCOUNT = 2;
149 static final String CALENDARS_WHERE = Calendars._ID + "=%d";
151 private static final String[] REMINDERS_PROJECTION = new String[] {
153 Reminders.MINUTES, // 1
155 private static final int REMINDERS_INDEX_MINUTES = 1;
156 private static final String REMINDERS_WHERE = Reminders.EVENT_ID + "=%d AND (" +
157 Reminders.METHOD + "=" + Reminders.METHOD_ALERT + " OR " + Reminders.METHOD + "=" +
158 Reminders.METHOD_DEFAULT + ")";
159 private static final String REMINDERS_SORT = Reminders.MINUTES;
161 private static final int MENU_GROUP_REMINDER = 1;
162 private static final int MENU_GROUP_EDIT = 2;
163 private static final int MENU_GROUP_DELETE = 3;
165 private static final int MENU_ADD_REMINDER = 1;
166 private static final int MENU_EDIT = 2;
167 private static final int MENU_DELETE = 3;
169 private static final int ATTENDEE_NO_RESPONSE = -1;
170 private static final int[] ATTENDEE_VALUES = {
171 ATTENDEE_NO_RESPONSE,
172 Attendees.ATTENDEE_STATUS_ACCEPTED,
173 Attendees.ATTENDEE_STATUS_TENTATIVE,
174 Attendees.ATTENDEE_STATUS_DECLINED,
177 private LinearLayout mRemindersContainer;
180 private long mEventId;
181 private Cursor mEventCursor;
182 private Cursor mAttendeesCursor;
183 private Cursor mCalendarsCursor;
185 private long mStartMillis;
186 private long mEndMillis;
187 private int mVisibility = Calendars.NO_ACCESS;
188 private int mRelationship = Attendees.RELATIONSHIP_ORGANIZER;
190 private ArrayList<Integer> mOriginalMinutes = new ArrayList<Integer>();
191 private ArrayList<LinearLayout> mReminderItems = new ArrayList<LinearLayout>(0);
192 private ArrayList<Integer> mReminderValues;
193 private ArrayList<String> mReminderLabels;
194 private int mDefaultReminderMinutes;
196 private DeleteEventHelper mDeleteEventHelper;
197 private EditResponseHelper mEditResponseHelper;
199 private int mResponseOffset;
200 private int mOriginalAttendeeResponse;
201 private int mAttendeeResponseFromIntent = ATTENDEE_NO_RESPONSE;
202 private boolean mIsRepeating;
204 private Pattern mWildcardPattern = Pattern.compile("^.*$");
205 private LayoutInflater mLayoutInflater;
207 // TODO This can be removed when the contacts content provider doesn't return duplicates
208 private int mUpdateCounts;
209 private static class ViewHolder {
214 private HashMap<String, ViewHolder> mViewHolders = new HashMap<String, ViewHolder>();
215 private PresenceQueryHandler mPresenceQueryHandler;
217 private static final Uri CONTACT_DATA_WITH_PRESENCE_URI =
218 Uri.withAppendedPath(ContactsContract.AUTHORITY_URI, "data_with_presence");
220 int PRESENCE_PROJECTION_CONTACT_ID_INDEX = 0;
221 int PRESENCE_PROJECTION_PRESENCE_INDEX = 1;
222 int PRESENCE_PROJECTION_EMAIL_INDEX = 2;
223 int PRESENCE_PROJECTION_PHOTO_ID_INDEX = 3;
225 private static final String[] PRESENCE_PROJECTION = new String[] {
226 RawContacts.CONTACT_ID, // 0
227 Presence.PRESENCE_STATUS, // 1
229 Contacts.PHOTO_ID, // 3
232 public static final boolean DEBUG = false;
234 public static final String TAG = "EventInfoActivity";
236 ArrayList<Attendee> mAcceptedAttendees = new ArrayList<Attendee>();
237 ArrayList<Attendee> mDeclinedAttendees = new ArrayList<Attendee>();
238 ArrayList<Attendee> mTentativeAttendees = new ArrayList<Attendee>();
239 private OnClickListener contactOnClickListener = new OnClickListener() {
240 private Rect getTargetRect(View anchor) {
241 final int[] location = new int[2];
242 anchor.getLocationOnScreen(location);
244 final Rect rect = new Rect();
245 rect.left = location[0];
246 rect.top = location[1];
247 rect.right = rect.left + anchor.getWidth();
248 rect.bottom = rect.top + anchor.getHeight();
252 public void onClick(View avatar) {
253 final Rect target = getTargetRect(avatar);
254 View attendeeItem = (View) avatar.getParent();
255 showContactInfo((Attendee) attendeeItem.getTag(), target);
259 private String mCalendarOwnerAccount = "";
261 // This is called when one of the "remove reminder" buttons is selected.
262 public void onClick(View v) {
263 LinearLayout reminderItem = (LinearLayout) v.getParent();
264 LinearLayout parent = (LinearLayout) reminderItem.getParent();
265 parent.removeView(reminderItem);
266 mReminderItems.remove(reminderItem);
267 updateRemindersVisibility();
270 public void onItemSelected(AdapterView parent, View v, int position, long id) {
271 // If they selected the "No response" option, then don't display the
272 // dialog asking which events to change.
273 if (id == 0 && mResponseOffset == 0) {
277 // If this is not a repeating event, then don't display the dialog
278 // asking which events to change.
283 // If the selection is the same as the original, then don't display the
284 // dialog asking which events to change.
285 int index = findResponseIndexFor(mOriginalAttendeeResponse);
286 if (position == index + mResponseOffset) {
290 // This is a repeating event. We need to ask the user if they mean to
291 // change just this one instance or all instances.
292 mEditResponseHelper.showDialog(mEditResponseHelper.getWhichEvents());
295 public void onNothingSelected(AdapterView parent) {
299 protected void onCreate(Bundle icicle) {
300 super.onCreate(icicle);
303 Intent intent = getIntent();
304 mUri = intent.getData();
305 ContentResolver cr = getContentResolver();
306 mStartMillis = intent.getLongExtra(EVENT_BEGIN_TIME, 0);
307 mEndMillis = intent.getLongExtra(EVENT_END_TIME, 0);
308 mAttendeeResponseFromIntent = intent.getIntExtra(ATTENDEE_STATUS, ATTENDEE_NO_RESPONSE);
309 mEventCursor = managedQuery(mUri, EVENT_PROJECTION, null, null);
310 if (initEventCursor()) {
311 // The cursor is empty. This can happen if the event was deleted.
316 setContentView(R.layout.event_info_activity);
319 Uri uri = Attendees.CONTENT_URI;
320 String where = String.format(ATTENDEES_WHERE, mEventId);
321 mAttendeesCursor = managedQuery(uri, ATTENDEES_PROJECTION, where, ATTENDEES_SORT_ORDER);
324 uri = Calendars.CONTENT_URI;
325 where = String.format(CALENDARS_WHERE, mEventCursor.getLong(EVENT_INDEX_CALENDAR_ID));
326 mCalendarsCursor = managedQuery(uri, CALENDARS_PROJECTION, where, null);
327 initCalendarsCursor();
329 // Initialize the reminder values array.
330 Resources r = getResources();
331 String[] strings = r.getStringArray(R.array.reminder_minutes_values);
332 int size = strings.length;
333 ArrayList<Integer> list = new ArrayList<Integer>(size);
334 for (int i = 0 ; i < size ; i++) {
335 list.add(Integer.parseInt(strings[i]));
337 mReminderValues = list;
338 String[] labels = r.getStringArray(R.array.reminder_minutes_labels);
339 mReminderLabels = new ArrayList<String>(Arrays.asList(labels));
341 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
342 String durationString =
343 prefs.getString(CalendarPreferenceActivity.KEY_DEFAULT_REMINDER, "0");
344 mDefaultReminderMinutes = Integer.parseInt(durationString);
346 mRemindersContainer = (LinearLayout) findViewById(R.id.reminder_items_container);
349 boolean hasAlarm = mEventCursor.getInt(EVENT_INDEX_HAS_ALARM) != 0;
351 uri = Reminders.CONTENT_URI;
352 where = String.format(REMINDERS_WHERE, mEventId);
353 Cursor reminderCursor = cr.query(uri, REMINDERS_PROJECTION, where, null,
356 // First pass: collect all the custom reminder minutes (e.g.,
357 // a reminder of 8 minutes) into a global list.
358 while (reminderCursor.moveToNext()) {
359 int minutes = reminderCursor.getInt(REMINDERS_INDEX_MINUTES);
360 EditEvent.addMinutesToList(this, mReminderValues, mReminderLabels, minutes);
363 // Second pass: create the reminder spinners
364 reminderCursor.moveToPosition(-1);
365 while (reminderCursor.moveToNext()) {
366 int minutes = reminderCursor.getInt(REMINDERS_INDEX_MINUTES);
367 mOriginalMinutes.add(minutes);
368 EditEvent.addReminder(this, this, mReminderItems, mReminderValues,
369 mReminderLabels, minutes);
372 reminderCursor.close();
377 updateRemindersVisibility();
379 // Setup the + Add Reminder Button
380 View.OnClickListener addReminderOnClickListener = new View.OnClickListener() {
381 public void onClick(View v) {
385 ImageButton reminderRemoveButton = (ImageButton) findViewById(R.id.reminder_add);
386 reminderRemoveButton.setOnClickListener(addReminderOnClickListener);
388 mDeleteEventHelper = new DeleteEventHelper(this, true /* exit when done */);
389 mEditResponseHelper = new EditResponseHelper(this);
391 mPresenceQueryHandler = new PresenceQueryHandler(this, cr);
392 mLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
396 protected void onResume() {
398 if (initEventCursor()) {
399 // The cursor is empty. This can happen if the event was deleted.
403 initAttendeesCursor();
404 initCalendarsCursor();
409 private void updateTitle() {
410 Resources res = getResources();
411 if (mVisibility >= Calendars.CONTRIBUTOR_ACCESS &&
412 mRelationship == Attendees.RELATIONSHIP_ATTENDEE) {
413 setTitle(res.getString(R.string.event_info_title_invite));
415 setTitle(res.getString(R.string.event_info_title));
420 * Initializes the event cursor, which is expected to point to the first
421 * (and only) result from a query.
422 * @return true if the cursor is empty.
424 private boolean initEventCursor() {
425 if ((mEventCursor == null) || (mEventCursor.getCount() == 0)) {
428 mEventCursor.moveToFirst();
429 mVisibility = mEventCursor.getInt(EVENT_INDEX_ACCESS_LEVEL);
430 mEventId = mEventCursor.getInt(EVENT_INDEX_ID);
431 String rRule = mEventCursor.getString(EVENT_INDEX_RRULE);
432 mIsRepeating = (rRule != null);
436 private static class Attendee {
440 Attendee(String name, String email) {
446 private void initAttendeesCursor() {
447 if (mAttendeesCursor != null) {
448 if (mAttendeesCursor.moveToFirst()) {
449 mAcceptedAttendees.clear();
450 mDeclinedAttendees.clear();
451 mTentativeAttendees.clear();
454 int status = mAttendeesCursor.getInt(ATTENDEES_INDEX_STATUS);
455 String name = mAttendeesCursor.getString(ATTENDEES_INDEX_NAME);
456 String email = mAttendeesCursor.getString(ATTENDEES_INDEX_EMAIL);
457 int relationship = mAttendeesCursor.getInt(ATTENDEES_INDEX_RELATIONSHIP);
458 if (mRelationship != relationship && mCalendarOwnerAccount.equals(email)) {
459 mRelationship = relationship;
463 case Attendees.ATTENDEE_STATUS_ACCEPTED:
464 mAcceptedAttendees.add(new Attendee(name, email));
466 case Attendees.ATTENDEE_STATUS_DECLINED:
467 mDeclinedAttendees.add(new Attendee(name, email));
470 mTentativeAttendees.add(new Attendee(name, email));
472 } while (mAttendeesCursor.moveToNext());
473 mAttendeesCursor.moveToFirst();
479 // TODO We shouldn't have to guess whether the current user is the organizer or not
480 if (mVisibility < Calendars.CONTRIBUTOR_ACCESS
481 && mRelationship == Attendees.RELATIONSHIP_ORGANIZER) {
482 mRelationship = Attendees.RELATIONSHIP_ATTENDEE;
486 private void initCalendarsCursor() {
487 if (mCalendarsCursor != null) {
488 mCalendarsCursor.moveToFirst();
493 public void onPause() {
495 if (!isFinishing()) {
498 ContentResolver cr = getContentResolver();
499 ArrayList<Integer> reminderMinutes = EditEvent.reminderItemsToMinutes(mReminderItems,
501 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(3);
502 boolean changed = EditEvent.saveReminders(ops, mEventId, reminderMinutes, mOriginalMinutes,
503 false /* no force save */);
505 cr.applyBatch(Calendars.CONTENT_URI.getAuthority(), ops);
506 } catch (RemoteException e) {
507 // TODO Auto-generated catch block
509 } catch (OperationApplicationException e) {
510 // TODO Auto-generated catch block
514 changed |= saveResponse(cr);
516 Toast.makeText(this, R.string.saving_event, Toast.LENGTH_SHORT).show();
521 public boolean onCreateOptionsMenu(Menu menu) {
523 item = menu.add(MENU_GROUP_REMINDER, MENU_ADD_REMINDER, 0,
524 R.string.add_new_reminder);
525 item.setIcon(R.drawable.ic_menu_reminder);
526 item.setAlphabeticShortcut('r');
528 item = menu.add(MENU_GROUP_EDIT, MENU_EDIT, 0, R.string.edit_event_label);
529 item.setIcon(android.R.drawable.ic_menu_edit);
530 item.setAlphabeticShortcut('e');
532 item = menu.add(MENU_GROUP_DELETE, MENU_DELETE, 0, R.string.delete_event_label);
533 item.setIcon(android.R.drawable.ic_menu_delete);
535 return super.onCreateOptionsMenu(menu);
539 public boolean onPrepareOptionsMenu(Menu menu) {
540 // Cannot add reminders to a shared calendar with only free/busy
542 boolean canAddReminders =
543 mVisibility >= Calendars.READ_ACCESS && mReminderItems.size() < MAX_REMINDERS;
544 menu.setGroupVisible(MENU_GROUP_REMINDER, canAddReminders);
545 menu.setGroupEnabled(MENU_GROUP_REMINDER, canAddReminders);
547 boolean canModifyCalendar = mVisibility >= Calendars.CONTRIBUTOR_ACCESS;
548 boolean canModifyEvent = canModifyCalendar
549 && mRelationship >= Attendees.RELATIONSHIP_ORGANIZER;
550 menu.setGroupVisible(MENU_GROUP_EDIT, canModifyEvent);
551 menu.setGroupEnabled(MENU_GROUP_EDIT, canModifyEvent);
552 menu.setGroupVisible(MENU_GROUP_DELETE, canModifyCalendar);
553 menu.setGroupEnabled(MENU_GROUP_DELETE, canModifyCalendar);
555 return super.onPrepareOptionsMenu(menu);
558 private void addReminder() {
559 // TODO: when adding a new reminder, make it different from the
560 // last one in the list (if any).
561 if (mDefaultReminderMinutes == 0) {
562 EditEvent.addReminder(this, this, mReminderItems,
563 mReminderValues, mReminderLabels, 10 /* minutes */);
565 EditEvent.addReminder(this, this, mReminderItems,
566 mReminderValues, mReminderLabels, mDefaultReminderMinutes);
568 updateRemindersVisibility();
572 public boolean onOptionsItemSelected(MenuItem item) {
573 super.onOptionsItemSelected(item);
574 switch (item.getItemId()) {
575 case MENU_ADD_REMINDER:
589 public boolean onKeyDown(int keyCode, KeyEvent event) {
590 if (keyCode == KeyEvent.KEYCODE_DEL) {
594 return super.onKeyDown(keyCode, event);
597 private void updateRemindersVisibility() {
598 if (mReminderItems.size() == 0) {
599 mRemindersContainer.setVisibility(View.GONE);
601 mRemindersContainer.setVisibility(View.VISIBLE);
606 * Saves the response to an invitation if the user changed the response.
607 * Returns true if the database was updated.
609 * @param cr the ContentResolver
610 * @return true if the database was changed
612 private boolean saveResponse(ContentResolver cr) {
613 if (mAttendeesCursor == null || mEventCursor == null) {
616 Spinner spinner = (Spinner) findViewById(R.id.response_value);
617 int position = spinner.getSelectedItemPosition() - mResponseOffset;
622 int status = ATTENDEE_VALUES[position];
624 // If the status has not changed, then don't update the database
625 if (status == mOriginalAttendeeResponse) {
629 // TODO find the right row first
630 long attendeeId = mAttendeesCursor.getInt(ATTENDEES_INDEX_ID);
632 // This is a non-repeating event
633 updateResponse(cr, mEventId, attendeeId, status);
637 // This is a repeating event
638 int whichEvents = mEditResponseHelper.getWhichEvents();
639 switch (whichEvents) {
643 createExceptionResponse(cr, mEventId, attendeeId, status);
646 updateResponse(cr, mEventId, attendeeId, status);
649 Log.e("Calendar", "Unexpected choice for updating invitation response");
655 private void updateResponse(ContentResolver cr, long eventId, long attendeeId, int status) {
656 // Update the "selfAttendeeStatus" field for the event
657 ContentValues values = new ContentValues();
659 // Will need to add email when MULTIPLE_ATTENDEES_PER_EVENT supported.
660 values.put(Attendees.ATTENDEE_STATUS, status);
661 values.put(Attendees.EVENT_ID, eventId);
663 Uri uri = ContentUris.withAppendedId(Attendees.CONTENT_URI, attendeeId);
664 cr.update(uri, values, null /* where */, null /* selection args */);
667 private void createExceptionResponse(ContentResolver cr, long eventId,
668 long attendeeId, int status) {
669 // Fetch information about the repeating event.
670 Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
671 Cursor cursor = cr.query(uri, EVENT_PROJECTION, null, null, null);
672 if (cursor == null) {
677 cursor.moveToFirst();
678 ContentValues values = new ContentValues();
680 String title = cursor.getString(EVENT_INDEX_TITLE);
681 String timezone = cursor.getString(EVENT_INDEX_EVENT_TIMEZONE);
682 int calendarId = cursor.getInt(EVENT_INDEX_CALENDAR_ID);
683 boolean allDay = cursor.getInt(EVENT_INDEX_ALL_DAY) != 0;
684 String syncId = cursor.getString(EVENT_INDEX_SYNC_ID);
686 values.put(Events.TITLE, title);
687 values.put(Events.EVENT_TIMEZONE, timezone);
688 values.put(Events.ALL_DAY, allDay ? 1 : 0);
689 values.put(Events.CALENDAR_ID, calendarId);
690 values.put(Events.DTSTART, mStartMillis);
691 values.put(Events.DTEND, mEndMillis);
692 values.put(Events.ORIGINAL_EVENT, syncId);
693 values.put(Events.ORIGINAL_INSTANCE_TIME, mStartMillis);
694 values.put(Events.ORIGINAL_ALL_DAY, allDay ? 1 : 0);
695 values.put(Events.STATUS, Events.STATUS_CONFIRMED);
696 values.put(Events.SELF_ATTENDEE_STATUS, status);
698 // Create a recurrence exception
699 cr.insert(Events.CONTENT_URI, values);
705 private int findResponseIndexFor(int response) {
706 int size = ATTENDEE_VALUES.length;
707 for (int index = 0; index < size; index++) {
708 if (ATTENDEE_VALUES[index] == response) {
715 private void doEdit() {
716 Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, mEventId);
717 Intent intent = new Intent(Intent.ACTION_EDIT, uri);
718 intent.putExtra(Calendar.EVENT_BEGIN_TIME, mStartMillis);
719 intent.putExtra(Calendar.EVENT_END_TIME, mEndMillis);
720 intent.setClass(EventInfoActivity.this, EditEvent.class);
721 startActivity(intent);
725 private void doDelete() {
726 mDeleteEventHelper.delete(mStartMillis, mEndMillis, mEventCursor, -1);
729 private void updateView() {
730 if (mEventCursor == null) {
733 Resources res = getResources();
735 String eventName = mEventCursor.getString(EVENT_INDEX_TITLE);
736 if (eventName == null || eventName.length() == 0) {
737 eventName = res.getString(R.string.no_title_label);
740 boolean allDay = mEventCursor.getInt(EVENT_INDEX_ALL_DAY) != 0;
741 String location = mEventCursor.getString(EVENT_INDEX_EVENT_LOCATION);
742 String description = mEventCursor.getString(EVENT_INDEX_DESCRIPTION);
743 String rRule = mEventCursor.getString(EVENT_INDEX_RRULE);
744 boolean hasAlarm = mEventCursor.getInt(EVENT_INDEX_HAS_ALARM) != 0;
745 String eventTimezone = mEventCursor.getString(EVENT_INDEX_EVENT_TIMEZONE);
746 mColor = mEventCursor.getInt(EVENT_INDEX_COLOR) & 0xbbffffff;
748 View calBackground = findViewById(R.id.cal_background);
749 calBackground.setBackgroundColor(mColor);
751 TextView title = (TextView) findViewById(R.id.title);
752 title.setTextColor(mColor);
754 View divider = findViewById(R.id.divider);
755 divider.getBackground().setColorFilter(mColor, PorterDuff.Mode.SRC_IN);
758 if (eventName != null) {
759 setTextCommon(R.id.title, eventName);
766 flags = DateUtils.FORMAT_UTC | DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_DATE;
768 flags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE;
769 if (DateFormat.is24HourFormat(this)) {
770 flags |= DateUtils.FORMAT_24HOUR;
773 when = DateUtils.formatDateRange(this, mStartMillis, mEndMillis, flags);
774 setTextCommon(R.id.when, when);
776 // Show the event timezone if it is different from the local timezone
777 Time time = new Time();
778 String localTimezone = time.timezone;
780 localTimezone = Time.TIMEZONE_UTC;
782 if (eventTimezone != null && !localTimezone.equals(eventTimezone) && !allDay) {
784 TimeZone tz = TimeZone.getTimeZone(localTimezone);
785 if (tz == null || tz.getID().equals("GMT")) {
786 displayName = localTimezone;
788 displayName = tz.getDisplayName();
791 setTextCommon(R.id.timezone, displayName);
793 setVisibilityCommon(R.id.timezone_container, View.GONE);
798 EventRecurrence eventRecurrence = new EventRecurrence();
799 eventRecurrence.parse(rRule);
800 Time date = new Time();
802 date.timezone = Time.TIMEZONE_UTC;
804 date.set(mStartMillis);
805 eventRecurrence.setStartDate(date);
806 String repeatString = eventRecurrence.getRepeatString();
807 setTextCommon(R.id.repeat, repeatString);
809 setVisibilityCommon(R.id.repeat_container, View.GONE);
813 if (location == null || location.length() == 0) {
814 setVisibilityCommon(R.id.where, View.GONE);
816 final TextView textView = (TextView) findViewById(R.id.where);
817 if (textView != null) {
818 textView.setAutoLinkMask(0);
819 textView.setText(location);
820 Linkify.addLinks(textView, mWildcardPattern, "geo:0,0?q=");
821 textView.setOnTouchListener(new OnTouchListener() {
822 public boolean onTouch(View v, MotionEvent event) {
824 return v.onTouchEvent(event);
825 } catch (ActivityNotFoundException e) {
835 if (description == null || description.length() == 0) {
836 setVisibilityCommon(R.id.description, View.GONE);
838 setTextCommon(R.id.description, description);
842 if (mCalendarsCursor != null) {
843 mCalendarsCursor.moveToFirst();
844 String calendarName = mCalendarsCursor.getString(CALENDARS_INDEX_DISPLAY_NAME);
845 setTextCommon(R.id.calendar, calendarName);
846 mCalendarOwnerAccount = mCalendarsCursor.getString(CALENDARS_INDEX_OWNER_ACCOUNT);
848 setVisibilityCommon(R.id.calendar_container, View.GONE);
852 private void updateAttendees() {
853 CharSequence[] entries;
854 entries = getResources().getTextArray(R.array.response_labels2);
855 LinearLayout attendeesLayout = (LinearLayout) findViewById(R.id.attendee_list);
856 attendeesLayout.removeAllViewsInLayout();
858 addAttendeesToLayout(mAcceptedAttendees, attendeesLayout, entries[0]);
859 addAttendeesToLayout(mDeclinedAttendees, attendeesLayout, entries[2]);
860 addAttendeesToLayout(mTentativeAttendees, attendeesLayout, entries[1]);
863 private void addAttendeesToLayout(ArrayList<Attendee> attendees, LinearLayout attendeeList,
864 CharSequence sectionTitle) {
865 if (attendees.size() == 0) {
869 ContentResolver cr = getContentResolver();
870 // Yes/No/Maybe Title
871 View titleView = mLayoutInflater.inflate(R.layout.contact_item, null);
872 titleView.findViewById(R.id.avatar).setVisibility(View.GONE);
873 View divider = titleView.findViewById(R.id.separator);
874 divider.getBackground().setColorFilter(mColor, PorterDuff.Mode.SRC_IN);
876 TextView title = (TextView) titleView.findViewById(R.id.name);
877 title.setText(getString(R.string.response_label, sectionTitle, attendees.size()));
878 title.setTextAppearance(this, R.style.TextAppearance_EventInfo_Label);
879 attendeeList.addView(titleView);
882 int numOfAttendees = attendees.size();
883 StringBuilder selection = new StringBuilder(Email.DATA + " IN (");
884 String[] selectionArgs = new String[numOfAttendees];
886 for (int i = 0; i < numOfAttendees; ++i) {
887 Attendee attendee = attendees.get(i);
888 selectionArgs[i] = attendee.mEmail;
890 View v = mLayoutInflater.inflate(R.layout.contact_item, null);
893 View separator = v.findViewById(R.id.separator);
894 separator.getBackground().setColorFilter(mColor, PorterDuff.Mode.SRC_IN);
897 TextView tv = (TextView) v.findViewById(R.id.name);
898 String name = attendee.mName;
899 if (name == null || name.length() == 0) {
900 name = attendee.mEmail;
904 ViewHolder vh = new ViewHolder();
905 vh.avatar = (ImageView) v.findViewById(R.id.avatar);
906 vh.avatar.setOnClickListener(contactOnClickListener);
907 vh.presence = (ImageView) v.findViewById(R.id.presence);
908 mViewHolders.put(attendee.mEmail, vh);
911 selection.append('?');
913 selection.append(", ?");
916 attendeeList.addView(v);
918 selection.append(')');
920 mPresenceQueryHandler.startQuery(mUpdateCounts, attendees, CONTACT_DATA_WITH_PRESENCE_URI,
921 PRESENCE_PROJECTION, selection.toString(), selectionArgs, null);
924 private class PresenceQueryHandler extends AsyncQueryHandler {
926 ContentResolver mContentResolver;
928 public PresenceQueryHandler(Context context, ContentResolver cr) {
930 mContentResolver = cr;
935 protected void onQueryComplete(int queryIndex, Object cookie, Cursor cursor) {
936 if (cursor == null) {
938 Log.e(TAG, "onQueryComplete: cursor == null");
943 cursor.moveToPosition(-1);
944 while (cursor.moveToNext()) {
945 String email = cursor.getString(PRESENCE_PROJECTION_EMAIL_INDEX);
946 int contactId = cursor.getInt(PRESENCE_PROJECTION_CONTACT_ID_INDEX);
947 ViewHolder vh = mViewHolders.get(email);
948 int photoId = cursor.getInt(PRESENCE_PROJECTION_PHOTO_ID_INDEX);
950 Log.e(TAG, "onQueryComplete Id: " + contactId + " PhotoId: " + photoId
951 + " Email: " + email);
956 ImageView presenceView = vh.presence;
957 if (presenceView != null) {
958 int status = cursor.getInt(PRESENCE_PROJECTION_PRESENCE_INDEX);
959 presenceView.setImageResource(Presence.getPresenceIconResourceId(status));
960 presenceView.setVisibility(View.VISIBLE);
963 if (photoId > 0 && vh.updateCounts < queryIndex) {
964 vh.updateCounts = queryIndex;
965 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
966 ContactsAsyncHelper.updateImageViewWithContactPhotoAsync(mContext, vh.avatar,
967 personUri, R.drawable.ic_contact_picture);
973 void updateResponse() {
974 if (mVisibility < Calendars.CONTRIBUTOR_ACCESS ||
975 mRelationship != Attendees.RELATIONSHIP_ATTENDEE) {
976 setVisibilityCommon(R.id.response_container, View.GONE);
980 setVisibilityCommon(R.id.response_container, View.VISIBLE);
982 Spinner spinner = (Spinner) findViewById(R.id.response_value);
984 mOriginalAttendeeResponse = ATTENDEE_NO_RESPONSE;
985 if (mAttendeesCursor != null) {
986 // TODO find the right row first
987 mOriginalAttendeeResponse = mAttendeesCursor.getInt(ATTENDEES_INDEX_STATUS);
991 /* If the user has previously responded to this event
992 * we should not allow them to select no response again.
993 * Switch the entries to a set of entries without the
994 * no response option.
996 if ((mOriginalAttendeeResponse != Attendees.ATTENDEE_STATUS_INVITED)
997 && (mOriginalAttendeeResponse != ATTENDEE_NO_RESPONSE)
998 && (mOriginalAttendeeResponse != Attendees.ATTENDEE_STATUS_NONE)) {
999 CharSequence[] entries;
1000 entries = getResources().getTextArray(R.array.response_labels2);
1001 mResponseOffset = -1;
1002 ArrayAdapter<CharSequence> adapter =
1003 new ArrayAdapter<CharSequence>(this,
1004 android.R.layout.simple_spinner_item, entries);
1005 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
1006 spinner.setAdapter(adapter);
1010 if (mAttendeeResponseFromIntent != ATTENDEE_NO_RESPONSE) {
1011 index = findResponseIndexFor(mAttendeeResponseFromIntent);
1013 index = findResponseIndexFor(mOriginalAttendeeResponse);
1015 spinner.setSelection(index + mResponseOffset);
1016 spinner.setOnItemSelectedListener(this);
1019 private void setTextCommon(int id, CharSequence text) {
1020 TextView textView = (TextView) findViewById(id);
1021 if (textView == null)
1023 textView.setText(text);
1026 private void setVisibilityCommon(int id, int visibility) {
1027 View v = findViewById(id);
1029 v.setVisibility(visibility);
1035 * Taken from com.google.android.gm.HtmlConversationActivity
1037 * Send the intent that shows the Contact info corresponding to the email address.
1039 public void showContactInfo(Attendee attendee, Rect rect) {
1040 Uri contactUri = Uri.fromParts("mailto", attendee.mEmail, null);
1042 Intent contactIntent = new Intent(Intents.SHOW_OR_CREATE_CONTACT);
1043 contactIntent.setData(contactUri);
1045 // Pass along full E-mail string for possible create dialog
1046 Rfc822Token sender = new Rfc822Token(attendee.mName, attendee.mEmail, null);
1047 contactIntent.putExtra(Intents.EXTRA_CREATE_DESCRIPTION,
1050 // Mark target position using on-screen coordinates
1051 contactIntent.putExtra(Intents.EXTRA_TARGET_RECT, rect);
1053 // Show the small version of fast track
1054 contactIntent.putExtra(Intents.EXTRA_MODE, Intents.MODE_SMALL);
1056 // Only provide personal name hint if we have one
1057 if (attendee.mName != null && attendee.mName.length() > 0) {
1058 contactIntent.putExtra(Intents.Insert.NAME, attendee.mName);
1061 startActivity(contactIntent);