Updated on 2026-08-14
This commit is contained in:
parent
2dec303212
commit
db178b206b
7 changed files with 115 additions and 5 deletions
|
|
@ -1,6 +1,8 @@
|
|||
plugins {
|
||||
alias(deps.plugins.android.library)
|
||||
alias(deps.plugins.kotlin.android)
|
||||
alias(deps.plugins.kotlin.kapt)
|
||||
alias(deps.plugins.hilt.android)
|
||||
id("configuration")
|
||||
}
|
||||
|
||||
|
|
@ -9,6 +11,10 @@ android {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
implementation(deps.hilt.android)
|
||||
kapt(deps.hilt.kapt)
|
||||
|
||||
implementation(deps.material)
|
||||
implementation(deps.reKotlin)
|
||||
implementation(deps.timber)
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package com.tangem.core.navigation.di
|
||||
|
||||
import android.content.Context
|
||||
import com.tangem.core.navigation.email.AndroidEmailSender
|
||||
import com.tangem.core.navigation.email.EmailSender
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.components.ActivityComponent
|
||||
import dagger.hilt.android.qualifiers.ActivityContext
|
||||
import dagger.hilt.android.scopes.ActivityScoped
|
||||
|
||||
@Module
|
||||
@InstallIn(ActivityComponent::class)
|
||||
internal object EmailSenderModule {
|
||||
|
||||
@Provides
|
||||
@ActivityScoped
|
||||
fun provideEmailSender(@ActivityContext context: Context): EmailSender {
|
||||
return AndroidEmailSender(context)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package com.tangem.core.navigation.email
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.core.app.ShareCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.content.FileProvider
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
* Implementation of email sender for Android
|
||||
*
|
||||
* @property context context
|
||||
*
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
internal class AndroidEmailSender(private val context: Context) : EmailSender {
|
||||
|
||||
override fun send(email: EmailSender.Email, onFail: ((Exception) -> Unit)?) {
|
||||
val originalIntent = createEmailShareIntent(email)
|
||||
val emailFilterIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
|
||||
|
||||
val packageManager = context.packageManager
|
||||
val originalIntentResults = packageManager.queryIntentActivities(originalIntent, 0)
|
||||
val emailFilterIntentResults = packageManager.queryIntentActivities(emailFilterIntent, 0)
|
||||
|
||||
val targetedIntents = originalIntentResults
|
||||
.filter { originalResult ->
|
||||
emailFilterIntentResults.any {
|
||||
originalResult.activityInfo.packageName == it.activityInfo.packageName
|
||||
}
|
||||
}
|
||||
.map {
|
||||
createEmailShareIntent(email).apply { setPackage(it.activityInfo.packageName) }
|
||||
}
|
||||
.toMutableList()
|
||||
|
||||
try {
|
||||
val chooserIntent = Intent.createChooser(targetedIntents.removeAt(0), "Send mail...").apply {
|
||||
putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toTypedArray())
|
||||
}
|
||||
|
||||
ContextCompat.startActivity(context, chooserIntent, null)
|
||||
} catch (ex: Exception) {
|
||||
Timber.e("Failed to send email: $ex")
|
||||
}
|
||||
}
|
||||
|
||||
private fun createEmailShareIntent(email: EmailSender.Email): Intent {
|
||||
val builder = ShareCompat.IntentBuilder(context)
|
||||
.setType("message/rfc822")
|
||||
.setEmailTo(arrayOf(email.address))
|
||||
.setSubject(email.subject)
|
||||
.setText(email.message)
|
||||
|
||||
email.attachment?.let {
|
||||
builder.setStream(
|
||||
FileProvider.getUriForFile(context, "${context.packageName}.provider", it),
|
||||
)
|
||||
}
|
||||
|
||||
return builder.intent
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package com.tangem.core.navigation.email
|
||||
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* Email sender
|
||||
*
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
interface EmailSender {
|
||||
|
||||
/** Send [email] or handle exception [onFail] */
|
||||
fun send(email: Email, onFail: ((Exception) -> Unit)? = null)
|
||||
|
||||
data class Email(
|
||||
val address: String,
|
||||
val subject: String,
|
||||
val message: String,
|
||||
val attachment: File? = null,
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue