Skip to content

Commit

Permalink
Merge pull request #133 from Hamza417/live_wallpaper_fix
Browse files Browse the repository at this point in the history
Live wallpaper fix
  • Loading branch information
Hamza417 authored Jan 29, 2025
2 parents 8f2493f + 13ebabd commit e71ea28
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,55 @@ package app.simple.peri.abstraction
import android.app.ActivityManager
import android.content.Context
import android.os.Parcelable
import android.util.Log
import app.simple.peri.models.Wallpaper
import app.simple.peri.preferences.MainPreferences
import app.simple.peri.services.LiveAutoWallpaperService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

abstract class AbstractAutoLiveWallpaperService : AbstractComposeAutoWallpaperService() {

protected fun postLiveWallpaper(onComplete: () -> Unit) {
setComposeWallpaper {
onComplete()
CoroutineScope(Dispatchers.IO).launch {
runCatching {
when {
MainPreferences.isSettingForHomeScreen() -> {
Log.d(TAG, "Setting wallpaper for home screen")
getHomeScreenWallpaper()?.let { wallpaper ->
setHomeScreenWallpaper(wallpaper)
}
}
else -> {
Log.d(TAG, "Setting wallpaper for both home and lock screen")
getRandomWallpaperFromDatabase()?.let { wallpaper ->
setSameWallpaper(wallpaper)
}
}
}

withContext(Dispatchers.Main) {
onComplete()
stopSelf()
}
}.getOrElse {
it.printStackTrace()
Log.e(TAG, "Error setting wallpaper: $it")

withContext(Dispatchers.Main) {
showErrorNotification(it.stackTraceToString())
onComplete()
stopSelf()
}
}
}
}

override fun setSameWallpaper(wallpaper: Wallpaper) {
if (isWallpaperServiceRunning()) {
val intent = LiveAutoWallpaperService.getIntent(applicationContext, LiveAutoWallpaperService.SAME_WALLPAPER)
val intent = LiveAutoWallpaperService.getIntent(applicationContext, LiveAutoWallpaperService.NEXT_WALLPAPER)
intent.putExtra(LiveAutoWallpaperService.EXTRA_WALLPAPER, wallpaper as Parcelable)
applicationContext.startService(intent)
} else {
Expand All @@ -29,7 +61,7 @@ abstract class AbstractAutoLiveWallpaperService : AbstractComposeAutoWallpaperSe

override fun setHomeScreenWallpaper(wallpaper: Wallpaper) {
if (isWallpaperServiceRunning()) {
val intent = LiveAutoWallpaperService.getIntent(applicationContext, LiveAutoWallpaperService.HOME_SCREEN_WALLPAPER)
val intent = LiveAutoWallpaperService.getIntent(applicationContext, LiveAutoWallpaperService.NEXT_WALLPAPER)
intent.putExtra(LiveAutoWallpaperService.EXTRA_WALLPAPER, wallpaper as Parcelable)
applicationContext.startService(intent)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ abstract class AbstractComposeAutoWallpaperService : AbstractLegacyAutoWallpaper
}
}

private fun shouldSetSameWallpaper(): Boolean {
fun shouldSetSameWallpaper(): Boolean {
if (!MainPreferences.isSettingForHomeScreen() || !MainPreferences.isSettingForLockScreen()) {
return false
}
Expand Down Expand Up @@ -191,7 +191,7 @@ abstract class AbstractComposeAutoWallpaperService : AbstractLegacyAutoWallpaper
return wallpaper
}

private fun getLockScreenWallpaper(): Wallpaper? {
fun getLockScreenWallpaper(): Wallpaper? {
val wallpaperDatabase = WallpaperDatabase.getInstance(applicationContext)
val wallpaperDao = wallpaperDatabase?.wallpaperDao()
val position = MainComposePreferences.getLastLockWallpaperPosition().plus(1)
Expand Down Expand Up @@ -337,7 +337,7 @@ abstract class AbstractComposeAutoWallpaperService : AbstractLegacyAutoWallpaper
notificationManager.notify(notificationId, notification)
}

private fun showErrorNotification(message: String) {
fun showErrorNotification(message: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (PermissionUtils.checkNotificationPermission(applicationContext).invert()) {
Log.i(TAG, "Notification permission not granted, skipping notification")
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/app/simple/peri/compose/screens/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ fun BottomMenu(modifier: Modifier = Modifier, navController: NavController? = nu
imageVector = Icons.Rounded.Schedule,
title = R.string.auto_wallpaper
) {
navController?.navigate(Routes.AUTO_WALLPAPER)
autoWallpaperScreenSelection.value = true
}

BottomMenuItem(
Expand All @@ -464,7 +464,7 @@ fun BottomMenu(modifier: Modifier = Modifier, navController: NavController? = nu
imageVector = Icons.Rounded.MotionPhotosOn,
title = R.string.live_wallpapers
) {
autoWallpaperScreenSelection.value = true
navController?.navigate(Routes.LIVE_WALLPAPERS)
}

Card(
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/app/simple/peri/models/Wallpaper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class Wallpaper() : Comparable<Wallpaper>, Serializable, Parcelable {

constructor(parcel: Parcel) : this() {
name = parcel.readString()
uri = parcel.readString().toString()
filePath = parcel.readString().toString()
id = parcel.readString().toString()
uri = parcel.readString() ?: ""
filePath = parcel.readString() ?: ""
id = parcel.readString() ?: ""
prominentColor = parcel.readInt()
width = parcel.readValue(Int::class.java.classLoader) as? Int
height = parcel.readValue(Int::class.java.classLoader) as? Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ class LiveAutoWallpaperService : WallpaperService() {

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
SAME_WALLPAPER, PREVIEW_WALLPAPER -> {
val wallpaper: Wallpaper = intent.parcelable<Wallpaper>(EXTRA_WALLPAPER)!!
Log.i(TAG, "Setting wallpaper: ${wallpaper.filePath}")
val msg = handler?.obtainMessage(MSG_SET_WALLPAPER, wallpaper.filePath)!!
handler?.sendMessage(msg)
NEXT_WALLPAPER, PREVIEW_WALLPAPER -> {
try {
val wallpaper: Wallpaper = intent.parcelable<Wallpaper>(EXTRA_WALLPAPER)!!
Log.i(TAG, "Setting wallpaper: ${wallpaper.filePath}")
val msg = handler?.obtainMessage(MSG_SET_WALLPAPER, wallpaper.filePath)!!
handler?.sendMessage(msg)
} catch (e: NullPointerException) {
e.printStackTrace()
}
}
}

Expand All @@ -113,13 +117,21 @@ class LiveAutoWallpaperService : WallpaperService() {
var localBitmap: Bitmap? = null

withContext(Dispatchers.Default) {
getBitmapFromFile(filePath, canvas.width, canvas.height, recycle = false) { bmp ->
localBitmap = bmp
try {
getBitmapFromFile(filePath, canvas.width, canvas.height, recycle = false) { bmp ->
localBitmap = bmp
}
} catch (e: NullPointerException) {
if (localBitmap == null) {
localBitmap = bitmap
}
}
}

surfaceHolder.unlockCanvasAndPost(canvas)
setBitmapWithCrossfade(localBitmap!!)
if (localBitmap != null) {
surfaceHolder.unlockCanvasAndPost(canvas)
setBitmapWithCrossfade(localBitmap!!)
}
}
}

Expand Down Expand Up @@ -220,14 +232,11 @@ class LiveAutoWallpaperService : WallpaperService() {

companion object {
private const val TAG = "LiveAutoWallpaperService"
const val SAME_WALLPAPER = "action.SAME_WALLPAPER"
const val HOME_SCREEN_WALLPAPER = "action.HOME_SCREEN_WALLPAPER"
const val LOCK_SCREEN_WALLPAPER = "action.LOCK_SCREEN_WALLPAPER"
const val NEXT_WALLPAPER = "action.SAME_WALLPAPER"
const val PREVIEW_WALLPAPER = "action.PREVIEW_WALLPAPER"
const val EXTRA_WALLPAPER = "extra.WALLPAPER"
const val MSG_SET_WALLPAPER = 1
private const val CROSSFADE_DURATION = 500L
private const val ASK_NEXT_WALLPAPER_DELAY = 250L

fun getIntent(context: Context, action: String): Intent {
return Intent(context, LiveAutoWallpaperService::class.java).apply {
Expand Down

0 comments on commit e71ea28

Please sign in to comment.