Unverified Commit 7f43dd12 authored by Alex Catarineu's avatar Alex Catarineu Committed by Matthew Finkel
Browse files

Bug 34378: Port external helper app prompting

With the corresponding android-components patch, this allows all `startActivity`
that may open external apps to be replaced by `TorUtils.startActivityPrompt`.
parent 627e13c6
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
@@ -44,6 +44,8 @@ import mozilla.components.browser.state.state.SessionState
import mozilla.components.browser.state.state.WebExtensionState
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.EngineView
import mozilla.components.feature.app.links.RedirectDialogFragment
import mozilla.components.feature.app.links.SimpleRedirectDialogFragment
import mozilla.components.feature.contextmenu.DefaultSelectionActionDelegate
import mozilla.components.feature.privatemode.notification.PrivateNotificationFeature
import mozilla.components.feature.search.BrowserStoreSearchAdapter
@@ -57,6 +59,7 @@ import mozilla.components.support.ktx.kotlin.isUrl
import mozilla.components.support.ktx.kotlin.toNormalizedUrl
import mozilla.components.support.locale.LocaleAwareAppCompatActivity
import mozilla.components.support.utils.SafeIntent
import mozilla.components.support.utils.TorUtils
import mozilla.components.support.utils.toSafeIntent
import mozilla.components.support.webextensions.WebExtensionPopupFeature
import org.mozilla.fenix.GleanMetrics.Metrics
@@ -155,6 +158,8 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {

    private lateinit var navigationToolbar: Toolbar

    private var dialog: RedirectDialogFragment? = null

    final override fun onCreate(savedInstanceState: Bundle?) {
        components.strictMode.attachListenerToDisablePenaltyDeath(supportFragmentManager)

@@ -406,6 +411,26 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
        super.recreate()
    }

    // Copied from mozac AppLinksFeature.kt
    internal fun getOrCreateDialog(): RedirectDialogFragment {
        val existingDialog = dialog
        if (existingDialog != null) {
            return existingDialog
        }

        SimpleRedirectDialogFragment.newInstance().also {
            dialog = it
            return it
        }
    }
    private fun isAlreadyADialogCreated(): Boolean {
        return findPreviousDialogFragment() != null
    }

    private fun findPreviousDialogFragment(): RedirectDialogFragment? {
        return supportFragmentManager.findFragmentByTag(RedirectDialogFragment.FRAGMENT_TAG) as? RedirectDialogFragment
    }

    /**
     * Handles intents received when the activity is open.
     */
@@ -413,6 +438,26 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
        super.onNewIntent(intent)
        intent ?: return

        val startIntent = intent.getParcelableExtra<PendingIntent>(TorUtils.TORBROWSER_START_ACTIVITY_PROMPT)
        if (startIntent != null) {
            if (startIntent.creatorPackage == applicationContext.packageName) {
                val dialog = getOrCreateDialog()
                dialog.onConfirmRedirect = {
                    @Suppress("EmptyCatchBlock")
                    try {
                        startIntent.send()
                    } catch (error: PendingIntent.CanceledException) {
                    }
                }
                dialog.onCancelRedirect = {}

                if (!isAlreadyADialogCreated()) {
                    dialog.showNow(supportFragmentManager, RedirectDialogFragment.FRAGMENT_TAG)
                }
            }
            return
        }

        // Diagnostic breadcrumb for "Display already aquired" crash:
        // https://github.com/mozilla-mobile/android-components/issues/7960
        breadcrumb(
+2 −1
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import mozilla.components.concept.sync.TabData
import mozilla.components.feature.accounts.push.SendTabUseCases
import mozilla.components.feature.share.RecentAppsStorage
import mozilla.components.support.ktx.kotlin.isExtensionUrl
import mozilla.components.support.utils.TorUtils
import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.components.metrics.Event
@@ -101,7 +102,7 @@ class DefaultShareController(

        @Suppress("TooGenericExceptionCaught")
        val result = try {
            context.startActivity(intent)
            TorUtils.startActivityPrompt(context, intent)
            ShareController.Result.SUCCESS
        } catch (e: Exception) {
            when (e) {