EditEventActivity.java revision 016d576c1f0fbcbf060fab132d8e6a1016dd7091
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.Calendar.EVENT_BEGIN_TIME;
20import static android.provider.Calendar.EVENT_END_TIME;
21
22import android.app.ActionBar;
23import android.app.FragmentTransaction;
24import android.content.Intent;
25import android.net.Uri;
26import android.os.Bundle;
27import android.provider.Calendar;
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.edit_event_fragment);
55
56        mEventInfo = getEventInfoFromIntent(icicle);
57
58        mEditFragment = (EditEventFragment) getFragmentManager().findFragmentById(R.id.edit_event);
59
60        mIsMultipane = Utils.isMultiPaneConfiguration (this);
61
62        if (mIsMultipane) {
63            getActionBar().setDisplayOptions(
64                ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
65        }
66        else {
67            getActionBar().setDisplayOptions(0,
68                    ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME|
69                    ActionBar.DISPLAY_SHOW_TITLE);
70        }
71
72        if (mEditFragment == null) {
73            Intent intent = null;
74            if (mEventInfo.id == -1) {
75                intent = getIntent();
76            }
77
78            mEditFragment = new EditEventFragment(mEventInfo, false, intent);
79
80            mEditFragment.mShowModifyDialogOnLaunch = getIntent().getBooleanExtra(
81                    CalendarController.EVENT_EDIT_ON_LAUNCH, false);
82
83            FragmentTransaction ft = getFragmentManager().beginTransaction();
84            ft.replace(R.id.edit_event, mEditFragment);
85            ft.show(mEditFragment);
86            ft.commit();
87        }
88    }
89
90    private EventInfo getEventInfoFromIntent(Bundle icicle) {
91        EventInfo info = new EventInfo();
92        long eventId = -1;
93        Intent intent = getIntent();
94        Uri data = intent.getData();
95        if (data != null) {
96            try {
97                eventId = Long.parseLong(data.getLastPathSegment());
98            } catch (NumberFormatException e) {
99                if (DEBUG) {
100                    Log.d(TAG, "Create new event");
101                }
102            }
103        } else if (icicle != null && icicle.containsKey(BUNDLE_KEY_EVENT_ID)) {
104            eventId = icicle.getLong(BUNDLE_KEY_EVENT_ID);
105        }
106
107        long begin = intent.getLongExtra(EVENT_BEGIN_TIME, -1);
108        long end = intent.getLongExtra(EVENT_END_TIME, -1);
109        if (end != -1) {
110            info.endTime = new Time();
111            info.endTime.set(end);
112        }
113        if (begin != -1) {
114            info.startTime = new Time();
115            info.startTime.set(begin);
116        }
117        info.id = eventId;
118
119        return info;
120    }
121
122    @Override
123    public boolean onOptionsItemSelected(MenuItem item) {
124        if (item.getItemId() == android.R.id.home) {
125            Intent launchIntent = new Intent();
126            launchIntent.setAction(Intent.ACTION_VIEW);
127            launchIntent.setData(Uri.parse(Calendar.CONTENT_URI + "/time"));
128            launchIntent.setFlags(
129                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP);
130            startActivity(launchIntent);
131            return true;
132        }
133        return super.onOptionsItemSelected(item);
134    }
135}
136