From d6be966db36bca3d40611675e0d1a6693870f8d4 Mon Sep 17 00:00:00 2001 From: "Yevhen Babiichuk (DustDFG)" Date: Fri, 9 Jan 2026 19:39:47 +0200 Subject: [PATCH] Replace Illegal{State,Argument} exceptions with more idiomatic kotlin code --- .../main/java/org/schabi/newpipe/about/AboutActivity.kt | 4 ++-- .../org/schabi/newpipe/database/stream/dao/StreamDAO.kt | 2 +- .../newpipe/database/subscription/SubscriptionDAO.kt | 2 +- .../newpipe/player/mediabrowser/MediaBrowserCommon.kt | 4 ++-- .../newpipe/player/mediabrowser/PackageValidator.kt | 8 ++++---- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/about/AboutActivity.kt b/app/src/main/java/org/schabi/newpipe/about/AboutActivity.kt index cfb504200..94bde796b 100644 --- a/app/src/main/java/org/schabi/newpipe/about/AboutActivity.kt +++ b/app/src/main/java/org/schabi/newpipe/about/AboutActivity.kt @@ -92,7 +92,7 @@ class AboutActivity : AppCompatActivity() { return when (position) { posAbout -> AboutFragment() posLicense -> LicenseFragment.newInstance(SOFTWARE_COMPONENTS) - else -> throw IllegalArgumentException("Unknown position for ViewPager2") + else -> error("Unknown position for ViewPager2") } } @@ -105,7 +105,7 @@ class AboutActivity : AppCompatActivity() { return when (position) { posAbout -> R.string.tab_about posLicense -> R.string.tab_licenses - else -> throw IllegalArgumentException("Unknown position for ViewPager2") + else -> error("Unknown position for ViewPager2") } } } diff --git a/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.kt b/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.kt index a6ab2c6cc..86ba262f5 100644 --- a/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.kt +++ b/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.kt @@ -87,7 +87,7 @@ abstract class StreamDAO : BasicDAO { private fun compareAndUpdateStream(newerStream: StreamEntity) { val existentMinimalStream = getMinimalStreamForCompare(newerStream.serviceId, newerStream.url) - ?: throw IllegalStateException("Stream cannot be null just after insertion.") + ?: error("Stream cannot be null just after insertion.") newerStream.uid = existentMinimalStream.uid if (!StreamTypeUtil.isLiveStream(newerStream.streamType)) { diff --git a/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionDAO.kt b/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionDAO.kt index e6fdcbf70..72bdbcf5c 100644 --- a/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionDAO.kt +++ b/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionDAO.kt @@ -100,7 +100,7 @@ abstract class SubscriptionDAO : BasicDAO { entity.uid = uidFromInsert } else { val subscriptionIdFromDb = getSubscriptionIdInternal(entity.serviceId, entity.url!!) - ?: throw IllegalStateException("Subscription cannot be null just after insertion.") + ?: error("Subscription cannot be null just after insertion.") entity.uid = subscriptionIdFromDb update(entity) diff --git a/app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserCommon.kt b/app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserCommon.kt index 12d69a163..fb879bd7c 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserCommon.kt +++ b/app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserCommon.kt @@ -22,7 +22,7 @@ internal fun infoItemTypeToString(type: InfoType): String { InfoType.STREAM -> ID_STREAM InfoType.PLAYLIST -> ID_PLAYLIST InfoType.CHANNEL -> ID_CHANNEL - else -> throw IllegalStateException("Unexpected value: $type") + else -> error("Unexpected value: $type") } } @@ -31,7 +31,7 @@ internal fun infoItemTypeFromString(type: String): InfoType { ID_STREAM -> InfoType.STREAM ID_PLAYLIST -> InfoType.PLAYLIST ID_CHANNEL -> InfoType.CHANNEL - else -> throw IllegalStateException("Unexpected value: $type") + else -> error("Unexpected value: $type") } } diff --git a/app/src/main/java/org/schabi/newpipe/player/mediabrowser/PackageValidator.kt b/app/src/main/java/org/schabi/newpipe/player/mediabrowser/PackageValidator.kt index 05c94e990..ebc70c3b0 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediabrowser/PackageValidator.kt +++ b/app/src/main/java/org/schabi/newpipe/player/mediabrowser/PackageValidator.kt @@ -82,11 +82,11 @@ internal class PackageValidator(context: Context) { // Build the caller info for the rest of the checks here. val callerPackageInfo = buildCallerInfo(callingPackage) - ?: throw IllegalStateException("Caller wasn't found in the system?") + ?: error("Caller wasn't found in the system?") // Verify that things aren't ... broken. (This test should always pass.) - if (callerPackageInfo.uid != callingUid) { - throw IllegalStateException("Caller's package UID doesn't match caller's actual UID?") + check(callerPackageInfo.uid != callingUid) { + "Caller's package UID doesn't match caller's actual UID?" } val callerSignature = callerPackageInfo.signature @@ -202,7 +202,7 @@ internal class PackageValidator(context: Context) { */ private fun getSystemSignature(): String = getPackageInfo(ANDROID_PLATFORM)?.let { platformInfo -> getSignature(platformInfo) - } ?: throw IllegalStateException("Platform signature not found") + } ?: error("Platform signature not found") /** * Creates a SHA-256 signature given a certificate byte array.