Commit 67e28112 authored by Alex Catarineu's avatar Alex Catarineu
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 7b11da27
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

package org.mozilla.fenix

import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
@@ -45,6 +46,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
@@ -58,6 +61,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
@@ -154,6 +158,8 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {

    private lateinit var navigationToolbar: Toolbar

    private var dialog: RedirectDialogFragment? = null

    final override fun onCreate(savedInstanceState: Bundle?) {
        // Give Orbot the base Context
        Prefs.setContext(applicationContext)
@@ -387,6 +393,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.
     */
@@ -394,6 +420,25 @@ 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 = {
                    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
@@ -25,6 +25,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
@@ -99,7 +100,7 @@ class DefaultShareController(
        }

        val result = try {
            context.startActivity(intent)
            TorUtils.startActivityPrompt(context, intent)
            ShareController.Result.SUCCESS
        } catch (e: SecurityException) {
            snackbar.setText(context.getString(R.string.share_error_snackbar))