Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Closes #8122: Do not redirect to market app if app is already installed
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketsroger committed Aug 14, 2020
1 parent d48e98e commit d2aa92b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class AppLinksUseCases(
}

val marketplaceIntent = intent?.`package`?.let {
if (includeInstallAppFallback) {
if (includeInstallAppFallback && !isPackageInstalled(it)) {
Intent.parseUri(MARKET_INTENT_URI_PACKAGE_PREFIX + it, 0)
} else {
null
Expand Down Expand Up @@ -173,6 +173,15 @@ class AppLinksUseCases(

return RedirectData(appIntent, fallbackIntent, marketplaceIntent, resolveInfo)
}

private fun isPackageInstalled(packageName: String): Boolean {
return try {
context.packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.ActivityInfo
import android.content.pm.PackageInfo
import android.content.pm.ResolveInfo
import android.net.Uri
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand Down Expand Up @@ -56,13 +57,19 @@ class AppLinksUseCasesTest {
private val layerActivity = "com.example2.app.intentActivity"
private val mailUrl = "mailto:[email protected]"
private val mailPackage = "com.mail.app"
private val appIntentWithPackageAndFallback =
"intent://com.example.app#Intent;package=com.example.com;S.browser_fallback_url=https://example.com;end"

@Before
fun setup() {
AppLinksUseCases.redirectCache = null
}

private fun createContext(vararg urlToPackages: Triple<String, String, String>, default: Boolean = false): Context {
private fun createContext(
vararg urlToPackages: Triple<String, String, String>,
default: Boolean = false,
installedApps: List<String> = emptyList()
): Context {

val pm = testContext.packageManager
val packageManager = shadowOf(pm)
Expand Down Expand Up @@ -94,6 +101,13 @@ class AppLinksUseCasesTest {
`when`(context.packageName).thenReturn(testBrowserPackage)
}

installedApps.forEach { name ->
val packageInfo = PackageInfo().apply {
packageName = name
}
packageManager.addPackageNoDefaults(packageInfo)
}

return context
}

Expand Down Expand Up @@ -135,6 +149,27 @@ class AppLinksUseCasesTest {
assertTrue(redirect.isRedirect())
}

@Test
fun `A intent that targets a specific package but installed will not uses market intent`() {
val context = createContext(installedApps = listOf("com.example.com"))
val subject = AppLinksUseCases(context, { true })

val redirect = subject.interceptedAppLinkRedirect(appIntentWithPackageAndFallback)
assertFalse(redirect.hasMarketplaceIntent())
assertTrue(redirect.hasFallback())
}

@Test
fun `A intent that targets a specific package but not installed will uses market intent`() {
val context = createContext()
val subject = AppLinksUseCases(context, { true })

val redirect = subject.interceptedAppLinkRedirect(appIntentWithPackageAndFallback)
assertFalse(redirect.hasExternalApp())
assertTrue(redirect.hasMarketplaceIntent())
assertTrue(redirect.hasFallback())
}

@Test
fun `A file is not an app link`() {
val context = createContext(Triple(filePath, appPackage, ""))
Expand Down

0 comments on commit d2aa92b

Please sign in to comment.