diff options
author | Damian Patel <damianpatel@google.com> | 2021-07-09 20:21:26 +0000 |
---|---|---|
committer | Damian Patel <damianpatel@google.com> | 2021-07-09 20:52:04 +0000 |
commit | 83dac0c872c57ee8ebe066de2e4e0ff808e8aa54 (patch) | |
tree | ff09549178a3a5107cf0a51547eaceab7d139820 | |
parent | 83732c13ab8f01ceb3ffa9a7990d773c9abb28e2 (diff) | |
download | Calendar-83dac0c872c57ee8ebe066de2e4e0ff808e8aa54.tar.gz |
AOSP/Calendar - MonthByWeekAdapter and SimpleWeeksAdapter
fully converted with bp file
This is the fully converted and touched-up Kotlin file
after the converter was run once. A few other files had
to be tweaked in order ensure all files within the month
packages worked together properly. For instance, a few
variables in SimpleWeekView.kt had to have their visibility
modifier be changed to "internal" rather than "protected".
This is because the "protected" modifier in Kotlin means
only subclasses have visiblity. Otherwise, it is identical
to the "private" modifier. The "internal' modifier more
closely resembles "protected" since it is package-private
while subclasses still have access to members. I also
tried to move away from the "Integer" type and replace it
with "Int?" since it is not proper practice according to
Kotlin docs to use "Integer" as a type since it is deprecated.
Moreover, various functions in SimpleWeeksAdapter.kt had to
have the "open" modifier added since these functions were
being overridden in MonthByWeekAdapter.kt.
Test: manual - build tests, unit tests, and
CTS tests were conducted using the following
commands:
$ make Calendar
$ make CalendarTests -j
$ adb install -r -t -d out/target/product/bonito/
testcases/CalendarTests/arm64/CalendarTests.apk
Success
$ adb shell am instrument -w com.android.calendar.tests
com.android.calendar.FormatDateRangeTest:.
com.android.calendar.UtilsTests:..................
com.android.calendar.WeekNumberTest:.
com.android.calendar.widget.CalendarAppWidgetServiceTest:..
Test results for InstrumentationTestRunner=..........
Time: 0.141
OK (22 tests)
General CTS Tests:
$ tools/cts-tradefed
cts-tf > run cts -m CtsProviderTestCases
================= Results ==================
=============== Consumed Time ==============
arm64-v8a CtsProviderTestCases: 7m 48s
Total aggregated tests run time: 7m 48s
============== TOP 1 Slow Modules ==============
arm64-v8a CtsProviderTestCases: 0.80 tests/sec [376 tests / 468191 msec]
============== Modules Preparation Times ==============
arm64-v8a CtsProviderTestCases => prep = 9130 ms || clean = 2088 ms
Total preparation time: 9s || Total tear down time: 2s
=======================================================
=============== Summary ===============
Total Run time: 9m 30s
1/1 modules completed
Total Tests : 376
PASSED : 362
FAILED : 0
IGNORED : 6
ASSUMPTION_FAILURE: 8
============== End of Results ==============
Change-Id: Ia0fa3cebf53983c3915ed668c9a2f7a570826738
-rw-r--r-- | Android.bp | 2 | ||||
-rw-r--r-- | src/com/android/calendar/month/MonthByWeekAdapter.kt | 139 | ||||
-rw-r--r-- | src/com/android/calendar/month/MonthByWeekFragment.kt | 32 | ||||
-rw-r--r-- | src/com/android/calendar/month/SimpleDayPickerFragment.kt | 33 | ||||
-rw-r--r-- | src/com/android/calendar/month/SimpleWeekView.kt | 2 | ||||
-rw-r--r-- | src/com/android/calendar/month/SimpleWeeksAdapter.kt | 112 |
6 files changed, 174 insertions, 146 deletions
@@ -27,7 +27,9 @@ exclude_srcsd = [ "src/**/calendar/alerts/InitAlarmsService.java", "src/**/calendar/alerts/NotificationMgr.java", "src/**/calendar/alerts/QuickResponseActivity.java", + "src/**/calendar/month/MonthByWeekAdapter.java", "src/**/calendar/month/MonthByWeekFragment.java", + "src/**/calendar/month/SimpleWeeksAdapter.java", "src/**/calendar/widget/CalendarAppWidgetModel.java", "src/**/calendar/widget/CalendarAppWidgetProvider.java", "src/**/calendar/widget/CalendarAppWidgetService.java", diff --git a/src/com/android/calendar/month/MonthByWeekAdapter.kt b/src/com/android/calendar/month/MonthByWeekAdapter.kt index a0ae1b1b..da617950 100644 --- a/src/com/android/calendar/month/MonthByWeekAdapter.kt +++ b/src/com/android/calendar/month/MonthByWeekAdapter.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 The Android Open Source Project + * 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. @@ -37,8 +37,8 @@ import com.android.calendar.Utils import java.util.ArrayList import java.util.HashMap -class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) : - SimpleWeeksAdapter(context, params) { +class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Int?>) : + SimpleWeeksAdapter(context as Context, params) { protected var mController: CalendarController? = null protected var mHomeTimeZone: String? = null protected var mTempTime: Time? = null @@ -56,10 +56,8 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) var mClickedView: MonthWeekEventsView? = null var mSingleTapUpView: MonthWeekEventsView? = null var mLongClickedView: MonthWeekEventsView? = null - var mClickedXLocation // Used to find which day was clicked - = 0f - var mClickTime // Used to calculate minimum click animation time - : Long = 0 + var mClickedXLocation = 0f // Used to find which day was clicked + var mClickTime: Long = 0 // Used to calculate minimum click animation time fun animateToday() { mAnimateToday = true @@ -67,31 +65,31 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) } @Override - protected fun init() { + protected override fun init() { super.init() mGestureDetector = GestureDetector(mContext, CalendarGestureListener()) mController = CalendarController.getInstance(mContext) mHomeTimeZone = Utils.getTimeZone(mContext, null) - mSelectedDay.switchTimezone(mHomeTimeZone) + mSelectedDay?.switchTimezone(mHomeTimeZone) mToday = Time(mHomeTimeZone) - mToday.setToNow() + mToday?.setToNow() mTempTime = Time(mHomeTimeZone) } private fun updateTimeZones() { - mSelectedDay.timezone = mHomeTimeZone - mSelectedDay.normalize(true) - mToday.timezone = mHomeTimeZone - mToday.setToNow() - mTempTime.switchTimezone(mHomeTimeZone) + mSelectedDay!!.timezone = mHomeTimeZone + mSelectedDay?.normalize(true) + mToday!!.timezone = mHomeTimeZone + mToday?.setToNow() + mTempTime?.switchTimezone(mHomeTimeZone) } @Override - fun setSelectedDay(selectedTime: Time?) { - mSelectedDay.set(selectedTime) - val millis: Long = mSelectedDay.normalize(true) + override fun setSelectedDay(selectedTime: Time?) { + mSelectedDay?.set(selectedTime) + val millis: Long = mSelectedDay!!.normalize(true) mSelectedWeek = Utils.getWeeksSinceEpochFromJulianDay( - Time.getJulianDay(millis, mSelectedDay.gmtoff), mFirstDayOfWeek + Time.getJulianDay(millis, mSelectedDay!!.gmtoff), mFirstDayOfWeek ) notifyDataSetChanged() } @@ -100,8 +98,8 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) if (mIsMiniMonth) { if (Log.isLoggable(TAG, Log.ERROR)) { Log.e( - TAG, "Attempted to set events for mini view. Events only supported in full" - + " view." + TAG, "Attempted to set events for mini view. Events only supported in full" + + " view." ) } return @@ -115,7 +113,7 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) for (i in 0 until numDays) { eventDayList.add(ArrayList<Event>()) } - if (events == null || events.size() === 0) { + if (events == null || events.size == 0) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "No events. Returning early--go schedule something fun.") } @@ -147,7 +145,7 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) } } if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Processed " + events.size().toString() + " events.") + Log.d(TAG, "Processed " + events.size.toString() + " events.") } mEventDayList = eventDayList refresh() @@ -155,7 +153,7 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) @SuppressWarnings("unchecked") @Override - fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { if (mIsMiniMonth) { return super.getView(position, convertView, parent) } @@ -163,13 +161,13 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) val params = LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ) - var drawingParams: HashMap<String?, Integer?>? = null + var drawingParams: HashMap<String?, Int?>? = null var isAnimatingToday = false if (convertView != null) { v = convertView as MonthWeekEventsView // Checking updateToday uses the current params instead of the new // params, so this is assuming the view is relatively stable - if (mAnimateToday && v.updateToday(mSelectedDay.timezone)) { + if (mAnimateToday && v.updateToday(mSelectedDay!!.timezone)) { val currentTime: Long = System.currentTimeMillis() // If it's been too long since we tried to start the animation // don't show it. This can happen if the user stops a scroll @@ -184,13 +182,13 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) v = MonthWeekEventsView(mContext) } } else { - drawingParams = v.getTag() as HashMap<String?, Integer?> + drawingParams = v.getTag() as HashMap<String?, Int?> } } else { v = MonthWeekEventsView(mContext) } if (drawingParams == null) { - drawingParams = HashMap<String, Integer>() + drawingParams = HashMap<String?, Int?>() } drawingParams.clear() v.setLayoutParams(params) @@ -198,7 +196,7 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) v.setOnTouchListener(this) var selectedDay = -1 if (mSelectedWeek === position) { - selectedDay = mSelectedDay.weekDay + selectedDay = mSelectedDay!!.weekDay } drawingParams.put( SimpleWeekView.VIEW_PARAMS_HEIGHT, @@ -215,12 +213,12 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) drawingParams.put(MonthWeekEventsView.VIEW_PARAMS_ANIMATE_TODAY, 1) mAnimateToday = false } - v.setWeekParams(drawingParams, mSelectedDay.timezone) + v.setWeekParams(drawingParams, mSelectedDay!!.timezone) return v } @Override - protected fun refresh() { + internal override fun refresh() { mFirstDayOfWeek = Utils.getFirstDayOfWeek(mContext) mShowWeekNumber = Utils.getShowWeekNumber(mContext) mHomeTimeZone = Utils.getTimeZone(mContext, null) @@ -230,21 +228,21 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) } @Override - protected fun onDayTapped(day: Time) { + protected override fun onDayTapped(day: Time) { setDayParameters(day) if (mShowAgendaWithMonth || mIsMiniMonth) { // If agenda view is visible with month view , refresh the views // with the selected day's info - mController.sendEvent( - mContext, EventType.GO_TO, day, day, -1, + mController?.sendEvent( + mContext as Object?, EventType.GO_TO, day, day, -1, ViewType.CURRENT, CalendarController.EXTRA_GOTO_DATE, null, null ) } else { // Else , switch to the detailed view - mController.sendEvent( - mContext, EventType.GO_TO, day, day, -1, + mController?.sendEvent( + mContext as Object?, EventType.GO_TO, day, day, -1, ViewType.DETAIL, CalendarController.EXTRA_GOTO_DATE - or CalendarController.EXTRA_GOTO_BACK_TO_PREVIOUS, null, null + or CalendarController.EXTRA_GOTO_BACK_TO_PREVIOUS, null, null ) } } @@ -252,7 +250,7 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) private fun setDayParameters(day: Time) { day.timezone = mHomeTimeZone val currTime = Time(mHomeTimeZone) - currTime.set(mController.getTime()) + currTime.set(mController!!.time as Long) day.hour = currTime.hour day.minute = currTime.minute day.allDay = false @@ -260,19 +258,19 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) } @Override - fun onTouch(v: View?, event: MotionEvent): Boolean { + override fun onTouch(v: View, event: MotionEvent): Boolean { if (v !is MonthWeekEventsView) { return super.onTouch(v, event) } - val action: Int = event.getAction() + val action: Int = event!!.getAction() // Event was tapped - switch to the detailed view making sure the click animation // is done first. - if (mGestureDetector.onTouchEvent(event)) { + if (mGestureDetector!!.onTouchEvent(event)) { mSingleTapUpView = v as MonthWeekEventsView? val delay: Long = System.currentTimeMillis() - mClickTime // Make sure the animation is visible for at least mOnTapDelay - mOnDownDelay ms - mListView.postDelayed( + mListView?.postDelayed( mDoSingleTapUp, if (delay > mTotalClickDelay) 0 else mTotalClickDelay - delay ) @@ -285,12 +283,14 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) mClickedView = v as MonthWeekEventsView mClickedXLocation = event.getX() mClickTime = System.currentTimeMillis() - mListView.postDelayed(mDoClick, mOnDownDelay) + mListView?.postDelayed(mDoClick, mOnDownDelay.toLong()) } - MotionEvent.ACTION_UP, MotionEvent.ACTION_SCROLL, MotionEvent.ACTION_CANCEL -> clearClickedView( + MotionEvent.ACTION_UP, MotionEvent.ACTION_SCROLL, MotionEvent.ACTION_CANCEL -> + clearClickedView( v as MonthWeekEventsView? ) - MotionEvent.ACTION_MOVE -> // No need to cancel on vertical movement, ACTION_SCROLL will do that. + MotionEvent.ACTION_MOVE -> // No need to cancel on vertical movement, + // ACTION_SCROLL will do that. if (Math.abs(event.getX() - mClickedXLocation) > mMovedPixelToCancel) { clearClickedView(v as MonthWeekEventsView?) } @@ -308,20 +308,20 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) */ protected inner class CalendarGestureListener : GestureDetector.SimpleOnGestureListener() { @Override - fun onSingleTapUp(e: MotionEvent?): Boolean { + override fun onSingleTapUp(e: MotionEvent?): Boolean { return true } @Override - fun onLongPress(e: MotionEvent?) { + override fun onLongPress(e: MotionEvent?) { if (mLongClickedView != null) { - val day: Time = mLongClickedView.getDayFromLocation(mClickedXLocation) + val day: Time? = mLongClickedView?.getDayFromLocation(mClickedXLocation) if (day != null) { - mLongClickedView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) + mLongClickedView?.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) val message = Message() message.obj = day } - mLongClickedView.clearClickedDay() + mLongClickedView?.clearClickedDay() mLongClickedView = null } } @@ -329,38 +329,40 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) // Clear the visual cues of the click animation and related running code. private fun clearClickedView(v: MonthWeekEventsView?) { - mListView.removeCallbacks(mDoClick) - synchronized(v) { v.clearClickedDay() } + mListView?.removeCallbacks(mDoClick) + synchronized(v as Any) { v?.clearClickedDay() } mClickedView = null } // Perform the tap animation in a runnable to allow a delay before showing the tap color. // This is done to prevent a click animation when a fling is done. - private val mDoClick: Runnable = object : Runnable() { + private val mDoClick: Runnable = object : Runnable { @Override - fun run() { + override fun run() { if (mClickedView != null) { - synchronized(mClickedView) { mClickedView.setClickedDay(mClickedXLocation) } + synchronized(mClickedView as MonthWeekEventsView) { + mClickedView?.setClickedDay(mClickedXLocation) } mLongClickedView = mClickedView mClickedView = null // This is a workaround , sometimes the top item on the listview doesn't refresh on // invalidate, so this forces a re-draw. - mListView.invalidate() + mListView?.invalidate() } } } // Performs the single tap operation: go to the tapped day. // This is done in a runnable to allow the click animation to finish before switching views - private val mDoSingleTapUp: Runnable = object : Runnable() { + private val mDoSingleTapUp: Runnable = object : Runnable { @Override - fun run() { + override fun run() { if (mSingleTapUpView != null) { - val day: Time = mSingleTapUpView.getDayFromLocation(mClickedXLocation) + val day: Time? = mSingleTapUpView?.getDayFromLocation(mClickedXLocation) if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d( TAG, - "Touched day at Row=" + mSingleTapUpView.mWeek.toString() + " day=" + day.toString() + "Touched day at Row=" + mSingleTapUpView?.mWeek?.toString() + + " day=" + day?.toString() ) } if (day != null) { @@ -381,23 +383,24 @@ class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) // Used to insure minimal time for seeing the click animation before switching views private const val mOnTapDelay = 100 - // Minimal time for a down touch action before stating the click animation, this insures that - // there is no click animation on flings - private var mOnDownDelay: Int - private var mTotalClickDelay: Int + // Minimal time for a down touch action before stating the click animation, this ensures + // that there is no click animation on flings + private var mOnDownDelay: Int = 0 + private var mTotalClickDelay: Int = 0 // Minimal distance to move the finger in order to cancel the click animation - private var mMovedPixelToCancel: Float + private var mMovedPixelToCancel: Float = 0f } init { if (params.containsKey(WEEK_PARAMS_IS_MINI)) { - mIsMiniMonth = params.get(WEEK_PARAMS_IS_MINI) !== 0 + mIsMiniMonth = params.get(WEEK_PARAMS_IS_MINI) != 0 } - mShowAgendaWithMonth = Utils.getConfigBool(context, R.bool.show_agenda_with_month) + mShowAgendaWithMonth = Utils.getConfigBool(context as Context, + R.bool.show_agenda_with_month) val vc: ViewConfiguration = ViewConfiguration.get(context) mOnDownDelay = ViewConfiguration.getTapTimeout() - mMovedPixelToCancel = vc.getScaledTouchSlop() + mMovedPixelToCancel = vc.getScaledTouchSlop().toFloat() mTotalClickDelay = mOnDownDelay + mOnTapDelay } }
\ No newline at end of file diff --git a/src/com/android/calendar/month/MonthByWeekFragment.kt b/src/com/android/calendar/month/MonthByWeekFragment.kt index 7c1eec9a..9fe9fe49 100644 --- a/src/com/android/calendar/month/MonthByWeekFragment.kt +++ b/src/com/android/calendar/month/MonthByWeekFragment.kt @@ -53,8 +53,8 @@ import java.util.Calendar import java.util.HashMap class MonthByWeekFragment @JvmOverloads constructor( - initialTime: Long = System.currentTimeMillis(), - protected var mIsMiniMonth: Boolean = true + initialTime: Long = System.currentTimeMillis(), + protected var mIsMiniMonth: Boolean = true ) : SimpleDayPickerFragment(initialTime), CalendarController.EventHandler, LoaderManager.LoaderCallbacks<Cursor?>, OnScrollListener, OnTouchListener { protected var mMinimumTwoMonthFlingVelocity = 0f @@ -221,18 +221,18 @@ class MonthByWeekFragment @JvmOverloads constructor( protected override fun setUpAdapter() { mFirstDayOfWeek = Utils.getFirstDayOfWeek(mContext) mShowWeekNumber = Utils.getShowWeekNumber(mContext) - val weekParams: HashMap<String, Int> = HashMap<String, Int>() - weekParams.put(SimpleWeeksAdapter.WEEK_PARAMS_NUM_WEEKS, mNumWeeks) - weekParams.put(SimpleWeeksAdapter.WEEK_PARAMS_SHOW_WEEK, if (mShowWeekNumber) 1 else 0) - weekParams.put(SimpleWeeksAdapter.WEEK_PARAMS_WEEK_START, mFirstDayOfWeek) - weekParams.put(MonthByWeekAdapter.WEEK_PARAMS_IS_MINI, if (mIsMiniMonth) 1 else 0) - weekParams.put( + val weekParams = HashMap<String?, Int?>() + weekParams?.put(SimpleWeeksAdapter.WEEK_PARAMS_NUM_WEEKS, mNumWeeks) + weekParams?.put(SimpleWeeksAdapter.WEEK_PARAMS_SHOW_WEEK, if (mShowWeekNumber) 1 else 0) + weekParams?.put(SimpleWeeksAdapter.WEEK_PARAMS_WEEK_START, mFirstDayOfWeek) + weekParams?.put(MonthByWeekAdapter.WEEK_PARAMS_IS_MINI, if (mIsMiniMonth) 1 else 0) + weekParams?.put( SimpleWeeksAdapter.WEEK_PARAMS_JULIAN_DAY, Time.getJulianDay(mSelectedDay.toMillis(true), mSelectedDay.gmtoff) ) weekParams.put(SimpleWeeksAdapter.WEEK_PARAMS_DAYS_PER_WEEK, mDaysPerWeek) if (mAdapter == null) { - mAdapter = MonthByWeekAdapter(getActivity(), weekParams) + mAdapter = MonthByWeekAdapter(getActivity(), weekParams) as SimpleWeeksAdapter? mAdapter?.registerDataSetObserver(mObserver) } else { mAdapter?.updateParams(weekParams) @@ -242,9 +242,9 @@ class MonthByWeekFragment @JvmOverloads constructor( @Override override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? ): View { val v: View v = if (mIsMiniMonth) { @@ -338,7 +338,8 @@ class MonthByWeekFragment @JvmOverloads constructor( if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d( TAG, - "Found " + data?.getCount()?.toString() + " cursor entries for uri " + mEventUri + "Found " + data?.getCount()?.toString() + " cursor entries for uri " + + mEventUri ) } val cLoader: CursorLoader = loader as CursorLoader @@ -357,7 +358,7 @@ class MonthByWeekFragment @JvmOverloads constructor( ) (mAdapter as MonthByWeekAdapter).setEvents( mFirstLoadedJulianDay, - mLastLoadedJulianDay - mFirstLoadedJulianDay + 1, events + mLastLoadedJulianDay - mFirstLoadedJulianDay + 1, events as ArrayList<Event>? ) } } @@ -396,7 +397,8 @@ class MonthByWeekFragment @JvmOverloads constructor( val animateToday = event?.extraLong and CalendarController.EXTRA_GOTO_TODAY.toLong() != 0L val delayAnimation: Boolean = - goTo(event?.selectedTime?.toMillis(true)?.toLong() as Long, animate, true, false) + goTo(event?.selectedTime?.toMillis(true)?.toLong() as Long, + animate, true, false) if (animateToday) { // If we need to flash today start the animation after any // movement from listView has ended. diff --git a/src/com/android/calendar/month/SimpleDayPickerFragment.kt b/src/com/android/calendar/month/SimpleDayPickerFragment.kt index d3c43e66..01fcbac6 100644 --- a/src/com/android/calendar/month/SimpleDayPickerFragment.kt +++ b/src/com/android/calendar/month/SimpleDayPickerFragment.kt @@ -132,9 +132,9 @@ open class SimpleDayPickerFragment(initialTime: Long) : ListFragment(), OnScroll @JvmField protected var mObserver: DataSetObserver = object : DataSetObserver() { @Override override fun onChanged() { - val day: Time = mAdapter!!.getSelectedDay() - if (day.year !== mSelectedDay.year || day.yearDay !== mSelectedDay.yearDay) { - goTo(day.toMillis(true), true, true, false) + val day: Time? = mAdapter!!.getSelectedDay() + if (day!!.year !== mSelectedDay!!.year || day!!.yearDay !== mSelectedDay.yearDay) { + goTo(day!!.toMillis(true), true, true, false) } } } @@ -178,11 +178,11 @@ open class SimpleDayPickerFragment(initialTime: Long) : ListFragment(), OnScroll * this method to provide a custom adapter. */ protected open fun setUpAdapter() { - val weekParams: HashMap<String, Int> = HashMap<String, Int>() - weekParams.put(SimpleWeeksAdapter.WEEK_PARAMS_NUM_WEEKS, mNumWeeks) - weekParams.put(SimpleWeeksAdapter.WEEK_PARAMS_SHOW_WEEK, if (mShowWeekNumber) 1 else 0) - weekParams.put(SimpleWeeksAdapter.WEEK_PARAMS_WEEK_START, mFirstDayOfWeek) - weekParams.put(SimpleWeeksAdapter.WEEK_PARAMS_JULIAN_DAY, + val weekParams = HashMap<String?, Int?>() + weekParams?.put(SimpleWeeksAdapter.WEEK_PARAMS_NUM_WEEKS, mNumWeeks) + weekParams?.put(SimpleWeeksAdapter.WEEK_PARAMS_SHOW_WEEK, if (mShowWeekNumber) 1 else 0) + weekParams?.put(SimpleWeeksAdapter.WEEK_PARAMS_WEEK_START, mFirstDayOfWeek) + weekParams?.put(SimpleWeeksAdapter.WEEK_PARAMS_JULIAN_DAY, Time.getJulianDay(mSelectedDay.toMillis(false), mSelectedDay.gmtoff)) if (mAdapter == null) { mAdapter = SimpleWeeksAdapter(getActivity(), weekParams) @@ -316,8 +316,11 @@ open class SimpleDayPickerFragment(initialTime: Long) : ListFragment(), OnScroll } @Override - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View { + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { val v: View = inflater.inflate(R.layout.month_by_week, container, false) mDayNamesHeader = v.findViewById(R.id.day_names) as ViewGroup @@ -438,7 +441,11 @@ open class SimpleDayPickerFragment(initialTime: Long) : ListFragment(), OnScroll */ @Override override fun onScroll( - view: AbsListView, firstVisibleItem: Int, visibleItemCount: Int, totalItemCount: Int) { + view: AbsListView, + firstVisibleItem: Int, + visibleItemCount: Int, + totalItemCount: Int + ) { val child = view.getChildAt(0) as? SimpleWeekView if (child == null) { return @@ -569,8 +576,8 @@ open class SimpleDayPickerFragment(initialTime: Long) : ListFragment(), OnScroll "new scroll state: $mNewState old state: $mPreviousScrollState") } // Fix the position after a scroll or a fling ends - if (mNewState == OnScrollListener.SCROLL_STATE_IDLE - && mPreviousScrollState != OnScrollListener.SCROLL_STATE_IDLE) { + if (mNewState == OnScrollListener.SCROLL_STATE_IDLE && + mPreviousScrollState != OnScrollListener.SCROLL_STATE_IDLE) { mPreviousScrollState = mNewState mAdapter?.updateFocusMonth(mCurrentMonthDisplayed) } else { diff --git a/src/com/android/calendar/month/SimpleWeekView.kt b/src/com/android/calendar/month/SimpleWeekView.kt index 7c1ef44e..a71cff86 100644 --- a/src/com/android/calendar/month/SimpleWeekView.kt +++ b/src/com/android/calendar/month/SimpleWeekView.kt @@ -72,7 +72,7 @@ open class SimpleWeekView(context: Context) : View(context) { // The position of this week, equivalent to weeks since the week of Jan 1st, // 1970 - @JvmField protected var mWeek = -1 + @JvmField var mWeek = -1 // Quick reference to the width of this view, matches parent @JvmField protected var mWidth = 0 diff --git a/src/com/android/calendar/month/SimpleWeeksAdapter.kt b/src/com/android/calendar/month/SimpleWeeksAdapter.kt index 085d2ff4..67be4d89 100644 --- a/src/com/android/calendar/month/SimpleWeeksAdapter.kt +++ b/src/com/android/calendar/month/SimpleWeeksAdapter.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 The Android Open Source Project + * 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. @@ -42,8 +42,8 @@ import java.util.Locale * weeks at a time. See [SimpleDayPickerFragment] for usage. * */ -class SimpleWeeksAdapter(context: Context, params: HashMap<String?, Integer?>?) : BaseAdapter(), - OnTouchListener { +open class SimpleWeeksAdapter(context: Context, params: HashMap<String?, Int?>?) : BaseAdapter(), + OnTouchListener { protected var mContext: Context // The day to highlight as selected @@ -63,10 +63,10 @@ class SimpleWeeksAdapter(context: Context, params: HashMap<String?, Integer?>?) /** * Set up the gesture detector and selected time */ - protected fun init() { + protected open fun init() { mGestureDetector = GestureDetector(mContext, CalendarGestureListener()) mSelectedDay = Time() - mSelectedDay.setToNow() + mSelectedDay?.setToNow() } /** @@ -75,80 +75,94 @@ class SimpleWeeksAdapter(context: Context, params: HashMap<String?, Integer?>?) * * @param params A list of parameters for this adapter */ - fun updateParams(params: HashMap<String?, Integer?>?) { + fun updateParams(params: HashMap<String?, Int?>?) { if (params == null) { Log.e(TAG, "WeekParameters are null! Cannot update adapter.") return } if (params.containsKey(WEEK_PARAMS_FOCUS_MONTH)) { - mFocusMonth = params.get(WEEK_PARAMS_FOCUS_MONTH) + // Casting from Int? --> Int + mFocusMonth = params.get(WEEK_PARAMS_FOCUS_MONTH) as Int } if (params.containsKey(WEEK_PARAMS_FOCUS_MONTH)) { - mNumWeeks = params.get(WEEK_PARAMS_NUM_WEEKS) + // Casting from Int? --> Int + mNumWeeks = params.get(WEEK_PARAMS_NUM_WEEKS) as Int } if (params.containsKey(WEEK_PARAMS_SHOW_WEEK)) { - mShowWeekNumber = params.get(WEEK_PARAMS_SHOW_WEEK) !== 0 + // Casting from Int? --> Int + mShowWeekNumber = params.get(WEEK_PARAMS_SHOW_WEEK) as Int != 0 } if (params.containsKey(WEEK_PARAMS_WEEK_START)) { - mFirstDayOfWeek = params.get(WEEK_PARAMS_WEEK_START) + // Casting from Int? --> Int + mFirstDayOfWeek = params.get(WEEK_PARAMS_WEEK_START) as Int } if (params.containsKey(WEEK_PARAMS_JULIAN_DAY)) { - val julianDay: Int = params.get(WEEK_PARAMS_JULIAN_DAY) - mSelectedDay.setJulianDay(julianDay) + // Casting from Int? --> Int + val julianDay: Int = params.get(WEEK_PARAMS_JULIAN_DAY) as Int + mSelectedDay?.setJulianDay(julianDay) mSelectedWeek = Utils.getWeeksSinceEpochFromJulianDay(julianDay, mFirstDayOfWeek) } if (params.containsKey(WEEK_PARAMS_DAYS_PER_WEEK)) { - mDaysPerWeek = params.get(WEEK_PARAMS_DAYS_PER_WEEK) + // Casting from Int? --> Int + mDaysPerWeek = params.get(WEEK_PARAMS_DAYS_PER_WEEK) as Int } refresh() } + /** - * Returns the currently highlighted day + * Updates the selected day and related parameters. * - * @return + * @param selectedTime The time to highlight */ + open fun setSelectedDay(selectedTime: Time?) { + mSelectedDay?.set(selectedTime) + val millis: Long = mSelectedDay!!.normalize(true) + mSelectedWeek = Utils.getWeeksSinceEpochFromJulianDay( + Time.getJulianDay(millis, mSelectedDay!!.gmtoff), mFirstDayOfWeek + ) + notifyDataSetChanged() + } + /** - * Updates the selected day and related parameters. + * Returns the currently highlighted day * - * @param selectedTime The time to highlight + * @return */ - var selectedDay: Time? - get() = mSelectedDay - set(selectedTime) { - mSelectedDay.set(selectedTime) - val millis: Long = mSelectedDay.normalize(true) - mSelectedWeek = Utils.getWeeksSinceEpochFromJulianDay( - Time.getJulianDay(millis, mSelectedDay.gmtoff), mFirstDayOfWeek - ) - notifyDataSetChanged() - } + fun getSelectedDay(): Time? { + return mSelectedDay + } /** * updates any config options that may have changed and refreshes the view */ - protected fun refresh() { + internal open fun refresh() { notifyDataSetChanged() } @Override - fun getItem(position: Int): Object? { + override fun getCount(): Int { + return WEEK_COUNT + } + + @Override + override fun getItem(position: Int): Any? { return null } @Override - fun getItemId(position: Int): Long { + override fun getItemId(position: Int): Long { return position.toLong() } @SuppressWarnings("unchecked") @Override - fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { val v: SimpleWeekView - var drawingParams: HashMap<String?, Integer?>? = null + var drawingParams: HashMap<String?, Int?>? = null if (convertView != null) { v = convertView as SimpleWeekView // We store the drawing parameters in the view so it can be recycled - drawingParams = v.getTag() as HashMap<String?, Integer?> + drawingParams = v.getTag() as HashMap<String?, Int?> } else { v = SimpleWeekView(mContext) // Set up the new view @@ -160,12 +174,12 @@ class SimpleWeeksAdapter(context: Context, params: HashMap<String?, Integer?>?) v.setOnTouchListener(this) } if (drawingParams == null) { - drawingParams = HashMap<String, Integer>() + drawingParams = HashMap<String?, Int?>() } drawingParams.clear() var selectedDay = -1 if (mSelectedWeek == position) { - selectedDay = mSelectedDay.weekDay + selectedDay = mSelectedDay!!.weekDay } // pass in all the view parameters @@ -179,7 +193,7 @@ class SimpleWeeksAdapter(context: Context, params: HashMap<String?, Integer?>?) drawingParams.put(SimpleWeekView.VIEW_PARAMS_NUM_DAYS, mDaysPerWeek) drawingParams.put(SimpleWeekView.VIEW_PARAMS_WEEK, position) drawingParams.put(SimpleWeekView.VIEW_PARAMS_FOCUS_MONTH, mFocusMonth) - v.setWeekParams(drawingParams, mSelectedDay.timezone) + v.setWeekParams(drawingParams, mSelectedDay!!.timezone) v.invalidate() return v } @@ -195,12 +209,13 @@ class SimpleWeeksAdapter(context: Context, params: HashMap<String?, Integer?>?) } @Override - fun onTouch(v: View, event: MotionEvent): Boolean { - if (mGestureDetector.onTouchEvent(event)) { + override fun onTouch(v: View, event: MotionEvent): Boolean { + if (mGestureDetector!!.onTouchEvent(event)) { val view: SimpleWeekView = v as SimpleWeekView - val day: Time = (v as SimpleWeekView).getDayFromLocation(event.getX()) + val day: Time? = (v as SimpleWeekView).getDayFromLocation(event.getX()) if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Touched day at Row=" + view.mWeek.toString() + " day=" + day.toString()) + Log.d(TAG, "Touched day at Row=" + view.mWeek.toString() + " day=" + + day?.toString()) } if (day != null) { onDayTapped(day) @@ -215,11 +230,11 @@ class SimpleWeeksAdapter(context: Context, params: HashMap<String?, Integer?>?) * * @param day The day that was tapped */ - protected fun onDayTapped(day: Time) { - day.hour = mSelectedDay.hour - day.minute = mSelectedDay.minute - day.second = mSelectedDay.second - selectedDay = day + protected open fun onDayTapped(day: Time) { + day.hour = mSelectedDay!!.hour + day.minute = mSelectedDay!!.minute + day.second = mSelectedDay!!.second + setSelectedDay(day) } /** @@ -228,7 +243,7 @@ class SimpleWeeksAdapter(context: Context, params: HashMap<String?, Integer?>?) */ protected inner class CalendarGestureListener : GestureDetector.SimpleOnGestureListener() { @Override - fun onSingleTapUp(e: MotionEvent?): Boolean { + override fun onSingleTapUp(e: MotionEvent?): Boolean { return true } } @@ -271,9 +286,8 @@ class SimpleWeeksAdapter(context: Context, params: HashMap<String?, Integer?>?) * How many days of the week to display [1-7]. */ const val WEEK_PARAMS_DAYS_PER_WEEK = "days_per_week" - @get:Override val count: Int = (CalendarController.MAX_CALENDAR_WEEK - - CalendarController.MIN_CALENDAR_WEEK) - get() = Companion.field + protected const val WEEK_COUNT = CalendarController.MAX_CALENDAR_WEEK - + CalendarController.MIN_CALENDAR_WEEK protected var DEFAULT_NUM_WEEKS = 6 protected var DEFAULT_MONTH_FOCUS = 0 protected var DEFAULT_DAYS_PER_WEEK = 7 |