summaryrefslogtreecommitdiff
path: root/src/com/android/calendar/month/SimpleWeekView.kt
blob: cd8eb84cbd1927608c431f228d1dd2a6f4dd5574 (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
/*
 * 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.month

import com.android.calendar.R
import com.android.calendar.Utils
import android.app.Service
import android.content.Context
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Paint.Align
import android.graphics.Paint.Style
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.text.format.DateUtils
import android.text.format.Time
import android.view.MotionEvent
import android.view.View
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityManager
import java.security.InvalidParameterException
import java.util.HashMap

/**
 *
 *
 * This is a dynamic view for drawing a single week. It can be configured to
 * display the week number, start the week on a given day, or show a reduced
 * number of days. It is intended for use as a single view within a ListView.
 * See [SimpleWeeksAdapter] for usage.
 *
 */
open class SimpleWeekView(context: Context) : View(context) {
    // affects the padding on the sides of this view
    @JvmField protected var mPadding = 0
    @JvmField protected var r: Rect = Rect()
    @JvmField protected var p: Paint = Paint()
    @JvmField protected var mMonthNumPaint: Paint = Paint()
    @JvmField protected var mSelectedDayLine: Drawable

    // Cache the number strings so we don't have to recompute them each time
    @JvmField protected var mDayNumbers: Array<String?>? = null

    // How many days to display
    @JvmField protected var mNumDays = DEFAULT_NUM_DAYS

    // The number of days + a spot for week number if it is displayed
    @JvmField protected var mNumCells = mNumDays

    // Quick lookup for checking which days are in the focus month
    @JvmField protected var mFocusDay: BooleanArray = BooleanArray(mNumCells)

    // Quick lookup for checking which days are in an odd month (to set a different background)
    @JvmField protected var mOddMonth: BooleanArray = BooleanArray(mNumCells)

    // The Julian day of the first day displayed by this item
    @JvmField protected var mFirstJulianDay = -1

    // The month of the first day in this week
    @JvmField protected var firstMonth = -1

    // The month of the last day in this week
    @JvmField protected var lastMonth = -1

    // The position of this week, equivalent to weeks since the week of Jan 1st,
    // 1970
    @JvmField var mWeek = -1

    // Quick reference to the width of this view, matches parent
    @JvmField protected var mWidth = 0

    // The height this view should draw at in pixels, set by height param
    @JvmField protected var mHeight = DEFAULT_HEIGHT

    // Whether the week number should be shown
    @JvmField protected var mShowWeekNum = false

    // If this view contains the selected day
    @JvmField protected var mHasSelectedDay = false

    // If this view contains the today
    open protected var mHasToday = false

    // Which day is selected [0-6] or -1 if no day is selected
    @JvmField protected var mSelectedDay = DEFAULT_SELECTED_DAY

    // Which day is today [0-6] or -1 if no day is today
    @JvmField protected var mToday: Int = DEFAULT_SELECTED_DAY

    // Which day of the week to start on [0-6]
    @JvmField protected var mWeekStart = DEFAULT_WEEK_START

    // The left edge of the selected day
    @JvmField protected var mSelectedLeft = -1

    // The right edge of the selected day
    @JvmField protected var mSelectedRight = -1

    // The timezone to display times/dates in (used for determining when Today
    // is)
    @JvmField protected var mTimeZone: String = Time.getCurrentTimezone()
    @JvmField protected var mBGColor: Int
    @JvmField protected var mSelectedWeekBGColor: Int
    @JvmField protected var mFocusMonthColor: Int
    @JvmField protected var mOtherMonthColor: Int
    @JvmField protected var mDaySeparatorColor: Int
    @JvmField protected var mTodayOutlineColor: Int
    @JvmField protected var mWeekNumColor: Int

    /**
     * Sets all the parameters for displaying this week. The only required
     * parameter is the week number. Other parameters have a default value and
     * will only update if a new value is included, except for focus month,
     * which will always default to no focus month if no value is passed in. See
     * [.VIEW_PARAMS_HEIGHT] for more info on parameters.
     *
     * @param params A map of the new parameters, see
     * [.VIEW_PARAMS_HEIGHT]
     * @param tz The time zone this view should reference times in
     */
    open fun setWeekParams(params: HashMap<String?, Int?>, tz: String) {
        if (!params.containsKey(VIEW_PARAMS_WEEK)) {
            throw InvalidParameterException("You must specify the week number for this view")
        }
        setTag(params)
        mTimeZone = tz
        // We keep the current value for any params not present
        if (params.containsKey(VIEW_PARAMS_HEIGHT)) {
            mHeight = (params.get(VIEW_PARAMS_HEIGHT))!!.toInt()
            if (mHeight < MIN_HEIGHT) {
                mHeight = MIN_HEIGHT
            }
        }
        if (params.containsKey(VIEW_PARAMS_SELECTED_DAY)) {
            mSelectedDay = (params.get(VIEW_PARAMS_SELECTED_DAY))!!.toInt()
        }
        mHasSelectedDay = mSelectedDay != -1
        if (params.containsKey(VIEW_PARAMS_NUM_DAYS)) {
            mNumDays = (params.get(VIEW_PARAMS_NUM_DAYS))!!.toInt()
        }
        if (params.containsKey(VIEW_PARAMS_SHOW_WK_NUM)) {
            mShowWeekNum =
                    if (params.get(VIEW_PARAMS_SHOW_WK_NUM) != 0) {
                        true
                    } else {
                        false
                    }
        }
        mNumCells = if (mShowWeekNum) mNumDays + 1 else mNumDays

        // Allocate space for caching the day numbers and focus values
        mDayNumbers = arrayOfNulls(mNumCells)
        mFocusDay = BooleanArray(mNumCells)
        mOddMonth = BooleanArray(mNumCells)
        mWeek = (params.get(VIEW_PARAMS_WEEK))!!.toInt()
        val julianMonday: Int = Utils.getJulianMondayFromWeeksSinceEpoch(mWeek)
        val time = Time(tz)
        time.setJulianDay(julianMonday)

        // If we're showing the week number calculate it based on Monday
        var i = 0
        if (mShowWeekNum) {
            mDayNumbers!![0] = Integer.toString(time.getWeekNumber())
            i++
        }
        if (params.containsKey(VIEW_PARAMS_WEEK_START)) {
            mWeekStart = (params.get(VIEW_PARAMS_WEEK_START))!!.toInt()
        }

        // Now adjust our starting day based on the start day of the week
        // If the week is set to start on a Saturday the first week will be
        // Dec 27th 1969 -Jan 2nd, 1970
        if (time.weekDay !== mWeekStart) {
            var diff: Int = time.weekDay - mWeekStart
            if (diff < 0) {
                diff += 7
            }
            time.monthDay -= diff
            time.normalize(true)
        }
        mFirstJulianDay = Time.getJulianDay(time.toMillis(true), time.gmtoff)
        firstMonth = time.month

        // Figure out what day today is
        val today = Time(tz)
        today.setToNow()
        mHasToday = false
        mToday = -1
        val focusMonth = if (params.containsKey(VIEW_PARAMS_FOCUS_MONTH)) params.get(
                VIEW_PARAMS_FOCUS_MONTH
        ) else DEFAULT_FOCUS_MONTH
        while (i < mNumCells) {
            if (time.monthDay === 1) {
                firstMonth = time.month
            }
            mOddMonth[i] = time.month % 2 === 1
            if (time.month === focusMonth) {
                mFocusDay[i] = true
            } else {
                mFocusDay[i] = false
            }
            if (time.year === today.year && time.yearDay === today.yearDay) {
                mHasToday = true
                mToday = i
            }
            mDayNumbers!![i] = Integer.toString(time.monthDay++)
            time.normalize(true)
            i++
        }
        // We do one extra add at the end of the loop, if that pushed us to a
        // new month undo it
        if (time.monthDay === 1) {
            time.monthDay--
            time.normalize(true)
        }
        lastMonth = time.month
        updateSelectionPositions()
    }

    /**
     * Sets up the text and style properties for painting. Override this if you
     * want to use a different paint.
     */
    protected open fun initView() {
        p.setFakeBoldText(false)
        p.setAntiAlias(true)
        p.setTextSize(MINI_DAY_NUMBER_TEXT_SIZE.toFloat())
        p.setStyle(Style.FILL)
        mMonthNumPaint = Paint()
        mMonthNumPaint.setFakeBoldText(true)
        mMonthNumPaint.setAntiAlias(true)
        mMonthNumPaint.setTextSize(MINI_DAY_NUMBER_TEXT_SIZE.toFloat())
        mMonthNumPaint.setColor(mFocusMonthColor)
        mMonthNumPaint.setStyle(Style.FILL)
        mMonthNumPaint.setTextAlign(Align.CENTER)
    }

    /**
     * Returns the month of the first day in this week
     *
     * @return The month the first day of this view is in
     */
    fun getFirstMonth(): Int {
        return firstMonth
    }

    /**
     * Returns the month of the last day in this week
     *
     * @return The month the last day of this view is in
     */
    fun getLastMonth(): Int {
        return lastMonth
    }

    /**
     * Returns the julian day of the first day in this view.
     *
     * @return The julian day of the first day in the view.
     */
    fun getFirstJulianDay(): Int {
        return mFirstJulianDay
    }

    /**
     * Calculates the day that the given x position is in, accounting for week
     * number. Returns a Time referencing that day or null if
     *
     * @param x The x position of the touch event
     * @return A time object for the tapped day or null if the position wasn't
     * in a day
     */
    open fun getDayFromLocation(x: Float): Time? {
        val dayStart =
                if (mShowWeekNum) (mWidth - mPadding * 2) / mNumCells + mPadding else mPadding
        if (x < dayStart || x > mWidth - mPadding) {
            return null
        }
        // Selection is (x - start) / (pixels/day) == (x -s) * day / pixels
        val dayPosition = ((x - dayStart) * mNumDays / (mWidth - dayStart - mPadding)).toInt()
        var day = mFirstJulianDay + dayPosition
        val time = Time(mTimeZone)
        if (mWeek == 0) {
            // This week is weird...
            if (day < Time.EPOCH_JULIAN_DAY) {
                day++
            } else if (day == Time.EPOCH_JULIAN_DAY) {
                time.set(1, 0, 1970)
                time.normalize(true)
                return time
            }
        }
        time.setJulianDay(day)
        return time
    }

    @Override
    protected override fun onDraw(canvas: Canvas) {
        drawBackground(canvas)
        drawWeekNums(canvas)
        drawDaySeparators(canvas)
    }

    /**
     * This draws the selection highlight if a day is selected in this week.
     * Override this method if you wish to have a different background drawn.
     *
     * @param canvas The canvas to draw on
     */
    protected open fun drawBackground(canvas: Canvas) {
        if (mHasSelectedDay) {
            p.setColor(mSelectedWeekBGColor)
            p.setStyle(Style.FILL)
        } else {
            return
        }
        r.top = 1
        r.bottom = mHeight - 1
        r.left = mPadding
        r.right = mSelectedLeft
        canvas.drawRect(r, p)
        r.left = mSelectedRight
        r.right = mWidth - mPadding
        canvas.drawRect(r, p)
    }

    /**
     * Draws the week and month day numbers for this week. Override this method
     * if you need different placement.
     *
     * @param canvas The canvas to draw on
     */
    protected open fun drawWeekNums(canvas: Canvas) {
        val y = (mHeight + MINI_DAY_NUMBER_TEXT_SIZE) / 2 - DAY_SEPARATOR_WIDTH
        val nDays = mNumCells
        var i = 0
        val divisor = 2 * nDays
        if (mShowWeekNum) {
            p.setTextSize(MINI_WK_NUMBER_TEXT_SIZE.toFloat())
            p.setStyle(Style.FILL)
            p.setTextAlign(Align.CENTER)
            p.setAntiAlias(true)
            p.setColor(mWeekNumColor)
            val x = (mWidth - mPadding * 2) / divisor + mPadding
            canvas.drawText(mDayNumbers!![0] as String, x.toFloat(), y.toFloat(), p)
            i++
        }
        var isFocusMonth = mFocusDay[i]
        mMonthNumPaint.setColor(if (isFocusMonth) mFocusMonthColor else mOtherMonthColor)
        mMonthNumPaint.setFakeBoldText(false)
        while (i < nDays) {
            if (mFocusDay[i] != isFocusMonth) {
                isFocusMonth = mFocusDay[i]
                mMonthNumPaint.setColor(if (isFocusMonth) mFocusMonthColor else mOtherMonthColor)
            }
            if (mHasToday && mToday == i) {
                mMonthNumPaint.setTextSize(MINI_TODAY_NUMBER_TEXT_SIZE.toFloat())
                mMonthNumPaint.setFakeBoldText(true)
            }
            val x = (2 * i + 1) * (mWidth - mPadding * 2) / divisor + mPadding
            canvas.drawText(mDayNumbers!![i] as String, x.toFloat(), y.toFloat(),
                    mMonthNumPaint as Paint)
            if (mHasToday && mToday == i) {
                mMonthNumPaint.setTextSize(MINI_DAY_NUMBER_TEXT_SIZE.toFloat())
                mMonthNumPaint.setFakeBoldText(false)
            }
            i++
        }
    }

    /**
     * Draws a horizontal line for separating the weeks. Override this method if
     * you want custom separators.
     *
     * @param canvas The canvas to draw on
     */
    protected open fun drawDaySeparators(canvas: Canvas) {
        if (mHasSelectedDay) {
            r.top = 1
            r.bottom = mHeight - 1
            r.left = mSelectedLeft + 1
            r.right = mSelectedRight - 1
            p.setStrokeWidth(MINI_TODAY_OUTLINE_WIDTH.toFloat())
            p.setStyle(Style.STROKE)
            p.setColor(mTodayOutlineColor)
            canvas.drawRect(r, p)
        }
        if (mShowWeekNum) {
            p.setColor(mDaySeparatorColor)
            p.setStrokeWidth(DAY_SEPARATOR_WIDTH.toFloat())
            val x = (mWidth - mPadding * 2) / mNumCells + mPadding
            canvas.drawLine(x.toFloat(), 0f, x.toFloat(), mHeight.toFloat(), p)
        }
    }

    @Override
    protected override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        mWidth = w
        updateSelectionPositions()
    }

    /**
     * This calculates the positions for the selected day lines.
     */
    protected open fun updateSelectionPositions() {
        if (mHasSelectedDay) {
            var selectedPosition = mSelectedDay - mWeekStart
            if (selectedPosition < 0) {
                selectedPosition += 7
            }
            if (mShowWeekNum) {
                selectedPosition++
            }
            mSelectedLeft = (selectedPosition * (mWidth - mPadding * 2) / mNumCells +
                    mPadding)
            mSelectedRight = ((selectedPosition + 1) * (mWidth - mPadding * 2) / mNumCells +
                    mPadding)
        }
    }

    @Override
    protected override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mHeight)
    }

    @Override
    override fun onHoverEvent(event: MotionEvent): Boolean {
        val context: Context = getContext()
        // only send accessibility events if accessibility and exploration are
        // on.
        val am: AccessibilityManager = context
                .getSystemService(Service.ACCESSIBILITY_SERVICE) as AccessibilityManager
        if (!am.isEnabled() || !am.isTouchExplorationEnabled()) {
            return super.onHoverEvent(event)
        }
        if (event.getAction() !== MotionEvent.ACTION_HOVER_EXIT) {
            val hover: Time? = getDayFromLocation(event.getX())
            if (hover != null &&
                    (mLastHoverTime == null || Time.compare(hover, mLastHoverTime) !== 0)
            ) {
                val millis: Long = hover.toMillis(true)
                val date: String? = Utils.formatDateRange(
                        context, millis, millis,
                        DateUtils.FORMAT_SHOW_DATE
                )
                val accessEvent: AccessibilityEvent =
                        AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED)
                accessEvent.getText().add(date)
                sendAccessibilityEventUnchecked(accessEvent)
                mLastHoverTime = hover
            }
        }
        return true
    }

    @JvmField var mLastHoverTime: Time? = null

    companion object {
        private const val TAG = "MonthView"
        /**
         * These params can be passed into the view to control how it appears.
         * [.VIEW_PARAMS_WEEK] is the only required field, though the default
         * values are unlikely to fit most layouts correctly.
         */
        /**
         * This sets the height of this week in pixels
         */
        const val VIEW_PARAMS_HEIGHT = "height"

        /**
         * This specifies the position (or weeks since the epoch) of this week,
         * calculated using [Utils.getWeeksSinceEpochFromJulianDay]
         */
        const val VIEW_PARAMS_WEEK = "week"

        /**
         * This sets one of the days in this view as selected [Time.SUNDAY]
         * through [Time.SATURDAY].
         */
        const val VIEW_PARAMS_SELECTED_DAY = "selected_day"

        /**
         * Which day the week should start on. [Time.SUNDAY] through
         * [Time.SATURDAY].
         */
        const val VIEW_PARAMS_WEEK_START = "week_start"

        /**
         * How many days to display at a time. Days will be displayed starting with
         * [.mWeekStart].
         */
        const val VIEW_PARAMS_NUM_DAYS = "num_days"

        /**
         * Which month is currently in focus, as defined by [Time.month]
         * [0-11].
         */
        const val VIEW_PARAMS_FOCUS_MONTH = "focus_month"

        /**
         * If this month should display week numbers. false if 0, true otherwise.
         */
        const val VIEW_PARAMS_SHOW_WK_NUM = "show_wk_num"
        protected var DEFAULT_HEIGHT = 32
        protected var MIN_HEIGHT = 10
        protected const val DEFAULT_SELECTED_DAY = -1
        protected val DEFAULT_WEEK_START: Int = Time.SUNDAY
        protected const val DEFAULT_NUM_DAYS = 7
        protected const val DEFAULT_SHOW_WK_NUM = 0
        protected const val DEFAULT_FOCUS_MONTH = -1
        protected var DAY_SEPARATOR_WIDTH = 1
        protected var MINI_DAY_NUMBER_TEXT_SIZE = 14
        protected var MINI_WK_NUMBER_TEXT_SIZE = 12
        protected var MINI_TODAY_NUMBER_TEXT_SIZE = 18
        protected var MINI_TODAY_OUTLINE_WIDTH = 2
        protected var WEEK_NUM_MARGIN_BOTTOM = 4

        // used for scaling to the device density
        @JvmStatic protected var mScale = 0f
    }

    init {
        val res: Resources = context.getResources()
        mBGColor = res.getColor(R.color.month_bgcolor)
        mSelectedWeekBGColor = res.getColor(R.color.month_selected_week_bgcolor)
        mFocusMonthColor = res.getColor(R.color.month_mini_day_number)
        mOtherMonthColor = res.getColor(R.color.month_other_month_day_number)
        mDaySeparatorColor = res.getColor(R.color.month_grid_lines)
        mTodayOutlineColor = res.getColor(R.color.mini_month_today_outline_color)
        mWeekNumColor = res.getColor(R.color.month_week_num_color)
        mSelectedDayLine = res.getDrawable(R.drawable.dayline_minical_holo_light)
        if (mScale == 0f) {
            mScale = context.getResources().getDisplayMetrics().density
            if (mScale != 1f) {
                DEFAULT_HEIGHT *= mScale.toInt()
                MIN_HEIGHT *= mScale.toInt()
                MINI_DAY_NUMBER_TEXT_SIZE *= mScale.toInt()
                MINI_TODAY_NUMBER_TEXT_SIZE *= mScale.toInt()
                MINI_TODAY_OUTLINE_WIDTH *= mScale.toInt()
                WEEK_NUM_MARGIN_BOTTOM *= mScale.toInt()
                DAY_SEPARATOR_WIDTH *= mScale.toInt()
                MINI_WK_NUMBER_TEXT_SIZE *= mScale.toInt()
            }
        }

        // Sets up any standard paints that will be used
        initView()
    }
}