Added additional error catching

Updated Kotlin to 2.0.0
Updated KSP to 2.0.0
Updated AboutLibraries to 11.2.1
This commit is contained in:
Anthonyy232 committed 2024-06-11 12:18:22 -07:00
1 parent d8fc106a89
commit 4760ab01d5
7 files changed
+134 -71

No files matched your search

@@ -150,15 +150,13 @@ fun blurBitmap(source: Bitmap, percent: Int): Bitmap {
* Retrieve wallpaper URIs from a folder directory URI
*/
fun getWallpaperFromFolder(folderUri: String, context: Context): List<String> {
/*try {
try {
val folderDocumentFile = DocumentFileCompat.fromTreeUri(context, folderUri.toUri())
return listFilesRecursive(folderDocumentFile, context)
} catch (e: Exception) {
val folderDocumentFile = DocumentFile.fromTreeUri(context, folderUri.toUri())
return listFilesRecursive(folderDocumentFile, context)
}*/
val folderDocumentFile = DocumentFileCompat.fromTreeUri(context, folderUri.toUri())
return listFilesRecursive(folderDocumentFile, context)
}
}
/**
@@ -200,16 +198,30 @@ fun listFilesRecursive(parent: DocumentFile?, context: Context): List<String> {
*/
fun findFirstValidUri(context: Context, wallpapers: List<Wallpaper>, folders: List<Folder>): String? {
wallpapers.forEach { wallpaper ->
val file = DocumentFileCompat.fromSingleUri(context, wallpaper.wallpaperUri.toUri())
if (file?.exists() == true) {
return wallpaper.wallpaperUri
try {
val file = DocumentFileCompat.fromSingleUri(context, wallpaper.wallpaperUri.toUri())
if (file?.exists() == true) {
return wallpaper.wallpaperUri
}
} catch (e: Exception) {
val file = DocumentFile.fromSingleUri(context, wallpaper.wallpaperUri.toUri())
if (file?.exists() == true) {
return wallpaper.wallpaperUri
}
}
}
folders.forEach { folder ->
folder.wallpapers.forEach { wallpaper ->
val file = DocumentFileCompat.fromSingleUri(context, wallpaper.toUri())
if (file?.exists() == true) {
return wallpaper
try {
val file = DocumentFileCompat.fromSingleUri(context, wallpaper.toUri())
if (file?.exists() == true) {
return wallpaper
}
} catch (e: Exception) {
val file = DocumentFile.fromSingleUri(context, wallpaper.toUri())
if (file?.exists() == true) {
return wallpaper
}
}
}
}
@@ -220,12 +232,11 @@ fun findFirstValidUri(context: Context, wallpapers: List<Wallpaper>, folders: Li
* Get the folder name from the folder URI
*/
fun getFolderNameFromUri(folderUri: String, context: Context): String? {
/*return try {
return try {
DocumentFileCompat.fromTreeUri(context, folderUri.toUri())?.name
} catch (e: Exception) {
DocumentFile.fromTreeUri(context, folderUri.toUri())?.name
}*/
return DocumentFileCompat.fromTreeUri(context, folderUri.toUri())?.name
}
}
/**
@@ -86,8 +86,17 @@ class AlbumsViewModel @Inject constructor (
albumWithWallpapers.forEach { albumWithWallpaper ->
// Delete wallpaper if the URI is invalid
val invalidWallpapers = albumWithWallpaper.wallpapers.filterNot { wallpaper ->
val file = DocumentFileCompat.fromSingleUri(context, wallpaper.wallpaperUri.toUri())
file?.exists() == true
try {
val file = DocumentFileCompat.fromSingleUri(
context,
wallpaper.wallpaperUri.toUri()
)
file?.exists() == true
} catch (e: Exception) {
val file =
DocumentFile.fromSingleUri(context, wallpaper.wallpaperUri.toUri())
file?.exists() == true
}
}
if (invalidWallpapers.isNotEmpty()) {
repository.deleteWallpaperList(invalidWallpapers)
@@ -96,42 +105,99 @@ class AlbumsViewModel @Inject constructor (
// Update folder cover uri and wallpapers uri
albumWithWallpaper.folders.forEach { folder ->
try {
DocumentFileCompat.fromTreeUri(context, folder.folderUri.toUri())?.let { folderDirectory ->
if (!folderDirectory.isDirectory()) {
repository.deleteFolder(folder)
} else {
val wallpapers = getWallpaperFromFolder(folder.folderUri, context)
val folderCoverFile = folder.coverUri?.let { DocumentFileCompat.fromSingleUri(context, it.toUri()) }
val folderCover = folderCoverFile?.takeIf { it.exists() }?.uri?.toString() ?: wallpapers.randomOrNull()
repository.updateFolder(folder.copy(coverUri = folderCover, wallpapers = wallpapers))
DocumentFileCompat.fromTreeUri(context, folder.folderUri.toUri())
?.let { folderDirectory ->
if (!folderDirectory.isDirectory()) {
repository.deleteFolder(folder)
} else {
val wallpapers =
getWallpaperFromFolder(folder.folderUri, context)
val folderCoverFile = folder.coverUri?.let {
DocumentFileCompat.fromSingleUri(
context,
it.toUri()
)
}
val folderCover =
folderCoverFile?.takeIf { it.exists() }?.uri?.toString()
?: wallpapers.randomOrNull()
repository.updateFolder(
folder.copy(
coverUri = folderCover,
wallpapers = wallpapers
)
)
}
}
}
} catch (e: Exception) {
DocumentFile.fromTreeUri(context, folder.folderUri.toUri())?.let { folderDirectory ->
if (!folderDirectory.isDirectory) {
repository.deleteFolder(folder)
} else {
val wallpapers = getWallpaperFromFolder(folder.folderUri, context)
val folderCoverFile = folder.coverUri?.let { DocumentFileCompat.fromSingleUri(context, it.toUri()) }
val folderCover = folderCoverFile?.takeIf { it.exists() }?.uri?.toString() ?: wallpapers.randomOrNull()
repository.updateFolder(folder.copy(coverUri = folderCover, wallpapers = wallpapers))
DocumentFile.fromTreeUri(context, folder.folderUri.toUri())
?.let { folderDirectory ->
if (!folderDirectory.isDirectory) {
repository.deleteFolder(folder)
} else {
val wallpapers =
getWallpaperFromFolder(folder.folderUri, context)
try {
val folderCoverFile = folder.coverUri?.let {
DocumentFileCompat.fromSingleUri(
context,
it.toUri()
)
}
val folderCover =
folderCoverFile?.takeIf { it.exists() }?.uri?.toString()
?: wallpapers.randomOrNull()
repository.updateFolder(
folder.copy(
coverUri = folderCover,
wallpapers = wallpapers
)
)
} catch (e: Exception) {
val folderCoverFile = folder.coverUri?.let {
DocumentFile.fromSingleUri(
context,
it.toUri()
)
}
val folderCover =
folderCoverFile?.takeIf { it.exists() }?.uri?.toString()
?: wallpapers.randomOrNull()
repository.updateFolder(
folder.copy(
coverUri = folderCover,
wallpapers = wallpapers
)
)
}
}
}
}
}
}
}
albumWithWallpapers = repository.getAlbumsWithWallpaperAndFolder().first()
albumWithWallpapers.forEach { albumWithWallpaper ->
// Delete empty albums
if (albumWithWallpaper.wallpapers.isEmpty() && albumWithWallpaper.folders.all { it.wallpapers.isEmpty() }) {
repository.deleteAlbum(albumWithWallpaper.album)
}
else {
// Update album cover uri if null or invalid
val albumCoverFile = albumWithWallpaper.album.coverUri?.toUri()?.let { DocumentFileCompat.fromSingleUri(context, it) }
if (albumCoverFile == null || !albumCoverFile.exists()) {
val newCoverUri = findFirstValidUri(context, albumWithWallpaper.wallpapers, albumWithWallpaper.folders)
repository.updateAlbum(albumWithWallpaper.album.copy(coverUri = newCoverUri))
albumWithWallpapers = repository.getAlbumsWithWallpaperAndFolder().first()
albumWithWallpapers.forEach { album ->
// Delete empty albums
if (album.wallpapers.isEmpty() && album.folders.all { it.wallpapers.isEmpty() }) {
repository.deleteAlbum(album.album)
} else {
// Update album cover uri if null or invalid
try {
val albumCoverFile = album.album.coverUri?.toUri()
?.let { DocumentFileCompat.fromSingleUri(context, it) }
if (albumCoverFile == null || !albumCoverFile.exists()) {
val newCoverUri =
findFirstValidUri(context, album.wallpapers, album.folders)
repository.updateAlbum(album.album.copy(coverUri = newCoverUri))
}
} catch (e: Exception) {
val albumCoverFile = album.album.coverUri?.toUri()
?.let { DocumentFile.fromSingleUri(context, it) }
if (albumCoverFile == null || !albumCoverFile.exists()) {
val newCoverUri =
findFirstValidUri(context, album.wallpapers, album.folders)
repository.updateAlbum(album.album.copy(coverUri = newCoverUri))
}
}
}
}
}
@@ -531,14 +531,6 @@ class WallpaperService1: Service() {
blur: Boolean = false,
blurPercent: Int,
): Boolean {
when {
setLockOrHome == true -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for home")
setLockOrHome == false -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for lock")
setHome && setLock -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for both")
setHome -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for home")
setLock -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for lock")
else -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for both")
}
val wallpaperManager = WallpaperManager.getInstance(context)
try {
val imageSize = wallpaper.getImageDimensions(context) ?: return false
@@ -514,14 +514,6 @@ class WallpaperService2: Service() {
blur: Boolean = false,
blurPercent: Int
): Boolean {
when {
setLockOrHome == true -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for home")
setLockOrHome == false -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for lock")
setHome && setLock -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for both")
setHome -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for home")
setLock -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for lock")
else -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for both")
}
val wallpaperManager = WallpaperManager.getInstance(context)
try {
val imageSize = wallpaper.getImageDimensions(context) ?: return false