Skip to content

Commit 21379a0

Browse files
authored
Merge pull request #423 from akitikkx/fix/422-activity-not-found-exception
Fall back to any browser if Chrome is not installed
2 parents 1be1d59 + 69f90ee commit 21379a0

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

app/src/main/java/com/theupnextapp/ui/traktAccount/TraktAccountScreen.kt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ package com.theupnextapp.ui.traktAccount
2424
import android.app.Activity
2525
import android.content.Context
2626
import android.content.Intent
27+
import android.content.pm.PackageManager
28+
import android.widget.Toast
2729
import androidx.browser.customtabs.CustomTabsIntent
2830
import androidx.compose.foundation.ExperimentalFoundationApi
2931
import androidx.compose.foundation.Image
@@ -170,7 +172,7 @@ private fun DisconnectTraktDialog(
170172
}
171173

172174
fun openCustomTab(context: Context) {
173-
val packageName = "com.android.chrome"
175+
val packageName = "com.android.chrome" // This is the specific package causing the issue
174176

175177
val traktUrl =
176178
"https://trakt.tv/oauth/authorize?response_type=code&client_id=" +
@@ -185,12 +187,26 @@ fun openCustomTab(context: Context) {
185187

186188
val customBuilder = builder.build()
187189

188-
if (packageName != null) {
190+
// Check if Chrome is available
191+
val chromePackageInfo = try {
192+
context.packageManager.getPackageInfo(packageName, 0)
193+
} catch (e: PackageManager.NameNotFoundException) {
194+
null
195+
}
196+
197+
if (chromePackageInfo != null) {
198+
// If Chrome is found
189199
customBuilder.intent.setPackage(packageName)
190200
customBuilder.launchUrl(context, traktUrl.toUri())
191201
} else {
202+
// Fallback to any browser if Chrome is not found
192203
val intent = Intent(Intent.ACTION_VIEW, traktUrl.toUri())
193-
activity?.startActivity(intent)
204+
// Ensure there's an activity to handle this intent to prevent another crash
205+
if (intent.resolveActivity(context.packageManager) != null) {
206+
activity?.startActivity(intent)
207+
} else {
208+
Toast.makeText(context, "No browser found", Toast.LENGTH_SHORT).show()
209+
}
194210
}
195211
}
196212

0 commit comments

Comments
 (0)