EditEventActivity.java 5.16 KB
Newer Older
Erik's avatar
Erik committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.calendar.event;

19
import static android.provider.CalendarContract.EXTRA_EVENT_ALL_DAY;
RoboErik's avatar
RoboErik committed
20 21
import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME;
import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME;
Erik's avatar
Erik committed
22

23
import android.app.ActionBar;
Erik's avatar
Erik committed
24 25 26 27
import android.app.FragmentTransaction;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
28
import android.provider.CalendarContract.Events;
Erik's avatar
Erik committed
29 30
import android.text.format.Time;
import android.util.Log;
31
import android.view.MenuItem;
Michael Chan's avatar
Michael Chan committed
32 33 34 35 36 37

import com.android.calendar.AbstractCalendarActivity;
import com.android.calendar.CalendarController;
import com.android.calendar.CalendarController.EventInfo;
import com.android.calendar.R;
import com.android.calendar.Utils;
Erik's avatar
Erik committed
38 39 40 41 42 43 44 45

public class EditEventActivity extends AbstractCalendarActivity {
    private static final String TAG = "EditEventActivity";

    private static final boolean DEBUG = false;

    private static final String BUNDLE_KEY_EVENT_ID = "key_event_id";

46 47
    private static boolean mIsMultipane;

Erik's avatar
Erik committed
48 49 50 51 52 53 54
    private EditEventFragment mEditFragment;

    private EventInfo mEventInfo;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
55
        setContentView(R.layout.simple_frame_layout);
Erik's avatar
Erik committed
56 57 58

        mEventInfo = getEventInfoFromIntent(icicle);

59
        mEditFragment = (EditEventFragment) getFragmentManager().findFragmentById(R.id.main_frame);
Erik's avatar
Erik committed
60

Isaac Katzenelson's avatar
Isaac Katzenelson committed
61
        mIsMultipane = Utils.getConfigBool(this, R.bool.multiple_pane_config);
62 63 64

        if (mIsMultipane) {
            getActionBar().setDisplayOptions(
65 66 67 68 69
                    ActionBar.DISPLAY_SHOW_TITLE,
                    ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME
                            | ActionBar.DISPLAY_SHOW_TITLE);
            getActionBar().setTitle(
                    mEventInfo.id == -1 ? R.string.event_create : R.string.event_edit);
70 71
        }
        else {
72
            getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
73
                    ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME|
74
                    ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
75 76
        }

77
        if (mEditFragment == null) {
Michael Chan's avatar
Michael Chan committed
78 79 80 81 82 83
            Intent intent = null;
            if (mEventInfo.id == -1) {
                intent = getIntent();
            }

            mEditFragment = new EditEventFragment(mEventInfo, false, intent);
84

Erik's avatar
Erik committed
85 86 87
            mEditFragment.mShowModifyDialogOnLaunch = getIntent().getBooleanExtra(
                    CalendarController.EVENT_EDIT_ON_LAUNCH, false);

Dianne Hackborn's avatar
Dianne Hackborn committed
88
            FragmentTransaction ft = getFragmentManager().beginTransaction();
89
            ft.replace(R.id.main_frame, mEditFragment);
90 91 92
            ft.show(mEditFragment);
            ft.commit();
        }
Erik's avatar
Erik committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    }

    private EventInfo getEventInfoFromIntent(Bundle icicle) {
        EventInfo info = new EventInfo();
        long eventId = -1;
        Intent intent = getIntent();
        Uri data = intent.getData();
        if (data != null) {
            try {
                eventId = Long.parseLong(data.getLastPathSegment());
            } catch (NumberFormatException e) {
                if (DEBUG) {
                    Log.d(TAG, "Create new event");
                }
            }
        } else if (icicle != null && icicle.containsKey(BUNDLE_KEY_EVENT_ID)) {
            eventId = icicle.getLong(BUNDLE_KEY_EVENT_ID);
        }

112 113
        boolean allDay = intent.getBooleanExtra(EXTRA_EVENT_ALL_DAY, false);

RoboErik's avatar
RoboErik committed
114 115
        long begin = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, -1);
        long end = intent.getLongExtra(EXTRA_EVENT_END_TIME, -1);
Erik's avatar
Erik committed
116 117
        if (end != -1) {
            info.endTime = new Time();
118 119 120
            if (allDay) {
                info.endTime.timezone = Time.TIMEZONE_UTC;
            }
Erik's avatar
Erik committed
121 122 123 124
            info.endTime.set(end);
        }
        if (begin != -1) {
            info.startTime = new Time();
125 126 127
            if (allDay) {
                info.startTime.timezone = Time.TIMEZONE_UTC;
            }
Erik's avatar
Erik committed
128 129 130
            info.startTime.set(begin);
        }
        info.id = eventId;
131 132
        info.eventTitle = intent.getStringExtra(Events.TITLE);
        info.calendarId = intent.getLongExtra(Events.CALENDAR_ID, -1);
Erik's avatar
Erik committed
133

134
        if (allDay) {
135 136 137 138
            info.extraLong = CalendarController.EXTRA_CREATE_ALL_DAY;
        } else {
            info.extraLong = 0;
        }
Erik's avatar
Erik committed
139 140
        return info;
    }
141 142 143 144

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
145
            Utils.returnToCalendarHome(this);
146 147 148 149
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
Erik's avatar
Erik committed
150
}