Skip to content

Commit

Permalink
Allow sharing vCard from other apps
Browse files Browse the repository at this point in the history
  • Loading branch information
moezbhatti committed Mar 30, 2020
1 parent 057806e commit b56111a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions presentation/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/x-vcard" />
</intent-filter>

<meta-data
android:name="android.service.chooser.chooser_target_service"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ package com.moez.QKSMS.feature.compose

import android.content.Intent
import android.net.Uri
import androidx.core.net.toFile
import androidx.lifecycle.ViewModel
import com.google.android.mms.ContentType
import com.moez.QKSMS.injection.ViewModelKey
import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.model.Attachments
import dagger.Module
import dagger.Provides
import dagger.multibindings.IntoMap
import java.net.URLDecoder
import java.nio.charset.Charset
import javax.inject.Named

@Module
Expand Down Expand Up @@ -73,10 +76,25 @@ class ComposeActivityModule {
@Provides
@Named("attachments")
fun provideSharedAttachments(activity: ComposeActivity): Attachments {
val sharedImages = mutableListOf<Uri>()
activity.intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)?.run(sharedImages::add)
activity.intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)?.run(sharedImages::addAll)
return Attachments(sharedImages.map { Attachment.Image(it) })
val uris = mutableListOf<Uri>()
activity.intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)?.run(uris::add)
activity.intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)?.run(uris::addAll)
return Attachments(uris.mapNotNull { uri ->
val mimeType = activity.contentResolver.getType(uri)
when {
ContentType.isImageType(mimeType) -> {
Attachment.Image(uri)
}

ContentType.TEXT_VCARD.equals(mimeType, true) -> {
val inputStream = activity.contentResolver.openInputStream(uri)
val text = inputStream?.reader(Charset.forName("utf-8"))?.readText()
text?.let(Attachment::Contact)
}

else -> null
}
})
}

@Provides
Expand All @@ -88,7 +106,7 @@ class ComposeActivityModule {
private fun Intent.decodedDataString(): String? {
val data = data?.toString()
if (data?.contains('%') == true) {
return URLDecoder.decode(data, "UTF-8")
return URLDecoder.decode(data, "UTF-8")
}
return data
}
Expand Down

0 comments on commit b56111a

Please sign in to comment.