Updated on 2026-08-14
This commit is contained in:
parent
ad582bfc1a
commit
e79d1c1290
3 changed files with 37 additions and 34 deletions
|
|
@ -1,27 +1,34 @@
|
||||||
package com.tangem.core.navigation.email
|
package com.tangem.tap.core.navigation.email
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.app.ShareCompat
|
import androidx.core.app.ShareCompat
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
|
import com.tangem.core.navigation.email.EmailSender
|
||||||
|
import com.tangem.tap.foregroundActivityObserver
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of email sender for Android
|
* Implementation of email sender for Android
|
||||||
*
|
*
|
||||||
* @property context context
|
|
||||||
*
|
|
||||||
[REDACTED_AUTHOR]
|
[REDACTED_AUTHOR]
|
||||||
*/
|
*/
|
||||||
internal class AndroidEmailSender(private val context: Context) : EmailSender {
|
internal class AndroidEmailSender : EmailSender {
|
||||||
|
|
||||||
override fun send(email: EmailSender.Email, onFail: ((Exception) -> Unit)?) {
|
override fun send(email: EmailSender.Email, onFail: ((Exception) -> Unit)?) {
|
||||||
val originalIntent = createEmailShareIntent(email)
|
val activity = foregroundActivityObserver.foregroundActivity
|
||||||
|
|
||||||
|
if (activity == null) {
|
||||||
|
Timber.e("Foreground activity not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val originalIntent = createEmailShareIntent(activity, email)
|
||||||
val emailFilterIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
|
val emailFilterIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
|
||||||
|
|
||||||
val packageManager = context.packageManager
|
val packageManager = activity.packageManager
|
||||||
val originalIntentResults = packageManager.queryIntentActivities(originalIntent, 0)
|
val originalIntentResults = packageManager.queryIntentActivities(originalIntent, 0)
|
||||||
val emailFilterIntentResults = packageManager.queryIntentActivities(emailFilterIntent, 0)
|
val emailFilterIntentResults = packageManager.queryIntentActivities(emailFilterIntent, 0)
|
||||||
|
|
||||||
|
|
@ -32,7 +39,7 @@ internal class AndroidEmailSender(private val context: Context) : EmailSender {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.map {
|
.map {
|
||||||
createEmailShareIntent(email).apply { setPackage(it.activityInfo.packageName) }
|
createEmailShareIntent(activity, email).apply { setPackage(it.activityInfo.packageName) }
|
||||||
}
|
}
|
||||||
.toMutableList()
|
.toMutableList()
|
||||||
|
|
||||||
|
|
@ -41,14 +48,14 @@ internal class AndroidEmailSender(private val context: Context) : EmailSender {
|
||||||
putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toTypedArray())
|
putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toTypedArray())
|
||||||
}
|
}
|
||||||
|
|
||||||
ContextCompat.startActivity(context, chooserIntent, null)
|
ContextCompat.startActivity(activity, chooserIntent, null)
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
Timber.e("Failed to send email: $ex")
|
Timber.e("Failed to send email: $ex")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createEmailShareIntent(email: EmailSender.Email): Intent {
|
private fun createEmailShareIntent(activity: AppCompatActivity, email: EmailSender.Email): Intent {
|
||||||
val builder = ShareCompat.IntentBuilder(context)
|
val builder = ShareCompat.IntentBuilder(activity)
|
||||||
.setType("message/rfc822")
|
.setType("message/rfc822")
|
||||||
.setEmailTo(arrayOf(email.address))
|
.setEmailTo(arrayOf(email.address))
|
||||||
.setSubject(email.subject)
|
.setSubject(email.subject)
|
||||||
|
|
@ -56,7 +63,7 @@ internal class AndroidEmailSender(private val context: Context) : EmailSender {
|
||||||
|
|
||||||
email.attachment?.let {
|
email.attachment?.let {
|
||||||
builder.setStream(
|
builder.setStream(
|
||||||
FileProvider.getUriForFile(context, "${context.packageName}.provider", it),
|
FileProvider.getUriForFile(activity, "${activity.packageName}.provider", it),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.tangem.tap.di.core.navigation.email
|
||||||
|
|
||||||
|
import com.tangem.core.navigation.email.EmailSender
|
||||||
|
import com.tangem.tap.core.navigation.email.AndroidEmailSender
|
||||||
|
import dagger.Module
|
||||||
|
import dagger.Provides
|
||||||
|
import dagger.hilt.InstallIn
|
||||||
|
import dagger.hilt.components.SingletonComponent
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Module
|
||||||
|
@InstallIn(SingletonComponent::class)
|
||||||
|
internal object EmailSenderModule {
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun provideEmailSender(): EmailSender = AndroidEmailSender()
|
||||||
|
}
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue