1/*
2 * Copyright (C) 2010 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.event;
18
19import static android.provider.CalendarContract.EXTRA_EVENT_ALL_DAY;
20import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME;
21import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME;
22
23import android.app.ActionBar;
24import android.app.FragmentTransaction;
25import android.content.Intent;
26import android.net.Uri;
27import android.os.Bundle;
28import android.provider.CalendarContract.Events;
29import android.text.format.Time;
30import android.util.Log;
31import android.view.MenuItem;
32
33import com.android.calendar.AbstractCalendarActivity;
34import com.android.calendar.CalendarController;
35import com.android.calendar.CalendarController.EventInfo;
36import com.android.calendar.CalendarEventModel.ReminderEntry;
37import com.android.calendar.R;
38import com.android.calendar.Utils;
39
40import java.util.ArrayList;
41
42public class EditEventActivity extends AbstractCalendarActivity {
43    private static final String TAG = "EditEventActivity";
44
45    private static final boolean DEBUG = false;
46
47    private static final String BUNDLE_KEY_EVENT_ID = "key_event_id";
48
49    public static final String EXTRA_EVENT_COLOR = "event_color";
50
51    public static final String EXTRA_EVENT_REMINDERS = "reminders";
52
53    private static boolean mIsMultipane;
54
55    private EditEventFragment mEditFragment;
56
57    private ArrayList<ReminderEntry> mReminders;
58
59    private int mEventColor;
60
61    private boolean mEventColorInitialized;
62
63    private EventInfo mEventInfo;
64
65    @Override
66    protected void onCreate(Bundle icicle) {
67        super.onCreate(icicle);
68        setContentView(R.layout.simple_frame_layout);
69
70        mEventInfo = getEventInfoFromIntent(icicle);
71        mReminders = getReminderEntriesFromIntent();
72        mEventColorInitialized = getIntent().hasExtra(EXTRA_EVENT_COLOR);
73        mEventColor = getIntent().getIntExtra(EXTRA_EVENT_COLOR, -1);
74
75
76        mEditFragment = (EditEventFragment) getFragmentManager().findFragmentById(R.id.main_frame);
77
78        mIsMultipane = Utils.getConfigBool(this, R.bool.multiple_pane_config);
79
80        if (mIsMultipane) {
81            getActionBar().setDisplayOptions(
82                    ActionBar.DISPLAY_SHOW_TITLE,
83                    ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME
84                            | ActionBar.DISPLAY_SHOW_TITLE);
85            getActionBar().setTitle(
86                    mEventInfo.id == -1 ? R.string.event_create : R.string.event_edit);
87        }
88        else {
89            getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
90                    ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME|
91                    ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
92        }
93
94        if (mEditFragment == null) {
95            Intent intent = null;
96            if (mEventInfo.id == -1) {
97                intent = getIntent();
98            }
99
100            mEditFragment = new EditEventFragment(mEventInfo, mReminders, mEventColorInitialized,
101                    mEventColor, false, intent);
102
103            mEditFragment.mShowModifyDialogOnLaunch = getIntent().getBooleanExtra(
104                    CalendarController.EVENT_EDIT_ON_LAUNCH, false);
105
106            FragmentTransaction ft = getFragmentManager().beginTransaction();
107            ft.replace(R.id.main_frame, mEditFragment);
108            ft.show(mEditFragment);
109            ft.commit();
110        }
111    }
112
113    @SuppressWarnings("unchecked")
114    private ArrayList<ReminderEntry> getReminderEntriesFromIntent() {
115        Intent intent = getIntent();
116        return (ArrayList<ReminderEntry>) intent.getSerializableExtra(EXTRA_EVENT_REMINDERS);
117    }
118
119    private EventInfo getEventInfoFromIntent(Bundle icicle) {
120        EventInfo info = new EventInfo();
121        long eventId = -1;
122        Intent intent = getIntent();
123        Uri data = intent.getData();
124        if (data != null) {
125            try {
126                eventId = Long.parseLong(data.getLastPathSegment());
127            } catch (NumberFormatException e) {
128                if (DEBUG) {
129                    Log.d(TAG, "Create new event");
130                }
131            }
132        } else if (icicle != null && icicle.containsKey(BUNDLE_KEY_EVENT_ID)) {
133            eventId = icicle.getLong(BUNDLE_KEY_EVENT_ID);
134        }
135
136        boolean allDay = intent.getBooleanExtra(EXTRA_EVENT_ALL_DAY, false);
137
138        long begin = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, -1);
139        long end = intent.getLongExtra(EXTRA_EVENT_END_TIME, -1);
140        if (end != -1) {
141            info.endTime = new Time();
142            if (allDay) {
143                info.endTime.timezone = Time.TIMEZONE_UTC;
144            }
145            info.endTime.set(end);
146        }
147        if (begin != -1) {
148            info.startTime = new Time();
149            if (allDay) {
150                info.startTime.timezone = Time.TIMEZONE_UTC;
151            }
152            info.startTime.set(begin);
153        }
154        info.id = eventId;
155        info.eventTitle = intent.getStringExtra(Events.TITLE);
156        info.calendarId = intent.getLongExtra(Events.CALENDAR_ID, -1);
157
158        if (allDay) {
159            info.extraLong = CalendarController.EXTRA_CREATE_ALL_DAY;
160        } else {
161            info.extraLong = 0;
162        }
163        return info;
164    }
165
166    @Override
167    public boolean onOptionsItemSelected(MenuItem item) {
168        if (item.getItemId() == android.R.id.home) {
169            Utils.returnToCalendarHome(this);
170            return true;
171        }
172        return super.onOptionsItemSelected(item);
173    }
174}
175