Skip to content

Commit 5be5b8a

Browse files
Merge pull request #92 from Web3Auth/feat/customtabs
Feat/customtabs
2 parents 9161753 + 44a3bdd commit 5be5b8a

File tree

6 files changed

+119
-45
lines changed

6 files changed

+119
-45
lines changed

app/src/main/java/com/web3auth/app/MainActivity.kt

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,9 @@ import com.web3auth.core.types.WhiteLabelData
4040
import org.json.JSONObject
4141
import org.web3j.crypto.Credentials
4242
import java.util.concurrent.CompletableFuture
43-
import java.util.concurrent.atomic.AtomicBoolean
4443

4544
class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
4645
private lateinit var web3Auth: Web3Auth
47-
private val isLoginCompleted = AtomicBoolean(false)
48-
private var count = 0
4946

5047
private val verifierList: List<LoginVerifier> = listOf(
5148
LoginVerifier("Google", Provider.GOOGLE),
@@ -125,6 +122,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
125122
val signOutButton = findViewById<Button>(R.id.signOutButton)
126123
val launchWalletButton = findViewById<Button>(R.id.launchWalletButton)
127124
val signMsgButton = findViewById<Button>(R.id.signMsgButton)
125+
val signResultButton = findViewById<Button>(R.id.signResultButton)
128126
val btnSetUpMfa = findViewById<Button>(R.id.btnSetUpMfa)
129127
val spinner = findViewById<TextInputLayout>(R.id.verifierList)
130128
val hintEmailEditText = findViewById<EditText>(R.id.etEmailHint)
@@ -157,6 +155,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
157155
btnSetUpMfa.visibility = View.GONE
158156
launchWalletButton.visibility = View.GONE
159157
signMsgButton.visibility = View.GONE
158+
signResultButton.visibility = View.GONE
160159
spinner.visibility = View.VISIBLE
161160
}
162161
}
@@ -295,23 +294,14 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
295294
override fun onNewIntent(intent: Intent?) {
296295
super.onNewIntent(intent)
297296
web3Auth.setResultUrl(intent?.data)
298-
isLoginCompleted.set(true)
299297
}
300298

301299
override fun onResume() {
302300
super.onResume()
303-
if (isLoginCompleted.get()) {
304-
isLoginCompleted.set(false)
305-
count = 0
306-
} else {
307-
if (count > 0) {
308-
if (Web3Auth.getSignResponse() != null) {
309-
return
310-
}
311-
Toast.makeText(this, "User closed the browser.", Toast.LENGTH_SHORT).show()
312-
web3Auth.setResultUrl(null)
313-
}
314-
count++
301+
if (Web3Auth.getCustomTabsClosed()) {
302+
Toast.makeText(this, "User closed the browser.", Toast.LENGTH_SHORT).show()
303+
web3Auth.setResultUrl(null)
304+
Web3Auth.setCustomTabsClosed(false)
315305
}
316306
}
317307

core/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
android:name="com.web3auth.core.WebViewActivity"
2121
android:theme="@style/Theme.AppCompat" />
2222

23+
<activity
24+
android:name=".CustomChromeTabsActivity"
25+
android:theme="@style/Theme.AppCompat" />
26+
2327
</application>
2428

2529
</manifest>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.web3auth.core
2+
3+
import android.content.Intent
4+
import android.net.Uri
5+
import android.os.Bundle
6+
import androidx.activity.result.ActivityResultLauncher
7+
import androidx.activity.result.contract.ActivityResultContracts
8+
import androidx.appcompat.app.AppCompatActivity
9+
import androidx.browser.customtabs.CustomTabsIntent
10+
import com.web3auth.core.types.WEBVIEW_URL
11+
12+
class CustomChromeTabsActivity : AppCompatActivity() {
13+
14+
private lateinit var customTabLauncher: ActivityResultLauncher<Intent>
15+
16+
override fun onCreate(savedInstanceState: Bundle?) {
17+
super.onCreate(savedInstanceState)
18+
setContentView(R.layout.activity_cct)
19+
20+
customTabLauncher =
21+
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
22+
if (result.resultCode == RESULT_CANCELED) {
23+
Web3Auth.setCustomTabsClosed(true)
24+
finish()
25+
}
26+
}
27+
28+
val extras = intent.extras
29+
if (extras != null) {
30+
val webViewUrl = extras.getString(WEBVIEW_URL)
31+
if (webViewUrl != null) {
32+
launchCustomTabs(webViewUrl)
33+
}
34+
}
35+
}
36+
37+
private fun launchCustomTabs(url: String) {
38+
val defaultBrowser = this.getDefaultBrowser()
39+
val customTabsBrowsers = this.getCustomTabsBrowsers()
40+
if (customTabsBrowsers.contains(defaultBrowser)) {
41+
val intent = CustomTabsIntent.Builder().build().intent
42+
intent.data = Uri.parse(url)
43+
intent.`package` = defaultBrowser
44+
customTabLauncher.launch(intent)
45+
} else if (customTabsBrowsers.isNotEmpty()) {
46+
val intent = CustomTabsIntent.Builder().build().intent
47+
intent.data = Uri.parse(url)
48+
intent.`package` = customTabsBrowsers[0]
49+
customTabLauncher.launch(intent)
50+
} else {
51+
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
52+
}
53+
}
54+
}

core/src/main/java/com/web3auth/core/Web3Auth.kt

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,33 @@ package com.web3auth.core
22

33
import android.content.Intent
44
import android.net.Uri
5-
import androidx.browser.customtabs.CustomTabsIntent
65
import com.google.gson.GsonBuilder
76
import com.google.gson.JsonArray
87
import com.google.gson.JsonObject
98
import com.web3auth.core.api.ApiHelper
109
import com.web3auth.core.api.ApiService
1110
import com.web3auth.core.keystore.KeyStoreManagerUtils
12-
import com.web3auth.core.types.*
11+
import com.web3auth.core.types.ChainConfig
12+
import com.web3auth.core.types.ErrorCode
13+
import com.web3auth.core.types.ExtraLoginOptions
14+
import com.web3auth.core.types.LoginConfigItem
15+
import com.web3auth.core.types.LoginParams
16+
import com.web3auth.core.types.MFALevel
17+
import com.web3auth.core.types.REDIRECT_URL
18+
import com.web3auth.core.types.SessionResponse
19+
import com.web3auth.core.types.SignResponse
20+
import com.web3auth.core.types.UnKnownException
21+
import com.web3auth.core.types.UserCancelledException
22+
import com.web3auth.core.types.UserInfo
23+
import com.web3auth.core.types.WEBVIEW_URL
24+
import com.web3auth.core.types.Web3AuthError
25+
import com.web3auth.core.types.Web3AuthOptions
26+
import com.web3auth.core.types.Web3AuthResponse
1327
import com.web3auth.session_manager_android.SessionManager
1428
import kotlinx.coroutines.GlobalScope
1529
import kotlinx.coroutines.launch
1630
import org.json.JSONObject
17-
import java.util.*
31+
import java.util.Locale
1832
import java.util.concurrent.CompletableFuture
1933

2034
class Web3Auth(web3AuthOptions: Web3AuthOptions) {
@@ -140,21 +154,9 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
140154
Uri.Builder().scheme(sdkUrl.scheme).encodedAuthority(sdkUrl.encodedAuthority)
141155
.encodedPath(sdkUrl.encodedPath).appendPath("start").fragment(hash).build()
142156
//print("url: => $url")
143-
val defaultBrowser = context.getDefaultBrowser()
144-
val customTabsBrowsers = context.getCustomTabsBrowsers()
145-
146-
if (customTabsBrowsers.contains(defaultBrowser)) {
147-
val customTabs = CustomTabsIntent.Builder().build()
148-
customTabs.intent.setPackage(defaultBrowser)
149-
customTabs.launchUrl(context, url)
150-
} else if (customTabsBrowsers.isNotEmpty()) {
151-
val customTabs = CustomTabsIntent.Builder().build()
152-
customTabs.intent.setPackage(customTabsBrowsers[0])
153-
customTabs.launchUrl(context, url)
154-
} else {
155-
// Open in browser externally
156-
context.startActivity(Intent(Intent.ACTION_VIEW, url))
157-
}
157+
val intent = Intent(context, CustomChromeTabsActivity::class.java)
158+
intent.putExtra(WEBVIEW_URL, url.toString())
159+
context.startActivity(intent)
158160
}
159161
}
160162
}
@@ -175,15 +177,19 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
175177
if (ApiHelper.isNetworkAvailable(web3AuthOption.context)) {
176178

177179
//fetch project config
178-
fetchProjectConfig()
179-
180-
this.authorizeSession().whenComplete { resp, error ->
181-
if (error == null) {
182-
web3AuthResponse = resp
180+
fetchProjectConfig().whenComplete { _, err ->
181+
if (err == null) {
182+
this.authorizeSession().whenComplete { resp, error ->
183+
if (error == null) {
184+
web3AuthResponse = resp
185+
initializeCf.complete(null)
186+
} else {
187+
print(error)
188+
}
189+
}
183190
} else {
184-
print(error)
191+
initializeCf.completeExceptionally(err)
185192
}
186-
initializeCf.complete(null)
187193
}
188194
}
189195
return initializeCf
@@ -377,9 +383,8 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
377383
return sessionCompletableFuture
378384
}
379385

380-
private fun fetchProjectConfig() {
381-
val projectConfigCompletableFuture: CompletableFuture<ProjectConfigResponse> =
382-
CompletableFuture()
386+
private fun fetchProjectConfig(): CompletableFuture<Boolean> {
387+
val projectConfigCompletableFuture: CompletableFuture<Boolean> = CompletableFuture()
383388
val web3AuthApi =
384389
ApiHelper.getInstance(web3AuthOption.network.name).create(ApiService::class.java)
385390
GlobalScope.launch {
@@ -400,6 +405,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
400405
web3AuthOption.whiteLabel!!.merge(response.whitelabel)
401406
}
402407
}
408+
projectConfigCompletableFuture.complete(true)
403409
} else {
404410
projectConfigCompletableFuture.completeExceptionally(
405411
Exception(
@@ -420,6 +426,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
420426
)
421427
}
422428
}
429+
return projectConfigCompletableFuture
423430
}
424431

425432
/**
@@ -646,13 +653,22 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
646653
companion object {
647654

648655
private var signResponse: SignResponse? = null
656+
private var isCustomTabsClosed: Boolean = false
649657
fun setSignResponse(_response: SignResponse?) {
650658
signResponse = _response
651659
}
652660

653661
fun getSignResponse(): SignResponse? {
654662
return signResponse
655663
}
664+
665+
fun setCustomTabsClosed(_isCustomTabsClosed: Boolean) {
666+
isCustomTabsClosed = _isCustomTabsClosed
667+
}
668+
669+
fun getCustomTabsClosed(): Boolean {
670+
return isCustomTabsClosed
671+
}
656672
}
657673
}
658674

core/src/main/java/com/web3auth/core/types/Web3AuthOptions.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ fun getWalletSdkUrl(buildEnv: BuildEnv?): String {
5757
const val openLoginVersion = "v8"
5858
const val walletServicesVersion = "v2"
5959
const val WEBVIEW_URL = "walletUrl"
60-
const val REDIRECT_URL = "redirectUrl"
60+
const val REDIRECT_URL = "redirectUrl"
61+
const val CUSTOM_TABS_URL = "customTabsUrl"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
tools:context=".WebViewActivity">
6+
7+
<!-- You can add any views or layout elements here as needed -->
8+
9+
</RelativeLayout>

0 commit comments

Comments
 (0)