AllInOneActivity.java revision edecd9a97e02ff14bd5abeec3414e1bf3cc631df
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 */
16
17package com.android.calendar;
18
19import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME;
20import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME;
21import static android.provider.CalendarContract.Attendees.ATTENDEE_STATUS;
22import static com.android.calendar.CalendarController.EVENT_ATTENDEE_RESPONSE;
23
24import com.android.calendar.CalendarController.EventHandler;
25import com.android.calendar.CalendarController.EventInfo;
26import com.android.calendar.CalendarController.EventType;
27import com.android.calendar.CalendarController.ViewType;
28import com.android.calendar.agenda.AgendaFragment;
29import com.android.calendar.month.MonthByWeekFragment;
30import com.android.calendar.selectcalendars.SelectVisibleCalendarsFragment;
31
32import android.animation.Animator;
33import android.animation.Animator.AnimatorListener;
34import android.animation.ObjectAnimator;
35import android.app.ActionBar;
36import android.app.ActionBar.Tab;
37import android.app.Activity;
38import android.app.Fragment;
39import android.app.FragmentManager;
40import android.app.FragmentTransaction;
41import android.content.ContentResolver;
42import android.content.ContentUris;
43import android.content.Intent;
44import android.content.SharedPreferences;
45import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
46import android.content.res.Resources;
47import android.database.ContentObserver;
48import android.net.Uri;
49import android.os.Bundle;
50import android.os.Handler;
51import android.provider.CalendarContract;
52import android.provider.CalendarContract.Events;
53import android.text.TextUtils;
54import android.text.format.DateFormat;
55import android.text.format.DateUtils;
56import android.text.format.Time;
57import android.util.Log;
58import android.view.Menu;
59import android.view.MenuItem;
60import android.view.View;
61import android.view.accessibility.AccessibilityEvent;
62import android.widget.RelativeLayout;
63import android.widget.RelativeLayout.LayoutParams;
64import android.widget.SearchView;
65import android.widget.SearchView.OnSuggestionListener;
66import android.widget.TextView;
67
68import java.util.List;
69import java.util.Locale;
70import java.util.TimeZone;
71
72public class AllInOneActivity extends Activity implements EventHandler,
73        OnSharedPreferenceChangeListener, SearchView.OnQueryTextListener, ActionBar.TabListener,
74        ActionBar.OnNavigationListener, OnSuggestionListener {
75    private static final String TAG = "AllInOneActivity";
76    private static final boolean DEBUG = false;
77    private static final String EVENT_INFO_FRAGMENT_TAG = "EventInfoFragment";
78    private static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time";
79    private static final String BUNDLE_KEY_EVENT_ID = "key_event_id";
80    private static final String BUNDLE_KEY_RESTORE_VIEW = "key_restore_view";
81    private static final int HANDLER_KEY = 0;
82    private static final long CONTROLS_ANIMATE_DURATION = 400;
83    private static int CONTROLS_ANIMATE_WIDTH = 280;
84    private static float mScale = 0;
85
86    // Indices of buttons for the drop down menu (tabs replacement)
87    // Must match the strings in the array buttons_list in arrays.xml and the
88    // OnNavigationListener
89    private static final int BUTTON_DAY_INDEX = 0;
90    private static final int BUTTON_WEEK_INDEX = 1;
91    private static final int BUTTON_MONTH_INDEX = 2;
92    private static final int BUTTON_AGENDA_INDEX = 3;
93
94    private CalendarController mController;
95    private static boolean mIsMultipane;
96    private static boolean mIsTabletConfig;
97    private static boolean mShowAgendaWithMonth;
98    private static boolean mShowEventDetailsWithAgenda;
99    private boolean mOnSaveInstanceStateCalled = false;
100    private boolean mBackToPreviousView = false;
101    private ContentResolver mContentResolver;
102    private int mPreviousView;
103    private int mCurrentView;
104    private boolean mPaused = true;
105    private boolean mUpdateOnResume = false;
106    private boolean mHideControls = false;
107    private boolean mShowSideViews = true;
108    private boolean mShowWeekNum = false;
109    private TextView mHomeTime;
110    private TextView mDateRange;
111    private TextView mWeekTextView;
112    private View mMiniMonth;
113    private View mCalendarsList;
114    private View mMiniMonthContainer;
115    private View mSecondaryPane;
116    private String mTimeZone;
117    private boolean mShowCalendarControls;
118    private boolean mShowEventInfoFullScreen;
119    private int mWeekNum;
120
121    private long mViewEventId = -1;
122    private long mIntentEventStartMillis = -1;
123    private long mIntentEventEndMillis = -1;
124    private int mIntentAttendeeResponse = CalendarController.ATTENDEE_NO_RESPONSE;
125
126    // Action bar and Navigation bar (left side of Action bar)
127    private ActionBar mActionBar;
128    private ActionBar.Tab mDayTab;
129    private ActionBar.Tab mWeekTab;
130    private ActionBar.Tab mMonthTab;
131    private ActionBar.Tab mAgendaTab;
132    private SearchView mSearchView;
133    private MenuItem mSearchMenu;
134    private MenuItem mControlsMenu;
135    private Menu mOptionsMenu;
136    private CalendarViewAdapter mActionBarMenuSpinnerAdapter;
137
138    private String mHideString;
139    private String mShowString;
140
141    // Params for animating the controls on the right
142    private LayoutParams mControlsParams = new LayoutParams(CONTROLS_ANIMATE_WIDTH, 0);
143
144    private AnimatorListener mSlideAnimationDoneListener = new AnimatorListener() {
145
146        @Override
147        public void onAnimationCancel(Animator animation) {
148        }
149
150        @Override
151        public void onAnimationEnd(android.animation.Animator animation) {
152            int visibility = mShowSideViews ? View.VISIBLE : View.GONE;
153            mMiniMonth.setVisibility(visibility);
154            mCalendarsList.setVisibility(visibility);
155            mMiniMonthContainer.setVisibility(visibility);
156        }
157
158        @Override
159        public void onAnimationRepeat(android.animation.Animator animation) {
160        }
161
162        @Override
163        public void onAnimationStart(android.animation.Animator animation) {
164        }
165    };
166
167    private Runnable mHomeTimeUpdater = new Runnable() {
168        @Override
169        public void run() {
170            updateSecondaryTitleFields(-1);
171        }
172    };
173
174    // Create an observer so that we can update the views whenever a
175    // Calendar event changes.
176    private ContentObserver mObserver = new ContentObserver(new Handler()) {
177        @Override
178        public boolean deliverSelfNotifications() {
179            return true;
180        }
181
182        @Override
183        public void onChange(boolean selfChange) {
184            eventsChanged();
185        }
186    };
187
188    @Override
189    protected void onNewIntent(Intent intent) {
190        String action = intent.getAction();
191        if (Intent.ACTION_VIEW.equals(action)) {
192            parseViewAction(intent);
193        }
194    }
195
196    @Override
197    protected void onCreate(Bundle icicle) {
198        if (Utils.getSharedPreference(this, OtherPreferences.KEY_OTHER_1, false)) {
199            setTheme(R.style.CalendarTheme_WithActionBarWallpaper);
200        }
201        super.onCreate(icicle);
202
203        // This needs to be created before setContentView
204        mController = CalendarController.getInstance(this);
205
206
207        // Get time from intent or icicle
208        long timeMillis = -1;
209        int viewType = -1;
210        final Intent intent = getIntent();
211        if (icicle != null) {
212            timeMillis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME);
213            viewType = icicle.getInt(BUNDLE_KEY_RESTORE_VIEW, -1);
214        } else {
215            String action = intent.getAction();
216            if (Intent.ACTION_VIEW.equals(action)) {
217                // Open EventInfo later
218                timeMillis = parseViewAction(intent);
219            }
220
221            if (timeMillis == -1) {
222                timeMillis = Utils.timeFromIntentInMillis(intent);
223            }
224        }
225
226        if (viewType == -1) {
227            viewType = Utils.getViewTypeFromIntentAndSharedPref(this);
228        }
229        mTimeZone = Utils.getTimeZone(this, mHomeTimeUpdater);
230        Time t = new Time(mTimeZone);
231        t.set(timeMillis);
232
233        if (icicle != null && intent != null) {
234            Log.d(TAG, "both, icicle:" + icicle.toString() + "  intent:" + intent.toString());
235        } else {
236            Log.d(TAG, "not both, icicle:" + icicle + " intent:" + intent);
237        }
238
239        Resources res = getResources();
240        if (mScale == 0) {
241            mScale = res.getDisplayMetrics().density;
242            CONTROLS_ANIMATE_WIDTH *= mScale;
243        }
244        mHideString = res.getString(R.string.hide_controls);
245        mShowString = res.getString(R.string.show_controls);
246        mControlsParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
247
248        mIsMultipane = Utils.isMultiPaneConfiguration(this);
249        mIsTabletConfig = Utils.getConfigBool(this, R.bool.tablet_config);
250        mShowAgendaWithMonth = Utils.getConfigBool(this, R.bool.show_agenda_with_month);
251        mShowCalendarControls = Utils.getConfigBool(this, R.bool.show_calendar_controls);
252        mShowEventDetailsWithAgenda =
253            Utils.getConfigBool(this, R.bool.show_event_details_with_agenda);
254        mShowEventInfoFullScreen =
255            Utils.getConfigBool(this, R.bool.show_event_info_full_screen);
256
257        Utils.setAllowWeekForDetailView(mIsMultipane);
258
259        // setContentView must be called before configureActionBar
260        setContentView(R.layout.all_in_one);
261
262        if (mIsTabletConfig) {
263            mDateRange = (TextView) findViewById(R.id.date_bar);
264            mWeekTextView = (TextView) findViewById(R.id.week_num);
265        } else {
266            mDateRange = (TextView) getLayoutInflater().inflate(R.layout.date_range_title, null);
267        }
268
269        // configureActionBar auto-selects the first tab you add, so we need to
270        // call it before we set up our own fragments to make sure it doesn't
271        // overwrite us
272        configureActionBar(viewType);
273
274        mHomeTime = (TextView) findViewById(R.id.home_time);
275        mMiniMonth = findViewById(R.id.mini_month);
276        mCalendarsList = findViewById(R.id.calendar_list);
277        mMiniMonthContainer = findViewById(R.id.mini_month_container);
278        mSecondaryPane = findViewById(R.id.secondary_pane);
279
280        // Must register as the first activity because this activity can modify
281        // the list of event handlers in it's handle method. This affects who
282        // the rest of the handlers the controller dispatches to are.
283        mController.registerFirstEventHandler(HANDLER_KEY, this);
284
285        initFragments(timeMillis, viewType, icicle);
286
287        // Listen for changes that would require this to be refreshed
288        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(this);
289        prefs.registerOnSharedPreferenceChangeListener(this);
290
291        mContentResolver = getContentResolver();
292    }
293
294    private long parseViewAction(final Intent intent) {
295        long timeMillis = -1;
296        Uri data = intent.getData();
297        if (data != null && data.isHierarchical()) {
298            List<String> path = data.getPathSegments();
299            if (path.size() == 2 && path.get(0).equals("events")) {
300                try {
301                    mViewEventId = Long.valueOf(data.getLastPathSegment());
302                    if (mViewEventId != -1) {
303                        mIntentEventStartMillis = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, 0);
304                        mIntentEventEndMillis = intent.getLongExtra(EXTRA_EVENT_END_TIME, 0);
305                        mIntentAttendeeResponse = intent.getIntExtra(
306                                ATTENDEE_STATUS, CalendarController.ATTENDEE_NO_RESPONSE);
307                        timeMillis = mIntentEventStartMillis;
308                    }
309                } catch (NumberFormatException e) {
310                    // Ignore if mViewEventId can't be parsed
311                }
312            }
313        }
314        return timeMillis;
315    }
316
317    private void configureActionBar(int viewType) {
318        if (mIsTabletConfig) {
319            createTabs();
320        } else {
321            createButtonsSpinner(viewType);
322        }
323        if (mIsMultipane) {
324            mActionBar.setDisplayOptions(
325                    ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
326        } else {
327            mActionBar.setDisplayOptions(0);
328        }
329    }
330
331    private void createTabs() {
332        mActionBar = getActionBar();
333        if (mActionBar == null) {
334            Log.w(TAG, "ActionBar is null.");
335        } else {
336            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
337            mDayTab = mActionBar.newTab();
338            mDayTab.setText(getString(R.string.day_view));
339            mDayTab.setTabListener(this);
340            mActionBar.addTab(mDayTab);
341            mWeekTab = mActionBar.newTab();
342            mWeekTab.setText(getString(R.string.week_view));
343            mWeekTab.setTabListener(this);
344            mActionBar.addTab(mWeekTab);
345            mMonthTab = mActionBar.newTab();
346            mMonthTab.setText(getString(R.string.month_view));
347            mMonthTab.setTabListener(this);
348            mActionBar.addTab(mMonthTab);
349            mAgendaTab = mActionBar.newTab();
350            mAgendaTab.setText(getString(R.string.agenda_view));
351            mAgendaTab.setTabListener(this);
352            mActionBar.addTab(mAgendaTab);
353        }
354    }
355
356    private void createButtonsSpinner(int viewType) {
357        mActionBarMenuSpinnerAdapter = new CalendarViewAdapter (this, viewType);
358        mActionBar = getActionBar();
359        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
360        mActionBar.setListNavigationCallbacks(mActionBarMenuSpinnerAdapter, this);
361        switch (viewType) {
362            case ViewType.AGENDA:
363                mActionBar.setSelectedNavigationItem(BUTTON_AGENDA_INDEX);
364                break;
365            case ViewType.DAY:
366                mActionBar.setSelectedNavigationItem(BUTTON_DAY_INDEX);
367                break;
368            case ViewType.WEEK:
369                mActionBar.setSelectedNavigationItem(BUTTON_WEEK_INDEX);
370                break;
371            case ViewType.MONTH:
372                mActionBar.setSelectedNavigationItem(BUTTON_MONTH_INDEX);
373                break;
374            default:
375                mActionBar.setSelectedNavigationItem(BUTTON_DAY_INDEX);
376                break;
377       }
378    }
379    // Clear buttons used in the agenda view
380    private void clearOptionsMenu() {
381        if (mOptionsMenu == null) {
382            return;
383        }
384        MenuItem cancelItem = mOptionsMenu.findItem(R.id.action_cancel);
385        MenuItem editItem = mOptionsMenu.findItem(R.id.action_edit);
386        if (cancelItem != null) {
387            cancelItem.setVisible(false);
388        }
389        if (editItem != null) {
390            editItem.setVisible(false);
391        }
392    }
393
394    @Override
395    protected void onResume() {
396        super.onResume();
397
398        // Must register as the first activity because this activity can modify
399        // the list of event handlers in it's handle method. This affects who
400        // the rest of the handlers the controller dispatches to are.
401        mController.registerFirstEventHandler(HANDLER_KEY, this);
402
403        mOnSaveInstanceStateCalled = false;
404        mContentResolver.registerContentObserver(CalendarContract.Events.CONTENT_URI,
405                true, mObserver);
406        if (mUpdateOnResume) {
407            initFragments(mController.getTime(), mController.getViewType(), null);
408            mUpdateOnResume = false;
409        }
410        updateSecondaryTitleFields(mController.getTime());
411        // Make sure the drop-down menu will get its date updated at midnight
412        if (mActionBarMenuSpinnerAdapter != null) {
413            mActionBarMenuSpinnerAdapter.refresh(this);
414        }
415
416        if (mControlsMenu != null) {
417            mControlsMenu.setTitle(mHideControls ? mShowString : mHideString);
418        }
419        mPaused = false;
420
421        if (mViewEventId != -1 && mIntentEventStartMillis != -1 && mIntentEventEndMillis != -1) {
422            long currentMillis = System.currentTimeMillis();
423            long selectedTime = -1;
424            if (currentMillis > mIntentEventStartMillis && currentMillis < mIntentEventEndMillis) {
425                selectedTime = currentMillis;
426            }
427            mController.sendEventRelatedEventWithExtra(this, EventType.VIEW_EVENT, mViewEventId,
428                    mIntentEventStartMillis, mIntentEventEndMillis, -1, -1,
429                    mIntentAttendeeResponse, selectedTime);
430            mViewEventId = -1;
431            mIntentEventStartMillis = -1;
432            mIntentEventEndMillis = -1;
433        }
434    }
435
436    @Override
437    protected void onPause() {
438        super.onPause();
439
440        mController.deregisterEventHandler(HANDLER_KEY);
441        mPaused = true;
442        mHomeTime.removeCallbacks(mHomeTimeUpdater);
443        if (mActionBarMenuSpinnerAdapter != null) {
444            mActionBarMenuSpinnerAdapter.onPause();
445        }
446        mContentResolver.unregisterContentObserver(mObserver);
447        if (isFinishing()) {
448            // Stop listening for changes that would require this to be refreshed
449            SharedPreferences prefs = GeneralPreferences.getSharedPreferences(this);
450            prefs.unregisterOnSharedPreferenceChangeListener(this);
451        }
452        // FRAG_TODO save highlighted days of the week;
453        if (mController.getViewType() != ViewType.EDIT) {
454            Utils.setDefaultView(this, mController.getViewType());
455        }
456    }
457
458    @Override
459    protected void onUserLeaveHint() {
460        mController.sendEvent(this, EventType.USER_HOME, null, null, -1, ViewType.CURRENT);
461        super.onUserLeaveHint();
462    }
463
464    @Override
465    public void onSaveInstanceState(Bundle outState) {
466        mOnSaveInstanceStateCalled = true;
467        super.onSaveInstanceState(outState);
468
469        outState.putLong(BUNDLE_KEY_RESTORE_TIME, mController.getTime());
470        if (mCurrentView == ViewType.EDIT) {
471            outState.putInt(BUNDLE_KEY_RESTORE_VIEW, mCurrentView);
472            outState.putLong(BUNDLE_KEY_EVENT_ID, mController.getEventId());
473        }
474    }
475
476    @Override
477    protected void onDestroy() {
478        super.onDestroy();
479
480        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(this);
481        prefs.unregisterOnSharedPreferenceChangeListener(this);
482        CalendarController.removeInstance(this);
483    }
484
485    private void initFragments(long timeMillis, int viewType, Bundle icicle) {
486        if (DEBUG) {
487            Log.d(TAG, "Initializing to " + timeMillis + " for view " + viewType);
488        }
489        FragmentTransaction ft = getFragmentManager().beginTransaction();
490
491        if (mShowCalendarControls) {
492            Fragment miniMonthFrag = new MonthByWeekFragment(timeMillis, true);
493            ft.replace(R.id.mini_month, miniMonthFrag);
494            mController.registerEventHandler(R.id.mini_month, (EventHandler) miniMonthFrag);
495
496            Fragment selectCalendarsFrag = new SelectVisibleCalendarsFragment();
497            ft.replace(R.id.calendar_list, selectCalendarsFrag);
498            mController.registerEventHandler(
499                    R.id.calendar_list, (EventHandler) selectCalendarsFrag);
500        }
501        if (!mShowCalendarControls || viewType == ViewType.EDIT) {
502            mMiniMonth.setVisibility(View.GONE);
503            mCalendarsList.setVisibility(View.GONE);
504        }
505
506        EventInfo info = null;
507        if (viewType == ViewType.EDIT) {
508            mPreviousView = GeneralPreferences.getSharedPreferences(this).getInt(
509                    GeneralPreferences.KEY_START_VIEW, GeneralPreferences.DEFAULT_START_VIEW);
510
511            long eventId = -1;
512            Intent intent = getIntent();
513            Uri data = intent.getData();
514            if (data != null) {
515                try {
516                    eventId = Long.parseLong(data.getLastPathSegment());
517                } catch (NumberFormatException e) {
518                    if (DEBUG) {
519                        Log.d(TAG, "Create new event");
520                    }
521                }
522            } else if (icicle != null && icicle.containsKey(BUNDLE_KEY_EVENT_ID)) {
523                eventId = icicle.getLong(BUNDLE_KEY_EVENT_ID);
524            }
525
526            long begin = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, -1);
527            long end = intent.getLongExtra(EXTRA_EVENT_END_TIME, -1);
528            info = new EventInfo();
529            if (end != -1) {
530                info.endTime = new Time();
531                info.endTime.set(end);
532            }
533            if (begin != -1) {
534                info.startTime = new Time();
535                info.startTime.set(begin);
536            }
537            info.id = eventId;
538            // We set the viewtype so if the user presses back when they are
539            // done editing the controller knows we were in the Edit Event
540            // screen. Likewise for eventId
541            mController.setViewType(viewType);
542            mController.setEventId(eventId);
543        } else {
544            mPreviousView = viewType;
545        }
546
547        setMainPane(ft, R.id.main_pane, viewType, timeMillis, true);
548        ft.commit(); // this needs to be after setMainPane()
549
550        Time t = new Time(mTimeZone);
551        t.set(timeMillis);
552        if (viewType != ViewType.EDIT) {
553            mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
554        }
555    }
556
557    @Override
558    public void onBackPressed() {
559        if (mCurrentView == ViewType.EDIT || mBackToPreviousView) {
560            mController.sendEvent(this, EventType.GO_TO, null, null, -1, mPreviousView);
561        } else {
562            super.onBackPressed();
563        }
564    }
565
566    @Override
567    public boolean onCreateOptionsMenu(Menu menu) {
568        super.onCreateOptionsMenu(menu);
569        mOptionsMenu = menu;
570        getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu);
571
572        mSearchMenu = menu.findItem(R.id.action_search);
573        mSearchView = (SearchView) mSearchMenu.getActionView();
574        if (mSearchView != null) {
575            Utils.setUpSearchView(mSearchView, this);
576            mSearchView.setOnQueryTextListener(this);
577            mSearchView.setOnSuggestionListener(this);
578        }
579
580        // Hide the "show/hide controls" button if this is a phone
581        // or the view type is "Month" or "Agenda".
582
583        mControlsMenu = menu.findItem(R.id.action_hide_controls);
584        if (!mShowCalendarControls) {
585            if (mControlsMenu != null) {
586                mControlsMenu.setVisible(false);
587                mControlsMenu.setEnabled(false);
588            }
589        } else if (mControlsMenu != null && mController != null
590                    && (mController.getViewType() == ViewType.MONTH ||
591                        mController.getViewType() == ViewType.AGENDA)) {
592            mControlsMenu.setVisible(false);
593            mControlsMenu.setEnabled(false);
594        } else if (mControlsMenu != null){
595            mControlsMenu.setTitle(mHideControls ? mShowString : mHideString);
596        }
597        return true;
598    }
599
600    @Override
601    public boolean onOptionsItemSelected(MenuItem item) {
602        Time t = null;
603        int viewType = ViewType.CURRENT;
604        switch (item.getItemId()) {
605            case R.id.action_refresh:
606                mController.refreshCalendars();
607                return true;
608            case R.id.action_today:
609                viewType = ViewType.CURRENT;
610                t = new Time(mTimeZone);
611                t.setToNow();
612                break;
613            case R.id.action_create_event:
614                t = new Time();
615                t.set(mController.getTime());
616                if (t.minute >= 30) {
617                    t.hour++;
618                    t.minute = 0;
619                } else {
620                    t.minute = 30;
621                }
622                mController.sendEventRelatedEvent(
623                        this, EventType.CREATE_EVENT, -1, t.toMillis(true), 0, 0, 0, -1);
624                return true;
625            case R.id.action_select_visible_calendars:
626                mController.sendEvent(this, EventType.LAUNCH_SELECT_VISIBLE_CALENDARS, null, null,
627                        0, 0);
628                return true;
629            case R.id.action_settings:
630                mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, 0, 0);
631                return true;
632            case R.id.action_hide_controls:
633                mHideControls = !mHideControls;
634                item.setTitle(mHideControls ? mShowString : mHideString);
635                final ObjectAnimator slideAnimation = ObjectAnimator.ofInt(this, "controlsOffset",
636                        mHideControls ? 0 : CONTROLS_ANIMATE_WIDTH,
637                        mHideControls ? CONTROLS_ANIMATE_WIDTH : 0);
638                slideAnimation.setDuration(CONTROLS_ANIMATE_DURATION);
639                ObjectAnimator.setFrameDelay(0);
640                slideAnimation.start();
641                return true;
642            case R.id.action_search:
643                return false;
644            default:
645                return false;
646        }
647        mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
648        return true;
649    }
650
651    /**
652     * Sets the offset of the controls on the right for animating them off/on
653     * screen. ProGuard strips this if it's not in proguard.flags
654     *
655     * @param controlsOffset The current offset in pixels
656     */
657    public void setControlsOffset(int controlsOffset) {
658        mMiniMonth.setTranslationX(controlsOffset);
659        mCalendarsList.setTranslationX(controlsOffset);
660        mControlsParams.width = Math.max(0, CONTROLS_ANIMATE_WIDTH - controlsOffset);
661        mMiniMonthContainer.setLayoutParams(mControlsParams);
662    }
663
664    @Override
665    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
666        if (key.equals(GeneralPreferences.KEY_WEEK_START_DAY)) {
667            if (mPaused) {
668                mUpdateOnResume = true;
669            } else {
670                initFragments(mController.getTime(), mController.getViewType(), null);
671            }
672        }
673    }
674
675    private void setMainPane(
676            FragmentTransaction ft, int viewId, int viewType, long timeMillis, boolean force) {
677        if (mOnSaveInstanceStateCalled) {
678            return;
679        }
680        if (!force && mCurrentView == viewType) {
681            return;
682        }
683
684        // Remove this when transition to and from month view looks fine.
685        boolean doTransition = viewType != ViewType.MONTH && mCurrentView != ViewType.MONTH;
686        FragmentManager fragmentManager = getFragmentManager();
687        // Check if our previous view was an Agenda view
688        // TODO remove this if framework ever supports nested fragments
689        if (mCurrentView == ViewType.AGENDA) {
690            // If it was, we need to do some cleanup on it to prevent the
691            // edit/delete buttons from coming back on a rotation.
692            Fragment oldFrag = fragmentManager.findFragmentById(viewId);
693            if (oldFrag instanceof AgendaFragment) {
694                ((AgendaFragment) oldFrag).removeFragments(fragmentManager);
695            }
696        }
697
698        if (viewType != mCurrentView) {
699            // The rules for this previous view are different than the
700            // controller's and are used for intercepting the back button.
701            if (mCurrentView != ViewType.EDIT && mCurrentView > 0) {
702                mPreviousView = mCurrentView;
703            }
704            mCurrentView = viewType;
705        }
706        // Create new fragment
707        Fragment frag = null;
708        Fragment secFrag = null;
709        switch (viewType) {
710            case ViewType.AGENDA:
711                if (mActionBar != null && (mActionBar.getSelectedTab() != mAgendaTab)) {
712                    mActionBar.selectTab(mAgendaTab);
713                }
714                if (mActionBarMenuSpinnerAdapter != null) {
715                    mActionBar.setSelectedNavigationItem(CalendarViewAdapter.AGENDA_BUTTON_INDEX);
716                }
717                frag = new AgendaFragment(timeMillis, false);
718                break;
719            case ViewType.DAY:
720                if (mActionBar != null && (mActionBar.getSelectedTab() != mDayTab)) {
721                    mActionBar.selectTab(mDayTab);
722                }
723                if (mActionBarMenuSpinnerAdapter != null) {
724                    mActionBar.setSelectedNavigationItem(CalendarViewAdapter.DAY_BUTTON_INDEX);
725                }
726                frag = new DayFragment(timeMillis, 1);
727                break;
728            case ViewType.WEEK:
729                if (mActionBar != null && (mActionBar.getSelectedTab() != mWeekTab)) {
730                    mActionBar.selectTab(mWeekTab);
731                }
732                if (mActionBarMenuSpinnerAdapter != null) {
733                    mActionBar.setSelectedNavigationItem(CalendarViewAdapter.WEEK_BUTTON_INDEX);
734                }
735                frag = new DayFragment(timeMillis, 7);
736                break;
737            case ViewType.MONTH:
738                if (mActionBar != null && (mActionBar.getSelectedTab() != mMonthTab)) {
739                    mActionBar.selectTab(mMonthTab);
740                }
741                if (mActionBarMenuSpinnerAdapter != null) {
742                    mActionBar.setSelectedNavigationItem(CalendarViewAdapter.MONTH_BUTTON_INDEX);
743                }
744                frag = new MonthByWeekFragment(timeMillis, false);
745                if (mShowAgendaWithMonth) {
746                    secFrag = new AgendaFragment(timeMillis, false);
747                }
748                break;
749            default:
750                throw new IllegalArgumentException(
751                        "Must be Agenda, Day, Week, or Month ViewType, not " + viewType);
752        }
753
754        // Update the current view so that the menu can update its look according to the
755        // current view.
756        if (!mIsTabletConfig && mActionBarMenuSpinnerAdapter != null) {
757            mActionBarMenuSpinnerAdapter.setMainView(viewType);
758        }
759
760
761        // Show date only on tablet configurations in views different than Agenda
762        if (!mIsTabletConfig) {
763            mDateRange.setVisibility(View.GONE);
764        } else if (viewType != ViewType.AGENDA) {
765            mDateRange.setVisibility(View.VISIBLE);
766        } else {
767            mDateRange.setVisibility(View.GONE);
768        }
769
770        // Clear unnecessary buttons from the option menu when switching from the agenda view
771        if (viewType != ViewType.AGENDA) {
772            clearOptionsMenu();
773        }
774
775        boolean doCommit = false;
776        if (ft == null) {
777            doCommit = true;
778            ft = fragmentManager.beginTransaction();
779        }
780
781        if (doTransition) {
782            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
783        }
784
785        ft.replace(viewId, frag);
786        if (mShowAgendaWithMonth) {
787
788            // Show/hide secondary fragment
789
790            if (secFrag != null) {
791                ft.replace(R.id.secondary_pane, secFrag);
792                mSecondaryPane.setVisibility(View.VISIBLE);
793            } else {
794                mSecondaryPane.setVisibility(View.GONE);
795                Fragment f = fragmentManager.findFragmentById(R.id.secondary_pane);
796                if (f != null) {
797                    ft.remove(f);
798                }
799                mController.deregisterEventHandler(R.id.secondary_pane);
800            }
801        }
802        if (DEBUG) {
803            Log.d(TAG, "Adding handler with viewId " + viewId + " and type " + viewType);
804        }
805        // If the key is already registered this will replace it
806        mController.registerEventHandler(viewId, (EventHandler) frag);
807        if (secFrag != null) {
808            mController.registerEventHandler(viewId, (EventHandler) secFrag);
809        }
810
811        if (doCommit) {
812            Log.d(TAG, "setMainPane AllInOne=" + this + " finishing:" + this.isFinishing());
813            ft.commit();
814        }
815    }
816
817    private void setTitleInActionBar(EventInfo event) {
818        if (event.eventType != EventType.UPDATE_TITLE || mActionBar == null) {
819            return;
820        }
821
822        final long start = event.startTime.toMillis(false /* use isDst */);
823        final long end;
824        if (event.endTime != null) {
825            end = event.endTime.toMillis(false /* use isDst */);
826        } else {
827            end = start;
828        }
829
830        final String msg = Utils.formatDateRange(this, start, end, (int) event.extraLong);
831        CharSequence oldDate = mDateRange.getText();
832        mDateRange.setText(msg);
833        updateSecondaryTitleFields(start);
834        if (!TextUtils.equals(oldDate, msg)) {
835            mDateRange.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
836            if (mShowWeekNum && mWeekTextView != null) {
837                mWeekTextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
838            }
839        }
840    }
841
842    private void updateSecondaryTitleFields(long visibleMillisSinceEpoch) {
843        mShowWeekNum = Utils.getShowWeekNumber(this);
844        mTimeZone = Utils.getTimeZone(this, mHomeTimeUpdater);
845        if (visibleMillisSinceEpoch != -1) {
846            int weekNum = Utils.getWeekNumberFromTime(visibleMillisSinceEpoch, this);
847            mWeekNum = weekNum;
848        }
849
850        if (mShowWeekNum && (mCurrentView == ViewType.WEEK) && mIsTabletConfig
851                && mWeekTextView != null) {
852            String weekString = getResources().getQuantityString(R.plurals.weekN, mWeekNum,
853                    mWeekNum);
854            mWeekTextView.setText(weekString);
855            mWeekTextView.setVisibility(View.VISIBLE);
856        } else if (visibleMillisSinceEpoch != -1 && mWeekTextView != null
857                && mCurrentView == ViewType.DAY && mIsTabletConfig) {
858            Time time = new Time(mTimeZone);
859            time.set(visibleMillisSinceEpoch);
860            int julianDay = Time.getJulianDay(visibleMillisSinceEpoch, time.gmtoff);
861            time.setToNow();
862            int todayJulianDay = Time.getJulianDay(time.toMillis(false), time.gmtoff);
863            String dayString = Utils.getDayOfWeekString(julianDay, todayJulianDay,
864                    visibleMillisSinceEpoch, this);
865            mWeekTextView.setText(dayString);
866            mWeekTextView.setVisibility(View.VISIBLE);
867        } else if (mWeekTextView != null && (!mIsTabletConfig || mCurrentView != ViewType.DAY)) {
868            mWeekTextView.setVisibility(View.GONE);
869        }
870
871        if (mHomeTime != null
872                && (mCurrentView == ViewType.DAY || mCurrentView == ViewType.WEEK
873                        || mCurrentView == ViewType.AGENDA)
874                && !TextUtils.equals(mTimeZone, Time.getCurrentTimezone())) {
875            Time time = new Time(mTimeZone);
876            time.setToNow();
877            long millis = time.toMillis(true);
878            boolean isDST = time.isDst != 0;
879            int flags = DateUtils.FORMAT_SHOW_TIME;
880            if (DateFormat.is24HourFormat(this)) {
881                flags |= DateUtils.FORMAT_24HOUR;
882            }
883            // Formats the time as
884            String timeString = (new StringBuilder(
885                    Utils.formatDateRange(this, millis, millis, flags))).append(" ").append(
886                    TimeZone.getTimeZone(mTimeZone).getDisplayName(
887                            isDST, TimeZone.SHORT, Locale.getDefault())).toString();
888            mHomeTime.setText(timeString);
889            mHomeTime.setVisibility(View.VISIBLE);
890            // Update when the minute changes
891            mHomeTime.postDelayed(
892                    mHomeTimeUpdater,
893                    DateUtils.MINUTE_IN_MILLIS - (millis % DateUtils.MINUTE_IN_MILLIS));
894        } else if (mHomeTime != null) {
895            mHomeTime.setVisibility(View.GONE);
896        }
897    }
898
899    @Override
900    public long getSupportedEventTypes() {
901        return EventType.GO_TO | EventType.VIEW_EVENT | EventType.UPDATE_TITLE;
902    }
903
904    @Override
905    public void handleEvent(EventInfo event) {
906        long displayTime = -1;
907        if (event.eventType == EventType.GO_TO) {
908            if ((event.extraLong & CalendarController.EXTRA_GOTO_BACK_TO_PREVIOUS) != 0) {
909                mBackToPreviousView = true;
910            } else if (event.viewType != mController.getPreviousViewType()
911                    && event.viewType != ViewType.EDIT) {
912                // Clear the flag is change to a different view type
913                mBackToPreviousView = false;
914            }
915
916            setMainPane(
917                    null, R.id.main_pane, event.viewType, event.startTime.toMillis(false), false);
918            if (mSearchView != null) {
919                mSearchView.clearFocus();
920            }
921
922            if (mShowCalendarControls) {
923                if (event.viewType == ViewType.MONTH || event.viewType == ViewType.AGENDA) {
924                    // hide minimonth and calendar frag
925                    mShowSideViews = false;
926                    if (mControlsMenu != null) {
927                        mControlsMenu.setVisible(false);
928                        mControlsMenu.setEnabled(false);
929
930                        if (!mHideControls) {
931                            final ObjectAnimator slideAnimation = ObjectAnimator.ofInt(this,
932                                    "controlsOffset", 0, CONTROLS_ANIMATE_WIDTH);
933                            slideAnimation.addListener(mSlideAnimationDoneListener);
934                            slideAnimation.setDuration(220);
935                            ObjectAnimator.setFrameDelay(0);
936                            slideAnimation.start();
937                        }
938                    } else {
939                        mMiniMonth.setVisibility(View.GONE);
940                        mCalendarsList.setVisibility(View.GONE);
941                        mMiniMonthContainer.setVisibility(View.GONE);
942                    }
943                } else {
944                    // show minimonth and calendar frag
945                    mShowSideViews = true;
946                    mMiniMonth.setVisibility(View.VISIBLE);
947                    mCalendarsList.setVisibility(View.VISIBLE);
948                    mMiniMonthContainer.setVisibility(View.VISIBLE);
949                    if (mControlsMenu != null) {
950                        mControlsMenu.setVisible(true);
951                        mControlsMenu.setEnabled(true);
952                        if (!mHideControls &&
953                                (mController.getPreviousViewType() == ViewType.MONTH ||
954                                 mController.getPreviousViewType() == ViewType.AGENDA)) {
955                            final ObjectAnimator slideAnimation = ObjectAnimator.ofInt(this,
956                                    "controlsOffset", CONTROLS_ANIMATE_WIDTH, 0);
957                            slideAnimation.setDuration(220);
958                            ObjectAnimator.setFrameDelay(0);
959                            slideAnimation.start();
960                        }
961                    }
962                }
963            }
964            displayTime = event.startTime.toMillis(true);
965            if (!mIsTabletConfig) {
966                mActionBarMenuSpinnerAdapter.setTime(displayTime);
967            }
968        } else if (event.eventType == EventType.VIEW_EVENT) {
969
970            // If in Agenda view and "show_event_details_with_agenda" is "true",
971            // do not create the event info fragment here, it will be created by the Agenda
972            // fragment
973
974            if (mCurrentView == ViewType.AGENDA && mShowEventDetailsWithAgenda) {
975                if (event.startTime != null && event.endTime != null) {
976                    mController.sendEvent(this, EventType.GO_TO, event.startTime, event.endTime,
977                            event.id, ViewType.AGENDA);
978                } else if (event.selectedTime != null) {
979                    mController.sendEvent(this, EventType.GO_TO, event.selectedTime,
980                        event.selectedTime, event.id, ViewType.AGENDA);
981                }
982            } else {
983                if (mShowEventInfoFullScreen) {
984                    // start event info as activity
985                    Intent intent = new Intent(Intent.ACTION_VIEW);
986                    Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, event.id);
987                    intent.setData(eventUri);
988                    intent.setClass(this, EventInfoActivity.class);
989                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
990                            Intent.FLAG_ACTIVITY_SINGLE_TOP);
991                    intent.putExtra(EXTRA_EVENT_BEGIN_TIME, event.startTime.toMillis(false));
992                    intent.putExtra(EXTRA_EVENT_END_TIME, event.endTime.toMillis(false));
993                    intent.putExtra(EVENT_ATTENDEE_RESPONSE, (int)event.extraLong);
994                    startActivity(intent);
995                } else {
996                    // start event info as a dialog
997                    EventInfoFragment fragment = new EventInfoFragment(this,
998                            event.id, event.startTime.toMillis(false),
999                            event.endTime.toMillis(false), (int) event.extraLong, true);
1000                    // TODO Fix the temp hack below: && mCurrentView !=
1001                    // ViewType.AGENDA
1002                    if (event.selectedTime != null && mCurrentView != ViewType.AGENDA) {
1003                        mController.sendEvent(this, EventType.GO_TO, event.selectedTime,
1004                                event.selectedTime, -1, ViewType.DETAIL);
1005                    }
1006                    fragment.setDialogParams(event.x, event.y);
1007                    FragmentManager fm = getFragmentManager();
1008                    FragmentTransaction ft = fm.beginTransaction();
1009                    // if we have an old popup replace it
1010                    Fragment fOld = fm.findFragmentByTag(EVENT_INFO_FRAGMENT_TAG);
1011                    int oldId;
1012                    if (fOld != null && fOld.isAdded() && (oldId = fOld.getId()) != 0) {
1013                        ft.replace(oldId, fragment, EVENT_INFO_FRAGMENT_TAG);
1014                    } else {
1015                        ft.add(fragment, EVENT_INFO_FRAGMENT_TAG);
1016                    }
1017                    ft.commit();
1018                }
1019            }
1020            displayTime = event.startTime.toMillis(true);
1021        } else if (event.eventType == EventType.UPDATE_TITLE) {
1022            setTitleInActionBar(event);
1023            if (!mIsTabletConfig) {
1024                mActionBarMenuSpinnerAdapter.setTime(event.startTime.toMillis(false));
1025            }
1026        }
1027        updateSecondaryTitleFields(displayTime);
1028    }
1029
1030    // Needs to be in proguard whitelist
1031    // Specified as listener via android:onClick in a layout xml
1032    public void handleSelectSyncedCalendarsClicked(View v) {
1033        mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, null, 0, 0,
1034                CalendarController.EXTRA_GOTO_TIME, null,
1035                null);
1036    }
1037
1038    @Override
1039    public void eventsChanged() {
1040        mController.sendEvent(this, EventType.EVENTS_CHANGED, null, null, -1, ViewType.CURRENT);
1041    }
1042
1043    @Override
1044    public boolean onQueryTextChange(String newText) {
1045        return false;
1046    }
1047
1048    @Override
1049    public boolean onQueryTextSubmit(String query) {
1050        if ("TARDIS".equalsIgnoreCase(query)) {
1051            Utils.tardis();
1052        }
1053        mSearchMenu.collapseActionView();
1054        mController.sendEvent(this, EventType.SEARCH, null, null, -1, ViewType.CURRENT, 0, query,
1055                getComponentName());
1056        return true;
1057    }
1058
1059    @Override
1060    public void onTabSelected(Tab tab, FragmentTransaction ft) {
1061        Log.w(TAG, "TabSelected AllInOne=" + this + " finishing:" + this.isFinishing());
1062        if (tab == mDayTab && mCurrentView != ViewType.DAY) {
1063            mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.DAY);
1064        } else if (tab == mWeekTab && mCurrentView != ViewType.WEEK) {
1065            mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.WEEK);
1066        } else if (tab == mMonthTab && mCurrentView != ViewType.MONTH) {
1067            mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.MONTH);
1068        } else if (tab == mAgendaTab && mCurrentView != ViewType.AGENDA) {
1069            mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.AGENDA);
1070        } else {
1071            Log.w(TAG, "TabSelected event from unknown tab: "
1072                    + (tab == null ? "null" : tab.getText()));
1073            Log.w(TAG, "CurrentView:" + mCurrentView + " Tab:" + tab.toString() + " Day:" + mDayTab
1074                    + " Week:" + mWeekTab + " Month:" + mMonthTab + " Agenda:" + mAgendaTab);
1075        }
1076    }
1077
1078    @Override
1079    public void onTabReselected(Tab tab, FragmentTransaction ft) {
1080    }
1081
1082    @Override
1083    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
1084    }
1085
1086
1087    @Override
1088    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
1089        switch (itemPosition) {
1090            case CalendarViewAdapter.DAY_BUTTON_INDEX:
1091                if (mCurrentView != ViewType.DAY) {
1092                    mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.DAY);
1093                }
1094                break;
1095            case CalendarViewAdapter.WEEK_BUTTON_INDEX:
1096                if (mCurrentView != ViewType.WEEK) {
1097                    mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.WEEK);
1098                }
1099                break;
1100            case CalendarViewAdapter.MONTH_BUTTON_INDEX:
1101                if (mCurrentView != ViewType.MONTH) {
1102                    mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.MONTH);
1103                }
1104                break;
1105            case CalendarViewAdapter.AGENDA_BUTTON_INDEX:
1106                if (mCurrentView != ViewType.AGENDA) {
1107                    mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.AGENDA);
1108                }
1109                break;
1110            default:
1111                Log.w(TAG, "ItemSelected event from unknown button: " + itemPosition);
1112                Log.w(TAG, "CurrentView:" + mCurrentView + " Button:" + itemPosition +
1113                        " Day:" + mDayTab + " Week:" + mWeekTab + " Month:" + mMonthTab +
1114                        " Agenda:" + mAgendaTab);
1115                break;
1116        }
1117        return false;
1118    }
1119
1120    @Override
1121    public boolean onSuggestionSelect(int position) {
1122        return false;
1123    }
1124
1125    @Override
1126    public boolean onSuggestionClick(int position) {
1127        mSearchMenu.collapseActionView();
1128        return false;
1129    }
1130
1131    @Override
1132    public boolean onSearchRequested() {
1133        if (mSearchMenu != null) {
1134            mSearchMenu.expandActionView();
1135        }
1136        return false;
1137    }
1138}
1139