Fix crash on Android 8.0

This commit is contained in:
MM20 2022-10-25 21:25:52 +02:00
parent 5163189789
commit 98d12d309f
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389

View File

@ -13,6 +13,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import de.mm20.launcher2.ktx.isAtLeastApiLevel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -39,12 +40,12 @@ data class WallpaperColors(
} }
} }
@RequiresApi(Build.VERSION_CODES.O_MR1)
@Composable @Composable
fun wallpaperColorsAsState(): State<WallpaperColors> { fun wallpaperColorsAsState(): State<WallpaperColors> {
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val context = LocalContext.current val context = LocalContext.current
val state = remember { mutableStateOf(WallpaperColors()) } val state = remember { mutableStateOf(WallpaperColors()) }
if (isAtLeastApiLevel(27)) {
DisposableEffect(null) { DisposableEffect(null) {
val wallpaperManager = WallpaperManager.getInstance(context) val wallpaperManager = WallpaperManager.getInstance(context)
val callback = callback@{ colors: android.app.WallpaperColors?, which: Int -> val callback = callback@{ colors: android.app.WallpaperColors?, which: Int ->
@ -70,5 +71,6 @@ fun wallpaperColorsAsState(): State<WallpaperColors> {
wallpaperManager.removeOnColorsChangedListener(callback) wallpaperManager.removeOnColorsChangedListener(callback)
} }
} }
}
return state return state
} }