diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 2cc9c2f..1463be9 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -1,2 +1,8 @@ # Library consumer rules preserve their runtime entry points. Keep release crash locations. -keepattributes SourceFile,LineNumberTable + +# Strip debug and verbose logging from release builds. +-assumenosideeffects class android.util.Log { + public static int d(...); + public static int v(...); +} diff --git a/app/src/main/java/com/anthonyla/paperize/core/util/Extensions.kt b/app/src/main/java/com/anthonyla/paperize/core/util/Extensions.kt index 00ed8b0..bd4f104 100644 --- a/app/src/main/java/com/anthonyla/paperize/core/util/Extensions.kt +++ b/app/src/main/java/com/anthonyla/paperize/core/util/Extensions.kt @@ -2,14 +2,14 @@ package com.anthonyla.paperize.core.util import android.content.ContentResolver import android.content.Context +import android.database.Cursor import android.net.Uri import android.os.CancellationSignal import android.provider.DocumentsContract import com.anthonyla.paperize.core.constants.Constants -import android.database.Cursor +import com.anthonyla.paperize.core.runCatchingCancellable import java.io.IOException import java.util.UUID -import kotlinx.coroutines.CancellationException import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.ensureActive import kotlinx.coroutines.suspendCancellableCoroutine @@ -35,18 +35,14 @@ fun Uri.isValid(contentResolver: ContentResolver): Boolean { /** Only a successful, complete query with no rows proves that a document was removed. */ fun Uri.isDocumentMissing(contentResolver: ContentResolver): Boolean { if (scheme != ContentResolver.SCHEME_CONTENT) return false - return try { + // Offline providers, revoked permissions and query failures do not prove deletion. + return runCatchingCancellable { contentResolver.query(this, arrayOf(android.provider.OpenableColumns.DISPLAY_NAME), null, null, null) ?.use { cursor -> cursor.requireComplete() !cursor.moveToFirst() } ?: false - } catch (e: CancellationException) { - throw e - } catch (_: Exception) { - // Offline providers, revoked permissions and query failures do not prove deletion. - false - } + }.getOrDefault(false) } private fun Cursor.requireComplete() { diff --git a/app/src/main/java/com/anthonyla/paperize/domain/usecase/ChangeWallpaperUseCase.kt b/app/src/main/java/com/anthonyla/paperize/domain/usecase/ChangeWallpaperUseCase.kt index 2b7667f..c6bbbf5 100644 --- a/app/src/main/java/com/anthonyla/paperize/domain/usecase/ChangeWallpaperUseCase.kt +++ b/app/src/main/java/com/anthonyla/paperize/domain/usecase/ChangeWallpaperUseCase.kt @@ -5,6 +5,7 @@ import com.anthonyla.paperize.core.EmptyAlbumException import com.anthonyla.paperize.core.NoValidWallpaperException import com.anthonyla.paperize.core.ScreenType import com.anthonyla.paperize.core.constants.Constants +import com.anthonyla.paperize.core.runCatchingCancellable import com.anthonyla.paperize.domain.model.PreparedWallpaper import com.anthonyla.paperize.domain.model.Wallpaper import com.anthonyla.paperize.domain.repository.SettingsRepository @@ -87,49 +88,26 @@ class ChangeWallpaperUseCase @Inject constructor( wallpaperId: String, shuffle: Boolean ) { - try { - wallpaperRepository.setCurrentWallpaper( - albumId, - screenType, - wallpaperId - ) - } catch (e: CancellationException) { - throw e - } catch (e: Exception) { - Log.e(TAG, "Applied wallpaper could not be recorded as current", e) - return - } + runCatchingCancellable { wallpaperRepository.setCurrentWallpaper(albumId, screenType, wallpaperId) } + .onFailure { + Log.e(TAG, "Applied wallpaper could not be recorded as current", it) + return + } - try { + runCatchingCancellable { wallpaperRepository.ensureWallpaperQueue(albumId, screenType, shuffle) // Build first when this is the first synchronized use of a screen queue, then remove // the exact applied item. This prevents the just-applied wallpaper from being // reintroduced at the head of a freshly built queue. - wallpaperRepository.removeWallpaperFromQueue( - albumId, - screenType, - wallpaperId - ) - } catch (e: CancellationException) { - throw e - } catch (e: Exception) { - Log.w(TAG, "Queue sync failed; it will rebuild on the next change", e) - } + wallpaperRepository.removeWallpaperFromQueue(albumId, screenType, wallpaperId) + }.onFailure { Log.w(TAG, "Queue sync failed; it will rebuild on the next change", it) } } /** Restore a prepared item after WallpaperManager rejected it. */ suspend fun restore(prepared: PreparedWallpaper) { - try { - wallpaperRepository.restoreWallpaperToQueueFront( - prepared.albumId, - prepared.screenType, - prepared.wallpaperId - ) - } catch (e: CancellationException) { - throw e - } catch (e: Exception) { - Log.e(TAG, "Failed to restore rejected wallpaper to its queue", e) - } + runCatchingCancellable { + wallpaperRepository.restoreWallpaperToQueueFront(prepared.albumId, prepared.screenType, prepared.wallpaperId) + }.onFailure { Log.e(TAG, "Failed to restore rejected wallpaper to its queue", it) } } private companion object { diff --git a/app/src/main/java/com/anthonyla/paperize/presentation/screens/album_view/AlbumViewViewModel.kt b/app/src/main/java/com/anthonyla/paperize/presentation/screens/album_view/AlbumViewViewModel.kt index 269a0f6..c81b928 100644 --- a/app/src/main/java/com/anthonyla/paperize/presentation/screens/album_view/AlbumViewViewModel.kt +++ b/app/src/main/java/com/anthonyla/paperize/presentation/screens/album_view/AlbumViewViewModel.kt @@ -168,11 +168,6 @@ class AlbumViewViewModel @Inject constructor( .onSuccess { _selectedFolders.value -= folderId } .onFailure { _message.value = R.string.delete_items_failed } } - } catch (e: CancellationException) { - throw e - } catch (e: Exception) { - Log.e(TAG, "Error removing selected items", e) - _message.value = R.string.delete_items_failed } finally { _isDeleting.value = false } diff --git a/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeScreen.kt b/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeScreen.kt index a3f48fa..580b654 100644 --- a/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeScreen.kt +++ b/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeScreen.kt @@ -97,14 +97,15 @@ fun HomeScreen( ) { index -> when (index) { 0 -> { - if (wallpaperMode == null) { + val mode = wallpaperMode + if (mode == null) { Box(modifier = Modifier.fillMaxSize()) } else { WallpaperScreen( albums = albums, persistedScheduleSettings = scheduleSettings, appSettings = appSettings, - wallpaperMode = wallpaperMode!!, + wallpaperMode = mode, onToggleChanger = viewModel::setChangerEnabled, onSelectAlbum = viewModel::selectAlbum, onUpdateScheduleSettings = viewModel::updateScheduleSettings, diff --git a/app/src/main/java/com/anthonyla/paperize/presentation/screens/library/LibraryScreen.kt b/app/src/main/java/com/anthonyla/paperize/presentation/screens/library/LibraryScreen.kt index 09ac24a..68184dc 100644 --- a/app/src/main/java/com/anthonyla/paperize/presentation/screens/library/LibraryScreen.kt +++ b/app/src/main/java/com/anthonyla/paperize/presentation/screens/library/LibraryScreen.kt @@ -1,4 +1,5 @@ package com.anthonyla.paperize.presentation.screens.library + import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column @@ -39,7 +40,6 @@ import com.anthonyla.paperize.presentation.common.theme.AppSpacing import com.anthonyla.paperize.presentation.screens.library.components.AlbumItem import kotlinx.coroutines.launch - @Composable fun LibraryScreen( albums: List, diff --git a/app/src/main/java/com/anthonyla/paperize/service/livewallpaper/PaperizeLiveWallpaperService.kt b/app/src/main/java/com/anthonyla/paperize/service/livewallpaper/PaperizeLiveWallpaperService.kt index 947d92a..be60c64 100644 --- a/app/src/main/java/com/anthonyla/paperize/service/livewallpaper/PaperizeLiveWallpaperService.kt +++ b/app/src/main/java/com/anthonyla/paperize/service/livewallpaper/PaperizeLiveWallpaperService.kt @@ -276,7 +276,6 @@ class PaperizeLiveWallpaperService : GLWallpaperService() { yPixelOffset: Int ) { super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixelOffset, yPixelOffset) - Log.d(TAG, "onOffsetsChanged: xOffset=$xOffset, xOffsetStep=$xOffsetStep, xPixelOffset=$xPixelOffset") renderer.setNormalOffsetX(xOffset) } diff --git a/app/src/main/java/com/anthonyla/paperize/service/worker/AlbumRefreshWorker.kt b/app/src/main/java/com/anthonyla/paperize/service/worker/AlbumRefreshWorker.kt index 32e1308..853a343 100644 --- a/app/src/main/java/com/anthonyla/paperize/service/worker/AlbumRefreshWorker.kt +++ b/app/src/main/java/com/anthonyla/paperize/service/worker/AlbumRefreshWorker.kt @@ -10,7 +10,6 @@ import com.anthonyla.paperize.domain.repository.AlbumRepository import com.anthonyla.paperize.domain.usecase.RefreshFolderUseCase import dagger.assisted.Assisted import dagger.assisted.AssistedInject -import kotlinx.coroutines.CancellationException import kotlinx.coroutines.flow.first @HiltWorker @@ -21,57 +20,22 @@ class AlbumRefreshWorker @AssistedInject constructor( private val refreshFolderUseCase: RefreshFolderUseCase ) : CoroutineWorker(appContext, workerParams) { + /** Prune missing items and pick up new folder files. One failing album or folder does not stop the rest. */ override suspend fun doWork(): Result { - return try { - Log.d(TAG, "Starting daily album refresh") + albumRepository.getAlbumSummaries().first().forEach { album -> + runCatchingCancellable { albumRepository.pruneMissingEntries(album.id) } + .onFailure { Log.e(TAG, "Error validating album '${album.name}'", it) } - val albums = albumRepository.getAlbumSummaries().first() - - if (albums.isEmpty()) { - Log.d(TAG, "No albums to refresh") - return Result.success() + // Reload after validation, which may have removed folders from the snapshot. + albumRepository.getAlbumById(album.id).first()?.folders.orEmpty().forEach { folder -> + runCatchingCancellable { refreshFolderUseCase(folder.id) } + .onFailure { Log.e(TAG, "Error refreshing folder '${folder.name}'", it) } } - - var totalRemoved = 0 - var totalAdded = 0 - var failedCount = 0 - - albums.forEach { album -> - runCatchingCancellable { albumRepository.pruneMissingEntries(album.id) } - .onSuccess { removedCount -> - totalRemoved += removedCount - if (removedCount > 0) { - Log.d(TAG, "Album '${album.name}': removed $removedCount invalid items") - } - } - .onFailure { - Log.e(TAG, "Error validating album '${album.name}'", it) - failedCount++ - } - - // Reload after validation, which may have removed folders from the snapshot. - val currentAlbum = albumRepository.getAlbumById(album.id).first() - currentAlbum?.folders.orEmpty().forEach { folder -> - runCatchingCancellable { refreshFolderUseCase(folder.id) } - .onSuccess { totalAdded += it } - .onFailure { - Log.e(TAG, "Error refreshing folder '${folder.name}'", it) - failedCount++ - } - } - } - Log.d(TAG, "Daily album refresh completed: removed $totalRemoved items, added $totalAdded new wallpapers across ${albums.size} albums ($failedCount failures)") - - Result.success() - } catch (e: CancellationException) { - throw e - } catch (e: Exception) { - Log.e(TAG, "Error during album refresh", e) - Result.failure() } + return Result.success() } - companion object { - private const val TAG = "AlbumRefreshWorker" + private companion object { + const val TAG = "AlbumRefreshWorker" } }