From 97d6c852c8070905f026a383e845706a66cb2168 Mon Sep 17 00:00:00 2001 From: Yasan Ghaffarian Date: Mon, 12 Sep 2022 11:43:52 +0430 Subject: [PATCH] Automatic icon reload on night mode change (#2930) Co-authored-by: Daria Hamrah Paytakht Co-authored-by: Goooler --- AndroidManifest.xml | 2 +- .../src/app/lawnchair/LawnchairLauncher.kt | 34 +++++++++++++++++++ quickstep/AndroidManifest-launcher.xml | 2 +- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index d34430c428..cec30bd37d 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -53,7 +53,7 @@ android:stateNotNeeded="true" android:windowSoftInputMode="adjustPan" android:screenOrientation="unspecified" - android:configChanges="keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenSize|screenLayout|smallestScreenSize" + android:configChanges="keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenSize|screenLayout|smallestScreenSize|uiMode" android:resizeableActivity="true" android:resumeWhilePausing="true" android:taskAffinity="" diff --git a/lawnchair/src/app/lawnchair/LawnchairLauncher.kt b/lawnchair/src/app/lawnchair/LawnchairLauncher.kt index 781ba2ad01..5e9b9d0dc7 100644 --- a/lawnchair/src/app/lawnchair/LawnchairLauncher.kt +++ b/lawnchair/src/app/lawnchair/LawnchairLauncher.kt @@ -19,6 +19,8 @@ package app.lawnchair import android.content.Context import android.content.Intent import android.content.IntentSender +import android.content.pm.ActivityInfo +import android.content.res.Configuration import android.os.Bundle import android.os.Handler import android.os.Looper @@ -162,6 +164,7 @@ class LawnchairLauncher : QuickstepLauncher(), LifecycleOwner, private val themeProvider by lazy { ThemeProvider.INSTANCE.get(this) } private lateinit var colorScheme: ColorScheme + private var mOldConfig: Configuration? = null private val noStatusBarStateListener = object : StateManager.StateListener { override fun onStateTransitionStart(toState: LauncherState) { @@ -183,6 +186,7 @@ class LawnchairLauncher : QuickstepLauncher(), LifecycleOwner, savedStateRegistryController.performRestore(savedInstanceState) super.onCreate(savedInstanceState) lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) + mOldConfig = Configuration(resources.configuration) prefs.launcherTheme.subscribeChanges(this, ::updateTheme) @@ -359,6 +363,16 @@ class LawnchairLauncher : QuickstepLauncher(), LifecycleOwner, return defaultOverlay } + override fun onConfigurationChanged(newConfig: Configuration) { + if (isNightConfigChanged(newConfig)) { + // Reload icons when night mode changes + // Done to avoid incorrect icons being shown when the active icon pack is dynamic + forceReloadIcons() + } + mOldConfig = newConfig + super.onConfigurationChanged(newConfig) + } + private fun restartIfPending() { when { sRestartFlags and FLAG_RESTART != 0 -> lawnchairApp.restart(false) @@ -390,6 +404,26 @@ class LawnchairLauncher : QuickstepLauncher(), LifecycleOwner, } } + /** + * Reset the value of [PreferenceManager.iconPackPackage] to force reload icons in the launcher. + */ + private fun forceReloadIcons() { + val iconPack = prefs.iconPackPackage.get() + if (iconPack != "") { + prefs.iconPackPackage.set("") + prefs.iconPackPackage.set(iconPack) + } + } + + private fun isNightConfigChanged(newConfig: Configuration): Boolean { + return newConfig.diff(mOldConfig) and ActivityInfo.CONFIG_UI_MODE != 0 && + newConfig.isOnDarkMode() != mOldConfig?.isOnDarkMode() + } + + private fun Configuration.isOnDarkMode(): Boolean { + return uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES + } + companion object { private const val FLAG_RECREATE = 1 shl 0 private const val FLAG_RESTART = 1 shl 1 diff --git a/quickstep/AndroidManifest-launcher.xml b/quickstep/AndroidManifest-launcher.xml index e6119fac7a..7cc7f04262 100644 --- a/quickstep/AndroidManifest-launcher.xml +++ b/quickstep/AndroidManifest-launcher.xml @@ -52,7 +52,7 @@ android:stateNotNeeded="true" android:windowSoftInputMode="adjustPan" android:screenOrientation="unspecified" - android:configChanges="keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenSize|screenLayout|smallestScreenSize" + android:configChanges="keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenSize|screenLayout|smallestScreenSize|uiMode" android:resizeableActivity="true" android:resumeWhilePausing="true" android:taskAffinity=""