From 1f399f0fe7ae3ee714e5a3aafb3ba32fd039b3ff Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Wed, 20 Nov 2024 18:07:34 +0530 Subject: [PATCH] Introduced a new way to attech log file in email body without showing addtional apps in suggestion list --- .../kiwixmobile/core/error/ErrorActivity.kt | 76 +++++++++++++++++-- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/error/ErrorActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/error/ErrorActivity.kt index 9a51bbb90..700157942 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/error/ErrorActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/error/ErrorActivity.kt @@ -19,12 +19,13 @@ package org.kiwix.kiwixmobile.core.error import android.annotation.SuppressLint import android.content.Intent -import android.net.Uri +import android.content.pm.ResolveInfo import android.os.Build import android.os.Bundle import android.os.Process import androidx.activity.result.contract.ActivityResultContracts import androidx.core.content.ContextCompat +import androidx.core.content.FileProvider import androidx.lifecycle.lifecycleScope import kotlinx.coroutines.launch import org.kiwix.kiwixmobile.core.CoreApp.Companion.coreComponent @@ -96,10 +97,13 @@ open class ErrorActivity : BaseActivity() { activityKiwixErrorBinding?.reportButton?.setOnClickListener { lifecycleScope.launch { val emailIntent = emailIntent() - val activities = - packageManager.queryIntentActivitiesCompat(emailIntent, ResolveInfoFlagsCompat.EMPTY) - if (activities.isNotEmpty()) { - sendEmailLauncher.launch(Intent.createChooser(emailIntent, "Send email...")) + val activities = getSupportedEmailApps(emailIntent, supportedEmailPackages) + val targetedIntents = createEmailIntents(emailIntent, activities) + if (activities.isNotEmpty() && targetedIntents.isNotEmpty()) { + val chooserIntent = + Intent.createChooser(targetedIntents.removeFirst(), "Send email...") + chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toTypedArray()) + sendEmailLauncher.launch(chooserIntent) } else { toast(getString(R.string.no_email_application_installed)) } @@ -107,6 +111,53 @@ open class ErrorActivity : BaseActivity() { } } + /** + * Get a list of supported email apps. + */ + private fun getSupportedEmailApps( + emailIntent: Intent, + supportedPackages: List + ): List { + return packageManager.queryIntentActivitiesCompat(emailIntent, ResolveInfoFlagsCompat.EMPTY) + .filter { + supportedPackages.any(it.activityInfo.packageName::contains) + } + } + + /** + * Create a list of intents for supported email apps. + */ + private fun createEmailIntents( + emailIntent: Intent, + activities: List + ): MutableList { + return activities.map { resolveInfo -> + Intent(emailIntent).apply { + setPackage(resolveInfo.activityInfo.packageName) + } + }.toMutableList() + } + + // List of supported email apps + private val supportedEmailPackages = listOf( + "com.google.android.gm", // Gmail + "com.zoho.mail", // Zoho Mail + "com.microsoft.office.outlook", // Outlook + "com.yahoo.mobile.client.android.mail",// Yahoo Mail + "me.bluemail.mail", // BlueMail + "ch.protonmail.android", // ProtonMail + "com.fsck.k9", // K-9 Mail + "com.maildroid", // Maildroid + "org.kman.AquaMail", // Aqua Mail + "com.edison.android.mail", // Edison Mail + "com.readdle.spark", // Spark + "com.gmx.mobile.android.mail", // GMX Mail + "com.fastmail", // FastMail + "ru.mail.mailapp", // Mail.ru + "ru.yandex.mail", // Yandex.Mail + "de.tutao.tutanota" // Tutanota + ) + private val sendEmailLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { restartApp() @@ -114,15 +165,24 @@ open class ErrorActivity : BaseActivity() { private suspend fun emailIntent(): Intent { val emailBody = buildBody() - return Intent(Intent.ACTION_SENDTO).apply { - data = Uri.parse("mailto:") + return Intent(Intent.ACTION_SEND).apply { + type = "text/plain" putExtra(Intent.EXTRA_EMAIL, arrayOf(CRASH_AND_FEEDBACK_EMAIL_ADDRESS)) putExtra(Intent.EXTRA_SUBJECT, subject) val file = fileLogger.writeLogFile( this@ErrorActivity, activityKiwixErrorBinding?.allowLogs?.isChecked == true ) - putExtra(Intent.EXTRA_TEXT, "$emailBody\n\nDevice Logs:\n$${file.readText()}") + file.appendText(emailBody) + val path = + FileProvider.getUriForFile( + this@ErrorActivity, + applicationContext.packageName + ".fileprovider", + file + ) + addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION) + putExtra(android.content.Intent.EXTRA_STREAM, path) + putExtra(Intent.EXTRA_TEXT, emailBody) } }