Skip to content

Commit

Permalink
For mozilla-mobile#18608 made set a default browser functionality pub…
Browse files Browse the repository at this point in the history
…licly available.
  • Loading branch information
Amejia481 authored and pkirakosyan committed Aug 8, 2021
1 parent 2b89d2e commit 1fcddbf
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 54 deletions.
64 changes: 64 additions & 0 deletions app/src/main/java/org/mozilla/fenix/ext/Activity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"
59 changes: 5 additions & 54 deletions app/src/main/java/org/mozilla/fenix/settings/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -53,6 +51,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.settings.advanced.getSupportedLocales
Expand Down Expand Up @@ -448,44 +448,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
}
}

Expand All @@ -498,16 +463,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()
}
Expand Down Expand Up @@ -614,12 +569,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"
}
}

0 comments on commit 1fcddbf

Please sign in to comment.