EventInfoActivity.java revision a7c0722f4d5f23fc5450605ea4ff2f4a0abc6978
1/* 2 * Copyright (C) 2007 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 com.android.calendar; 18 19import static android.provider.Calendar.EVENT_BEGIN_TIME; 20import static android.provider.Calendar.EVENT_END_TIME; 21import static android.provider.Calendar.AttendeesColumns.ATTENDEE_STATUS; 22 23import android.app.Activity; 24import android.content.ActivityNotFoundException; 25import android.content.AsyncQueryHandler; 26import android.content.ContentProviderOperation; 27import android.content.ContentResolver; 28import android.content.ContentUris; 29import android.content.ContentValues; 30import android.content.Context; 31import android.content.Intent; 32import android.content.OperationApplicationException; 33import android.content.SharedPreferences; 34import android.content.res.Resources; 35import android.database.Cursor; 36import android.graphics.PorterDuff; 37import android.graphics.Rect; 38import android.net.Uri; 39import android.os.Bundle; 40import android.os.RemoteException; 41import android.pim.EventRecurrence; 42import android.preference.PreferenceManager; 43import android.provider.Calendar; 44import android.provider.ContactsContract; 45import android.provider.Calendar.Attendees; 46import android.provider.Calendar.Calendars; 47import android.provider.Calendar.Events; 48import android.provider.Calendar.Reminders; 49import android.provider.ContactsContract.CommonDataKinds; 50import android.provider.ContactsContract.Contacts; 51import android.provider.ContactsContract.Data; 52import android.provider.ContactsContract.Intents; 53import android.provider.ContactsContract.Presence; 54import android.provider.ContactsContract.QuickContact; 55import android.provider.ContactsContract.CommonDataKinds.Email; 56import android.text.TextUtils; 57import android.text.format.DateFormat; 58import android.text.format.DateUtils; 59import android.text.format.Time; 60import android.text.util.Linkify; 61import android.text.util.Rfc822Token; 62import android.util.Log; 63import android.view.KeyEvent; 64import android.view.LayoutInflater; 65import android.view.Menu; 66import android.view.MenuItem; 67import android.view.MotionEvent; 68import android.view.View; 69import android.view.View.OnTouchListener; 70import android.widget.AdapterView; 71import android.widget.ArrayAdapter; 72import android.widget.ImageButton; 73import android.widget.ImageView; 74import android.widget.LinearLayout; 75import android.widget.QuickContactBadge; 76import android.widget.Spinner; 77import android.widget.TextView; 78import android.widget.Toast; 79 80import java.util.ArrayList; 81import java.util.Arrays; 82import java.util.HashMap; 83import java.util.TimeZone; 84import java.util.regex.Pattern; 85 86public class EventInfoActivity extends Activity implements View.OnClickListener, 87 AdapterView.OnItemSelectedListener { 88 public static final boolean DEBUG = false; 89 90 public static final String TAG = "EventInfoActivity"; 91 92 private static final int MAX_REMINDERS = 5; 93 94 /** 95 * These are the corresponding indices into the array of strings 96 * "R.array.change_response_labels" in the resource file. 97 */ 98 static final int UPDATE_SINGLE = 0; 99 static final int UPDATE_ALL = 1; 100 101 private static final String[] EVENT_PROJECTION = new String[] { 102 Events._ID, // 0 do not remove; used in DeleteEventHelper 103 Events.TITLE, // 1 do not remove; used in DeleteEventHelper 104 Events.RRULE, // 2 do not remove; used in DeleteEventHelper 105 Events.ALL_DAY, // 3 do not remove; used in DeleteEventHelper 106 Events.CALENDAR_ID, // 4 do not remove; used in DeleteEventHelper 107 Events.DTSTART, // 5 do not remove; used in DeleteEventHelper 108 Events._SYNC_ID, // 6 do not remove; used in DeleteEventHelper 109 Events.EVENT_TIMEZONE, // 7 do not remove; used in DeleteEventHelper 110 Events.DESCRIPTION, // 8 111 Events.EVENT_LOCATION, // 9 112 Events.HAS_ALARM, // 10 113 Events.ACCESS_LEVEL, // 11 114 Events.COLOR, // 12 115 Events.HAS_ATTENDEE_DATA, // 13 116 Events.GUESTS_CAN_MODIFY, // 14 117 // TODO Events.GUESTS_CAN_INVITE_OTHERS has not been implemented in calendar provider 118 Events.GUESTS_CAN_INVITE_OTHERS, // 15 119 Events.ORGANIZER, // 16 120 }; 121 private static final int EVENT_INDEX_ID = 0; 122 private static final int EVENT_INDEX_TITLE = 1; 123 private static final int EVENT_INDEX_RRULE = 2; 124 private static final int EVENT_INDEX_ALL_DAY = 3; 125 private static final int EVENT_INDEX_CALENDAR_ID = 4; 126 private static final int EVENT_INDEX_SYNC_ID = 6; 127 private static final int EVENT_INDEX_EVENT_TIMEZONE = 7; 128 private static final int EVENT_INDEX_DESCRIPTION = 8; 129 private static final int EVENT_INDEX_EVENT_LOCATION = 9; 130 private static final int EVENT_INDEX_HAS_ALARM = 10; 131 private static final int EVENT_INDEX_ACCESS_LEVEL = 11; 132 private static final int EVENT_INDEX_COLOR = 12; 133 private static final int EVENT_INDEX_HAS_ATTENDEE_DATA = 13; 134 private static final int EVENT_INDEX_GUESTS_CAN_MODIFY = 14; 135 private static final int EVENT_INDEX_CAN_INVITE_OTHERS = 15; 136 private static final int EVENT_INDEX_ORGANIZER = 16; 137 138 private static final String[] ATTENDEES_PROJECTION = new String[] { 139 Attendees._ID, // 0 140 Attendees.ATTENDEE_NAME, // 1 141 Attendees.ATTENDEE_EMAIL, // 2 142 Attendees.ATTENDEE_RELATIONSHIP, // 3 143 Attendees.ATTENDEE_STATUS, // 4 144 }; 145 private static final int ATTENDEES_INDEX_ID = 0; 146 private static final int ATTENDEES_INDEX_NAME = 1; 147 private static final int ATTENDEES_INDEX_EMAIL = 2; 148 private static final int ATTENDEES_INDEX_RELATIONSHIP = 3; 149 private static final int ATTENDEES_INDEX_STATUS = 4; 150 151 private static final String ATTENDEES_WHERE = Attendees.EVENT_ID + "=%d"; 152 153 private static final String ATTENDEES_SORT_ORDER = Attendees.ATTENDEE_NAME + " ASC, " 154 + Attendees.ATTENDEE_EMAIL + " ASC"; 155 156 static final String[] CALENDARS_PROJECTION = new String[] { 157 Calendars._ID, // 0 158 Calendars.DISPLAY_NAME, // 1 159 Calendars.OWNER_ACCOUNT, // 2 160 }; 161 static final int CALENDARS_INDEX_DISPLAY_NAME = 1; 162 static final int CALENDARS_INDEX_OWNER_ACCOUNT = 2; 163 164 static final String CALENDARS_WHERE = Calendars._ID + "=%d"; 165 166 private static final String[] REMINDERS_PROJECTION = new String[] { 167 Reminders._ID, // 0 168 Reminders.MINUTES, // 1 169 }; 170 private static final int REMINDERS_INDEX_MINUTES = 1; 171 private static final String REMINDERS_WHERE = Reminders.EVENT_ID + "=%d AND (" + 172 Reminders.METHOD + "=" + Reminders.METHOD_ALERT + " OR " + Reminders.METHOD + "=" + 173 Reminders.METHOD_DEFAULT + ")"; 174 private static final String REMINDERS_SORT = Reminders.MINUTES; 175 176 private static final int MENU_GROUP_REMINDER = 1; 177 private static final int MENU_GROUP_EDIT = 2; 178 private static final int MENU_GROUP_DELETE = 3; 179 180 private static final int MENU_ADD_REMINDER = 1; 181 private static final int MENU_EDIT = 2; 182 private static final int MENU_DELETE = 3; 183 184 private static final int ATTENDEE_NO_RESPONSE = -1; 185 private static final int[] ATTENDEE_VALUES = { 186 ATTENDEE_NO_RESPONSE, 187 Attendees.ATTENDEE_STATUS_ACCEPTED, 188 Attendees.ATTENDEE_STATUS_TENTATIVE, 189 Attendees.ATTENDEE_STATUS_DECLINED, 190 }; 191 192 private LinearLayout mRemindersContainer; 193 private LinearLayout mOrganizerContainer; 194 private TextView mOrganizerView; 195 196 private Uri mUri; 197 private long mEventId; 198 private Cursor mEventCursor; 199 private Cursor mAttendeesCursor; 200 private Cursor mCalendarsCursor; 201 202 private long mStartMillis; 203 private long mEndMillis; 204 205 private boolean mHasAttendeeData; 206 private boolean mIsOrganizer; 207 private long mCalendarOwnerAttendeeId = -1; 208 private String mCalendarOwnerAccount; 209 private boolean mCanModifyCalendar; 210 private boolean mIsBusyFreeCalendar; 211 private boolean mCanModifyEvent; 212 private int mNumOfAttendees; 213 private String mOrganizer; 214 215 private ArrayList<Integer> mOriginalMinutes = new ArrayList<Integer>(); 216 private ArrayList<LinearLayout> mReminderItems = new ArrayList<LinearLayout>(0); 217 private ArrayList<Integer> mReminderValues; 218 private ArrayList<String> mReminderLabels; 219 private int mDefaultReminderMinutes; 220 221 private DeleteEventHelper mDeleteEventHelper; 222 private EditResponseHelper mEditResponseHelper; 223 224 private int mResponseOffset; 225 private int mOriginalAttendeeResponse; 226 private int mAttendeeResponseFromIntent = ATTENDEE_NO_RESPONSE; 227 private boolean mIsRepeating; 228 229 private Pattern mWildcardPattern = Pattern.compile("^.*$"); 230 private LayoutInflater mLayoutInflater; 231 private LinearLayout mReminderAdder; 232 233 // TODO This can be removed when the contacts content provider doesn't return duplicates 234 private int mUpdateCounts; 235 private static class ViewHolder { 236 QuickContactBadge badge; 237 ImageView presence; 238 int updateCounts; 239 } 240 private HashMap<String, ViewHolder> mViewHolders = new HashMap<String, ViewHolder>(); 241 private PresenceQueryHandler mPresenceQueryHandler; 242 243 private static final Uri CONTACT_DATA_WITH_PRESENCE_URI = Data.CONTENT_URI; 244 245 int PRESENCE_PROJECTION_CONTACT_ID_INDEX = 0; 246 int PRESENCE_PROJECTION_PRESENCE_INDEX = 1; 247 int PRESENCE_PROJECTION_EMAIL_INDEX = 2; 248 int PRESENCE_PROJECTION_PHOTO_ID_INDEX = 3; 249 250 private static final String[] PRESENCE_PROJECTION = new String[] { 251 Email.CONTACT_ID, // 0 252 Email.CONTACT_PRESENCE, // 1 253 Email.DATA, // 2 254 Email.PHOTO_ID, // 3 255 }; 256 257 ArrayList<Attendee> mAcceptedAttendees = new ArrayList<Attendee>(); 258 ArrayList<Attendee> mDeclinedAttendees = new ArrayList<Attendee>(); 259 ArrayList<Attendee> mTentativeAttendees = new ArrayList<Attendee>(); 260 private int mColor; 261 262 // This is called when one of the "remove reminder" buttons is selected. 263 public void onClick(View v) { 264 LinearLayout reminderItem = (LinearLayout) v.getParent(); 265 LinearLayout parent = (LinearLayout) reminderItem.getParent(); 266 parent.removeView(reminderItem); 267 mReminderItems.remove(reminderItem); 268 updateRemindersVisibility(); 269 } 270 271 public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { 272 // If they selected the "No response" option, then don't display the 273 // dialog asking which events to change. 274 if (id == 0 && mResponseOffset == 0) { 275 return; 276 } 277 278 // If this is not a repeating event, then don't display the dialog 279 // asking which events to change. 280 if (!mIsRepeating) { 281 return; 282 } 283 284 // If the selection is the same as the original, then don't display the 285 // dialog asking which events to change. 286 int index = findResponseIndexFor(mOriginalAttendeeResponse); 287 if (position == index + mResponseOffset) { 288 return; 289 } 290 291 // This is a repeating event. We need to ask the user if they mean to 292 // change just this one instance or all instances. 293 mEditResponseHelper.showDialog(mEditResponseHelper.getWhichEvents()); 294 } 295 296 public void onNothingSelected(AdapterView<?> parent) { 297 } 298 299 @Override 300 protected void onCreate(Bundle icicle) { 301 super.onCreate(icicle); 302 303 // Event cursor 304 Intent intent = getIntent(); 305 mUri = intent.getData(); 306 ContentResolver cr = getContentResolver(); 307 mStartMillis = intent.getLongExtra(EVENT_BEGIN_TIME, 0); 308 mEndMillis = intent.getLongExtra(EVENT_END_TIME, 0); 309 mAttendeeResponseFromIntent = intent.getIntExtra(ATTENDEE_STATUS, ATTENDEE_NO_RESPONSE); 310 mEventCursor = managedQuery(mUri, EVENT_PROJECTION, null, null, null); 311 if (initEventCursor()) { 312 // The cursor is empty. This can happen if the event was deleted. 313 finish(); 314 return; 315 } 316 317 setContentView(R.layout.event_info_activity); 318 mPresenceQueryHandler = new PresenceQueryHandler(this, cr); 319 mLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 320 mRemindersContainer = (LinearLayout) findViewById(R.id.reminders_container); 321 mOrganizerContainer = (LinearLayout) findViewById(R.id.organizer_container); 322 mOrganizerView = (TextView) findViewById(R.id.organizer); 323 324 // Calendars cursor 325 Uri uri = Calendars.CONTENT_URI; 326 String where = String.format(CALENDARS_WHERE, mEventCursor.getLong(EVENT_INDEX_CALENDAR_ID)); 327 mCalendarsCursor = managedQuery(uri, CALENDARS_PROJECTION, where, null, null); 328 mCalendarOwnerAccount = ""; 329 if (mCalendarsCursor != null) { 330 mCalendarsCursor.moveToFirst(); 331 mCalendarOwnerAccount = mCalendarsCursor.getString(CALENDARS_INDEX_OWNER_ACCOUNT); 332 } 333 String eventOrganizer = mEventCursor.getString(EVENT_INDEX_ORGANIZER); 334 mIsOrganizer = mCalendarOwnerAccount.equals(eventOrganizer); 335 mHasAttendeeData = mEventCursor.getInt(EVENT_INDEX_HAS_ATTENDEE_DATA) != 0; 336 337 updateView(); 338 339 // Attendees cursor 340 uri = Attendees.CONTENT_URI; 341 where = String.format(ATTENDEES_WHERE, mEventId); 342 mAttendeesCursor = managedQuery(uri, ATTENDEES_PROJECTION, where, null, 343 ATTENDEES_SORT_ORDER); 344 initAttendeesCursor(); 345 346 mOrganizer = eventOrganizer; 347 mCanModifyCalendar = 348 mEventCursor.getInt(EVENT_INDEX_ACCESS_LEVEL) >= Calendars.CONTRIBUTOR_ACCESS; 349 mIsBusyFreeCalendar = 350 mEventCursor.getInt(EVENT_INDEX_ACCESS_LEVEL) == Calendars.FREEBUSY_ACCESS; 351 352 mCanModifyEvent = mCanModifyCalendar 353 && (mIsOrganizer || (mEventCursor.getInt(EVENT_INDEX_GUESTS_CAN_MODIFY) != 0)); 354 355 // Initialize the reminder values array. 356 Resources r = getResources(); 357 String[] strings = r.getStringArray(R.array.reminder_minutes_values); 358 int size = strings.length; 359 ArrayList<Integer> list = new ArrayList<Integer>(size); 360 for (int i = 0 ; i < size ; i++) { 361 list.add(Integer.parseInt(strings[i])); 362 } 363 mReminderValues = list; 364 String[] labels = r.getStringArray(R.array.reminder_minutes_labels); 365 mReminderLabels = new ArrayList<String>(Arrays.asList(labels)); 366 367 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this); 368 String durationString = 369 prefs.getString(CalendarPreferenceActivity.KEY_DEFAULT_REMINDER, "0"); 370 mDefaultReminderMinutes = Integer.parseInt(durationString); 371 372 // Reminders cursor 373 boolean hasAlarm = mEventCursor.getInt(EVENT_INDEX_HAS_ALARM) != 0; 374 if (hasAlarm) { 375 uri = Reminders.CONTENT_URI; 376 where = String.format(REMINDERS_WHERE, mEventId); 377 Cursor reminderCursor = cr.query(uri, REMINDERS_PROJECTION, where, null, 378 REMINDERS_SORT); 379 try { 380 // First pass: collect all the custom reminder minutes (e.g., 381 // a reminder of 8 minutes) into a global list. 382 while (reminderCursor.moveToNext()) { 383 int minutes = reminderCursor.getInt(REMINDERS_INDEX_MINUTES); 384 EditEvent.addMinutesToList(this, mReminderValues, mReminderLabels, minutes); 385 } 386 387 // Second pass: create the reminder spinners 388 reminderCursor.moveToPosition(-1); 389 while (reminderCursor.moveToNext()) { 390 int minutes = reminderCursor.getInt(REMINDERS_INDEX_MINUTES); 391 mOriginalMinutes.add(minutes); 392 EditEvent.addReminder(this, this, mReminderItems, mReminderValues, 393 mReminderLabels, minutes); 394 } 395 } finally { 396 reminderCursor.close(); 397 } 398 } 399 400 // Setup the + Add Reminder Button 401 View.OnClickListener addReminderOnClickListener = new View.OnClickListener() { 402 public void onClick(View v) { 403 addReminder(); 404 } 405 }; 406 ImageButton reminderAddButton = (ImageButton) findViewById(R.id.reminder_add); 407 reminderAddButton.setOnClickListener(addReminderOnClickListener); 408 409 mReminderAdder = (LinearLayout) findViewById(R.id.reminder_adder); 410 updateRemindersVisibility(); 411 412 mDeleteEventHelper = new DeleteEventHelper(this, true /* exit when done */); 413 mEditResponseHelper = new EditResponseHelper(this); 414 } 415 416 @Override 417 protected void onResume() { 418 super.onResume(); 419 if (initEventCursor()) { 420 // The cursor is empty. This can happen if the event was deleted. 421 finish(); 422 return; 423 } 424 initCalendarsCursor(); 425 updateResponse(); 426 updateTitle(); 427 } 428 429 private void updateTitle() { 430 Resources res = getResources(); 431 if (mCanModifyCalendar && !mIsOrganizer) { 432 setTitle(res.getString(R.string.event_info_title_invite)); 433 } else { 434 setTitle(res.getString(R.string.event_info_title)); 435 } 436 } 437 438 /** 439 * Initializes the event cursor, which is expected to point to the first 440 * (and only) result from a query. 441 * @return true if the cursor is empty. 442 */ 443 private boolean initEventCursor() { 444 if ((mEventCursor == null) || (mEventCursor.getCount() == 0)) { 445 return true; 446 } 447 mEventCursor.moveToFirst(); 448 mEventId = mEventCursor.getInt(EVENT_INDEX_ID); 449 String rRule = mEventCursor.getString(EVENT_INDEX_RRULE); 450 mIsRepeating = (rRule != null); 451 return false; 452 } 453 454 private static class Attendee { 455 String mName; 456 String mEmail; 457 458 Attendee(String name, String email) { 459 mName = name; 460 mEmail = email; 461 } 462 } 463 464 private void initAttendeesCursor() { 465 mOriginalAttendeeResponse = ATTENDEE_NO_RESPONSE; 466 mCalendarOwnerAttendeeId = -1; 467 mNumOfAttendees = 0; 468 if (mAttendeesCursor != null) { 469 mNumOfAttendees = mAttendeesCursor.getCount(); 470 if (mAttendeesCursor.moveToFirst()) { 471 mAcceptedAttendees.clear(); 472 mDeclinedAttendees.clear(); 473 mTentativeAttendees.clear(); 474 475 do { 476 int status = mAttendeesCursor.getInt(ATTENDEES_INDEX_STATUS); 477 String name = mAttendeesCursor.getString(ATTENDEES_INDEX_NAME); 478 String email = mAttendeesCursor.getString(ATTENDEES_INDEX_EMAIL); 479 480 if (mAttendeesCursor.getInt(ATTENDEES_INDEX_RELATIONSHIP) == 481 Attendees.RELATIONSHIP_ORGANIZER) { 482 // Overwrites the one from Event table if available 483 if (name != null && name.length() > 0) { 484 mOrganizer = name; 485 } else if (email != null && email.length() > 0) { 486 mOrganizer = email; 487 } 488 } 489 490 if (mCalendarOwnerAttendeeId == -1 && mCalendarOwnerAccount.equals(email)) { 491 mCalendarOwnerAttendeeId = mAttendeesCursor.getInt(ATTENDEES_INDEX_ID); 492 mOriginalAttendeeResponse = mAttendeesCursor.getInt(ATTENDEES_INDEX_STATUS); 493 } else { 494 // Don't show your own status in the list because: 495 // 1) it doesn't make sense for event without other guests. 496 // 2) there's a spinner for that for events with guests. 497 switch(status) { 498 case Attendees.ATTENDEE_STATUS_ACCEPTED: 499 mAcceptedAttendees.add(new Attendee(name, email)); 500 break; 501 case Attendees.ATTENDEE_STATUS_DECLINED: 502 mDeclinedAttendees.add(new Attendee(name, email)); 503 break; 504 default: 505 mTentativeAttendees.add(new Attendee(name, email)); 506 } 507 } 508 } while (mAttendeesCursor.moveToNext()); 509 mAttendeesCursor.moveToFirst(); 510 511 updateAttendees(); 512 } 513 } 514 // only show the organizer if we're not the organizer and if 515 // we have attendee data (might have been removed by the server 516 // for events with a lot of attendees). 517 if (!mIsOrganizer && mHasAttendeeData) { 518 mOrganizerContainer.setVisibility(View.VISIBLE); 519 mOrganizerView.setText(mOrganizer); 520 } else { 521 mOrganizerContainer.setVisibility(View.GONE); 522 } 523 } 524 525 private void initCalendarsCursor() { 526 if (mCalendarsCursor != null) { 527 mCalendarsCursor.moveToFirst(); 528 } 529 } 530 531 @Override 532 public void onPause() { 533 super.onPause(); 534 if (!isFinishing()) { 535 return; 536 } 537 ContentResolver cr = getContentResolver(); 538 ArrayList<Integer> reminderMinutes = EditEvent.reminderItemsToMinutes(mReminderItems, 539 mReminderValues); 540 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(3); 541 boolean changed = EditEvent.saveReminders(ops, mEventId, reminderMinutes, mOriginalMinutes, 542 false /* no force save */); 543 try { 544 cr.applyBatch(Calendars.CONTENT_URI.getAuthority(), ops); 545 } catch (RemoteException e) { 546 Log.w(TAG, "Ignoring exception: ", e); 547 } catch (OperationApplicationException e) { 548 Log.w(TAG, "Ignoring exception: ", e); 549 } 550 551 changed |= saveResponse(cr); 552 if (changed) { 553 Toast.makeText(this, R.string.saving_event, Toast.LENGTH_SHORT).show(); 554 } 555 } 556 557 @Override 558 public boolean onCreateOptionsMenu(Menu menu) { 559 MenuItem item; 560 item = menu.add(MENU_GROUP_REMINDER, MENU_ADD_REMINDER, 0, 561 R.string.add_new_reminder); 562 item.setIcon(R.drawable.ic_menu_reminder); 563 item.setAlphabeticShortcut('r'); 564 565 item = menu.add(MENU_GROUP_EDIT, MENU_EDIT, 0, R.string.edit_event_label); 566 item.setIcon(android.R.drawable.ic_menu_edit); 567 item.setAlphabeticShortcut('e'); 568 569 item = menu.add(MENU_GROUP_DELETE, MENU_DELETE, 0, R.string.delete_event_label); 570 item.setIcon(android.R.drawable.ic_menu_delete); 571 572 return super.onCreateOptionsMenu(menu); 573 } 574 575 @Override 576 public boolean onPrepareOptionsMenu(Menu menu) { 577 boolean canAddReminders = canAddReminders(); 578 menu.setGroupVisible(MENU_GROUP_REMINDER, canAddReminders); 579 menu.setGroupEnabled(MENU_GROUP_REMINDER, canAddReminders); 580 581 menu.setGroupVisible(MENU_GROUP_EDIT, mCanModifyEvent); 582 menu.setGroupEnabled(MENU_GROUP_EDIT, mCanModifyEvent); 583 menu.setGroupVisible(MENU_GROUP_DELETE, mCanModifyCalendar); 584 menu.setGroupEnabled(MENU_GROUP_DELETE, mCanModifyCalendar); 585 586 return super.onPrepareOptionsMenu(menu); 587 } 588 589 private boolean canAddReminders() { 590 return !mIsBusyFreeCalendar && mReminderItems.size() < MAX_REMINDERS; 591 } 592 593 private void addReminder() { 594 // TODO: when adding a new reminder, make it different from the 595 // last one in the list (if any). 596 if (mDefaultReminderMinutes == 0) { 597 EditEvent.addReminder(this, this, mReminderItems, 598 mReminderValues, mReminderLabels, 10 /* minutes */); 599 } else { 600 EditEvent.addReminder(this, this, mReminderItems, 601 mReminderValues, mReminderLabels, mDefaultReminderMinutes); 602 } 603 updateRemindersVisibility(); 604 } 605 606 @Override 607 public boolean onOptionsItemSelected(MenuItem item) { 608 super.onOptionsItemSelected(item); 609 switch (item.getItemId()) { 610 case MENU_ADD_REMINDER: 611 addReminder(); 612 break; 613 case MENU_EDIT: 614 doEdit(); 615 break; 616 case MENU_DELETE: 617 doDelete(); 618 break; 619 } 620 return true; 621 } 622 623 @Override 624 public boolean onKeyDown(int keyCode, KeyEvent event) { 625 if (keyCode == KeyEvent.KEYCODE_DEL) { 626 doDelete(); 627 return true; 628 } 629 return super.onKeyDown(keyCode, event); 630 } 631 632 private void updateRemindersVisibility() { 633 if (mIsBusyFreeCalendar) { 634 mRemindersContainer.setVisibility(View.GONE); 635 } else { 636 mRemindersContainer.setVisibility(View.VISIBLE); 637 mReminderAdder.setVisibility(canAddReminders() ? View.VISIBLE : View.GONE); 638 } 639 } 640 641 /** 642 * Saves the response to an invitation if the user changed the response. 643 * Returns true if the database was updated. 644 * 645 * @param cr the ContentResolver 646 * @return true if the database was changed 647 */ 648 private boolean saveResponse(ContentResolver cr) { 649 if (mAttendeesCursor == null || mEventCursor == null) { 650 return false; 651 } 652 Spinner spinner = (Spinner) findViewById(R.id.response_value); 653 int position = spinner.getSelectedItemPosition() - mResponseOffset; 654 if (position <= 0) { 655 return false; 656 } 657 658 int status = ATTENDEE_VALUES[position]; 659 660 // If the status has not changed, then don't update the database 661 if (status == mOriginalAttendeeResponse) { 662 return false; 663 } 664 665 if (!mIsRepeating) { 666 // This is a non-repeating event 667 updateResponse(cr, mEventId, mCalendarOwnerAttendeeId, status); 668 return true; 669 } 670 671 // This is a repeating event 672 int whichEvents = mEditResponseHelper.getWhichEvents(); 673 switch (whichEvents) { 674 case -1: 675 return false; 676 case UPDATE_SINGLE: 677 createExceptionResponse(cr, mEventId, mCalendarOwnerAttendeeId, status); 678 return true; 679 case UPDATE_ALL: 680 updateResponse(cr, mEventId, mCalendarOwnerAttendeeId, status); 681 return true; 682 default: 683 Log.e(TAG, "Unexpected choice for updating invitation response"); 684 break; 685 } 686 return false; 687 } 688 689 private void updateResponse(ContentResolver cr, long eventId, long attendeeId, int status) { 690 // Update the attendee status in the attendees table. the provider 691 // takes care of updating the self attendance status. 692 ContentValues values = new ContentValues(); 693 694 if (!TextUtils.isEmpty(mCalendarOwnerAccount)) { 695 values.put(Attendees.ATTENDEE_EMAIL, mCalendarOwnerAccount); 696 } 697 values.put(Attendees.ATTENDEE_STATUS, status); 698 values.put(Attendees.EVENT_ID, eventId); 699 700 Uri uri = ContentUris.withAppendedId(Attendees.CONTENT_URI, attendeeId); 701 cr.update(uri, values, null /* where */, null /* selection args */); 702 } 703 704 private void createExceptionResponse(ContentResolver cr, long eventId, 705 long attendeeId, int status) { 706 // Fetch information about the repeating event. 707 Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId); 708 Cursor cursor = cr.query(uri, EVENT_PROJECTION, null, null, null); 709 if (cursor == null) { 710 return; 711 } 712 if(!cursor.moveToFirst()) { 713 cursor.close(); 714 return; 715 } 716 717 try { 718 ContentValues values = new ContentValues(); 719 720 String title = cursor.getString(EVENT_INDEX_TITLE); 721 String timezone = cursor.getString(EVENT_INDEX_EVENT_TIMEZONE); 722 int calendarId = cursor.getInt(EVENT_INDEX_CALENDAR_ID); 723 boolean allDay = cursor.getInt(EVENT_INDEX_ALL_DAY) != 0; 724 String syncId = cursor.getString(EVENT_INDEX_SYNC_ID); 725 726 values.put(Events.TITLE, title); 727 values.put(Events.EVENT_TIMEZONE, timezone); 728 values.put(Events.ALL_DAY, allDay ? 1 : 0); 729 values.put(Events.CALENDAR_ID, calendarId); 730 values.put(Events.DTSTART, mStartMillis); 731 values.put(Events.DTEND, mEndMillis); 732 values.put(Events.ORIGINAL_EVENT, syncId); 733 values.put(Events.ORIGINAL_INSTANCE_TIME, mStartMillis); 734 values.put(Events.ORIGINAL_ALL_DAY, allDay ? 1 : 0); 735 values.put(Events.STATUS, Events.STATUS_CONFIRMED); 736 values.put(Events.SELF_ATTENDEE_STATUS, status); 737 738 // Create a recurrence exception 739 cr.insert(Events.CONTENT_URI, values); 740 } finally { 741 cursor.close(); 742 } 743 } 744 745 private int findResponseIndexFor(int response) { 746 int size = ATTENDEE_VALUES.length; 747 for (int index = 0; index < size; index++) { 748 if (ATTENDEE_VALUES[index] == response) { 749 return index; 750 } 751 } 752 return 0; 753 } 754 755 private void doEdit() { 756 Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, mEventId); 757 Intent intent = new Intent(Intent.ACTION_EDIT, uri); 758 intent.putExtra(Calendar.EVENT_BEGIN_TIME, mStartMillis); 759 intent.putExtra(Calendar.EVENT_END_TIME, mEndMillis); 760 intent.setClass(EventInfoActivity.this, EditEvent.class); 761 startActivity(intent); 762 finish(); 763 } 764 765 private void doDelete() { 766 mDeleteEventHelper.delete(mStartMillis, mEndMillis, mEventCursor, -1); 767 } 768 769 private void updateView() { 770 if (mEventCursor == null) { 771 return; 772 } 773 774 String eventName = mEventCursor.getString(EVENT_INDEX_TITLE); 775 if (eventName == null || eventName.length() == 0) { 776 Resources res = getResources(); 777 eventName = res.getString(R.string.no_title_label); 778 } 779 780 boolean allDay = mEventCursor.getInt(EVENT_INDEX_ALL_DAY) != 0; 781 String location = mEventCursor.getString(EVENT_INDEX_EVENT_LOCATION); 782 String description = mEventCursor.getString(EVENT_INDEX_DESCRIPTION); 783 String rRule = mEventCursor.getString(EVENT_INDEX_RRULE); 784 boolean hasAlarm = mEventCursor.getInt(EVENT_INDEX_HAS_ALARM) != 0; 785 String eventTimezone = mEventCursor.getString(EVENT_INDEX_EVENT_TIMEZONE); 786 mColor = mEventCursor.getInt(EVENT_INDEX_COLOR) & 0xbbffffff; 787 788 View calBackground = findViewById(R.id.cal_background); 789 calBackground.setBackgroundColor(mColor); 790 791 TextView title = (TextView) findViewById(R.id.title); 792 title.setTextColor(mColor); 793 794 View divider = findViewById(R.id.divider); 795 divider.getBackground().setColorFilter(mColor, PorterDuff.Mode.SRC_IN); 796 797 // What 798 if (eventName != null) { 799 setTextCommon(R.id.title, eventName); 800 } 801 802 // When 803 String when; 804 int flags; 805 if (allDay) { 806 flags = DateUtils.FORMAT_UTC | DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_DATE; 807 } else { 808 flags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE; 809 if (DateFormat.is24HourFormat(this)) { 810 flags |= DateUtils.FORMAT_24HOUR; 811 } 812 } 813 when = DateUtils.formatDateRange(this, mStartMillis, mEndMillis, flags); 814 setTextCommon(R.id.when, when); 815 816 // Show the event timezone if it is different from the local timezone 817 Time time = new Time(); 818 String localTimezone = time.timezone; 819 if (allDay) { 820 localTimezone = Time.TIMEZONE_UTC; 821 } 822 if (eventTimezone != null && !localTimezone.equals(eventTimezone) && !allDay) { 823 String displayName; 824 TimeZone tz = TimeZone.getTimeZone(localTimezone); 825 if (tz == null || tz.getID().equals("GMT")) { 826 displayName = localTimezone; 827 } else { 828 displayName = tz.getDisplayName(); 829 } 830 831 setTextCommon(R.id.timezone, displayName); 832 } else { 833 setVisibilityCommon(R.id.timezone_container, View.GONE); 834 } 835 836 // Repeat 837 if (rRule != null) { 838 EventRecurrence eventRecurrence = new EventRecurrence(); 839 eventRecurrence.parse(rRule); 840 Time date = new Time(); 841 if (allDay) { 842 date.timezone = Time.TIMEZONE_UTC; 843 } 844 date.set(mStartMillis); 845 eventRecurrence.setStartDate(date); 846 String repeatString = EventRecurrenceFormatter.getRepeatString(getResources(), 847 eventRecurrence); 848 setTextCommon(R.id.repeat, repeatString); 849 } else { 850 setVisibilityCommon(R.id.repeat_container, View.GONE); 851 } 852 853 // Where 854 if (location == null || location.length() == 0) { 855 setVisibilityCommon(R.id.where, View.GONE); 856 } else { 857 final TextView textView = (TextView) findViewById(R.id.where); 858 if (textView != null) { 859 textView.setAutoLinkMask(0); 860 textView.setText(location); 861 Linkify.addLinks(textView, mWildcardPattern, "geo:0,0?q="); 862 textView.setOnTouchListener(new OnTouchListener() { 863 public boolean onTouch(View v, MotionEvent event) { 864 try { 865 return v.onTouchEvent(event); 866 } catch (ActivityNotFoundException e) { 867 // ignore 868 return true; 869 } 870 } 871 }); 872 } 873 } 874 875 // Description 876 if (description == null || description.length() == 0) { 877 setVisibilityCommon(R.id.description, View.GONE); 878 } else { 879 setTextCommon(R.id.description, description); 880 } 881 882 // Calendar 883 if (mCalendarsCursor != null) { 884 String calendarName = mCalendarsCursor.getString(CALENDARS_INDEX_DISPLAY_NAME); 885 setTextCommon(R.id.calendar, calendarName); 886 } else { 887 setVisibilityCommon(R.id.calendar_container, View.GONE); 888 } 889 } 890 891 private void updateAttendees() { 892 CharSequence[] entries; 893 entries = getResources().getTextArray(R.array.response_labels2); 894 LinearLayout attendeesLayout = (LinearLayout) findViewById(R.id.attendee_list); 895 attendeesLayout.removeAllViewsInLayout(); 896 ++mUpdateCounts; 897 addAttendeesToLayout(mAcceptedAttendees, attendeesLayout, entries[0]); 898 addAttendeesToLayout(mDeclinedAttendees, attendeesLayout, entries[2]); 899 addAttendeesToLayout(mTentativeAttendees, attendeesLayout, entries[1]); 900 } 901 902 private void addAttendeesToLayout(ArrayList<Attendee> attendees, LinearLayout attendeeList, 903 CharSequence sectionTitle) { 904 if (attendees.size() == 0) { 905 return; 906 } 907 908 ContentResolver cr = getContentResolver(); 909 // Yes/No/Maybe Title 910 View titleView = mLayoutInflater.inflate(R.layout.contact_item, null); 911 titleView.findViewById(R.id.badge).setVisibility(View.GONE); 912 View divider = titleView.findViewById(R.id.separator); 913 divider.getBackground().setColorFilter(mColor, PorterDuff.Mode.SRC_IN); 914 915 TextView title = (TextView) titleView.findViewById(R.id.name); 916 title.setText(getString(R.string.response_label, sectionTitle, attendees.size())); 917 title.setTextAppearance(this, R.style.TextAppearance_EventInfo_Label); 918 attendeeList.addView(titleView); 919 920 // Attendees 921 int numOfAttendees = attendees.size(); 922 StringBuilder selection = new StringBuilder(Email.DATA + " IN ("); 923 String[] selectionArgs = new String[numOfAttendees]; 924 925 for (int i = 0; i < numOfAttendees; ++i) { 926 Attendee attendee = attendees.get(i); 927 selectionArgs[i] = attendee.mEmail; 928 929 View v = mLayoutInflater.inflate(R.layout.contact_item, null); 930 v.setTag(attendee); 931 932 View separator = v.findViewById(R.id.separator); 933 separator.getBackground().setColorFilter(mColor, PorterDuff.Mode.SRC_IN); 934 935 // Text 936 TextView tv = (TextView) v.findViewById(R.id.name); 937 String name = attendee.mName; 938 if (name == null || name.length() == 0) { 939 name = attendee.mEmail; 940 } 941 tv.setText(name); 942 943 ViewHolder vh = new ViewHolder(); 944 vh.badge = (QuickContactBadge) v.findViewById(R.id.badge); 945 vh.badge.assignContactFromEmail(attendee.mEmail, true); 946 vh.presence = (ImageView) v.findViewById(R.id.presence); 947 mViewHolders.put(attendee.mEmail, vh); 948 949 if (i == 0) { 950 selection.append('?'); 951 } else { 952 selection.append(", ?"); 953 } 954 955 attendeeList.addView(v); 956 } 957 selection.append(')'); 958 959 mPresenceQueryHandler.startQuery(mUpdateCounts, attendees, CONTACT_DATA_WITH_PRESENCE_URI, 960 PRESENCE_PROJECTION, selection.toString(), selectionArgs, null); 961 } 962 963 private class PresenceQueryHandler extends AsyncQueryHandler { 964 Context mContext; 965 ContentResolver mContentResolver; 966 967 public PresenceQueryHandler(Context context, ContentResolver cr) { 968 super(cr); 969 mContentResolver = cr; 970 mContext = context; 971 } 972 973 @Override 974 protected void onQueryComplete(int queryIndex, Object cookie, Cursor cursor) { 975 if (cursor == null) { 976 if (DEBUG) { 977 Log.e(TAG, "onQueryComplete: cursor == null"); 978 } 979 return; 980 } 981 982 try { 983 cursor.moveToPosition(-1); 984 while (cursor.moveToNext()) { 985 String email = cursor.getString(PRESENCE_PROJECTION_EMAIL_INDEX); 986 int contactId = cursor.getInt(PRESENCE_PROJECTION_CONTACT_ID_INDEX); 987 ViewHolder vh = mViewHolders.get(email); 988 int photoId = cursor.getInt(PRESENCE_PROJECTION_PHOTO_ID_INDEX); 989 if (DEBUG) { 990 Log.e(TAG, "onQueryComplete Id: " + contactId + " PhotoId: " + photoId 991 + " Email: " + email); 992 } 993 if (vh == null) { 994 continue; 995 } 996 ImageView presenceView = vh.presence; 997 if (presenceView != null) { 998 int status = cursor.getInt(PRESENCE_PROJECTION_PRESENCE_INDEX); 999 presenceView.setImageResource(Presence.getPresenceIconResourceId(status)); 1000 presenceView.setVisibility(View.VISIBLE); 1001 } 1002 1003 if (photoId > 0 && vh.updateCounts < queryIndex) { 1004 vh.updateCounts = queryIndex; 1005 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); 1006 1007 // TODO, modify to batch queries together 1008 ContactsAsyncHelper.updateImageViewWithContactPhotoAsync(mContext, vh.badge, 1009 personUri, R.drawable.ic_contact_picture); 1010 } 1011 } 1012 } finally { 1013 cursor.close(); 1014 } 1015 } 1016 } 1017 1018 void updateResponse() { 1019 // we only let the user accept/reject/etc. a meeting if: 1020 // a) you can edit the event's containing calendar AND 1021 // b) you're not the organizer and only attendee 1022 // (if the attendee data has been hidden, the visible number of attendees 1023 // will be 1 -- the calendar owner's). 1024 // (there are more cases involved to be 100% accurate, such as 1025 // paying attention to whether or not an attendee status was 1026 // included in the feed, but we're currently omitting those corner cases 1027 // for simplicity). 1028 if (!mCanModifyCalendar || (mHasAttendeeData && mIsOrganizer && mNumOfAttendees <= 1)) { 1029 setVisibilityCommon(R.id.response_container, View.GONE); 1030 return; 1031 } 1032 1033 setVisibilityCommon(R.id.response_container, View.VISIBLE); 1034 1035 Spinner spinner = (Spinner) findViewById(R.id.response_value); 1036 1037 mResponseOffset = 0; 1038 1039 /* If the user has previously responded to this event 1040 * we should not allow them to select no response again. 1041 * Switch the entries to a set of entries without the 1042 * no response option. 1043 */ 1044 if ((mOriginalAttendeeResponse != Attendees.ATTENDEE_STATUS_INVITED) 1045 && (mOriginalAttendeeResponse != ATTENDEE_NO_RESPONSE) 1046 && (mOriginalAttendeeResponse != Attendees.ATTENDEE_STATUS_NONE)) { 1047 CharSequence[] entries; 1048 entries = getResources().getTextArray(R.array.response_labels2); 1049 mResponseOffset = -1; 1050 ArrayAdapter<CharSequence> adapter = 1051 new ArrayAdapter<CharSequence>(this, 1052 android.R.layout.simple_spinner_item, entries); 1053 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 1054 spinner.setAdapter(adapter); 1055 } 1056 1057 int index; 1058 if (mAttendeeResponseFromIntent != ATTENDEE_NO_RESPONSE) { 1059 index = findResponseIndexFor(mAttendeeResponseFromIntent); 1060 } else { 1061 index = findResponseIndexFor(mOriginalAttendeeResponse); 1062 } 1063 spinner.setSelection(index + mResponseOffset); 1064 spinner.setOnItemSelectedListener(this); 1065 } 1066 1067 private void setTextCommon(int id, CharSequence text) { 1068 TextView textView = (TextView) findViewById(id); 1069 if (textView == null) 1070 return; 1071 textView.setText(text); 1072 } 1073 1074 private void setVisibilityCommon(int id, int visibility) { 1075 View v = findViewById(id); 1076 if (v != null) { 1077 v.setVisibility(visibility); 1078 } 1079 return; 1080 } 1081 1082 /** 1083 * Taken from com.google.android.gm.HtmlConversationActivity 1084 * 1085 * Send the intent that shows the Contact info corresponding to the email address. 1086 */ 1087 public void showContactInfo(Attendee attendee, Rect rect) { 1088 // First perform lookup query to find existing contact 1089 final ContentResolver resolver = getContentResolver(); 1090 final String address = attendee.mEmail; 1091 final Uri dataUri = Uri.withAppendedPath(CommonDataKinds.Email.CONTENT_FILTER_URI, 1092 Uri.encode(address)); 1093 final Uri lookupUri = ContactsContract.Data.getContactLookupUri(resolver, dataUri); 1094 1095 if (lookupUri != null) { 1096 // Found matching contact, trigger QuickContact 1097 QuickContact.showQuickContact(this, rect, lookupUri, QuickContact.MODE_MEDIUM, null); 1098 } else { 1099 // No matching contact, ask user to create one 1100 final Uri mailUri = Uri.fromParts("mailto", address, null); 1101 final Intent intent = new Intent(Intents.SHOW_OR_CREATE_CONTACT, mailUri); 1102 1103 // Pass along full E-mail string for possible create dialog 1104 Rfc822Token sender = new Rfc822Token(attendee.mName, attendee.mEmail, null); 1105 intent.putExtra(Intents.EXTRA_CREATE_DESCRIPTION, sender.toString()); 1106 1107 // Only provide personal name hint if we have one 1108 final String senderPersonal = attendee.mName; 1109 if (!TextUtils.isEmpty(senderPersonal)) { 1110 intent.putExtra(Intents.Insert.NAME, senderPersonal); 1111 } 1112 1113 startActivity(intent); 1114 } 1115 } 1116} 1117