mirror of
https://gitlab.linphone.org/BC/public/linphone-android.git
synced 2026-01-17 11:28:06 +00:00
Updated way of setting light/dark/auto mode
This commit is contained in:
parent
e6d33a9e1a
commit
fa796b9609
4 changed files with 69 additions and 16 deletions
|
|
@ -19,11 +19,15 @@
|
|||
*/
|
||||
package org.linphone.compatibility
|
||||
|
||||
import android.app.UiModeManager
|
||||
import android.content.Context
|
||||
import android.graphics.RenderEffect
|
||||
import android.graphics.Shader
|
||||
import android.os.Build
|
||||
import android.view.View
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.core.content.ContextCompat
|
||||
import org.linphone.core.tools.Log
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.S)
|
||||
class Api31Compatibility {
|
||||
|
|
@ -38,5 +42,29 @@ class Api31Compatibility {
|
|||
fun removeBlurRenderEffect(view: View) {
|
||||
view.setRenderEffect(null)
|
||||
}
|
||||
|
||||
fun forceDarkMode(context: Context) {
|
||||
val uiManager = ContextCompat.getSystemService(context, UiModeManager::class.java)
|
||||
if (uiManager == null) {
|
||||
Log.e("$TAG Failed to get UiModeManager system service!")
|
||||
}
|
||||
uiManager?.setApplicationNightMode(UiModeManager.MODE_NIGHT_YES)
|
||||
}
|
||||
|
||||
fun forceLightMode(context: Context) {
|
||||
val uiManager = ContextCompat.getSystemService(context, UiModeManager::class.java)
|
||||
if (uiManager == null) {
|
||||
Log.e("$TAG Failed to get UiModeManager system service!")
|
||||
}
|
||||
uiManager?.setApplicationNightMode(UiModeManager.MODE_NIGHT_NO)
|
||||
}
|
||||
|
||||
fun setAutoLightDarkMode(context: Context) {
|
||||
val uiManager = ContextCompat.getSystemService(context, UiModeManager::class.java)
|
||||
if (uiManager == null) {
|
||||
Log.e("$TAG Failed to get UiModeManager system service!")
|
||||
}
|
||||
uiManager?.setApplicationNightMode(UiModeManager.MODE_NIGHT_AUTO)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ import android.app.Service
|
|||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.view.View
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import org.linphone.core.tools.Log
|
||||
import org.linphone.mediastream.Version
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
|
|
@ -104,5 +106,32 @@ class Compatibility {
|
|||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun forceDarkMode(context: Context) {
|
||||
Log.i("$TAG Forcing dark/night theme")
|
||||
if (Version.sdkAboveOrEqual(Version.API31_ANDROID_12)) {
|
||||
Api31Compatibility.forceDarkMode(context)
|
||||
} else {
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
|
||||
}
|
||||
}
|
||||
|
||||
fun forceLightMode(context: Context) {
|
||||
Log.i("$TAG Forcing light/day theme")
|
||||
if (Version.sdkAboveOrEqual(Version.API31_ANDROID_12)) {
|
||||
Api31Compatibility.forceLightMode(context)
|
||||
} else {
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
||||
}
|
||||
}
|
||||
|
||||
fun setAutoLightDarkMode(context: Context) {
|
||||
Log.i("$TAG Following Android's choice for light/dark theme")
|
||||
if (Version.sdkAboveOrEqual(Version.API31_ANDROID_12)) {
|
||||
Api31Compatibility.setAutoLightDarkMode(context)
|
||||
} else {
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,10 +23,10 @@ import android.annotation.SuppressLint
|
|||
import android.content.res.Configuration
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.core.view.WindowCompat
|
||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||
import org.linphone.LinphoneApplication.Companion.corePreferences
|
||||
import org.linphone.compatibility.Compatibility
|
||||
import org.linphone.core.tools.Log
|
||||
|
||||
open class GenericActivity : AppCompatActivity() {
|
||||
|
|
@ -44,20 +44,18 @@ open class GenericActivity : AppCompatActivity() {
|
|||
|
||||
val nightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
|
||||
val darkModeEnabled = corePreferences.darkMode
|
||||
Log.i("$TAG Theme selected in config file is [$darkModeEnabled]")
|
||||
Log.i(
|
||||
"$TAG Theme selected in config file is [${if (darkModeEnabled == -1) "auto" else if (darkModeEnabled == 0) "light" else "dark"}]"
|
||||
)
|
||||
when (nightMode) {
|
||||
Configuration.UI_MODE_NIGHT_NO, Configuration.UI_MODE_NIGHT_UNDEFINED -> {
|
||||
if (darkModeEnabled == 1) {
|
||||
// Force dark mode
|
||||
Log.w("$TAG Forcing night mode")
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
|
||||
Compatibility.forceDarkMode(this)
|
||||
}
|
||||
}
|
||||
Configuration.UI_MODE_NIGHT_YES -> {
|
||||
if (darkModeEnabled == 0) {
|
||||
// Force light mode
|
||||
Log.w("$TAG Forcing day mode")
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
||||
Compatibility.forceLightMode(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,10 +7,10 @@ import android.view.ViewGroup
|
|||
import android.widget.AdapterView
|
||||
import android.widget.ArrayAdapter
|
||||
import androidx.annotation.UiThread
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||
import org.linphone.R
|
||||
import org.linphone.compatibility.Compatibility
|
||||
import org.linphone.core.tools.Log
|
||||
import org.linphone.databinding.SettingsFragmentBinding
|
||||
import org.linphone.ui.main.fragment.GenericFragment
|
||||
|
|
@ -45,13 +45,11 @@ class SettingsFragment : GenericFragment() {
|
|||
Log.i("$TAG Selected theme is now [$label] ($value)")
|
||||
viewModel.setTheme(value)
|
||||
|
||||
AppCompatDelegate.setDefaultNightMode(
|
||||
when (value) {
|
||||
0 -> AppCompatDelegate.MODE_NIGHT_NO
|
||||
1 -> AppCompatDelegate.MODE_NIGHT_YES
|
||||
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
||||
}
|
||||
)
|
||||
when (value) {
|
||||
0 -> Compatibility.forceLightMode(requireContext())
|
||||
1 -> Compatibility.forceDarkMode(requireContext())
|
||||
else -> Compatibility.setAutoLightDarkMode(requireContext())
|
||||
}
|
||||
}
|
||||
|
||||
override fun onNothingSelected(parent: AdapterView<*>?) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue