1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.calendar
17 
18 import android.content.Context
19 import android.graphics.Canvas
20 import android.graphics.ColorFilter
21 import android.graphics.Paint
22 import android.graphics.PixelFormat
23 import android.graphics.Rect
24 import android.graphics.Typeface
25 import android.graphics.drawable.Drawable
26 
27 /**
28  * A custom view to draw the day of the month in the today button in the options menu
29  */
30 class DayOfMonthDrawable(c: Context) : Drawable() {
31     private var mDayOfMonth = "1"
32     private val mPaint: Paint
33     private val mTextBounds: Rect = Rect()
drawnull34     override fun draw(canvas: Canvas) {
35         mPaint.getTextBounds(mDayOfMonth, 0, mDayOfMonth.length, mTextBounds)
36         val textHeight: Int = mTextBounds.bottom - mTextBounds.top
37         val bounds: Rect = getBounds()
38         canvas.drawText(
39             mDayOfMonth, (bounds.right).toFloat() / 2f, ((bounds.bottom).toFloat() +
40                 textHeight + 1) / 2f, mPaint
41         )
42     }
43 
setAlphanull44     override fun setAlpha(alpha: Int) {
45         mPaint.setAlpha(alpha)
46     }
47 
setColorFilternull48     override fun setColorFilter(cf: ColorFilter?) {
49         // Ignore
50     }
51 
getOpacitynull52     override fun getOpacity(): Int {
53         return PixelFormat.UNKNOWN
54     }
55 
setDayOfMonthnull56     fun setDayOfMonth(day: Int) {
57         mDayOfMonth = Integer.toString(day)
58         invalidateSelf()
59     }
60 
61     companion object {
62         private var mTextSize = 14f
63     }
64 
65     init {
66         mTextSize = c.getResources().getDimension(R.dimen.today_icon_text_size)
67         mPaint = Paint()
68         mPaint.setAlpha(255)
69         mPaint.setColor(-0x888889)
70         mPaint.setTypeface(Typeface.DEFAULT_BOLD)
71         mPaint.setTextSize(mTextSize)
72         mPaint.setTextAlign(Paint.Align.CENTER)
73     }
74 }