EventInfoActivity.java revision 72f0026f31ab2171042e2b962534fcb2bd999afa
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 android.app.Activity;
22import android.content.ContentResolver;
23import android.content.ContentUris;
24import android.content.ContentValues;
25import android.content.Intent;
26import android.content.SharedPreferences;
27import android.content.res.Resources;
28import android.database.Cursor;
29import android.graphics.PorterDuff;
30import android.net.Uri;
31import android.os.Bundle;
32import android.pim.EventRecurrence;
33import android.preference.PreferenceManager;
34import android.provider.Calendar;
35import android.provider.Calendar.Attendees;
36import android.provider.Calendar.Calendars;
37import android.provider.Calendar.Events;
38import android.provider.Calendar.Reminders;
39import android.text.format.DateFormat;
40import android.text.format.DateUtils;
41import android.text.format.Time;
42import android.text.util.Linkify;
43import android.util.Log;
44import android.view.KeyEvent;
45import android.view.Menu;
46import android.view.MenuItem;
47import android.view.View;
48import android.widget.AdapterView;
49import android.widget.ArrayAdapter;
50import android.widget.ImageButton;
51import android.widget.LinearLayout;
52import android.widget.Spinner;
53import android.widget.TextView;
54import android.widget.Toast;
55
56import java.util.ArrayList;
57import java.util.Arrays;
58import java.util.TimeZone;
59import java.util.regex.Pattern;
60
61public class EventInfoActivity extends Activity implements View.OnClickListener,
62        AdapterView.OnItemSelectedListener {
63    private static final int MAX_REMINDERS = 5;
64
65    /**
66     * These are the corresponding indices into the array of strings
67     * "R.array.change_response_labels" in the resource file.
68     */
69    static final int UPDATE_SINGLE = 0;
70    static final int UPDATE_ALL = 1;
71
72    private static final String[] EVENT_PROJECTION = new String[] {
73        Events._ID,                  // 0  do not remove; used in DeleteEventHelper
74        Events.TITLE,                // 1  do not remove; used in DeleteEventHelper
75        Events.RRULE,                // 2  do not remove; used in DeleteEventHelper
76        Events.ALL_DAY,              // 3  do not remove; used in DeleteEventHelper
77        Events.CALENDAR_ID,          // 4  do not remove; used in DeleteEventHelper
78        Events.DTSTART,              // 5  do not remove; used in DeleteEventHelper
79        Events._SYNC_ID,             // 6  do not remove; used in DeleteEventHelper
80        Events.EVENT_TIMEZONE,       // 7  do not remove; used in DeleteEventHelper
81        Events.DESCRIPTION,          // 8
82        Events.EVENT_LOCATION,       // 9
83        Events.HAS_ALARM,            // 10
84        Events.ACCESS_LEVEL,         // 11
85        Events.COLOR,                // 12
86    };
87    private static final int EVENT_INDEX_ID = 0;
88    private static final int EVENT_INDEX_TITLE = 1;
89    private static final int EVENT_INDEX_RRULE = 2;
90    private static final int EVENT_INDEX_ALL_DAY = 3;
91    private static final int EVENT_INDEX_CALENDAR_ID = 4;
92    private static final int EVENT_INDEX_SYNC_ID = 6;
93    private static final int EVENT_INDEX_EVENT_TIMEZONE = 7;
94    private static final int EVENT_INDEX_DESCRIPTION = 8;
95    private static final int EVENT_INDEX_EVENT_LOCATION = 9;
96    private static final int EVENT_INDEX_HAS_ALARM = 10;
97    private static final int EVENT_INDEX_ACCESS_LEVEL = 11;
98    private static final int EVENT_INDEX_COLOR = 12;
99
100    private static final String[] ATTENDEES_PROJECTION = new String[] {
101        Attendees._ID,                      // 0
102        Attendees.ATTENDEE_RELATIONSHIP,    // 1
103        Attendees.ATTENDEE_STATUS,          // 2
104    };
105    private static final int ATTENDEES_INDEX_ID = 0;
106    private static final int ATTENDEES_INDEX_RELATIONSHIP = 1;
107    private static final int ATTENDEES_INDEX_STATUS = 2;
108    private static final String ATTENDEES_WHERE = Attendees.EVENT_ID + "=%d";
109
110    static final String[] CALENDARS_PROJECTION = new String[] {
111        Calendars._ID,          // 0
112        Calendars.DISPLAY_NAME, // 1
113    };
114    static final int CALENDARS_INDEX_DISPLAY_NAME = 1;
115    static final String CALENDARS_WHERE = Calendars._ID + "=%d";
116
117    private static final String[] REMINDERS_PROJECTION = new String[] {
118        Reminders._ID,      // 0
119        Reminders.MINUTES,  // 1
120    };
121    private static final int REMINDERS_INDEX_MINUTES = 1;
122    private static final String REMINDERS_WHERE = Reminders.EVENT_ID + "=%d AND (" +
123            Reminders.METHOD + "=" + Reminders.METHOD_ALERT + " OR " + Reminders.METHOD + "=" +
124            Reminders.METHOD_DEFAULT + ")";
125
126    private static final int MENU_GROUP_REMINDER = 1;
127    private static final int MENU_GROUP_EDIT = 2;
128    private static final int MENU_GROUP_DELETE = 3;
129
130    private static final int MENU_ADD_REMINDER = 1;
131    private static final int MENU_EDIT = 2;
132    private static final int MENU_DELETE = 3;
133
134    private static final int ATTENDEE_NO_RESPONSE = -1;
135    private static final int[] ATTENDEE_VALUES = {
136            ATTENDEE_NO_RESPONSE,
137            Attendees.ATTENDEE_STATUS_ACCEPTED,
138            Attendees.ATTENDEE_STATUS_TENTATIVE,
139            Attendees.ATTENDEE_STATUS_DECLINED,
140    };
141
142    private LinearLayout mRemindersContainer;
143
144    private Uri mUri;
145    private long mEventId;
146    private Cursor mEventCursor;
147    private Cursor mAttendeesCursor;
148    private Cursor mCalendarsCursor;
149
150    private long mStartMillis;
151    private long mEndMillis;
152    private int mVisibility = Calendars.NO_ACCESS;
153    private int mRelationship = Attendees.RELATIONSHIP_ORGANIZER;
154
155    private ArrayList<Integer> mOriginalMinutes = new ArrayList<Integer>();
156    private ArrayList<LinearLayout> mReminderItems = new ArrayList<LinearLayout>(0);
157    private ArrayList<Integer> mReminderValues;
158    private ArrayList<String> mReminderLabels;
159    private int mDefaultReminderMinutes;
160
161    private DeleteEventHelper mDeleteEventHelper;
162    private EditResponseHelper mEditResponseHelper;
163
164    private int mResponseOffset;
165    private int mOriginalAttendeeResponse;
166    private boolean mIsRepeating;
167
168    private Pattern mWildcardPattern = Pattern.compile("^.*$");
169
170    // This is called when one of the "remove reminder" buttons is selected.
171    public void onClick(View v) {
172        LinearLayout reminderItem = (LinearLayout) v.getParent();
173        LinearLayout parent = (LinearLayout) reminderItem.getParent();
174        parent.removeView(reminderItem);
175        mReminderItems.remove(reminderItem);
176        updateRemindersVisibility();
177    }
178
179    public void onItemSelected(AdapterView parent, View v, int position, long id) {
180        // If they selected the "No response" option, then don't display the
181        // dialog asking which events to change.
182        if (id == 0 && mResponseOffset == 0) {
183            return;
184        }
185
186        // If this is not a repeating event, then don't display the dialog
187        // asking which events to change.
188        if (!mIsRepeating) {
189            return;
190        }
191
192        // If the selection is the same as the original, then don't display the
193        // dialog asking which events to change.
194        int index = findResponseIndexFor(mOriginalAttendeeResponse);
195        if (position == index + mResponseOffset) {
196            return;
197        }
198
199        // This is a repeating event. We need to ask the user if they mean to
200        // change just this one instance or all instances.
201        mEditResponseHelper.showDialog(mEditResponseHelper.getWhichEvents());
202    }
203
204    public void onNothingSelected(AdapterView parent) {
205    }
206
207    @Override
208    protected void onCreate(Bundle icicle) {
209        super.onCreate(icicle);
210
211        // Event cursor
212        Intent intent = getIntent();
213        mUri = intent.getData();
214        ContentResolver cr = getContentResolver();
215        mStartMillis = intent.getLongExtra(EVENT_BEGIN_TIME, 0);
216        mEndMillis = intent.getLongExtra(EVENT_END_TIME, 0);
217        mEventCursor = managedQuery(mUri, EVENT_PROJECTION, null, null);
218        if (initEventCursor()) {
219            // The cursor is empty. This can happen if the event was deleted.
220            finish();
221            return;
222        }
223
224        setContentView(R.layout.event_info_activity);
225
226        // Attendees cursor
227        Uri uri = Attendees.CONTENT_URI;
228        String where = String.format(ATTENDEES_WHERE, mEventId);
229        mAttendeesCursor = managedQuery(uri, ATTENDEES_PROJECTION, where, null);
230        initAttendeesCursor();
231
232        // Calendars cursor
233        uri = Calendars.CONTENT_URI;
234        where = String.format(CALENDARS_WHERE, mEventCursor.getLong(EVENT_INDEX_CALENDAR_ID));
235        mCalendarsCursor = managedQuery(uri, CALENDARS_PROJECTION, where, null);
236        initCalendarsCursor();
237
238        Resources res = getResources();
239
240        if (mVisibility >= Calendars.CONTRIBUTOR_ACCESS &&
241                mRelationship == Attendees.RELATIONSHIP_ATTENDEE) {
242            setTitle(res.getString(R.string.event_info_title_invite));
243        } else {
244            setTitle(res.getString(R.string.event_info_title));
245        }
246
247        // Initialize the reminder values array.
248        Resources r = getResources();
249        String[] strings = r.getStringArray(R.array.reminder_minutes_values);
250        int size = strings.length;
251        ArrayList<Integer> list = new ArrayList<Integer>(size);
252        for (int i = 0 ; i < size ; i++) {
253            list.add(Integer.parseInt(strings[i]));
254        }
255        mReminderValues = list;
256        String[] labels = r.getStringArray(R.array.reminder_minutes_labels);
257        mReminderLabels = new ArrayList<String>(Arrays.asList(labels));
258
259        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
260        String durationString =
261                prefs.getString(CalendarPreferenceActivity.KEY_DEFAULT_REMINDER, "0");
262        mDefaultReminderMinutes = Integer.parseInt(durationString);
263
264        mRemindersContainer = (LinearLayout) findViewById(R.id.reminder_items_container);
265
266        // Reminders cursor
267        boolean hasAlarm = mEventCursor.getInt(EVENT_INDEX_HAS_ALARM) != 0;
268        if (hasAlarm) {
269            uri = Reminders.CONTENT_URI;
270            where = String.format(REMINDERS_WHERE, mEventId);
271            Cursor reminderCursor = cr.query(uri, REMINDERS_PROJECTION, where, null, null);
272            try {
273                // First pass: collect all the custom reminder minutes (e.g.,
274                // a reminder of 8 minutes) into a global list.
275                while (reminderCursor.moveToNext()) {
276                    int minutes = reminderCursor.getInt(REMINDERS_INDEX_MINUTES);
277                    EditEvent.addMinutesToList(this, mReminderValues, mReminderLabels, minutes);
278                }
279
280                // Second pass: create the reminder spinners
281                reminderCursor.moveToPosition(-1);
282                while (reminderCursor.moveToNext()) {
283                    int minutes = reminderCursor.getInt(REMINDERS_INDEX_MINUTES);
284                    mOriginalMinutes.add(minutes);
285                    EditEvent.addReminder(this, this, mReminderItems, mReminderValues,
286                            mReminderLabels, minutes);
287                }
288            } finally {
289                reminderCursor.close();
290            }
291        }
292
293        updateView();
294        updateRemindersVisibility();
295
296        // Setup the + Add Reminder Button
297        View.OnClickListener addReminderOnClickListener = new View.OnClickListener() {
298            public void onClick(View v) {
299                addReminder();
300            }
301        };
302        ImageButton reminderRemoveButton = (ImageButton) findViewById(R.id.reminder_add);
303        reminderRemoveButton.setOnClickListener(addReminderOnClickListener);
304
305        mDeleteEventHelper = new DeleteEventHelper(this, true /* exit when done */);
306        mEditResponseHelper = new EditResponseHelper(this);
307    }
308
309    @Override
310    protected void onResume() {
311        super.onResume();
312        if (initEventCursor()) {
313            // The cursor is empty. This can happen if the event was deleted.
314            finish();
315            return;
316        }
317        initAttendeesCursor();
318        initCalendarsCursor();
319    }
320
321    /**
322     * Initializes the event cursor, which is expected to point to the first
323     * (and only) result from a query.
324     * @return true if the cursor is empty.
325     */
326    private boolean initEventCursor() {
327        if ((mEventCursor == null) || (mEventCursor.getCount() == 0)) {
328            return true;
329        }
330        mEventCursor.moveToFirst();
331        mVisibility = mEventCursor.getInt(EVENT_INDEX_ACCESS_LEVEL);
332        mEventId = mEventCursor.getInt(EVENT_INDEX_ID);
333        String rRule = mEventCursor.getString(EVENT_INDEX_RRULE);
334        mIsRepeating = (rRule != null);
335        return false;
336    }
337
338    private void initAttendeesCursor() {
339        if (mAttendeesCursor != null) {
340            if (mAttendeesCursor.moveToFirst()) {
341                mRelationship = mAttendeesCursor.getInt(ATTENDEES_INDEX_RELATIONSHIP);
342            }
343        }
344    }
345
346    private void initCalendarsCursor() {
347        if (mCalendarsCursor != null) {
348            mCalendarsCursor.moveToFirst();
349        }
350    }
351
352    @Override
353    public void onPause() {
354        super.onPause();
355        if (!isFinishing()) {
356            return;
357        }
358        ContentResolver cr = getContentResolver();
359        ArrayList<Integer> reminderMinutes = EditEvent.reminderItemsToMinutes(mReminderItems,
360                mReminderValues);
361        boolean changed = EditEvent.saveReminders(cr, mEventId, reminderMinutes, mOriginalMinutes,
362                false /* no force save */);
363        changed |= saveResponse(cr);
364        if (changed) {
365            Toast.makeText(this, R.string.saving_event, Toast.LENGTH_SHORT).show();
366        }
367    }
368
369    @Override
370    public boolean onCreateOptionsMenu(Menu menu) {
371        MenuItem item;
372        item = menu.add(MENU_GROUP_REMINDER, MENU_ADD_REMINDER, 0,
373                R.string.add_new_reminder);
374        item.setIcon(R.drawable.ic_menu_reminder);
375        item.setAlphabeticShortcut('r');
376
377        item = menu.add(MENU_GROUP_EDIT, MENU_EDIT, 0, R.string.edit_event_label);
378        item.setIcon(android.R.drawable.ic_menu_edit);
379        item.setAlphabeticShortcut('e');
380
381        item = menu.add(MENU_GROUP_DELETE, MENU_DELETE, 0, R.string.delete_event_label);
382        item.setIcon(android.R.drawable.ic_menu_delete);
383
384        return super.onCreateOptionsMenu(menu);
385    }
386
387    @Override
388    public boolean onPrepareOptionsMenu(Menu menu) {
389        // Cannot add reminders to a shared calendar with only free/busy
390        // permissions
391        if (mVisibility >= Calendars.READ_ACCESS && mReminderItems.size() < MAX_REMINDERS) {
392            menu.setGroupVisible(MENU_GROUP_REMINDER, true);
393            menu.setGroupEnabled(MENU_GROUP_REMINDER, true);
394        } else {
395            menu.setGroupVisible(MENU_GROUP_REMINDER, false);
396            menu.setGroupEnabled(MENU_GROUP_REMINDER, false);
397        }
398
399        if (mVisibility >= Calendars.CONTRIBUTOR_ACCESS &&
400                mRelationship >= Attendees.RELATIONSHIP_ORGANIZER) {
401            menu.setGroupVisible(MENU_GROUP_EDIT, true);
402            menu.setGroupEnabled(MENU_GROUP_EDIT, true);
403            menu.setGroupVisible(MENU_GROUP_DELETE, true);
404            menu.setGroupEnabled(MENU_GROUP_DELETE, true);
405        } else {
406            menu.setGroupVisible(MENU_GROUP_EDIT, false);
407            menu.setGroupEnabled(MENU_GROUP_EDIT, false);
408            menu.setGroupVisible(MENU_GROUP_DELETE, false);
409            menu.setGroupEnabled(MENU_GROUP_DELETE, false);
410        }
411
412        return super.onPrepareOptionsMenu(menu);
413    }
414
415    private void addReminder() {
416        // TODO: when adding a new reminder, make it different from the
417        // last one in the list (if any).
418        if (mDefaultReminderMinutes == 0) {
419            EditEvent.addReminder(this, this, mReminderItems,
420                    mReminderValues, mReminderLabels, 10 /* minutes */);
421        } else {
422            EditEvent.addReminder(this, this, mReminderItems,
423                    mReminderValues, mReminderLabels, mDefaultReminderMinutes);
424        }
425        updateRemindersVisibility();
426    }
427
428    @Override
429    public boolean onOptionsItemSelected(MenuItem item) {
430        super.onOptionsItemSelected(item);
431        switch (item.getItemId()) {
432        case MENU_ADD_REMINDER:
433            addReminder();
434            break;
435        case MENU_EDIT:
436            doEdit();
437            break;
438        case MENU_DELETE:
439            doDelete();
440            break;
441        }
442        return true;
443    }
444
445    @Override
446    public boolean onKeyDown(int keyCode, KeyEvent event) {
447        if (keyCode == KeyEvent.KEYCODE_DEL) {
448            doDelete();
449            return true;
450        }
451        return super.onKeyDown(keyCode, event);
452    }
453
454    private void updateRemindersVisibility() {
455        if (mReminderItems.size() == 0) {
456            mRemindersContainer.setVisibility(View.GONE);
457        } else {
458            mRemindersContainer.setVisibility(View.VISIBLE);
459        }
460    }
461
462    /**
463     * Saves the response to an invitation if the user changed the response.
464     * Returns true if the database was updated.
465     *
466     * @param cr the ContentResolver
467     * @return true if the database was changed
468     */
469    private boolean saveResponse(ContentResolver cr) {
470        if (mAttendeesCursor == null || mEventCursor == null) {
471            return false;
472        }
473        Spinner spinner = (Spinner) findViewById(R.id.response_value);
474        int position = spinner.getSelectedItemPosition() - mResponseOffset;
475        if (position <= 0) {
476            return false;
477        }
478
479        int status = ATTENDEE_VALUES[position];
480
481        // If the status has not changed, then don't update the database
482        if (status == mOriginalAttendeeResponse) {
483            return false;
484        }
485
486        long attendeeId = mAttendeesCursor.getInt(ATTENDEES_INDEX_ID);
487        if (!mIsRepeating) {
488            // This is a non-repeating event
489            updateResponse(cr, mEventId, attendeeId, status);
490            return true;
491        }
492
493        // This is a repeating event
494        int whichEvents = mEditResponseHelper.getWhichEvents();
495        switch (whichEvents) {
496            case -1:
497                return false;
498            case UPDATE_SINGLE:
499                createExceptionResponse(cr, mEventId, attendeeId, status);
500                return true;
501            case UPDATE_ALL:
502                updateResponse(cr, mEventId, attendeeId, status);
503                return true;
504            default:
505                Log.e("Calendar", "Unexpected choice for updating invitation response");
506                break;
507        }
508        return false;
509    }
510
511    private void updateResponse(ContentResolver cr, long eventId, long attendeeId, int status) {
512        // Update the "selfAttendeeStatus" field for the event
513        ContentValues values = new ContentValues();
514
515        // Will need to add email when MULTIPLE_ATTENDEES_PER_EVENT supported.
516        values.put(Attendees.ATTENDEE_STATUS, status);
517        values.put(Attendees.EVENT_ID, eventId);
518
519        Uri uri = ContentUris.withAppendedId(Attendees.CONTENT_URI, attendeeId);
520        cr.update(uri, values, null /* where */, null /* selection args */);
521    }
522
523    private void createExceptionResponse(ContentResolver cr, long eventId,
524            long attendeeId, int status) {
525        // Fetch information about the repeating event.
526        Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
527        Cursor cursor = cr.query(uri, EVENT_PROJECTION, null, null, null);
528        if (cursor == null) {
529            return;
530        }
531
532        try {
533            cursor.moveToFirst();
534            ContentValues values = new ContentValues();
535
536            String title = cursor.getString(EVENT_INDEX_TITLE);
537            String timezone = cursor.getString(EVENT_INDEX_EVENT_TIMEZONE);
538            int calendarId = cursor.getInt(EVENT_INDEX_CALENDAR_ID);
539            boolean allDay = cursor.getInt(EVENT_INDEX_ALL_DAY) != 0;
540            String syncId = cursor.getString(EVENT_INDEX_SYNC_ID);
541
542            values.put(Events.TITLE, title);
543            values.put(Events.EVENT_TIMEZONE, timezone);
544            values.put(Events.ALL_DAY, allDay ? 1 : 0);
545            values.put(Events.CALENDAR_ID, calendarId);
546            values.put(Events.DTSTART, mStartMillis);
547            values.put(Events.DTEND, mEndMillis);
548            values.put(Events.ORIGINAL_EVENT, syncId);
549            values.put(Events.ORIGINAL_INSTANCE_TIME, mStartMillis);
550            values.put(Events.ORIGINAL_ALL_DAY, allDay ? 1 : 0);
551            values.put(Events.STATUS, Events.STATUS_CONFIRMED);
552            values.put(Events.SELF_ATTENDEE_STATUS, status);
553
554            // Create a recurrence exception
555            Uri newUri = cr.insert(Events.CONTENT_URI, values);
556        } finally {
557            cursor.close();
558        }
559    }
560
561    private int findResponseIndexFor(int response) {
562        int size = ATTENDEE_VALUES.length;
563        for (int index = 0; index < size; index++) {
564            if (ATTENDEE_VALUES[index] == response) {
565                return index;
566            }
567        }
568        return 0;
569    }
570
571    private void doEdit() {
572        Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, mEventId);
573        Intent intent = new Intent(Intent.ACTION_EDIT, uri);
574        intent.putExtra(Calendar.EVENT_BEGIN_TIME, mStartMillis);
575        intent.putExtra(Calendar.EVENT_END_TIME, mEndMillis);
576        intent.setClass(EventInfoActivity.this, EditEvent.class);
577        startActivity(intent);
578        finish();
579    }
580
581    private void doDelete() {
582        mDeleteEventHelper.delete(mStartMillis, mEndMillis, mEventCursor, -1);
583    }
584
585    private void updateView() {
586        if (mEventCursor == null) {
587            return;
588        }
589        Resources res = getResources();
590        ContentResolver cr = getContentResolver();
591
592        String eventName = mEventCursor.getString(EVENT_INDEX_TITLE);
593        if (eventName == null || eventName.length() == 0) {
594            eventName = res.getString(R.string.no_title_label);
595        }
596
597        boolean allDay = mEventCursor.getInt(EVENT_INDEX_ALL_DAY) != 0;
598        String location = mEventCursor.getString(EVENT_INDEX_EVENT_LOCATION);
599        String description = mEventCursor.getString(EVENT_INDEX_DESCRIPTION);
600        String rRule = mEventCursor.getString(EVENT_INDEX_RRULE);
601        boolean hasAlarm = mEventCursor.getInt(EVENT_INDEX_HAS_ALARM) != 0;
602        String eventTimezone = mEventCursor.getString(EVENT_INDEX_EVENT_TIMEZONE);
603        int color = mEventCursor.getInt(EVENT_INDEX_COLOR) & 0xbbffffff;
604
605        View calBackground = findViewById(R.id.cal_background);
606        calBackground.setBackgroundColor(color);
607
608        TextView title = (TextView) findViewById(R.id.title);
609        title.setTextColor(color);
610
611        View divider = (View) findViewById(R.id.divider);
612        divider.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
613
614        // What
615        if (eventName != null) {
616            setTextCommon(R.id.title, eventName);
617        }
618
619        // When
620        String when;
621        int flags;
622        if (allDay) {
623            flags = DateUtils.FORMAT_UTC | DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_DATE;
624        } else {
625            flags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE;
626            if (DateFormat.is24HourFormat(this)) {
627                flags |= DateUtils.FORMAT_24HOUR;
628            }
629        }
630        when = DateUtils.formatDateRange(this, mStartMillis, mEndMillis, flags);
631        setTextCommon(R.id.when, when);
632
633        // Show the event timezone if it is different from the local timezone
634        Time time = new Time();
635        String localTimezone = time.timezone;
636        if (allDay) {
637            localTimezone = Time.TIMEZONE_UTC;
638        }
639        if (eventTimezone != null && !localTimezone.equals(eventTimezone) && !allDay) {
640            String displayName;
641            TimeZone tz = TimeZone.getTimeZone(localTimezone);
642            if (tz == null || tz.getID().equals("GMT")) {
643                displayName = localTimezone;
644            } else {
645                displayName = tz.getDisplayName();
646            }
647
648            setTextCommon(R.id.timezone, displayName);
649        } else {
650            setVisibilityCommon(R.id.timezone_container, View.GONE);
651        }
652
653        // Repeat
654        if (rRule != null) {
655            EventRecurrence eventRecurrence = new EventRecurrence();
656            eventRecurrence.parse(rRule);
657            Time date = new Time();
658            if (allDay) {
659                date.timezone = Time.TIMEZONE_UTC;
660            }
661            date.set(mStartMillis);
662            eventRecurrence.setStartDate(date);
663            String repeatString = eventRecurrence.getRepeatString();
664            setTextCommon(R.id.repeat, repeatString);
665        } else {
666            setVisibilityCommon(R.id.repeat_container, View.GONE);
667        }
668
669        // Where
670        if (location == null || location.length() == 0) {
671            setVisibilityCommon(R.id.where, View.GONE);
672        } else {
673            TextView textView = (TextView) findViewById(R.id.where);
674            if (textView != null) {
675                    textView.setAutoLinkMask(0);
676                    textView.setText(location);
677                    Linkify.addLinks(textView, mWildcardPattern, "geo:0,0?q=");
678            }
679        }
680
681        // Description
682        if (description == null || description.length() == 0) {
683            setVisibilityCommon(R.id.description, View.GONE);
684        } else {
685            setTextCommon(R.id.description, description);
686        }
687
688        // Calendar
689        if (mCalendarsCursor != null) {
690            mCalendarsCursor.moveToFirst();
691            String calendarName = mCalendarsCursor.getString(CALENDARS_INDEX_DISPLAY_NAME);
692            setTextCommon(R.id.calendar, calendarName);
693        } else {
694            setVisibilityCommon(R.id.calendar_container, View.GONE);
695        }
696
697        // Response
698        updateResponse();
699    }
700
701    void updateResponse() {
702        if (mVisibility < Calendars.CONTRIBUTOR_ACCESS ||
703                mRelationship != Attendees.RELATIONSHIP_ATTENDEE) {
704            setVisibilityCommon(R.id.response_container, View.GONE);
705            return;
706        }
707
708        setVisibilityCommon(R.id.response_container, View.VISIBLE);
709
710        Spinner spinner = (Spinner) findViewById(R.id.response_value);
711
712        mOriginalAttendeeResponse = ATTENDEE_NO_RESPONSE;
713        if (mAttendeesCursor != null) {
714            mOriginalAttendeeResponse = mAttendeesCursor.getInt(ATTENDEES_INDEX_STATUS);
715        }
716        mResponseOffset = 0;
717
718        /* If the user has previously responded to this event
719         * we should not allow them to select no response again.
720         * Switch the entries to a set of entries without the
721         * no response option.
722         */
723        if ((mOriginalAttendeeResponse != Attendees.ATTENDEE_STATUS_INVITED)
724                && (mOriginalAttendeeResponse != ATTENDEE_NO_RESPONSE)
725                && (mOriginalAttendeeResponse != Attendees.ATTENDEE_STATUS_NONE)) {
726            CharSequence[] entries;
727            entries = getResources().getTextArray(R.array.response_labels2);
728            mResponseOffset = -1;
729            ArrayAdapter<CharSequence> adapter =
730                new ArrayAdapter<CharSequence>(this,
731                        android.R.layout.simple_spinner_item, entries);
732            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
733            spinner.setAdapter(adapter);
734        }
735
736        int index = findResponseIndexFor(mOriginalAttendeeResponse);
737        spinner.setSelection(index + mResponseOffset);
738        spinner.setOnItemSelectedListener(this);
739    }
740
741    private void setTextCommon(int id, CharSequence text) {
742        TextView textView = (TextView) findViewById(id);
743        if (textView == null)
744            return;
745        textView.setText(text);
746    }
747
748    private void setVisibilityCommon(int id, int visibility) {
749        View v = findViewById(id);
750        if (v != null) {
751            v.setVisibility(visibility);
752        }
753        return;
754    }
755}
756