Jason Monk | ae7ced2 | 2018-08-22 16:56:58 -0400 | [diff] [blame] | 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 |
Jason Monk | ae7ced2 | 2018-08-22 16:56:58 -0400 | [diff] [blame] | 21 | import com.android.systemui.statusbar.policy.ConfigurationController |
| 22 | |
| 23 | import java.util.ArrayList |
| 24 | |
Dave Mankoff | bba732d | 2019-11-20 13:04:54 -0500 | [diff] [blame] | 25 | class ConfigurationControllerImpl(context: Context) : ConfigurationController { |
Jason Monk | ae7ced2 | 2018-08-22 16:56:58 -0400 | [diff] [blame] | 26 | |
| 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 Dupin | fae63a2 | 2018-09-18 10:55:15 -0700 | [diff] [blame] | 34 | private val context: Context |
Jason Monk | ae7ced2 | 2018-08-22 16:56:58 -0400 | [diff] [blame] | 35 | |
| 36 | init { |
| 37 | val currentConfig = context.resources.configuration |
Lucas Dupin | fae63a2 | 2018-09-18 10:55:15 -0700 | [diff] [blame] | 38 | this.context = context |
Jason Monk | ae7ced2 | 2018-08-22 16:56:58 -0400 | [diff] [blame] | 39 | 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 Dupin | fae63a2 | 2018-09-18 10:55:15 -0700 | [diff] [blame] | 84 | // 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 Monk | ae7ced2 | 2018-08-22 16:56:58 -0400 | [diff] [blame] | 88 | 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. |
| 114 | inline 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 | } |