Add an preference to disable the blur effect on Android 12+

This commit is contained in:
MM20
2022-07-11 18:11:25 +02:00
parent 18539c5f64
commit 24242c45cd
10 changed files with 70 additions and 7 deletions
@@ -2,6 +2,7 @@ package de.mm20.launcher2.ui.launcher
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import de.mm20.launcher2.preferences.LauncherDataStore
import kotlinx.coroutines.flow.first
@@ -47,4 +48,6 @@ class LauncherScaffoldVM : ViewModel(), KoinComponent {
isSearchOpen.value = false
isWidgetEditMode.value = editMode
}
val wallpaperBlur = dataStore.data.map { it.appearance.blurWallpaper }.asLiveData()
}
@@ -126,11 +126,14 @@ fun PagerScaffold(
}
}
val blurEnabled by viewModel.wallpaperBlur.observeAsState(false)
val blurWallpaper by remember {
derivedStateOf {
isSearchOpen || swipeableState.progress.to == Page.Widgets && swipeableState.progress.fraction <= 0.5f ||
swipeableState.progress.to == Page.Search && swipeableState.progress.fraction > 0.5f ||
!isWidgetsScrollZero
blurEnabled && (
isSearchOpen || swipeableState.progress.to == Page.Widgets && swipeableState.progress.fraction <= 0.5f ||
swipeableState.progress.to == Page.Search && swipeableState.progress.fraction > 0.5f ||
!isWidgetsScrollZero)
}
}
@@ -131,9 +131,11 @@ fun PullDownScaffold(
val maxSearchBarOffset = with(density) { 128.dp.toPx() }
val blurEnabled by viewModel.wallpaperBlur.observeAsState(false)
val blurWallpaper by remember {
derivedStateOf {
isSearchOpen || offsetY.value > toggleSearchThreshold || widgetsScrollState.value > 0
blurEnabled && (isSearchOpen || offsetY.value > toggleSearchThreshold || widgetsScrollState.value > 0)
}
}
@@ -2,7 +2,6 @@ package de.mm20.launcher2.ui.settings.appearance
import android.graphics.drawable.ColorDrawable
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
@@ -28,9 +27,9 @@ import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.HorizontalPagerIndicator
import com.google.accompanist.pager.rememberPagerState
import de.mm20.launcher2.icons.ColorLayer
import de.mm20.launcher2.icons.StaticIconLayer
import de.mm20.launcher2.icons.StaticLauncherIcon
import de.mm20.launcher2.ktx.isAtLeastApiLevel
import de.mm20.launcher2.preferences.Settings.*
import de.mm20.launcher2.preferences.Settings.AppearanceSettings.ColorScheme
import de.mm20.launcher2.preferences.Settings.AppearanceSettings.Theme
@@ -155,6 +154,22 @@ fun AppearanceSettingsScreen() {
viewModel.setDimWallpaper(it)
}
)
if (isAtLeastApiLevel(31)) {
val isBlurSupported = remember { viewModel.isBlurAvailable(context) }
val blurWallpaper by viewModel.blurWallpaper.observeAsState()
SwitchPreference(
title = stringResource(R.string.preference_blur_wallpaper),
summary = stringResource(
if (isBlurSupported) R.string.preference_blur_wallpaper_summary
else R.string.preference_blur_wallpaper_unsupported
),
value = blurWallpaper == true && isBlurSupported,
onValueChanged = {
viewModel.setBlurWallpaper(it)
},
enabled = isBlurSupported
)
}
}
PreferenceCategory(stringResource(R.string.preference_category_icons)) {
val iconShape by viewModel.iconShape.observeAsState(IconSettings.IconShape.PlatformDefault)
@@ -1,10 +1,14 @@
package de.mm20.launcher2.ui.settings.appearance
import android.content.Context
import android.content.Intent
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.getSystemService
import androidx.lifecycle.*
import de.mm20.launcher2.icons.IconPack
import de.mm20.launcher2.icons.IconRepository
import de.mm20.launcher2.ktx.isAtLeastApiLevel
import de.mm20.launcher2.preferences.LauncherDataStore
import de.mm20.launcher2.preferences.Settings
import de.mm20.launcher2.preferences.Settings.AppearanceSettings.*
@@ -85,6 +89,22 @@ class AppearanceSettingsScreenVM : ViewModel(), KoinComponent {
}
}
fun isBlurAvailable(context: Context): Boolean {
if (!isAtLeastApiLevel(31)) return false
return context.getSystemService<WindowManager>()?.isCrossWindowBlurEnabled == true
}
val blurWallpaper = dataStore.data.map { it.appearance.blurWallpaper }.asLiveData()
fun setBlurWallpaper(blurWallpaper: Boolean) {
viewModelScope.launch {
dataStore.updateData {
it.toBuilder()
.setAppearance(it.appearance.toBuilder().setBlurWallpaper(blurWallpaper))
.build()
}
}
}
fun openWallpaperChooser(context: AppCompatActivity) {
context.startActivity(Intent.createChooser(Intent(Intent.ACTION_SET_WALLPAPER), null))
}