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