Android: Share to Voelin
Other apps could not hand anything to Voelin. The activity now takes ACTION_SEND and ACTION_SEND_MULTIPLE of any type. Text and links go into the composer of the current chat, which the phone then shows: one tap sends them, and a mis-tap in the share sheet does not post into a busy channel. Files are copied out of the sending app while its content URIs are readable (on a thread of their own, under the name the app gives, made safe as a path component) and uploaded to the current chat's channel, or the channel we are in, and linked there as the composer's attach button does. The copies live in the cache and are removed at the next start. Two development switches show both platform requests without a phone: VOELIN_OPEN=notification (a tapped voice notification) and VOELIN_OPEN=shared:<text>. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d4b6baaa42
commit
ef976cf31e
9 files changed
+192
-7
No files matched your search
@@ -3,13 +3,20 @@ package io.github.faumaray.voelin
|
||||
import android.app.NativeActivity
|
||||
import android.content.Intent
|
||||
import android.media.projection.MediaProjectionManager
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.provider.OpenableColumns
|
||||
import android.util.Log
|
||||
import java.io.File
|
||||
import java.lang.ref.WeakReference
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
/**
|
||||
* The window: a NativeActivity running `android_main` of libvoelin_android.so
|
||||
* (the Slint UI). Kotlin only handles what needs an Activity: permission
|
||||
* prompts, the screen-capture consent dialog and a tapped notification.
|
||||
* prompts, the screen-capture consent dialog, a tapped notification and
|
||||
* what is shared to the app ("Share to Voelin").
|
||||
*/
|
||||
class MainActivity : NativeActivity() {
|
||||
private var captureFps = 30
|
||||
@@ -42,13 +49,80 @@ class MainActivity : NativeActivity() {
|
||||
moveTaskToBack(true)
|
||||
}
|
||||
|
||||
/** A tapped voice notification. */
|
||||
/** A tapped voice notification, or something shared to us. */
|
||||
private fun handle(intent: Intent?) {
|
||||
when (intent?.action) {
|
||||
VoiceService.ACTION_OPEN_VOICE -> Native.onOpenVoice()
|
||||
Intent.ACTION_SEND, Intent.ACTION_SEND_MULTIPLE -> share(intent)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Text goes into the current chat's composer; files are copied out of the
|
||||
* sending app (its content URIs are readable only now) into our cache,
|
||||
* then uploaded to the current channel by the Rust side.
|
||||
*/
|
||||
private fun share(intent: Intent) {
|
||||
val subject = intent.getStringExtra(Intent.EXTRA_SUBJECT)?.trim().orEmpty()
|
||||
val body = intent.getCharSequenceExtra(Intent.EXTRA_TEXT)?.toString()?.trim().orEmpty()
|
||||
val text = when {
|
||||
subject.isEmpty() -> body
|
||||
body.isEmpty() || body.contains(subject) -> body.ifEmpty { subject }
|
||||
else -> "$subject\n$body"
|
||||
}.ifEmpty { null }
|
||||
val uris = sharedUris(intent)
|
||||
if (uris.isEmpty()) {
|
||||
if (text != null) Native.onShare(text, null)
|
||||
return
|
||||
}
|
||||
val resolver = contentResolver
|
||||
val dir = File(cacheDir, "shared/${System.nanoTime()}")
|
||||
thread(name = "voelin-share") {
|
||||
val paths = uris.mapIndexedNotNull { i, uri ->
|
||||
try {
|
||||
val name = displayName(uri) ?: "shared-file-${i + 1}"
|
||||
// Two shared files may have the same name.
|
||||
val file = File(dir, name).takeUnless { it.exists() } ?: File(dir, "${i + 1}-$name")
|
||||
dir.mkdirs()
|
||||
resolver.openInputStream(uri)?.use { input ->
|
||||
file.outputStream().use { input.copyTo(it) }
|
||||
} ?: return@mapIndexedNotNull null
|
||||
file.path
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "cannot read the shared $uri", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
Native.onShare(text, paths.joinToString("\n").ifEmpty { null })
|
||||
}
|
||||
}
|
||||
|
||||
private fun sharedUris(intent: Intent): List<Uri> = if (intent.action == Intent.ACTION_SEND_MULTIPLE) {
|
||||
if (Build.VERSION.SDK_INT >= 33) {
|
||||
intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM, Uri::class.java)
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM)
|
||||
}.orEmpty()
|
||||
} else {
|
||||
listOfNotNull(
|
||||
if (Build.VERSION.SDK_INT >= 33) {
|
||||
intent.getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java)
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
intent.getParcelableExtra(Intent.EXTRA_STREAM)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/** The file name the sending app gives, made safe as one path component. */
|
||||
private fun displayName(uri: Uri): String? {
|
||||
val name = contentResolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)?.use {
|
||||
if (it.moveToFirst()) it.getString(0) else null
|
||||
} ?: uri.lastPathSegment
|
||||
return name?.replace(Regex("[/\\\\\n\r\u0000]"), "_")?.trim()?.takeIf { it.isNotEmpty() && it != "." && it != ".." }
|
||||
}
|
||||
|
||||
/** Show the system's "start recording or casting?" dialog. */
|
||||
fun requestScreenCapture(fps: Int, maxSize: Int) {
|
||||
captureFps = fps
|
||||
@@ -72,6 +146,7 @@ class MainActivity : NativeActivity() {
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "MainActivity"
|
||||
private const val REQUEST_SCREEN_CAPTURE = 1001
|
||||
|
||||
/** The activity in front, if any. */
|
||||
|
||||
@@ -57,4 +57,12 @@ object Native {
|
||||
/** The voice notification was tapped: show the voice channel. */
|
||||
@JvmStatic
|
||||
external fun onOpenVoice()
|
||||
|
||||
/**
|
||||
* Shared to the app ("Share to Voelin"): text for the current chat's
|
||||
* composer, and copies of the shared files (one path per line) for the
|
||||
* current channel.
|
||||
*/
|
||||
@JvmStatic
|
||||
external fun onShare(text: String?, paths: String?)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.github.faumaray.voelin
|
||||
|
||||
import android.app.Application
|
||||
import java.io.File
|
||||
|
||||
/** The process's application object; the Rust side reaches Android through it. */
|
||||
class VoelinApp : Application() {
|
||||
@@ -8,6 +9,8 @@ class VoelinApp : Application() {
|
||||
super.onCreate()
|
||||
instance = this
|
||||
Notifications.createChannels(this)
|
||||
// Copies of files shared to us (MainActivity.share) from before.
|
||||
File(cacheDir, "shared").deleteRecursively()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
Reference in new issue
Block a user