|
1 | 1 | package com.jraska.github.client.chrome |
2 | 2 |
|
3 | | -import android.content.pm.PackageManager |
4 | 3 | import android.net.Uri |
5 | 4 | import androidx.browser.customtabs.CustomTabsIntent |
6 | 5 | import com.jraska.github.client.WebLinkLauncher |
7 | 6 | import com.jraska.github.client.core.android.TopActivityProvider |
8 | 7 | import okhttp3.HttpUrl |
9 | | -import timber.log.Timber |
| 8 | +import javax.inject.Inject |
10 | 9 |
|
11 | | -private const val CHROME_BROWSER_PACKAGE = "com.android.chrome" |
12 | | - |
13 | | -internal class ChromeCustomTabsLauncher( |
14 | | - private val provider: TopActivityProvider, private val packageManager: PackageManager |
| 10 | +internal class ChromeCustomTabsLauncher @Inject constructor( |
| 11 | + private val provider: TopActivityProvider, |
15 | 12 | ) : WebLinkLauncher { |
16 | 13 | override fun launchOnWeb(url: HttpUrl) { |
17 | 14 | val uri = Uri.parse(url.toString()) |
18 | | - |
19 | | - val customTabsIntent = prepareIntent(uri) |
20 | | - provider.onTopActivity { customTabsIntent.launchUrl(it, uri) } |
21 | | - } |
22 | | - |
23 | | - private fun prepareIntent(uri: Uri): CustomTabsIntent { |
24 | 15 | val customTabsIntent = CustomTabsIntent.Builder().build() |
25 | | - customTabsIntent.intent.data = uri |
26 | | - |
27 | | - val browsersToHandler = packageManager.queryIntentActivities(customTabsIntent.intent, 0) |
28 | | - return when (browsersToHandler.size) { |
29 | | - 0 -> { |
30 | | - Timber.e("No apps found for uri: %s", uri) |
31 | | - customTabsIntent |
32 | | - } |
33 | 16 |
|
34 | | - 1 -> customTabsIntent |
35 | | - else -> { |
36 | | - val chromeAvailable = |
37 | | - null != browsersToHandler.find { it.activityInfo?.packageName == CHROME_BROWSER_PACKAGE } |
38 | | - if (chromeAvailable) { |
39 | | - customTabsIntent.intent.`package` = CHROME_BROWSER_PACKAGE |
40 | | - } |
41 | | - customTabsIntent |
42 | | - } |
43 | | - } |
| 17 | + provider.onTopActivity { customTabsIntent.launchUrl(it, uri) } |
44 | 18 | } |
45 | 19 | } |
0 commit comments