Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
138 lines
4.7 KiB
Kotlin
138 lines
4.7 KiB
Kotlin
package dev.mygo
|
|
|
|
import android.content.Context
|
|
import android.graphics.SurfaceTexture
|
|
import android.media.MediaPlayer
|
|
import android.media.PlaybackParams
|
|
import android.net.Uri
|
|
import android.os.Handler
|
|
import android.os.Looper
|
|
import android.view.Surface
|
|
import android.view.TextureView
|
|
import org.json.JSONObject
|
|
|
|
private class PlayerView(context: Context, val id: Long, args: JSONObject) : TextureView(context), TextureView.SurfaceTextureListener {
|
|
val player = MediaPlayer()
|
|
var ready = false
|
|
var ended = false
|
|
var error = ""
|
|
private val auto = args.optBoolean("autoPlay")
|
|
private val ticks = Handler(Looper.getMainLooper())
|
|
private val tick = object : Runnable {
|
|
override fun run() {
|
|
if (ready && player.isPlaying) send()
|
|
ticks.postDelayed(this, 250)
|
|
}
|
|
}
|
|
|
|
init {
|
|
surfaceTextureListener = this
|
|
player.isLooping = args.optBoolean("loop")
|
|
if (args.optBoolean("muted")) player.setVolume(0f, 0f)
|
|
val source = args.optString("source")
|
|
try {
|
|
if (source.contains("://")) player.setDataSource(context, Uri.parse(source)) else player.setDataSource(source)
|
|
} catch (e: Exception) {
|
|
error = e.toString()
|
|
}
|
|
player.setOnPreparedListener {
|
|
ready = true
|
|
if (auto) it.start()
|
|
send()
|
|
}
|
|
player.setOnCompletionListener {
|
|
ended = true
|
|
send()
|
|
}
|
|
player.setOnVideoSizeChangedListener { _, _, _ ->
|
|
fit()
|
|
send()
|
|
}
|
|
addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> fit() }
|
|
player.setOnErrorListener { _, what, extra ->
|
|
error = "MediaPlayer error $what/$extra"
|
|
send()
|
|
true
|
|
}
|
|
ticks.post(tick)
|
|
}
|
|
|
|
/** Shows the video whole in the view, keeping its shape: a texture fills the view. */
|
|
private fun fit() {
|
|
val vw = player.videoWidth.toFloat()
|
|
val vh = player.videoHeight.toFloat()
|
|
if (vw <= 0 || vh <= 0 || width == 0 || height == 0) return
|
|
val k = minOf(width / vw, height / vh)
|
|
val sx = vw * k / width
|
|
val sy = vh * k / height
|
|
val m = android.graphics.Matrix()
|
|
m.setScale(sx, sy, width / 2f, height / 2f)
|
|
setTransform(m)
|
|
}
|
|
|
|
fun send() {
|
|
PluginHost.emit("videoplayer", "state", JSONObject().put("id", id).put("ready", ready).put("ended", ended)
|
|
.put("playing", ready && player.isPlaying)
|
|
.put("positionMs", if (ready) player.currentPosition else 0).put("durationMs", if (ready) player.duration else 0)
|
|
.put("width", player.videoWidth).put("height", player.videoHeight).put("error", error))
|
|
}
|
|
|
|
fun dispose() {
|
|
ticks.removeCallbacks(tick)
|
|
player.release()
|
|
}
|
|
|
|
override fun onSurfaceTextureAvailable(t: SurfaceTexture, w: Int, h: Int) {
|
|
player.setSurface(Surface(t))
|
|
if (error.isEmpty() && !ready) runCatching { player.prepareAsync() }.onFailure { error = it.toString(); send() }
|
|
}
|
|
|
|
override fun onSurfaceTextureSizeChanged(t: SurfaceTexture, w: Int, h: Int) {}
|
|
override fun onSurfaceTextureDestroyed(t: SurfaceTexture) = true
|
|
override fun onSurfaceTextureUpdated(t: SurfaceTexture) {}
|
|
}
|
|
|
|
class VideoPlayerPlugin : MyGoPlugin {
|
|
private val views = HashMap<Long, PlayerView>()
|
|
|
|
init {
|
|
PluginHost.registerView("videoplayer") { context, id, args ->
|
|
PlayerView(context, id, args).also { views[id] = it }
|
|
}
|
|
}
|
|
|
|
override fun call(method: String, args: JSONObject, reply: PluginReply) {
|
|
val v = views[args.optLong("id")]
|
|
if (v == null) {
|
|
reply.failure(PluginFailure("NoView"))
|
|
return
|
|
}
|
|
when (method) {
|
|
"play" -> {
|
|
v.ended = false
|
|
if (v.ready) v.player.start()
|
|
v.send()
|
|
}
|
|
"pause" -> {
|
|
if (v.ready) v.player.pause()
|
|
v.send()
|
|
}
|
|
"seek" -> {
|
|
v.ended = false
|
|
if (v.ready) v.player.seekTo(args.optLong("ms"), MediaPlayer.SEEK_CLOSEST)
|
|
}
|
|
"volume" -> v.player.setVolume(args.optDouble("volume", 1.0).toFloat(), args.optDouble("volume", 1.0).toFloat())
|
|
"rate" -> if (v.ready) v.player.playbackParams = PlaybackParams().setSpeed(args.optDouble("rate", 1.0).toFloat())
|
|
"dispose" -> {
|
|
v.dispose()
|
|
views.remove(args.optLong("id"))
|
|
}
|
|
else -> {
|
|
reply.failure(PluginFailure("NoMethod", method))
|
|
return
|
|
}
|
|
}
|
|
reply.success(null)
|
|
}
|
|
}
|