One-tap song transfer from Mikro Seq without the replace dialog, with a backup (0.36.0)

Skipped only for ACTION_VIEW + extra.REPLACE + a content URI whose provider
belongs to cc.tiredsysadmin.android.mikroseq. The replaced song is saved as
"Before Mikro Seq import" first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-13 20:01:02 +01:00
co-authored by Claude Opus 5.5
parent 1eafe20410
commit 3170e8dd7a
8 changed files with 86 additions and 6 deletions
+6
View File
@@ -22,6 +22,12 @@
<uses-feature android:name="android.hardware.bluetooth" android:required="false" />
<uses-feature android:name="android.hardware.microphone" android:required="false" />
<!-- Lets SongIntents.isTrustedReplace see which app owns a content provider, so
only Mikro Seq's one-tap transfer can replace the song without asking. -->
<queries>
<package android:name="cc.tiredsysadmin.android.mikroseq" />
</queries>
<application
android:name=".SizzleApp"
android:allowBackup="true"
@@ -104,6 +104,7 @@ class MainActivity : ComponentActivity() {
* A `.sng` handed over by another app (Mikro Seq's "Open in Sizzle", a file
* manager, the share sheet): read it off the main thread, then let the UI ask
* before it replaces the current song. See [SongIntents] for what is accepted.
* Mikro Seq's one-tap transfer replaces it straight away, after a backup.
*/
private fun handleOpenIntent(intent: Intent?) {
intent ?: return
@@ -113,7 +114,7 @@ class MainActivity : ComponentActivity() {
setIntent(Intent(this, MainActivity::class.java))
lifecycleScope.launch {
val song = withContext(Dispatchers.IO) { SongIntents.read(this@MainActivity, intent) }
viewModel.offerIncomingSong(song)
if (song.replaceNow) viewModel.replaceFromMikroSeq(song) else viewModel.offerIncomingSong(song)
}
}
@@ -12,7 +12,14 @@ import java.io.InputStream
/** A song handed over by another app: its display [name] and [text], or an [error]
* to show instead. */
data class IncomingSong(val name: String, val text: String?, val error: String? = null)
data class IncomingSong(
val name: String,
val text: String?,
val error: String? = null,
/** Mikro Seq asked to overwrite the current song without asking (see
* [SongIntents.isTrustedReplace]). */
val replaceNow: Boolean = false,
)
/**
* Reads a `.sng` that another app opened in Sizzle (Mikro Seq's "Open in Sizzle",
@@ -33,6 +40,29 @@ object SongIntents {
/** Larger than any real song (Mikro Seq's biggest export is a few tens of KB). */
const val MAX_BYTES = 4 * 1024 * 1024
/** Boolean extra Mikro Seq's one-tap transfer sets to replace the song without the dialog. */
const val EXTRA_REPLACE = "cc.tiredsysadmin.android.sizzle.extra.REPLACE"
/** The only app allowed to skip the dialog. */
const val MIKROSEQ_PACKAGE = "cc.tiredsysadmin.android.mikroseq"
/**
* True only for Mikro Seq's one-tap transfer: an `ACTION_VIEW` with [EXTRA_REPLACE]
* set, of a `content:` URI served by a provider that belongs to Mikro Seq. That
* provider isn't exported, so nobody else can hand us such a URI; any other app
* setting the extra still gets the confirmation dialog. Needs Mikro Seq in the
* manifest's `<queries>` for the provider lookup to see it.
*/
fun isTrustedReplace(context: Context, intent: Intent): Boolean {
if (intent.action != Intent.ACTION_VIEW) return false
if (!intent.getBooleanExtra(EXTRA_REPLACE, false)) return false
val uri = intent.data ?: return false
if (uri.scheme != "content") return false
val authority = uri.authority ?: return false
val owner = runCatching { context.packageManager.resolveContentProvider(authority, 0)?.packageName }.getOrNull()
return owner == MIKROSEQ_PACKAGE
}
fun read(context: Context, intent: Intent): IncomingSong {
val uri: Uri? = when (intent.action) {
Intent.ACTION_VIEW -> intent.data
@@ -55,7 +85,7 @@ object SongIntents {
} ?: return IncomingSong(name, null, "Couldn't read $name.")
val text = String(bytes, Charsets.UTF_8)
if (!SngFormat.looksLikeSng(text)) return IncomingSong(name, null, NOT_A_SONG)
return IncomingSong(name, text)
return IncomingSong(name, text, replaceNow = isTrustedReplace(context, intent))
}
private const val NOT_A_SONG = "This doesn't look like a Sizzle .sng song, so nothing was imported."
@@ -163,12 +163,29 @@ class AppViewModel(
// ----- A song opened from another app (VIEW / SEND intent) -----
/** Waiting for the user to confirm replacing the current song, or to read why
* it can't be opened. */
private val BEFORE_MIKROSEQ = "Before Mikro Seq import"
var incomingSong by mutableStateOf<cc.tiredsysadmin.android.sizzle.io.IncomingSong?>(null); private set
fun offerIncomingSong(song: cc.tiredsysadmin.android.sizzle.io.IncomingSong) { incomingSong = song }
fun dismissIncomingSong() { incomingSong = null }
/**
* Mikro Seq's one-tap transfer ([cc.tiredsysadmin.android.sizzle.io.SongIntents.isTrustedReplace]):
* nothing asks first, so the current song is saved in full to the library as
* [BEFORE_MIKROSEQ] (overwriting the previous such backup), then replaced.
*/
fun replaceFromMikroSeq(song: cc.tiredsysadmin.android.sizzle.io.IncomingSong) {
val backedUp = runCatching { songLibrary.save(BEFORE_MIKROSEQ, saveProjectText()) }.isSuccess
incomingSong = song
if (acceptIncomingSong()) {
val msg = if (backedUp) "Loaded ${song.name}. Previous song saved as \"$BEFORE_MIKROSEQ\"."
else "Loaded ${song.name}. Couldn't back up the previous song."
android.widget.Toast.makeText(appContext, msg, android.widget.Toast.LENGTH_LONG).show()
}
}
/** Replace the current song with the offered one. Stops playback first so the
* audio thread isn't reading the model while it is rewritten. Returns false
* (leaving the song as it was parsed so far) if the import fails. */