Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the correct error message is shown when failing to open a Uri in paywalls #1922

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.revenuecat.purchases.ui.revenuecatui.composables

import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Context
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.compose.foundation.background
Expand All @@ -28,9 +28,7 @@ import androidx.compose.ui.Alignment.Companion.CenterHorizontally
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.platform.UriHandler
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.semantics
Expand All @@ -50,6 +48,7 @@ import com.revenuecat.purchases.ui.revenuecatui.data.processed.TemplateConfigura
import com.revenuecat.purchases.ui.revenuecatui.data.testdata.MockViewModel
import com.revenuecat.purchases.ui.revenuecatui.data.testdata.TestData
import com.revenuecat.purchases.ui.revenuecatui.data.testdata.templates.template2
import com.revenuecat.purchases.ui.revenuecatui.extensions.findActivityOrThrow
import com.revenuecat.purchases.ui.revenuecatui.extensions.openUriOrElse
import com.revenuecat.purchases.ui.revenuecatui.helpers.Logger
import java.net.URL
Expand Down Expand Up @@ -85,8 +84,9 @@ private fun Footer(
childModifier: Modifier = Modifier,
allPlansTapped: (() -> Unit)? = null,
) {
val context = LocalContext.current
val uriHandler = LocalUriHandler.current
// Not using LocalContext, as that is not an Activity context in (Robolectric) tests.
val context = LocalView.current.context
val activity = context.findActivityOrThrow()
val shouldDisplayAllPlansButton = mode == PaywallMode.FOOTER_CONDENSED && allPlansTapped != null
val anyButtonDisplayed = shouldDisplayAllPlansButton ||
configuration.displayRestorePurchases ||
Expand Down Expand Up @@ -147,7 +147,7 @@ private fun Footer(
R.string.terms_and_conditions,
R.string.terms,
) {
openURL(context, uriHandler, it)
openURL(activity, it)
}

if (configuration.privacyURL != null) {
Expand All @@ -162,7 +162,7 @@ private fun Footer(
R.string.privacy_policy,
R.string.privacy,
) {
openURL(context, uriHandler, it)
openURL(activity, it)
}
}
}
Expand Down Expand Up @@ -256,14 +256,14 @@ private object FooterConstants {
fun style(): TextStyle = MaterialTheme.typography.bodySmall
}

private fun openURL(context: Context, uriHandler: UriHandler, url: URL) {
uriHandler.openUriOrElse(url.toString()) { exception ->
private fun openURL(activity: Activity, url: URL) {
activity.openUriOrElse(url.toString()) { exception ->
val msg = if (exception is ActivityNotFoundException) {
context.getString(R.string.no_browser_cannot_open_link)
activity.getString(R.string.no_browser_cannot_open_link)
} else {
context.getString(R.string.cannot_open_link)
activity.getString(R.string.cannot_open_link)
}
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show()
Logger.w(msg)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.revenuecat.purchases.ui.revenuecatui.extensions

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper

/**
* Find the closest Activity in a given Context. Throws an IllegalStateException if this Context is not an Activity
* context.
*/
internal fun Context.findActivityOrThrow(): Activity {
var context = this
while (context is ContextWrapper) {
if (context is Activity) return context
context = context.baseContext
}
error("This Context is not an Activity context.")
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.revenuecat.purchases.ui.revenuecatui.extensions

import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri

/**
* Opens the given [uri] in a browser. If the [uri] is invalid or no browser is installed, [fallbackAction] is called.
*
* This is an extension on Activity, since we need an Activity Context to avoid having to pass the
* FLAG_ACTIVITY_NEW_TASK flag.
*/
@Suppress("SwallowedException")
fun Activity.openUriOrElse(uri: String, fallbackAction: (e: Exception) -> Unit) {
val parsed = try {
Uri.parse(uri)
} catch (e: IllegalArgumentException) {
fallbackAction(e)
return
}

try {
startActivity(Intent(Intent.ACTION_VIEW, parsed))
} catch (e: ActivityNotFoundException) {
fallbackAction(e)
return
}
}