For #18608 made set a default browser functionality publicly available.

upstream-sync
Arturo Mejia 3 years ago committed by Mihai Adrian Carare
parent b43a11e9f6
commit be7318f608

@ -8,6 +8,14 @@ import android.app.Activity
import android.view.View
import android.view.WindowManager
import mozilla.components.concept.base.crash.Breadcrumb
import android.app.role.RoleManager
import android.content.Intent
import android.os.Build
import android.provider.Settings
import androidx.core.os.bundleOf
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.settings.SupportUtils
/**
* Attempts to call immersive mode using the View to hide the status bar and navigation buttons.
@ -48,3 +56,59 @@ fun Activity.breadcrumb(
)
)
}
/**
* Opens Android's Manage Default Apps Settings if possible.
*/
fun Activity.openSetDefaultBrowserOption() {
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> {
getSystemService(RoleManager::class.java).also {
if (it.isRoleAvailable(RoleManager.ROLE_BROWSER) && !it.isRoleHeld(
RoleManager.ROLE_BROWSER
)
) {
startActivityForResult(
it.createRequestRoleIntent(RoleManager.ROLE_BROWSER),
REQUEST_CODE_BROWSER_ROLE
)
} else {
navigateToDefaultBrowserAppsSettings()
}
}
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> {
navigateToDefaultBrowserAppsSettings()
}
else -> {
(this as HomeActivity).openToBrowserAndLoad(
searchTermOrURL = SupportUtils.getSumoURLForTopic(
this,
SupportUtils.SumoTopic.SET_AS_DEFAULT_BROWSER
),
newTab = true,
from = BrowserDirection.FromSettings
)
}
}
}
private fun Activity.navigateToDefaultBrowserAppsSettings() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val intent = Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS)
intent.putExtra(
SETTINGS_SELECT_OPTION_KEY,
DEFAULT_BROWSER_APP_OPTION
)
intent.putExtra(
SETTINGS_SHOW_FRAGMENT_ARGS,
bundleOf(SETTINGS_SELECT_OPTION_KEY to DEFAULT_BROWSER_APP_OPTION)
)
startActivity(intent)
}
}
const val REQUEST_CODE_BROWSER_ROLE = 1
const val SETTINGS_SELECT_OPTION_KEY = ":settings:fragment_args_key"
const val SETTINGS_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args"
const val DEFAULT_BROWSER_APP_OPTION = "default_browser"

@ -6,7 +6,6 @@ package org.mozilla.fenix.settings
import android.annotation.SuppressLint
import android.app.Activity
import android.app.role.RoleManager
import android.content.ActivityNotFoundException
import android.content.DialogInterface
import android.content.Intent
@ -19,7 +18,6 @@ import android.view.LayoutInflater
import android.widget.Toast
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AlertDialog
import androidx.core.os.bundleOf
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavDirections
import androidx.navigation.findNavController
@ -51,6 +49,8 @@ import org.mozilla.fenix.ext.metrics
import org.mozilla.fenix.ext.navigateToNotificationsSettings
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.REQUEST_CODE_BROWSER_ROLE
import org.mozilla.fenix.ext.openSetDefaultBrowserOption
import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.settings.account.AccountUiView
import org.mozilla.fenix.utils.Settings
@ -458,44 +458,9 @@ class SettingsFragment : PreferenceFragmentCompat() {
* For <N -> Open sumo page to show user how to change default app.
*/
private fun getClickListenerForMakeDefaultBrowser(): Preference.OnPreferenceClickListener {
return when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> {
Preference.OnPreferenceClickListener {
requireContext().getSystemService(RoleManager::class.java).also {
if (it.isRoleAvailable(RoleManager.ROLE_BROWSER) && !it.isRoleHeld(
RoleManager.ROLE_BROWSER
)
) {
startActivityForResult(
it.createRequestRoleIntent(RoleManager.ROLE_BROWSER),
REQUEST_CODE_BROWSER_ROLE
)
} else {
navigateUserToDefaultAppsSettings()
}
}
true
}
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> {
Preference.OnPreferenceClickListener {
navigateUserToDefaultAppsSettings()
true
}
}
else -> {
Preference.OnPreferenceClickListener {
(activity as HomeActivity).openToBrowserAndLoad(
searchTermOrURL = SupportUtils.getSumoURLForTopic(
requireContext(),
SupportUtils.SumoTopic.SET_AS_DEFAULT_BROWSER
),
newTab = true,
from = BrowserDirection.FromSettings
)
true
}
}
return Preference.OnPreferenceClickListener {
activity?.openSetDefaultBrowserOption()
true
}
}
@ -508,16 +473,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}
private fun navigateUserToDefaultAppsSettings() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val intent = Intent(android.provider.Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS)
intent.putExtra(SETTINGS_SELECT_OPTION_KEY, DEFAULT_BROWSER_APP_OPTION)
intent.putExtra(SETTINGS_SHOW_FRAGMENT_ARGS,
bundleOf(SETTINGS_SELECT_OPTION_KEY to DEFAULT_BROWSER_APP_OPTION))
startActivity(intent)
}
}
private fun updateMakeDefaultBrowserPreference() {
requirePreference<DefaultBrowserPreference>(R.string.pref_key_make_default_browser).updateSwitch()
}
@ -624,12 +579,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
companion object {
private const val REQUEST_CODE_BROWSER_ROLE = 1
private const val SCROLL_INDICATOR_DELAY = 10L
private const val FXA_SYNC_OVERRIDE_EXIT_DELAY = 2000L
private const val AMO_COLLECTION_OVERRIDE_EXIT_DELAY = 3000L
private const val SETTINGS_SELECT_OPTION_KEY = ":settings:fragment_args_key"
private const val SETTINGS_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args"
private const val DEFAULT_BROWSER_APP_OPTION = "default_browser"
}
}

Loading…
Cancel
Save