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