• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 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.compatui.letterbox
18 
19 import android.annotation.ColorRes
20 import android.content.Context
21 import android.graphics.Color
22 import com.android.internal.R
23 import com.android.wm.shell.dagger.WMSingleton
24 import javax.inject.Inject
25 
26 /**
27  * Contains configuration properties for the letterbox implementation in Shell.
28  */
29 @WMSingleton
30 class LetterboxConfiguration @Inject constructor(
31     private val context: Context
32 ) {
33     // Color to use for the solid color letterbox background type.
34     private var letterboxBackgroundColorOverride: Color? = null
35 
36     // Color resource id for the solid color letterbox background type.
37     private var letterboxBackgroundColorResourceIdOverride: Int? = null
38 
39     // Default value for corners radius for activities presented in the letterbox mode.
40     // Values < 0 will be ignored.
41     private val letterboxActivityDefaultCornersRadius: Int
42 
43     // Current corners radius for activities presented in the letterbox mode.
44     // Values can be modified at runtime and values < 0 will be ignored.
45     private var letterboxActivityCornersRadius = 0
46 
47     init {
48         letterboxActivityDefaultCornersRadius = context.resources.getInteger(
49             R.integer.config_letterboxActivityCornersRadius
50         )
51         letterboxActivityCornersRadius = letterboxActivityDefaultCornersRadius
52     }
53 
54     /**
55      * Sets color of letterbox background which is used when using the solid background mode.
56      */
setLetterboxBackgroundColornull57     fun setLetterboxBackgroundColor(color: Color) {
58         letterboxBackgroundColorOverride = color
59     }
60 
61     /**
62      * Sets color ID of letterbox background which is used when using the solid background mode.
63      */
setLetterboxBackgroundColorResourceIdnull64     fun setLetterboxBackgroundColorResourceId(@ColorRes colorId: Int) {
65         letterboxBackgroundColorResourceIdOverride = colorId
66     }
67 
68     /**
69      * Gets color of letterbox background which is used when the solid color mode is active.
70      */
getLetterboxBackgroundColornull71     fun getLetterboxBackgroundColor(): Color {
72         if (letterboxBackgroundColorOverride != null) {
73             return letterboxBackgroundColorOverride!!
74         }
75         val colorId = if (letterboxBackgroundColorResourceIdOverride != null) {
76             letterboxBackgroundColorResourceIdOverride
77         } else {
78             R.color.config_letterboxBackgroundColor
79         }
80         // Query color dynamically because material colors extracted from wallpaper are updated
81         // when wallpaper is changed.
82         return Color.valueOf(context.getResources().getColor(colorId!!, null))
83     }
84 
85     /**
86      * Resets color of letterbox background to the default.
87      */
resetLetterboxBackgroundColornull88     fun resetLetterboxBackgroundColor() {
89         letterboxBackgroundColorOverride = null
90         letterboxBackgroundColorResourceIdOverride = null
91     }
92 
93     /**
94      * The background color for the Letterbox.
95      */
getBackgroundColorRgbArraynull96     fun getBackgroundColorRgbArray(): FloatArray = getLetterboxBackgroundColor().components
97 
98     /**
99      * Overrides corners radius for activities presented in the letterbox mode. Values < 0,
100      * will be ignored and corners of the activity won't be rounded.
101      */
102     fun setLetterboxActivityCornersRadius(cornersRadius: Int) {
103         letterboxActivityCornersRadius = cornersRadius
104     }
105 
106     /**
107      * Resets corners radius for activities presented in the letterbox mode.
108      */
resetLetterboxActivityCornersRadiusnull109     fun resetLetterboxActivityCornersRadius() {
110         letterboxActivityCornersRadius = letterboxActivityDefaultCornersRadius
111     }
112 
113     /**
114      * Whether corners of letterboxed activities are rounded.
115      */
isLetterboxActivityCornersRoundednull116     fun isLetterboxActivityCornersRounded(): Boolean {
117         return getLetterboxActivityCornersRadius() > 0
118     }
119 
120     /**
121      * Gets corners radius for activities presented in the letterbox mode.
122      */
getLetterboxActivityCornersRadiusnull123     fun getLetterboxActivityCornersRadius(): Int {
124         return maxOf(letterboxActivityCornersRadius, 0)
125     }
126 }
127