blob: 54ef623e95ab8136312a7720e57fb66158413873 [file] [log] [blame]
Jason Monkae7ced22018-08-22 16:56:58 -04001/*
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
15package com.android.systemui.statusbar.phone
16
17import android.content.Context
18import android.content.pm.ActivityInfo
19import android.content.res.Configuration
20import android.os.LocaleList
Jason Monkae7ced22018-08-22 16:56:58 -040021import com.android.systemui.statusbar.policy.ConfigurationController
22
23import java.util.ArrayList
24
Dave Mankoffbba732d2019-11-20 13:04:54 -050025class ConfigurationControllerImpl(context: Context) : ConfigurationController {
Jason Monkae7ced22018-08-22 16:56:58 -040026
27 private val listeners: MutableList<ConfigurationController.ConfigurationListener> = ArrayList()
28 private val lastConfig = Configuration()
29 private var density: Int = 0
30 private var fontScale: Float = 0.toFloat()
31 private val inCarMode: Boolean
32 private var uiMode: Int = 0
33 private var localeList: LocaleList? = null
Lucas Dupinfae63a22018-09-18 10:55:15 -070034 private val context: Context
Jason Monkae7ced22018-08-22 16:56:58 -040035
36 init {
37 val currentConfig = context.resources.configuration
Lucas Dupinfae63a22018-09-18 10:55:15 -070038 this.context = context
Jason Monkae7ced22018-08-22 16:56:58 -040039 fontScale = currentConfig.fontScale
40 density = currentConfig.densityDpi
41 inCarMode = currentConfig.uiMode and Configuration.UI_MODE_TYPE_MASK ==
42 Configuration.UI_MODE_TYPE_CAR
43 uiMode = currentConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK
44 localeList = currentConfig.locales
45 }
46
47 override fun notifyThemeChanged() {
48 val listeners = ArrayList(listeners)
49
50 listeners.filterForEach({ this.listeners.contains(it) }) {
51 it.onThemeChanged()
52 }
53 }
54
55 override fun onConfigurationChanged(newConfig: Configuration) {
56 // Avoid concurrent modification exception
57 val listeners = ArrayList(listeners)
58
59 listeners.filterForEach({ this.listeners.contains(it) }) {
60 it.onConfigChanged(newConfig)
61 }
62 val fontScale = newConfig.fontScale
63 val density = newConfig.densityDpi
64 val uiMode = newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK
65 val uiModeChanged = uiMode != this.uiMode
66 if (density != this.density || fontScale != this.fontScale ||
67 inCarMode && uiModeChanged) {
68 listeners.filterForEach({ this.listeners.contains(it) }) {
69 it.onDensityOrFontScaleChanged()
70 }
71 this.density = density
72 this.fontScale = fontScale
73 }
74
75 val localeList = newConfig.locales
76 if (localeList != this.localeList) {
77 this.localeList = localeList
78 listeners.filterForEach({ this.listeners.contains(it) }) {
79 it.onLocaleListChanged()
80 }
81 }
82
83 if (uiModeChanged) {
Lucas Dupinfae63a22018-09-18 10:55:15 -070084 // We need to force the style re-evaluation to make sure that it's up to date
85 // and attrs were reloaded.
86 context.theme.applyStyle(context.themeResId, true)
87
Jason Monkae7ced22018-08-22 16:56:58 -040088 this.uiMode = uiMode
89 listeners.filterForEach({ this.listeners.contains(it) }) {
90 it.onUiModeChanged()
91 }
92 }
93
94 if (lastConfig.updateFrom(newConfig) and ActivityInfo.CONFIG_ASSETS_PATHS != 0) {
95 listeners.filterForEach({ this.listeners.contains(it) }) {
96 it.onOverlayChanged()
97 }
98 }
99 }
100
101 override fun addCallback(listener: ConfigurationController.ConfigurationListener) {
102 listeners.add(listener)
103 listener.onDensityOrFontScaleChanged()
104 }
105
106 override fun removeCallback(listener: ConfigurationController.ConfigurationListener) {
107 listeners.remove(listener)
108 }
109}
110
111// This could be done with a Collection.filter and Collection.forEach, but Collection.filter
112// creates a new array to store them in and we really don't need that here, so this provides
113// a little more optimized inline version.
114inline fun <T> Collection<T>.filterForEach(f: (T) -> Boolean, execute: (T) -> Unit) {
115 forEach {
116 if (f.invoke(it)) {
117 execute.invoke(it)
118 }
119 }
120}