11package co.thepeer.sdk.ui.activity
22
3+ import android.annotation.SuppressLint
4+ import android.content.Intent
5+ import android.os.Build
36import android.os.Bundle
7+ import android.webkit.WebView
8+ import android.webkit.WebViewClient
9+ import androidx.activity.OnBackPressedCallback
410import androidx.appcompat.app.AppCompatActivity
11+ import androidx.core.view.isVisible
512import co.thepeer.sdk.databinding.SdkActivityBinding
613import co.thepeer.sdk.model.ThepeerParam
14+ import co.thepeer.sdk.model.ThepeerResult
715import co.thepeer.sdk.ui.fragments.ThepeerFragment
16+ import co.thepeer.sdk.utils.Logger
817import co.thepeer.sdk.utils.ThepeerConstants
18+ import co.thepeer.sdk.utils.Urls
19+ import co.thepeer.sdk.utils.WebInterface
920
1021class ThepeerSDKActivity : AppCompatActivity () {
1122 private var params: ThepeerParam ? = null
@@ -14,21 +25,79 @@ class ThepeerSDKActivity : AppCompatActivity() {
1425 super .onCreate(savedInstanceState)
1526 binding = SdkActivityBinding .inflate(layoutInflater)
1627 setContentView(binding.root)
17- params = intent.getParcelableExtra<ThepeerParam >(ThepeerConstants .THE_PEER_PARAMS )!!
28+ onBackPressedDispatcher.addCallback(this , onBackPressedCallback)
29+
30+ params = if (Build .VERSION .SDK_INT >= 33 ) {
31+ intent.getParcelableExtra(
32+ ThepeerConstants .THE_PEER_PARAMS ,
33+ ThepeerParam ::class .java
34+ )
35+ } else {
36+ intent.getParcelableExtra<ThepeerParam >(ThepeerConstants .THE_PEER_PARAMS )
37+ }
38+
39+
40+ if (params == null ) {
41+ throw IllegalStateException (" Params should not be null. Initialize thePeer using this function Thepeer.Builder(...)" )
42+ }
1843
1944 params?.let {
20- supportFragmentManager.beginTransaction( )
21- .replace(binding.fragmentContainer.id, ThepeerFragment (it) )
22- .commit( )
45+ val url = Urls .createTransactionUrl(it )
46+ setupWebView(url )
47+ Logger .log( this , url )
2348 }
49+
50+
51+
2452 }
2553
26- override fun onBackPressed () {
27- finish()
54+ @SuppressLint(" SetJavaScriptEnabled" )
55+ private fun setupWebView (transactionUrl : String ) {
56+ binding.webViewPeer.apply {
57+ settings.apply {
58+ javaScriptEnabled = true
59+ javaScriptCanOpenWindowsAutomatically = true
60+ domStorageEnabled = true
61+ }
62+ }
63+ binding.webViewPeer.webViewClient = object : WebViewClient () {
64+ override fun onPageFinished (view : WebView , url : String ) {
65+ super .onPageFinished(view, url)
66+ binding.webViewPeer.isVisible = true
67+ }
68+ }
69+
70+
71+ binding.webViewPeer.addJavascriptInterface(
72+ WebInterface { results ->
73+ redirectWithResult(results)
74+ },
75+ " Android"
76+ )
77+
78+ binding.webViewPeer.loadUrl(transactionUrl)
2879 }
2980
3081 override fun onDestroy () {
3182 super .onDestroy()
3283 params = null
84+ binding.webViewPeer.loadUrl(" about:blank" )
85+ binding.webViewPeer.clearHistory()
86+ redirectWithResult(ThepeerResult .Cancelled )
87+ }
88+
89+ private fun redirectWithResult (result : ThepeerResult ) {
90+ val resultData = Intent ()
91+ resultData.putExtra(ThepeerConstants .TRANSACTION_RESULT , result)
92+ setResult(AppCompatActivity .RESULT_OK , resultData)
93+ finish()
3394 }
95+
96+ private val onBackPressedCallback: OnBackPressedCallback =
97+ object : OnBackPressedCallback (true ) {
98+ override fun handleOnBackPressed () {
99+ params = null
100+ }
101+ }
102+
34103}
0 commit comments