Created
July 28, 2024 02:51
-
-
Save ericksli/7db6fd8e005c4a29e401b1a16429f509 to your computer and use it in GitHub Desktop.
Custom Tabs warm up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.swiftzer.metroride.app.customtab | |
import android.content.ActivityNotFoundException | |
import android.content.ComponentName | |
import android.content.Context | |
import android.content.Intent | |
import android.content.pm.PackageManager | |
import android.net.Uri | |
import androidx.browser.customtabs.CustomTabsCallback | |
import androidx.browser.customtabs.CustomTabsClient | |
import androidx.browser.customtabs.CustomTabsIntent | |
import androidx.browser.customtabs.CustomTabsServiceConnection | |
import androidx.browser.customtabs.CustomTabsSession | |
import kotlinx.coroutines.channels.awaitClose | |
import kotlinx.coroutines.flow.callbackFlow | |
import timber.log.Timber | |
private const val STABLE_PACKAGE = "com.android.chrome" | |
private const val BETA_PACKAGE = "com.chrome.beta" | |
private const val DEV_PACKAGE = "com.chrome.dev" | |
private const val LOCAL_PACKAGE = "com.google.android.apps.chrome" | |
/** | |
* Goes through all apps that handle VIEW intents and have a warmup service. Picks | |
* the one chosen by the user if there is one, otherwise makes a best effort to return a | |
* valid package name. | |
* | |
* This is **not** threadsafe. | |
* | |
* @param context [Context] to use for accessing [PackageManager]. | |
* @return The package name recommended to use for connecting to custom tabs related components. | |
*/ | |
fun getCustomTabPackageNameToUse(context: Context): String? { | |
// Get default VIEW intent handler. | |
val activityIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://example.com")) | |
val defaultViewHandlerInfo = context.packageManager.resolveActivity(activityIntent, 0) | |
val defaultViewHandlerPackageName = defaultViewHandlerInfo?.activityInfo?.packageName | |
// Get all apps that can handle VIEW intents. | |
val packagesSupportingCustomTabs = | |
context.packageManager.queryIntentActivities(activityIntent, 0) | |
.asSequence() | |
.map { | |
val serviceIntent = Intent().apply { | |
action = "android.support.customtabs.action.CustomTabsService" | |
`package` = it.activityInfo.packageName | |
} | |
if (context.packageManager.resolveService(serviceIntent, 0) != null) { | |
it.activityInfo.packageName | |
} else { | |
"" | |
} | |
} | |
.filter { it.isNotBlank() } | |
.toList() | |
// Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents | |
// and service calls. | |
return when { | |
packagesSupportingCustomTabs.isEmpty() -> null | |
packagesSupportingCustomTabs.size == 1 -> packagesSupportingCustomTabs.first() | |
!defaultViewHandlerPackageName.isNullOrBlank() && | |
!hasSpecializedHandlerIntents(context, activityIntent) && | |
packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName) -> { | |
defaultViewHandlerPackageName | |
} | |
packagesSupportingCustomTabs.contains(STABLE_PACKAGE) -> STABLE_PACKAGE | |
packagesSupportingCustomTabs.contains(BETA_PACKAGE) -> BETA_PACKAGE | |
packagesSupportingCustomTabs.contains(DEV_PACKAGE) -> DEV_PACKAGE | |
packagesSupportingCustomTabs.contains(LOCAL_PACKAGE) -> LOCAL_PACKAGE | |
else -> null | |
} | |
} | |
/** | |
* Used to check whether there is a specialized handler for a given intent. | |
* | |
* @param intent The intent to check with. | |
* @return Whether there is a specialized handler for the given intent. | |
*/ | |
private fun hasSpecializedHandlerIntents(context: Context, intent: Intent): Boolean { | |
try { | |
val handlers = | |
context.packageManager.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER) | |
if (handlers.isEmpty()) return false | |
for (resolveInfo in handlers) { | |
val filter = resolveInfo.filter ?: continue | |
if (filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0) continue | |
if (resolveInfo.activityInfo == null) continue | |
return true | |
} | |
} catch (e: RuntimeException) { | |
Timber.e(e, "hasSpecializedHandlerIntents: runtime error") | |
} | |
return false | |
} | |
fun connectAndBindCustomTabsService(context: Context) = callbackFlow { | |
trySend(null) | |
var isCustomTabsServiceBindingSuccess = false | |
val connection = object : CustomTabsServiceConnection() { | |
override fun onCustomTabsServiceConnected( | |
name: ComponentName, | |
client: CustomTabsClient, | |
) { | |
client.warmup(0) | |
val session = client.newSession(CustomTabsCallback()) | |
trySend(session) | |
} | |
override fun onServiceDisconnected(name: ComponentName?) { | |
// no-op | |
} | |
} | |
val packageName = getCustomTabPackageNameToUse(context) | |
if (packageName != null) { | |
isCustomTabsServiceBindingSuccess = | |
CustomTabsClient.bindCustomTabsService(context, packageName, connection) | |
} | |
awaitClose { | |
if (isCustomTabsServiceBindingSuccess) { | |
context.unbindService(connection) | |
} | |
} | |
} | |
suspend fun launchCustomTab( | |
context: Context, | |
customTabsSession: CustomTabsSession?, | |
uri: Uri, | |
onMissingDefaultBrowser: suspend () -> Unit, | |
) { | |
when { | |
getCustomTabPackageNameToUse(context) != null -> { | |
val customTabsIntent = CustomTabsIntent.Builder(customTabsSession) | |
.setShowTitle(true) | |
.build() | |
customTabsIntent.launchUrl(context, uri) | |
} | |
else -> try { | |
context.startActivity(Intent(Intent.ACTION_VIEW, uri)) | |
} catch (e: ActivityNotFoundException) { | |
onMissingDefaultBrowser() | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
coroutineScope.launch { | |
val customTabSession = connectAndBindCustomTabsService(context) | |
.flowWithLifecycle( | |
lifecycleOwner.lifecycle, | |
Lifecycle.State.STARTED, | |
) | |
.firstOrNull() | |
launchCustomTab( | |
context, | |
customTabSession, | |
"https://support.google.com/googleplay/answer/9037938".toUri(), | |
) { | |
scaffoldState.snackbarHostState.showSnackbar(activityNotFoundText) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment