feat(android): albums from the phone — several files shared at once are one link
Albums.encode and Grant.encode (live grants too) write what TS writes; the album vector is resealed byte for byte from Kotlin. MediaUploader.uploadAlbum seals and uploads one. The demo turns a multi-file share into an album and its player lists an album's items and opens them in place. The live cross-check uploads an album from Kotlin that TS opens. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -21,6 +21,9 @@ import androidx.media3.common.Player
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
import androidx.media3.exoplayer.ExoPlayer
|
||||
import androidx.media3.ui.PlayerView
|
||||
import no.zyon.shade.media.ALBUM_MIME
|
||||
import no.zyon.shade.media.Album
|
||||
import no.zyon.shade.media.Albums
|
||||
import no.zyon.shade.media.LiveContent
|
||||
import no.zyon.shade.media.LiveGrant
|
||||
import no.zyon.shade.media.MediaGrant
|
||||
@@ -53,6 +56,10 @@ class PlayerActivity : Activity() {
|
||||
private lateinit var logScroll: ScrollView
|
||||
private lateinit var picture: ImageView
|
||||
private lateinit var share: Button
|
||||
private lateinit var albumList: LinearLayout
|
||||
private lateinit var albumScroll: ScrollView
|
||||
/** The album the item on screen came from, to go back to. */
|
||||
private var album: Album? = null
|
||||
/** The link playing now, to hand on. */
|
||||
private var current: String? = null
|
||||
|
||||
@@ -95,6 +102,11 @@ class PlayerActivity : Activity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
albumList = LinearLayout(this).apply { orientation = LinearLayout.VERTICAL }
|
||||
albumScroll = ScrollView(this).apply {
|
||||
addView(albumList)
|
||||
visibility = android.view.View.GONE
|
||||
}
|
||||
val bar = LinearLayout(this).apply {
|
||||
orientation = LinearLayout.HORIZONTAL
|
||||
gravity = Gravity.CENTER_VERTICAL
|
||||
@@ -111,6 +123,7 @@ class PlayerActivity : Activity() {
|
||||
addView(view, LinearLayout.LayoutParams(MATCH_PARENT, 0, 1f))
|
||||
addView(logScroll, LinearLayout.LayoutParams(MATCH_PARENT, 0, 1f))
|
||||
addView(picture, LinearLayout.LayoutParams(MATCH_PARENT, 0, 1f))
|
||||
addView(albumScroll, LinearLayout.LayoutParams(MATCH_PARENT, 0, 1f))
|
||||
},
|
||||
)
|
||||
linkFrom(intent)?.let {
|
||||
@@ -138,6 +151,13 @@ class PlayerActivity : Activity() {
|
||||
}
|
||||
current = link
|
||||
share.visibility = android.view.View.VISIBLE
|
||||
album = null
|
||||
open(grant)
|
||||
}
|
||||
|
||||
/** Open a grant: from a link, or an item of the album on screen. */
|
||||
private fun open(grant: no.zyon.shade.media.Grant) {
|
||||
stop()
|
||||
when (grant) {
|
||||
is LiveGrant -> watch(grant)
|
||||
is MediaGrant -> {
|
||||
@@ -201,13 +221,54 @@ class PlayerActivity : Activity() {
|
||||
if (atBottom) logScroll.post { logScroll.fullScroll(android.view.View.FOCUS_DOWN) }
|
||||
}
|
||||
|
||||
private enum class View { PLAYER, LOG, PICTURE }
|
||||
/**
|
||||
* An album: its items as a list, each opening in place. Back returns to
|
||||
* the list. Read already (it is small), so this needs no network.
|
||||
*/
|
||||
private fun showAlbum(a: Album) {
|
||||
album = a
|
||||
show(View.ALBUM)
|
||||
status.text = "${a.title} · ${a.items.size} item(s) · end-to-end encrypted"
|
||||
albumList.removeAllViews()
|
||||
a.items.forEachIndexed { i, g ->
|
||||
val meta = when (g) {
|
||||
is MediaGrant -> g.meta
|
||||
is LiveGrant -> g.meta
|
||||
}
|
||||
val kind = when {
|
||||
g is LiveGrant -> "live"
|
||||
Albums.isAlbum(g) -> "album"
|
||||
else -> meta?.mime?.substringBefore('/') ?: "file"
|
||||
}
|
||||
albumList.addView(Button(this).apply {
|
||||
text = "${meta?.name ?: "item ${i + 1}"} · $kind"
|
||||
isAllCaps = false
|
||||
gravity = Gravity.START or Gravity.CENTER_VERTICAL
|
||||
setOnClickListener { open(g) }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated("Activity.onBackPressed")
|
||||
override fun onBackPressed() {
|
||||
val a = album
|
||||
if (a != null && albumScroll.visibility != android.view.View.VISIBLE) {
|
||||
stop()
|
||||
showAlbum(a)
|
||||
return
|
||||
}
|
||||
@Suppress("DEPRECATION")
|
||||
super.onBackPressed()
|
||||
}
|
||||
|
||||
private enum class View { PLAYER, LOG, PICTURE, ALBUM }
|
||||
|
||||
private fun show(which: View) {
|
||||
fun vis(on: Boolean) = if (on) android.view.View.VISIBLE else android.view.View.GONE
|
||||
view.visibility = vis(which == View.PLAYER)
|
||||
logScroll.visibility = vis(which == View.LOG)
|
||||
picture.visibility = vis(which == View.PICTURE)
|
||||
albumScroll.visibility = vis(which == View.ALBUM)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -245,6 +306,11 @@ class PlayerActivity : Activity() {
|
||||
private fun start(f: ShadeFilm) {
|
||||
film = f
|
||||
val meta0 = f.reader.meta
|
||||
if (meta0.mime == ALBUM_MIME) {
|
||||
val read = runCatching { Albums.read(f.reader) }
|
||||
read.fold(onSuccess = { showAlbum(it) }, onFailure = { status.text = "Cannot read the album: ${it.message}" })
|
||||
return
|
||||
}
|
||||
if (meta0.mime?.startsWith("image/") == true) {
|
||||
status.text = "${meta0.name ?: "picture"} · ${meta0.mime} · ${f.reader.size / 1024} KiB · end-to-end encrypted"
|
||||
if (f.reader.size > MAX_PICTURE_BYTES) {
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.util.concurrent.Executors
|
||||
*/
|
||||
class ShareActivity : Activity() {
|
||||
private val io = Executors.newSingleThreadExecutor()
|
||||
private var album = "Shared"
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -44,6 +45,12 @@ class ShareActivity : Activity() {
|
||||
setText(Uploads.relay(this@ShareActivity) ?: "")
|
||||
setTextColor(Color.WHITE)
|
||||
}
|
||||
val albumTitle = EditText(this).apply {
|
||||
hint = "Album title"
|
||||
setSingleLine()
|
||||
setText("Shared ${java.text.DateFormat.getDateInstance().format(java.util.Date())}")
|
||||
setTextColor(Color.WHITE)
|
||||
}
|
||||
val ownerId = label("Owner key: …", 12f)
|
||||
val copy = Button(this).apply { text = "Copy owner id"; isEnabled = false }
|
||||
val upload = Button(this).apply { text = "Seal and upload"; isEnabled = uris.isNotEmpty() }
|
||||
@@ -57,6 +64,10 @@ class ShareActivity : Activity() {
|
||||
addView(files)
|
||||
addView(label("Relay"))
|
||||
addView(relay, LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT))
|
||||
if (uris.size > 1) {
|
||||
addView(label("One link for all of them: an album"))
|
||||
addView(albumTitle, LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT))
|
||||
}
|
||||
addView(ownerId)
|
||||
addView(copy)
|
||||
addView(upload)
|
||||
@@ -96,6 +107,7 @@ class ShareActivity : Activity() {
|
||||
return@setOnClickListener
|
||||
}
|
||||
Uploads.setRelay(this, url)
|
||||
album = albumTitle.text.toString().trim().ifEmpty { "Shared" }.take(512)
|
||||
if (Build.VERSION.SDK_INT >= 33 && checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != android.content.pm.PackageManager.PERMISSION_GRANTED) {
|
||||
requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1)
|
||||
} else {
|
||||
@@ -114,6 +126,7 @@ class ShareActivity : Activity() {
|
||||
val svc = Intent(this, UploadService::class.java).apply {
|
||||
clipData = clip
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
if (uris.size > 1) putExtra(UploadService.EXTRA_ALBUM, album)
|
||||
}
|
||||
startForegroundService(svc)
|
||||
finish()
|
||||
|
||||
@@ -13,6 +13,7 @@ import android.os.Handler
|
||||
import android.os.IBinder
|
||||
import android.os.Looper
|
||||
import android.os.PowerManager
|
||||
import no.zyon.shade.media.Album
|
||||
import no.zyon.shade.media.MediaGrant
|
||||
import no.zyon.shade.media.UploadJobs
|
||||
import no.zyon.shade.media.android.ShadeUpload
|
||||
@@ -24,7 +25,8 @@ import java.util.concurrent.atomic.AtomicInteger
|
||||
* a progress notification) so Android lets a film finish. A file that was
|
||||
* sealed before the app was killed is resumed when the service starts again;
|
||||
* one that was still being sealed is started over the next time it is shared.
|
||||
* When a file is up, a notification opens it, or shares its link.
|
||||
* Several files shared at once become one album (one link for the photos of
|
||||
* a trip). When a file is up, a notification opens it, or shares its link.
|
||||
*/
|
||||
class UploadService : Service() {
|
||||
private val worker = Executors.newSingleThreadExecutor()
|
||||
@@ -49,10 +51,11 @@ class UploadService : Service() {
|
||||
jobs.prune()
|
||||
for ((key, job) in jobs.unfinished()) {
|
||||
if (job.server.isEmpty()) continue
|
||||
enqueue(key, job.meta.name ?: "file") { onProgress ->
|
||||
val name = job.meta.name ?: "file"
|
||||
enqueue(key, name) { progress ->
|
||||
Uploads.uploader(this, job.server).upload(jobs, key, job.source, job.meta, {
|
||||
throw IllegalStateException("sealed copy missing")
|
||||
}, onProgress = onProgress)
|
||||
}) { acked, total -> progress(name, acked, total) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,19 +64,35 @@ class UploadService : Service() {
|
||||
lastStart = startId
|
||||
val relay = Uploads.relay(this)
|
||||
val uris = intent?.let(::urisOf).orEmpty()
|
||||
if (relay != null) {
|
||||
for (uri in uris) {
|
||||
// Described now, while the share's read permission still holds.
|
||||
val item = try {
|
||||
if (relay != null && uris.isNotEmpty()) {
|
||||
val uploader = Uploads.uploader(this, relay)
|
||||
val jobs = Uploads.jobs(this)
|
||||
// Described now, while the share's read permission still holds.
|
||||
val items = uris.mapNotNull { uri ->
|
||||
try {
|
||||
ShadeUpload.describe(this, uri)
|
||||
} catch (e: Exception) {
|
||||
failed(uri.lastPathSegment ?: "file", e)
|
||||
continue
|
||||
null
|
||||
}
|
||||
val uploader = Uploads.uploader(this, relay)
|
||||
val jobs = Uploads.jobs(this)
|
||||
enqueue(ShadeUpload.jobKey(jobs, uploader, item), item.meta.name ?: "file") { onProgress ->
|
||||
ShadeUpload.upload(this, uploader, jobs, item, onProgress)
|
||||
}
|
||||
val album = intent?.getStringExtra(EXTRA_ALBUM)
|
||||
if (album != null && items.size > 1) {
|
||||
// One link for all of them: each file is its own job, then the album of their grants.
|
||||
val keys = items.map { ShadeUpload.jobKey(jobs, uploader, it) }
|
||||
enqueue(keys.joinToString(","), album) { progress ->
|
||||
val grants = items.mapIndexed { i, item ->
|
||||
val label = "${i + 1}/${items.size} · ${item.meta.name ?: "file"}"
|
||||
ShadeUpload.upload(this, uploader, jobs, item) { acked, total -> progress(label, acked, total) }
|
||||
}
|
||||
uploader.uploadAlbum(Album(album, grants))
|
||||
}
|
||||
} else {
|
||||
for (item in items) {
|
||||
val name = item.meta.name ?: "file"
|
||||
enqueue(ShadeUpload.jobKey(jobs, uploader, item), name) { progress ->
|
||||
ShadeUpload.upload(this, uploader, jobs, item) { acked, total -> progress(name, acked, total) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,19 +100,20 @@ class UploadService : Service() {
|
||||
return START_REDELIVER_INTENT
|
||||
}
|
||||
|
||||
private fun enqueue(key: String, name: String, run: ((Long, Long) -> Unit) -> MediaGrant) {
|
||||
/** `run` reports progress as (what, acked, total). */
|
||||
private fun enqueue(key: String, name: String, run: ((String, Long, Long) -> Unit) -> MediaGrant) {
|
||||
if (!queued.add(key)) return
|
||||
pending.incrementAndGet()
|
||||
worker.execute {
|
||||
val wake = getSystemService(PowerManager::class.java).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "shade:upload")
|
||||
wake.acquire(6 * 3600_000L)
|
||||
var shown = -1
|
||||
var shown = ""
|
||||
try {
|
||||
val grant = run { acked, total ->
|
||||
val grant = run { what, acked, total ->
|
||||
val pct = if (total > 0) (acked * 100 / total).toInt() else 0
|
||||
if (pct != shown) {
|
||||
shown = pct
|
||||
nm.notify(FOREGROUND_ID, progress(name, "${mib(acked)} of ${mib(total)} MiB", pct, 100))
|
||||
if ("$what $pct" != shown) {
|
||||
shown = "$what $pct"
|
||||
nm.notify(FOREGROUND_ID, progress(what, "${mib(acked)} of ${mib(total)} MiB", pct, 100))
|
||||
}
|
||||
}
|
||||
uploaded(name, grant)
|
||||
@@ -175,6 +195,9 @@ class UploadService : Service() {
|
||||
private const val DONE = "uploaded"
|
||||
private const val FOREGROUND_ID = 1
|
||||
|
||||
/** A share of several files becomes one album with this title. */
|
||||
const val EXTRA_ALBUM = "no.zyon.shade.mediademo.ALBUM"
|
||||
|
||||
private fun mib(b: Long) = "%.1f".format(b / 1048576.0)
|
||||
|
||||
/** The relay's own watch page opens the link in a browser too. */
|
||||
|
||||
Reference in New Issue
Block a user