1ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan/*
2954c871f4753d3182314d7027c0dd17c236d9ca7Erik * Copyright (C) 2010 The Android Open Source Project
3ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan *
4ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan * Licensed under the Apache License, Version 2.0 (the "License");
5ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan * you may not use this file except in compliance with the License.
6ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan * You may obtain a copy of the License at
7ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan *
8ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan *      http://www.apache.org/licenses/LICENSE-2.0
9ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan *
10ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan * Unless required by applicable law or agreed to in writing, software
11ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan * distributed under the License is distributed on an "AS IS" BASIS,
12ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan * See the License for the specific language governing permissions and
14ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan * limitations under the License.
15ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan */
16ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
17ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanpackage com.android.calendar;
18ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
1983b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chanimport com.android.calendar.CalendarController.EventInfo;
2083b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chanimport com.android.calendar.CalendarController.EventType;
2183b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan
22ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanimport android.app.Fragment;
23ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanimport android.content.Context;
24ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanimport android.os.Bundle;
25ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanimport android.text.format.Time;
26ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanimport android.view.LayoutInflater;
27ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanimport android.view.View;
28ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanimport android.view.ViewGroup;
29ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanimport android.view.ViewGroup.LayoutParams;
30ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanimport android.view.animation.Animation;
31ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanimport android.view.animation.AnimationUtils;
32ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanimport android.widget.ProgressBar;
33ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanimport android.widget.ViewSwitcher;
34ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chanimport android.widget.ViewSwitcher.ViewFactory;
35ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
36ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan/**
37ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan * This is the base class for Day and Week Activities.
38ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan */
3983b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chanpublic class DayFragment extends Fragment implements CalendarController.EventHandler, ViewFactory {
40ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    /**
41ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan     * The view id used for all the views we create. It's OK to have all child
42ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan     * views have the same ID. This ID is used to pick which view receives
43ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan     * focus when a view hierarchy is saved / restore
44ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan     */
45ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    private static final int VIEW_ID = 1;
46ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
47ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    protected static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time";
48ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
49ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    protected ProgressBar mProgressBar;
50ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    protected ViewSwitcher mViewSwitcher;
51ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    protected Animation mInAnimationForward;
52ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    protected Animation mOutAnimationForward;
53ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    protected Animation mInAnimationBackward;
54ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    protected Animation mOutAnimationBackward;
55ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    EventLoader mEventLoader;
56ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
57ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    Time mSelectedDay = new Time();
58ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
590a30ef1efee8fee2a2aecb202afb56f971f3e604Isaac Katzenelson    private final Runnable mTZUpdater = new Runnable() {
6040bcd101b212c9863c3110c05a487a7ae6ebc3caErik        @Override
6140bcd101b212c9863c3110c05a487a7ae6ebc3caErik        public void run() {
6240bcd101b212c9863c3110c05a487a7ae6ebc3caErik            if (!DayFragment.this.isAdded()) {
6340bcd101b212c9863c3110c05a487a7ae6ebc3caErik                return;
6440bcd101b212c9863c3110c05a487a7ae6ebc3caErik            }
6540bcd101b212c9863c3110c05a487a7ae6ebc3caErik            String tz = Utils.getTimeZone(getActivity(), mTZUpdater);
6604b288958078f8b29870dcbb2239df448c559610Erik            mSelectedDay.timezone = tz;
6704b288958078f8b29870dcbb2239df448c559610Erik            mSelectedDay.normalize(true);
6840bcd101b212c9863c3110c05a487a7ae6ebc3caErik        }
6940bcd101b212c9863c3110c05a487a7ae6ebc3caErik    };
7040bcd101b212c9863c3110c05a487a7ae6ebc3caErik
7175d9b567e1fd3b9c6945cd0afe28781f5a19c6c8Michael Chan    private int mNumDays;
7275d9b567e1fd3b9c6945cd0afe28781f5a19c6c8Michael Chan
733458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan    public DayFragment() {
743458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan        mSelectedDay.setToNow();
753458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan    }
763458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan
7775d9b567e1fd3b9c6945cd0afe28781f5a19c6c8Michael Chan    public DayFragment(long timeMillis, int numOfDays) {
7875d9b567e1fd3b9c6945cd0afe28781f5a19c6c8Michael Chan        mNumDays = numOfDays;
793458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan        if (timeMillis == 0) {
803458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan            mSelectedDay.setToNow();
813458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan        } else {
823458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan            mSelectedDay.set(timeMillis);
833458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan        }
843458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan    }
853458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan
86ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    @Override
87ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    public void onCreate(Bundle icicle) {
88ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        super.onCreate(icicle);
89ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
90ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        Context context = getActivity();
91ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
92ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        mInAnimationForward = AnimationUtils.loadAnimation(context, R.anim.slide_left_in);
93ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        mOutAnimationForward = AnimationUtils.loadAnimation(context, R.anim.slide_left_out);
94ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        mInAnimationBackward = AnimationUtils.loadAnimation(context, R.anim.slide_right_in);
95ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        mOutAnimationBackward = AnimationUtils.loadAnimation(context, R.anim.slide_right_out);
96ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
97ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        mEventLoader = new EventLoader(context);
98ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
99ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
100954c871f4753d3182314d7027c0dd17c236d9ca7Erik    @Override
10183b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan    public View onCreateView(LayoutInflater inflater, ViewGroup container,
10283b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan            Bundle savedInstanceState) {
10375d9b567e1fd3b9c6945cd0afe28781f5a19c6c8Michael Chan        View v = inflater.inflate(R.layout.day_activity, null);
104ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
105ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        mViewSwitcher = (ViewSwitcher) v.findViewById(R.id.switcher);
106ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        mViewSwitcher.setFactory(this);
107ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        mViewSwitcher.getCurrentView().requestFocus();
108eaafa2b48be7194a61754604ae37b3d62e9118d8Erik        ((DayView) mViewSwitcher.getCurrentView()).updateTitle();
109ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
110ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        return v;
111ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
112ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
113ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    public View makeView() {
11440bcd101b212c9863c3110c05a487a7ae6ebc3caErik        mTZUpdater.run();
115d190418220467c02327dd9b3ae1eae238889720bMichael Chan        DayView view = new DayView(getActivity(), CalendarController
11675d9b567e1fd3b9c6945cd0afe28781f5a19c6c8Michael Chan                .getInstance(getActivity()), mViewSwitcher, mEventLoader, mNumDays);
117ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        view.setId(VIEW_ID);
118ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        view.setLayoutParams(new ViewSwitcher.LayoutParams(
119ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
1205b872523e28fc778a3fc34e7e9096ca1197c6509RoboErik        view.setSelected(mSelectedDay, false, false);
121ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        return view;
122ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
123ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
124ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    @Override
125ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    public void onResume() {
126ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        super.onResume();
127ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        mEventLoader.startBackgroundThread();
12804b288958078f8b29870dcbb2239df448c559610Erik        mTZUpdater.run();
129ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        eventsChanged();
130d190418220467c02327dd9b3ae1eae238889720bMichael Chan        DayView view = (DayView) mViewSwitcher.getCurrentView();
13161a2e87e7412281d17e86ccff4352cbf32fe77e7Michael Chan        view.handleOnResume();
132ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        view.restartCurrentTimeUpdates();
133ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
134d190418220467c02327dd9b3ae1eae238889720bMichael Chan        view = (DayView) mViewSwitcher.getNextView();
13561a2e87e7412281d17e86ccff4352cbf32fe77e7Michael Chan        view.handleOnResume();
1364d090a71665abd77cbf6dd582915acd24bd8f741Erik        view.restartCurrentTimeUpdates();
137ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
138ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
139ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    @Override
140ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    public void onSaveInstanceState(Bundle outState) {
141ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        super.onSaveInstanceState(outState);
142ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
143a60657d3f50d8943f27b9e649953690d797cc7f9RoboErik        long time = getSelectedTimeInMillis();
144a60657d3f50d8943f27b9e649953690d797cc7f9RoboErik        if (time != -1) {
145a60657d3f50d8943f27b9e649953690d797cc7f9RoboErik            outState.putLong(BUNDLE_KEY_RESTORE_TIME, time);
146a60657d3f50d8943f27b9e649953690d797cc7f9RoboErik        }
147ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
148ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
149ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    @Override
150ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    public void onPause() {
151ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        super.onPause();
152d190418220467c02327dd9b3ae1eae238889720bMichael Chan        DayView view = (DayView) mViewSwitcher.getCurrentView();
153ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        view.cleanup();
154d190418220467c02327dd9b3ae1eae238889720bMichael Chan        view = (DayView) mViewSwitcher.getNextView();
155ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        view.cleanup();
156ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        mEventLoader.stopBackgroundThread();
1570a30ef1efee8fee2a2aecb202afb56f971f3e604Isaac Katzenelson
1580a30ef1efee8fee2a2aecb202afb56f971f3e604Isaac Katzenelson        // Stop events cross-fade animation
1590a30ef1efee8fee2a2aecb202afb56f971f3e604Isaac Katzenelson        view.stopEventsAnimation();
1600a30ef1efee8fee2a2aecb202afb56f971f3e604Isaac Katzenelson        ((DayView) mViewSwitcher.getNextView()).stopEventsAnimation();
161ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
162ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
163ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    void startProgressSpinner() {
164ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        // start the progress spinner
165ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        mProgressBar.setVisibility(View.VISIBLE);
166ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
167ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
168ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    void stopProgressSpinner() {
169ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        // stop the progress spinner
170ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        mProgressBar.setVisibility(View.GONE);
171ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
172ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
1735b872523e28fc778a3fc34e7e9096ca1197c6509RoboErik    private void goTo(Time goToTime, boolean ignoreTime, boolean animateToday) {
1743458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan        if (mViewSwitcher == null) {
1753458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan            // The view hasn't been set yet. Just save the time and use it later.
1763458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan            mSelectedDay.set(goToTime);
1773458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan            return;
1783458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan        }
1793458a1775920dda53099b7f1913cd4ec4c8b4095Michael Chan
180d190418220467c02327dd9b3ae1eae238889720bMichael Chan        DayView currentView = (DayView) mViewSwitcher.getCurrentView();
181ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
18283b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan        // How does goTo time compared to what's already displaying?
183487552be8ae3c1b148211c0fbf594d2b73727fd6Michael Chan        int diff = currentView.compareToVisibleTimeRange(goToTime);
18483b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan
18583b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan        if (diff == 0) {
18683b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan            // In visible range. No need to switch view
1875b872523e28fc778a3fc34e7e9096ca1197c6509RoboErik            currentView.setSelected(goToTime, ignoreTime, animateToday);
18883b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan        } else {
18983b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan            // Figure out which way to animate
19046a8b1117b0510a8dfd50586d5d22aa86b8dc1b9Michael Chan            if (diff > 0) {
19146a8b1117b0510a8dfd50586d5d22aa86b8dc1b9Michael Chan                mViewSwitcher.setInAnimation(mInAnimationForward);
19246a8b1117b0510a8dfd50586d5d22aa86b8dc1b9Michael Chan                mViewSwitcher.setOutAnimation(mOutAnimationForward);
19346a8b1117b0510a8dfd50586d5d22aa86b8dc1b9Michael Chan            } else {
19446a8b1117b0510a8dfd50586d5d22aa86b8dc1b9Michael Chan                mViewSwitcher.setInAnimation(mInAnimationBackward);
19546a8b1117b0510a8dfd50586d5d22aa86b8dc1b9Michael Chan                mViewSwitcher.setOutAnimation(mOutAnimationBackward);
19683b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan            }
19783b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan
198d190418220467c02327dd9b3ae1eae238889720bMichael Chan            DayView next = (DayView) mViewSwitcher.getNextView();
19946a8b1117b0510a8dfd50586d5d22aa86b8dc1b9Michael Chan            if (ignoreTime) {
20046a8b1117b0510a8dfd50586d5d22aa86b8dc1b9Michael Chan                next.setFirstVisibleHour(currentView.getFirstVisibleHour());
20146a8b1117b0510a8dfd50586d5d22aa86b8dc1b9Michael Chan            }
20246a8b1117b0510a8dfd50586d5d22aa86b8dc1b9Michael Chan
2035b872523e28fc778a3fc34e7e9096ca1197c6509RoboErik            next.setSelected(goToTime, ignoreTime, animateToday);
20483b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan            next.reloadEvents();
20583b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan            mViewSwitcher.showNext();
20683b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan            next.requestFocus();
207eaafa2b48be7194a61754604ae37b3d62e9118d8Erik            next.updateTitle();
208d9c9a548ea5b0862482b6079c2e1a581c4f6b899Michael Chan            next.restartCurrentTimeUpdates();
20983b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan        }
210ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
211ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
212ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    /**
213ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan     * Returns the selected time in milliseconds. The milliseconds are measured
214ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan     * in UTC milliseconds from the epoch and uniquely specifies any selectable
215ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan     * time.
216ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan     *
217ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan     * @return the selected time in milliseconds
218ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan     */
219ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    public long getSelectedTimeInMillis() {
220a60657d3f50d8943f27b9e649953690d797cc7f9RoboErik        if (mViewSwitcher == null) {
221a60657d3f50d8943f27b9e649953690d797cc7f9RoboErik            return -1;
222a60657d3f50d8943f27b9e649953690d797cc7f9RoboErik        }
223d190418220467c02327dd9b3ae1eae238889720bMichael Chan        DayView view = (DayView) mViewSwitcher.getCurrentView();
224a60657d3f50d8943f27b9e649953690d797cc7f9RoboErik        if (view == null) {
225a60657d3f50d8943f27b9e649953690d797cc7f9RoboErik            return -1;
226a60657d3f50d8943f27b9e649953690d797cc7f9RoboErik        }
227ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        return view.getSelectedTimeInMillis();
228ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
229ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
230ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    public void eventsChanged() {
231e6131a56b85db5a26639aa4e22e1f266c63b2da8Michael Chan        if (mViewSwitcher == null) {
232e6131a56b85db5a26639aa4e22e1f266c63b2da8Michael Chan            return;
233e6131a56b85db5a26639aa4e22e1f266c63b2da8Michael Chan        }
234d190418220467c02327dd9b3ae1eae238889720bMichael Chan        DayView view = (DayView) mViewSwitcher.getCurrentView();
235ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        view.clearCachedEvents();
236ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        view.reloadEvents();
23703f5c96b8163de6709e2b1fa1df61eb15f5958f6Michael Chan
23803f5c96b8163de6709e2b1fa1df61eb15f5958f6Michael Chan        view = (DayView) mViewSwitcher.getNextView();
23903f5c96b8163de6709e2b1fa1df61eb15f5958f6Michael Chan        view.clearCachedEvents();
240ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
241ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
242ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    Event getSelectedEvent() {
243d190418220467c02327dd9b3ae1eae238889720bMichael Chan        DayView view = (DayView) mViewSwitcher.getCurrentView();
244ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        return view.getSelectedEvent();
245ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
246ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
247ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    boolean isEventSelected() {
248d190418220467c02327dd9b3ae1eae238889720bMichael Chan        DayView view = (DayView) mViewSwitcher.getCurrentView();
249ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        return view.isEventSelected();
250ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
251ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
252ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    Event getNewEvent() {
253d190418220467c02327dd9b3ae1eae238889720bMichael Chan        DayView view = (DayView) mViewSwitcher.getCurrentView();
254ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan        return view.getNewEvent();
255ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
256ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
257d190418220467c02327dd9b3ae1eae238889720bMichael Chan    public DayView getNextView() {
258d190418220467c02327dd9b3ae1eae238889720bMichael Chan        return (DayView) mViewSwitcher.getNextView();
259ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan    }
260ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan
26183b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan    public long getSupportedEventTypes() {
262954c871f4753d3182314d7027c0dd17c236d9ca7Erik        return EventType.GO_TO | EventType.EVENTS_CHANGED;
26383b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan    }
26483b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan
26583b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan    public void handleEvent(EventInfo msg) {
26683b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan        if (msg.eventType == EventType.GO_TO) {
26783b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan// TODO support a range of time
26883b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan// TODO support event_id
26983b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan// TODO support select message
2705b872523e28fc778a3fc34e7e9096ca1197c6509RoboErik            goTo(msg.selectedTime, (msg.extraLong & CalendarController.EXTRA_GOTO_DATE) != 0,
2715b872523e28fc778a3fc34e7e9096ca1197c6509RoboErik                    (msg.extraLong & CalendarController.EXTRA_GOTO_TODAY) != 0);
272954c871f4753d3182314d7027c0dd17c236d9ca7Erik        } else if (msg.eventType == EventType.EVENTS_CHANGED) {
273954c871f4753d3182314d7027c0dd17c236d9ca7Erik            eventsChanged();
27483b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan        }
27583b0fe36b523192a504a1d2baf2f36676a80b9d5Michael Chan    }
276ef1a51d6dda6dd8a013bf0937f10c6eb2041b888Michael Chan}
277