Commit b999f107 authored by Alex Catarineu's avatar Alex Catarineu Committed by Pier Angelo Vendrame
Browse files

TB 34378: [android] Port external helper app prompting

Together with the corresponding fenix patch, this allows all `startActivity`
that may open external apps to be replaced by `TorUtils.startActivityPrompt`.

Originally, android-components#40007 and fenix#34378.
parent 66c94c04
Loading
Loading
Loading
Loading
+1 −1
Changes for mobile/android/android-components/components/feature/app-links/src/main/java/mozilla/components/feature/app/links/AppLinksFeature.kt: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -178,7 +178,7 @@ class AppLinksFeature(
        isPrivate: Boolean,
        isWallet: Boolean,
    ): Boolean {
        val shouldShowPrompt = isPrivate || isWallet || shouldPrompt()
        val shouldShowPrompt = true
        return !shouldShowPrompt
    }

+2 −1
Changes for mobile/android/android-components/components/feature/app-links/src/main/java/mozilla/components/feature/app/links/AppLinksUseCases.kt: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import mozilla.components.support.utils.Browsers
import mozilla.components.support.utils.BrowsersCache
import mozilla.components.support.utils.ext.packageManagerCompatHelper
import java.net.URISyntaxException
import mozilla.components.support.utils.TorUtils

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal const val EXTRA_BROWSER_FALLBACK_URL = "browser_fallback_url"
@@ -260,7 +261,7 @@ class AppLinksUseCases(
                    if (clearTop) {
                        it.flags = it.flags or Intent.FLAG_ACTIVITY_CLEAR_TOP
                    }
                    context.startActivity(it)
                    TorUtils.startActivityPrompt(context, it)
                } catch (e: Exception) {
                    when (e) {
                        is ActivityNotFoundException, is SecurityException, is NullPointerException -> {
+3 −1
Changes for mobile/android/android-components/components/feature/contextmenu/src/main/java/mozilla/components/feature/contextmenu/ContextMenuCandidate.kt: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import mozilla.components.support.ktx.kotlin.takeOrReplace
import mozilla.components.ui.widgets.DefaultSnackbarDelegate
import mozilla.components.ui.widgets.SnackbarDelegate
import kotlin.String
import mozilla.components.support.utils.TorUtils

/**
 * A candidate for an item to be displayed in the context menu.
@@ -508,7 +509,8 @@ data class ContextMenuCandidate(
                }

                try {
                    context.startActivity(
                    TorUtils.startActivityPrompt(
                        context,
                        intent.createChooserExcludingCurrentApp(
                            context,
                            context.getString(R.string.mozac_feature_contextmenu_share_link),
+9 −9
Changes for mobile/android/android-components/components/support/ktx/src/main/java/mozilla/components/support/ktx/android/content/Context.kt: 9 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ import mozilla.components.support.base.log.logger.Logger
import mozilla.components.support.ktx.R
import mozilla.components.support.ktx.android.content.res.resolveAttribute
import mozilla.components.support.utils.ext.packageManagerCompatHelper
import mozilla.components.support.utils.TorUtils
import java.io.File

/**
@@ -120,12 +121,11 @@ fun Context.share(text: String, subject: String = getString(R.string.mozac_suppo
            flags = FLAG_ACTIVITY_NEW_TASK
        }

        startActivity(
            intent.createChooserExcludingCurrentApp(
                this,
                getString(R.string.mozac_support_ktx_menu_share_with),
            ),
        )
        val shareIntent = intent.createChooserExcludingCurrentApp(this, getString(R.string.mozac_support_ktx_menu_share_with)).apply {
            flags = FLAG_ACTIVITY_NEW_TASK
        }

        TorUtils.startActivityPrompt(this, shareIntent)
        true
    } catch (e: ActivityNotFoundException) {
        Log.log(Log.Priority.WARN, message = "No activity to share to found", throwable = e, tag = "share")
@@ -314,7 +314,7 @@ fun Context.email(
            flags = FLAG_ACTIVITY_NEW_TASK
        }

        startActivity(emailIntent)
        TorUtils.startActivityPrompt(this, emailIntent)
        true
    } catch (e: ActivityNotFoundException) {
        Logger.warn("No activity found to handle email intent", throwable = e)
@@ -345,7 +345,7 @@ fun Context.call(
            flags = FLAG_ACTIVITY_NEW_TASK
        }

        startActivity(callIntent)
        TorUtils.startActivityPrompt(this, callIntent)
        true
    } catch (e: ActivityNotFoundException) {
        Logger.warn("No activity found to handle dial intent", throwable = e)
@@ -373,7 +373,7 @@ fun Context.addContact(
            addFlags(FLAG_ACTIVITY_NEW_TASK)
        }

        startActivity(intent)
        TorUtils.startActivityPrompt(this, intent)
        true
    } catch (e: ActivityNotFoundException) {
        Logger.warn("No activity found to handle dial intent", throwable = e)
+23 −1
Changes for mobile/android/android-components/components/support/utils/src/main/java/mozilla/components/support/utils/DefaultDownloadFileUtils.kt: 23 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -31,6 +31,8 @@ import mozilla.components.support.utils.DownloadUtils.truncateFileName
import java.io.File
import java.io.FileNotFoundException

import android.content.pm.PackageManager

/**
 * The default implementation of [DownloadFileUtils].
 *
@@ -61,6 +63,8 @@ class DefaultDownloadFileUtils(
        private const val SCHEME_CONTENT = "content://"
        private const val SCHEME_FILE = "file"
        private const val FILE_PROVIDER_EXTENSION = ".feature.downloads.fileprovider"

        const val EXTRA_IS_PDF = "mozilla.components.feature.downloads.extras.IS_PDF"
    }
    override val currentDownloadLocation: String
        get() = downloadLocation()
@@ -102,7 +106,15 @@ class DefaultDownloadFileUtils(
        )

        return try {
            if (newIntent.getBooleanExtra(
                    EXTRA_IS_PDF,
                    false,
                ) || newIntent.type == INTENT_TYPE_PDF
            ) {
                context.startActivity(newIntent)
            } else {
                TorUtils.startActivityPrompt(context, newIntent)
            }
            true
        } catch (_: ActivityNotFoundException) {
            false
@@ -117,13 +129,23 @@ class DefaultDownloadFileUtils(
        val shareableUri = findShareableDownloadFileUri(fileName, directoryPath)

        return if (shareableUri != null) {
            Intent(Intent.ACTION_VIEW).apply {
            val newIntent = Intent(Intent.ACTION_VIEW).apply {
                setDataAndType(
                    shareableUri,
                    getSafeContentType(fileName, downloadContentType, shareableUri),
                )
                flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
            }

            val isPdf: Boolean = (downloadContentType == "application/pdf" ||
                fileName?.let { File(it).extension } == "pdf")

            if (isPdf) {
                newIntent.setPackage(context.packageName) // Set it to open in TBA
                newIntent.putExtra(EXTRA_IS_PDF, true)
            }

            newIntent
        } else {
            // Fallback to opening the downloads manager if the file URI could not be determined.
            Intent(DownloadManager.ACTION_VIEW_DOWNLOADS).apply {
Loading