Rename classes for clarity.
This commit is contained in:
parent
37934eee7e
commit
64cb42af75
4 changed files with 24 additions and 24 deletions
|
|
@ -41,9 +41,14 @@ import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
interface ShareIntentHandler {
|
interface ShareIntentHandler {
|
||||||
|
data class UriToShare(
|
||||||
|
val uri: Uri,
|
||||||
|
val mimeType: String,
|
||||||
|
)
|
||||||
|
|
||||||
suspend fun handleIncomingShareIntent(
|
suspend fun handleIncomingShareIntent(
|
||||||
intent: Intent,
|
intent: Intent,
|
||||||
onFiles: suspend (List<DefaultShareIntentHandler.FileToShare>) -> Boolean,
|
onUris: suspend (List<UriToShare>) -> Boolean,
|
||||||
onPlainText: suspend (String) -> Boolean,
|
onPlainText: suspend (String) -> Boolean,
|
||||||
): Boolean
|
): Boolean
|
||||||
}
|
}
|
||||||
|
|
@ -52,11 +57,6 @@ interface ShareIntentHandler {
|
||||||
class DefaultShareIntentHandler @Inject constructor(
|
class DefaultShareIntentHandler @Inject constructor(
|
||||||
@ApplicationContext private val context: Context,
|
@ApplicationContext private val context: Context,
|
||||||
) : ShareIntentHandler {
|
) : ShareIntentHandler {
|
||||||
data class FileToShare(
|
|
||||||
val uri: Uri,
|
|
||||||
val mimeType: String,
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This methods aims to handle incoming share intents.
|
* This methods aims to handle incoming share intents.
|
||||||
*
|
*
|
||||||
|
|
@ -64,7 +64,7 @@ class DefaultShareIntentHandler @Inject constructor(
|
||||||
*/
|
*/
|
||||||
override suspend fun handleIncomingShareIntent(
|
override suspend fun handleIncomingShareIntent(
|
||||||
intent: Intent,
|
intent: Intent,
|
||||||
onFiles: suspend (List<FileToShare>) -> Boolean,
|
onUris: suspend (List<ShareIntentHandler.UriToShare>) -> Boolean,
|
||||||
onPlainText: suspend (String) -> Boolean,
|
onPlainText: suspend (String) -> Boolean,
|
||||||
): Boolean {
|
): Boolean {
|
||||||
val type = intent.resolveType(context) ?: return false
|
val type = intent.resolveType(context) ?: return false
|
||||||
|
|
@ -78,7 +78,7 @@ class DefaultShareIntentHandler @Inject constructor(
|
||||||
type.isMimeTypeText() ||
|
type.isMimeTypeText() ||
|
||||||
type.isMimeTypeAny() -> {
|
type.isMimeTypeAny() -> {
|
||||||
val files = getIncomingFiles(intent, type)
|
val files = getIncomingFiles(intent, type)
|
||||||
val result = onFiles(files)
|
val result = onUris(files)
|
||||||
revokeUriPermissions(files.map { it.uri })
|
revokeUriPermissions(files.map { it.uri })
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
@ -99,32 +99,32 @@ class DefaultShareIntentHandler @Inject constructor(
|
||||||
* Use this function to retrieve files which are shared from another application or internally
|
* Use this function to retrieve files which are shared from another application or internally
|
||||||
* by using android.intent.action.SEND or android.intent.action.SEND_MULTIPLE actions.
|
* by using android.intent.action.SEND or android.intent.action.SEND_MULTIPLE actions.
|
||||||
*/
|
*/
|
||||||
private fun getIncomingFiles(data: Intent, type: String): List<FileToShare> {
|
private fun getIncomingFiles(intent: Intent, type: String): List<ShareIntentHandler.UriToShare> {
|
||||||
val uriList = mutableListOf<Uri>()
|
val uriList = mutableListOf<Uri>()
|
||||||
if (data.action == Intent.ACTION_SEND) {
|
if (intent.action == Intent.ACTION_SEND) {
|
||||||
data.getParcelableExtraCompat<Uri>(Intent.EXTRA_STREAM)?.let { uriList.add(it) }
|
intent.getParcelableExtraCompat<Uri>(Intent.EXTRA_STREAM)?.let { uriList.add(it) }
|
||||||
} else if (data.action == Intent.ACTION_SEND_MULTIPLE) {
|
} else if (intent.action == Intent.ACTION_SEND_MULTIPLE) {
|
||||||
val extraUriList: List<Uri>? = data.getParcelableArrayListExtraCompat(Intent.EXTRA_STREAM)
|
val extraUriList: List<Uri>? = intent.getParcelableArrayListExtraCompat(Intent.EXTRA_STREAM)
|
||||||
extraUriList?.let { uriList.addAll(it) }
|
extraUriList?.let { uriList.addAll(it) }
|
||||||
}
|
}
|
||||||
val resInfoList: List<ResolveInfo> = context.packageManager.queryIntentActivitiesCompat(data, PackageManager.MATCH_DEFAULT_ONLY)
|
val resInfoList: List<ResolveInfo> = context.packageManager.queryIntentActivitiesCompat(intent, PackageManager.MATCH_DEFAULT_ONLY)
|
||||||
uriList.forEach {
|
uriList.forEach { uri ->
|
||||||
resInfoList.forEach resolve@{ resolveInfo ->
|
resInfoList.forEach resolve@{ resolveInfo ->
|
||||||
val packageName: String = resolveInfo.activityInfo.packageName
|
val packageName: String = resolveInfo.activityInfo.packageName
|
||||||
// Replace implicit intent by an explicit to fix crash on some devices like Xiaomi.
|
// Replace implicit intent by an explicit to fix crash on some devices like Xiaomi.
|
||||||
// see https://juejin.cn/post/7031736325422186510
|
// see https://juejin.cn/post/7031736325422186510
|
||||||
try {
|
try {
|
||||||
context.grantUriPermission(packageName, it, Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.w(e, "Unable to grant Uri permission")
|
Timber.w(e, "Unable to grant Uri permission")
|
||||||
return@resolve
|
return@resolve
|
||||||
}
|
}
|
||||||
data.action = null
|
intent.action = null
|
||||||
data.component = ComponentName(packageName, resolveInfo.activityInfo.name)
|
intent.component = ComponentName(packageName, resolveInfo.activityInfo.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return uriList.map { uri ->
|
return uriList.map { uri ->
|
||||||
FileToShare(
|
ShareIntentHandler.UriToShare(
|
||||||
uri = uri,
|
uri = uri,
|
||||||
mimeType = type
|
mimeType = type
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,7 @@ class SharePresenter @AssistedInject constructor(
|
||||||
suspend {
|
suspend {
|
||||||
val result = shareIntentHandler.handleIncomingShareIntent(
|
val result = shareIntentHandler.handleIncomingShareIntent(
|
||||||
intent,
|
intent,
|
||||||
onFiles = { filesToShare ->
|
onUris = { filesToShare ->
|
||||||
roomIds
|
roomIds
|
||||||
.map { roomId ->
|
.map { roomId ->
|
||||||
val room = matrixClient.getRoom(roomId) ?: return@map false
|
val room = matrixClient.getRoom(roomId) ?: return@map false
|
||||||
|
|
|
||||||
|
|
@ -21,15 +21,15 @@ import android.content.Intent
|
||||||
class FakeShareIntentHandler(
|
class FakeShareIntentHandler(
|
||||||
private val onIncomingShareIntent: suspend (
|
private val onIncomingShareIntent: suspend (
|
||||||
Intent,
|
Intent,
|
||||||
suspend (List<DefaultShareIntentHandler.FileToShare>) -> Boolean,
|
suspend (List<ShareIntentHandler.UriToShare>) -> Boolean,
|
||||||
suspend (String) -> Boolean,
|
suspend (String) -> Boolean,
|
||||||
) -> Boolean = { _, _, _ -> false },
|
) -> Boolean = { _, _, _ -> false },
|
||||||
) : ShareIntentHandler {
|
) : ShareIntentHandler {
|
||||||
override suspend fun handleIncomingShareIntent(
|
override suspend fun handleIncomingShareIntent(
|
||||||
intent: Intent,
|
intent: Intent,
|
||||||
onFiles: suspend (List<DefaultShareIntentHandler.FileToShare>) -> Boolean,
|
onUris: suspend (List<ShareIntentHandler.UriToShare>) -> Boolean,
|
||||||
onPlainText: suspend (String) -> Boolean,
|
onPlainText: suspend (String) -> Boolean,
|
||||||
): Boolean {
|
): Boolean {
|
||||||
return onIncomingShareIntent(intent, onFiles, onPlainText)
|
return onIncomingShareIntent(intent, onUris, onPlainText)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -126,7 +126,7 @@ class SharePresenterTest {
|
||||||
shareIntentHandler = FakeShareIntentHandler { _, onFile, _ ->
|
shareIntentHandler = FakeShareIntentHandler { _, onFile, _ ->
|
||||||
onFile(
|
onFile(
|
||||||
listOf(
|
listOf(
|
||||||
DefaultShareIntentHandler.FileToShare(
|
ShareIntentHandler.UriToShare(
|
||||||
uri = Uri.parse("content://image.jpg"),
|
uri = Uri.parse("content://image.jpg"),
|
||||||
mimeType = MimeTypes.Jpeg,
|
mimeType = MimeTypes.Jpeg,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue