-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove custom link launcher logic (#965)
- Loading branch information
Showing
3 changed files
with
8 additions
and
39 deletions.
There are no files selected for viewing
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
34 changes: 4 additions & 30 deletions
34
...ome-custom-tabs/src/main/java/com/jraska/github/client/chrome/ChromeCustomTabsLauncher.kt
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
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) } | ||
} | ||
} |
12 changes: 4 additions & 8 deletions
12
...hrome-custom-tabs/src/main/java/com/jraska/github/client/chrome/ChromeCustomTabsModule.kt
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
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 | ||
} |