1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15 package com.android.systemui.statusbar.phone
16
17 import android.content.Context
18 import android.content.pm.ActivityInfo
19 import android.content.res.Configuration
20 import android.graphics.Rect
21 import android.os.LocaleList
22 import android.view.View.LAYOUT_DIRECTION_RTL
23 import com.android.systemui.dagger.SysUISingleton
24 import com.android.systemui.statusbar.policy.ConfigurationController
25
26 import java.util.ArrayList
27 import javax.inject.Inject
28
29 @SysUISingleton
30 class ConfigurationControllerImpl @Inject constructor(context: Context) : ConfigurationController {
31
32 private val listeners: MutableList<ConfigurationController.ConfigurationListener> = ArrayList()
33 private val lastConfig = Configuration()
34 private var density: Int = 0
35 private var smallestScreenWidth: Int = 0
36 private var maxBounds = Rect()
37 private var fontScale: Float = 0.toFloat()
38 private val inCarMode: Boolean
39 private var uiMode: Int = 0
40 private var localeList: LocaleList? = null
41 private val context: Context
42 private var layoutDirection: Int
43
44 init {
45 val currentConfig = context.resources.configuration
46 this.context = context
47 fontScale = currentConfig.fontScale
48 density = currentConfig.densityDpi
49 smallestScreenWidth = currentConfig.smallestScreenWidthDp
50 maxBounds.set(currentConfig.windowConfiguration.maxBounds)
51 inCarMode = currentConfig.uiMode and Configuration.UI_MODE_TYPE_MASK ==
52 Configuration.UI_MODE_TYPE_CAR
53 uiMode = currentConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK
54 localeList = currentConfig.locales
55 layoutDirection = currentConfig.layoutDirection
56 }
57
notifyThemeChangednull58 override fun notifyThemeChanged() {
59 val listeners = ArrayList(listeners)
60
61 listeners.filterForEach({ this.listeners.contains(it) }) {
62 it.onThemeChanged()
63 }
64 }
65
onConfigurationChangednull66 override fun onConfigurationChanged(newConfig: Configuration) {
67 // Avoid concurrent modification exception
68 val listeners = ArrayList(listeners)
69
70 listeners.filterForEach({ this.listeners.contains(it) }) {
71 it.onConfigChanged(newConfig)
72 }
73 val fontScale = newConfig.fontScale
74 val density = newConfig.densityDpi
75 val uiMode = newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK
76 val uiModeChanged = uiMode != this.uiMode
77 if (density != this.density || fontScale != this.fontScale ||
78 inCarMode && uiModeChanged) {
79 listeners.filterForEach({ this.listeners.contains(it) }) {
80 it.onDensityOrFontScaleChanged()
81 }
82 this.density = density
83 this.fontScale = fontScale
84 }
85
86 val smallestScreenWidth = newConfig.smallestScreenWidthDp
87 if (smallestScreenWidth != this.smallestScreenWidth) {
88 this.smallestScreenWidth = smallestScreenWidth
89 listeners.filterForEach({ this.listeners.contains(it) }) {
90 it.onSmallestScreenWidthChanged()
91 }
92 }
93
94 val maxBounds = newConfig.windowConfiguration.maxBounds
95 if (maxBounds != this.maxBounds) {
96 // Update our internal rect to have the same bounds, instead of using
97 // `this.maxBounds = maxBounds` directly. Setting it directly means that `maxBounds`
98 // would be a direct reference to windowConfiguration.maxBounds, so the if statement
99 // above would always fail. See b/245799099 for more information.
100 this.maxBounds.set(maxBounds)
101 listeners.filterForEach({ this.listeners.contains(it) }) {
102 it.onMaxBoundsChanged()
103 }
104 }
105
106 val localeList = newConfig.locales
107 if (localeList != this.localeList) {
108 this.localeList = localeList
109 listeners.filterForEach({ this.listeners.contains(it) }) {
110 it.onLocaleListChanged()
111 }
112 }
113
114 if (uiModeChanged) {
115 // We need to force the style re-evaluation to make sure that it's up to date
116 // and attrs were reloaded.
117 context.theme.applyStyle(context.themeResId, true)
118
119 this.uiMode = uiMode
120 listeners.filterForEach({ this.listeners.contains(it) }) {
121 it.onUiModeChanged()
122 }
123 }
124
125 if (layoutDirection != newConfig.layoutDirection) {
126 layoutDirection = newConfig.layoutDirection
127 listeners.filterForEach({ this.listeners.contains(it) }) {
128 it.onLayoutDirectionChanged(layoutDirection == LAYOUT_DIRECTION_RTL)
129 }
130 }
131
132 if (lastConfig.updateFrom(newConfig) and ActivityInfo.CONFIG_ASSETS_PATHS != 0) {
133 listeners.filterForEach({ this.listeners.contains(it) }) {
134 it.onThemeChanged()
135 }
136 }
137 }
138
addCallbacknull139 override fun addCallback(listener: ConfigurationController.ConfigurationListener) {
140 listeners.add(listener)
141 listener.onDensityOrFontScaleChanged()
142 }
143
removeCallbacknull144 override fun removeCallback(listener: ConfigurationController.ConfigurationListener) {
145 listeners.remove(listener)
146 }
147
isLayoutRtlnull148 override fun isLayoutRtl(): Boolean {
149 return layoutDirection == LAYOUT_DIRECTION_RTL
150 }
151 }
152
153 // This could be done with a Collection.filter and Collection.forEach, but Collection.filter
154 // creates a new array to store them in and we really don't need that here, so this provides
155 // a little more optimized inline version.
filterForEachnull156 inline fun <T> Collection<T>.filterForEach(f: (T) -> Boolean, execute: (T) -> Unit) {
157 forEach {
158 if (f.invoke(it)) {
159 execute.invoke(it)
160 }
161 }
162 }
163