EventInfoActivity.java revision 60f01c23282284e691fcd24918ea67087540f95d
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 */
16package com.android.calendar;
17
18import static android.provider.Calendar.EVENT_BEGIN_TIME;
19import static android.provider.Calendar.EVENT_END_TIME;
20import static com.android.calendar.CalendarController.ATTENDEE_NO_RESPONSE;
21import static com.android.calendar.CalendarController.EVENT_ATTENDEE_RESPONSE;
22import static com.android.calendar.CalendarController.EVENT_EDIT_ON_LAUNCH;
23
24import android.app.ActionBar;
25import android.app.Activity;
26import android.app.FragmentManager;
27import android.app.FragmentTransaction;
28import android.content.ContentUris;
29import android.content.Context;
30import android.content.Intent;
31import android.net.Uri;
32import android.os.Bundle;
33import android.os.Handler;
34import android.provider.Calendar;
35import android.provider.Calendar.Events;
36import android.text.format.Time;
37import android.util.Log;
38import android.view.Menu;
39import android.view.MenuItem;
40
41import com.android.calendar.EventInfoFragment;
42import com.android.calendar.CalendarController.EventType;
43import com.android.calendar.event.EditEventActivity;
44
45import dalvik.system.VMRuntime;
46
47public class EventInfoActivity extends Activity {
48//        implements CalendarController.EventHandler, SearchView.OnQueryTextListener,
49//        SearchView.OnCloseListener {
50
51    private static final String TAG = "EventInfoActivity";
52    private EventInfoFragment mInfoFragment;
53    private long mStartMillis, mEndMillis;
54    private long mEventId;
55
56    @Override
57    protected void onCreate(Bundle icicle) {
58        super.onCreate(icicle);
59
60        setContentView(R.layout.event_info_activity);
61
62        // Make sure the home button is visible
63        getActionBar().setDisplayOptions(
64                ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
65
66        // Get the fragment if exists
67        mInfoFragment = (EventInfoFragment)
68            getFragmentManager().findFragmentById(R.id.event_info_fullscreen);
69
70
71        // Get the info needed for the fragment
72        Intent intent = getIntent();
73        int attendeeResponse = 0;
74        mEventId = 0;
75        boolean isDialog = false;
76
77        if (icicle != null) {
78            mEventId = icicle.getLong(EventInfoFragment.BUNDLE_KEY_EVENT_ID);
79            mStartMillis = icicle.getLong(EventInfoFragment.BUNDLE_KEY_START_MILLIS);
80            mEndMillis = icicle.getLong(EventInfoFragment.BUNDLE_KEY_END_MILLIS);
81            attendeeResponse = icicle.getInt(EventInfoFragment.BUNDLE_KEY_ATTENDEE_RESPONSE);
82            isDialog = icicle.getBoolean(EventInfoFragment.BUNDLE_KEY_IS_DIALOG);
83        } else if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
84            mStartMillis = intent.getLongExtra(EVENT_BEGIN_TIME, 0);
85            mEndMillis = intent.getLongExtra(EVENT_END_TIME, 0);
86            attendeeResponse = intent.getIntExtra(EVENT_ATTENDEE_RESPONSE, ATTENDEE_NO_RESPONSE);
87            Uri data = intent.getData();
88            if (data != null) {
89                try {
90                    mEventId = Long.parseLong(data.getLastPathSegment());
91                } catch (NumberFormatException e) {
92                    Log.wtf(TAG,"No event id");
93                }
94            }
95        }
96
97        // Create a new fragment if none exists
98        if (mInfoFragment == null) {
99            FragmentManager fragmentManager = getFragmentManager();
100            FragmentTransaction ft = fragmentManager.beginTransaction();
101            mInfoFragment = new EventInfoFragment(this, mEventId, mStartMillis, mEndMillis,
102                    attendeeResponse, isDialog);
103            ft.replace(R.id.event_info_fullscreen, mInfoFragment);
104            ft.commit();
105        }
106    }
107
108    @Override
109    public boolean onOptionsItemSelected(MenuItem item) {
110
111        // Handles option menu selections:
112        // Home button - close event info activity and start the main calendar one
113        // Edit button - start the event edit activity and close the info activity
114        // Delete button - start a delete query that calls a runnable that close the info activity
115
116        switch (item.getItemId()) {
117            case android.R.id.home:
118                Intent launchIntent = new Intent();
119                launchIntent.setAction(Intent.ACTION_VIEW);
120                launchIntent.setData(Uri.parse(Calendar.CONTENT_URI + "/time"));
121                launchIntent.setFlags(
122                        Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP);
123                startActivity(launchIntent);
124                finish();
125                return true;
126            case R.id.info_action_edit:
127                Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, mEventId);
128                Intent intent = new Intent(Intent.ACTION_EDIT, uri);
129                intent.putExtra(EVENT_BEGIN_TIME, mStartMillis);
130                intent.putExtra(EVENT_END_TIME, mEndMillis);
131                intent.setClass(this, EditEventActivity.class);
132                intent.putExtra(EVENT_EDIT_ON_LAUNCH, true);
133                startActivity(intent);
134                finish ();
135                break;
136            case R.id.info_action_delete:
137                DeleteEventHelper deleteHelper = new DeleteEventHelper(
138                        this, this, true /* exitWhenDone */);
139                deleteHelper.delete(mStartMillis, mEndMillis, mEventId, -1, onDeleteRunnable);
140                break;
141            default:
142                break;
143        }
144        return super.onOptionsItemSelected(item);
145    }
146
147    // runs at the end of a delete action and closes the activity
148    private Runnable onDeleteRunnable = new Runnable() {
149        @Override
150        public void run() {
151            finish ();
152        }
153    };
154
155    @Override
156    protected void onNewIntent(Intent intent) {
157        // From the Android Dev Guide: "It's important to note that when
158        // onNewIntent(Intent) is called, the Activity has not been restarted,
159        // so the getIntent() method will still return the Intent that was first
160        // received with onCreate(). This is why setIntent(Intent) is called
161        // inside onNewIntent(Intent) (just in case you call getIntent() at a
162        // later time)."
163        setIntent(intent);
164    }
165
166
167    @Override
168    public void onSaveInstanceState(Bundle outState) {
169        super.onSaveInstanceState(outState);
170    }
171
172    @Override
173    protected void onResume() {
174        super.onResume();
175    }
176
177    @Override
178    protected void onPause() {
179        super.onPause();
180    }
181
182    @Override
183    protected void onDestroy() {
184        super.onDestroy();
185    }
186}
187