Fixed wallpaper setting with the wrong dimensions

This commit is contained in:
Anthonyy232 committed 2024-08-11 00:03:08 -07:00
1 parent b03e860dee
commit e8f8a145ea
3 files changed
+26 -17

No files matched your search

@@ -3,6 +3,7 @@ import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Color
import android.graphics.ColorMatrix import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter import android.graphics.ColorMatrixColorFilter
import android.graphics.Paint import android.graphics.Paint
@@ -270,3 +271,20 @@ fun isValidUri(context: Context, uriString: String?): Boolean {
} catch (e: Exception) { false } } catch (e: Exception) { false }
} }
/**
* Calculate the brightness of a bitmap (0-100)
* https://gist.github.com/httnn/b1d772caf76cdc0c11e2
*/
fun calculateBrightness(bitmap: Bitmap, pixelSpacing: Int = 1): Int {
var brightness = 0.0
val pixels = IntArray(bitmap.width * bitmap.height)
bitmap.getPixels(pixels, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height)
for (i in pixels.indices step pixelSpacing.coerceAtLeast(1)) {
val color = pixels[i]
val R = Color.red(color)
val G = Color.green(color)
val B = Color.blue(color)
brightness += 0.299*R + 0.587*G + 0.114*B
}
return (brightness / (pixels.size / pixelSpacing)).toInt()
}
@@ -50,7 +50,6 @@ import java.time.LocalDateTime
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle import java.time.format.FormatStyle
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.min
/** /**
* Service for changing home screen * Service for changing home screen
@@ -241,7 +240,7 @@ class HomeWallpaperService: Service() {
delay(5000) delay(5000)
selectedAlbum = selectedRepository.getSelectedAlbum().first() selectedAlbum = selectedRepository.getSelectedAlbum().first()
homeAlbum = selectedAlbum.find { it.album.initialAlbumName == homeAlbumName } homeAlbum = selectedAlbum.find { it.album.initialAlbumName == homeAlbumName }
delay(5000) delay(1000)
} }
if (homeAlbum != null) { if (homeAlbum != null) {
if (success) { if (success) {
@@ -280,7 +279,7 @@ class HomeWallpaperService: Service() {
delay(5000) delay(5000)
selectedAlbum = selectedRepository.getSelectedAlbum().first() selectedAlbum = selectedRepository.getSelectedAlbum().first()
homeAlbum = selectedAlbum.find { it.album.initialAlbumName == homeAlbumName } homeAlbum = selectedAlbum.find { it.album.initialAlbumName == homeAlbumName }
delay(5000) delay(1000)
} }
if (homeAlbum != null) { if (homeAlbum != null) {
if (success) { if (success) {
@@ -552,21 +551,18 @@ class HomeWallpaperService: Service() {
val wallpaperManager = WallpaperManager.getInstance(context) val wallpaperManager = WallpaperManager.getInstance(context)
try { try {
val imageSize = wallpaper.getImageDimensions(context) ?: return false val imageSize = wallpaper.getImageDimensions(context) ?: return false
val aspectRatio = imageSize.height.toFloat() / imageSize.width.toFloat()
val device = context.resources.displayMetrics val device = context.resources.displayMetrics
val targetWidth = min(2 * device.widthPixels, imageSize.width)
val targetHeight = (targetWidth / aspectRatio).toInt()
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
try { try {
val source = ImageDecoder.createSource(context.contentResolver, wallpaper) val source = ImageDecoder.createSource(context.contentResolver, wallpaper)
ImageDecoder.decodeBitmap(source) { decoder, _, _ -> ImageDecoder.decodeBitmap(source) { decoder, _, _ ->
decoder.setTargetSize(targetWidth, targetHeight) decoder.setTargetSampleSize(calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels))
decoder.isMutableRequired = true decoder.isMutableRequired = true
} }
} catch (e: Exception) { } catch (e: Exception) {
context.contentResolver.openInputStream(wallpaper)?.use { inputStream -> context.contentResolver.openInputStream(wallpaper)?.use { inputStream ->
val options = BitmapFactory.Options().apply { val options = BitmapFactory.Options().apply {
inSampleSize = calculateInSampleSize(imageSize, targetWidth, targetHeight) inSampleSize = calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels)
inMutable = true inMutable = true
} }
BitmapFactory.decodeStream(inputStream, null, options) BitmapFactory.decodeStream(inputStream, null, options)
@@ -576,7 +572,7 @@ class HomeWallpaperService: Service() {
else { else {
context.contentResolver.openInputStream(wallpaper)?.use { inputStream -> context.contentResolver.openInputStream(wallpaper)?.use { inputStream ->
val options = BitmapFactory.Options().apply { val options = BitmapFactory.Options().apply {
inSampleSize = calculateInSampleSize(imageSize, targetWidth, targetHeight) inSampleSize = calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels)
inMutable = true inMutable = true
} }
BitmapFactory.decodeStream(inputStream, null, options) BitmapFactory.decodeStream(inputStream, null, options)
@@ -49,7 +49,6 @@ import java.time.LocalDateTime
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle import java.time.format.FormatStyle
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.min
/** /**
* Service to change lock screen * Service to change lock screen
@@ -483,22 +482,18 @@ class LockWallpaperService: Service() {
val wallpaperManager = WallpaperManager.getInstance(context) val wallpaperManager = WallpaperManager.getInstance(context)
try { try {
val imageSize = wallpaper.getImageDimensions(context) ?: return false val imageSize = wallpaper.getImageDimensions(context) ?: return false
val aspectRatio = imageSize.height.toFloat() / imageSize.width.toFloat()
val device = context.resources.displayMetrics val device = context.resources.displayMetrics
val targetWidth = min(2 * device.widthPixels, imageSize.width)
val targetHeight = (targetWidth / aspectRatio).toInt()
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
try { try {
val source = ImageDecoder.createSource(context.contentResolver, wallpaper) val source = ImageDecoder.createSource(context.contentResolver, wallpaper)
ImageDecoder.decodeBitmap(source) { decoder, _, _ -> ImageDecoder.decodeBitmap(source) { decoder, _, _ ->
decoder.setTargetSize(targetWidth, targetHeight) decoder.setTargetSampleSize(calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels))
decoder.isMutableRequired = true decoder.isMutableRequired = true
} }
} catch (e: Exception) { } catch (e: Exception) {
context.contentResolver.openInputStream(wallpaper)?.use { inputStream -> context.contentResolver.openInputStream(wallpaper)?.use { inputStream ->
val options = BitmapFactory.Options().apply { val options = BitmapFactory.Options().apply {
inSampleSize = calculateInSampleSize(imageSize, targetWidth, targetHeight) inSampleSize = calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels)
inMutable = true inMutable = true
} }
BitmapFactory.decodeStream(inputStream, null, options) BitmapFactory.decodeStream(inputStream, null, options)
@@ -508,7 +503,7 @@ class LockWallpaperService: Service() {
else { else {
context.contentResolver.openInputStream(wallpaper)?.use { inputStream -> context.contentResolver.openInputStream(wallpaper)?.use { inputStream ->
val options = BitmapFactory.Options().apply { val options = BitmapFactory.Options().apply {
inSampleSize = calculateInSampleSize(imageSize, targetWidth, targetHeight) inSampleSize = calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels)
inMutable = true inMutable = true
} }
BitmapFactory.decodeStream(inputStream, null, options) BitmapFactory.decodeStream(inputStream, null, options)