Fixed crash on API lower than 27 because of WallpaperAccentManager

This commit is contained in:
Prathamesh More 2022-01-17 16:45:35 +05:30
parent df21419b8b
commit 93ad0d0a90

View file

@ -1,24 +1,21 @@
package code.name.monkey.retromusic.helper package code.name.monkey.retromusic.helper
import android.app.WallpaperColors
import android.app.WallpaperManager import android.app.WallpaperManager
import android.content.Context import android.content.Context
import android.os.Build
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import androidx.annotation.RequiresApi
import code.name.monkey.appthemehelper.ThemeStore import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.appthemehelper.util.VersionUtils import code.name.monkey.appthemehelper.util.VersionUtils
import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.PreferenceUtil
class WallpaperAccentManager(val context: Context) { class WallpaperAccentManager(val context: Context) {
@RequiresApi(Build.VERSION_CODES.O_MR1) private val onColorsChangedListener by lazy {
private val onColorsChangedListener =
WallpaperManager.OnColorsChangedListener { colors, which -> WallpaperManager.OnColorsChangedListener { colors, which ->
if (which == WallpaperManager.FLAG_SYSTEM && colors != null) { updateColors(colors, which)
ThemeStore.editTheme(context).wallpaperColor(colors.primaryColor.toArgb()).commit()
}
} }
}
fun init() { fun init() {
if (VersionUtils.hasOreoMR1() && PreferenceUtil.wallpaperAccent) { if (VersionUtils.hasOreoMR1() && PreferenceUtil.wallpaperAccent) {
@ -37,4 +34,13 @@ class WallpaperAccentManager(val context: Context) {
.removeOnColorsChangedListener(onColorsChangedListener) .removeOnColorsChangedListener(onColorsChangedListener)
} }
} }
private fun updateColors(colors: WallpaperColors?, which: Int) {
if (VersionUtils.hasOreoMR1()) {
if (which == WallpaperManager.FLAG_SYSTEM && colors != null) {
ThemeStore.editTheme(context).wallpaperColor(colors.primaryColor.toArgb())
.commit()
}
}
}
} }