Skip to content

Commit f1050cc

Browse files
author
Martin Dinh
committed
Merge branch '205-intercept-fallback-redirect-scheme-in-webview' into 'master'
Resolve "Intercept fallback redirect scheme in WebView" Closes #205 See merge request pace/mobile/android/pace-cloud-sdk!218
2 parents 46494d5 + 788f4f7 commit f1050cc

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ x.y.z Release notes (yyyy-MM-dd)
1313
### Fixes
1414

1515
* Fix a bug where the `additionalParameters` of the `CustomOIDConfiguration` were overwritten by the values from the `PACECloudSDK.additionalQueryParams`
16+
* Fix a bug where the payment process was not possible if the `pace_redirect_scheme` is not specified
1617

1718
### Internal
1819

library/src/main/java/cloud/pace/sdk/appkit/app/deeplink/WebViewActivity.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ class WebViewActivity : AppCompatActivity() {
8080

8181
private fun intercept(newUri: Uri?): Boolean {
8282
// Intercept redirect service deep link
83-
return if (newUri?.scheme == DeviceUtils.getPACERedirectScheme(this)) {
84-
startActivity(Intent(Intent.ACTION_VIEW, newUri))
83+
return if (newUri?.scheme == DeviceUtils.getPACERedirectScheme(this) || newUri?.scheme == FALLBACK_REDIRECT_SCHEME) {
84+
val newIntent = Intent(this, RedirectUriReceiverActivity::class.java)
85+
newIntent.data = newUri
86+
startActivity(newIntent)
8587
true
8688
} else {
8789
false
@@ -102,4 +104,9 @@ class WebViewActivity : AppCompatActivity() {
102104
}
103105
super.onDestroy()
104106
}
107+
108+
companion object {
109+
// Is needed so that the payment process also works for apps that have not set a redirect scheme (e.g. instant apps)
110+
private const val FALLBACK_REDIRECT_SCHEME = "cloudsdk"
111+
}
105112
}

library/src/main/java/cloud/pace/sdk/appkit/app/webview/AppWebView.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ import android.webkit.JavascriptInterface
88
import android.webkit.WebView.setWebContentsDebuggingEnabled
99
import android.widget.RelativeLayout
1010
import androidx.fragment.app.Fragment
11-
import androidx.lifecycle.LifecycleOwner
1211
import androidx.lifecycle.Observer
12+
import androidx.lifecycle.findViewTreeLifecycleOwner
1313
import cloud.pace.sdk.R
1414
import cloud.pace.sdk.appkit.AppKit
1515
import cloud.pace.sdk.appkit.communication.generated.CommunicationManager
16-
import cloud.pace.sdk.appkit.communication.generated.model.request.*
17-
import cloud.pace.sdk.appkit.communication.generated.model.response.*
1816
import cloud.pace.sdk.appkit.utils.BiometricUtils
1917
import cloud.pace.sdk.utils.CloudSDKKoinComponent
2018
import cloud.pace.sdk.utils.Event
@@ -41,6 +39,10 @@ class AppWebView(context: Context, attributeSet: AttributeSet) : RelativeLayout(
4139
loadUrl(url)
4240
}
4341

42+
private val loadUrlObserver = Observer<Event<String>> {
43+
val url = it.getContentIfNotHandled() ?: return@Observer
44+
loadUrl(url)
45+
}
4446
private val isInErrorStateObserver = Observer<Event<Boolean>> {
4547
val isInErrorState = it.getContentIfNotHandled() ?: return@Observer
4648

@@ -140,11 +142,10 @@ class AppWebView(context: Context, attributeSet: AttributeSet) : RelativeLayout(
140142

141143
override fun onAttachedToWindow() {
142144
super.onAttachedToWindow()
143-
val lifecycleOwner = context as? LifecycleOwner
144-
?: throw RuntimeException("lifecycle owner not found ")
145145

146-
// TODO: should this be moved to "onResume" and "onPause" and replaced with "observeForever"?
146+
val lifecycleOwner = findViewTreeLifecycleOwner() ?: throw RuntimeException("lifecycle owner not found ")
147147
webViewModel.init.observe(lifecycleOwner, initObserver)
148+
webViewModel.loadUrl.observe(lifecycleOwner, loadUrlObserver)
148149
webViewModel.isInErrorState.observe(lifecycleOwner, isInErrorStateObserver)
149150
webViewModel.showLoadingIndicator.observe(lifecycleOwner, showLoadingIndicatorObserver)
150151
webViewModel.biometricRequest.observe(lifecycleOwner, biometricRequestObserver)

library/src/main/java/cloud/pace/sdk/appkit/app/webview/AppWebViewModel.kt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ abstract class AppWebViewModel : ViewModel(), AppWebViewClient.WebClientCallback
4040

4141
abstract val currentUrl: MutableLiveData<String?>
4242
abstract val init: LiveData<Event<String>>
43+
abstract val loadUrl: LiveData<Event<String>>
4344
abstract val isInErrorState: LiveData<Event<Boolean>>
4445
abstract val showLoadingIndicator: LiveData<Event<Boolean>>
4546
abstract val biometricRequest: LiveData<Event<BiometricRequest>>
@@ -68,13 +69,14 @@ class AppWebViewModelImpl(
6869

6970
override val currentUrl = MutableLiveData<String?>()
7071
override val init = MutableLiveData<Event<String>>()
72+
override val loadUrl = MutableLiveData<Event<String>>()
7173
override val isInErrorState = MutableLiveData<Event<Boolean>>()
7274
override val showLoadingIndicator = MutableLiveData<Event<Boolean>>()
7375
override val biometricRequest = MutableLiveData<Event<BiometricRequest>>()
7476
override val goBack = MutableLiveData<Event<Unit>>()
7577

7678
override fun init(url: String) {
77-
this.init.value = Event(url)
79+
init.value = Event(url)
7880
}
7981

8082
override fun closeApp() {
@@ -380,18 +382,25 @@ class AppWebViewModelImpl(
380382
OpenURLInNewTabResult(OpenURLInNewTabResult.Failure(OpenURLInNewTabResult.Failure.StatusCode.RequestTimeout, OpenURLInNewTabError("Timeout for openURLInNewTab"))),
381383
OpenURLInNewTabResult(OpenURLInNewTabResult.Failure(OpenURLInNewTabResult.Failure.StatusCode.InternalServerError, OpenURLInNewTabError("An error occurred")))
382384
) {
383-
val redirectScheme = DeviceUtils.getPACERedirectScheme(context)
384-
if (!redirectScheme.isNullOrEmpty()) {
385+
// If integrated is true, do not check if PACE redirect scheme is set but always load the URL in the WebViewActivity
386+
if (openURLInNewTabRequest.integrated == true) {
385387
appModel.openUrlInNewTab(openURLInNewTabRequest)
386388
OpenURLInNewTabResult(OpenURLInNewTabResult.Success())
387389
} else {
388-
appModel.onCustomSchemeError(context, "${redirectScheme}://redirect")
389-
OpenURLInNewTabResult(
390-
OpenURLInNewTabResult.Failure(
391-
OpenURLInNewTabResult.Failure.StatusCode.MethodNotAllowed,
392-
OpenURLInNewTabError("Redirect scheme for deep linking has not been specified.")
390+
val redirectScheme = DeviceUtils.getPACERedirectScheme(context)
391+
if (!redirectScheme.isNullOrEmpty()) {
392+
appModel.openUrlInNewTab(openURLInNewTabRequest)
393+
OpenURLInNewTabResult(OpenURLInNewTabResult.Success())
394+
} else {
395+
appModel.onCustomSchemeError(context, "${redirectScheme}://redirect")
396+
loadUrl.postValue(Event(openURLInNewTabRequest.cancelUrl))
397+
OpenURLInNewTabResult(
398+
OpenURLInNewTabResult.Failure(
399+
OpenURLInNewTabResult.Failure.StatusCode.MethodNotAllowed,
400+
OpenURLInNewTabError("Redirect scheme for deep linking has not been specified.")
401+
)
393402
)
394-
)
403+
}
395404
}
396405
}
397406
}

0 commit comments

Comments
 (0)