Fix crash on Android 8.0
This commit is contained in:
parent
5163189789
commit
98d12d309f
@ -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,35 +40,36 @@ 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()) }
|
||||||
DisposableEffect(null) {
|
if (isAtLeastApiLevel(27)) {
|
||||||
val wallpaperManager = WallpaperManager.getInstance(context)
|
DisposableEffect(null) {
|
||||||
val callback = callback@{ colors: android.app.WallpaperColors?, which: Int ->
|
val wallpaperManager = WallpaperManager.getInstance(context)
|
||||||
if (which and WallpaperManager.FLAG_SYSTEM == 0) return@callback
|
val callback = callback@{ colors: android.app.WallpaperColors?, which: Int ->
|
||||||
if (colors != null) {
|
if (which and WallpaperManager.FLAG_SYSTEM == 0) return@callback
|
||||||
state.value = WallpaperColors.fromPlatformType(colors)
|
if (colors != null) {
|
||||||
} else {
|
state.value = WallpaperColors.fromPlatformType(colors)
|
||||||
state.value = WallpaperColors()
|
} else {
|
||||||
|
state.value = WallpaperColors()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
wallpaperManager.addOnColorsChangedListener(
|
||||||
wallpaperManager.addOnColorsChangedListener(
|
callback,
|
||||||
callback,
|
Handler(Looper.getMainLooper())
|
||||||
Handler(Looper.getMainLooper())
|
)
|
||||||
)
|
|
||||||
|
|
||||||
scope.launch {
|
scope.launch {
|
||||||
val colors = withContext(Dispatchers.IO) {
|
val colors = withContext(Dispatchers.IO) {
|
||||||
wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM)
|
wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM)
|
||||||
} ?: return@launch
|
} ?: return@launch
|
||||||
state.value = WallpaperColors.fromPlatformType(colors)
|
state.value = WallpaperColors.fromPlatformType(colors)
|
||||||
}
|
}
|
||||||
onDispose {
|
onDispose {
|
||||||
wallpaperManager.removeOnColorsChangedListener(callback)
|
wallpaperManager.removeOnColorsChangedListener(callback)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return state
|
return state
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user