Fixed album of size 1 not working properly

This commit is contained in:
Anthonyy232 committed 2024-08-14 01:29:51 -07:00
1 parent 7615d7786a
commit 7c3c64149a
7 files changed
+89 -89

No files matched your search

@@ -6,8 +6,11 @@ import android.graphics.Canvas
import android.graphics.Color
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.ImageDecoder
import android.graphics.Paint
import android.net.Uri
import android.os.Build
import android.util.DisplayMetrics
import android.util.Log
import android.util.Size
import androidx.compose.ui.util.fastRoundToInt
@@ -62,6 +65,50 @@ fun calculateInSampleSize(imageSize: Size, width: Int, height: Int): Int {
else { return 1 }
}
/**
* Retrieve a bitmap from a URI that is scaled down to the device's screen size
*/
fun retrieveBitmap(
context: Context,
wallpaper: Uri,
device: DisplayMetrics
): Bitmap? {
val imageSize = wallpaper.getImageDimensions(context) ?: return null
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
try {
val source = ImageDecoder.createSource(context.contentResolver, wallpaper)
ImageDecoder.decodeBitmap(source) { decoder, _, _ ->
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, device.widthPixels, device.heightPixels)
inMutable = true
}
BitmapFactory.decodeStream(inputStream, null, options)
}
}
} else {
context.contentResolver.openInputStream(wallpaper)?.use { inputStream ->
val options = BitmapFactory.Options().apply {
inSampleSize =
calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels)
inMutable = true
}
BitmapFactory.decodeStream(inputStream, null, options)
}
}
}
/**
* Scale a bitmap using the fit width method
* The bitmap will fit into the given width while maintaining the same aspect ratio
@@ -1,6 +1,5 @@
package com.anthonyla.paperize.feature.wallpaper.presentation.settings_screen
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.anthonyla.paperize.core.ScalingConstants
@@ -93,7 +92,6 @@ class SettingsViewModel @Inject constructor (
)
}
setKeepOnScreenCondition = false
Log.d("PaperizeWallpaperChanger", "home blur ${homeBlurPercentage.await()} lock blur ${lockBlurPercentage.await()}")
}
}
@@ -135,7 +135,7 @@ fun BlurSwitchAndSlider(
)
if (bothEnabled) {
Text(
text = stringResource(R.string.lock_screen) + " | " + stringResource(R.string.percentage, lockPercentage.roundToInt()),
text = stringResource(R.string.lock) + " | " + stringResource(R.string.percentage, lockPercentage.roundToInt()),
modifier = Modifier.padding(PaddingValues(horizontal = 24.dp)),
fontWeight = FontWeight.W400
)
@@ -198,7 +198,7 @@ fun BlurSwitchAndSlider(
)
if (bothEnabled) {
Text(
text = stringResource(R.string.lock_screen) + " | " + stringResource(R.string.percentage, lockPercentage.roundToInt()),
text = stringResource(R.string.lock) + " | " + stringResource(R.string.percentage, lockPercentage.roundToInt()),
modifier = Modifier.padding(PaddingValues(horizontal = 24.dp)),
fontWeight = FontWeight.W400
)
@@ -137,7 +137,7 @@ fun DarkenSwitchAndSlider(
if (bothEnabled) {
Spacer(modifier = Modifier.height(16.dp))
Text(
text = stringResource(R.string.lock_screen) + " | " + stringResource(R.string.percentage, lockPercentage.roundToInt()),
text = stringResource(R.string.lock) + " | " + stringResource(R.string.percentage, lockPercentage.roundToInt()),
modifier = Modifier.padding(PaddingValues(horizontal = 24.dp)),
fontWeight = FontWeight.W400
)
@@ -201,7 +201,7 @@ fun DarkenSwitchAndSlider(
if (bothEnabled) {
Spacer(modifier = Modifier.height(16.dp))
Text(
text = stringResource(R.string.lock_screen) + " | " + stringResource(R.string.percentage, lockPercentage.roundToInt()),
text = stringResource(R.string.lock) + " | " + stringResource(R.string.percentage, lockPercentage.roundToInt()),
modifier = Modifier.padding(PaddingValues(horizontal = 24.dp)),
fontWeight = FontWeight.W400
)
@@ -8,10 +8,7 @@ import android.app.WallpaperManager
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.ImageDecoder
import android.net.Uri
import android.os.Build
import android.os.Handler
import android.os.HandlerThread
import android.os.IBinder
@@ -25,12 +22,11 @@ import com.anthonyla.paperize.core.ScalingConstants
import com.anthonyla.paperize.core.SettingsConstants
import com.anthonyla.paperize.core.Type
import com.anthonyla.paperize.core.blurBitmap
import com.anthonyla.paperize.core.calculateInSampleSize
import com.anthonyla.paperize.core.darkenBitmap
import com.anthonyla.paperize.core.fillBitmap
import com.anthonyla.paperize.core.fitBitmap
import com.anthonyla.paperize.core.getImageDimensions
import com.anthonyla.paperize.core.getWallpaperFromFolder
import com.anthonyla.paperize.core.retrieveBitmap
import com.anthonyla.paperize.core.stretchBitmap
import com.anthonyla.paperize.data.settings.SettingsDataStore
import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper
@@ -119,6 +115,7 @@ class HomeWallpaperService: Service() {
private fun workerTaskStart() {
workerHandler.post {
CoroutineScope(Dispatchers.IO).launch {
delay(250) // To ensure lock screen wallpaper is set first
changeWallpaper(this@HomeWallpaperService)
}
stopSelf()
@@ -233,7 +230,7 @@ class HomeWallpaperService: Service() {
blur = blur,
blurPercent = homeBlurPercentage
)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (newWallpapers.size > 1) newWallpapers[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (newWallpapers.size > 1) newWallpapers[1] else homeAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
if (success) {
selectedRepository.upsertSelectedAlbum(homeAlbum.copy(album = homeAlbum.album.copy(homeWallpapersInQueue = newWallpapers.drop(1))))
settingsDataStoreImpl.putString(SettingsConstants.CURRENT_HOME_WALLPAPER, wallpaper.toString())
@@ -265,7 +262,7 @@ class HomeWallpaperService: Service() {
blur = blur,
blurPercent = homeBlurPercentage
)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (homeAlbum.album.homeWallpapersInQueue.size > 1) homeAlbum.album.homeWallpapersInQueue[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (homeAlbum.album.homeWallpapersInQueue.size > 1) homeAlbum.album.homeWallpapersInQueue[1] else homeAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
if ((homeInterval % lockInterval == 0) || (lockInterval % homeInterval == 0) && (homeAlbumName == lockAlbumName)) {
delay(1000)
selectedAlbum = selectedRepository.getSelectedAlbum().first()
@@ -317,9 +314,11 @@ class HomeWallpaperService: Service() {
blur = blur,
blurPercent = homeBlurPercentage
)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (newWallpapers.size > 1) newWallpapers[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (homeAlbum.album.homeWallpapersInQueue.size > 1) homeAlbum.album.homeWallpapersInQueue[1] else homeAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
if (success) {
selectedRepository.upsertSelectedAlbum(homeAlbum.copy(album = homeAlbum.album.copy(homeWallpapersInQueue = newWallpapers.drop(1))))
if (newWallpapers.size > 1) {
selectedRepository.upsertSelectedAlbum(homeAlbum.copy(album = homeAlbum.album.copy(homeWallpapersInQueue = newWallpapers.drop(1))))
}
settingsDataStoreImpl.putString(SettingsConstants.CURRENT_HOME_WALLPAPER, wallpaper.toString())
}
else {
@@ -349,9 +348,22 @@ class HomeWallpaperService: Service() {
blur = blur,
blurPercent = homeBlurPercentage
)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (homeAlbum.album.homeWallpapersInQueue.size > 1) homeAlbum.album.homeWallpapersInQueue[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (homeAlbum.album.homeWallpapersInQueue.size > 1) homeAlbum.album.homeWallpapersInQueue[1] else homeAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
if (success) {
selectedRepository.upsertSelectedAlbum(homeAlbum.copy(album = homeAlbum.album.copy(homeWallpapersInQueue = homeAlbum.album.homeWallpapersInQueue.drop(1))))
if (homeAlbum.album.homeWallpapersInQueue.size > 1) {
selectedRepository.upsertSelectedAlbum(homeAlbum.copy(album = homeAlbum.album.copy(homeWallpapersInQueue = homeAlbum.album.homeWallpapersInQueue.drop(1))))
}
else {
val newWallpapers = homeAlbum.wallpapers.map { it.wallpaperUri }.shuffled()
if (newWallpapers.isNotEmpty()) {
selectedRepository.upsertSelectedAlbum(homeAlbum.copy(album = homeAlbum.album.copy(homeWallpapersInQueue = newWallpapers)))
}
else {
selectedRepository.cascadeDeleteAlbum(homeAlbum.album.initialAlbumName)
onDestroy()
return
}
}
settingsDataStoreImpl.putString(SettingsConstants.CURRENT_HOME_WALLPAPER, wallpaper.toString())
}
else {
@@ -394,8 +406,8 @@ class HomeWallpaperService: Service() {
blur = blur,
blurPercent = homeBlurPercentage
)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (newWallpapers.size > 1) newWallpapers[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (newWallpapers.size > 1) newWallpapers[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (homeAlbum.album.homeWallpapersInQueue.size > 1) homeAlbum.album.homeWallpapersInQueue[1] else homeAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (homeAlbum.album.homeWallpapersInQueue.size > 1) homeAlbum.album.homeWallpapersInQueue[1] else homeAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
if (success) {
selectedRepository.upsertSelectedAlbum(homeAlbum.copy(album = homeAlbum.album.copy(homeWallpapersInQueue = newWallpapers.drop(1))))
settingsDataStoreImpl.putString(SettingsConstants.CURRENT_HOME_WALLPAPER, wallpaper.toString())
@@ -429,8 +441,8 @@ class HomeWallpaperService: Service() {
blur = blur,
blurPercent = homeBlurPercentage
)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (homeAlbum.album.homeWallpapersInQueue.size > 1) homeAlbum.album.homeWallpapersInQueue[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (homeAlbum.album.homeWallpapersInQueue.size > 1) homeAlbum.album.homeWallpapersInQueue[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (homeAlbum.album.homeWallpapersInQueue.size > 1) homeAlbum.album.homeWallpapersInQueue[1] else homeAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (homeAlbum.album.homeWallpapersInQueue.size > 1) homeAlbum.album.homeWallpapersInQueue[1] else homeAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
if (success) {
selectedRepository.upsertSelectedAlbum(homeAlbum.copy(album = homeAlbum.album.copy(homeWallpapersInQueue = homeAlbum.album.homeWallpapersInQueue.drop(1))))
settingsDataStoreImpl.putString(SettingsConstants.CURRENT_HOME_WALLPAPER, wallpaper.toString())
@@ -546,35 +558,8 @@ class HomeWallpaperService: Service() {
): Boolean {
val wallpaperManager = WallpaperManager.getInstance(context)
try {
val imageSize = wallpaper.getImageDimensions(context) ?: return false
val device = context.resources.displayMetrics
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
try {
val source = ImageDecoder.createSource(context.contentResolver, wallpaper)
ImageDecoder.decodeBitmap(source) { decoder, _, _ ->
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, device.widthPixels, device.heightPixels)
inMutable = true
}
BitmapFactory.decodeStream(inputStream, null, options)
}
}
}
else {
context.contentResolver.openInputStream(wallpaper)?.use { inputStream ->
val options = BitmapFactory.Options().apply {
inSampleSize = calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels)
inMutable = true
}
BitmapFactory.decodeStream(inputStream, null, options)
}
}
val bitmap = retrieveBitmap(context, wallpaper, device)
if (bitmap == null) return false
else {
processBitmap(device, bitmap, darken, darkenPercent, scaling, blur, blurPercent)?.let { image ->
@@ -8,10 +8,7 @@ import android.app.WallpaperManager
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.ImageDecoder
import android.net.Uri
import android.os.Build
import android.os.Handler
import android.os.HandlerThread
import android.os.IBinder
@@ -25,12 +22,11 @@ import com.anthonyla.paperize.core.ScalingConstants
import com.anthonyla.paperize.core.SettingsConstants
import com.anthonyla.paperize.core.Type
import com.anthonyla.paperize.core.blurBitmap
import com.anthonyla.paperize.core.calculateInSampleSize
import com.anthonyla.paperize.core.darkenBitmap
import com.anthonyla.paperize.core.fillBitmap
import com.anthonyla.paperize.core.fitBitmap
import com.anthonyla.paperize.core.getImageDimensions
import com.anthonyla.paperize.core.getWallpaperFromFolder
import com.anthonyla.paperize.core.retrieveBitmap
import com.anthonyla.paperize.core.stretchBitmap
import com.anthonyla.paperize.data.settings.SettingsDataStore
import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper
@@ -234,7 +230,7 @@ class LockWallpaperService: Service() {
blur = blur,
blurPercent = lockBlurPercentage
)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (newWallpapers.size > 1) newWallpapers[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (newWallpapers.size > 1) newWallpapers[1] else lockAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
if (success) {
selectedRepository.upsertSelectedAlbum(lockAlbum.copy(album = lockAlbum.album.copy(lockWallpapersInQueue = newWallpapers.drop(1))))
settingsDataStoreImpl.putString(SettingsConstants.CURRENT_LOCK_WALLPAPER, wallpaper.toString())
@@ -266,7 +262,7 @@ class LockWallpaperService: Service() {
blur = blur,
blurPercent = lockBlurPercentage
)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (lockAlbum.album.lockWallpapersInQueue.size > 1) lockAlbum.album.lockWallpapersInQueue[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (lockAlbum.album.lockWallpapersInQueue.size > 1) lockAlbum.album.lockWallpapersInQueue[1] else lockAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
if (success) {
selectedRepository.upsertSelectedAlbum(lockAlbum.copy(album = lockAlbum.album.copy(lockWallpapersInQueue = lockAlbum.album.lockWallpapersInQueue.drop(1))))
settingsDataStoreImpl.putString(SettingsConstants.CURRENT_LOCK_WALLPAPER, wallpaper.toString())
@@ -312,7 +308,7 @@ class LockWallpaperService: Service() {
blur = blur,
blurPercent = lockBlurPercentage
)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (homeAlbum.album.homeWallpapersInQueue.size > 1) homeAlbum.album.homeWallpapersInQueue[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (homeAlbum.album.homeWallpapersInQueue.size > 1) homeAlbum.album.homeWallpapersInQueue[1] else homeAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
if (success) {
settingsDataStoreImpl.putString(SettingsConstants.CURRENT_LOCK_WALLPAPER, wallpaper.toString())
}
@@ -341,8 +337,8 @@ class LockWallpaperService: Service() {
blur = blur,
blurPercent = homeBlurPercentage
)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (newWallpapers.size > 1) newWallpapers[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (newWallpapers.size > 1) newWallpapers[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (newWallpapers.size > 1) newWallpapers[1] else lockAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (newWallpapers.size > 1) newWallpapers[1] else lockAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
if (success) {
selectedRepository.upsertSelectedAlbum(lockAlbum.copy(album = lockAlbum.album.copy(lockWallpapersInQueue = newWallpapers.drop(1))))
settingsDataStoreImpl.putString(SettingsConstants.CURRENT_LOCK_WALLPAPER, wallpaper.toString())
@@ -375,8 +371,8 @@ class LockWallpaperService: Service() {
blur = blur,
blurPercent = homeBlurPercentage
)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (lockAlbum.album.lockWallpapersInQueue.size > 1) lockAlbum.album.lockWallpapersInQueue[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (lockAlbum.album.lockWallpapersInQueue.size > 1) lockAlbum.album.lockWallpapersInQueue[1] else "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_LOCK_WALLPAPER, if (lockAlbum.album.lockWallpapersInQueue.size > 1) lockAlbum.album.lockWallpapersInQueue[1] else lockAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
settingsDataStoreImpl.putString(SettingsConstants.NEXT_HOME_WALLPAPER, if (lockAlbum.album.lockWallpapersInQueue.size > 1) lockAlbum.album.lockWallpapersInQueue[1] else lockAlbum.wallpapers.firstOrNull()?.wallpaperUri ?: "")
if (success) {
selectedRepository.upsertSelectedAlbum(lockAlbum.copy(album = lockAlbum.album.copy(lockWallpapersInQueue = lockAlbum.album.lockWallpapersInQueue.drop(1))))
settingsDataStoreImpl.putString(SettingsConstants.CURRENT_LOCK_WALLPAPER, wallpaper.toString())
@@ -483,35 +479,8 @@ class LockWallpaperService: Service() {
): Boolean {
val wallpaperManager = WallpaperManager.getInstance(context)
try {
val imageSize = wallpaper.getImageDimensions(context) ?: return false
val device = context.resources.displayMetrics
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
try {
val source = ImageDecoder.createSource(context.contentResolver, wallpaper)
ImageDecoder.decodeBitmap(source) { decoder, _, _ ->
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, device.widthPixels, device.heightPixels)
inMutable = true
}
BitmapFactory.decodeStream(inputStream, null, options)
}
}
}
else {
context.contentResolver.openInputStream(wallpaper)?.use { inputStream ->
val options = BitmapFactory.Options().apply {
inSampleSize = calculateInSampleSize(imageSize, device.widthPixels, device.heightPixels)
inMutable = true
}
BitmapFactory.decodeStream(inputStream, null, options)
}
}
val bitmap = retrieveBitmap(context, wallpaper, device)
if (bitmap == null) return false
else {
processBitmap(device, bitmap, darken, darkenPercent, scaling, blur, blurPercent)?.let { image ->
+1
View File
@@ -161,4 +161,5 @@
<string name="cannot_change_wallpaper">Cannot change wallpaper</string>
<string name="enable_wallpaper_changer">Enable Wallpaper Changer</string>
<string name="unselected_album">Unselected album</string>
<string name="lock">Lock</string>
</resources>