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
11 changed files with 5 additions and 332 deletions
@@ -59,7 +59,6 @@ import de.mm20.launcher2.transition.OneShotLayoutTransition
import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.legacy.component.EditFavoritesView
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.widget.LauncherWidget
import de.mm20.launcher2.weather.WeatherViewModel
@@ -208,7 +207,9 @@ class LauncherActivity : AppCompatActivity() {
overlayView = rootView.overlay
if (LauncherPreferences.instance.dimWallpaper) {
dimWallpaper.setBackgroundColor(getColor(R.color.wallpaper_dim))
}
scrollContainer.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
searchContainer.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
@@ -221,12 +222,6 @@ class LauncherActivity : AppCompatActivity() {
adjustWidgetSpace()
}
initWidgets()
if (preferences.blurCards && preferences.cardOpacity < 0xFF) {
container.viewTreeObserver.addOnPreDrawListener {
blurView.invalidate()
true
}
}
scrollView.setOnTouchListener(scrollViewOnTouchListener)
scrollView.setOnScrollChangeListener { _: NestedScrollView?, _: Int, scrollY: Int, _: Int, oldScrollY: Int ->
when {
@@ -608,14 +603,6 @@ class LauncherActivity : AppCompatActivity() {
override fun 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)
widgetHost.startListening()
}
@@ -627,8 +614,6 @@ class LauncherActivity : AppCompatActivity() {
override fun onStop() {
super.onStop()
WallpaperBlur.blurredWallpaper?.takeIf { !it.isRecycled }?.recycle()
WallpaperBlur.blurredWallpaper = null
try {
widgetHost.stopListening()
} catch (e: NullPointerException) {
@@ -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
}
@@ -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)
}
}
}
}
}
+2 -2
View File
@@ -7,8 +7,8 @@
android:layout_height="match_parent"
android:clipChildren="false">
<de.mm20.launcher2.ui.legacy.view.BlurView
android:id="@+id/blurView"
<FrameLayout
android:id="@+id/dimWallpaper"
android:layout_width="match_parent"
android:layout_height="match_parent" />