AllInOneActivity.java revision 666123ec1274420a77ace01e32df70baf87fca57
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_ALL_DAY;
20import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME;
21import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME;
22import static android.provider.CalendarContract.Attendees.ATTENDEE_STATUS;
23
24import android.accounts.AccountManager;
25import android.accounts.AccountManagerCallback;
26import android.accounts.AccountManagerFuture;
27import android.accounts.AuthenticatorException;
28import android.accounts.OperationCanceledException;
29import android.animation.Animator;
30import android.animation.Animator.AnimatorListener;
31import android.animation.ObjectAnimator;
32import android.app.ActionBar;
33import android.app.ActionBar.Tab;
34import android.app.Fragment;
35import android.app.FragmentManager;
36import android.app.FragmentTransaction;
37import android.content.AsyncQueryHandler;
38import android.content.BroadcastReceiver;
39import android.content.ContentResolver;
40import android.content.ContentUris;
41import android.content.Intent;
42import android.content.SharedPreferences;
43import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
44import android.content.res.Configuration;
45import android.content.res.Resources;
46import android.database.ContentObserver;
47import android.database.Cursor;
48import android.graphics.drawable.LayerDrawable;
49import android.net.Uri;
50import android.os.Bundle;
51import android.os.Handler;
52import android.provider.CalendarContract;
53import android.provider.CalendarContract.Attendees;
54import android.provider.CalendarContract.Calendars;
55import android.provider.CalendarContract.Events;
56import android.text.TextUtils;
57import android.text.format.DateFormat;
58import android.text.format.DateUtils;
59import android.text.format.Time;
60import android.util.Log;
61import android.view.Menu;
62import android.view.MenuItem;
63import android.view.View;
64import android.view.accessibility.AccessibilityEvent;
65import android.widget.LinearLayout;
66import android.widget.RelativeLayout;
67import android.widget.RelativeLayout.LayoutParams;
68import android.widget.SearchView;
69import android.widget.SearchView.OnSuggestionListener;
70import android.widget.TextView;
71
72import com.android.calendar.CalendarController.EventHandler;
73import com.android.calendar.CalendarController.EventInfo;
74import com.android.calendar.CalendarController.EventType;
75import com.android.calendar.CalendarController.ViewType;
76import com.android.calendar.agenda.AgendaFragment;
77import com.android.calendar.month.MonthByWeekFragment;
78import com.android.calendar.selectcalendars.SelectVisibleCalendarsFragment;
79
80import java.io.IOException;
81import java.util.List;
82import java.util.Locale;
83import java.util.TimeZone;
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        // Must register as the first activity because this activity can modify
511        // the list of event handlers in it's handle method. This affects who
512        // the rest of the handlers the controller dispatches to are.
513        mController.registerFirstEventHandler(HANDLER_KEY, this);
514
515        mOnSaveInstanceStateCalled = false;
516        mContentResolver.registerContentObserver(CalendarContract.Events.CONTENT_URI,
517                true, mObserver);
518        if (mUpdateOnResume) {
519            initFragments(mController.getTime(), mController.getViewType(), null);
520            mUpdateOnResume = false;
521        }
522        Time t = new Time(mTimeZone);
523        t.set(mController.getTime());
524        mController.sendEvent(this, EventType.UPDATE_TITLE, t, t, -1, ViewType.CURRENT,
525                mController.getDateFlags(), null, null);
526        // Make sure the drop-down menu will get its date updated at midnight
527        if (mActionBarMenuSpinnerAdapter != null) {
528            mActionBarMenuSpinnerAdapter.refresh(this);
529        }
530
531        if (mControlsMenu != null) {
532            mControlsMenu.setTitle(mHideControls ? mShowString : mHideString);
533        }
534        mPaused = false;
535
536        if (mViewEventId != -1 && mIntentEventStartMillis != -1 && mIntentEventEndMillis != -1) {
537            long currentMillis = System.currentTimeMillis();
538            long selectedTime = -1;
539            if (currentMillis > mIntentEventStartMillis && currentMillis < mIntentEventEndMillis) {
540                selectedTime = currentMillis;
541            }
542            mController.sendEventRelatedEventWithExtra(this, EventType.VIEW_EVENT, mViewEventId,
543                    mIntentEventStartMillis, mIntentEventEndMillis, -1, -1,
544                    EventInfo.buildViewExtraLong(mIntentAttendeeResponse,mIntentAllDay),
545                    selectedTime);
546            mViewEventId = -1;
547            mIntentEventStartMillis = -1;
548            mIntentEventEndMillis = -1;
549            mIntentAllDay = false;
550        }
551        Utils.setMidnightUpdater(mHandler, mTimeChangesUpdater, mTimeZone);
552        // Make sure the today icon is up to date
553        invalidateOptionsMenu();
554
555        mCalIntentReceiver = Utils.setTimeChangesReceiver(this, mTimeChangesUpdater);
556    }
557
558    @Override
559    protected void onPause() {
560        super.onPause();
561
562        mController.deregisterEventHandler(HANDLER_KEY);
563        mPaused = true;
564        mHomeTime.removeCallbacks(mHomeTimeUpdater);
565        if (mActionBarMenuSpinnerAdapter != null) {
566            mActionBarMenuSpinnerAdapter.onPause();
567        }
568        mContentResolver.unregisterContentObserver(mObserver);
569        if (isFinishing()) {
570            // Stop listening for changes that would require this to be refreshed
571            SharedPreferences prefs = GeneralPreferences.getSharedPreferences(this);
572            prefs.unregisterOnSharedPreferenceChangeListener(this);
573        }
574        // FRAG_TODO save highlighted days of the week;
575        if (mController.getViewType() != ViewType.EDIT) {
576            Utils.setDefaultView(this, mController.getViewType());
577        }
578        Utils.resetMidnightUpdater(mHandler, mTimeChangesUpdater);
579        Utils.clearTimeChangesReceiver(this, mCalIntentReceiver);
580    }
581
582    @Override
583    protected void onUserLeaveHint() {
584        mController.sendEvent(this, EventType.USER_HOME, null, null, -1, ViewType.CURRENT);
585        super.onUserLeaveHint();
586    }
587
588    @Override
589    public void onSaveInstanceState(Bundle outState) {
590        mOnSaveInstanceStateCalled = true;
591        super.onSaveInstanceState(outState);
592        outState.putLong(BUNDLE_KEY_RESTORE_TIME, mController.getTime());
593        outState.putInt(BUNDLE_KEY_RESTORE_VIEW, mCurrentView);
594        if (mCurrentView == ViewType.EDIT) {
595            outState.putLong(BUNDLE_KEY_EVENT_ID, mController.getEventId());
596        } else if (mCurrentView == ViewType.AGENDA) {
597            FragmentManager fm = getFragmentManager();
598            Fragment f = fm.findFragmentById(R.id.main_pane);
599            if (f instanceof AgendaFragment) {
600                outState.putLong(BUNDLE_KEY_EVENT_ID, ((AgendaFragment)f).getLastShowEventId());
601            }
602        }
603        outState.putBoolean(BUNDLE_KEY_CHECK_ACCOUNTS, mCheckForAccounts);
604    }
605
606    @Override
607    protected void onDestroy() {
608        super.onDestroy();
609
610        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(this);
611        prefs.unregisterOnSharedPreferenceChangeListener(this);
612
613        mController.deregisterAllEventHandlers();
614
615        CalendarController.removeInstance(this);
616    }
617
618    private void initFragments(long timeMillis, int viewType, Bundle icicle) {
619        if (DEBUG) {
620            Log.d(TAG, "Initializing to " + timeMillis + " for view " + viewType);
621        }
622        FragmentTransaction ft = getFragmentManager().beginTransaction();
623
624        if (mShowCalendarControls) {
625            Fragment miniMonthFrag = new MonthByWeekFragment(timeMillis, true);
626            ft.replace(R.id.mini_month, miniMonthFrag);
627            mController.registerEventHandler(R.id.mini_month, (EventHandler) miniMonthFrag);
628
629            Fragment selectCalendarsFrag = new SelectVisibleCalendarsFragment();
630            ft.replace(R.id.calendar_list, selectCalendarsFrag);
631            mController.registerEventHandler(
632                    R.id.calendar_list, (EventHandler) selectCalendarsFrag);
633        }
634        if (!mShowCalendarControls || viewType == ViewType.EDIT) {
635            mMiniMonth.setVisibility(View.GONE);
636            mCalendarsList.setVisibility(View.GONE);
637        }
638
639        EventInfo info = null;
640        if (viewType == ViewType.EDIT) {
641            mPreviousView = GeneralPreferences.getSharedPreferences(this).getInt(
642                    GeneralPreferences.KEY_START_VIEW, GeneralPreferences.DEFAULT_START_VIEW);
643
644            long eventId = -1;
645            Intent intent = getIntent();
646            Uri data = intent.getData();
647            if (data != null) {
648                try {
649                    eventId = Long.parseLong(data.getLastPathSegment());
650                } catch (NumberFormatException e) {
651                    if (DEBUG) {
652                        Log.d(TAG, "Create new event");
653                    }
654                }
655            } else if (icicle != null && icicle.containsKey(BUNDLE_KEY_EVENT_ID)) {
656                eventId = icicle.getLong(BUNDLE_KEY_EVENT_ID);
657            }
658
659            long begin = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, -1);
660            long end = intent.getLongExtra(EXTRA_EVENT_END_TIME, -1);
661            info = new EventInfo();
662            if (end != -1) {
663                info.endTime = new Time();
664                info.endTime.set(end);
665            }
666            if (begin != -1) {
667                info.startTime = new Time();
668                info.startTime.set(begin);
669            }
670            info.id = eventId;
671            // We set the viewtype so if the user presses back when they are
672            // done editing the controller knows we were in the Edit Event
673            // screen. Likewise for eventId
674            mController.setViewType(viewType);
675            mController.setEventId(eventId);
676        } else {
677            mPreviousView = viewType;
678        }
679
680        setMainPane(ft, R.id.main_pane, viewType, timeMillis, true);
681        ft.commit(); // this needs to be after setMainPane()
682
683        Time t = new Time(mTimeZone);
684        t.set(timeMillis);
685        if (viewType == ViewType.AGENDA && icicle != null) {
686            mController.sendEvent(this, EventType.GO_TO, t, null,
687                    icicle.getLong(BUNDLE_KEY_EVENT_ID, -1), viewType);
688        } else if (viewType != ViewType.EDIT) {
689            mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
690        }
691    }
692
693    @Override
694    public void onBackPressed() {
695        if (mCurrentView == ViewType.EDIT || mBackToPreviousView) {
696            mController.sendEvent(this, EventType.GO_TO, null, null, -1, mPreviousView);
697        } else {
698            super.onBackPressed();
699        }
700    }
701
702    @Override
703    public boolean onCreateOptionsMenu(Menu menu) {
704        super.onCreateOptionsMenu(menu);
705        mOptionsMenu = menu;
706        getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu);
707
708        // Add additional options (if any).
709        Integer extensionMenuRes = mExtensions.getExtensionMenuResource(menu);
710        if (extensionMenuRes != null) {
711            getMenuInflater().inflate(extensionMenuRes, menu);
712        }
713
714        mSearchMenu = menu.findItem(R.id.action_search);
715        mSearchView = (SearchView) mSearchMenu.getActionView();
716        if (mSearchView != null) {
717            Utils.setUpSearchView(mSearchView, this);
718            mSearchView.setOnQueryTextListener(this);
719            mSearchView.setOnSuggestionListener(this);
720        }
721
722        // Hide the "show/hide controls" button if this is a phone
723        // or the view type is "Month" or "Agenda".
724
725        mControlsMenu = menu.findItem(R.id.action_hide_controls);
726        if (!mShowCalendarControls) {
727            if (mControlsMenu != null) {
728                mControlsMenu.setVisible(false);
729                mControlsMenu.setEnabled(false);
730            }
731        } else if (mControlsMenu != null && mController != null
732                    && (mController.getViewType() == ViewType.MONTH ||
733                        mController.getViewType() == ViewType.AGENDA)) {
734            mControlsMenu.setVisible(false);
735            mControlsMenu.setEnabled(false);
736        } else if (mControlsMenu != null){
737            mControlsMenu.setTitle(mHideControls ? mShowString : mHideString);
738        }
739
740        MenuItem menuItem = menu.findItem(R.id.action_today);
741        if (Utils.isJellybeanOrLater()) {
742            // replace the default top layer drawable of the today icon with a
743            // custom drawable that shows the day of the month of today
744            LayerDrawable icon = (LayerDrawable) menuItem.getIcon();
745            Utils.setTodayIcon(icon, this, mTimeZone);
746        } else {
747            menuItem.setIcon(R.drawable.ic_menu_today_no_date_holo_light);
748        }
749        return true;
750    }
751
752    @Override
753    public boolean onOptionsItemSelected(MenuItem item) {
754        Time t = null;
755        int viewType = ViewType.CURRENT;
756        long extras = CalendarController.EXTRA_GOTO_TIME;
757        final int itemId = item.getItemId();
758        if (itemId == R.id.action_refresh) {
759            mController.refreshCalendars();
760            return true;
761        } else if (itemId == R.id.action_today) {
762            viewType = ViewType.CURRENT;
763            t = new Time(mTimeZone);
764            t.setToNow();
765            extras |= CalendarController.EXTRA_GOTO_TODAY;
766        } else if (itemId == R.id.action_create_event) {
767            t = new Time();
768            t.set(mController.getTime());
769            if (t.minute > 30) {
770                t.hour++;
771                t.minute = 0;
772            } else if (t.minute > 0 && t.minute < 30) {
773                t.minute = 30;
774            }
775            mController.sendEventRelatedEvent(
776                    this, EventType.CREATE_EVENT, -1, t.toMillis(true), 0, 0, 0, -1);
777            return true;
778        } else if (itemId == R.id.action_select_visible_calendars) {
779            mController.sendEvent(this, EventType.LAUNCH_SELECT_VISIBLE_CALENDARS, null, null,
780                    0, 0);
781            return true;
782        } else if (itemId == R.id.action_settings) {
783            mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, 0, 0);
784            return true;
785        } else if (itemId == R.id.action_hide_controls) {
786            mHideControls = !mHideControls;
787            Utils.setSharedPreference(
788                    this, GeneralPreferences.KEY_SHOW_CONTROLS, !mHideControls);
789            item.setTitle(mHideControls ? mShowString : mHideString);
790            if (!mHideControls) {
791                mMiniMonth.setVisibility(View.VISIBLE);
792                mCalendarsList.setVisibility(View.VISIBLE);
793                mMiniMonthContainer.setVisibility(View.VISIBLE);
794            }
795            final ObjectAnimator slideAnimation = ObjectAnimator.ofInt(this, "controlsOffset",
796                    mHideControls ? 0 : mControlsAnimateWidth,
797                    mHideControls ? mControlsAnimateWidth : 0);
798            slideAnimation.setDuration(mCalendarControlsAnimationTime);
799            ObjectAnimator.setFrameDelay(0);
800            slideAnimation.start();
801            return true;
802        } else if (itemId == R.id.action_search) {
803            return false;
804        } else {
805            return mExtensions.handleItemSelected(item, this);
806        }
807        mController.sendEvent(this, EventType.GO_TO, t, null, t, -1, viewType, extras, null, null);
808        return true;
809    }
810
811    /**
812     * Sets the offset of the controls on the right for animating them off/on
813     * screen. ProGuard strips this if it's not in proguard.flags
814     *
815     * @param controlsOffset The current offset in pixels
816     */
817    public void setControlsOffset(int controlsOffset) {
818        if (mOrientation == Configuration.ORIENTATION_LANDSCAPE) {
819            mMiniMonth.setTranslationX(controlsOffset);
820            mCalendarsList.setTranslationX(controlsOffset);
821            mControlsParams.width = Math.max(0, mControlsAnimateWidth - controlsOffset);
822            mMiniMonthContainer.setLayoutParams(mControlsParams);
823        } else {
824            mMiniMonth.setTranslationY(controlsOffset);
825            mCalendarsList.setTranslationY(controlsOffset);
826            if (mVerticalControlsParams == null) {
827                mVerticalControlsParams = new LinearLayout.LayoutParams(
828                        LinearLayout.LayoutParams.MATCH_PARENT, mControlsAnimateHeight);
829            }
830            mVerticalControlsParams.height = Math.max(0, mControlsAnimateHeight - controlsOffset);
831            mMiniMonthContainer.setLayoutParams(mVerticalControlsParams);
832        }
833    }
834
835    @Override
836    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
837        if (key.equals(GeneralPreferences.KEY_WEEK_START_DAY)) {
838            if (mPaused) {
839                mUpdateOnResume = true;
840            } else {
841                initFragments(mController.getTime(), mController.getViewType(), null);
842            }
843        }
844    }
845
846    private void setMainPane(
847            FragmentTransaction ft, int viewId, int viewType, long timeMillis, boolean force) {
848        if (mOnSaveInstanceStateCalled) {
849            return;
850        }
851        if (!force && mCurrentView == viewType) {
852            return;
853        }
854
855        // Remove this when transition to and from month view looks fine.
856        boolean doTransition = viewType != ViewType.MONTH && mCurrentView != ViewType.MONTH;
857        FragmentManager fragmentManager = getFragmentManager();
858        // Check if our previous view was an Agenda view
859        // TODO remove this if framework ever supports nested fragments
860        if (mCurrentView == ViewType.AGENDA) {
861            // If it was, we need to do some cleanup on it to prevent the
862            // edit/delete buttons from coming back on a rotation.
863            Fragment oldFrag = fragmentManager.findFragmentById(viewId);
864            if (oldFrag instanceof AgendaFragment) {
865                ((AgendaFragment) oldFrag).removeFragments(fragmentManager);
866            }
867        }
868
869        if (viewType != mCurrentView) {
870            // The rules for this previous view are different than the
871            // controller's and are used for intercepting the back button.
872            if (mCurrentView != ViewType.EDIT && mCurrentView > 0) {
873                mPreviousView = mCurrentView;
874            }
875            mCurrentView = viewType;
876        }
877        // Create new fragment
878        Fragment frag = null;
879        Fragment secFrag = null;
880        switch (viewType) {
881            case ViewType.AGENDA:
882                if (mActionBar != null && (mActionBar.getSelectedTab() != mAgendaTab)) {
883                    mActionBar.selectTab(mAgendaTab);
884                }
885                if (mActionBarMenuSpinnerAdapter != null) {
886                    mActionBar.setSelectedNavigationItem(CalendarViewAdapter.AGENDA_BUTTON_INDEX);
887                }
888                frag = new AgendaFragment(timeMillis, false);
889                break;
890            case ViewType.DAY:
891                if (mActionBar != null && (mActionBar.getSelectedTab() != mDayTab)) {
892                    mActionBar.selectTab(mDayTab);
893                }
894                if (mActionBarMenuSpinnerAdapter != null) {
895                    mActionBar.setSelectedNavigationItem(CalendarViewAdapter.DAY_BUTTON_INDEX);
896                }
897                frag = new DayFragment(timeMillis, 1);
898                break;
899            case ViewType.MONTH:
900                if (mActionBar != null && (mActionBar.getSelectedTab() != mMonthTab)) {
901                    mActionBar.selectTab(mMonthTab);
902                }
903                if (mActionBarMenuSpinnerAdapter != null) {
904                    mActionBar.setSelectedNavigationItem(CalendarViewAdapter.MONTH_BUTTON_INDEX);
905                }
906                frag = new MonthByWeekFragment(timeMillis, false);
907                if (mShowAgendaWithMonth) {
908                    secFrag = new AgendaFragment(timeMillis, false);
909                }
910                break;
911            case ViewType.WEEK:
912            default:
913                if (mActionBar != null && (mActionBar.getSelectedTab() != mWeekTab)) {
914                    mActionBar.selectTab(mWeekTab);
915                }
916                if (mActionBarMenuSpinnerAdapter != null) {
917                    mActionBar.setSelectedNavigationItem(CalendarViewAdapter.WEEK_BUTTON_INDEX);
918                }
919                frag = new DayFragment(timeMillis, 7);
920                break;
921        }
922
923        // Update the current view so that the menu can update its look according to the
924        // current view.
925        if (mActionBarMenuSpinnerAdapter != null) {
926            mActionBarMenuSpinnerAdapter.setMainView(viewType);
927            if (!mIsTabletConfig) {
928                mActionBarMenuSpinnerAdapter.setTime(timeMillis);
929            }
930        }
931
932
933        // Show date only on tablet configurations in views different than Agenda
934        if (!mIsTabletConfig) {
935            mDateRange.setVisibility(View.GONE);
936        } else if (viewType != ViewType.AGENDA) {
937            mDateRange.setVisibility(View.VISIBLE);
938        } else {
939            mDateRange.setVisibility(View.GONE);
940        }
941
942        // Clear unnecessary buttons from the option menu when switching from the agenda view
943        if (viewType != ViewType.AGENDA) {
944            clearOptionsMenu();
945        }
946
947        boolean doCommit = false;
948        if (ft == null) {
949            doCommit = true;
950            ft = fragmentManager.beginTransaction();
951        }
952
953        if (doTransition) {
954            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
955        }
956
957        ft.replace(viewId, frag);
958        if (mShowAgendaWithMonth) {
959
960            // Show/hide secondary fragment
961
962            if (secFrag != null) {
963                ft.replace(R.id.secondary_pane, secFrag);
964                mSecondaryPane.setVisibility(View.VISIBLE);
965            } else {
966                mSecondaryPane.setVisibility(View.GONE);
967                Fragment f = fragmentManager.findFragmentById(R.id.secondary_pane);
968                if (f != null) {
969                    ft.remove(f);
970                }
971                mController.deregisterEventHandler(R.id.secondary_pane);
972            }
973        }
974        if (DEBUG) {
975            Log.d(TAG, "Adding handler with viewId " + viewId + " and type " + viewType);
976        }
977        // If the key is already registered this will replace it
978        mController.registerEventHandler(viewId, (EventHandler) frag);
979        if (secFrag != null) {
980            mController.registerEventHandler(viewId, (EventHandler) secFrag);
981        }
982
983        if (doCommit) {
984            if (DEBUG) {
985                Log.d(TAG, "setMainPane AllInOne=" + this + " finishing:" + this.isFinishing());
986            }
987            ft.commit();
988        }
989    }
990
991    private void setTitleInActionBar(EventInfo event) {
992        if (event.eventType != EventType.UPDATE_TITLE || mActionBar == null) {
993            return;
994        }
995
996        final long start = event.startTime.toMillis(false /* use isDst */);
997        final long end;
998        if (event.endTime != null) {
999            end = event.endTime.toMillis(false /* use isDst */);
1000        } else {
1001            end = start;
1002        }
1003
1004        final String msg = Utils.formatDateRange(this, start, end, (int) event.extraLong);
1005        CharSequence oldDate = mDateRange.getText();
1006        mDateRange.setText(msg);
1007        updateSecondaryTitleFields(event.selectedTime != null ? event.selectedTime.toMillis(true)
1008                : start);
1009        if (!TextUtils.equals(oldDate, msg)) {
1010            mDateRange.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
1011            if (mShowWeekNum && mWeekTextView != null) {
1012                mWeekTextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
1013            }
1014        }
1015    }
1016
1017    private void updateSecondaryTitleFields(long visibleMillisSinceEpoch) {
1018        mShowWeekNum = Utils.getShowWeekNumber(this);
1019        mTimeZone = Utils.getTimeZone(this, mHomeTimeUpdater);
1020        if (visibleMillisSinceEpoch != -1) {
1021            int weekNum = Utils.getWeekNumberFromTime(visibleMillisSinceEpoch, this);
1022            mWeekNum = weekNum;
1023        }
1024
1025        if (mShowWeekNum && (mCurrentView == ViewType.WEEK) && mIsTabletConfig
1026                && mWeekTextView != null) {
1027            String weekString = getResources().getQuantityString(R.plurals.weekN, mWeekNum,
1028                    mWeekNum);
1029            mWeekTextView.setText(weekString);
1030            mWeekTextView.setVisibility(View.VISIBLE);
1031        } else if (visibleMillisSinceEpoch != -1 && mWeekTextView != null
1032                && mCurrentView == ViewType.DAY && mIsTabletConfig) {
1033            Time time = new Time(mTimeZone);
1034            time.set(visibleMillisSinceEpoch);
1035            int julianDay = Time.getJulianDay(visibleMillisSinceEpoch, time.gmtoff);
1036            time.setToNow();
1037            int todayJulianDay = Time.getJulianDay(time.toMillis(false), time.gmtoff);
1038            String dayString = Utils.getDayOfWeekString(julianDay, todayJulianDay,
1039                    visibleMillisSinceEpoch, this);
1040            mWeekTextView.setText(dayString);
1041            mWeekTextView.setVisibility(View.VISIBLE);
1042        } else if (mWeekTextView != null && (!mIsTabletConfig || mCurrentView != ViewType.DAY)) {
1043            mWeekTextView.setVisibility(View.GONE);
1044        }
1045
1046        if (mHomeTime != null
1047                && (mCurrentView == ViewType.DAY || mCurrentView == ViewType.WEEK
1048                        || mCurrentView == ViewType.AGENDA)
1049                && !TextUtils.equals(mTimeZone, Time.getCurrentTimezone())) {
1050            Time time = new Time(mTimeZone);
1051            time.setToNow();
1052            long millis = time.toMillis(true);
1053            boolean isDST = time.isDst != 0;
1054            int flags = DateUtils.FORMAT_SHOW_TIME;
1055            if (DateFormat.is24HourFormat(this)) {
1056                flags |= DateUtils.FORMAT_24HOUR;
1057            }
1058            // Formats the time as
1059            String timeString = (new StringBuilder(
1060                    Utils.formatDateRange(this, millis, millis, flags))).append(" ").append(
1061                    TimeZone.getTimeZone(mTimeZone).getDisplayName(
1062                            isDST, TimeZone.SHORT, Locale.getDefault())).toString();
1063            mHomeTime.setText(timeString);
1064            mHomeTime.setVisibility(View.VISIBLE);
1065            // Update when the minute changes
1066            mHomeTime.removeCallbacks(mHomeTimeUpdater);
1067            mHomeTime.postDelayed(
1068                    mHomeTimeUpdater,
1069                    DateUtils.MINUTE_IN_MILLIS - (millis % DateUtils.MINUTE_IN_MILLIS));
1070        } else if (mHomeTime != null) {
1071            mHomeTime.setVisibility(View.GONE);
1072        }
1073    }
1074
1075    @Override
1076    public long getSupportedEventTypes() {
1077        return EventType.GO_TO | EventType.VIEW_EVENT | EventType.UPDATE_TITLE;
1078    }
1079
1080    @Override
1081    public void handleEvent(EventInfo event) {
1082        long displayTime = -1;
1083        if (event.eventType == EventType.GO_TO) {
1084            if ((event.extraLong & CalendarController.EXTRA_GOTO_BACK_TO_PREVIOUS) != 0) {
1085                mBackToPreviousView = true;
1086            } else if (event.viewType != mController.getPreviousViewType()
1087                    && event.viewType != ViewType.EDIT) {
1088                // Clear the flag is change to a different view type
1089                mBackToPreviousView = false;
1090            }
1091
1092            setMainPane(
1093                    null, R.id.main_pane, event.viewType, event.startTime.toMillis(false), false);
1094            if (mSearchView != null) {
1095                mSearchView.clearFocus();
1096            }
1097            if (mShowCalendarControls) {
1098                int animationSize = (mOrientation == Configuration.ORIENTATION_LANDSCAPE) ?
1099                        mControlsAnimateWidth : mControlsAnimateHeight;
1100                boolean noControlsView = event.viewType == ViewType.MONTH || event.viewType == ViewType.AGENDA;
1101                if (mControlsMenu != null) {
1102                    mControlsMenu.setVisible(!noControlsView);
1103                    mControlsMenu.setEnabled(!noControlsView);
1104                }
1105                if (noControlsView || mHideControls) {
1106                    // hide minimonth and calendar frag
1107                    mShowSideViews = false;
1108                    if (!mHideControls) {
1109                            final ObjectAnimator slideAnimation = ObjectAnimator.ofInt(this,
1110                                    "controlsOffset", 0, animationSize);
1111                            slideAnimation.addListener(mSlideAnimationDoneListener);
1112                            slideAnimation.setDuration(mCalendarControlsAnimationTime);
1113                            ObjectAnimator.setFrameDelay(0);
1114                            slideAnimation.start();
1115                    } else {
1116                        mMiniMonth.setVisibility(View.GONE);
1117                        mCalendarsList.setVisibility(View.GONE);
1118                        mMiniMonthContainer.setVisibility(View.GONE);
1119                    }
1120                } else {
1121                    // show minimonth and calendar frag
1122                    mShowSideViews = true;
1123                    mMiniMonth.setVisibility(View.VISIBLE);
1124                    mCalendarsList.setVisibility(View.VISIBLE);
1125                    mMiniMonthContainer.setVisibility(View.VISIBLE);
1126                    if (!mHideControls &&
1127                            (mController.getPreviousViewType() == ViewType.MONTH ||
1128                             mController.getPreviousViewType() == ViewType.AGENDA)) {
1129                        final ObjectAnimator slideAnimation = ObjectAnimator.ofInt(this,
1130                                "controlsOffset", animationSize, 0);
1131                        slideAnimation.setDuration(mCalendarControlsAnimationTime);
1132                        ObjectAnimator.setFrameDelay(0);
1133                        slideAnimation.start();
1134                    }
1135                }
1136            }
1137            displayTime = event.selectedTime != null ? event.selectedTime.toMillis(true)
1138                    : event.startTime.toMillis(true);
1139            if (!mIsTabletConfig) {
1140                mActionBarMenuSpinnerAdapter.setTime(displayTime);
1141            }
1142        } else if (event.eventType == EventType.VIEW_EVENT) {
1143
1144            // If in Agenda view and "show_event_details_with_agenda" is "true",
1145            // do not create the event info fragment here, it will be created by the Agenda
1146            // fragment
1147
1148            if (mCurrentView == ViewType.AGENDA && mShowEventDetailsWithAgenda) {
1149                if (event.startTime != null && event.endTime != null) {
1150                    // Event is all day , adjust the goto time to local time
1151                    if (event.isAllDay()) {
1152                        Utils.convertAlldayUtcToLocal(
1153                                event.startTime, event.startTime.toMillis(false), mTimeZone);
1154                        Utils.convertAlldayUtcToLocal(
1155                                event.endTime, event.endTime.toMillis(false), mTimeZone);
1156                    }
1157                    mController.sendEvent(this, EventType.GO_TO, event.startTime, event.endTime,
1158                            event.selectedTime, event.id, ViewType.AGENDA,
1159                            CalendarController.EXTRA_GOTO_TIME, null, null);
1160                } else if (event.selectedTime != null) {
1161                    mController.sendEvent(this, EventType.GO_TO, event.selectedTime,
1162                        event.selectedTime, event.id, ViewType.AGENDA);
1163                }
1164            } else {
1165                // TODO Fix the temp hack below: && mCurrentView !=
1166                // ViewType.AGENDA
1167                if (event.selectedTime != null && mCurrentView != ViewType.AGENDA) {
1168                    mController.sendEvent(this, EventType.GO_TO, event.selectedTime,
1169                            event.selectedTime, -1, ViewType.CURRENT);
1170                }
1171                int response = event.getResponse();
1172                if ((mCurrentView == ViewType.AGENDA && mShowEventInfoFullScreenAgenda) ||
1173                        ((mCurrentView == ViewType.DAY || (mCurrentView == ViewType.WEEK) ||
1174                                mCurrentView == ViewType.MONTH) && mShowEventInfoFullScreen)){
1175                    // start event info as activity
1176                    Intent intent = new Intent(Intent.ACTION_VIEW);
1177                    Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, event.id);
1178                    intent.setData(eventUri);
1179                    intent.setClass(this, EventInfoActivity.class);
1180                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
1181                            Intent.FLAG_ACTIVITY_SINGLE_TOP);
1182                    intent.putExtra(EXTRA_EVENT_BEGIN_TIME, event.startTime.toMillis(false));
1183                    intent.putExtra(EXTRA_EVENT_END_TIME, event.endTime.toMillis(false));
1184                    intent.putExtra(ATTENDEE_STATUS, response);
1185                    startActivity(intent);
1186                } else {
1187                    // start event info as a dialog
1188                    EventInfoFragment fragment = new EventInfoFragment(this,
1189                            event.id, event.startTime.toMillis(false),
1190                            event.endTime.toMillis(false), response, true,
1191                            EventInfoFragment.DIALOG_WINDOW_STYLE,
1192                            null /* No reminders to explicitly pass in. */);
1193                    fragment.setDialogParams(event.x, event.y, mActionBar.getHeight());
1194                    FragmentManager fm = getFragmentManager();
1195                    FragmentTransaction ft = fm.beginTransaction();
1196                    // if we have an old popup replace it
1197                    Fragment fOld = fm.findFragmentByTag(EVENT_INFO_FRAGMENT_TAG);
1198                    if (fOld != null && fOld.isAdded()) {
1199                        ft.remove(fOld);
1200                    }
1201                    ft.add(fragment, EVENT_INFO_FRAGMENT_TAG);
1202                    ft.commit();
1203                }
1204            }
1205            displayTime = event.startTime.toMillis(true);
1206        } else if (event.eventType == EventType.UPDATE_TITLE) {
1207            setTitleInActionBar(event);
1208            if (!mIsTabletConfig) {
1209                mActionBarMenuSpinnerAdapter.setTime(mController.getTime());
1210            }
1211        }
1212        updateSecondaryTitleFields(displayTime);
1213    }
1214
1215    // Needs to be in proguard whitelist
1216    // Specified as listener via android:onClick in a layout xml
1217    public void handleSelectSyncedCalendarsClicked(View v) {
1218        mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, null, 0, 0,
1219                CalendarController.EXTRA_GOTO_TIME, null,
1220                null);
1221    }
1222
1223    @Override
1224    public void eventsChanged() {
1225        mController.sendEvent(this, EventType.EVENTS_CHANGED, null, null, -1, ViewType.CURRENT);
1226    }
1227
1228    @Override
1229    public boolean onQueryTextChange(String newText) {
1230        return false;
1231    }
1232
1233    @Override
1234    public boolean onQueryTextSubmit(String query) {
1235        mSearchMenu.collapseActionView();
1236        mController.sendEvent(this, EventType.SEARCH, null, null, -1, ViewType.CURRENT, 0, query,
1237                getComponentName());
1238        return true;
1239    }
1240
1241    @Override
1242    public void onTabSelected(Tab tab, FragmentTransaction ft) {
1243        Log.w(TAG, "TabSelected AllInOne=" + this + " finishing:" + this.isFinishing());
1244        if (tab == mDayTab && mCurrentView != ViewType.DAY) {
1245            mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.DAY);
1246        } else if (tab == mWeekTab && mCurrentView != ViewType.WEEK) {
1247            mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.WEEK);
1248        } else if (tab == mMonthTab && mCurrentView != ViewType.MONTH) {
1249            mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.MONTH);
1250        } else if (tab == mAgendaTab && mCurrentView != ViewType.AGENDA) {
1251            mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.AGENDA);
1252        } else {
1253            Log.w(TAG, "TabSelected event from unknown tab: "
1254                    + (tab == null ? "null" : tab.getText()));
1255            Log.w(TAG, "CurrentView:" + mCurrentView + " Tab:" + tab.toString() + " Day:" + mDayTab
1256                    + " Week:" + mWeekTab + " Month:" + mMonthTab + " Agenda:" + mAgendaTab);
1257        }
1258    }
1259
1260    @Override
1261    public void onTabReselected(Tab tab, FragmentTransaction ft) {
1262    }
1263
1264    @Override
1265    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
1266    }
1267
1268
1269    @Override
1270    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
1271        switch (itemPosition) {
1272            case CalendarViewAdapter.DAY_BUTTON_INDEX:
1273                if (mCurrentView != ViewType.DAY) {
1274                    mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.DAY);
1275                }
1276                break;
1277            case CalendarViewAdapter.WEEK_BUTTON_INDEX:
1278                if (mCurrentView != ViewType.WEEK) {
1279                    mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.WEEK);
1280                }
1281                break;
1282            case CalendarViewAdapter.MONTH_BUTTON_INDEX:
1283                if (mCurrentView != ViewType.MONTH) {
1284                    mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.MONTH);
1285                }
1286                break;
1287            case CalendarViewAdapter.AGENDA_BUTTON_INDEX:
1288                if (mCurrentView != ViewType.AGENDA) {
1289                    mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.AGENDA);
1290                }
1291                break;
1292            default:
1293                Log.w(TAG, "ItemSelected event from unknown button: " + itemPosition);
1294                Log.w(TAG, "CurrentView:" + mCurrentView + " Button:" + itemPosition +
1295                        " Day:" + mDayTab + " Week:" + mWeekTab + " Month:" + mMonthTab +
1296                        " Agenda:" + mAgendaTab);
1297                break;
1298        }
1299        return false;
1300    }
1301
1302    @Override
1303    public boolean onSuggestionSelect(int position) {
1304        return false;
1305    }
1306
1307    @Override
1308    public boolean onSuggestionClick(int position) {
1309        mSearchMenu.collapseActionView();
1310        return false;
1311    }
1312
1313    @Override
1314    public boolean onSearchRequested() {
1315        if (mSearchMenu != null) {
1316            mSearchMenu.expandActionView();
1317        }
1318        return false;
1319    }
1320}
1321