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