Skip to content

Commit

Permalink
Scale wallpaper preview down
Browse files Browse the repository at this point in the history
  • Loading branch information
suphon-t committed May 18, 2022
1 parent 059286c commit e639a59
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app.lawnchair.ui.preferences.components

import android.annotation.SuppressLint
import android.app.WallpaperManager
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import androidx.compose.foundation.Image
import androidx.compose.runtime.Composable
Expand All @@ -10,6 +11,8 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.core.graphics.drawable.toBitmap
import app.lawnchair.util.scaleDownToDisplaySize
import com.google.accompanist.drawablepainter.rememberDrawablePainter
import com.google.accompanist.permissions.*

Expand All @@ -35,7 +38,12 @@ fun wallpaperDrawable(): Drawable? {

return when {
wallpaperInfo != null -> remember { wallpaperInfo.loadThumbnail(context.packageManager) }
permissionState.status.isGranted -> remember { wallpaperManager.drawable }
permissionState.status.isGranted -> remember {
wallpaperManager.drawable?.let {
val bitmap = it.toBitmap().scaleDownToDisplaySize(context)
BitmapDrawable(context.resources, bitmap)
}
}
permissionState.status.isGranted.not() -> {
SideEffect { permissionState.launchPermissionRequest() }
null
Expand Down
31 changes: 31 additions & 0 deletions lawnchair/src/app/lawnchair/util/LawnchairUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import android.content.Intent
import android.content.pm.PackageInfo.REQUESTED_PERMISSION_GRANTED
import android.content.pm.PackageManager
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Matrix
import android.net.Uri
import android.os.Looper
import android.provider.OpenableColumns
Expand Down Expand Up @@ -185,3 +187,32 @@ fun ContentResolver.getDisplayName(uri: Uri): String? {
}
return null
}

fun Bitmap.scaleDownToDisplaySize(context: Context, keepOriginal: Boolean = false): Bitmap {
val metrics = context.resources.displayMetrics
return scaleDownTo(kotlin.math.max(metrics.widthPixels, metrics.heightPixels), keepOriginal)
}

fun Bitmap.scaleDownTo(maxSize: Int, keepOriginal: Boolean = false): Bitmap {
val width = width
val height = height
val newWidth: Int
val newHeight: Int

when {
width > height && width > maxSize -> {
newWidth = maxSize
newHeight = (height * newWidth.toFloat() / width).toInt()
}
height > maxSize -> {
newHeight = maxSize
newWidth = (width * newHeight.toFloat() / height).toInt()
}
else -> return this
}
val newBitmap = Bitmap.createScaledBitmap(this, newWidth, newHeight, true)
if (!keepOriginal) {
recycle()
}
return newBitmap
}

0 comments on commit e639a59

Please sign in to comment.