From 15b58ce4fb8bfc711bb8724cefb5a231e544d423 Mon Sep 17 00:00:00 2001 From: T8RIN Date: Thu, 16 Jan 2025 17:01:15 +0300 Subject: [PATCH] Replace Exception by Throwable --- .../ui/utils/helper/LocaleConfigCompat.kt | 10 +-- .../ui/widget/enhanced/EnhancedAlertDialog.kt | 2 +- .../core/ui/widget/image/ImagePager.kt | 6 +- .../cipher/data/AndroidCryptographyManager.kt | 4 +- .../presentation/LibrariesInfoContent.kt | 2 +- .../media_picker/data/utils/MediaObserver.kt | 11 ++-- .../media_picker/data/utils/MediaQuery.kt | 63 +------------------ .../components/MediaImagePager.kt | 6 +- .../presentation/components/PdfPage.kt | 2 +- .../components/FullscreenEditOption.kt | 2 +- 10 files changed, 21 insertions(+), 87 deletions(-) diff --git a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/utils/helper/LocaleConfigCompat.kt b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/utils/helper/LocaleConfigCompat.kt index a5e810930c..cc17fa845f 100644 --- a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/utils/helper/LocaleConfigCompat.kt +++ b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/utils/helper/LocaleConfigCompat.kt @@ -74,15 +74,13 @@ class LocaleConfigCompat(context: Context) { private class Api21Impl(context: Context) : Impl() { override var status = 0 - private set override var supportedLocales: LocaleListCompat? = null - private set init { val resourceId = try { getLocaleConfigResourceId(context) - } catch (e: Exception) { + } catch (e: Throwable) { Log.w(TAG, "The resource file pointed to by the given resource ID isn't found.", e) } if (resourceId == ResourcesCompat.ID_NULL) { @@ -92,7 +90,7 @@ class LocaleConfigCompat(context: Context) { try { supportedLocales = resources.getXml(resourceId).use { parseLocaleConfig(it) } status = STATUS_SUCCESS - } catch (e: Exception) { + } catch (e: Throwable) { val resourceEntryName = resources.getResourceEntryName(resourceId) Log.w(TAG, "Failed to parse XML configuration from $resourceEntryName", e) status = STATUS_PARSING_FAILED @@ -113,7 +111,7 @@ class LocaleConfigCompat(context: Context) { while (true) { val parser = try { context.assets.openXmlResourceParser(cookie, FILE_NAME_ANDROID_MANIFEST) - } catch (e: FileNotFoundException) { + } catch (_: FileNotFoundException) { if (!isAndroidManifestFound) { ++cookie continue @@ -209,10 +207,8 @@ class LocaleConfigCompat(context: Context) { @RequiresApi(Build.VERSION_CODES.TIRAMISU) private class Api33Impl(context: Context) : Impl() { override var status: Int = 0 - private set override var supportedLocales: LocaleListCompat? = null - private set init { val platformLocaleConfig = LocaleConfig(context) diff --git a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/enhanced/EnhancedAlertDialog.kt b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/enhanced/EnhancedAlertDialog.kt index a5a7c682da..627c364922 100644 --- a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/enhanced/EnhancedAlertDialog.kt +++ b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/enhanced/EnhancedAlertDialog.kt @@ -231,7 +231,7 @@ fun BasicEnhancedAlertDialog( onDismissRequest() delay(400) scale = 1f - } catch (_: Exception) { + } catch (_: Throwable) { scale = 1f } } diff --git a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/image/ImagePager.kt b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/image/ImagePager.kt index 354fa28f3a..65de643080 100644 --- a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/image/ImagePager.kt +++ b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/image/ImagePager.kt @@ -65,7 +65,6 @@ import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text -import androidx.compose.material3.TopAppBarDefaults import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.derivedStateOf @@ -105,6 +104,7 @@ import ru.tech.imageresizershrinker.core.ui.utils.helper.ContextUtils.getFilenam import ru.tech.imageresizershrinker.core.ui.utils.navigation.Screen import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedIconButton import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBar +import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBarDefaults import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBarType import ru.tech.imageresizershrinker.core.ui.widget.modifier.toShape import ru.tech.imageresizershrinker.core.ui.widget.modifier.withLayoutCorners @@ -309,7 +309,7 @@ fun ImagePager( exit = fadeOut() + slideOutVertically() ) { EnhancedTopAppBar( - colors = TopAppBarDefaults.centerAlignedTopAppBarColors( + colors = EnhancedTopAppBarDefaults.colors( containerColor = MaterialTheme.colorScheme.scrim.copy(alpha = 0.5f) ), type = EnhancedTopAppBarType.Center, @@ -472,7 +472,7 @@ fun ImagePager( } onDismiss() onUriSelected(null) - } catch (_: Exception) { + } catch (_: Throwable) { predictiveBackProgress = 0f } } diff --git a/feature/cipher/src/main/java/ru/tech/imageresizershrinker/feature/cipher/data/AndroidCryptographyManager.kt b/feature/cipher/src/main/java/ru/tech/imageresizershrinker/feature/cipher/data/AndroidCryptographyManager.kt index 21b0648bee..4e26b59b55 100644 --- a/feature/cipher/src/main/java/ru/tech/imageresizershrinker/feature/cipher/data/AndroidCryptographyManager.kt +++ b/feature/cipher/src/main/java/ru/tech/imageresizershrinker/feature/cipher/data/AndroidCryptographyManager.kt @@ -118,7 +118,7 @@ internal class AndroidCryptographyManager @Inject constructor() : CryptographyMa ) } } - } catch (_: Exception) { + } catch (_: Throwable) { runCatching { init( mode, @@ -148,7 +148,7 @@ internal class AndroidCryptographyManager @Inject constructor() : CryptographyMa private fun Cipher.doOrThrow(data: ByteArray): ByteArray { return try { doFinal(data) - } catch (e: Exception) { + } catch (e: Throwable) { throw if (e.message?.contains("mac") == true && e.message?.contains("failed") == true) { WrongKeyException() } else e diff --git a/feature/libraries-info/src/main/java/ru/tech/imageresizershrinker/feature/libraries_info/presentation/LibrariesInfoContent.kt b/feature/libraries-info/src/main/java/ru/tech/imageresizershrinker/feature/libraries_info/presentation/LibrariesInfoContent.kt index fecb70fa56..085814f441 100644 --- a/feature/libraries-info/src/main/java/ru/tech/imageresizershrinker/feature/libraries_info/presentation/LibrariesInfoContent.kt +++ b/feature/libraries-info/src/main/java/ru/tech/imageresizershrinker/feature/libraries_info/presentation/LibrariesInfoContent.kt @@ -262,7 +262,7 @@ fun LibrariesInfoContent( predictiveBackProgress = event.progress } component.selectLibrary(null) - } catch (_: Exception) { + } catch (_: Throwable) { predictiveBackProgress = 0f } } diff --git a/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/data/utils/MediaObserver.kt b/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/data/utils/MediaObserver.kt index 82299e8b28..0e3907272f 100644 --- a/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/data/utils/MediaObserver.kt +++ b/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/data/utils/MediaObserver.kt @@ -80,7 +80,7 @@ suspend fun ContentResolver.getMedia( while (cursor.moveToNext()) { try { media.add(cursor.getMediaFromCursor()) - } catch (e: Exception) { + } catch (e: Throwable) { e.printStackTrace() } } @@ -91,7 +91,6 @@ suspend fun ContentResolver.getMedia( } -@Throws(Exception::class) fun Cursor.getMediaFromCursor(): Media { val id: Long = getLong(getColumnIndexOrThrow(MediaStore.MediaColumns._ID)) @@ -115,25 +114,25 @@ fun Cursor.getMediaFromCursor(): Media { MediaStore.MediaColumns.BUCKET_DISPLAY_NAME ) ) - } catch (_: Exception) { + } catch (_: Throwable) { Build.MODEL } val takenTimestamp: Long? = try { getLong(getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_TAKEN)) - } catch (_: Exception) { + } catch (_: Throwable) { null } val modifiedTimestamp: Long = getLong(getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_MODIFIED)) val duration: String? = try { getString(getColumnIndexOrThrow(MediaStore.MediaColumns.DURATION)) - } catch (_: Exception) { + } catch (_: Throwable) { null } val expiryTimestamp: Long? = try { getLong(getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_EXPIRES)) - } catch (_: Exception) { + } catch (_: Throwable) { null } diff --git a/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/data/utils/MediaQuery.kt b/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/data/utils/MediaQuery.kt index bf65787c2c..149024dd5d 100644 --- a/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/data/utils/MediaQuery.kt +++ b/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/data/utils/MediaQuery.kt @@ -19,23 +19,15 @@ package ru.tech.imageresizershrinker.feature.media_picker.data.utils import android.content.ContentResolver import android.content.ContentUris -import android.content.Context -import android.media.MediaMetadataRetriever -import android.net.Uri import android.os.Build import android.os.Bundle import android.provider.MediaStore -import android.webkit.MimeTypeMap import androidx.annotation.RequiresApi import kotlinx.coroutines.coroutineScope import ru.tech.imageresizershrinker.core.resources.BuildConfig import ru.tech.imageresizershrinker.feature.media_picker.domain.model.Album -import ru.tech.imageresizershrinker.feature.media_picker.domain.model.EXTENDED_DATE_FORMAT -import ru.tech.imageresizershrinker.feature.media_picker.domain.model.Media import ru.tech.imageresizershrinker.feature.media_picker.domain.model.MediaOrder import ru.tech.imageresizershrinker.feature.media_picker.domain.model.OrderType -import java.io.File -import kotlin.random.Random @RequiresApi(26) sealed class Query( @@ -234,7 +226,7 @@ suspend fun ContentResolver.getAlbums( MediaStore.MediaColumns.BUCKET_DISPLAY_NAME ) ) - } catch (_: Exception) { + } catch (_: Throwable) { Build.MODEL } val thumbnailPath = @@ -284,57 +276,4 @@ suspend fun ContentResolver.getAlbums( println("Album parsing took: ${System.currentTimeMillis() - timeStart}ms") } } -} - -fun mediaFromUri( - context: Context, - uri: Uri -): Media? { - if (uri.path == null) return null - val extension = uri.toString().substringAfterLast(".") - var mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension).toString() - var duration: String? = null - try { - val retriever = MediaMetadataRetriever().apply { - setDataSource(context, uri) - } - val hasVideo = - retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO) - val isVideo = "yes" == hasVideo - if (isVideo) { - duration = - retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION) - } - if (mimeType.isEmpty()) { - mimeType = if (isVideo) "video/*" else "image/*" - } - } catch (_: Exception) { - } - var timestamp = 0L - uri.path?.let { File(it) }?.let { - timestamp = try { - it.lastModified() - } catch (_: Exception) { - 0L - } - } - var formattedDate = "" - if (timestamp != 0L) { - formattedDate = timestamp.getDate(EXTENDED_DATE_FORMAT) - } - return Media( - id = Random(System.currentTimeMillis()).nextLong(-1000, 25600000), - label = uri.toString().substringAfterLast("/"), - uri = uri.toString(), - path = uri.path.toString(), - relativePath = uri.path.toString().substringBeforeLast("/"), - albumID = -99L, - albumLabel = "", - timestamp = timestamp, - fullDate = formattedDate, - mimeType = mimeType, - duration = duration, - favorite = 0, - trashed = 0 - ) } \ No newline at end of file diff --git a/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/presentation/components/MediaImagePager.kt b/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/presentation/components/MediaImagePager.kt index 7e71ce7917..b16efbc012 100644 --- a/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/presentation/components/MediaImagePager.kt +++ b/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/presentation/components/MediaImagePager.kt @@ -65,7 +65,6 @@ import androidx.compose.material.icons.filled.Error import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text -import androidx.compose.material3.TopAppBarDefaults import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.derivedStateOf @@ -102,6 +101,7 @@ import ru.tech.imageresizershrinker.core.ui.theme.White import ru.tech.imageresizershrinker.core.ui.theme.takeColorFromScheme import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedIconButton import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBar +import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBarDefaults import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBarType import ru.tech.imageresizershrinker.core.ui.widget.image.Picture import ru.tech.imageresizershrinker.core.ui.widget.modifier.toShape @@ -304,7 +304,7 @@ internal fun MediaImagePager( exit = fadeOut() + slideOutVertically() ) { EnhancedTopAppBar( - colors = TopAppBarDefaults.centerAlignedTopAppBarColors( + colors = EnhancedTopAppBarDefaults.colors( containerColor = MaterialTheme.colorScheme.scrim.copy(alpha = 0.5f) ), type = EnhancedTopAppBarType.Center, @@ -437,7 +437,7 @@ internal fun MediaImagePager( predictiveBackProgress = event.progress } onDismiss() - } catch (_: Exception) { + } catch (_: Throwable) { predictiveBackProgress = 0f } } diff --git a/feature/pdf-tools/src/main/java/ru/tech/imageresizershrinker/feature/pdf_tools/presentation/components/PdfPage.kt b/feature/pdf-tools/src/main/java/ru/tech/imageresizershrinker/feature/pdf_tools/presentation/components/PdfPage.kt index 496a91dc43..9874cd6838 100644 --- a/feature/pdf-tools/src/main/java/ru/tech/imageresizershrinker/feature/pdf_tools/presentation/components/PdfPage.kt +++ b/feature/pdf-tools/src/main/java/ru/tech/imageresizershrinker/feature/pdf_tools/presentation/components/PdfPage.kt @@ -123,7 +123,7 @@ internal fun PdfPage( bitmap = destinationBitmap.asImage() } } - } catch (_: Exception) { + } catch (_: Throwable) { //Just catch and return in case the renderer is being closed return@launch } diff --git a/feature/single-edit/src/main/java/ru/tech/imageresizershrinker/feature/single_edit/presentation/components/FullscreenEditOption.kt b/feature/single-edit/src/main/java/ru/tech/imageresizershrinker/feature/single_edit/presentation/components/FullscreenEditOption.kt index f2bf86a01b..c50a8a917c 100644 --- a/feature/single-edit/src/main/java/ru/tech/imageresizershrinker/feature/single_edit/presentation/components/FullscreenEditOption.kt +++ b/feature/single-edit/src/main/java/ru/tech/imageresizershrinker/feature/single_edit/presentation/components/FullscreenEditOption.kt @@ -331,7 +331,7 @@ fun FullscreenEditOption( predictiveBackProgress = event.progress } internalOnDismiss() - } catch (_: Exception) { + } catch (_: Throwable) { predictiveBackProgress = 0f } }