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