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.CalendarContract.EXTRA_EVENT_BEGIN_TIME;
19import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME;
20import static android.provider.CalendarContract.Attendees.ATTENDEE_STATUS;
21
22import android.app.ActionBar;
23import android.app.Activity;
24import android.app.FragmentManager;
25import android.app.FragmentTransaction;
26import android.content.Intent;
27import android.content.res.Resources;
28import android.net.Uri;
29import android.os.Bundle;
30import android.provider.CalendarContract.Attendees;
31import android.util.Log;
32import android.widget.Toast;
33
34import java.util.List;
35
36public class EventInfoActivity extends Activity {
37//        implements CalendarController.EventHandler, SearchView.OnQueryTextListener,
38//        SearchView.OnCloseListener {
39
40    private static final String TAG = "EventInfoActivity";
41    private EventInfoFragment mInfoFragment;
42    private long mStartMillis, mEndMillis;
43    private long mEventId;
44
45    @Override
46    protected void onCreate(Bundle icicle) {
47        super.onCreate(icicle);
48
49        // Get the info needed for the fragment
50        Intent intent = getIntent();
51        int attendeeResponse = 0;
52        mEventId = -1;
53        boolean isDialog = false;
54
55        if (icicle != null) {
56            mEventId = icicle.getLong(EventInfoFragment.BUNDLE_KEY_EVENT_ID);
57            mStartMillis = icicle.getLong(EventInfoFragment.BUNDLE_KEY_START_MILLIS);
58            mEndMillis = icicle.getLong(EventInfoFragment.BUNDLE_KEY_END_MILLIS);
59            attendeeResponse = icicle.getInt(EventInfoFragment.BUNDLE_KEY_ATTENDEE_RESPONSE);
60            isDialog = icicle.getBoolean(EventInfoFragment.BUNDLE_KEY_IS_DIALOG);
61        } else if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
62            mStartMillis = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, 0);
63            mEndMillis = intent.getLongExtra(EXTRA_EVENT_END_TIME, 0);
64            attendeeResponse = intent.getIntExtra(ATTENDEE_STATUS,
65                    Attendees.ATTENDEE_STATUS_NONE);
66            Uri data = intent.getData();
67            if (data != null) {
68                try {
69                    List<String> pathSegments = data.getPathSegments();
70                    int size = pathSegments.size();
71                    if (size > 2 && "EventTime".equals(pathSegments.get(2))) {
72                        // Support non-standard VIEW intent format:
73                        //dat = content://com.android.calendar/events/[id]/EventTime/[start]/[end]
74                        mEventId = Long.parseLong(pathSegments.get(1));
75                        if (size > 4) {
76                            mStartMillis = Long.parseLong(pathSegments.get(3));
77                            mEndMillis = Long.parseLong(pathSegments.get(4));
78                        }
79                    } else {
80                        mEventId = Long.parseLong(data.getLastPathSegment());
81                    }
82                } catch (NumberFormatException e) {
83                    if (mEventId == -1) {
84                        // do nothing here , deal with it later
85                    } else if (mStartMillis == 0 || mEndMillis ==0) {
86                        // Parsing failed on the start or end time , make sure the times were not
87                        // pulled from the intent's extras and reset them.
88                        mStartMillis = 0;
89                        mEndMillis = 0;
90                    }
91                }
92            }
93        }
94
95        if (mEventId == -1) {
96            Log.w(TAG, "No event id");
97            Toast.makeText(this, R.string.event_not_found, Toast.LENGTH_SHORT).show();
98            finish();
99        }
100
101        // If we do not support showing full screen event info in this configuration,
102        // close the activity and show the event in AllInOne.
103        Resources res = getResources();
104        if (!res.getBoolean(R.bool.agenda_show_event_info_full_screen)
105                && !res.getBoolean(R.bool.show_event_info_full_screen)) {
106            CalendarController.getInstance(this)
107                    .launchViewEvent(mEventId, mStartMillis, mEndMillis, attendeeResponse);
108            finish();
109            return;
110        }
111
112        setContentView(R.layout.simple_frame_layout);
113
114        // Get the fragment if exists
115        mInfoFragment = (EventInfoFragment)
116                getFragmentManager().findFragmentById(R.id.main_frame);
117
118
119        // Remove the application title
120        ActionBar bar = getActionBar();
121        if (bar != null) {
122            bar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
123        }
124
125        // Create a new fragment if none exists
126        if (mInfoFragment == null) {
127            FragmentManager fragmentManager = getFragmentManager();
128            FragmentTransaction ft = fragmentManager.beginTransaction();
129            mInfoFragment = new EventInfoFragment(this, mEventId, mStartMillis, mEndMillis,
130                    attendeeResponse, isDialog, isDialog ?
131                            EventInfoFragment.DIALOG_WINDOW_STYLE :
132                                EventInfoFragment.FULL_WINDOW_STYLE);
133            ft.replace(R.id.main_frame, mInfoFragment);
134            ft.commit();
135        }
136    }
137
138//    @Override
139//    public boolean onOptionsItemSelected(MenuItem item) {
140//
141//        // Handles option menu selections:
142//        // Home button - close event info activity and start the main calendar one
143//        // Edit button - start the event edit activity and close the info activity
144//        // Delete button - start a delete query that calls a runnable that close the info activity
145//
146//        switch (item.getItemId()) {
147//            case android.R.id.home:
148//                Intent launchIntent = new Intent();
149//                launchIntent.setAction(Intent.ACTION_VIEW);
150//                launchIntent.setData(Uri.parse(CalendarContract.CONTENT_URI + "/time"));
151//                launchIntent.setFlags(
152//                        Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP);
153//                startActivity(launchIntent);
154//                finish();
155//                return true;
156//            case R.id.info_action_edit:
157//                Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, mEventId);
158//                Intent intent = new Intent(Intent.ACTION_EDIT, uri);
159//                intent.putExtra(EXTRA_EVENT_BEGIN_TIME, mStartMillis);
160//                intent.putExtra(EXTRA_EVENT_END_TIME, mEndMillis);
161//                intent.setClass(this, EditEventActivity.class);
162//                intent.putExtra(EVENT_EDIT_ON_LAUNCH, true);
163//                startActivity(intent);
164//                finish ();
165//                break;
166//            case R.id.info_action_delete:
167//                DeleteEventHelper deleteHelper = new DeleteEventHelper(
168//                        this, this, true /* exitWhenDone */);
169//                deleteHelper.delete(mStartMillis, mEndMillis, mEventId, -1, onDeleteRunnable);
170//                break;
171//            default:
172//                break;
173//        }
174//        return super.onOptionsItemSelected(item);
175//    }
176
177    // runs at the end of a delete action and closes the activity
178//    private Runnable onDeleteRunnable = new Runnable() {
179//        @Override
180//        public void run() {
181//            finish ();
182//        }
183//    };
184
185    @Override
186    protected void onNewIntent(Intent intent) {
187        // From the Android Dev Guide: "It's important to note that when
188        // onNewIntent(Intent) is called, the Activity has not been restarted,
189        // so the getIntent() method will still return the Intent that was first
190        // received with onCreate(). This is why setIntent(Intent) is called
191        // inside onNewIntent(Intent) (just in case you call getIntent() at a
192        // later time)."
193        setIntent(intent);
194    }
195
196
197    @Override
198    public void onSaveInstanceState(Bundle outState) {
199        super.onSaveInstanceState(outState);
200    }
201
202    @Override
203    protected void onResume() {
204        super.onResume();
205    }
206
207    @Override
208    protected void onPause() {
209        super.onPause();
210    }
211
212    @Override
213    protected void onDestroy() {
214        super.onDestroy();
215    }
216}
217