• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.wm.shell.shared
18 
19 import android.graphics.Typeface
20 import android.widget.TextView
21 import com.android.wm.shell.Flags
22 
23 /**
24  * Utility class to apply a specified typeface to a [TextView].
25  *
26  * This class provides a method, [setTypeface],
27  * to easily set a pre-defined font family and style to a given [TextView].
28  */
29 class TypefaceUtils {
30 
31     enum class FontFamily(val value: String) {
32         GSF_DISPLAY_LARGE("variable-display-large"),
33         GSF_DISPLAY_MEDIUM("variable-display-medium"),
34         GSF_DISPLAY_SMALL("variable-display-small"),
35         GSF_HEADLINE_LARGE("variable-headline-large"),
36         GSF_HEADLINE_MEDIUM("variable-headline-medium"),
37         GSF_HEADLINE_SMALL("variable-headline-small"),
38         GSF_TITLE_LARGE("variable-title-large"),
39         GSF_TITLE_MEDIUM("variable-title-medium"),
40         GSF_TITLE_SMALL("variable-title-small"),
41         GSF_LABEL_LARGE("variable-label-large"),
42         GSF_LABEL_MEDIUM("variable-label-medium"),
43         GSF_LABEL_SMALL("variable-label-small"),
44         GSF_BODY_LARGE("variable-body-large"),
45         GSF_BODY_MEDIUM("variable-body-medium"),
46         GSF_BODY_SMALL("variable-body-small"),
47         GSF_DISPLAY_LARGE_EMPHASIZED("variable-display-large-emphasized"),
48         GSF_DISPLAY_MEDIUM_EMPHASIZED("variable-display-medium-emphasized"),
49         GSF_DISPLAY_SMALL_EMPHASIZED("variable-display-small-emphasized"),
50         GSF_HEADLINE_LARGE_EMPHASIZED("variable-headline-large-emphasized"),
51         GSF_HEADLINE_MEDIUM_EMPHASIZED("variable-headline-medium-emphasized"),
52         GSF_HEADLINE_SMALL_EMPHASIZED("variable-headline-small-emphasized"),
53         GSF_TITLE_LARGE_EMPHASIZED("variable-title-large-emphasized"),
54         GSF_TITLE_MEDIUM_EMPHASIZED("variable-title-medium-emphasized"),
55         GSF_TITLE_SMALL_EMPHASIZED("variable-title-small-emphasized"),
56         GSF_LABEL_LARGE_EMPHASIZED("variable-label-large-emphasized"),
57         GSF_LABEL_MEDIUM_EMPHASIZED("variable-label-medium-emphasized"),
58         GSF_LABEL_SMALL_EMPHASIZED("variable-label-small-emphasized"),
59         GSF_BODY_LARGE_EMPHASIZED("variable-body-large-emphasized"),
60         GSF_BODY_MEDIUM_EMPHASIZED("variable-body-medium-emphasized"),
61         GSF_BODY_SMALL_EMPHASIZED("variable-body-small-emphasized"),
62     }
63 
64     companion object {
65         /**
66          * Sets the typeface of the provided [textView] to the specified [fontFamily] and [fontStyle].
67          *
68          * The typeface is only applied to the [TextView] when [Flags.enableGsf] is `true`.
69          * If [Flags.enableGsf] is `false`, this method has no effect.
70          *
71          * @param textView The [TextView] to which the typeface should be applied. If `null`, this method does nothing.
72          * @param fontFamily The desired [FontFamily] for the [TextView].
73          * @param fontStyle The desired font style (e.g., [Typeface.NORMAL], [Typeface.BOLD], [Typeface.ITALIC]). Defaults to [Typeface.NORMAL].
74          */
75         @JvmStatic
76         @JvmOverloads
setTypefacenull77         fun setTypeface(
78             textView: TextView?,
79             fontFamily: FontFamily,
80             fontStyle: Int = Typeface.NORMAL,
81         ) {
82             if (!Flags.enableGsf()) return
83             textView?.typeface = Typeface.create(fontFamily.value, fontStyle)
84         }
85     }
86 }
87