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