Remove wallpaper blur effect

Never actually really worked
This commit is contained in:
MM20 2021-10-10 13:01:25 +02:00
parent 087d4fd455
commit dbc1444d7f
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
11 changed files with 5 additions and 332 deletions

View File

@ -1,9 +1,6 @@
package de.mm20.launcher2 package de.mm20.launcher2
import android.app.Application import android.app.Application
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.graphics.Bitmap import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
@ -21,7 +18,6 @@ import de.mm20.launcher2.music.musicModule
import de.mm20.launcher2.preferences.LauncherPreferences import de.mm20.launcher2.preferences.LauncherPreferences
import de.mm20.launcher2.preferences.Themes import de.mm20.launcher2.preferences.Themes
import de.mm20.launcher2.search.searchModule import de.mm20.launcher2.search.searchModule
import de.mm20.launcher2.ui.legacy.helper.WallpaperBlur
import de.mm20.launcher2.unitconverter.unitConverterModule import de.mm20.launcher2.unitconverter.unitConverterModule
import de.mm20.launcher2.websites.websitesModule import de.mm20.launcher2.websites.websitesModule
import de.mm20.launcher2.widgets.widgetsModule import de.mm20.launcher2.widgets.widgetsModule
@ -38,12 +34,9 @@ class LauncherApplication : Application(), CoroutineScope {
override val coroutineContext: CoroutineContext override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + SupervisorJob() get() = Dispatchers.Main + SupervisorJob()
var blurredWallpaper: Bitmap? = null
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
Debug() Debug()
instance = this
LauncherPreferences.initialize(this) LauncherPreferences.initialize(this)
val theme = LauncherPreferences.instance.theme val theme = LauncherPreferences.instance.theme
@ -55,9 +48,6 @@ class LauncherApplication : Application(), CoroutineScope {
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM //system else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM //system
} }
) )
WallpaperBlur.requestBlur(this)
@Suppress("DEPRECATION") // We need to access the wallpaper directly to blur it
registerReceiver(WallpaperReceiver(), IntentFilter(Intent.ACTION_WALLPAPER_CHANGED))
startKoin { startKoin {
androidLogger() androidLogger()
@ -85,7 +75,6 @@ class LauncherApplication : Application(), CoroutineScope {
} }
companion object { companion object {
lateinit var instance: LauncherApplication
val collator: Collator by lazy { val collator: Collator by lazy {
Collator.getInstance().apply { strength = Collator.SECONDARY } Collator.getInstance().apply { strength = Collator.SECONDARY }
@ -99,10 +88,3 @@ object PermissionRequests {
const val LOCATION = 410 const val LOCATION = 410
const val ALL = 666 const val ALL = 666
} }
class WallpaperReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent?) {
WallpaperBlur.requestBlur(context)
}
}

View File

@ -47,19 +47,6 @@ class PreferencesAppearanceFragment : PreferenceFragmentCompat() {
requireActivity().recreate() requireActivity().recreate()
true true
} }
if (WallpaperManager.getInstance(activity).wallpaperInfo != null) {
findPreference<Preference>("blur_cards")?.apply {
setSummary(R.string.preference_blur_cards_summary_lwp)
isEnabled = false
}
}
findPreference<Preference>("blur_cards")?.setOnPreferenceChangeListener { _, newValue ->
val newVal = newValue as? Boolean ?: return@setOnPreferenceChangeListener true
if (newVal && requireActivity().checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 0)
}
true
}
findPreference<Preference>("wallpaper")?.setOnPreferenceClickListener { findPreference<Preference>("wallpaper")?.setOnPreferenceClickListener {
requireContext().startActivity(Intent.createChooser(Intent(Intent.ACTION_SET_WALLPAPER), null)) requireContext().startActivity(Intent.createChooser(Intent(Intent.ACTION_SET_WALLPAPER), null))

View File

@ -13,16 +13,12 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import de.mm20.launcher2.LauncherApplication
import de.mm20.launcher2.R import de.mm20.launcher2.R
import de.mm20.launcher2.ktx.castTo
import de.mm20.launcher2.ktx.dp import de.mm20.launcher2.ktx.dp
import de.mm20.launcher2.ktx.translate import de.mm20.launcher2.ktx.translate
import de.mm20.launcher2.preferences.CardBackground import de.mm20.launcher2.preferences.CardBackground
import de.mm20.launcher2.preferences.LauncherPreferences import de.mm20.launcher2.preferences.LauncherPreferences
import de.mm20.launcher2.ui.legacy.helper.WallpaperBlur
import kotlinx.android.synthetic.main.fragment_card_settings.* import kotlinx.android.synthetic.main.fragment_card_settings.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -36,19 +32,10 @@ class PreferencesCardFragment : Fragment(R.layout.fragment_card_settings) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
previewCard.strokeOpacity = 0xFF previewCard.strokeOpacity = 0xFF
previewCardBlur.clipToOutline = true
previewCardBlur.outlineProvider = object : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline?) {
val radius = preferences.cardRadius
outline?.setRoundRect(0, 0, view.width, view.height, radius * dp)
}
}
val context = requireContext() val context = requireContext()
val previewCardBlur = previewCardBlur
val previewCard = previewCard val previewCard = previewCard
val prefFragment = PreferenesCardInnerFragment() val prefFragment = PreferenesCardInnerFragment()
@ -58,7 +45,6 @@ class PreferencesCardFragment : Fragment(R.layout.fragment_card_settings) {
it.setOnPreferenceChangeListener { pref, newValue -> it.setOnPreferenceChangeListener { pref, newValue ->
val value = newValue as Int val value = newValue as Int
previewCard.radius = value * dp previewCard.radius = value * dp
previewCardBlur.invalidateOutline()
pref.summary = value.toString() pref.summary = value.toString()
true true
} }
@ -82,27 +68,6 @@ class PreferencesCardFragment : Fragment(R.layout.fragment_card_settings) {
true true
} }
} }
findPreference<Preference>("blur_cards")?.let {
if (WallpaperManager.getInstance(requireContext()).wallpaperInfo != null) {
it.isEnabled = false
it.setSummary(R.string.preference_blur_cards_summary_lwp)
previewCardBlur.visibility = View.INVISIBLE
} else {
previewCardBlur.visibility = if (preferences.blurCards) {
View.VISIBLE
} else {
View.INVISIBLE
}
it.setOnPreferenceChangeListener { pref, newValue ->
previewCardBlur.visibility = if (newValue as Boolean) {
View.VISIBLE
} else {
View.INVISIBLE
}
true
}
}
}
findPreference<Preference>("card_background")?.let { findPreference<Preference>("card_background")?.let {
it.setOnPreferenceChangeListener { preference, newValue -> it.setOnPreferenceChangeListener { preference, newValue ->
val background = CardBackground.byValue(newValue as String) val background = CardBackground.byValue(newValue as String)
@ -137,49 +102,10 @@ class PreferencesCardFragment : Fragment(R.layout.fragment_card_settings) {
start() start()
} }
if (preferences.blurCards && preferences.cardOpacity < 0xFF) {
lifecycleScope.launch {
val wallpaper = withContext(Dispatchers.IO) {
WallpaperBlur.getCachedBitmap(requireContext())
}
LauncherApplication.instance.blurredWallpaper = wallpaper
}
}
content.doOnNextLayout { content.doOnNextLayout {
WallpaperManager.getInstance(requireContext()).setWallpaperOffsets(it.windowToken, 0.5f, 0.5f) WallpaperManager.getInstance(requireContext()).setWallpaperOffsets(it.windowToken, 0.5f, 0.5f)
} }
val activity = requireActivity()
lifecycleScope.launch {
val viewPosition = intArrayOf(0, 0)
val rect = Rect(0, 0, previewCardBlur.width, previewCardBlur.height)
val screen = Point()
activity.windowManager.defaultDisplay.getRealSize(screen)
previewCardBlur.getLocationOnScreen(viewPosition)
val file = File(requireContext().cacheDir, "wallpaper")
if (!file.exists()) return@launch
blurBitmap = withContext(Dispatchers.IO) {
val wallpaperWidth: Int
val wallpaperHeight: Int
val decoder = BitmapRegionDecoder
.newInstance(file.absolutePath, false)
wallpaperHeight = decoder.height
wallpaperWidth = decoder.width
if (wallpaperWidth >= screen.x && wallpaperHeight >= screen.y) {
val translateX = (wallpaperWidth - previewCardBlur.width) / 2f
val translateY = (wallpaperHeight - screen.y) / 2f + viewPosition[1]
rect.translate(translateX.roundToInt(),
translateY.roundToInt())
}
decoder.decodeRegion(rect, null)
}
previewCardBlur.setImageBitmap(blurBitmap)
}
} }
override fun onStop() { override fun onStop() {

View File

@ -13,14 +13,6 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/previewCardBlur"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="16dp"
android:scaleType="centerCrop" />
<de.mm20.launcher2.ui.legacy.view.LauncherCardView <de.mm20.launcher2.ui.legacy.view.LauncherCardView
android:id="@+id/previewCard" android:id="@+id/previewCard"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -24,12 +24,6 @@
app:min="0" app:min="0"
app:updatesContinuously="true" /> app:updatesContinuously="true" />
<SwitchPreference
app:key="blur_cards"
android:defaultValue="false"
app:summary="@string/preference_blur_cards_summary"
app:title="@string/preference_blur_cards" />
<ListPreference <ListPreference
app:key="card_background" app:key="card_background"
android:defaultValue="0" android:defaultValue="0"

View File

@ -137,9 +137,6 @@
<string name="preference_search_websearch">Websuchen-Shortcuts</string> <string name="preference_search_websearch">Websuchen-Shortcuts</string>
<string name="preference_search_edit_websearch">Websuchen bearbeiten</string> <string name="preference_search_edit_websearch">Websuchen bearbeiten</string>
<string name="file_meta_location">Ort</string> <string name="file_meta_location">Ort</string>
<string name="preference_blur_cards">Unschärfeeffekt</string>
<string name="preference_blur_cards_summary">Hintergrund hinter Karten unscharf darstellen</string>
<string name="preference_blur_cards_summary_lwp">Nicht mit Live-Hintergründen kompatibel</string>
<string name="websearch_google">Google</string> <string name="websearch_google">Google</string>
<string name="websearch_youtube">YouTube</string> <string name="websearch_youtube">YouTube</string>
<string name="websearch_playstore">Google Play</string> <string name="websearch_playstore">Google Play</string>

View File

@ -183,9 +183,6 @@
<string name="preference_search_websearch">Web search shortcuts</string> <string name="preference_search_websearch">Web search shortcuts</string>
<string name="preference_search_edit_websearch">Edit web searches</string> <string name="preference_search_edit_websearch">Edit web searches</string>
<string name="file_meta_location">Location</string> <string name="file_meta_location">Location</string>
<string name="preference_blur_cards">Blur effect</string>
<string name="preference_blur_cards_summary">Blur wallpaper behind translucent cards</string>
<string name="preference_blur_cards_summary_lwp">Not compatible with live wallpapers</string>
<string name="websearch_google">Google</string> <string name="websearch_google">Google</string>
<string name="websearch_youtube">YouTube</string> <string name="websearch_youtube">YouTube</string>
<string name="websearch_playstore">Google Play</string> <string name="websearch_playstore">Google Play</string>

View File

@ -59,7 +59,6 @@ import de.mm20.launcher2.transition.OneShotLayoutTransition
import de.mm20.launcher2.ui.R import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.legacy.component.EditFavoritesView import de.mm20.launcher2.ui.legacy.component.EditFavoritesView
import de.mm20.launcher2.ui.legacy.component.WidgetView import de.mm20.launcher2.ui.legacy.component.WidgetView
import de.mm20.launcher2.ui.legacy.helper.WallpaperBlur
import de.mm20.launcher2.ui.legacy.search.SearchGridView import de.mm20.launcher2.ui.legacy.search.SearchGridView
import de.mm20.launcher2.ui.legacy.widget.LauncherWidget import de.mm20.launcher2.ui.legacy.widget.LauncherWidget
import de.mm20.launcher2.weather.WeatherViewModel import de.mm20.launcher2.weather.WeatherViewModel
@ -208,7 +207,9 @@ class LauncherActivity : AppCompatActivity() {
overlayView = rootView.overlay overlayView = rootView.overlay
if (LauncherPreferences.instance.dimWallpaper) {
dimWallpaper.setBackgroundColor(getColor(R.color.wallpaper_dim))
}
scrollContainer.layoutTransition.enableTransitionType(LayoutTransition.CHANGING) scrollContainer.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
searchContainer.layoutTransition.enableTransitionType(LayoutTransition.CHANGING) searchContainer.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
@ -221,12 +222,6 @@ class LauncherActivity : AppCompatActivity() {
adjustWidgetSpace() adjustWidgetSpace()
} }
initWidgets() initWidgets()
if (preferences.blurCards && preferences.cardOpacity < 0xFF) {
container.viewTreeObserver.addOnPreDrawListener {
blurView.invalidate()
true
}
}
scrollView.setOnTouchListener(scrollViewOnTouchListener) scrollView.setOnTouchListener(scrollViewOnTouchListener)
scrollView.setOnScrollChangeListener { _: NestedScrollView?, _: Int, scrollY: Int, _: Int, oldScrollY: Int -> scrollView.setOnScrollChangeListener { _: NestedScrollView?, _: Int, scrollY: Int, _: Int, oldScrollY: Int ->
when { when {
@ -608,14 +603,6 @@ class LauncherActivity : AppCompatActivity() {
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
if (preferences.blurCards && preferences.cardOpacity < 0xFF) {
lifecycleScope.launch {
val wallpaper = withContext(Dispatchers.IO) {
WallpaperBlur.getCachedBitmap(this@LauncherActivity)
}
WallpaperBlur.blurredWallpaper = wallpaper
}
}
preferences.doOnPreferenceChange("is_light_wallpaper", action = themeListener) preferences.doOnPreferenceChange("is_light_wallpaper", action = themeListener)
widgetHost.startListening() widgetHost.startListening()
} }
@ -627,8 +614,6 @@ class LauncherActivity : AppCompatActivity() {
override fun onStop() { override fun onStop() {
super.onStop() super.onStop()
WallpaperBlur.blurredWallpaper?.takeIf { !it.isRecycled }?.recycle()
WallpaperBlur.blurredWallpaper = null
try { try {
widgetHost.stopListening() widgetHost.stopListening()
} catch (e: NullPointerException) { } catch (e: NullPointerException) {

View File

@ -1,91 +0,0 @@
package de.mm20.launcher2.ui.legacy.helper
import android.Manifest
import android.app.WallpaperManager
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Build
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import de.mm20.launcher2.ktx.dp
import de.mm20.launcher2.preferences.LauncherPreferences
import jp.wasabeef.glide.transformations.BlurTransformation
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileOutputStream
object WallpaperBlur {
fun requestBlur(context: Context) {
val wm = WallpaperManager.getInstance(context)
val lastId = context.getSharedPreferences("wallpaper", Context.MODE_PRIVATE)
.getInt("last_wallpaper_id", 0)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N || wm.getWallpaperId(WallpaperManager.FLAG_SYSTEM) != lastId) {
blurredWallpaper?.takeIf { !it.isRecycled }?.recycle()
blurredWallpaper = null
File(context.cacheDir, "wallpaper").takeIf { it.exists() }?.delete()
if (wm.wallpaperInfo != null) return
if (ContextCompat.checkSelfPermission(
context,
Manifest.permission.READ_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED
) return
val wallpaper = wm.drawable.toBitmap()
Glide.with(context)
.asBitmap()
.load(wallpaper)
.apply(
RequestOptions.bitmapTransform(
BlurTransformation(
(20 * context.dp).toInt(),
1
)
)
)
.into(object : SimpleTarget<Bitmap>() {
override fun onResourceReady(
resource: Bitmap,
transition: Transition<in Bitmap>?
) {
GlobalScope.launch {
withContext(Dispatchers.IO) {
val out = FileOutputStream(File(context.cacheDir, "wallpaper"))
resource.compress(Bitmap.CompressFormat.PNG, 100, out)
out.close()
context.getSharedPreferences("wallpaper", Context.MODE_PRIVATE)
.edit()
.putInt(
"last_wallpaper_id", wm.getWallpaperId(
WallpaperManager.FLAG_SYSTEM
)
)
.apply()
if (LauncherPreferences.instance.blurCards && LauncherPreferences.instance.cardOpacity < 0xFF) {
blurredWallpaper = resource
} else {
resource.recycle()
}
}
}
}
})
}
}
fun getCachedBitmap(context: Context): Bitmap? {
return BitmapFactory.decodeFile(File(context.cacheDir, "wallpaper").absolutePath)
}
var blurredWallpaper: Bitmap? = null
}

View File

@ -1,96 +0,0 @@
package de.mm20.launcher2.ui.legacy.view
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.graphics.toRect
import androidx.core.view.isVisible
import androidx.core.view.iterator
import de.mm20.launcher2.ktx.copyTo
import de.mm20.launcher2.ktx.scale
import de.mm20.launcher2.ktx.toRectF
import de.mm20.launcher2.ktx.translate
import de.mm20.launcher2.preferences.LauncherPreferences
import de.mm20.launcher2.ui.R
import kotlin.math.min
class BlurView : View {
private val globalRect = Rect()
private val blurPaint = Paint().apply { xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN) }
private val maskPaint = Paint().apply { color = Color.BLACK }
private val viewRect = RectF()
private val wallpaperRect = RectF()
private val windowRect = Rect()
private val dimWallpaper = LauncherPreferences.instance.dimWallpaper
private val dimPaint = Paint().apply { color = ContextCompat.getColor(context, R.color.wallpaper_dim) }
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleRes: Int) : super(context, attrs, defStyleRes)
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
/*val blurredWallpaper = LauncherApplication.instance.blurredWallpaper
blurredWallpaper ?: return drawWallpaperDim(canvas)
if (blurredWallpaper.isRecycled) return drawWallpaperDim(canvas)
val parent = parent as? ViewGroup ?: return drawWallpaperDim(canvas)
drawMasks(parent, canvas)
getGlobalVisibleRect(globalRect)
globalRect.toRectF(viewRect)
getWindowVisibleDisplayFrame(windowRect)
/*canvas.drawBitmap(blurredWallpaper,
-(blurredWallpaper.width - viewRect.width()) / 2f,
-(blurredWallpaper.height - viewRect.height()) / 2f,
blurPaint)*/
if (blurredWallpaper.width >= width && blurredWallpaper.height >= height) {
viewRect.copyTo(wallpaperRect)
wallpaperRect.translate((blurredWallpaper.width - wallpaperRect.width()) / 2f, (blurredWallpaper.height - wallpaperRect.height()) / 2f)
} else {
val scale = min(blurredWallpaper.width / width.toFloat(), blurredWallpaper.height / height.toFloat())
viewRect.copyTo(wallpaperRect)
wallpaperRect.scale(scale)
wallpaperRect.translate((blurredWallpaper.width - wallpaperRect.width()) / 2, (blurredWallpaper.height - wallpaperRect.height()) / 2)
}
if (viewRect.top > 0f) {
wallpaperRect.translate(0f, viewRect.top)
}
canvas.drawBitmap(blurredWallpaper, wallpaperRect.toRect(), viewRect, blurPaint)
drawWallpaperDim(canvas)*/
}
private fun drawWallpaperDim(canvas: Canvas) {
if (dimWallpaper) {
canvas.drawRect(Rect(0, 0, canvas.width, canvas.height), dimPaint)
}
}
private var viewBounds = RectF()
private fun drawMasks(parent: ViewGroup, canvas: Canvas) {
loop@ for (view in parent.iterator()) {
when {
!view.isVisible || view.alpha == 0f -> {
}
view is LauncherCardView -> {
if (view.backgroundOpacity == 0 || view.backgroundOpacity == 0xFF) {
continue@loop
}
if (!view.getGlobalVisibleRect(globalRect)) continue@loop
globalRect.toRectF(viewBounds)
if (viewRect.top > 0f) {
viewBounds.translate(0f, -viewRect.top)
}
canvas.drawRoundRect(viewBounds, view.radius, view.radius, maskPaint)
}
view is ViewGroup -> {
drawMasks(view, canvas)
}
}
}
}
}

View File

@ -7,8 +7,8 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:clipChildren="false"> android:clipChildren="false">
<de.mm20.launcher2.ui.legacy.view.BlurView <FrameLayout
android:id="@+id/blurView" android:id="@+id/dimWallpaper"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />