Skip to content

Commit

Permalink
Remove custom link launcher logic (#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
jraska authored Oct 29, 2023
1 parent a6dc4f2 commit 9d5b72d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 39 deletions.
1 change: 0 additions & 1 deletion feature/chrome-custom-tabs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ dependencies {
kapt daggerAnnotationProcessor
implementation 'androidx.browser:browser:1.6.0'
implementation 'androidx.core:core:1.12.0'
implementation 'com.jakewharton.timber:timber:5.0.1'

testImplementation 'junit:junit:4.13.2'
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
package com.jraska.github.client.chrome

import android.content.pm.PackageManager
import android.net.Uri
import androidx.browser.customtabs.CustomTabsIntent
import com.jraska.github.client.WebLinkLauncher
import com.jraska.github.client.core.android.TopActivityProvider
import okhttp3.HttpUrl
import timber.log.Timber
import javax.inject.Inject

private const val CHROME_BROWSER_PACKAGE = "com.android.chrome"

internal class ChromeCustomTabsLauncher(
private val provider: TopActivityProvider, private val packageManager: PackageManager
internal class ChromeCustomTabsLauncher @Inject constructor(
private val provider: TopActivityProvider,
) : WebLinkLauncher {
override fun launchOnWeb(url: HttpUrl) {
val uri = Uri.parse(url.toString())

val customTabsIntent = prepareIntent(uri)
provider.onTopActivity { customTabsIntent.launchUrl(it, uri) }
}

private fun prepareIntent(uri: Uri): CustomTabsIntent {
val customTabsIntent = CustomTabsIntent.Builder().build()
customTabsIntent.intent.data = uri

val browsersToHandler = packageManager.queryIntentActivities(customTabsIntent.intent, 0)
return when (browsersToHandler.size) {
0 -> {
Timber.e("No apps found for uri: %s", uri)
customTabsIntent
}

1 -> customTabsIntent
else -> {
val chromeAvailable =
null != browsersToHandler.find { it.activityInfo?.packageName == CHROME_BROWSER_PACKAGE }
if (chromeAvailable) {
customTabsIntent.intent.`package` = CHROME_BROWSER_PACKAGE
}
customTabsIntent
}
}
provider.onTopActivity { customTabsIntent.launchUrl(it, uri) }
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package com.jraska.github.client.chrome

import android.content.Context
import com.jraska.github.client.WebLinkLauncher
import com.jraska.github.client.core.android.TopActivityProvider
import dagger.Binds
import dagger.Module
import dagger.Provides

@Module
object ChromeCustomTabsModule {
abstract class ChromeCustomTabsModule {

@Provides
fun webLinkLauncher(provider: TopActivityProvider, context: Context): WebLinkLauncher {
return ChromeCustomTabsLauncher(provider, context.packageManager)
}
@Binds
internal abstract fun webLinkLauncher(launcher: ChromeCustomTabsLauncher): WebLinkLauncher
}

0 comments on commit 9d5b72d

Please sign in to comment.