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