Home | History | Annotate | Download | only in calendar
      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 
     17 package com.android.calendar;
     18 
     19 import android.accounts.AccountManager;
     20 import android.accounts.AccountManagerCallback;
     21 import android.accounts.AccountManagerFuture;
     22 import android.accounts.AuthenticatorException;
     23 import android.accounts.OperationCanceledException;
     24 import android.animation.Animator;
     25 import android.animation.Animator.AnimatorListener;
     26 import android.animation.ObjectAnimator;
     27 import android.app.ActionBar;
     28 import android.app.ActionBar.Tab;
     29 import android.app.Fragment;
     30 import android.app.FragmentManager;
     31 import android.app.FragmentTransaction;
     32 import android.content.AsyncQueryHandler;
     33 import android.content.BroadcastReceiver;
     34 import android.content.ContentResolver;
     35 import android.content.ContentUris;
     36 import android.content.Intent;
     37 import android.content.SharedPreferences;
     38 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
     39 import android.content.res.Configuration;
     40 import android.content.res.Resources;
     41 import android.database.ContentObserver;
     42 import android.database.Cursor;
     43 import android.graphics.drawable.LayerDrawable;
     44 import android.net.Uri;
     45 import android.os.Bundle;
     46 import android.os.Handler;
     47 import android.provider.CalendarContract;
     48 import android.provider.CalendarContract.Attendees;
     49 import android.provider.CalendarContract.Calendars;
     50 import android.provider.CalendarContract.Events;
     51 import android.text.TextUtils;
     52 import android.text.format.DateFormat;
     53 import android.text.format.DateUtils;
     54 import android.text.format.Time;
     55 import android.util.Log;
     56 import android.view.Menu;
     57 import android.view.MenuItem;
     58 import android.view.View;
     59 import android.view.accessibility.AccessibilityEvent;
     60 import android.widget.LinearLayout;
     61 import android.widget.RelativeLayout;
     62 import android.widget.RelativeLayout.LayoutParams;
     63 import android.widget.SearchView;
     64 import android.widget.SearchView.OnSuggestionListener;
     65 import android.widget.TextView;
     66 
     67 import com.android.calendar.CalendarController.EventHandler;
     68 import com.android.calendar.CalendarController.EventInfo;
     69 import com.android.calendar.CalendarController.EventType;
     70 import com.android.calendar.CalendarController.ViewType;
     71 import com.android.calendar.agenda.AgendaFragment;
     72 import com.android.calendar.month.MonthByWeekFragment;
     73 import com.android.calendar.selectcalendars.SelectVisibleCalendarsFragment;
     74 
     75 import java.io.IOException;
     76 import java.util.List;
     77 import java.util.Locale;
     78 import java.util.TimeZone;
     79 
     80 import static android.provider.CalendarContract.Attendees.ATTENDEE_STATUS;
     81 import static android.provider.CalendarContract.EXTRA_EVENT_ALL_DAY;
     82 import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME;
     83 import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME;
     84 
     85 public 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                 ExtensionsFactory.getAnalyticsLogger(getBaseContext()).trackView("agenda");
    890                 break;
    891             case ViewType.DAY:
    892                 if (mActionBar != null && (mActionBar.getSelectedTab() != mDayTab)) {
    893                     mActionBar.selectTab(mDayTab);
    894                 }
    895                 if (mActionBarMenuSpinnerAdapter != null) {
    896                     mActionBar.setSelectedNavigationItem(CalendarViewAdapter.DAY_BUTTON_INDEX);
    897                 }
    898                 frag = new DayFragment(timeMillis, 1);
    899                 ExtensionsFactory.getAnalyticsLogger(getBaseContext()).trackView("day");
    900                 break;
    901             case ViewType.MONTH:
    902                 if (mActionBar != null && (mActionBar.getSelectedTab() != mMonthTab)) {
    903                     mActionBar.selectTab(mMonthTab);
    904                 }
    905                 if (mActionBarMenuSpinnerAdapter != null) {
    906                     mActionBar.setSelectedNavigationItem(CalendarViewAdapter.MONTH_BUTTON_INDEX);
    907                 }
    908                 frag = new MonthByWeekFragment(timeMillis, false);
    909                 if (mShowAgendaWithMonth) {
    910                     secFrag = new AgendaFragment(timeMillis, false);
    911                 }
    912                 ExtensionsFactory.getAnalyticsLogger(getBaseContext()).trackView("month");
    913                 break;
    914             case ViewType.WEEK:
    915             default:
    916                 if (mActionBar != null && (mActionBar.getSelectedTab() != mWeekTab)) {
    917                     mActionBar.selectTab(mWeekTab);
    918                 }
    919                 if (mActionBarMenuSpinnerAdapter != null) {
    920                     mActionBar.setSelectedNavigationItem(CalendarViewAdapter.WEEK_BUTTON_INDEX);
    921                 }
    922                 frag = new DayFragment(timeMillis, 7);
    923                 ExtensionsFactory.getAnalyticsLogger(getBaseContext()).trackView("week");
    924                 break;
    925         }
    926 
    927         // Update the current view so that the menu can update its look according to the
    928         // current view.
    929         if (mActionBarMenuSpinnerAdapter != null) {
    930             mActionBarMenuSpinnerAdapter.setMainView(viewType);
    931             if (!mIsTabletConfig) {
    932                 mActionBarMenuSpinnerAdapter.setTime(timeMillis);
    933             }
    934         }
    935 
    936 
    937         // Show date only on tablet configurations in views different than Agenda
    938         if (!mIsTabletConfig) {
    939             mDateRange.setVisibility(View.GONE);
    940         } else if (viewType != ViewType.AGENDA) {
    941             mDateRange.setVisibility(View.VISIBLE);
    942         } else {
    943             mDateRange.setVisibility(View.GONE);
    944         }
    945 
    946         // Clear unnecessary buttons from the option menu when switching from the agenda view
    947         if (viewType != ViewType.AGENDA) {
    948             clearOptionsMenu();
    949         }
    950 
    951         boolean doCommit = false;
    952         if (ft == null) {
    953             doCommit = true;
    954             ft = fragmentManager.beginTransaction();
    955         }
    956 
    957         if (doTransition) {
    958             ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    959         }
    960 
    961         ft.replace(viewId, frag);
    962         if (mShowAgendaWithMonth) {
    963 
    964             // Show/hide secondary fragment
    965 
    966             if (secFrag != null) {
    967                 ft.replace(R.id.secondary_pane, secFrag);
    968                 mSecondaryPane.setVisibility(View.VISIBLE);
    969             } else {
    970                 mSecondaryPane.setVisibility(View.GONE);
    971                 Fragment f = fragmentManager.findFragmentById(R.id.secondary_pane);
    972                 if (f != null) {
    973                     ft.remove(f);
    974                 }
    975                 mController.deregisterEventHandler(R.id.secondary_pane);
    976             }
    977         }
    978         if (DEBUG) {
    979             Log.d(TAG, "Adding handler with viewId " + viewId + " and type " + viewType);
    980         }
    981         // If the key is already registered this will replace it
    982         mController.registerEventHandler(viewId, (EventHandler) frag);
    983         if (secFrag != null) {
    984             mController.registerEventHandler(viewId, (EventHandler) secFrag);
    985         }
    986 
    987         if (doCommit) {
    988             if (DEBUG) {
    989                 Log.d(TAG, "setMainPane AllInOne=" + this + " finishing:" + this.isFinishing());
    990             }
    991             ft.commit();
    992         }
    993     }
    994 
    995     private void setTitleInActionBar(EventInfo event) {
    996         if (event.eventType != EventType.UPDATE_TITLE || mActionBar == null) {
    997             return;
    998         }
    999 
   1000         final long start = event.startTime.toMillis(false /* use isDst */);
   1001         final long end;
   1002         if (event.endTime != null) {
   1003             end = event.endTime.toMillis(false /* use isDst */);
   1004         } else {
   1005             end = start;
   1006         }
   1007 
   1008         final String msg = Utils.formatDateRange(this, start, end, (int) event.extraLong);
   1009         CharSequence oldDate = mDateRange.getText();
   1010         mDateRange.setText(msg);
   1011         updateSecondaryTitleFields(event.selectedTime != null ? event.selectedTime.toMillis(true)
   1012                 : start);
   1013         if (!TextUtils.equals(oldDate, msg)) {
   1014             mDateRange.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
   1015             if (mShowWeekNum && mWeekTextView != null) {
   1016                 mWeekTextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
   1017             }
   1018         }
   1019     }
   1020 
   1021     private void updateSecondaryTitleFields(long visibleMillisSinceEpoch) {
   1022         mShowWeekNum = Utils.getShowWeekNumber(this);
   1023         mTimeZone = Utils.getTimeZone(this, mHomeTimeUpdater);
   1024         if (visibleMillisSinceEpoch != -1) {
   1025             int weekNum = Utils.getWeekNumberFromTime(visibleMillisSinceEpoch, this);
   1026             mWeekNum = weekNum;
   1027         }
   1028 
   1029         if (mShowWeekNum && (mCurrentView == ViewType.WEEK) && mIsTabletConfig
   1030                 && mWeekTextView != null) {
   1031             String weekString = getResources().getQuantityString(R.plurals.weekN, mWeekNum,
   1032                     mWeekNum);
   1033             mWeekTextView.setText(weekString);
   1034             mWeekTextView.setVisibility(View.VISIBLE);
   1035         } else if (visibleMillisSinceEpoch != -1 && mWeekTextView != null
   1036                 && mCurrentView == ViewType.DAY && mIsTabletConfig) {
   1037             Time time = new Time(mTimeZone);
   1038             time.set(visibleMillisSinceEpoch);
   1039             int julianDay = Time.getJulianDay(visibleMillisSinceEpoch, time.gmtoff);
   1040             time.setToNow();
   1041             int todayJulianDay = Time.getJulianDay(time.toMillis(false), time.gmtoff);
   1042             String dayString = Utils.getDayOfWeekString(julianDay, todayJulianDay,
   1043                     visibleMillisSinceEpoch, this);
   1044             mWeekTextView.setText(dayString);
   1045             mWeekTextView.setVisibility(View.VISIBLE);
   1046         } else if (mWeekTextView != null && (!mIsTabletConfig || mCurrentView != ViewType.DAY)) {
   1047             mWeekTextView.setVisibility(View.GONE);
   1048         }
   1049 
   1050         if (mHomeTime != null
   1051                 && (mCurrentView == ViewType.DAY || mCurrentView == ViewType.WEEK
   1052                         || mCurrentView == ViewType.AGENDA)
   1053                 && !TextUtils.equals(mTimeZone, Time.getCurrentTimezone())) {
   1054             Time time = new Time(mTimeZone);
   1055             time.setToNow();
   1056             long millis = time.toMillis(true);
   1057             boolean isDST = time.isDst != 0;
   1058             int flags = DateUtils.FORMAT_SHOW_TIME;
   1059             if (DateFormat.is24HourFormat(this)) {
   1060                 flags |= DateUtils.FORMAT_24HOUR;
   1061             }
   1062             // Formats the time as
   1063             String timeString = (new StringBuilder(
   1064                     Utils.formatDateRange(this, millis, millis, flags))).append(" ").append(
   1065                     TimeZone.getTimeZone(mTimeZone).getDisplayName(
   1066                             isDST, TimeZone.SHORT, Locale.getDefault())).toString();
   1067             mHomeTime.setText(timeString);
   1068             mHomeTime.setVisibility(View.VISIBLE);
   1069             // Update when the minute changes
   1070             mHomeTime.removeCallbacks(mHomeTimeUpdater);
   1071             mHomeTime.postDelayed(
   1072                     mHomeTimeUpdater,
   1073                     DateUtils.MINUTE_IN_MILLIS - (millis % DateUtils.MINUTE_IN_MILLIS));
   1074         } else if (mHomeTime != null) {
   1075             mHomeTime.setVisibility(View.GONE);
   1076         }
   1077     }
   1078 
   1079     @Override
   1080     public long getSupportedEventTypes() {
   1081         return EventType.GO_TO | EventType.VIEW_EVENT | EventType.UPDATE_TITLE;
   1082     }
   1083 
   1084     @Override
   1085     public void handleEvent(EventInfo event) {
   1086         long displayTime = -1;
   1087         if (event.eventType == EventType.GO_TO) {
   1088             if ((event.extraLong & CalendarController.EXTRA_GOTO_BACK_TO_PREVIOUS) != 0) {
   1089                 mBackToPreviousView = true;
   1090             } else if (event.viewType != mController.getPreviousViewType()
   1091                     && event.viewType != ViewType.EDIT) {
   1092                 // Clear the flag is change to a different view type
   1093                 mBackToPreviousView = false;
   1094             }
   1095 
   1096             setMainPane(
   1097                     null, R.id.main_pane, event.viewType, event.startTime.toMillis(false), false);
   1098             if (mSearchView != null) {
   1099                 mSearchView.clearFocus();
   1100             }
   1101             if (mShowCalendarControls) {
   1102                 int animationSize = (mOrientation == Configuration.ORIENTATION_LANDSCAPE) ?
   1103                         mControlsAnimateWidth : mControlsAnimateHeight;
   1104                 boolean noControlsView = event.viewType == ViewType.MONTH || event.viewType == ViewType.AGENDA;
   1105                 if (mControlsMenu != null) {
   1106                     mControlsMenu.setVisible(!noControlsView);
   1107                     mControlsMenu.setEnabled(!noControlsView);
   1108                 }
   1109                 if (noControlsView || mHideControls) {
   1110                     // hide minimonth and calendar frag
   1111                     mShowSideViews = false;
   1112                     if (!mHideControls) {
   1113                             final ObjectAnimator slideAnimation = ObjectAnimator.ofInt(this,
   1114                                     "controlsOffset", 0, animationSize);
   1115                             slideAnimation.addListener(mSlideAnimationDoneListener);
   1116                             slideAnimation.setDuration(mCalendarControlsAnimationTime);
   1117                             ObjectAnimator.setFrameDelay(0);
   1118                             slideAnimation.start();
   1119                     } else {
   1120                         mMiniMonth.setVisibility(View.GONE);
   1121                         mCalendarsList.setVisibility(View.GONE);
   1122                         mMiniMonthContainer.setVisibility(View.GONE);
   1123                     }
   1124                 } else {
   1125                     // show minimonth and calendar frag
   1126                     mShowSideViews = true;
   1127                     mMiniMonth.setVisibility(View.VISIBLE);
   1128                     mCalendarsList.setVisibility(View.VISIBLE);
   1129                     mMiniMonthContainer.setVisibility(View.VISIBLE);
   1130                     if (!mHideControls &&
   1131                             (mController.getPreviousViewType() == ViewType.MONTH ||
   1132                              mController.getPreviousViewType() == ViewType.AGENDA)) {
   1133                         final ObjectAnimator slideAnimation = ObjectAnimator.ofInt(this,
   1134                                 "controlsOffset", animationSize, 0);
   1135                         slideAnimation.setDuration(mCalendarControlsAnimationTime);
   1136                         ObjectAnimator.setFrameDelay(0);
   1137                         slideAnimation.start();
   1138                     }
   1139                 }
   1140             }
   1141             displayTime = event.selectedTime != null ? event.selectedTime.toMillis(true)
   1142                     : event.startTime.toMillis(true);
   1143             if (!mIsTabletConfig) {
   1144                 mActionBarMenuSpinnerAdapter.setTime(displayTime);
   1145             }
   1146         } else if (event.eventType == EventType.VIEW_EVENT) {
   1147 
   1148             // If in Agenda view and "show_event_details_with_agenda" is "true",
   1149             // do not create the event info fragment here, it will be created by the Agenda
   1150             // fragment
   1151 
   1152             if (mCurrentView == ViewType.AGENDA && mShowEventDetailsWithAgenda) {
   1153                 if (event.startTime != null && event.endTime != null) {
   1154                     // Event is all day , adjust the goto time to local time
   1155                     if (event.isAllDay()) {
   1156                         Utils.convertAlldayUtcToLocal(
   1157                                 event.startTime, event.startTime.toMillis(false), mTimeZone);
   1158                         Utils.convertAlldayUtcToLocal(
   1159                                 event.endTime, event.endTime.toMillis(false), mTimeZone);
   1160                     }
   1161                     mController.sendEvent(this, EventType.GO_TO, event.startTime, event.endTime,
   1162                             event.selectedTime, event.id, ViewType.AGENDA,
   1163                             CalendarController.EXTRA_GOTO_TIME, null, null);
   1164                 } else if (event.selectedTime != null) {
   1165                     mController.sendEvent(this, EventType.GO_TO, event.selectedTime,
   1166                         event.selectedTime, event.id, ViewType.AGENDA);
   1167                 }
   1168             } else {
   1169                 // TODO Fix the temp hack below: && mCurrentView !=
   1170                 // ViewType.AGENDA
   1171                 if (event.selectedTime != null && mCurrentView != ViewType.AGENDA) {
   1172                     mController.sendEvent(this, EventType.GO_TO, event.selectedTime,
   1173                             event.selectedTime, -1, ViewType.CURRENT);
   1174                 }
   1175                 int response = event.getResponse();
   1176                 if ((mCurrentView == ViewType.AGENDA && mShowEventInfoFullScreenAgenda) ||
   1177                         ((mCurrentView == ViewType.DAY || (mCurrentView == ViewType.WEEK) ||
   1178                                 mCurrentView == ViewType.MONTH) && mShowEventInfoFullScreen)){
   1179                     // start event info as activity
   1180                     Intent intent = new Intent(Intent.ACTION_VIEW);
   1181                     Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, event.id);
   1182                     intent.setData(eventUri);
   1183                     intent.setClass(this, EventInfoActivity.class);
   1184                     intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
   1185                             Intent.FLAG_ACTIVITY_SINGLE_TOP);
   1186                     intent.putExtra(EXTRA_EVENT_BEGIN_TIME, event.startTime.toMillis(false));
   1187                     intent.putExtra(EXTRA_EVENT_END_TIME, event.endTime.toMillis(false));
   1188                     intent.putExtra(ATTENDEE_STATUS, response);
   1189                     startActivity(intent);
   1190                 } else {
   1191                     // start event info as a dialog
   1192                     EventInfoFragment fragment = new EventInfoFragment(this,
   1193                             event.id, event.startTime.toMillis(false),
   1194                             event.endTime.toMillis(false), response, true,
   1195                             EventInfoFragment.DIALOG_WINDOW_STYLE,
   1196                             null /* No reminders to explicitly pass in. */);
   1197                     fragment.setDialogParams(event.x, event.y, mActionBar.getHeight());
   1198                     FragmentManager fm = getFragmentManager();
   1199                     FragmentTransaction ft = fm.beginTransaction();
   1200                     // if we have an old popup replace it
   1201                     Fragment fOld = fm.findFragmentByTag(EVENT_INFO_FRAGMENT_TAG);
   1202                     if (fOld != null && fOld.isAdded()) {
   1203                         ft.remove(fOld);
   1204                     }
   1205                     ft.add(fragment, EVENT_INFO_FRAGMENT_TAG);
   1206                     ft.commit();
   1207                 }
   1208             }
   1209             displayTime = event.startTime.toMillis(true);
   1210         } else if (event.eventType == EventType.UPDATE_TITLE) {
   1211             setTitleInActionBar(event);
   1212             if (!mIsTabletConfig) {
   1213                 mActionBarMenuSpinnerAdapter.setTime(mController.getTime());
   1214             }
   1215         }
   1216         updateSecondaryTitleFields(displayTime);
   1217     }
   1218 
   1219     // Needs to be in proguard whitelist
   1220     // Specified as listener via android:onClick in a layout xml
   1221     public void handleSelectSyncedCalendarsClicked(View v) {
   1222         mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, null, 0, 0,
   1223                 CalendarController.EXTRA_GOTO_TIME, null,
   1224                 null);
   1225     }
   1226 
   1227     @Override
   1228     public void eventsChanged() {
   1229         mController.sendEvent(this, EventType.EVENTS_CHANGED, null, null, -1, ViewType.CURRENT);
   1230     }
   1231 
   1232     @Override
   1233     public boolean onQueryTextChange(String newText) {
   1234         return false;
   1235     }
   1236 
   1237     @Override
   1238     public boolean onQueryTextSubmit(String query) {
   1239         mSearchMenu.collapseActionView();
   1240         mController.sendEvent(this, EventType.SEARCH, null, null, -1, ViewType.CURRENT, 0, query,
   1241                 getComponentName());
   1242         return true;
   1243     }
   1244 
   1245     @Override
   1246     public void onTabSelected(Tab tab, FragmentTransaction ft) {
   1247         Log.w(TAG, "TabSelected AllInOne=" + this + " finishing:" + this.isFinishing());
   1248         if (tab == mDayTab && mCurrentView != ViewType.DAY) {
   1249             mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.DAY);
   1250         } else if (tab == mWeekTab && mCurrentView != ViewType.WEEK) {
   1251             mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.WEEK);
   1252         } else if (tab == mMonthTab && mCurrentView != ViewType.MONTH) {
   1253             mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.MONTH);
   1254         } else if (tab == mAgendaTab && mCurrentView != ViewType.AGENDA) {
   1255             mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.AGENDA);
   1256         } else {
   1257             Log.w(TAG, "TabSelected event from unknown tab: "
   1258                     + (tab == null ? "null" : tab.getText()));
   1259             Log.w(TAG, "CurrentView:" + mCurrentView + " Tab:" + tab.toString() + " Day:" + mDayTab
   1260                     + " Week:" + mWeekTab + " Month:" + mMonthTab + " Agenda:" + mAgendaTab);
   1261         }
   1262     }
   1263 
   1264     @Override
   1265     public void onTabReselected(Tab tab, FragmentTransaction ft) {
   1266     }
   1267 
   1268     @Override
   1269     public void onTabUnselected(Tab tab, FragmentTransaction ft) {
   1270     }
   1271 
   1272 
   1273     @Override
   1274     public boolean onNavigationItemSelected(int itemPosition, long itemId) {
   1275         switch (itemPosition) {
   1276             case CalendarViewAdapter.DAY_BUTTON_INDEX:
   1277                 if (mCurrentView != ViewType.DAY) {
   1278                     mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.DAY);
   1279                 }
   1280                 break;
   1281             case CalendarViewAdapter.WEEK_BUTTON_INDEX:
   1282                 if (mCurrentView != ViewType.WEEK) {
   1283                     mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.WEEK);
   1284                 }
   1285                 break;
   1286             case CalendarViewAdapter.MONTH_BUTTON_INDEX:
   1287                 if (mCurrentView != ViewType.MONTH) {
   1288                     mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.MONTH);
   1289                 }
   1290                 break;
   1291             case CalendarViewAdapter.AGENDA_BUTTON_INDEX:
   1292                 if (mCurrentView != ViewType.AGENDA) {
   1293                     mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.AGENDA);
   1294                 }
   1295                 break;
   1296             default:
   1297                 Log.w(TAG, "ItemSelected event from unknown button: " + itemPosition);
   1298                 Log.w(TAG, "CurrentView:" + mCurrentView + " Button:" + itemPosition +
   1299                         " Day:" + mDayTab + " Week:" + mWeekTab + " Month:" + mMonthTab +
   1300                         " Agenda:" + mAgendaTab);
   1301                 break;
   1302         }
   1303         return false;
   1304     }
   1305 
   1306     @Override
   1307     public boolean onSuggestionSelect(int position) {
   1308         return false;
   1309     }
   1310 
   1311     @Override
   1312     public boolean onSuggestionClick(int position) {
   1313         mSearchMenu.collapseActionView();
   1314         return false;
   1315     }
   1316 
   1317     @Override
   1318     public boolean onSearchRequested() {
   1319         if (mSearchMenu != null) {
   1320             mSearchMenu.expandActionView();
   1321         }
   1322         return false;
   1323     }
   1324 }
   1325