summaryrefslogtreecommitdiff
path: root/src/com/android/calendar/AllInOneActivity.kt
blob: 6c2e825fc4fec379a15cc1a3e179025aba6f652c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.calendar

import android.accounts.AccountManager
import android.accounts.AccountManagerCallback
import android.accounts.AccountManagerFuture
import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.animation.ObjectAnimator
import android.app.ActionBar
import android.app.ActionBar.Tab
import android.app.Activity
import android.app.Fragment
import android.app.FragmentManager
import android.app.FragmentTransaction
import android.content.AsyncQueryHandler
import android.content.ContentResolver
import android.content.Intent
import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.content.res.Configuration
import android.content.res.Resources
import android.database.ContentObserver
import android.database.Cursor
import android.graphics.drawable.LayerDrawable
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.provider.CalendarContract
import android.provider.CalendarContract.Attendees
import android.provider.CalendarContract.Calendars
import android.provider.CalendarContract.Events
import android.text.TextUtils
import android.text.format.DateFormat
import android.text.format.DateUtils
import android.text.format.Time
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.accessibility.AccessibilityEvent
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.RelativeLayout.LayoutParams
import android.widget.TextView
import com.android.calendar.CalendarController.EventHandler
import com.android.calendar.CalendarController.EventInfo
import com.android.calendar.CalendarController.EventType
import com.android.calendar.CalendarController.ViewType
import com.android.calendar.month.MonthByWeekFragment
import java.util.Locale
import java.util.TimeZone
import android.provider.CalendarContract.Attendees.ATTENDEE_STATUS
import android.provider.CalendarContract.EXTRA_EVENT_ALL_DAY
import android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME
import android.provider.CalendarContract.EXTRA_EVENT_END_TIME

class AllInOneActivity : Activity(), EventHandler, OnSharedPreferenceChangeListener,
    ActionBar.TabListener, ActionBar.OnNavigationListener {
    private var mController: CalendarController? = null
    private var mOnSaveInstanceStateCalled = false
    private var mBackToPreviousView = false
    private var mContentResolver: ContentResolver? = null
    private var mPreviousView = 0
    private var mCurrentView = 0
    private var mPaused = true
    private var mUpdateOnResume = false
    private var mHideControls = false
    private var mShowSideViews = true
    private var mShowWeekNum = false
    private var mHomeTime: TextView? = null
    private var mDateRange: TextView? = null
    private var mWeekTextView: TextView? = null
    private var mMiniMonth: View? = null
    private var mCalendarsList: View? = null
    private var mMiniMonthContainer: View? = null
    private var mSecondaryPane: View? = null
    private var mTimeZone: String? = null
    private var mShowCalendarControls = false
    private var mShowEventInfoFullScreen = false
    private var mWeekNum = 0
    private var mCalendarControlsAnimationTime = 0
    private var mControlsAnimateWidth = 0
    private var mControlsAnimateHeight = 0
    private var mViewEventId: Long = -1
    private var mIntentEventStartMillis: Long = -1
    private var mIntentEventEndMillis: Long = -1
    private var mIntentAttendeeResponse: Int = Attendees.ATTENDEE_STATUS_NONE
    private var mIntentAllDay = false

    // Action bar and Navigation bar (left side of Action bar)
    private var mActionBar: ActionBar? = null
    private val mDayTab: Tab? = null
    private val mWeekTab: Tab? = null
    private val mMonthTab: Tab? = null
    private var mControlsMenu: MenuItem? = null
    private var mOptionsMenu: Menu? = null
    private var mActionBarMenuSpinnerAdapter: CalendarViewAdapter? = null
    private var mHandler: QueryHandler? = null
    private var mCheckForAccounts = true
    private var mHideString: String? = null
    private var mShowString: String? = null
    var mDayOfMonthIcon: DayOfMonthDrawable? = null
    var mOrientation = 0

    // Params for animating the controls on the right
    private var mControlsParams: LayoutParams? = null
    private var mVerticalControlsParams: LinearLayout.LayoutParams? = null
    private val mSlideAnimationDoneListener: AnimatorListener = object : AnimatorListener {
        @Override
        override fun onAnimationCancel(animation: Animator) {
        }

        @Override
        override fun onAnimationEnd(animation: Animator) {
            val visibility: Int = if (mShowSideViews) View.VISIBLE else View.GONE
            mMiniMonth?.setVisibility(visibility)
            mCalendarsList?.setVisibility(visibility)
            mMiniMonthContainer?.setVisibility(visibility)
        }

        @Override
        override fun onAnimationRepeat(animation: Animator) {
        }

        @Override
        override fun onAnimationStart(animation: Animator) {
        }
    }

    private inner class QueryHandler(cr: ContentResolver?) : AsyncQueryHandler(cr) {
        @Override
        protected override fun onQueryComplete(token: Int, cookie: Any?, cursor: Cursor?) {
            mCheckForAccounts = false
            try {
                // If the query didn't return a cursor for some reason return
                if (cursor == null || cursor.getCount() > 0 || isFinishing()) {
                    return
                }
            } finally {
                if (cursor != null) {
                    cursor.close()
                }
            }
            val options = Bundle()
            options.putCharSequence(
                "introMessage",
                getResources().getString(R.string.create_an_account_desc)
            )
            options.putBoolean("allowSkip", true)
            val am: AccountManager = AccountManager.get(this@AllInOneActivity)
            am.addAccount("com.google", CalendarContract.AUTHORITY, null, options,
                this@AllInOneActivity,
                    object : AccountManagerCallback<Bundle?> {
                        @Override
                        override fun run(future: AccountManagerFuture<Bundle?>?) {
                        }
                    }, null
            )
        }
    }

    private val mHomeTimeUpdater: Runnable = object : Runnable {
        @Override
        override fun run() {
            mTimeZone = Utils.getTimeZone(this@AllInOneActivity, this)
            updateSecondaryTitleFields(-1)
            this@AllInOneActivity.invalidateOptionsMenu()
            Utils.setMidnightUpdater(mHandler, mTimeChangesUpdater, mTimeZone)
        }
    }

    // runs every midnight/time changes and refreshes the today icon
    private val mTimeChangesUpdater: Runnable = object : Runnable {
        @Override
        override fun run() {
            mTimeZone = Utils.getTimeZone(this@AllInOneActivity, mHomeTimeUpdater)
            this@AllInOneActivity.invalidateOptionsMenu()
            Utils.setMidnightUpdater(mHandler, this, mTimeZone)
        }
    }

    // Create an observer so that we can update the views whenever a
    // Calendar event changes.
    private val mObserver: ContentObserver = object : ContentObserver(Handler()) {
        @Override
        override fun deliverSelfNotifications(): Boolean {
            return true
        }

        @Override
        override fun onChange(selfChange: Boolean) {
            eventsChanged()
        }
    }

    @Override
    protected override fun onNewIntent(intent: Intent) {
        val action: String? = intent.getAction()
        if (DEBUG) Log.d(TAG, "New intent received " + intent.toString())
        // Don't change the date if we're just returning to the app's home
        if (Intent.ACTION_VIEW.equals(action) &&
            !intent.getBooleanExtra(Utils.INTENT_KEY_HOME, false)
        ) {
            var millis = parseViewAction(intent)
            if (millis == -1L) {
                millis = Utils.timeFromIntentInMillis(intent) as Long
            }
            if (millis != -1L && mViewEventId == -1L && mController != null) {
                val time = Time(mTimeZone)
                time.set(millis)
                time.normalize(true)
                mController?.sendEvent(this as Object?, EventType.GO_TO, time, time, -1,
                    ViewType.CURRENT)
            }
        }
    }

    @Override
    protected override fun onCreate(icicle: Bundle?) {
        super.onCreate(icicle)
        if (icicle != null && icicle.containsKey(BUNDLE_KEY_CHECK_ACCOUNTS)) {
            mCheckForAccounts = icicle.getBoolean(BUNDLE_KEY_CHECK_ACCOUNTS)
        }
        // Launch add google account if this is first time and there are no
        // accounts yet
        if (mCheckForAccounts) {
            mHandler = QueryHandler(this.getContentResolver())
            mHandler?.startQuery(
                0, null, Calendars.CONTENT_URI, arrayOf<String>(
                    Calendars._ID
                ), null, null /* selection args */, null /* sort order */
            )
        }

        // This needs to be created before setContentView
        mController = CalendarController.getInstance(this)

        // Get time from intent or icicle
        var timeMillis: Long = -1
        var viewType = -1
        val intent: Intent = getIntent()
        if (icicle != null) {
            timeMillis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME)
            viewType = icicle.getInt(BUNDLE_KEY_RESTORE_VIEW, -1)
        } else {
            val action: String? = intent.getAction()
            if (Intent.ACTION_VIEW.equals(action)) {
                // Open EventInfo later
                timeMillis = parseViewAction(intent)
            }
            if (timeMillis == -1L) {
                timeMillis = Utils.timeFromIntentInMillis(intent) as Long
            }
        }
        if (viewType == -1 || viewType > ViewType.MAX_VALUE) {
            viewType = Utils.getViewTypeFromIntentAndSharedPref(this)
        }
        mTimeZone = Utils.getTimeZone(this, mHomeTimeUpdater)
        val t = Time(mTimeZone)
        t.set(timeMillis)
        if (DEBUG) {
            if (icicle != null && intent != null) {
                Log.d(
                    TAG,
                    "both, icicle:" + icicle.toString().toString() + "  intent:" + intent.toString()
                )
            } else {
                Log.d(TAG, "not both, icicle:$icicle intent:$intent")
            }
        }
        val res: Resources = getResources()
        mHideString = res.getString(R.string.hide_controls)
        mShowString = res.getString(R.string.show_controls)
        mOrientation = res.getConfiguration().orientation
        if (mOrientation == Configuration.ORIENTATION_LANDSCAPE) {
            mControlsAnimateWidth = res.getDimension(R.dimen.calendar_controls_width).toInt()
            if (mControlsParams == null) {
                mControlsParams = LayoutParams(mControlsAnimateWidth, 0)
            }
            mControlsParams?.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
                as RelativeLayout.LayoutParams
        } else {
            // Make sure width is in between allowed min and max width values
            mControlsAnimateWidth = Math.max(
                res.getDisplayMetrics().widthPixels * 45 / 100,
                res.getDimension(R.dimen.min_portrait_calendar_controls_width).toInt()
            )
            mControlsAnimateWidth = Math.min(
                mControlsAnimateWidth,
                res.getDimension(R.dimen.max_portrait_calendar_controls_width).toInt()
            )
        }
        mControlsAnimateHeight = res?.getDimension(R.dimen.calendar_controls_height).toInt()
        mHideControls = true
        mIsMultipane = Utils.getConfigBool(this, R.bool.multiple_pane_config)
        mIsTabletConfig = Utils.getConfigBool(this, R.bool.tablet_config)
        mShowCalendarControls = Utils.getConfigBool(this, R.bool.show_calendar_controls)
        mShowEventInfoFullScreen = Utils.getConfigBool(this, R.bool.show_event_info_full_screen)
        mCalendarControlsAnimationTime = res.getInteger(R.integer.calendar_controls_animation_time)
        Utils.setAllowWeekForDetailView(mIsMultipane)

        // setContentView must be called before configureActionBar
        setContentView(R.layout.all_in_one)
        if (mIsTabletConfig) {
            mDateRange = findViewById(R.id.date_bar) as TextView?
            mWeekTextView = findViewById(R.id.week_num) as TextView?
        } else {
            mDateRange = getLayoutInflater().inflate(R.layout.date_range_title, null) as TextView
        }

        // configureActionBar auto-selects the first tab you add, so we need to
        // call it before we set up our own fragments to make sure it doesn't
        // overwrite us
        configureActionBar(viewType)
        mHomeTime = findViewById(R.id.home_time) as TextView?
        mMiniMonth = findViewById(R.id.mini_month)
        if (mIsTabletConfig && mOrientation == Configuration.ORIENTATION_PORTRAIT) {
            mMiniMonth?.setLayoutParams(
                LayoutParams(
                    mControlsAnimateWidth,
                    mControlsAnimateHeight
                )
            )
        }
        mCalendarsList = findViewById(R.id.calendar_list)
        mMiniMonthContainer = findViewById(R.id.mini_month_container)
        mSecondaryPane = findViewById(R.id.secondary_pane)

        // Must register as the first activity because this activity can modify
        // the list of event handlers in it's handle method. This affects who
        // the rest of the handlers the controller dispatches to are.
        mController?.registerFirstEventHandler(HANDLER_KEY, this)
        initFragments(timeMillis, viewType, icicle)

        // Listen for changes that would require this to be refreshed
        val prefs: SharedPreferences? = GeneralPreferences.getSharedPreferences(this)
        prefs?.registerOnSharedPreferenceChangeListener(this)
        mContentResolver = getContentResolver()
    }

    private fun parseViewAction(intent: Intent?): Long {
        var timeMillis: Long = -1
        val data: Uri? = intent?.getData()
        if (data != null && data?.isHierarchical()) {
            val path = data.getPathSegments()
            if (path?.size == 2 && path!![0].equals("events")) {
                try {
                    mViewEventId = data.getLastPathSegment()?.toLong() as Long
                    if (mViewEventId != -1L) {
                        mIntentEventStartMillis = intent?.getLongExtra(EXTRA_EVENT_BEGIN_TIME, 0)
                        mIntentEventEndMillis = intent?.getLongExtra(EXTRA_EVENT_END_TIME, 0)
                        mIntentAttendeeResponse = intent?.getIntExtra(
                            ATTENDEE_STATUS, Attendees.ATTENDEE_STATUS_NONE
                        )
                        mIntentAllDay = intent?.getBooleanExtra(EXTRA_EVENT_ALL_DAY, false)
                            as Boolean
                        timeMillis = mIntentEventStartMillis
                    }
                } catch (e: NumberFormatException) {
                    // Ignore if mViewEventId can't be parsed
                }
            }
        }
        return timeMillis
    }

    private fun configureActionBar(viewType: Int) {
        createButtonsSpinner(viewType, mIsTabletConfig)
        if (mIsMultipane) {
            mActionBar?.setDisplayOptions(
                ActionBar.DISPLAY_SHOW_CUSTOM or ActionBar.DISPLAY_SHOW_HOME
            )
        } else {
            mActionBar?.setDisplayOptions(0)
        }
    }

    private fun createButtonsSpinner(viewType: Int, tabletConfig: Boolean) {
        // If tablet configuration , show spinner with no dates
        mActionBarMenuSpinnerAdapter = CalendarViewAdapter(this, viewType, !tabletConfig)
        mActionBar = getActionBar()
        mActionBar?.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST)
        mActionBar?.setListNavigationCallbacks(mActionBarMenuSpinnerAdapter, this)
        when (viewType) {
            ViewType.AGENDA -> {
            }
            ViewType.DAY -> mActionBar?.setSelectedNavigationItem(BUTTON_DAY_INDEX)
            ViewType.WEEK -> mActionBar?.setSelectedNavigationItem(BUTTON_WEEK_INDEX)
            ViewType.MONTH -> mActionBar?.setSelectedNavigationItem(BUTTON_MONTH_INDEX)
            else -> mActionBar?.setSelectedNavigationItem(BUTTON_DAY_INDEX)
        }
    }

    // Clear buttons used in the agenda view
    private fun clearOptionsMenu() {
        if (mOptionsMenu == null) {
            return
        }
        val cancelItem: MenuItem? = mOptionsMenu?.findItem(R.id.action_cancel)
        if (cancelItem != null) {
            cancelItem?.setVisible(false)
        }
    }

    @Override
    protected override fun onResume() {
        super.onResume()

        // Check if the upgrade code has ever been run. If not, force a sync just this one time.
        Utils.trySyncAndDisableUpgradeReceiver(this)

        // Must register as the first activity because this activity can modify
        // the list of event handlers in it's handle method. This affects who
        // the rest of the handlers the controller dispatches to are.
        mController?.registerFirstEventHandler(HANDLER_KEY, this)
        mOnSaveInstanceStateCalled = false
        mContentResolver?.registerContentObserver(
            CalendarContract.Events.CONTENT_URI,
            true, mObserver
        )
        if (mUpdateOnResume) {
            initFragments(mController?.time as Long, mController?.viewType as Int, null)
            mUpdateOnResume = false
        }
        val t = Time(mTimeZone)
        t.set(mController?.time as Long)
        mController?.sendEvent(
            this as Object?, EventType.UPDATE_TITLE, t, t, -1, ViewType.CURRENT,
            mController?.dateFlags as Long, null, null
        )
        // Make sure the drop-down menu will get its date updated at midnight
        if (mActionBarMenuSpinnerAdapter != null) {
            mActionBarMenuSpinnerAdapter?.refresh(this)
        }
        if (mControlsMenu != null) {
            mControlsMenu?.setTitle(if (mHideControls) mShowString else mHideString)
        }
        mPaused = false
        if (mViewEventId != -1L && mIntentEventStartMillis != -1L && mIntentEventEndMillis != -1L) {
            val currentMillis: Long = System.currentTimeMillis()
            var selectedTime: Long = -1
            if (currentMillis > mIntentEventStartMillis && currentMillis < mIntentEventEndMillis) {
                selectedTime = currentMillis
            }
            mController?.sendEventRelatedEventWithExtra(
                this as Object?, EventType.VIEW_EVENT, mViewEventId,
                mIntentEventStartMillis, mIntentEventEndMillis, -1, -1,
                EventInfo.buildViewExtraLong(mIntentAttendeeResponse, mIntentAllDay),
                selectedTime
            )
            mViewEventId = -1
            mIntentEventStartMillis = -1
            mIntentEventEndMillis = -1
            mIntentAllDay = false
        }
        Utils.setMidnightUpdater(mHandler, mTimeChangesUpdater, mTimeZone)
        // Make sure the today icon is up to date
        invalidateOptionsMenu()
    }

    @Override
    protected override fun onPause() {
        super.onPause()
        mController?.deregisterEventHandler(HANDLER_KEY)
        mPaused = true
        mHomeTime?.removeCallbacks(mHomeTimeUpdater)
        if (mActionBarMenuSpinnerAdapter != null) {
            mActionBarMenuSpinnerAdapter?.onPause()
        }
        mContentResolver?.unregisterContentObserver(mObserver)
        if (isFinishing()) {
            // Stop listening for changes that would require this to be refreshed
            val prefs: SharedPreferences? = GeneralPreferences.getSharedPreferences(this)
            prefs?.unregisterOnSharedPreferenceChangeListener(this)
        }
        // FRAG_TODO save highlighted days of the week;
        if (mController?.viewType != ViewType.EDIT) {
            Utils.setDefaultView(this, mController?.viewType as Int)
        }
        Utils.resetMidnightUpdater(mHandler, mTimeChangesUpdater)
    }

    @Override
    protected override fun onUserLeaveHint() {
        mController?.sendEvent(this as Object?, EventType.USER_HOME, null, null, -1,
            ViewType.CURRENT)
        super.onUserLeaveHint()
    }

    @Override
    override fun onSaveInstanceState(outState: Bundle) {
        mOnSaveInstanceStateCalled = true
        super.onSaveInstanceState(outState)
    }

    @Override
    protected override fun onDestroy() {
        super.onDestroy()
        val prefs: SharedPreferences? = GeneralPreferences.getSharedPreferences(this)
        prefs?.unregisterOnSharedPreferenceChangeListener(this)
        mController?.deregisterAllEventHandlers()
        CalendarController.removeInstance(this)
    }

    private fun initFragments(timeMillis: Long, viewType: Int, icicle: Bundle?) {
        if (DEBUG) {
            Log.d(TAG, "Initializing to $timeMillis for view $viewType")
        }
        val ft: FragmentTransaction = getFragmentManager().beginTransaction()
        if (mShowCalendarControls) {
            val miniMonthFrag: Fragment = MonthByWeekFragment(timeMillis, true)
            ft.replace(R.id.mini_month, miniMonthFrag)
            mController?.registerEventHandler(R.id.mini_month, miniMonthFrag as EventHandler)
        }
        if (!mShowCalendarControls || viewType == ViewType.EDIT) {
            mMiniMonth?.setVisibility(View.GONE)
            mCalendarsList?.setVisibility(View.GONE)
        }
        var info: EventInfo? = null
        if (viewType == ViewType.EDIT) {
            mPreviousView = GeneralPreferences.getSharedPreferences(this)?.getInt(
                GeneralPreferences.KEY_START_VIEW, GeneralPreferences.DEFAULT_START_VIEW
            ) as Int
            var eventId: Long = -1
            val intent: Intent = getIntent()
            val data: Uri? = intent.getData()
            if (data != null) {
                try {
                    eventId = data?.getLastPathSegment()?.toLong() as Long
                } catch (e: NumberFormatException) {
                    if (DEBUG) {
                        Log.d(TAG, "Create new event")
                    }
                }
            } else if (icicle != null && icicle.containsKey(BUNDLE_KEY_EVENT_ID)) {
                eventId = icicle.getLong(BUNDLE_KEY_EVENT_ID)
            }
            val begin: Long = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, -1)
            val end: Long = intent.getLongExtra(EXTRA_EVENT_END_TIME, -1)
            info = EventInfo()
            if (end != -1L) {
                info?.endTime = Time()
                info?.endTime?.set(end)
            }
            if (begin != -1L) {
                info?.startTime = Time()
                info?.startTime?.set(begin)
            }
            info.id = eventId
            // We set the viewtype so if the user presses back when they are
            // done editing the controller knows we were in the Edit Event
            // screen. Likewise for eventId
            mController?.viewType = viewType
            mController?.eventId = eventId
        } else {
            mPreviousView = viewType
        }
        setMainPane(ft, R.id.main_pane, viewType, timeMillis, true)
        ft.commit() // this needs to be after setMainPane()
        val t = Time(mTimeZone)
        t.set(timeMillis)
        if (viewType != ViewType.EDIT) {
            mController?.sendEvent(this as Object?, EventType.GO_TO, t, null, -1, viewType)
        }
    }

    @Override
    override fun onBackPressed() {
        if (mCurrentView == ViewType.EDIT || mBackToPreviousView) {
            mController?.sendEvent(this as Object?, EventType.GO_TO, null, null, -1, mPreviousView)
        } else {
            super.onBackPressed()
        }
    }

    @Override
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        super.onCreateOptionsMenu(menu)
        mOptionsMenu = menu
        getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu)

        // Hide the "show/hide controls" button if this is a phone
        // or the view type is "Month".
        mControlsMenu = menu.findItem(R.id.action_hide_controls)
        if (!mShowCalendarControls) {
            if (mControlsMenu != null) {
                mControlsMenu?.setVisible(false)
                mControlsMenu?.setEnabled(false)
            }
        } else if (mControlsMenu != null && mController != null &&
            mController?.viewType == ViewType.MONTH) {
            mControlsMenu?.setVisible(false)
            mControlsMenu?.setEnabled(false)
        } else if (mControlsMenu != null) {
            mControlsMenu?.setTitle(if (mHideControls) mShowString else mHideString)
        }
        val menuItem: MenuItem = menu.findItem(R.id.action_today)
        if (Utils.isJellybeanOrLater()) {
            // replace the default top layer drawable of the today icon with a
            // custom drawable that shows the day of the month of today
            val icon: LayerDrawable = menuItem.getIcon() as LayerDrawable
            Utils.setTodayIcon(icon, this, mTimeZone)
        } else {
            menuItem.setIcon(R.drawable.ic_menu_today_no_date_holo_light)
        }
        return true
    }

    @Override
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        var t: Time? = null
        var viewType: Int = ViewType.CURRENT
        var extras: Long = CalendarController.EXTRA_GOTO_TIME
        val itemId: Int = item.getItemId()
        if (itemId == R.id.action_today) {
            viewType = ViewType.CURRENT
            t = Time(mTimeZone)
            t.setToNow()
            extras = extras or CalendarController.EXTRA_GOTO_TODAY
        } else if (itemId == R.id.action_hide_controls) {
            mHideControls = !mHideControls
            item.setTitle(if (mHideControls) mShowString else mHideString)
            if (!mHideControls) {
                mMiniMonth?.setVisibility(View.VISIBLE)
                mCalendarsList?.setVisibility(View.VISIBLE)
                mMiniMonthContainer?.setVisibility(View.VISIBLE)
            }
            val slideAnimation: ObjectAnimator = ObjectAnimator.ofInt(
                this, "controlsOffset",
                if (mHideControls) 0 else mControlsAnimateWidth,
                if (mHideControls) mControlsAnimateWidth else 0
            )
            slideAnimation.setDuration(mCalendarControlsAnimationTime.toLong())
            ObjectAnimator.setFrameDelay(0)
            slideAnimation.start()
            return true
        } else {
            Log.d(TAG, "Unsupported itemId: $itemId")
            return true
        }
        mController?.sendEvent(this as Object?, EventType.GO_TO, t, null, t, -1,
            viewType, extras, null, null)
        return true
    }

    /**
     * Sets the offset of the controls on the right for animating them off/on
     * screen. ProGuard strips this if it's not in proguard.flags
     *
     * @param controlsOffset The current offset in pixels
     */
    fun setControlsOffset(controlsOffset: Int) {
        if (mOrientation == Configuration.ORIENTATION_LANDSCAPE) {
            mMiniMonth?.setTranslationX(controlsOffset.toFloat())
            mCalendarsList?.setTranslationX(controlsOffset.toFloat())
            mControlsParams?.width = Math.max(0, mControlsAnimateWidth - controlsOffset)
            mMiniMonthContainer?.setLayoutParams(mControlsParams)
        } else {
            mMiniMonth?.setTranslationY(controlsOffset.toFloat())
            mCalendarsList?.setTranslationY(controlsOffset.toFloat())
            if (mVerticalControlsParams == null) {
                mVerticalControlsParams = LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, mControlsAnimateHeight
                ) as LinearLayout.LayoutParams?
            }
            mVerticalControlsParams?.height = Math.max(0, mControlsAnimateHeight - controlsOffset)
            mMiniMonthContainer?.setLayoutParams(mVerticalControlsParams)
        }
    }

    @Override
    override fun onSharedPreferenceChanged(prefs: SharedPreferences?, key: String) {
        if (key.equals(GeneralPreferences.KEY_WEEK_START_DAY)) {
            if (mPaused) {
                mUpdateOnResume = true
            } else {
                initFragments(mController?.time as Long, mController?.viewType as Int, null)
            }
        }
    }

    private fun setMainPane(
        ft: FragmentTransaction?,
        viewId: Int,
        viewType: Int,
        timeMillis: Long,
        force: Boolean
    ) {
        var ft: FragmentTransaction? = ft
        if (mOnSaveInstanceStateCalled) {
            return
        }
        if (!force && mCurrentView == viewType) {
            return
        }

        // Remove this when transition to and from month view looks fine.
        val doTransition = viewType != ViewType.MONTH && mCurrentView != ViewType.MONTH
        val fragmentManager: FragmentManager = getFragmentManager()
        if (viewType != mCurrentView) {
            // The rules for this previous view are different than the
            // controller's and are used for intercepting the back button.
            if (mCurrentView != ViewType.EDIT && mCurrentView > 0) {
                mPreviousView = mCurrentView
            }
            mCurrentView = viewType
        }
        // Create new fragment
        var frag: Fragment? = null
        val secFrag: Fragment? = null
        when (viewType) {
            ViewType.AGENDA -> {
            }
            ViewType.DAY -> {
                if (mActionBar != null && mActionBar?.getSelectedTab() != mDayTab) {
                    mActionBar?.selectTab(mDayTab)
                }
                if (mActionBarMenuSpinnerAdapter != null) {
                    mActionBar?.setSelectedNavigationItem(CalendarViewAdapter.DAY_BUTTON_INDEX)
                }
                frag = DayFragment(timeMillis, 1)
            }
            ViewType.MONTH -> {
                if (mActionBar != null && mActionBar?.getSelectedTab() != mMonthTab) {
                    mActionBar?.selectTab(mMonthTab)
                }
                if (mActionBarMenuSpinnerAdapter != null) {
                    mActionBar?.setSelectedNavigationItem(CalendarViewAdapter.MONTH_BUTTON_INDEX)
                }
                frag = MonthByWeekFragment(timeMillis, false)
            }
            ViewType.WEEK -> {
                if (mActionBar != null && mActionBar?.getSelectedTab() != mWeekTab) {
                    mActionBar?.selectTab(mWeekTab)
                }
                if (mActionBarMenuSpinnerAdapter != null) {
                    mActionBar?.setSelectedNavigationItem(CalendarViewAdapter.WEEK_BUTTON_INDEX)
                }
                frag = DayFragment(timeMillis, 7)
            }
            else -> {
                if (mActionBar != null && mActionBar?.getSelectedTab() != mWeekTab) {
                    mActionBar?.selectTab(mWeekTab)
                }
                if (mActionBarMenuSpinnerAdapter != null) {
                    mActionBar?.setSelectedNavigationItem(CalendarViewAdapter.WEEK_BUTTON_INDEX)
                }
                frag = DayFragment(timeMillis, 7)
            }
        }

        // Update the current view so that the menu can update its look according to the
        // current view.
        if (mActionBarMenuSpinnerAdapter != null) {
            mActionBarMenuSpinnerAdapter?.setMainView(viewType)
            if (!mIsTabletConfig) {
                mActionBarMenuSpinnerAdapter?.setTime(timeMillis)
            }
        }

        // Show date only on tablet configurations in views different than Agenda
        if (!mIsTabletConfig) {
            mDateRange?.setVisibility(View.GONE)
        } else {
            mDateRange?.setVisibility(View.GONE)
        }

        // Clear unnecessary buttons from the option menu when switching from the agenda view
        if (viewType != ViewType.AGENDA) {
            clearOptionsMenu()
        }
        var doCommit = false
        if (ft == null) {
            doCommit = true
            ft = fragmentManager.beginTransaction()
        }
        if (doTransition) {
            ft?.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
        }
        ft?.replace(viewId, frag)
        if (DEBUG) {
            Log.d(TAG, "Adding handler with viewId $viewId and type $viewType")
        }
        // If the key is already registered this will replace it
        mController?.registerEventHandler(viewId, frag as EventHandler?)
        if (doCommit) {
            if (DEBUG) {
                Log.d(TAG, "setMainPane AllInOne=" + this + " finishing:" + this.isFinishing())
            }
            ft?.commit()
        }
    }

    private fun setTitleInActionBar(event: EventInfo) {
        if (event.eventType != EventType.UPDATE_TITLE || mActionBar == null) {
            return
        }
        val start: Long? = event?.startTime?.toMillis(false /* use isDst */)
        val end: Long?
        end = if (event.endTime != null) {
            event?.endTime?.toMillis(false /* use isDst */)
        } else {
            start
        }
        val msg: String? = Utils.formatDateRange(this,
            start as Long,
            end as Long,
            event.extraLong.toInt()
        )
        val oldDate: CharSequence? = mDateRange?.getText()
        mDateRange?.setText(msg)
        updateSecondaryTitleFields(if (event?.selectedTime != null)
            event?.selectedTime?.toMillis(true) as Long else start)
        if (!TextUtils.equals(oldDate, msg)) {
            mDateRange?.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)
            if (mShowWeekNum && mWeekTextView != null) {
                mWeekTextView?.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)
            }
        }
    }

    private fun updateSecondaryTitleFields(visibleMillisSinceEpoch: Long) {
        mShowWeekNum = Utils.getShowWeekNumber(this)
        mTimeZone = Utils.getTimeZone(this, mHomeTimeUpdater)
        if (visibleMillisSinceEpoch != -1L) {
            val weekNum: Int = Utils.getWeekNumberFromTime(visibleMillisSinceEpoch, this)
            mWeekNum = weekNum
        }
        if (mShowWeekNum && mCurrentView == ViewType.WEEK && mIsTabletConfig &&
            mWeekTextView != null
        ) {
            val weekString: String = getResources().getQuantityString(
                R.plurals.weekN, mWeekNum,
                mWeekNum
            )
            mWeekTextView?.setText(weekString)
            mWeekTextView?.setVisibility(View.VISIBLE)
        } else if (visibleMillisSinceEpoch != -1L && mWeekTextView != null &&
            mCurrentView == ViewType.DAY && mIsTabletConfig) {
            val time = Time(mTimeZone)
            time.set(visibleMillisSinceEpoch)
            val julianDay: Int = Time.getJulianDay(visibleMillisSinceEpoch, time.gmtoff)
            time.setToNow()
            val todayJulianDay: Int = Time.getJulianDay(time.toMillis(false), time.gmtoff)
            val dayString: String = Utils.getDayOfWeekString(
                julianDay,
                todayJulianDay,
                visibleMillisSinceEpoch,
                this
            )
            mWeekTextView?.setText(dayString)
            mWeekTextView?.setVisibility(View.VISIBLE)
        } else if (mWeekTextView != null && (!mIsTabletConfig || mCurrentView != ViewType.DAY)) {
            mWeekTextView?.setVisibility(View.GONE)
        }
        if (mHomeTime != null && (mCurrentView == ViewType.DAY || mCurrentView == ViewType.WEEK) &&
            !TextUtils.equals(mTimeZone, Time.getCurrentTimezone())
        ) {
            val time = Time(mTimeZone)
            time.setToNow()
            val millis: Long = time.toMillis(true)
            val isDST = time.isDst !== 0
            var flags: Int = DateUtils.FORMAT_SHOW_TIME
            if (DateFormat.is24HourFormat(this)) {
                flags = flags or DateUtils.FORMAT_24HOUR
            }
            // Formats the time as
            val timeString: String = StringBuilder(
                Utils.formatDateRange(this, millis, millis, flags)
            ).append(" ").append(
                TimeZone.getTimeZone(mTimeZone).getDisplayName(
                    isDST, TimeZone.SHORT, Locale.getDefault()
                )
            ).toString()
            mHomeTime?.setText(timeString)
            mHomeTime?.setVisibility(View.VISIBLE)
            // Update when the minute changes
            mHomeTime?.removeCallbacks(mHomeTimeUpdater)
            mHomeTime?.postDelayed(
                mHomeTimeUpdater,
                DateUtils.MINUTE_IN_MILLIS - millis % DateUtils.MINUTE_IN_MILLIS
            )
        } else if (mHomeTime != null) {
            mHomeTime?.setVisibility(View.GONE)
        }
    }

    @get:Override override val supportedEventTypes: Long
        get() = EventType.GO_TO or EventType.UPDATE_TITLE

    @Override
    override fun handleEvent(event: EventInfo?) {
        var displayTime: Long = -1
        if (event?.eventType == EventType.GO_TO) {
            if (event?.extraLong and CalendarController.EXTRA_GOTO_BACK_TO_PREVIOUS != 0L) {
                mBackToPreviousView = true
            } else if (event?.viewType != mController?.previousViewType &&
                event?.viewType != ViewType.EDIT
            ) {
                // Clear the flag is change to a different view type
                mBackToPreviousView = false
            }
            setMainPane(
                null, R.id.main_pane, event?.viewType, event?.startTime?.toMillis(false)
                    as Long, false
            )
            if (mShowCalendarControls) {
                val animationSize =
                    if (mOrientation == Configuration.ORIENTATION_LANDSCAPE) mControlsAnimateWidth
                    else mControlsAnimateHeight
                val noControlsView = event?.viewType == ViewType.MONTH
                if (mControlsMenu != null) {
                    mControlsMenu?.setVisible(!noControlsView)
                    mControlsMenu?.setEnabled(!noControlsView)
                }
                if (noControlsView || mHideControls) {
                    // hide minimonth and calendar frag
                    mShowSideViews = false
                    if (!mHideControls) {
                        val slideAnimation: ObjectAnimator = ObjectAnimator.ofInt(
                            this,
                            "controlsOffset", 0, animationSize
                        )
                        slideAnimation.addListener(mSlideAnimationDoneListener)
                        slideAnimation.setDuration(mCalendarControlsAnimationTime.toLong())
                        ObjectAnimator.setFrameDelay(0)
                        slideAnimation.start()
                    } else {
                        mMiniMonth?.setVisibility(View.GONE)
                        mCalendarsList?.setVisibility(View.GONE)
                        mMiniMonthContainer?.setVisibility(View.GONE)
                    }
                } else {
                    // show minimonth and calendar frag
                    mShowSideViews = true
                    mMiniMonth?.setVisibility(View.VISIBLE)
                    mCalendarsList?.setVisibility(View.VISIBLE)
                    mMiniMonthContainer?.setVisibility(View.VISIBLE)
                    if (!mHideControls &&
                        mController?.previousViewType == ViewType.MONTH
                    ) {
                        val slideAnimation: ObjectAnimator = ObjectAnimator.ofInt(
                            this,
                            "controlsOffset", animationSize, 0
                        )
                        slideAnimation.setDuration(mCalendarControlsAnimationTime.toLong())
                        ObjectAnimator.setFrameDelay(0)
                        slideAnimation.start()
                    }
                }
            }
            displayTime =
                if (event?.selectedTime != null) event?.selectedTime?.toMillis(true) as Long
                else event?.startTime?.toMillis(true) as Long
            if (!mIsTabletConfig) {
                mActionBarMenuSpinnerAdapter?.setTime(displayTime)
            }
        } else if (event?.eventType == EventType.UPDATE_TITLE) {
            setTitleInActionBar(event as CalendarController.EventInfo)
            if (!mIsTabletConfig) {
                mActionBarMenuSpinnerAdapter?.setTime(mController?.time as Long)
            }
        }
        updateSecondaryTitleFields(displayTime)
    }

    @Override
    override fun eventsChanged() {
        mController?.sendEvent(this as Object?, EventType.EVENTS_CHANGED, null, null, -1,
            ViewType.CURRENT)
    }

    @Override
    override fun onTabSelected(tab: Tab?, ft: FragmentTransaction?) {
        Log.w(TAG, "TabSelected AllInOne=" + this + " finishing:" + this.isFinishing())
        if (tab == mDayTab && mCurrentView != ViewType.DAY) {
            mController?.sendEvent(this as Object?, EventType.GO_TO, null, null, -1, ViewType.DAY)
        } else if (tab == mWeekTab && mCurrentView != ViewType.WEEK) {
            mController?.sendEvent(this as Object?, EventType.GO_TO, null, null, -1, ViewType.WEEK)
        } else if (tab == mMonthTab && mCurrentView != ViewType.MONTH) {
            mController?.sendEvent(this as Object?, EventType.GO_TO, null, null, -1, ViewType.MONTH)
        } else {
            Log.w(
                TAG, "TabSelected event from unknown tab: " +
                    if (tab == null) "null" else tab.getText()
            )
            Log.w(
                TAG, "CurrentView:" + mCurrentView + " Tab:" + tab.toString() + " Day:" + mDayTab +
                    " Week:" + mWeekTab + " Month:" + mMonthTab
            )
        }
    }

    @Override
    override fun onTabReselected(tab: Tab?, ft: FragmentTransaction?) {
    }

    @Override
    override fun onTabUnselected(tab: Tab?, ft: FragmentTransaction?) {
    }

    @Override
    override fun onNavigationItemSelected(itemPosition: Int, itemId: Long): Boolean {
        when (itemPosition) {
            CalendarViewAdapter.DAY_BUTTON_INDEX -> if (mCurrentView != ViewType.DAY) {
                mController?.sendEvent(this as Object?, EventType.GO_TO, null, null, -1,
                    ViewType.DAY)
            }
            CalendarViewAdapter.WEEK_BUTTON_INDEX -> if (mCurrentView != ViewType.WEEK) {
                mController?.sendEvent(this as Object?, EventType.GO_TO, null, null, -1,
                    ViewType.WEEK)
            }
            CalendarViewAdapter.MONTH_BUTTON_INDEX -> if (mCurrentView != ViewType.MONTH) {
                mController?.sendEvent(this as Object?, EventType.GO_TO, null, null, -1,
                    ViewType.MONTH)
            }
            CalendarViewAdapter.AGENDA_BUTTON_INDEX -> {
            }
            else -> {
                Log.w(TAG, "ItemSelected event from unknown button: $itemPosition")
                Log.w(
                    TAG, "CurrentView:" + mCurrentView + " Button:" + itemPosition +
                        " Day:" + mDayTab + " Week:" + mWeekTab + " Month:" + mMonthTab
                )
            }
        }
        return false
    }

    companion object {
        private const val TAG = "AllInOneActivity"
        private const val DEBUG = false
        private const val EVENT_INFO_FRAGMENT_TAG = "EventInfoFragment"
        private const val BUNDLE_KEY_RESTORE_TIME = "key_restore_time"
        private const val BUNDLE_KEY_EVENT_ID = "key_event_id"
        private const val BUNDLE_KEY_RESTORE_VIEW = "key_restore_view"
        private const val BUNDLE_KEY_CHECK_ACCOUNTS = "key_check_for_accounts"
        private const val HANDLER_KEY = 0

        // Indices of buttons for the drop down menu (tabs replacement)
        // Must match the strings in the array buttons_list in arrays.xml and the
        // OnNavigationListener
        private const val BUTTON_DAY_INDEX = 0
        private const val BUTTON_WEEK_INDEX = 1
        private const val BUTTON_MONTH_INDEX = 2
        private const val BUTTON_AGENDA_INDEX = 3
        private var mIsMultipane = false
        private var mIsTabletConfig = false
    }
}