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 android.app.Activity;
20 import android.content.ContentResolver;
21 import android.content.ContentUris;
22 import android.content.ContentValues;
23 import android.content.Intent;
24 import android.content.SharedPreferences;
25 import android.content.res.Resources;
26 import android.database.Cursor;
27 import android.graphics.PorterDuff;
28 import android.net.Uri;
29 import android.os.Bundle;
30 import android.pim.EventRecurrence;
31 import android.preference.PreferenceManager;
32 import android.provider.Calendar;
33 import android.provider.Calendar.Attendees;
34 import android.provider.Calendar.Calendars;
35 import static android.provider.Calendar.EVENT_BEGIN_TIME;
36 import static android.provider.Calendar.EVENT_END_TIME;
37 import android.provider.Calendar.Events;
38 import android.provider.Calendar.Reminders;
39 import android.text.format.DateFormat;
40 import android.text.format.DateUtils;
41 import android.text.format.Time;
42 import android.view.KeyEvent;
43 import android.view.Menu;
44 import android.view.MenuItem;
45 import android.view.View;
46 import android.widget.ArrayAdapter;
47 import android.widget.ImageView;
48 import android.widget.LinearLayout;
49 import android.widget.Spinner;
50 import android.widget.TextView;
52 import java.util.ArrayList;
53 import java.util.Arrays;
55 public class EventInfoActivity extends Activity implements View.OnClickListener {
56 private static final int MAX_REMINDERS = 5;
58 private static final String[] EVENT_PROJECTION = new String[] {
59 Events._ID, // 0 do not remove; used in DeleteEventHelper
60 Events.TITLE, // 1 do not remove; used in DeleteEventHelper
61 Events.RRULE, // 2 do not remove; used in DeleteEventHelper
62 Events.ALL_DAY, // 3 do not remove; used in DeleteEventHelper
63 Events.CALENDAR_ID, // 4 do not remove; used in DeleteEventHelper
64 Events.DTSTART, // 5 do not remove; used in DeleteEventHelper
65 Events._SYNC_ID, // 6 do not remove; used in DeleteEventHelper
66 Events.EVENT_TIMEZONE, // 7 do not remove; used in DeleteEventHelper
67 Events.DESCRIPTION, // 8
68 Events.EVENT_LOCATION, // 9
69 Events.HAS_ALARM, // 10
70 Events.ACCESS_LEVEL, // 11
72 Events.SELF_ATTENDEE_STATUS, // 13
74 private static final int EVENT_INDEX_ID = 0;
75 private static final int EVENT_INDEX_TITLE = 1;
76 private static final int EVENT_INDEX_RRULE = 2;
77 private static final int EVENT_INDEX_ALL_DAY = 3;
78 private static final int EVENT_INDEX_CALENDAR_ID = 4;
79 private static final int EVENT_INDEX_EVENT_TIMEZONE = 7;
80 private static final int EVENT_INDEX_DESCRIPTION = 8;
81 private static final int EVENT_INDEX_EVENT_LOCATION = 9;
82 private static final int EVENT_INDEX_HAS_ALARM = 10;
83 private static final int EVENT_INDEX_ACCESS_LEVEL = 11;
84 private static final int EVENT_INDEX_COLOR = 12;
85 private static final int EVENT_INDEX_SELF_ATTENDEE_STATUS = 13;
87 private static final String[] ATTENDEES_PROJECTION = new String[] {
89 Attendees.ATTENDEE_RELATIONSHIP, // 1
90 Attendees.ATTENDEE_STATUS, // 2
92 private static final int ATTENDEES_INDEX_ID = 0;
93 private static final int ATTENDEES_INDEX_RELATIONSHIP = 1;
94 private static final int ATTENDEES_INDEX_STATUS = 2;
95 private static final String ATTENDEES_WHERE = Attendees.EVENT_ID + "=%d";
97 private static final String[] CALENDARS_PROJECTION = new String[] {
99 Calendars.DISPLAY_NAME, // 1
101 private static final int CALENDARS_INDEX_DISPLAY_NAME = 1;
102 private static final String CALENDARS_WHERE = Calendars._ID + "=%d";
104 private static final String[] REMINDERS_PROJECTION = new String[] {
106 Reminders.MINUTES, // 1
108 private static final int REMINDERS_INDEX_MINUTES = 1;
109 private static final String REMINDERS_WHERE = Reminders.EVENT_ID + "=%d AND (" +
110 Reminders.METHOD + "=" + Reminders.METHOD_ALERT + " OR " + Reminders.METHOD + "=" +
111 Reminders.METHOD_DEFAULT + ")";
113 private static final int MENU_GROUP_REMINDER = 1;
114 private static final int MENU_GROUP_EDIT = 2;
115 private static final int MENU_GROUP_DELETE = 3;
117 private static final int MENU_ADD_REMINDER = 1;
118 private static final int MENU_EDIT = 2;
119 private static final int MENU_DELETE = 3;
121 private static final int ATTENDEE_NO_RESPONSE = -1;
122 private static final int[] ATTENDEE_VALUES = {
123 ATTENDEE_NO_RESPONSE,
124 Attendees.ATTENDEE_STATUS_ACCEPTED,
125 Attendees.ATTENDEE_STATUS_TENTATIVE,
126 Attendees.ATTENDEE_STATUS_DECLINED,
129 private LinearLayout mRemindersContainer;
132 private long mEventId;
133 private Cursor mEventCursor;
134 private Cursor mAttendeesCursor;
135 private Cursor mCalendarsCursor;
137 private long mStartMillis;
138 private long mEndMillis;
139 private int mVisibility = Calendars.NO_ACCESS;
140 private int mRelationship = Attendees.RELATIONSHIP_ORGANIZER;
142 private ArrayList<Integer> mOriginalMinutes = new ArrayList<Integer>();
143 private ArrayList<LinearLayout> mReminderItems = new ArrayList<LinearLayout>(0);
144 private ArrayList<Integer> mReminderValues;
145 private ArrayList<String> mReminderLabels;
146 private int mDefaultReminderMinutes;
148 private DeleteEventHelper mDeleteEventHelper;
150 private int mResponseOffset;
151 private int mOriginalAttendeeResponse;
153 // This is called when one of the "remove reminder" buttons is selected.
154 public void onClick(View v) {
155 LinearLayout reminderItem = (LinearLayout) v.getParent();
156 LinearLayout parent = (LinearLayout) reminderItem.getParent();
157 parent.removeView(reminderItem);
158 mReminderItems.remove(reminderItem);
159 updateRemindersVisibility();
163 protected void onCreate(Bundle icicle) {
164 super.onCreate(icicle);
167 Intent intent = getIntent();
168 mUri = intent.getData();
169 ContentResolver cr = getContentResolver();
170 mStartMillis = intent.getLongExtra(EVENT_BEGIN_TIME, 0);
171 mEndMillis = intent.getLongExtra(EVENT_END_TIME, 0);
172 mEventCursor = managedQuery(mUri, EVENT_PROJECTION, null, null);
173 if (initEventCursor()) {
174 // The cursor is empty. This can happen if the event was deleted.
179 setContentView(R.layout.event_info_activity);
182 Uri uri = Attendees.CONTENT_URI;
183 String where = String.format(ATTENDEES_WHERE, mEventId);
184 mAttendeesCursor = managedQuery(uri, ATTENDEES_PROJECTION, where, null);
185 initAttendeesCursor();
188 uri = Calendars.CONTENT_URI;
189 where = String.format(CALENDARS_WHERE, mEventCursor.getLong(EVENT_INDEX_CALENDAR_ID));
190 mCalendarsCursor = managedQuery(uri, CALENDARS_PROJECTION, where, null);
191 initCalendarsCursor();
193 Resources res = getResources();
195 if (mVisibility >= Calendars.CONTRIBUTOR_ACCESS &&
196 mRelationship == Attendees.RELATIONSHIP_ATTENDEE) {
197 setTitle(res.getString(R.string.event_info_title_invite));
199 setTitle(res.getString(R.string.event_info_title));
202 // Initialize the reminder values array.
203 Resources r = getResources();
204 String[] strings = r.getStringArray(R.array.reminder_minutes_values);
205 int size = strings.length;
206 ArrayList<Integer> list = new ArrayList<Integer>(size);
207 for (int i = 0 ; i < size ; i++) {
208 list.add(Integer.parseInt(strings[i]));
210 mReminderValues = list;
211 String[] labels = r.getStringArray(R.array.reminder_minutes_labels);
212 mReminderLabels = new ArrayList<String>(Arrays.asList(labels));
214 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
215 String durationString =
216 prefs.getString(CalendarPreferenceActivity.KEY_DEFAULT_REMINDER, "0");
217 mDefaultReminderMinutes = Integer.parseInt(durationString);
219 mRemindersContainer = (LinearLayout) findViewById(R.id.reminders_container);
222 boolean hasAlarm = mEventCursor.getInt(EVENT_INDEX_HAS_ALARM) != 0;
224 uri = Reminders.CONTENT_URI;
225 where = String.format(REMINDERS_WHERE, mEventId);
226 Cursor reminderCursor = cr.query(uri, REMINDERS_PROJECTION, where, null, null);
228 // First pass: collect all the custom reminder minutes (e.g.,
229 // a reminder of 8 minutes) into a global list.
230 while (reminderCursor.moveToNext()) {
231 int minutes = reminderCursor.getInt(REMINDERS_INDEX_MINUTES);
232 EditEvent.addMinutesToList(this, mReminderValues, mReminderLabels, minutes);
235 // Second pass: create the reminder spinners
236 reminderCursor.moveToPosition(-1);
237 while (reminderCursor.moveToNext()) {
238 int minutes = reminderCursor.getInt(REMINDERS_INDEX_MINUTES);
239 mOriginalMinutes.add(minutes);
240 EditEvent.addReminder(this, this, mReminderItems, mReminderValues,
241 mReminderLabels, minutes);
244 reminderCursor.close();
249 updateRemindersVisibility();
251 mDeleteEventHelper = new DeleteEventHelper(this, true /* exit when done */);
255 protected void onResume() {
257 if (initEventCursor()) {
258 // The cursor is empty. This can happen if the event was deleted.
262 initAttendeesCursor();
263 initCalendarsCursor();
267 * Initializes the event cursor, which is expected to point to the first
268 * (and only) result from a query.
269 * @return true if the cursor is empty.
271 private boolean initEventCursor() {
272 if ((mEventCursor == null) || (mEventCursor.getCount() == 0)) {
275 mEventCursor.moveToFirst();
276 mVisibility = mEventCursor.getInt(EVENT_INDEX_ACCESS_LEVEL);
277 mEventId = mEventCursor.getInt(EVENT_INDEX_ID);
281 private void initAttendeesCursor() {
282 if (mAttendeesCursor != null) {
283 if (mAttendeesCursor.moveToFirst()) {
284 mRelationship = mAttendeesCursor.getInt(ATTENDEES_INDEX_RELATIONSHIP);
289 private void initCalendarsCursor() {
290 if (mCalendarsCursor != null) {
291 mCalendarsCursor.moveToFirst();
296 public void onPause() {
298 ContentResolver cr = getContentResolver();
299 ArrayList<Integer> reminderMinutes = EditEvent.reminderItemsToMinutes(mReminderItems,
301 EditEvent.saveReminders(cr, mEventId, reminderMinutes, mOriginalMinutes);
306 public boolean onCreateOptionsMenu(Menu menu) {
308 item = menu.add(MENU_GROUP_REMINDER, MENU_ADD_REMINDER, 0,
309 R.string.add_new_reminder);
310 item.setIcon(R.drawable.ic_menu_reminder);
311 item.setAlphabeticShortcut('r');
313 item = menu.add(MENU_GROUP_EDIT, MENU_EDIT, 0, R.string.edit_event_label);
314 item.setIcon(android.R.drawable.ic_menu_edit);
315 item.setAlphabeticShortcut('e');
317 item = menu.add(MENU_GROUP_DELETE, MENU_DELETE, 0, R.string.delete_event_label);
318 item.setIcon(android.R.drawable.ic_menu_delete);
320 return super.onCreateOptionsMenu(menu);
324 public boolean onPrepareOptionsMenu(Menu menu) {
325 // Cannot add reminders to a shared calendar with only free/busy
327 if (mVisibility >= Calendars.READ_ACCESS && mReminderItems.size() < MAX_REMINDERS) {
328 menu.setGroupVisible(MENU_GROUP_REMINDER, true);
329 menu.setGroupEnabled(MENU_GROUP_REMINDER, true);
331 menu.setGroupVisible(MENU_GROUP_REMINDER, false);
332 menu.setGroupEnabled(MENU_GROUP_REMINDER, false);
335 if (mVisibility >= Calendars.CONTRIBUTOR_ACCESS &&
336 mRelationship >= Attendees.RELATIONSHIP_ORGANIZER) {
337 menu.setGroupVisible(MENU_GROUP_EDIT, true);
338 menu.setGroupEnabled(MENU_GROUP_EDIT, true);
339 menu.setGroupVisible(MENU_GROUP_DELETE, true);
340 menu.setGroupEnabled(MENU_GROUP_DELETE, true);
342 menu.setGroupVisible(MENU_GROUP_EDIT, false);
343 menu.setGroupEnabled(MENU_GROUP_EDIT, false);
344 menu.setGroupVisible(MENU_GROUP_DELETE, false);
345 menu.setGroupEnabled(MENU_GROUP_DELETE, false);
348 return super.onPrepareOptionsMenu(menu);
352 public boolean onOptionsItemSelected(MenuItem item) {
353 super.onOptionsItemSelected(item);
354 switch (item.getItemId()) {
355 case MENU_ADD_REMINDER:
356 // TODO: when adding a new reminder, make it different from the
357 // last one in the list (if any).
358 if (mDefaultReminderMinutes == 0) {
359 EditEvent.addReminder(this, this, mReminderItems,
360 mReminderValues, mReminderLabels, 10 /* minutes */);
362 EditEvent.addReminder(this, this, mReminderItems,
363 mReminderValues, mReminderLabels, mDefaultReminderMinutes);
365 updateRemindersVisibility();
378 public boolean onKeyDown(int keyCode, KeyEvent event) {
379 if (keyCode == KeyEvent.KEYCODE_DEL) {
383 return super.onKeyDown(keyCode, event);
386 private void updateRemindersVisibility() {
387 if (mReminderItems.size() == 0) {
388 mRemindersContainer.setVisibility(View.GONE);
390 mRemindersContainer.setVisibility(View.VISIBLE);
394 private void saveResponse(ContentResolver cr) {
395 if (mAttendeesCursor == null) {
398 Spinner spinner = (Spinner) findViewById(R.id.response_value);
399 int position = spinner.getSelectedItemPosition() - mResponseOffset;
404 int status = ATTENDEE_VALUES[position];
406 // If the status has not changed, then don't update the database
407 if (status == mOriginalAttendeeResponse) {
411 // Update the "selfAttendeeStatus" field for the event
412 ContentValues values = new ContentValues();
414 // Will need to add email when MULTIPLE_ATTENDEES_PER_EVENT supported.
415 values.put(Attendees.ATTENDEE_STATUS, status);
416 values.put(Attendees.EVENT_ID, mEventId);
418 int attendeeId = mAttendeesCursor.getInt(ATTENDEES_INDEX_ID);
420 Uri uri = ContentUris.withAppendedId(Attendees.CONTENT_URI, attendeeId);
421 cr.update(uri, values, null /* where */, null /* selection args */);
424 private int findResponseIndexFor(int response) {
425 int size = ATTENDEE_VALUES.length;
426 for (int index = 0; index < size; index++) {
427 if (ATTENDEE_VALUES[index] == response) {
434 private void doEdit() {
435 Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, mEventId);
436 Intent intent = new Intent(Intent.ACTION_EDIT, uri);
437 intent.putExtra(Calendar.EVENT_BEGIN_TIME, mStartMillis);
438 intent.putExtra(Calendar.EVENT_END_TIME, mEndMillis);
439 intent.setClass(EventInfoActivity.this, EditEvent.class);
440 startActivity(intent);
444 private void doDelete() {
445 mDeleteEventHelper.delete(mStartMillis, mEndMillis, mEventCursor, -1);
448 private void updateView() {
449 if (mEventCursor == null) {
452 Resources res = getResources();
453 ContentResolver cr = getContentResolver();
455 String eventName = mEventCursor.getString(EVENT_INDEX_TITLE);
456 if (eventName == null || eventName.length() == 0) {
457 eventName = res.getString(R.string.no_title_label);
460 boolean allDay = mEventCursor.getInt(EVENT_INDEX_ALL_DAY) != 0;
461 String location = mEventCursor.getString(EVENT_INDEX_EVENT_LOCATION);
462 String description = mEventCursor.getString(EVENT_INDEX_DESCRIPTION);
463 String rRule = mEventCursor.getString(EVENT_INDEX_RRULE);
464 boolean hasAlarm = mEventCursor.getInt(EVENT_INDEX_HAS_ALARM) != 0;
465 String eventTimezone = mEventCursor.getString(EVENT_INDEX_EVENT_TIMEZONE);
466 int color = mEventCursor.getInt(EVENT_INDEX_COLOR) & 0xbbffffff;
468 ImageView stripe = (ImageView) findViewById(R.id.vertical_stripe);
469 stripe.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
472 if (eventName != null) {
473 setTextCommon(R.id.title, eventName);
480 flags = DateUtils.FORMAT_UTC | DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_DATE;
482 flags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE;
483 if (DateFormat.is24HourFormat(this)) {
484 flags |= DateUtils.FORMAT_24HOUR;
487 when = DateUtils.formatDateRange(this, mStartMillis, mEndMillis, flags);
488 setTextCommon(R.id.when, when);
490 // Show the event timezone if it is different from the local timezone
491 Time time = new Time();
492 String localTimezone = time.timezone;
494 localTimezone = Time.TIMEZONE_UTC;
496 if (eventTimezone != null && !localTimezone.equals(eventTimezone) && !allDay) {
497 setTextCommon(R.id.timezone, localTimezone);
499 setVisibilityCommon(R.id.timezone_container, View.GONE);
504 EventRecurrence eventRecurrence = new EventRecurrence();
505 eventRecurrence.parse(rRule);
506 Time date = new Time();
508 date.timezone = Time.TIMEZONE_UTC;
510 date.set(mStartMillis);
511 eventRecurrence.setStartDate(date);
512 String repeatString = eventRecurrence.getRepeatString();
513 setTextCommon(R.id.repeat, repeatString);
515 setVisibilityCommon(R.id.repeat_container, View.GONE);
519 if (location == null || location.length() == 0) {
520 setVisibilityCommon(R.id.where, View.GONE);
522 setTextCommon(R.id.where, location);
526 if (description == null || description.length() == 0) {
527 setVisibilityCommon(R.id.description, View.GONE);
529 setTextCommon(R.id.description, description);
533 if (mCalendarsCursor != null) {
534 mCalendarsCursor.moveToFirst();
535 String calendarName = mCalendarsCursor.getString(CALENDARS_INDEX_DISPLAY_NAME);
536 setTextCommon(R.id.calendar, calendarName);
538 setVisibilityCommon(R.id.calendar_container, View.GONE);
545 void updateResponse() {
546 if (mVisibility < Calendars.CONTRIBUTOR_ACCESS ||
547 mRelationship != Attendees.RELATIONSHIP_ATTENDEE) {
548 setVisibilityCommon(R.id.response_container, View.GONE);
552 setVisibilityCommon(R.id.response_container, View.VISIBLE);
554 Spinner spinner = (Spinner) findViewById(R.id.response_value);
556 mOriginalAttendeeResponse = ATTENDEE_NO_RESPONSE;
557 if (mAttendeesCursor != null) {
558 mOriginalAttendeeResponse = mAttendeesCursor.getInt(ATTENDEES_INDEX_STATUS);
562 /* If the user has previously responded to this event
563 * we should not allow them to select no response again.
564 * Switch the entries to a set of entries without the
565 * no response option.
567 if ((mOriginalAttendeeResponse != Attendees.ATTENDEE_STATUS_INVITED)
568 && (mOriginalAttendeeResponse != ATTENDEE_NO_RESPONSE)
569 && (mOriginalAttendeeResponse != Attendees.ATTENDEE_STATUS_NONE)) {
570 CharSequence[] entries;
571 entries = getResources().getTextArray(R.array.response_labels2);
572 mResponseOffset = -1;
573 ArrayAdapter<CharSequence> adapter =
574 new ArrayAdapter<CharSequence>(this,
575 android.R.layout.simple_spinner_item, entries);
576 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
577 spinner.setAdapter(adapter);
580 int index = findResponseIndexFor(mOriginalAttendeeResponse);
581 spinner.setSelection(index + mResponseOffset);
584 private void setTextCommon(int id, CharSequence text) {
585 TextView textView = (TextView) findViewById(id);
586 if (textView == null)
588 textView.setText(text);
591 private void setVisibilityCommon(int id, int visibility) {
592 View v = findViewById(id);
594 v.setVisibility(visibility);