Skip to content

Commit

Permalink
Merge pull request #92 from Web3Auth/feat/customtabs
Browse files Browse the repository at this point in the history
Feat/customtabs
  • Loading branch information
chaitanyapotti authored Jun 25, 2024
2 parents 9161753 + 44a3bdd commit 5be5b8a
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 45 deletions.
22 changes: 6 additions & 16 deletions app/src/main/java/com/web3auth/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@ import com.web3auth.core.types.WhiteLabelData
import org.json.JSONObject
import org.web3j.crypto.Credentials
import java.util.concurrent.CompletableFuture
import java.util.concurrent.atomic.AtomicBoolean

class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
private lateinit var web3Auth: Web3Auth
private val isLoginCompleted = AtomicBoolean(false)
private var count = 0

private val verifierList: List<LoginVerifier> = listOf(
LoginVerifier("Google", Provider.GOOGLE),
Expand Down Expand Up @@ -125,6 +122,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
val signOutButton = findViewById<Button>(R.id.signOutButton)
val launchWalletButton = findViewById<Button>(R.id.launchWalletButton)
val signMsgButton = findViewById<Button>(R.id.signMsgButton)
val signResultButton = findViewById<Button>(R.id.signResultButton)
val btnSetUpMfa = findViewById<Button>(R.id.btnSetUpMfa)
val spinner = findViewById<TextInputLayout>(R.id.verifierList)
val hintEmailEditText = findViewById<EditText>(R.id.etEmailHint)
Expand Down Expand Up @@ -157,6 +155,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
btnSetUpMfa.visibility = View.GONE
launchWalletButton.visibility = View.GONE
signMsgButton.visibility = View.GONE
signResultButton.visibility = View.GONE
spinner.visibility = View.VISIBLE
}
}
Expand Down Expand Up @@ -295,23 +294,14 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
web3Auth.setResultUrl(intent?.data)
isLoginCompleted.set(true)
}

override fun onResume() {
super.onResume()
if (isLoginCompleted.get()) {
isLoginCompleted.set(false)
count = 0
} else {
if (count > 0) {
if (Web3Auth.getSignResponse() != null) {
return
}
Toast.makeText(this, "User closed the browser.", Toast.LENGTH_SHORT).show()
web3Auth.setResultUrl(null)
}
count++
if (Web3Auth.getCustomTabsClosed()) {
Toast.makeText(this, "User closed the browser.", Toast.LENGTH_SHORT).show()
web3Auth.setResultUrl(null)
Web3Auth.setCustomTabsClosed(false)
}
}

Expand Down
4 changes: 4 additions & 0 deletions core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
android:name="com.web3auth.core.WebViewActivity"
android:theme="@style/Theme.AppCompat" />

<activity
android:name=".CustomChromeTabsActivity"
android:theme="@style/Theme.AppCompat" />

</application>

</manifest>
54 changes: 54 additions & 0 deletions core/src/main/java/com/web3auth/core/CustomChromeTabsActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.web3auth.core

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.browser.customtabs.CustomTabsIntent
import com.web3auth.core.types.WEBVIEW_URL

class CustomChromeTabsActivity : AppCompatActivity() {

private lateinit var customTabLauncher: ActivityResultLauncher<Intent>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cct)

customTabLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_CANCELED) {
Web3Auth.setCustomTabsClosed(true)
finish()
}
}

val extras = intent.extras
if (extras != null) {
val webViewUrl = extras.getString(WEBVIEW_URL)
if (webViewUrl != null) {
launchCustomTabs(webViewUrl)
}
}
}

private fun launchCustomTabs(url: String) {
val defaultBrowser = this.getDefaultBrowser()
val customTabsBrowsers = this.getCustomTabsBrowsers()
if (customTabsBrowsers.contains(defaultBrowser)) {
val intent = CustomTabsIntent.Builder().build().intent
intent.data = Uri.parse(url)
intent.`package` = defaultBrowser
customTabLauncher.launch(intent)
} else if (customTabsBrowsers.isNotEmpty()) {
val intent = CustomTabsIntent.Builder().build().intent
intent.data = Uri.parse(url)
intent.`package` = customTabsBrowsers[0]
customTabLauncher.launch(intent)
} else {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
}
}
}
72 changes: 44 additions & 28 deletions core/src/main/java/com/web3auth/core/Web3Auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@ package com.web3auth.core

import android.content.Intent
import android.net.Uri
import androidx.browser.customtabs.CustomTabsIntent
import com.google.gson.GsonBuilder
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.web3auth.core.api.ApiHelper
import com.web3auth.core.api.ApiService
import com.web3auth.core.keystore.KeyStoreManagerUtils
import com.web3auth.core.types.*
import com.web3auth.core.types.ChainConfig
import com.web3auth.core.types.ErrorCode
import com.web3auth.core.types.ExtraLoginOptions
import com.web3auth.core.types.LoginConfigItem
import com.web3auth.core.types.LoginParams
import com.web3auth.core.types.MFALevel
import com.web3auth.core.types.REDIRECT_URL
import com.web3auth.core.types.SessionResponse
import com.web3auth.core.types.SignResponse
import com.web3auth.core.types.UnKnownException
import com.web3auth.core.types.UserCancelledException
import com.web3auth.core.types.UserInfo
import com.web3auth.core.types.WEBVIEW_URL
import com.web3auth.core.types.Web3AuthError
import com.web3auth.core.types.Web3AuthOptions
import com.web3auth.core.types.Web3AuthResponse
import com.web3auth.session_manager_android.SessionManager
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.util.*
import java.util.Locale
import java.util.concurrent.CompletableFuture

class Web3Auth(web3AuthOptions: Web3AuthOptions) {
Expand Down Expand Up @@ -140,21 +154,9 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
Uri.Builder().scheme(sdkUrl.scheme).encodedAuthority(sdkUrl.encodedAuthority)
.encodedPath(sdkUrl.encodedPath).appendPath("start").fragment(hash).build()
//print("url: => $url")
val defaultBrowser = context.getDefaultBrowser()
val customTabsBrowsers = context.getCustomTabsBrowsers()

if (customTabsBrowsers.contains(defaultBrowser)) {
val customTabs = CustomTabsIntent.Builder().build()
customTabs.intent.setPackage(defaultBrowser)
customTabs.launchUrl(context, url)
} else if (customTabsBrowsers.isNotEmpty()) {
val customTabs = CustomTabsIntent.Builder().build()
customTabs.intent.setPackage(customTabsBrowsers[0])
customTabs.launchUrl(context, url)
} else {
// Open in browser externally
context.startActivity(Intent(Intent.ACTION_VIEW, url))
}
val intent = Intent(context, CustomChromeTabsActivity::class.java)
intent.putExtra(WEBVIEW_URL, url.toString())
context.startActivity(intent)
}
}
}
Expand All @@ -175,15 +177,19 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
if (ApiHelper.isNetworkAvailable(web3AuthOption.context)) {

//fetch project config
fetchProjectConfig()

this.authorizeSession().whenComplete { resp, error ->
if (error == null) {
web3AuthResponse = resp
fetchProjectConfig().whenComplete { _, err ->
if (err == null) {
this.authorizeSession().whenComplete { resp, error ->
if (error == null) {
web3AuthResponse = resp
initializeCf.complete(null)
} else {
print(error)
}
}
} else {
print(error)
initializeCf.completeExceptionally(err)
}
initializeCf.complete(null)
}
}
return initializeCf
Expand Down Expand Up @@ -377,9 +383,8 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
return sessionCompletableFuture
}

private fun fetchProjectConfig() {
val projectConfigCompletableFuture: CompletableFuture<ProjectConfigResponse> =
CompletableFuture()
private fun fetchProjectConfig(): CompletableFuture<Boolean> {
val projectConfigCompletableFuture: CompletableFuture<Boolean> = CompletableFuture()
val web3AuthApi =
ApiHelper.getInstance(web3AuthOption.network.name).create(ApiService::class.java)
GlobalScope.launch {
Expand All @@ -400,6 +405,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
web3AuthOption.whiteLabel!!.merge(response.whitelabel)
}
}
projectConfigCompletableFuture.complete(true)
} else {
projectConfigCompletableFuture.completeExceptionally(
Exception(
Expand All @@ -420,6 +426,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
)
}
}
return projectConfigCompletableFuture
}

/**
Expand Down Expand Up @@ -646,13 +653,22 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
companion object {

private var signResponse: SignResponse? = null
private var isCustomTabsClosed: Boolean = false
fun setSignResponse(_response: SignResponse?) {
signResponse = _response
}

fun getSignResponse(): SignResponse? {
return signResponse
}

fun setCustomTabsClosed(_isCustomTabsClosed: Boolean) {
isCustomTabsClosed = _isCustomTabsClosed
}

fun getCustomTabsClosed(): Boolean {
return isCustomTabsClosed
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ fun getWalletSdkUrl(buildEnv: BuildEnv?): String {
const val openLoginVersion = "v8"
const val walletServicesVersion = "v2"
const val WEBVIEW_URL = "walletUrl"
const val REDIRECT_URL = "redirectUrl"
const val REDIRECT_URL = "redirectUrl"
const val CUSTOM_TABS_URL = "customTabsUrl"
9 changes: 9 additions & 0 deletions core/src/main/res/layout/activity_cct.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WebViewActivity">

<!-- You can add any views or layout elements here as needed -->

</RelativeLayout>

0 comments on commit 5be5b8a

Please sign in to comment.