diff options
author | Damian Patel <damianpatel@google.com> | 2021-07-09 14:13:01 +0000 |
---|---|---|
committer | Damian Patel <damianpatel@google.com> | 2021-07-09 14:13:01 +0000 |
commit | 83732c13ab8f01ceb3ffa9a7990d773c9abb28e2 (patch) | |
tree | 0dbad7dbc45c71ef3abc0fa002d69796ee62e9b9 | |
parent | ba5bef48f57609cbdfaae3f424741a48d3e0456a (diff) | |
download | Calendar-83732c13ab8f01ceb3ffa9a7990d773c9abb28e2.tar.gz |
AOSP/Calendar - Initial conversion of MonthByWeekAdapter
and SimpleWeeksAdapter
This is how Android Studio's built-in converter
converted MonthByWeekAdapter.java and SimpleWeeksAdapter.java
to Kotlin without any additional edits.
Change-Id: Ic727bcfd5bef2b1e70dc6c96ddea398b881ad5ec
-rw-r--r-- | src/com/android/calendar/month/MonthByWeekAdapter.kt | 521 | ||||
-rw-r--r-- | src/com/android/calendar/month/SimpleWeeksAdapter.kt | 364 |
2 files changed, 440 insertions, 445 deletions
diff --git a/src/com/android/calendar/month/MonthByWeekAdapter.kt b/src/com/android/calendar/month/MonthByWeekAdapter.kt index 45a1bea1..a0ae1b1b 100644 --- a/src/com/android/calendar/month/MonthByWeekAdapter.kt +++ b/src/com/android/calendar/month/MonthByWeekAdapter.kt @@ -13,394 +13,391 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -package com.android.calendar.month; - -import android.content.Context; -import android.content.res.Configuration; -import android.os.Handler; -import android.os.Message; -import android.text.format.Time; -import android.util.Log; -import android.view.GestureDetector; -import android.view.HapticFeedbackConstants; -import android.view.MotionEvent; -import android.view.View; -import android.view.ViewConfiguration; -import android.view.ViewGroup; -import android.widget.AbsListView.LayoutParams; - -import com.android.calendar.CalendarController; -import com.android.calendar.CalendarController.EventType; -import com.android.calendar.CalendarController.ViewType; -import com.android.calendar.Event; -import com.android.calendar.R; -import com.android.calendar.Utils; - -import java.util.ArrayList; -import java.util.HashMap; - -public class MonthByWeekAdapter extends SimpleWeeksAdapter { - private static final String TAG = "MonthByWeekAdapter"; - - public static final String WEEK_PARAMS_IS_MINI = "mini_month"; - protected static int DEFAULT_QUERY_DAYS = 7 * 8; // 8 weeks - private static final long ANIMATE_TODAY_TIMEOUT = 1000; - - protected CalendarController mController; - protected String mHomeTimeZone; - protected Time mTempTime; - protected Time mToday; - protected int mFirstJulianDay; - protected int mQueryDays; - protected boolean mIsMiniMonth = true; - protected int mOrientation = Configuration.ORIENTATION_LANDSCAPE; - private final boolean mShowAgendaWithMonth; - - protected ArrayList<ArrayList<Event>> mEventDayList = new ArrayList<ArrayList<Event>>(); - protected ArrayList<Event> mEvents = null; - - private boolean mAnimateToday = false; - private long mAnimateTime = 0; - - private Handler mEventDialogHandler; - - MonthWeekEventsView mClickedView; - MonthWeekEventsView mSingleTapUpView; - MonthWeekEventsView mLongClickedView; - - float mClickedXLocation; // Used to find which day was clicked - long mClickTime; // Used to calculate minimum click animation time - // Used to insure minimal time for seeing the click animation before switching views - private static final int 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 static int mOnDownDelay; - private static int mTotalClickDelay; - // Minimal distance to move the finger in order to cancel the click animation - private static float mMovedPixelToCancel; - - public MonthByWeekAdapter(Context context, HashMap<String, Integer> params) { - super(context, params); - if (params.containsKey(WEEK_PARAMS_IS_MINI)) { - mIsMiniMonth = params.get(WEEK_PARAMS_IS_MINI) != 0; - } - mShowAgendaWithMonth = Utils.getConfigBool(context, R.bool.show_agenda_with_month); - ViewConfiguration vc = ViewConfiguration.get(context); - mOnDownDelay = ViewConfiguration.getTapTimeout(); - mMovedPixelToCancel = vc.getScaledTouchSlop(); - mTotalClickDelay = mOnDownDelay + mOnTapDelay; - } - - public void animateToday() { - mAnimateToday = true; - mAnimateTime = System.currentTimeMillis(); +package com.android.calendar.month + +import android.content.Context +import android.content.res.Configuration +import android.os.Handler +import android.os.Message +import android.text.format.Time +import android.util.Log +import android.view.GestureDetector +import android.view.HapticFeedbackConstants +import android.view.MotionEvent +import android.view.View +import android.view.ViewConfiguration +import android.view.ViewGroup +import android.widget.AbsListView.LayoutParams +import com.android.calendar.CalendarController +import com.android.calendar.CalendarController.EventType +import com.android.calendar.CalendarController.ViewType +import com.android.calendar.Event +import com.android.calendar.R +import com.android.calendar.Utils +import java.util.ArrayList +import java.util.HashMap + +class MonthByWeekAdapter(context: Context?, params: HashMap<String?, Integer?>) : + SimpleWeeksAdapter(context, params) { + protected var mController: CalendarController? = null + protected var mHomeTimeZone: String? = null + protected var mTempTime: Time? = null + protected var mToday: Time? = null + protected var mFirstJulianDay = 0 + protected var mQueryDays = 0 + protected var mIsMiniMonth = true + protected var mOrientation: Int = Configuration.ORIENTATION_LANDSCAPE + private val mShowAgendaWithMonth: Boolean + protected var mEventDayList: ArrayList<ArrayList<Event>> = ArrayList<ArrayList<Event>>() + protected var mEvents: ArrayList<Event>? = null + private var mAnimateToday = false + private var mAnimateTime: Long = 0 + private val mEventDialogHandler: Handler? = null + 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 + + fun animateToday() { + mAnimateToday = true + mAnimateTime = System.currentTimeMillis() } @Override - protected void init() { - super.init(); - mGestureDetector = new GestureDetector(mContext, new CalendarGestureListener()); - mController = CalendarController.getInstance(mContext); - mHomeTimeZone = Utils.getTimeZone(mContext, null); - mSelectedDay.switchTimezone(mHomeTimeZone); - mToday = new Time(mHomeTimeZone); - mToday.setToNow(); - mTempTime = new Time(mHomeTimeZone); + protected fun init() { + super.init() + mGestureDetector = GestureDetector(mContext, CalendarGestureListener()) + mController = CalendarController.getInstance(mContext) + mHomeTimeZone = Utils.getTimeZone(mContext, null) + mSelectedDay.switchTimezone(mHomeTimeZone) + mToday = Time(mHomeTimeZone) + mToday.setToNow() + mTempTime = Time(mHomeTimeZone) } - private void updateTimeZones() { - mSelectedDay.timezone = mHomeTimeZone; - mSelectedDay.normalize(true); - mToday.timezone = mHomeTimeZone; - mToday.setToNow(); - mTempTime.switchTimezone(mHomeTimeZone); + private fun updateTimeZones() { + mSelectedDay.timezone = mHomeTimeZone + mSelectedDay.normalize(true) + mToday.timezone = mHomeTimeZone + mToday.setToNow() + mTempTime.switchTimezone(mHomeTimeZone) } @Override - public void setSelectedDay(Time selectedTime) { - mSelectedDay.set(selectedTime); - long millis = mSelectedDay.normalize(true); + fun setSelectedDay(selectedTime: Time?) { + mSelectedDay.set(selectedTime) + val millis: Long = mSelectedDay.normalize(true) mSelectedWeek = Utils.getWeeksSinceEpochFromJulianDay( - Time.getJulianDay(millis, mSelectedDay.gmtoff), mFirstDayOfWeek); - notifyDataSetChanged(); + Time.getJulianDay(millis, mSelectedDay.gmtoff), mFirstDayOfWeek + ) + notifyDataSetChanged() } - public void setEvents(int firstJulianDay, int numDays, ArrayList<Event> events) { + fun setEvents(firstJulianDay: Int, numDays: Int, events: ArrayList<Event>?) { if (mIsMiniMonth) { if (Log.isLoggable(TAG, Log.ERROR)) { - Log.e(TAG, "Attempted to set events for mini view. Events only supported in full" - + " view."); + Log.e( + TAG, "Attempted to set events for mini view. Events only supported in full" + + " view." + ) } - return; + return } - mEvents = events; - mFirstJulianDay = firstJulianDay; - mQueryDays = numDays; + mEvents = events + mFirstJulianDay = firstJulianDay + mQueryDays = numDays // Create a new list, this is necessary since the weeks are referencing // pieces of the old list - ArrayList<ArrayList<Event>> eventDayList = new ArrayList<ArrayList<Event>>(); - for (int i = 0; i < numDays; i++) { - eventDayList.add(new ArrayList<Event>()); + val eventDayList: ArrayList<ArrayList<Event>> = ArrayList<ArrayList<Event>>() + for (i in 0 until numDays) { + eventDayList.add(ArrayList<Event>()) } - - if (events == null || events.size() == 0) { - if(Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "No events. Returning early--go schedule something fun."); + if (events == null || events.size() === 0) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "No events. Returning early--go schedule something fun.") } - mEventDayList = eventDayList; - refresh(); - return; + mEventDayList = eventDayList + refresh() + return } // Compute the new set of days with events - for (Event event : events) { - int startDay = event.startDay - mFirstJulianDay; - int endDay = event.endDay - mFirstJulianDay + 1; + for (event in events) { + var startDay: Int = event.startDay - mFirstJulianDay + var endDay: Int = event.endDay - mFirstJulianDay + 1 if (startDay < numDays || endDay >= 0) { if (startDay < 0) { - startDay = 0; + startDay = 0 } if (startDay > numDays) { - continue; + continue } if (endDay < 0) { - continue; + continue } if (endDay > numDays) { - endDay = numDays; + endDay = numDays } - for (int j = startDay; j < endDay; j++) { - eventDayList.get(j).add(event); + for (j in startDay until endDay) { + eventDayList.get(j).add(event) } } } - if(Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Processed " + events.size() + " events."); + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "Processed " + events.size().toString() + " events.") } - mEventDayList = eventDayList; - refresh(); + mEventDayList = eventDayList + refresh() } @SuppressWarnings("unchecked") @Override - public View getView(int position, View convertView, ViewGroup parent) { + fun getView(position: Int, convertView: View?, parent: ViewGroup): View { if (mIsMiniMonth) { - return super.getView(position, convertView, parent); + return super.getView(position, convertView, parent) } - MonthWeekEventsView v; - LayoutParams params = new LayoutParams( - LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); - HashMap<String, Integer> drawingParams = null; - boolean isAnimatingToday = false; + var v: MonthWeekEventsView + val params = LayoutParams( + LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT + ) + var drawingParams: HashMap<String?, Integer?>? = null + var isAnimatingToday = false if (convertView != null) { - v = (MonthWeekEventsView) convertView; + 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)) { - long currentTime = System.currentTimeMillis(); + 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 // before reaching today. if (currentTime - mAnimateTime > ANIMATE_TODAY_TIMEOUT) { - mAnimateToday = false; - mAnimateTime = 0; + mAnimateToday = false + mAnimateTime = 0 } else { - isAnimatingToday = true; + isAnimatingToday = true // There is a bug that causes invalidates to not work some // of the time unless we recreate the view. - v = new MonthWeekEventsView(mContext); - } + v = MonthWeekEventsView(mContext) + } } else { - drawingParams = (HashMap<String, Integer>) v.getTag(); + drawingParams = v.getTag() as HashMap<String?, Integer?> } } else { - v = new MonthWeekEventsView(mContext); + v = MonthWeekEventsView(mContext) } if (drawingParams == null) { - drawingParams = new HashMap<String, Integer>(); + drawingParams = HashMap<String, Integer>() } - drawingParams.clear(); - - v.setLayoutParams(params); - v.setClickable(true); - v.setOnTouchListener(this); - - int selectedDay = -1; - if (mSelectedWeek == position) { - selectedDay = mSelectedDay.weekDay; + drawingParams.clear() + v.setLayoutParams(params) + v.setClickable(true) + v.setOnTouchListener(this) + var selectedDay = -1 + if (mSelectedWeek === position) { + selectedDay = mSelectedDay.weekDay } - - drawingParams.put(SimpleWeekView.VIEW_PARAMS_HEIGHT, - (parent.getHeight() + parent.getTop()) / mNumWeeks); - drawingParams.put(SimpleWeekView.VIEW_PARAMS_SELECTED_DAY, selectedDay); - drawingParams.put(SimpleWeekView.VIEW_PARAMS_SHOW_WK_NUM, mShowWeekNumber ? 1 : 0); - drawingParams.put(SimpleWeekView.VIEW_PARAMS_WEEK_START, mFirstDayOfWeek); - drawingParams.put(SimpleWeekView.VIEW_PARAMS_NUM_DAYS, mDaysPerWeek); - drawingParams.put(SimpleWeekView.VIEW_PARAMS_WEEK, position); - drawingParams.put(SimpleWeekView.VIEW_PARAMS_FOCUS_MONTH, mFocusMonth); - drawingParams.put(MonthWeekEventsView.VIEW_PARAMS_ORIENTATION, mOrientation); - + drawingParams.put( + SimpleWeekView.VIEW_PARAMS_HEIGHT, + (parent.getHeight() + parent.getTop()) / mNumWeeks + ) + drawingParams.put(SimpleWeekView.VIEW_PARAMS_SELECTED_DAY, selectedDay) + drawingParams.put(SimpleWeekView.VIEW_PARAMS_SHOW_WK_NUM, if (mShowWeekNumber) 1 else 0) + drawingParams.put(SimpleWeekView.VIEW_PARAMS_WEEK_START, mFirstDayOfWeek) + drawingParams.put(SimpleWeekView.VIEW_PARAMS_NUM_DAYS, mDaysPerWeek) + drawingParams.put(SimpleWeekView.VIEW_PARAMS_WEEK, position) + drawingParams.put(SimpleWeekView.VIEW_PARAMS_FOCUS_MONTH, mFocusMonth) + drawingParams.put(MonthWeekEventsView.VIEW_PARAMS_ORIENTATION, mOrientation) if (isAnimatingToday) { - drawingParams.put(MonthWeekEventsView.VIEW_PARAMS_ANIMATE_TODAY, 1); - mAnimateToday = false; + drawingParams.put(MonthWeekEventsView.VIEW_PARAMS_ANIMATE_TODAY, 1) + mAnimateToday = false } - - v.setWeekParams(drawingParams, mSelectedDay.timezone); - return v; + v.setWeekParams(drawingParams, mSelectedDay.timezone) + return v } @Override - protected void refresh() { - mFirstDayOfWeek = Utils.getFirstDayOfWeek(mContext); - mShowWeekNumber = Utils.getShowWeekNumber(mContext); - mHomeTimeZone = Utils.getTimeZone(mContext, null); - mOrientation = mContext.getResources().getConfiguration().orientation; - updateTimeZones(); - notifyDataSetChanged(); + protected fun refresh() { + mFirstDayOfWeek = Utils.getFirstDayOfWeek(mContext) + mShowWeekNumber = Utils.getShowWeekNumber(mContext) + mHomeTimeZone = Utils.getTimeZone(mContext, null) + mOrientation = mContext.getResources().getConfiguration().orientation + updateTimeZones() + notifyDataSetChanged() } @Override - protected void onDayTapped(Time day) { - setDayParameters(day); - if (mShowAgendaWithMonth || mIsMiniMonth) { + protected 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, - ViewType.CURRENT, CalendarController.EXTRA_GOTO_DATE, null, null); + mController.sendEvent( + mContext, 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, - ViewType.DETAIL, - CalendarController.EXTRA_GOTO_DATE - | CalendarController.EXTRA_GOTO_BACK_TO_PREVIOUS, null, null); + mController.sendEvent( + mContext, EventType.GO_TO, day, day, -1, + ViewType.DETAIL, CalendarController.EXTRA_GOTO_DATE + or CalendarController.EXTRA_GOTO_BACK_TO_PREVIOUS, null, null + ) } } - private void setDayParameters(Time day) { - day.timezone = mHomeTimeZone; - Time currTime = new Time(mHomeTimeZone); - currTime.set(mController.getTime()); - day.hour = currTime.hour; - day.minute = currTime.minute; - day.allDay = false; - day.normalize(true); + private fun setDayParameters(day: Time) { + day.timezone = mHomeTimeZone + val currTime = Time(mHomeTimeZone) + currTime.set(mController.getTime()) + day.hour = currTime.hour + day.minute = currTime.minute + day.allDay = false + day.normalize(true) } @Override - public boolean onTouch(View v, MotionEvent event) { - if (!(v instanceof MonthWeekEventsView)) { - return super.onTouch(v, event); + fun onTouch(v: View?, event: MotionEvent): Boolean { + if (v !is MonthWeekEventsView) { + return super.onTouch(v, event) } - - int action = 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)) { - mSingleTapUpView = (MonthWeekEventsView) v; - long delay = System.currentTimeMillis() - mClickTime; + mSingleTapUpView = v as MonthWeekEventsView? + val delay: Long = System.currentTimeMillis() - mClickTime // Make sure the animation is visible for at least mOnTapDelay - mOnDownDelay ms - mListView.postDelayed(mDoSingleTapUp, - delay > mTotalClickDelay ? 0 : mTotalClickDelay - delay); - return true; + mListView.postDelayed( + mDoSingleTapUp, + if (delay > mTotalClickDelay) 0 else mTotalClickDelay - delay + ) + return true } else { // Animate a click - on down: show the selected day in the "clicked" color. // On Up/scroll/move/cancel: hide the "clicked" color. - switch (action) { - case MotionEvent.ACTION_DOWN: - mClickedView = (MonthWeekEventsView)v; - mClickedXLocation = event.getX(); - mClickTime = System.currentTimeMillis(); - mListView.postDelayed(mDoClick, mOnDownDelay); - break; - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_SCROLL: - case MotionEvent.ACTION_CANCEL: - clearClickedView((MonthWeekEventsView)v); - break; - case MotionEvent.ACTION_MOVE: - // No need to cancel on vertical movement, ACTION_SCROLL will do that. + when (action) { + MotionEvent.ACTION_DOWN -> { + mClickedView = v as MonthWeekEventsView + mClickedXLocation = event.getX() + mClickTime = System.currentTimeMillis() + mListView.postDelayed(mDoClick, mOnDownDelay) + } + 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. if (Math.abs(event.getX() - mClickedXLocation) > mMovedPixelToCancel) { - clearClickedView((MonthWeekEventsView)v); + clearClickedView(v as MonthWeekEventsView?) } - break; - default: - break; + else -> { + } } } // Do not tell the frameworks we consumed the touch action so that fling actions can be // processed by the fragment. - return false; + return false } /** * This is here so we can identify events and process them */ - protected class CalendarGestureListener extends GestureDetector.SimpleOnGestureListener { + protected inner class CalendarGestureListener : GestureDetector.SimpleOnGestureListener() { @Override - public boolean onSingleTapUp(MotionEvent e) { - return true; + fun onSingleTapUp(e: MotionEvent?): Boolean { + return true } @Override - public void onLongPress(MotionEvent e) { + fun onLongPress(e: MotionEvent?) { if (mLongClickedView != null) { - Time day = mLongClickedView.getDayFromLocation(mClickedXLocation); + val day: Time = mLongClickedView.getDayFromLocation(mClickedXLocation) if (day != null) { - mLongClickedView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); - Message message = new Message(); - message.obj = day; + mLongClickedView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) + val message = Message() + message.obj = day } - mLongClickedView.clearClickedDay(); - mLongClickedView = null; - } + mLongClickedView.clearClickedDay() + mLongClickedView = null + } } } // Clear the visual cues of the click animation and related running code. - private void clearClickedView(MonthWeekEventsView v) { - mListView.removeCallbacks(mDoClick); - synchronized(v) { - v.clearClickedDay(); - } - mClickedView = null; + private fun clearClickedView(v: MonthWeekEventsView?) { + mListView.removeCallbacks(mDoClick) + synchronized(v) { 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 final Runnable mDoClick = new Runnable() { + private val mDoClick: Runnable = object : Runnable() { @Override - public void run() { + fun run() { if (mClickedView != null) { - synchronized(mClickedView) { - mClickedView.setClickedDay(mClickedXLocation); - } - mLongClickedView = mClickedView; - mClickedView = null; + synchronized(mClickedView) { 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 final Runnable mDoSingleTapUp = new Runnable() { + private val mDoSingleTapUp: Runnable = object : Runnable() { @Override - public void run() { + fun run() { if (mSingleTapUpView != null) { - Time day = mSingleTapUpView.getDayFromLocation(mClickedXLocation); + val day: Time = mSingleTapUpView.getDayFromLocation(mClickedXLocation) if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Touched day at Row=" + mSingleTapUpView.mWeek + " day=" + day.toString()); + Log.d( + TAG, + "Touched day at Row=" + mSingleTapUpView.mWeek.toString() + " day=" + day.toString() + ) } if (day != null) { - onDayTapped(day); + onDayTapped(day) } - clearClickedView(mSingleTapUpView); - mSingleTapUpView = null; + clearClickedView(mSingleTapUpView) + mSingleTapUpView = null } } - }; -} + } + + companion object { + private const val TAG = "MonthByWeekAdapter" + const val WEEK_PARAMS_IS_MINI = "mini_month" + protected var DEFAULT_QUERY_DAYS = 7 * 8 // 8 weeks + private const val ANIMATE_TODAY_TIMEOUT: Long = 1000 + + // 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 distance to move the finger in order to cancel the click animation + private var mMovedPixelToCancel: Float + } + + init { + if (params.containsKey(WEEK_PARAMS_IS_MINI)) { + mIsMiniMonth = params.get(WEEK_PARAMS_IS_MINI) !== 0 + } + mShowAgendaWithMonth = Utils.getConfigBool(context, R.bool.show_agenda_with_month) + val vc: ViewConfiguration = ViewConfiguration.get(context) + mOnDownDelay = ViewConfiguration.getTapTimeout() + mMovedPixelToCancel = vc.getScaledTouchSlop() + mTotalClickDelay = mOnDownDelay + mOnTapDelay + } +}
\ No newline at end of file diff --git a/src/com/android/calendar/month/SimpleWeeksAdapter.kt b/src/com/android/calendar/month/SimpleWeeksAdapter.kt index d29b2622..085d2ff4 100644 --- a/src/com/android/calendar/month/SimpleWeeksAdapter.kt +++ b/src/com/android/calendar/month/SimpleWeeksAdapter.kt @@ -13,235 +13,175 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -package com.android.calendar.month; - +package com.android.calendar.month // TODO Remove calendar imports when the required methods have been // refactored into the public api -import com.android.calendar.CalendarController; -import com.android.calendar.Utils; - -import android.content.Context; -import android.text.format.Time; -import android.util.Log; -import android.view.GestureDetector; -import android.view.MotionEvent; -import android.view.View; -import android.view.View.OnTouchListener; -import android.view.ViewGroup; -import android.widget.AbsListView.LayoutParams; -import android.widget.BaseAdapter; -import android.widget.ListView; - -import java.util.Calendar; -import java.util.HashMap; -import java.util.Locale; +import com.android.calendar.CalendarController +import com.android.calendar.Utils +import android.content.Context +import android.text.format.Time +import android.util.Log +import android.view.GestureDetector +import android.view.MotionEvent +import android.view.View +import android.view.View.OnTouchListener +import android.view.ViewGroup +import android.widget.AbsListView.LayoutParams +import android.widget.BaseAdapter +import android.widget.ListView +import java.util.Calendar +import java.util.HashMap +import java.util.Locale /** - * <p> + * + * * This is a specialized adapter for creating a list of weeks with selectable * days. It can be configured to display the week number, start the week on a * given day, show a reduced number of days, or display an arbitrary number of - * weeks at a time. See {@link SimpleDayPickerFragment} for usage. - * </p> + * weeks at a time. See [SimpleDayPickerFragment] for usage. + * */ -public class SimpleWeeksAdapter extends BaseAdapter implements OnTouchListener { - - private static final String TAG = "MonthByWeek"; - - /** - * The number of weeks to display at a time. - */ - public static final String WEEK_PARAMS_NUM_WEEKS = "num_weeks"; - /** - * Which month should be in focus currently. - */ - public static final String WEEK_PARAMS_FOCUS_MONTH = "focus_month"; - /** - * Whether the week number should be shown. Non-zero to show them. - */ - public static final String WEEK_PARAMS_SHOW_WEEK = "week_numbers"; - /** - * Which day the week should start on. {@link Time#SUNDAY} through - * {@link Time#SATURDAY}. - */ - public static final String WEEK_PARAMS_WEEK_START = "week_start"; - /** - * The Julian day to highlight as selected. - */ - public static final String WEEK_PARAMS_JULIAN_DAY = "selected_day"; - /** - * How many days of the week to display [1-7]. - */ - public static final String WEEK_PARAMS_DAYS_PER_WEEK = "days_per_week"; - - protected static final int WEEK_COUNT = CalendarController.MAX_CALENDAR_WEEK - - CalendarController.MIN_CALENDAR_WEEK; - protected static int DEFAULT_NUM_WEEKS = 6; - protected static int DEFAULT_MONTH_FOCUS = 0; - protected static int DEFAULT_DAYS_PER_WEEK = 7; - protected static int DEFAULT_WEEK_HEIGHT = 32; - protected static int WEEK_7_OVERHANG_HEIGHT = 7; +class SimpleWeeksAdapter(context: Context, params: HashMap<String?, Integer?>?) : BaseAdapter(), + OnTouchListener { + protected var mContext: Context - protected static float mScale = 0; - protected Context mContext; // The day to highlight as selected - protected Time mSelectedDay; - // The week since 1970 that the selected day is in - protected int mSelectedWeek; - // When the week starts; numbered like Time.<WEEKDAY> (e.g. SUNDAY=0). - protected int mFirstDayOfWeek; - protected boolean mShowWeekNumber = false; - protected GestureDetector mGestureDetector; - protected int mNumWeeks = DEFAULT_NUM_WEEKS; - protected int mDaysPerWeek = DEFAULT_DAYS_PER_WEEK; - protected int mFocusMonth = DEFAULT_MONTH_FOCUS; + protected var mSelectedDay: Time? = null - public SimpleWeeksAdapter(Context context, HashMap<String, Integer> params) { - mContext = context; - - // Get default week start based on locale, subtracting one for use with android Time. - Calendar cal = Calendar.getInstance(Locale.getDefault()); - mFirstDayOfWeek = cal.getFirstDayOfWeek() - 1; + // The week since 1970 that the selected day is in + protected var mSelectedWeek = 0 - if (mScale == 0) { - mScale = context.getResources().getDisplayMetrics().density; - if (mScale != 1) { - WEEK_7_OVERHANG_HEIGHT *= mScale; - } - } - init(); - updateParams(params); - } + // When the week starts; numbered like Time.<WEEKDAY> (e.g. SUNDAY=0). + protected var mFirstDayOfWeek: Int + protected var mShowWeekNumber = false + protected var mGestureDetector: GestureDetector? = null + protected var mNumWeeks = DEFAULT_NUM_WEEKS + protected var mDaysPerWeek = DEFAULT_DAYS_PER_WEEK + protected var mFocusMonth = DEFAULT_MONTH_FOCUS /** * Set up the gesture detector and selected time */ - protected void init() { - mGestureDetector = new GestureDetector(mContext, new CalendarGestureListener()); - mSelectedDay = new Time(); - mSelectedDay.setToNow(); + protected fun init() { + mGestureDetector = GestureDetector(mContext, CalendarGestureListener()) + mSelectedDay = Time() + mSelectedDay.setToNow() } /** * Parse the parameters and set any necessary fields. See - * {@link #WEEK_PARAMS_NUM_WEEKS} for parameter details. + * [.WEEK_PARAMS_NUM_WEEKS] for parameter details. * * @param params A list of parameters for this adapter */ - public void updateParams(HashMap<String, Integer> params) { + fun updateParams(params: HashMap<String?, Integer?>?) { if (params == null) { - Log.e(TAG, "WeekParameters are null! Cannot update adapter."); - return; + Log.e(TAG, "WeekParameters are null! Cannot update adapter.") + return } if (params.containsKey(WEEK_PARAMS_FOCUS_MONTH)) { - mFocusMonth = params.get(WEEK_PARAMS_FOCUS_MONTH); + mFocusMonth = params.get(WEEK_PARAMS_FOCUS_MONTH) } if (params.containsKey(WEEK_PARAMS_FOCUS_MONTH)) { - mNumWeeks = params.get(WEEK_PARAMS_NUM_WEEKS); + mNumWeeks = params.get(WEEK_PARAMS_NUM_WEEKS) } if (params.containsKey(WEEK_PARAMS_SHOW_WEEK)) { - mShowWeekNumber = params.get(WEEK_PARAMS_SHOW_WEEK) != 0; + mShowWeekNumber = params.get(WEEK_PARAMS_SHOW_WEEK) !== 0 } if (params.containsKey(WEEK_PARAMS_WEEK_START)) { - mFirstDayOfWeek = params.get(WEEK_PARAMS_WEEK_START); + mFirstDayOfWeek = params.get(WEEK_PARAMS_WEEK_START) } if (params.containsKey(WEEK_PARAMS_JULIAN_DAY)) { - int julianDay = params.get(WEEK_PARAMS_JULIAN_DAY); - mSelectedDay.setJulianDay(julianDay); - mSelectedWeek = Utils.getWeeksSinceEpochFromJulianDay(julianDay, mFirstDayOfWeek); + val julianDay: Int = params.get(WEEK_PARAMS_JULIAN_DAY) + mSelectedDay.setJulianDay(julianDay) + mSelectedWeek = Utils.getWeeksSinceEpochFromJulianDay(julianDay, mFirstDayOfWeek) } if (params.containsKey(WEEK_PARAMS_DAYS_PER_WEEK)) { - mDaysPerWeek = params.get(WEEK_PARAMS_DAYS_PER_WEEK); + mDaysPerWeek = params.get(WEEK_PARAMS_DAYS_PER_WEEK) } - refresh(); + refresh() } - /** - * Updates the selected day and related parameters. + * Returns the currently highlighted day * - * @param selectedTime The time to highlight + * @return */ - public void setSelectedDay(Time selectedTime) { - mSelectedDay.set(selectedTime); - long millis = mSelectedDay.normalize(true); - mSelectedWeek = Utils.getWeeksSinceEpochFromJulianDay( - Time.getJulianDay(millis, mSelectedDay.gmtoff), mFirstDayOfWeek); - notifyDataSetChanged(); - } - /** - * Returns the currently highlighted day + * Updates the selected day and related parameters. * - * @return + * @param selectedTime The time to highlight */ - public Time getSelectedDay() { - return mSelectedDay; - } + 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() + } /** * updates any config options that may have changed and refreshes the view */ - protected void refresh() { - notifyDataSetChanged(); + protected fun refresh() { + notifyDataSetChanged() } @Override - public int getCount() { - return WEEK_COUNT; + fun getItem(position: Int): Object? { + return null } @Override - public Object getItem(int position) { - return null; - } - - @Override - public long getItemId(int position) { - return position; + fun getItemId(position: Int): Long { + return position.toLong() } @SuppressWarnings("unchecked") @Override - public View getView(int position, View convertView, ViewGroup parent) { - SimpleWeekView v; - HashMap<String, Integer> drawingParams = null; + fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + val v: SimpleWeekView + var drawingParams: HashMap<String?, Integer?>? = null if (convertView != null) { - v = (SimpleWeekView) convertView; + v = convertView as SimpleWeekView // We store the drawing parameters in the view so it can be recycled - drawingParams = (HashMap<String, Integer>) v.getTag(); + drawingParams = v.getTag() as HashMap<String?, Integer?> } else { - v = new SimpleWeekView(mContext); + v = SimpleWeekView(mContext) // Set up the new view - LayoutParams params = new LayoutParams( - LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); - v.setLayoutParams(params); - v.setClickable(true); - v.setOnTouchListener(this); + val params = LayoutParams( + LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT + ) + v.setLayoutParams(params) + v.setClickable(true) + v.setOnTouchListener(this) } if (drawingParams == null) { - drawingParams = new HashMap<String, Integer>(); + drawingParams = HashMap<String, Integer>() } - drawingParams.clear(); - - int selectedDay = -1; + drawingParams.clear() + var selectedDay = -1 if (mSelectedWeek == position) { - selectedDay = mSelectedDay.weekDay; + selectedDay = mSelectedDay.weekDay } // pass in all the view parameters - drawingParams.put(SimpleWeekView.VIEW_PARAMS_HEIGHT, - (parent.getHeight() - WEEK_7_OVERHANG_HEIGHT) / mNumWeeks); - drawingParams.put(SimpleWeekView.VIEW_PARAMS_SELECTED_DAY, selectedDay); - drawingParams.put(SimpleWeekView.VIEW_PARAMS_SHOW_WK_NUM, mShowWeekNumber ? 1 : 0); - drawingParams.put(SimpleWeekView.VIEW_PARAMS_WEEK_START, mFirstDayOfWeek); - 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.invalidate(); - - return v; + drawingParams.put( + SimpleWeekView.VIEW_PARAMS_HEIGHT, + (parent.getHeight() - WEEK_7_OVERHANG_HEIGHT) / mNumWeeks + ) + drawingParams.put(SimpleWeekView.VIEW_PARAMS_SELECTED_DAY, selectedDay) + drawingParams.put(SimpleWeekView.VIEW_PARAMS_SHOW_WK_NUM, if (mShowWeekNumber) 1 else 0) + drawingParams.put(SimpleWeekView.VIEW_PARAMS_WEEK_START, mFirstDayOfWeek) + 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.invalidate() + return v } /** @@ -249,25 +189,25 @@ public class SimpleWeeksAdapter extends BaseAdapter implements OnTouchListener { * * @param month The month to show as in focus [0-11] */ - public void updateFocusMonth(int month) { - mFocusMonth = month; - notifyDataSetChanged(); + fun updateFocusMonth(month: Int) { + mFocusMonth = month + notifyDataSetChanged() } @Override - public boolean onTouch(View v, MotionEvent event) { + fun onTouch(v: View, event: MotionEvent): Boolean { if (mGestureDetector.onTouchEvent(event)) { - SimpleWeekView view = (SimpleWeekView) v; - Time day = ((SimpleWeekView)v).getDayFromLocation(event.getX()); + val view: SimpleWeekView = v as SimpleWeekView + val day: Time = (v as SimpleWeekView).getDayFromLocation(event.getX()) if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Touched day at Row=" + view.mWeek + " day=" + day.toString()); + Log.d(TAG, "Touched day at Row=" + view.mWeek.toString() + " day=" + day.toString()) } if (day != null) { - onDayTapped(day); + onDayTapped(day) } - return true; + return true } - return false; + return false } /** @@ -275,28 +215,86 @@ public class SimpleWeeksAdapter extends BaseAdapter implements OnTouchListener { * * @param day The day that was tapped */ - protected void onDayTapped(Time day) { - day.hour = mSelectedDay.hour; - day.minute = mSelectedDay.minute; - day.second = mSelectedDay.second; - setSelectedDay(day); + protected fun onDayTapped(day: Time) { + day.hour = mSelectedDay.hour + day.minute = mSelectedDay.minute + day.second = mSelectedDay.second + selectedDay = day } - /** * This is here so we can identify single tap events and set the selected * day correctly */ - protected class CalendarGestureListener extends GestureDetector.SimpleOnGestureListener { + protected inner class CalendarGestureListener : GestureDetector.SimpleOnGestureListener() { @Override - public boolean onSingleTapUp(MotionEvent e) { - return true; + fun onSingleTapUp(e: MotionEvent?): Boolean { + return true } } - ListView mListView; + var mListView: ListView? = null + fun setListView(lv: ListView?) { + mListView = lv + } + + companion object { + private const val TAG = "MonthByWeek" + + /** + * The number of weeks to display at a time. + */ + const val WEEK_PARAMS_NUM_WEEKS = "num_weeks" + + /** + * Which month should be in focus currently. + */ + const val WEEK_PARAMS_FOCUS_MONTH = "focus_month" + + /** + * Whether the week number should be shown. Non-zero to show them. + */ + const val WEEK_PARAMS_SHOW_WEEK = "week_numbers" + + /** + * Which day the week should start on. [Time.SUNDAY] through + * [Time.SATURDAY]. + */ + const val WEEK_PARAMS_WEEK_START = "week_start" - public void setListView(ListView lv) { - mListView = lv; + /** + * The Julian day to highlight as selected. + */ + const val WEEK_PARAMS_JULIAN_DAY = "selected_day" + + /** + * 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 var DEFAULT_NUM_WEEKS = 6 + protected var DEFAULT_MONTH_FOCUS = 0 + protected var DEFAULT_DAYS_PER_WEEK = 7 + protected var DEFAULT_WEEK_HEIGHT = 32 + protected var WEEK_7_OVERHANG_HEIGHT = 7 + protected var mScale = 0f + } + + init { + mContext = context + + // Get default week start based on locale, subtracting one for use with android Time. + val cal: Calendar = Calendar.getInstance(Locale.getDefault()) + mFirstDayOfWeek = cal.getFirstDayOfWeek() - 1 + if (mScale == 0f) { + mScale = context.getResources().getDisplayMetrics().density + if (mScale != 1f) { + WEEK_7_OVERHANG_HEIGHT *= mScale.toInt() + } + } + init() + updateParams(params) } -} +}
\ No newline at end of file |