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