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.os.LocaleList 21 import android.view.View.LAYOUT_DIRECTION_RTL 22 import com.android.systemui.statusbar.policy.ConfigurationController 23 24 import java.util.ArrayList 25 26 class ConfigurationControllerImpl(context: Context) : ConfigurationController { 27 28 private val listeners: MutableList<ConfigurationController.ConfigurationListener> = ArrayList() 29 private val lastConfig = Configuration() 30 private var density: Int = 0 31 private var smallestScreenWidth: Int = 0 32 private var fontScale: Float = 0.toFloat() 33 private val inCarMode: Boolean 34 private var uiMode: Int = 0 35 private var localeList: LocaleList? = null 36 private val context: Context 37 private var layoutDirection: Int 38 39 init { 40 val currentConfig = context.resources.configuration 41 this.context = context 42 fontScale = currentConfig.fontScale 43 density = currentConfig.densityDpi 44 smallestScreenWidth = currentConfig.smallestScreenWidthDp 45 inCarMode = currentConfig.uiMode and Configuration.UI_MODE_TYPE_MASK == 46 Configuration.UI_MODE_TYPE_CAR 47 uiMode = currentConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK 48 localeList = currentConfig.locales 49 layoutDirection = currentConfig.layoutDirection 50 } 51 notifyThemeChangednull52 override fun notifyThemeChanged() { 53 val listeners = ArrayList(listeners) 54 55 listeners.filterForEach({ this.listeners.contains(it) }) { 56 it.onThemeChanged() 57 } 58 } 59 onConfigurationChangednull60 override fun onConfigurationChanged(newConfig: Configuration) { 61 // Avoid concurrent modification exception 62 val listeners = ArrayList(listeners) 63 64 listeners.filterForEach({ this.listeners.contains(it) }) { 65 it.onConfigChanged(newConfig) 66 } 67 val fontScale = newConfig.fontScale 68 val density = newConfig.densityDpi 69 val uiMode = newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK 70 val uiModeChanged = uiMode != this.uiMode 71 if (density != this.density || fontScale != this.fontScale || 72 inCarMode && uiModeChanged) { 73 listeners.filterForEach({ this.listeners.contains(it) }) { 74 it.onDensityOrFontScaleChanged() 75 } 76 this.density = density 77 this.fontScale = fontScale 78 } 79 80 val smallestScreenWidth = newConfig.smallestScreenWidthDp 81 if (smallestScreenWidth != this.smallestScreenWidth) { 82 this.smallestScreenWidth = smallestScreenWidth 83 listeners.filterForEach({ this.listeners.contains(it) }) { 84 it.onSmallestScreenWidthChanged() 85 } 86 } 87 88 val localeList = newConfig.locales 89 if (localeList != this.localeList) { 90 this.localeList = localeList 91 listeners.filterForEach({ this.listeners.contains(it) }) { 92 it.onLocaleListChanged() 93 } 94 } 95 96 if (uiModeChanged) { 97 // We need to force the style re-evaluation to make sure that it's up to date 98 // and attrs were reloaded. 99 context.theme.applyStyle(context.themeResId, true) 100 101 this.uiMode = uiMode 102 listeners.filterForEach({ this.listeners.contains(it) }) { 103 it.onUiModeChanged() 104 } 105 } 106 107 if (layoutDirection != newConfig.layoutDirection) { 108 layoutDirection = newConfig.layoutDirection 109 listeners.filterForEach({ this.listeners.contains(it) }) { 110 it.onLayoutDirectionChanged(layoutDirection == LAYOUT_DIRECTION_RTL) 111 } 112 } 113 114 if (lastConfig.updateFrom(newConfig) and ActivityInfo.CONFIG_ASSETS_PATHS != 0) { 115 listeners.filterForEach({ this.listeners.contains(it) }) { 116 it.onOverlayChanged() 117 } 118 } 119 } 120 addCallbacknull121 override fun addCallback(listener: ConfigurationController.ConfigurationListener) { 122 listeners.add(listener) 123 listener.onDensityOrFontScaleChanged() 124 } 125 removeCallbacknull126 override fun removeCallback(listener: ConfigurationController.ConfigurationListener) { 127 listeners.remove(listener) 128 } 129 isLayoutRtlnull130 override fun isLayoutRtl(): Boolean { 131 return layoutDirection == LAYOUT_DIRECTION_RTL 132 } 133 } 134 135 // This could be done with a Collection.filter and Collection.forEach, but Collection.filter 136 // creates a new array to store them in and we really don't need that here, so this provides 137 // a little more optimized inline version. filterForEachnull138inline fun <T> Collection<T>.filterForEach(f: (T) -> Boolean, execute: (T) -> Unit) { 139 forEach { 140 if (f.invoke(it)) { 141 execute.invoke(it) 142 } 143 } 144 } 145