-
Notifications
You must be signed in to change notification settings - Fork 10
2.7.7 #380
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
Merged
2.7.7 #380
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0843c57
Fix LatestPeriodType case-insensitive deserialization for Stripe
anglinb 5eb3d98
Remove invalid test
ianrumac 73e1927
Add local resource support
ianrumac b290fab
Add testing items for assets
ianrumac 2bbc0e9
Merge pull request #371 from superwall/ir/feat/local-asset-manager
ianrumac bc0b8f5
Ensure we fallback to default values, add serial name casing instead …
ianrumac f5aa48a
Merge pull request #379 from superwall/fix/latest-period-type-case-in…
ianrumac a15c74a
Version bump
ianrumac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
superwall/src/main/java/com/superwall/sdk/paywall/view/webview/LocalResourceHandler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| package com.superwall.sdk.paywall.view.webview | ||
|
|
||
| import android.content.Context | ||
| import android.net.Uri | ||
| import android.webkit.MimeTypeMap | ||
| import android.webkit.WebResourceResponse | ||
| import com.superwall.sdk.logger.LogLevel | ||
| import com.superwall.sdk.logger.LogScope | ||
| import com.superwall.sdk.logger.Logger | ||
| import java.io.ByteArrayInputStream | ||
| import java.io.InputStream | ||
|
|
||
| /** | ||
| * Represents a local resource that can be served to paywall WebViews via `swlocal://` URLs. | ||
| */ | ||
| sealed class PaywallResource { | ||
| /** | ||
| * A resource backed by an Android [Uri] (`file://`, `content://`, etc.). | ||
| */ | ||
| data class FromUri( | ||
| val uri: Uri, | ||
| ) : PaywallResource() | ||
|
|
||
| /** | ||
| * A resource backed by an Android resource ID (e.g. `R.raw.hero_video`, `R.drawable.bg`). | ||
| */ | ||
| data class FromResources( | ||
| val resId: Int, | ||
| ) : PaywallResource() | ||
| } | ||
|
|
||
| internal class LocalResourceHandler( | ||
| private val context: Context, | ||
| private val localResources: () -> Map<String, PaywallResource>, | ||
| ) { | ||
| companion object { | ||
| private const val SCHEME = "swlocal" | ||
| private const val DEFAULT_MIME_TYPE = "application/octet-stream" | ||
| } | ||
|
|
||
| fun isLocalResourceUrl(url: Uri): Boolean = url.scheme == SCHEME | ||
|
|
||
| fun handleRequest(url: Uri): WebResourceResponse { | ||
| val resourceId = url.host | ||
| if (resourceId.isNullOrEmpty()) { | ||
| Logger.debug( | ||
| LogLevel.error, | ||
| LogScope.paywallView, | ||
| "swlocal:// URL has no resource ID: $url", | ||
| ) | ||
| return errorResponse(400, "Bad Request", "Missing resource ID in swlocal:// URL") | ||
| } | ||
|
|
||
| val resource = localResources()[resourceId] | ||
| if (resource == null) { | ||
| Logger.debug( | ||
| LogLevel.error, | ||
| LogScope.paywallView, | ||
| "No local resource found for ID: $resourceId", | ||
| ) | ||
| return errorResponse(404, "Not Found", "No local resource mapped for ID: $resourceId") | ||
| } | ||
|
|
||
| return when (resource) { | ||
| is PaywallResource.FromUri -> handleUri(resourceId, resource.uri) | ||
| is PaywallResource.FromResources -> handleAndroidResource(resourceId, resource.resId) | ||
| } | ||
| } | ||
|
|
||
| private fun handleUri( | ||
| resourceId: String, | ||
| uri: Uri, | ||
| ): WebResourceResponse { | ||
| val mimeType = resolveMimeType(uri) | ||
| val inputStream = | ||
| openStreamOrError(resourceId, uri) ?: return errorResponse(500, "Internal Error", "Failed to read resource: $resourceId") | ||
| return successResponse(mimeType, inputStream) | ||
| } | ||
|
|
||
| private fun handleAndroidResource( | ||
| resourceId: String, | ||
| resId: Int, | ||
| ): WebResourceResponse { | ||
| val uri = Uri.parse("android.resource://${context.packageName}/$resId") | ||
| val mimeType = resolveResourceMimeType(resId, uri) | ||
| val inputStream = | ||
| try { | ||
| context.resources.openRawResource(resId) | ||
| } catch (e: Exception) { | ||
| Logger.debug( | ||
| LogLevel.error, | ||
| LogScope.paywallView, | ||
| "Failed to open Android resource '$resourceId' (resId=$resId)", | ||
| error = e, | ||
| ) | ||
| return errorResponse(500, "Internal Error", "Failed to read resource: ${e.message}") | ||
| } | ||
| return successResponse(mimeType, inputStream) | ||
| } | ||
|
|
||
| private fun openStreamOrError( | ||
| resourceId: String, | ||
| uri: Uri, | ||
| ): InputStream? = | ||
| try { | ||
| context.contentResolver.openInputStream(uri) | ||
| ?: throw IllegalStateException("ContentResolver returned null InputStream") | ||
| } catch (e: Exception) { | ||
| Logger.debug( | ||
| LogLevel.error, | ||
| LogScope.paywallView, | ||
| "Failed to open local resource '$resourceId' at $uri", | ||
| error = e, | ||
| ) | ||
| null | ||
| } | ||
|
|
||
| private fun resolveMimeType(uri: Uri): String { | ||
| context.contentResolver.getType(uri)?.let { return it } | ||
| return mimeTypeFromExtension(uri.toString()) | ||
| } | ||
|
|
||
| private fun resolveResourceMimeType( | ||
| resId: Int, | ||
| uri: Uri, | ||
| ): String { | ||
| context.contentResolver.getType(uri)?.let { return it } | ||
|
|
||
| // Try to extract extension from the resource entry name (e.g. "hero_video" won't have one, | ||
| // but the resource type name "raw"/"drawable" gives us a hint) | ||
| try { | ||
| val entryName = context.resources.getResourceEntryName(resId) | ||
| val ext = entryName.substringAfterLast('.', "") | ||
| if (ext.isNotEmpty()) { | ||
| return mimeTypeFromExtension(entryName) | ||
| } | ||
| } catch (_: Exception) { | ||
| // Resource not found - fall through | ||
| } | ||
|
|
||
| return DEFAULT_MIME_TYPE | ||
| } | ||
|
|
||
| private fun mimeTypeFromExtension(path: String): String { | ||
| val extension = MimeTypeMap.getFileExtensionFromUrl(path) | ||
| if (!extension.isNullOrEmpty()) { | ||
| MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)?.let { return it } | ||
| } | ||
| return DEFAULT_MIME_TYPE | ||
| } | ||
|
|
||
| private fun successResponse( | ||
| mimeType: String, | ||
| inputStream: InputStream, | ||
| ): WebResourceResponse = | ||
| WebResourceResponse( | ||
| mimeType, | ||
| null, | ||
| 200, | ||
| "OK", | ||
| corsHeaders(), | ||
| inputStream, | ||
| ) | ||
|
ianrumac marked this conversation as resolved.
|
||
|
|
||
| private fun errorResponse( | ||
| statusCode: Int, | ||
| reasonPhrase: String, | ||
| body: String, | ||
| ): WebResourceResponse = | ||
| WebResourceResponse( | ||
| "text/plain", | ||
| "UTF-8", | ||
| statusCode, | ||
| reasonPhrase, | ||
| corsHeaders(), | ||
| ByteArrayInputStream(body.toByteArray()), | ||
| ) | ||
|
|
||
| private fun corsHeaders(): Map<String, String> = mapOf("Access-Control-Allow-Origin" to "*") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 13 additions & 26 deletions
39
...main/java/com/superwall/sdk/store/abstractions/product/receipt/LatestSubscriptionState.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,25 @@ | ||
| package com.superwall.sdk.store.abstractions.product.receipt | ||
|
|
||
| import kotlinx.serialization.KSerializer | ||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
| import kotlinx.serialization.descriptors.PrimitiveKind | ||
| import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
| import kotlinx.serialization.descriptors.SerialDescriptor | ||
| import kotlinx.serialization.encoding.Decoder | ||
| import kotlinx.serialization.encoding.Encoder | ||
|
|
||
| @Serializable(with = LatestSubscriptionStateSerializer::class) | ||
| @Serializable | ||
| enum class LatestSubscriptionState { | ||
| @SerialName("grace_period") | ||
| GRACE_PERIOD, | ||
|
|
||
| @SerialName("expired") | ||
| EXPIRED, | ||
|
|
||
| @SerialName("subscribed") | ||
| SUBSCRIBED, | ||
| BILLING_RETRY, | ||
| REVOKED, | ||
| UNKNOWN, | ||
| } | ||
|
|
||
| object LatestSubscriptionStateSerializer : KSerializer<LatestSubscriptionState> { | ||
| override val descriptor: SerialDescriptor = | ||
| PrimitiveSerialDescriptor("LatestSubscriptionState", PrimitiveKind.STRING) | ||
| @SerialName("billing_retry") | ||
| BILLING_RETRY, | ||
|
|
||
| override fun serialize( | ||
| encoder: Encoder, | ||
| value: LatestSubscriptionState, | ||
| ) { | ||
| encoder.encodeString(value.name) | ||
| } | ||
| @SerialName("revoked") | ||
| REVOKED, | ||
|
|
||
| override fun deserialize(decoder: Decoder): LatestSubscriptionState { | ||
| val value = decoder.decodeString() | ||
| return LatestSubscriptionState.entries.find { | ||
| it.name.equals(value, ignoreCase = true) | ||
| } ?: LatestSubscriptionState.UNKNOWN | ||
| } | ||
| @SerialName("unknown") | ||
| UNKNOWN, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.