Skip to content

Commit

Permalink
Merge pull request #44 from schibsted/handle-no-customtabs
Browse files Browse the repository at this point in the history
Fallback to regular browser if Custom Tabs are not supported.
  • Loading branch information
oscarsilver authored Jul 20, 2022
2 parents c6530ff + 0a7435c commit 91455a4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
5 changes: 5 additions & 0 deletions webflows/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
<activity android:name="com.schibsted.account.webflows.activities.RedirectUriReceiverActivity" />
</application>

<queries>
<intent>
<action android:name="android.support.customtabs.action.CustomTabsService" />
</intent>
</queries>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ class AuthorizationManagementActivity : Activity() {
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean(KEY_AUTHORIZATION_STARTED, authStarted)
outState.putParcelable(KEY_AUTH_INTENT, authIntent)
super.onSaveInstanceState(outState)
}

private fun handleAuthorizationComplete() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.schibsted.account.webflows.client

import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.browser.customtabs.CustomTabsIntent
import androidx.browser.customtabs.CustomTabsService
import com.schibsted.account.webflows.activities.AuthorizationManagementActivity
import com.schibsted.account.webflows.api.HttpError
import com.schibsted.account.webflows.api.SchibstedAccountApi
Expand Down Expand Up @@ -96,10 +96,15 @@ class Client : ClientInterface {
*/
@JvmOverloads
override fun getAuthenticationIntent(context: Context, authRequest: AuthRequest): Intent {
val customTabsIntent = CustomTabsIntent.Builder().build().apply {
intent.data = generateLoginUrl(authRequest)
val loginUrl = generateLoginUrl(authRequest)
val intent: Intent = if (this.isCustomTabsSupported(context)) {
CustomTabsIntent.Builder().build().apply {
intent.data = loginUrl
}.intent
} else {
Intent(Intent.ACTION_VIEW, loginUrl).addCategory(Intent.CATEGORY_BROWSABLE)
}
return AuthorizationManagementActivity.createStartIntent(context, customTabsIntent.intent)
return AuthorizationManagementActivity.createStartIntent(context, intent)
}

/**
Expand All @@ -110,12 +115,12 @@ class Client : ClientInterface {
@JvmOverloads
override fun launchAuth(context: Context, authRequest: AuthRequest) {
val loginUrl = generateLoginUrl(authRequest)
try {
if (this.isCustomTabsSupported(context)) {
CustomTabsIntent.Builder()
.build()
.launchUrl(context, loginUrl)
} catch (e: ActivityNotFoundException) {
val intent = Intent(Intent.ACTION_VIEW, loginUrl)
} else {
val intent = Intent(Intent.ACTION_VIEW, loginUrl).addCategory(Intent.CATEGORY_BROWSABLE)
context.startActivity(intent)
}
}
Expand All @@ -126,6 +131,13 @@ class Client : ClientInterface {
return Uri.parse(loginUrl)
}

private fun isCustomTabsSupported(context: Context): Boolean {
val serviceIntent = Intent(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION)
val resolveInfos = context.packageManager.queryIntentServices(serviceIntent, 0)

return !resolveInfos.isEmpty()
}

/**
* Call this with the intent received via deep link to complete the login flow.
*
Expand Down

0 comments on commit 91455a4

Please sign in to comment.