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 
17 package com.android.systemui.statusbar.policy
18 
19 import android.content.Context
20 import android.text.StaticLayout
21 import android.util.AttributeSet
22 import android.widget.TextView
23 import com.android.systemui.res.R
24 
25 /**
26  * View for showing a date that can toggle between two different formats depending on size.
27  *
28  * If no pattern can fit, it will display empty.
29  *
30  * @see R.styleable.VariableDateView_longDatePattern
31  * @see R.styleable.VariableDateView_shortDatePattern
32  */
33 class VariableDateView(context: Context, attrs: AttributeSet) : TextView(context, attrs) {
34 
35     val longerPattern: String
36     val shorterPattern: String
37 
38     init {
39         val a = context.theme.obtainStyledAttributes(
40                 attrs,
41                 R.styleable.VariableDateView,
42                 0, 0)
43         longerPattern = a.getString(R.styleable.VariableDateView_longDatePattern)
44                 ?: context.getString(R.string.system_ui_date_pattern)
45         shorterPattern = a.getString(R.styleable.VariableDateView_shortDatePattern)
46                 ?: context.getString(R.string.abbrev_month_day_no_year)
47 
48         a.recycle()
49     }
50 
51     /**
52      * Freeze the pattern switching
53      *
54      * Use during animations if the container will change its size but this view should not change
55      */
56     var freezeSwitching = false
57 
58     private var onMeasureListener: OnMeasureListener? = null
59 
onAttachnull60     fun onAttach(listener: OnMeasureListener?) {
61         onMeasureListener = listener
62     }
63 
onMeasurenull64     override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
65         val availableWidth = MeasureSpec.getSize(widthMeasureSpec) - paddingStart - paddingEnd
66         if (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED && !freezeSwitching) {
67             onMeasureListener?.onMeasureAction(availableWidth, widthMeasureSpec)
68         }
69         super.onMeasure(widthMeasureSpec, heightMeasureSpec)
70     }
71 
getDesiredWidthForTextnull72     fun getDesiredWidthForText(text: CharSequence): Float {
73         return StaticLayout.getDesiredWidth(text, paint)
74     }
75 
76     interface OnMeasureListener {
onMeasureActionnull77         fun onMeasureAction(availableWidth: Int, widthMeasureSpec: Int)
78     }
79 }