Fixed wallpaper setting with the wrong dimensions
This commit is contained in:
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.BitmapFactory
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import android.graphics.ColorMatrix
|
||||
import android.graphics.ColorMatrixColorFilter
|
||||
import android.graphics.Paint
|
||||
@@ -270,3 +271,20 @@ fun isValidUri(context: Context, uriString: String?): Boolean {
|
||||
} 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()
|
||||
}
|
||||
+5
-9
@@ -50,7 +50,6 @@ import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.format.FormatStyle
|
||||
import javax.inject.Inject
|
||||
import kotlin.math.min
|
||||
|
||||
/**
|
||||
* Service for changing home screen
|
||||
@@ -241,7 +240,7 @@ class HomeWallpaperService: Service() {
|
||||
delay(5000)
|
||||
selectedAlbum = selectedRepository.getSelectedAlbum().first()
|
||||
homeAlbum = selectedAlbum.find { it.album.initialAlbumName == homeAlbumName }
|
||||
delay(5000)
|
||||
delay(1000)
|
||||
}
|
||||
if (homeAlbum != null) {
|
||||
if (success) {
|
||||
@@ -280,7 +279,7 @@ class HomeWallpaperService: Service() {
|
||||
delay(5000)
|
||||
selectedAlbum = selectedRepository.getSelectedAlbum().first()
|
||||
homeAlbum = selectedAlbum.find { it.album.initialAlbumName == homeAlbumName }
|
||||
delay(5000)
|
||||
delay(1000)
|
||||
}
|
||||
if (homeAlbum != null) {
|
||||
if (success) {
|
||||
@@ -552,21 +551,18 @@ class HomeWallpaperService: Service() {
|
||||
val wallpaperManager = WallpaperManager.getInstance(context)
|
||||
try {
|
||||
val imageSize = wallpaper.getImageDimensions(context) ?: return false
|
||||
val aspectRatio = imageSize.height.toFloat() / imageSize.width.toFloat()
|
||||
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) {
|
||||
try {
|
||||
val source = ImageDecoder.createSource(context.contentResolver, wallpaper)
|
||||
ImageDecoder.decodeBitmap(source) { decoder, _, _ ->
|
||||
decoder.setTargetSize(targetWidth, targetHeight)
|
||||
decoder.setTargetSampleSize(calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels))
|
||||
decoder.isMutableRequired = true
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
context.contentResolver.openInputStream(wallpaper)?.use { inputStream ->
|
||||
val options = BitmapFactory.Options().apply {
|
||||
inSampleSize = calculateInSampleSize(imageSize, targetWidth, targetHeight)
|
||||
inSampleSize = calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels)
|
||||
inMutable = true
|
||||
}
|
||||
BitmapFactory.decodeStream(inputStream, null, options)
|
||||
@@ -576,7 +572,7 @@ class HomeWallpaperService: Service() {
|
||||
else {
|
||||
context.contentResolver.openInputStream(wallpaper)?.use { inputStream ->
|
||||
val options = BitmapFactory.Options().apply {
|
||||
inSampleSize = calculateInSampleSize(imageSize, targetWidth, targetHeight)
|
||||
inSampleSize = calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels)
|
||||
inMutable = true
|
||||
}
|
||||
BitmapFactory.decodeStream(inputStream, null, options)
|
||||
|
||||
+3
-8
@@ -49,7 +49,6 @@ import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.format.FormatStyle
|
||||
import javax.inject.Inject
|
||||
import kotlin.math.min
|
||||
|
||||
/**
|
||||
* Service to change lock screen
|
||||
@@ -483,22 +482,18 @@ class LockWallpaperService: Service() {
|
||||
val wallpaperManager = WallpaperManager.getInstance(context)
|
||||
try {
|
||||
val imageSize = wallpaper.getImageDimensions(context) ?: return false
|
||||
val aspectRatio = imageSize.height.toFloat() / imageSize.width.toFloat()
|
||||
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) {
|
||||
try {
|
||||
val source = ImageDecoder.createSource(context.contentResolver, wallpaper)
|
||||
ImageDecoder.decodeBitmap(source) { decoder, _, _ ->
|
||||
decoder.setTargetSize(targetWidth, targetHeight)
|
||||
decoder.setTargetSampleSize(calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels))
|
||||
decoder.isMutableRequired = true
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
context.contentResolver.openInputStream(wallpaper)?.use { inputStream ->
|
||||
val options = BitmapFactory.Options().apply {
|
||||
inSampleSize = calculateInSampleSize(imageSize, targetWidth, targetHeight)
|
||||
inSampleSize = calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels)
|
||||
inMutable = true
|
||||
}
|
||||
BitmapFactory.decodeStream(inputStream, null, options)
|
||||
@@ -508,7 +503,7 @@ class LockWallpaperService: Service() {
|
||||
else {
|
||||
context.contentResolver.openInputStream(wallpaper)?.use { inputStream ->
|
||||
val options = BitmapFactory.Options().apply {
|
||||
inSampleSize = calculateInSampleSize(imageSize, targetWidth, targetHeight)
|
||||
inSampleSize = calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels)
|
||||
inMutable = true
|
||||
}
|
||||
BitmapFactory.decodeStream(inputStream, null, options)
|
||||
|
||||
Reference in new issue
Block a user