mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-26 03:46:25 -08:00
fix(android): stage inbound shares to app data dir
This commit is contained in:
2 files changed
+111
No files matched your search
@@ -42,6 +42,24 @@
|
||||
<!-- AndroidTV support -->
|
||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
<!-- Inbound share targets — receive text and images from other apps.
|
||||
Mirrors the iOS share extension activation rules. -->
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.SEND" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<data android:mimeType="text/plain" />
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.SEND" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<data android:mimeType="image/*" />
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.SEND_MULTIPLE" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<data android:mimeType="image/*" />
|
||||
</intent-filter>
|
||||
<!-- DEEP LINK PLUGIN. AUTO-GENERATED. DO NOT REMOVE. -->
|
||||
<intent-filter >
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
package moe.sable.client
|
||||
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.provider.OpenableColumns
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.core.content.IntentCompat
|
||||
import androidx.core.view.WindowCompat
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.util.UUID
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
|
||||
class MainActivity : TauriActivity() {
|
||||
private external fun nativeInitStatusBar()
|
||||
@@ -13,6 +22,90 @@ class MainActivity : TauriActivity() {
|
||||
super.onCreate(savedInstanceState)
|
||||
instance = this
|
||||
runCatching { nativeInitStatusBar() }
|
||||
stageShareIntent(intent)
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
stageShareIntent(intent)
|
||||
}
|
||||
|
||||
private fun stageShareIntent(intent: Intent?) {
|
||||
val action = intent?.action ?: return
|
||||
if (action != Intent.ACTION_SEND && action != Intent.ACTION_SEND_MULTIPLE) return
|
||||
require(intent != null)
|
||||
|
||||
val batchDir = File(File(dataDir, "share_inbox"), "${System.currentTimeMillis()}-${UUID.randomUUID()}")
|
||||
batchDir.mkdirs()
|
||||
val items = JSONArray()
|
||||
|
||||
when (action) {
|
||||
Intent.ACTION_SEND -> {
|
||||
when (intent.type) {
|
||||
"text/plain" -> intent.getStringExtra(Intent.EXTRA_TEXT)?.let { addTextItem(items, it) }
|
||||
else -> if (intent.type?.startsWith("image/") == true) {
|
||||
IntentCompat.getParcelableExtra(intent, Intent.EXTRA_STREAM, Uri::class.java)
|
||||
?.let { stageFile(it, 0, batchDir, items) }
|
||||
intent.getStringExtra(Intent.EXTRA_TEXT)?.let { addTextItem(items, it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
Intent.ACTION_SEND_MULTIPLE -> {
|
||||
if (intent.type?.startsWith("image/") == true) {
|
||||
IntentCompat.getParcelableArrayListExtra(intent, Intent.EXTRA_STREAM, Uri::class.java)
|
||||
?.forEachIndexed { i, uri -> stageFile(uri, i, batchDir, items) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (items.length() == 0) {
|
||||
batchDir.deleteRecursively()
|
||||
return
|
||||
}
|
||||
File(batchDir, "share.json").writeText(
|
||||
JSONObject().apply {
|
||||
put("version", 1)
|
||||
put("items", items)
|
||||
}.toString()
|
||||
)
|
||||
}
|
||||
|
||||
private fun addTextItem(items: JSONArray, text: String) {
|
||||
items.put(JSONObject().apply {
|
||||
put("kind", if (text.startsWith("http://") || text.startsWith("https://")) "url" else "text")
|
||||
put("text", text)
|
||||
})
|
||||
}
|
||||
|
||||
private fun stageFile(uri: Uri, index: Int, batchDir: File, items: JSONArray) {
|
||||
val resolver = contentResolver
|
||||
var displayName = "shared-$index"
|
||||
resolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)?.use { c ->
|
||||
if (c.moveToFirst()) {
|
||||
val i = c.getColumnIndex(OpenableColumns.DISPLAY_NAME)
|
||||
if (i >= 0) displayName = c.getString(i)
|
||||
}
|
||||
}
|
||||
val sanitized = displayName
|
||||
.replace("/", "_").replace("\\", "_").replace("\u0000", "")
|
||||
.take(120)
|
||||
.let { if (it.isEmpty() || it == "." || it == "..") "shared" else it }
|
||||
|
||||
val fileName = "$index-$sanitized"
|
||||
val dest = File(batchDir, fileName)
|
||||
try {
|
||||
resolver.openInputStream(uri)?.use { input ->
|
||||
FileOutputStream(dest).use { output -> input.copyTo(output) }
|
||||
}
|
||||
items.put(JSONObject().apply {
|
||||
put("kind", "file")
|
||||
put("fileName", fileName)
|
||||
put("mime", resolver.getType(uri) ?: "application/octet-stream")
|
||||
})
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.w("ShareTarget", "stage failed: ${e.message}")
|
||||
dest.delete()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
||||
Reference in new issue
Block a user