2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-15 18:12:54 +00:00

Bug 1829619 - Fix external ActivityNotFoundException for default browser settings

This commit is contained in:
rahulsainani 2023-04-24 13:33:58 +02:00 committed by mergify[bot]
parent 990886fc6c
commit 3fcc8890b0
2 changed files with 68 additions and 40 deletions

View File

@ -12,6 +12,7 @@ import android.provider.Settings
import android.view.View import android.view.View
import android.view.WindowManager import android.view.WindowManager
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import mozilla.components.concept.base.crash.Breadcrumb import mozilla.components.concept.base.crash.Breadcrumb
@ -88,48 +89,87 @@ fun Activity.openSetDefaultBrowserOption(
REQUEST_CODE_BROWSER_ROLE, REQUEST_CODE_BROWSER_ROLE,
) )
} else { } else {
navigateToDefaultBrowserAppsSettings() navigateToDefaultBrowserAppsSettings(
useCustomTab = useCustomTab,
from = from,
flags = flags,
)
} }
} }
} }
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> { Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> {
navigateToDefaultBrowserAppsSettings() navigateToDefaultBrowserAppsSettings(
useCustomTab = useCustomTab,
from = from,
flags = flags,
)
} }
else -> { else -> {
val sumoDefaultBrowserUrl = SupportUtils.getGenericSumoURLForTopic( openDefaultBrowserSumoPage(useCustomTab, from, flags)
topic = SupportUtils.SumoTopic.SET_AS_DEFAULT_BROWSER,
)
if (useCustomTab) {
startActivity(
SupportUtils.createSandboxCustomTabIntent(
context = this,
url = sumoDefaultBrowserUrl,
),
)
} else {
(this as HomeActivity).openToBrowserAndLoad(
searchTermOrURL = sumoDefaultBrowserUrl,
newTab = true,
from = from,
flags = flags,
)
}
} }
} }
} }
private fun Activity.navigateToDefaultBrowserAppsSettings() { @RequiresApi(Build.VERSION_CODES.N)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { private fun Activity.navigateToDefaultBrowserAppsSettings(
val intent = Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS) from: BrowserDirection,
intent.putExtra( flags: EngineSession.LoadUrlFlags,
SETTINGS_SELECT_OPTION_KEY, useCustomTab: Boolean,
DEFAULT_BROWSER_APP_OPTION, ) {
) val intent = Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS).apply {
intent.putExtra( putExtra(SETTINGS_SELECT_OPTION_KEY, DEFAULT_BROWSER_APP_OPTION)
putExtra(
SETTINGS_SHOW_FRAGMENT_ARGS, SETTINGS_SHOW_FRAGMENT_ARGS,
bundleOf(SETTINGS_SELECT_OPTION_KEY to DEFAULT_BROWSER_APP_OPTION), bundleOf(SETTINGS_SELECT_OPTION_KEY to DEFAULT_BROWSER_APP_OPTION),
) )
}
startExternalActivitySafe(
intent = intent,
onActivityNotPresent = {
openDefaultBrowserSumoPage(useCustomTab = useCustomTab, from = from, flags = flags)
},
)
}
private fun Activity.openDefaultBrowserSumoPage(
useCustomTab: Boolean,
from: BrowserDirection,
flags: EngineSession.LoadUrlFlags,
) {
val sumoDefaultBrowserUrl = SupportUtils.getGenericSumoURLForTopic(
topic = SupportUtils.SumoTopic.SET_AS_DEFAULT_BROWSER,
)
if (useCustomTab) {
startActivity(
SupportUtils.createSandboxCustomTabIntent(
context = this,
url = sumoDefaultBrowserUrl,
),
)
} else {
(this as HomeActivity).openToBrowserAndLoad(
searchTermOrURL = sumoDefaultBrowserUrl,
newTab = true,
from = from,
flags = flags,
)
}
}
/**
* Checks for the presence of an activity before starting it. In case it's not present,
* [onActivityNotPresent] is invoked, preventing ActivityNotFoundException from being thrown.
* This is useful when navigating to external activities like device permission settings,
* notification settings, default app settings, etc.
*
* @param intent The Intent of the activity to resolve and start.
* @param onActivityNotPresent Invoked when the activity to handle the intent is not present.
*/
inline fun Activity.startExternalActivitySafe(intent: Intent, onActivityNotPresent: () -> Unit) {
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent) startActivity(intent)
} else {
onActivityNotPresent()
} }
} }

View File

@ -8,8 +8,6 @@ import android.content.Intent
import android.content.pm.PackageInfo import android.content.pm.PackageInfo
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build.VERSION_CODES.M import android.os.Build.VERSION_CODES.M
import android.os.Build.VERSION_CODES.N
import android.os.Build.VERSION_CODES.P
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.navigation.NavController import androidx.navigation.NavController
import io.mockk.Called import io.mockk.Called
@ -238,16 +236,6 @@ class HomeDeepLinkIntentProcessorTest {
verify { out wasNot Called } verify { out wasNot Called }
} }
@Test
@Config(minSdk = N, maxSdk = P)
fun `process make_default_browser deep link for above API 23`() {
assertTrue(processorHome.process(testIntent("make_default_browser"), navController, out))
verify { activity.startActivity(any()) }
verify { navController wasNot Called }
verify { out wasNot Called }
}
@Test @Test
@Config(maxSdk = M) @Config(maxSdk = M)
fun `process make_default_browser deep link for API 23 and below`() { fun `process make_default_browser deep link for API 23 and below`() {