Merge pull request #1350 from davotoula/share-media-to-other-apps
Share images to other apps
This commit is contained in:
@@ -0,0 +1,135 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Vitor Pamplona
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
* this software and associated documentation files (the "Software"), to deal in
|
||||||
|
* the Software without restriction, including without limitation the rights to use,
|
||||||
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||||
|
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
* subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||||
|
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.vitorpamplona.amethyst.ui.components
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
|
import android.util.Log
|
||||||
|
import androidx.core.content.FileProvider
|
||||||
|
import com.vitorpamplona.amethyst.Amethyst
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileInputStream
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
object ShareHelper {
|
||||||
|
private const val TAG = "ShareHelper"
|
||||||
|
private const val DEFAULT_EXTENSION = "jpg"
|
||||||
|
private const val SHARED_FILE_PREFIX = "shared_media"
|
||||||
|
|
||||||
|
// Media type magic numbers
|
||||||
|
private val JPEG_MAGIC = byteArrayOf(0xFF.toByte(), 0xD8.toByte())
|
||||||
|
private val PNG_MAGIC = byteArrayOf(0x89.toByte(), 0x50.toByte(), 0x4E.toByte(), 0x47.toByte())
|
||||||
|
private val WEBP_HEADER_START = "RIFF".toByteArray()
|
||||||
|
private val WEBP_HEADER_END = "WEBP".toByteArray()
|
||||||
|
private val GIF_MAGIC = "GIF8".toByteArray()
|
||||||
|
|
||||||
|
suspend fun getSharableUriFromUrl(
|
||||||
|
context: Context,
|
||||||
|
imageUrl: String,
|
||||||
|
): Pair<Uri, String> =
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
// Safely get snapshot and file
|
||||||
|
Amethyst.instance.diskCache.openSnapshot(imageUrl)?.use { snapshot ->
|
||||||
|
val file = snapshot.data.toFile()
|
||||||
|
|
||||||
|
// Determine file extension and prepare sharable file
|
||||||
|
val fileExtension = getImageExtension(file)
|
||||||
|
val fileCopy = prepareSharableFile(context, file, fileExtension)
|
||||||
|
|
||||||
|
// Return sharable uri
|
||||||
|
return@use Pair(
|
||||||
|
FileProvider.getUriForFile(context, "${context.packageName}.provider", fileCopy),
|
||||||
|
fileExtension,
|
||||||
|
)
|
||||||
|
} ?: throw IOException("Unable to open snapshot for: $imageUrl")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getImageExtension(file: File): String =
|
||||||
|
try {
|
||||||
|
FileInputStream(file).use { inputStream ->
|
||||||
|
val header = ByteArray(12)
|
||||||
|
val bytesRead = inputStream.read(header)
|
||||||
|
|
||||||
|
if (bytesRead < 4) {
|
||||||
|
// If we couldn't read at least 4 bytes, default to jpg
|
||||||
|
return DEFAULT_EXTENSION
|
||||||
|
}
|
||||||
|
|
||||||
|
when {
|
||||||
|
// JPEG: Check first 2 bytes
|
||||||
|
matchesMagicNumbers(header, 0, JPEG_MAGIC) -> "jpg"
|
||||||
|
|
||||||
|
// PNG: Check first 4 bytes
|
||||||
|
matchesMagicNumbers(header, 0, PNG_MAGIC) -> "png"
|
||||||
|
|
||||||
|
// GIF: Check first 4 bytes for "GIF8"
|
||||||
|
matchesMagicNumbers(header, 0, GIF_MAGIC) -> "gif"
|
||||||
|
|
||||||
|
// WEBP: Check "RIFF" (bytes 0-3) and "WEBP" (bytes 8-11)
|
||||||
|
matchesMagicNumbers(header, 0, WEBP_HEADER_START) &&
|
||||||
|
bytesRead >= 12 &&
|
||||||
|
matchesMagicNumbers(header, 8, WEBP_HEADER_END) -> "webp"
|
||||||
|
|
||||||
|
else -> DEFAULT_EXTENSION
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.w(TAG, "Could not determine image type for ${file.name}, defaulting to $DEFAULT_EXTENSION", e)
|
||||||
|
DEFAULT_EXTENSION
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun matchesMagicNumbers(
|
||||||
|
data: ByteArray,
|
||||||
|
offset: Int,
|
||||||
|
magicBytes: ByteArray,
|
||||||
|
): Boolean {
|
||||||
|
if (offset + magicBytes.size > data.size) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i in magicBytes.indices) {
|
||||||
|
if (data[offset + i] != magicBytes[i]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun prepareSharableFile(
|
||||||
|
context: Context,
|
||||||
|
originalFile: File,
|
||||||
|
extension: String,
|
||||||
|
): File {
|
||||||
|
val timestamp = System.currentTimeMillis()
|
||||||
|
val sharableFile = File(context.cacheDir, "${SHARED_FILE_PREFIX}_$timestamp.$extension")
|
||||||
|
|
||||||
|
try {
|
||||||
|
originalFile.copyTo(sharableFile, overwrite = true)
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.e(TAG, "Failed to copy file for sharing", e)
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
|
||||||
|
return sharableFile
|
||||||
|
}
|
||||||
|
}
|
||||||
+49
-1
@@ -20,6 +20,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.vitorpamplona.amethyst.ui.components
|
package com.vitorpamplona.amethyst.ui.components
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
import androidx.compose.animation.AnimatedVisibilityScope
|
import androidx.compose.animation.AnimatedVisibilityScope
|
||||||
@@ -47,6 +49,7 @@ import androidx.compose.runtime.collectAsState
|
|||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
@@ -104,6 +107,7 @@ import kotlinx.collections.immutable.ImmutableList
|
|||||||
import kotlinx.collections.immutable.persistentListOf
|
import kotlinx.collections.immutable.persistentListOf
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
@@ -680,6 +684,7 @@ fun ShareImageAction(
|
|||||||
hash = content.hash,
|
hash = content.hash,
|
||||||
mimeType = content.mimeType,
|
mimeType = content.mimeType,
|
||||||
onDismiss = onDismiss,
|
onDismiss = onDismiss,
|
||||||
|
content = content,
|
||||||
)
|
)
|
||||||
} else if (content is MediaPreloadedContent) {
|
} else if (content is MediaPreloadedContent) {
|
||||||
ShareImageAction(
|
ShareImageAction(
|
||||||
@@ -692,6 +697,7 @@ fun ShareImageAction(
|
|||||||
hash = null,
|
hash = null,
|
||||||
mimeType = content.mimeType,
|
mimeType = content.mimeType,
|
||||||
onDismiss = onDismiss,
|
onDismiss = onDismiss,
|
||||||
|
content = content,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -708,7 +714,10 @@ fun ShareImageAction(
|
|||||||
hash: String?,
|
hash: String?,
|
||||||
mimeType: String?,
|
mimeType: String?,
|
||||||
onDismiss: () -> Unit,
|
onDismiss: () -> Unit,
|
||||||
|
content: BaseMediaContent? = null,
|
||||||
) {
|
) {
|
||||||
|
val scope = rememberCoroutineScope()
|
||||||
|
|
||||||
DropdownMenu(
|
DropdownMenu(
|
||||||
expanded = popupExpanded.value,
|
expanded = popupExpanded.value,
|
||||||
onDismissRequest = onDismiss,
|
onDismissRequest = onDismiss,
|
||||||
@@ -751,10 +760,49 @@ fun ShareImageAction(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
content?.let {
|
||||||
|
if (content is MediaUrlImage) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
videoUri?.let {
|
||||||
|
if (videoUri.isNotEmpty()) {
|
||||||
|
DropdownMenuItem(
|
||||||
|
text = { Text(stringRes(R.string.share_image)) },
|
||||||
|
onClick = {
|
||||||
|
scope.launch { shareImageFile(context, videoUri, mimeType) }
|
||||||
|
onDismiss()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun verifyHash(content: MediaUrlContent): Boolean? {
|
private suspend fun shareImageFile(
|
||||||
|
context: Context,
|
||||||
|
videoUri: String,
|
||||||
|
mimeType: String?,
|
||||||
|
) {
|
||||||
|
// Get sharable URI and file extension
|
||||||
|
val (uri, fileExtension) = ShareHelper.getSharableUriFromUrl(context, videoUri)
|
||||||
|
|
||||||
|
// Determine mime type, use provided or derive from extension
|
||||||
|
val determinedMimeType = mimeType ?: "image/$fileExtension"
|
||||||
|
|
||||||
|
// Create share intent
|
||||||
|
val shareIntent =
|
||||||
|
Intent(Intent.ACTION_SEND).apply {
|
||||||
|
type = determinedMimeType
|
||||||
|
putExtra(Intent.EXTRA_STREAM, uri)
|
||||||
|
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||||
|
}
|
||||||
|
|
||||||
|
context.startActivity(Intent.createChooser(shareIntent, null))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun verifyHash(content: MediaUrlContent): Boolean? {
|
||||||
if (content.hash == null) return null
|
if (content.hash == null) return null
|
||||||
|
|
||||||
Amethyst.instance.diskCache.openSnapshot(content.url)?.use { snapshot ->
|
Amethyst.instance.diskCache.openSnapshot(content.url)?.use { snapshot ->
|
||||||
|
|||||||
@@ -936,4 +936,5 @@
|
|||||||
<string name="torrent_no_apps">Pro otevření a stažení souboru nejsou nainstalovány žádné torrent aplikace.</string>
|
<string name="torrent_no_apps">Pro otevření a stažení souboru nejsou nainstalovány žádné torrent aplikace.</string>
|
||||||
<string name="select_list_to_filter">Vyberte seznam pro filtrování kanálu</string>
|
<string name="select_list_to_filter">Vyberte seznam pro filtrování kanálu</string>
|
||||||
<string name="temporary_account">Odhlásit se na zámek zařízení</string>
|
<string name="temporary_account">Odhlásit se na zámek zařízení</string>
|
||||||
|
<string name="share_image">Sdílet obrázek…</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -941,4 +941,5 @@ anz der Bedingungen ist erforderlich</string>
|
|||||||
<string name="torrent_no_apps">Keine Torrent-Apps installiert, um die Datei zu öffnen und herunterzuladen.</string>
|
<string name="torrent_no_apps">Keine Torrent-Apps installiert, um die Datei zu öffnen und herunterzuladen.</string>
|
||||||
<string name="select_list_to_filter">Liste zum Filtern des Feeds auswählen</string>
|
<string name="select_list_to_filter">Liste zum Filtern des Feeds auswählen</string>
|
||||||
<string name="temporary_account">Beim Sperren des Geräts abmelden</string>
|
<string name="temporary_account">Beim Sperren des Geräts abmelden</string>
|
||||||
|
<string name="share_image">Bild teilen…</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -958,4 +958,5 @@
|
|||||||
<string name="private_message">Mensagem Privada</string>
|
<string name="private_message">Mensagem Privada</string>
|
||||||
<string name="group_relay">Relé de chat</string>
|
<string name="group_relay">Relé de chat</string>
|
||||||
<string name="group_relay_explanation">O relé a qual todos os usuários deste chat se conectam</string>
|
<string name="group_relay_explanation">O relé a qual todos os usuários deste chat se conectam</string>
|
||||||
|
<string name="share_image">Compartilhar imagem…</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -1170,4 +1170,5 @@
|
|||||||
|
|
||||||
<string name="group_relay">Chat Relay</string>
|
<string name="group_relay">Chat Relay</string>
|
||||||
<string name="group_relay_explanation">The relay that all users of this chat connect to</string>
|
<string name="group_relay_explanation">The relay that all users of this chat connect to</string>
|
||||||
|
<string name="share_image">Share image…</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -3,4 +3,6 @@
|
|||||||
<external-path
|
<external-path
|
||||||
name="external_files"
|
name="external_files"
|
||||||
path="." />
|
path="." />
|
||||||
|
|
||||||
|
<cache-path name="cache" path="." />
|
||||||
</paths>
|
</paths>
|
||||||
|
|||||||
Reference in New Issue
Block a user