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