Compare commits

..

No commits in common. "9874ea3cdb21c3cdf27fd7b0766ff1b2be9678b7" and "bc09479dfbfcbf104bf41b4f376c9dc078dd98a9" have entirely different histories.

53 changed files with 28 additions and 4846 deletions

View File

@ -1,5 +0,0 @@
.gradle/
build/
app/build/
local.properties
*.iml

View File

@ -1,34 +0,0 @@
# Blastoise Native Android Design Brief
Design direction: broadcast-console music app, not generic AI dashboard.
## Principles
- Put the listening task first: current channel, current track, transport, progress, channel switcher, and queue preview must stay visible on the main screen.
- Use explicit states: connected, loading, disconnected, guest session, drift, buffering, and playback state are visible without opening logs.
- Avoid generic AI decoration: no random blobs, ornamental gradients, nested cards, oversized empty hero areas, or decorative metrics.
- Keep controls direct and tactile: large touch targets, clear labels, predictable placement, no hidden primary actions.
- Keep diagnostics subordinate: useful for development, but visually quieter than playback.
## Theme
- Personality: late-night radio console, compact and technical.
- Display/body font: Rajdhani.
- Diagnostics font: JetBrains Mono.
- Background: near-black.
- Surfaces: layered blue-black panels.
- Accents: safety orange for primary actions, level-meter green for live sync, brass for warnings/session state, restrained red for failures.
- Avoid blue, cyan, and purple as dominant interface colors.
- Corners: squared receiver hardware. Use small 2-4dp radii, never bubbly cards.
## Layout
- Header: app identity plus connection status.
- Server row: compact editable endpoint and reconnect action.
- Mode switch: RADIO and LIBRARY are first-class modes.
- Main deck: station/library identity, track title, progress, transport, session/sync health.
- Transport controls: use a real player strip with previous, seek back, large play/pause, seek forward, and next. Avoid generic equal-width text buttons for playback.
- Radio channels: station cards that feel like playlists/radio rooms the user can join.
- Library: dense track rows for direct local-style MP3 playback from the server library.
- Queue: next few tracks for Radio mode, not the whole library.
- Diagnostics: compact, monospaced, last few events.

View File

@ -1,45 +0,0 @@
# Blastoise Native Android
Native Kotlin listener app for the Blastoise/MusicRoom server.
## Build
From this directory:
```bat
gradlew.bat :app:assembleDebug
```
The debug APK is written to:
```text
app\build\outputs\apk\debug\app-debug.apk
```
## Run
Use the root Android helper:
```bat
..\run.bat
```
Or install manually:
```bat
gradlew.bat :app:installDebug
adb shell am start -n com.peterino.blastoise/.MainActivity
```
## MVP scope
- Defaults to `http://mhsgroove.peterino.com:3001` and auto-connects on launch.
- Saves a server URL locally.
- Uses `/api/channels` to establish an authenticated or guest session.
- Supports `/api/auth/login` for named users.
- Connects to `/api/channels/:id/ws`.
- Plays `/api/tracks/:id` through AndroidX Media3.
- Corrects playback drift when the server timestamp differs by 2 seconds or more.
- Shows channel switching, queue preview, sync health, drift, session state, and recent WebSocket events on the main screen.
Offline track caching is intentionally not included yet.

View File

@ -1,36 +0,0 @@
plugins {
id("com.android.application")
}
android {
namespace = "com.peterino.blastoise"
compileSdk = 36
defaultConfig {
applicationId = "com.peterino.blastoise"
minSdk = 26
targetSdk = 36
versionCode = 1
versionName = "0.1.0"
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlin {
compilerOptions {
jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
}
}
}
dependencies {
val media3Version = "1.10.1"
implementation("androidx.media3:media3-exoplayer:$media3Version")
implementation("androidx.media3:media3-session:$media3Version")
implementation("androidx.media3:media3-common:$media3Version")
implementation("com.squareup.okhttp3:okhttp:5.3.0")
}

View File

@ -1,34 +0,0 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:allowBackup="false"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".PlaybackService"
android:exported="true"
android:foregroundServiceType="mediaPlayback">
<intent-filter>
<action android:name="androidx.media3.session.MediaSessionService" />
</intent-filter>
</service>
</application>
</manifest>

View File

@ -1,171 +0,0 @@
package com.peterino.blastoise
import org.json.JSONArray
import org.json.JSONObject
data class Track(
val id: String,
val filename: String,
val title: String,
val duration: Double,
)
data class ChannelInfo(
val id: String,
val name: String,
val description: String,
val listenerCount: Int,
val isDefault: Boolean,
val trackCount: Int = 0,
val listeners: List<String> = emptyList(),
)
data class ChannelState(
val track: Track?,
val currentTimestamp: Double,
val channelName: String,
val channelId: String,
val paused: Boolean,
val queue: List<Track>?,
val currentIndex: Int,
val playbackMode: String,
val listenerCount: Int,
val listeners: List<String>,
)
data class Playlist(
val id: String,
val name: String,
val description: String,
val ownerId: Int,
val ownerName: String,
val isPublic: Boolean,
val shareToken: String?,
val trackIds: List<String>,
)
data class PlaylistBundle(
val mine: List<Playlist>,
val shared: List<Playlist>,
)
data class Permission(
val resourceType: String,
val resourceId: String?,
val permission: String,
)
data class UserSession(
val id: Int,
val username: String,
val isAdmin: Boolean,
val isGuest: Boolean,
val permissions: List<Permission> = emptyList(),
) {
val isSignedIn: Boolean
get() = !isGuest
}
fun JSONObject.toTrack(): Track {
return Track(
id = optString("id", optString("filename")),
filename = optString("filename"),
title = optString("title", optString("filename", "Unknown")),
duration = optDouble("duration", 0.0),
)
}
fun JSONObject.toChannelInfo(): ChannelInfo {
return ChannelInfo(
id = getString("id"),
name = optString("name", "Channel"),
description = optString("description", ""),
listenerCount = optInt("listenerCount", 0),
isDefault = optBoolean("isDefault", false),
trackCount = optInt("trackCount", 0),
listeners = optJSONArray("listeners").toStringList(),
)
}
fun JSONObject.toChannelState(): ChannelState {
val trackJson = optJSONObject("track")
val queueJson = optJSONArray("queue")
val queue = if (queueJson == null) {
null
} else {
(0 until queueJson.length()).map { queueJson.getJSONObject(it).toTrack() }
}
return ChannelState(
track = trackJson?.toTrack(),
currentTimestamp = optDouble("currentTimestamp", 0.0),
channelName = optString("channelName", ""),
channelId = optString("channelId", ""),
paused = optBoolean("paused", true),
queue = queue,
currentIndex = optInt("currentIndex", 0),
playbackMode = optString("playbackMode", "repeat-all"),
listenerCount = optInt("listenerCount", 0),
listeners = optJSONArray("listeners").toStringList(),
)
}
fun JSONObject.toPermission(): Permission {
return Permission(
resourceType = optString("resource_type", optString("resourceType", "")),
resourceId = when {
isNull("resource_id") -> null
has("resource_id") -> optString("resource_id")
isNull("resourceId") -> null
else -> optString("resourceId")
},
permission = optString("permission", ""),
)
}
fun JSONObject.toUserSession(): UserSession {
val user = optJSONObject("user") ?: this
return UserSession(
id = user.optInt("id", 0),
username = user.optString("username", "guest"),
isAdmin = user.optBoolean("isAdmin", user.optBoolean("is_admin", false)),
isGuest = user.optBoolean("isGuest", user.optBoolean("is_guest", false)),
permissions = optJSONArray("permissions").toPermissions(),
)
}
fun JSONObject.toPlaylist(): Playlist {
return Playlist(
id = getString("id"),
name = optString("name", "Playlist"),
description = optString("description", ""),
ownerId = optInt("ownerId", 0),
ownerName = optString("ownerName", ""),
isPublic = optBoolean("isPublic", false),
shareToken = if (isNull("shareToken")) null else getString("shareToken"),
trackIds = optJSONArray("trackIds").toStringList(),
)
}
fun JSONArray.toChannels(): List<ChannelInfo> {
return (0 until length()).map { getJSONObject(it).toChannelInfo() }
}
fun JSONArray.toTracks(): List<Track> {
return (0 until length()).map { getJSONObject(it).toTrack() }
}
fun JSONArray?.toPlaylists(): List<Playlist> {
if (this == null) return emptyList()
return (0 until length()).map { getJSONObject(it).toPlaylist() }
}
fun JSONArray?.toStringList(): List<String> {
if (this == null) return emptyList()
return (0 until length()).mapNotNull { optString(it).takeIf(String::isNotBlank) }
}
fun JSONArray?.toPermissions(): List<Permission> {
if (this == null) return emptyList()
return (0 until length()).map { getJSONObject(it).toPermission() }
}

View File

@ -1,509 +0,0 @@
package com.peterino.blastoise
import android.content.Context
import okhttp3.Call
import okhttp3.Callback
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import okhttp3.WebSocket
import okhttp3.WebSocketListener
import org.json.JSONArray
import org.json.JSONObject
import java.io.IOException
import java.util.concurrent.TimeUnit
class MusicRoomClient(
private val context: Context,
private val listener: Listener,
) {
interface Listener {
fun onStatus(message: String)
fun onDebug(message: String)
fun onChannels(channels: List<ChannelInfo>)
fun onLibrary(tracks: List<Track>)
fun onPlaylists(playlists: PlaylistBundle)
fun onPlaylistDetail(playlist: Playlist)
fun onChannelState(state: ChannelState)
fun onSwitched(channelId: String)
fun onAuth(user: UserSession?)
fun onError(message: String)
}
private val client = OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(0, TimeUnit.SECONDS)
.build()
private var socket: WebSocket? = null
fun close() {
socket?.close(1000, "closing")
socket = null
}
fun login(username: String, password: String) {
listener.onDebug("POST /api/auth/login")
val body = JSONObject()
.put("username", username)
.put("password", password)
.toString()
.toRequestBody("application/json".toMediaType())
val request = request("/api/auth/login")
.post(body)
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
listener.onDebug("login failure: ${e.message ?: "network error"}")
listener.onError("Login failed: ${e.message ?: "network error"}")
}
override fun onResponse(call: Call, response: Response) {
response.use {
captureCookie(it)
listener.onDebug("login HTTP ${it.code}; cookie=${SessionStore.cookieHeader.isNotBlank()}")
if (!it.isSuccessful) {
listener.onError("Login failed: HTTP ${it.code}")
return
}
val session = JSONObject(it.body.string()).toUserSession()
listener.onAuth(session)
SessionStore.save(context)
listener.onStatus("Signed in")
loadMe()
}
}
})
}
fun signup(username: String, password: String) {
listener.onDebug("POST /api/auth/signup")
val body = JSONObject()
.put("username", username)
.put("password", password)
.toString()
.toRequestBody("application/json".toMediaType())
val request = request("/api/auth/signup")
.post(body)
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
listener.onDebug("signup failure: ${e.message ?: "network error"}")
listener.onError("Signup failed: ${e.message ?: "network error"}")
}
override fun onResponse(call: Call, response: Response) {
response.use {
captureCookie(it)
listener.onDebug("signup HTTP ${it.code}; cookie=${SessionStore.cookieHeader.isNotBlank()}")
if (!it.isSuccessful) {
listener.onError("Signup failed: HTTP ${it.code}")
return
}
val session = JSONObject(it.body.string()).toUserSession()
listener.onAuth(session)
SessionStore.save(context)
listener.onStatus("Signed up")
loadMe()
}
}
})
}
fun logout() {
listener.onDebug("POST /api/auth/logout")
val request = request("/api/auth/logout")
.post("{}".toRequestBody("application/json".toMediaType()))
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
listener.onDebug("logout failure: ${e.message ?: "network error"}")
SessionStore.cookieHeader = ""
SessionStore.save(context)
listener.onAuth(null)
listener.onStatus("Signed out")
}
override fun onResponse(call: Call, response: Response) {
response.use {
captureCookie(it)
SessionStore.cookieHeader = ""
SessionStore.save(context)
listener.onDebug("logout HTTP ${it.code}")
listener.onAuth(null)
listener.onStatus("Signed out")
}
}
})
}
fun loadMe() {
listener.onDebug("GET /api/auth/me")
val request = request("/api/auth/me").build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
listener.onDebug("me failure: ${e.message ?: "network error"}")
listener.onError("Auth check failed: ${e.message ?: "network error"}")
}
override fun onResponse(call: Call, response: Response) {
response.use {
captureCookie(it)
listener.onDebug("me HTTP ${it.code}; cookie=${SessionStore.cookieHeader.isNotBlank()}")
if (!it.isSuccessful) {
SessionStore.cookieHeader = ""
SessionStore.save(context)
listener.onAuth(null)
listener.onStatus("Sign in required")
return
}
val body = JSONObject(it.body.string())
val user = body.optJSONObject("user")
if (user == null) {
SessionStore.cookieHeader = ""
listener.onAuth(null)
listener.onStatus("Sign in required")
} else {
listener.onAuth(body.toUserSession())
}
SessionStore.save(context)
}
}
})
}
fun loadChannels() {
listener.onDebug("GET /api/channels ${SessionStore.serverBaseUrl}")
val request = request("/api/channels").build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
listener.onDebug("channels failure: ${e.message ?: "network error"}")
listener.onError("Could not load channels: ${e.message ?: "network error"}")
}
override fun onResponse(call: Call, response: Response) {
response.use {
captureCookie(it)
SessionStore.save(context)
listener.onDebug("channels HTTP ${it.code}; cookie=${SessionStore.cookieHeader.isNotBlank()}")
if (!it.isSuccessful) {
listener.onError("Could not load channels: HTTP ${it.code}")
return
}
val json = it.body.string()
val channels = JSONArray(json).toChannels()
listener.onDebug("channels parsed: ${channels.size}")
listener.onChannels(channels)
listener.onStatus("Loaded ${channels.size} channel(s)")
}
}
})
}
fun loadLibrary() {
listener.onDebug("GET /api/library")
val request = request("/api/library").build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
listener.onDebug("library failure: ${e.message ?: "network error"}")
listener.onError("Could not load library: ${e.message ?: "network error"}")
}
override fun onResponse(call: Call, response: Response) {
response.use {
captureCookie(it)
SessionStore.save(context)
listener.onDebug("library HTTP ${it.code}; cookie=${SessionStore.cookieHeader.isNotBlank()}")
if (!it.isSuccessful) {
listener.onError("Could not load library: HTTP ${it.code}")
return
}
val tracks = JSONArray(it.body.string()).toTracks()
listener.onDebug("library parsed: ${tracks.size}")
listener.onLibrary(tracks)
}
}
})
}
fun loadPlaylists() {
listener.onDebug("GET /api/playlists")
val request = request("/api/playlists").build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
listener.onDebug("playlists failure: ${e.message ?: "network error"}")
listener.onError("Could not load playlists: ${e.message ?: "network error"}")
}
override fun onResponse(call: Call, response: Response) {
response.use {
captureCookie(it)
SessionStore.save(context)
listener.onDebug("playlists HTTP ${it.code}; cookie=${SessionStore.cookieHeader.isNotBlank()}")
if (!it.isSuccessful) {
listener.onError("Could not load playlists: HTTP ${it.code}")
return
}
val json = JSONObject(it.body.string())
val bundle = PlaylistBundle(
mine = json.optJSONArray("mine").toPlaylists(),
shared = json.optJSONArray("shared").toPlaylists(),
)
listener.onDebug("playlists parsed: mine=${bundle.mine.size} shared=${bundle.shared.size}")
listener.onPlaylists(bundle)
}
}
})
}
fun loadPlaylist(playlistId: String) {
listener.onDebug("GET /api/playlists/$playlistId")
val request = request("/api/playlists/$playlistId").build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
listener.onDebug("playlist detail failure: ${e.message ?: "network error"}")
listener.onError("Could not load playlist: ${e.message ?: "network error"}")
}
override fun onResponse(call: Call, response: Response) {
response.use {
captureCookie(it)
SessionStore.save(context)
listener.onDebug("playlist detail HTTP ${it.code}")
if (!it.isSuccessful) {
listener.onError("Could not load playlist: HTTP ${it.code}")
return
}
listener.onPlaylistDetail(JSONObject(it.body.string()).toPlaylist())
}
}
})
}
fun connectChannel(channelId: String) {
socket?.close(1000, "switching")
listener.onDebug("WS connecting channel=$channelId")
val builder = Request.Builder()
.url(SessionStore.wsUrl(channelId))
.header("User-Agent", SessionStore.userAgent)
if (SessionStore.cookieHeader.isNotBlank()) {
builder.header("Cookie", SessionStore.cookieHeader)
}
socket = client.newWebSocket(builder.build(), object : WebSocketListener() {
override fun onOpen(webSocket: WebSocket, response: Response) {
listener.onDebug("WS open HTTP ${response.code}")
listener.onStatus("Connected")
loadChannelState(channelId)
}
override fun onMessage(webSocket: WebSocket, text: String) {
handleSocketMessage(text)
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
listener.onDebug("WS failure ${response?.code ?: "-"}: ${t.message ?: "websocket error"}")
listener.onError("Disconnected: ${t.message ?: "websocket error"}")
}
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
listener.onDebug("WS closed $code $reason")
listener.onStatus("Disconnected")
}
})
}
fun sendAction(action: String) {
listener.onDebug("WS send action=$action")
socket?.send(JSONObject().put("action", action).toString())
}
fun sendSeek(timestamp: Double) {
listener.onDebug("WS send seek=${"%.1f".format(timestamp)}")
socket?.send(
JSONObject()
.put("action", "seek")
.put("timestamp", timestamp)
.toString()
)
}
fun sendJump(index: Int) {
listener.onDebug("WS send jump=$index")
socket?.send(
JSONObject()
.put("action", "jump")
.put("index", index)
.toString()
)
}
fun setPlaybackMode(channelId: String, mode: String) {
listener.onDebug("POST /api/channels/$channelId/mode $mode")
val body = JSONObject()
.put("mode", mode)
.toString()
.toRequestBody("application/json".toMediaType())
val request = request("/api/channels/$channelId/mode")
.post(body)
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
listener.onDebug("mode failure: ${e.message ?: "network error"}")
listener.onError("Mode failed: ${e.message ?: "network error"}")
}
override fun onResponse(call: Call, response: Response) {
response.use {
listener.onDebug("mode HTTP ${it.code}")
if (it.code == 403) {
listener.onError("Mode denied")
} else if (!it.isSuccessful) {
listener.onError("Mode failed: HTTP ${it.code}")
} else {
listener.onStatus("Mode set")
}
}
}
})
}
fun addTracksToQueue(channelId: String, trackIds: List<String>, insertAt: Int? = null) {
if (trackIds.isEmpty()) return
val insertLabel = insertAt?.let { " insertAt=$it" } ?: ""
listener.onDebug("PATCH /api/channels/$channelId/queue add=${trackIds.size}$insertLabel")
val bodyJson = JSONObject().put("add", JSONArray(trackIds))
if (insertAt != null) bodyJson.put("insertAt", insertAt)
val request = request("/api/channels/$channelId/queue")
.patch(bodyJson.toString().toRequestBody("application/json".toMediaType()))
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
listener.onDebug("queue add failure: ${e.message ?: "network error"}")
listener.onError("Queue failed: ${e.message ?: "network error"}")
}
override fun onResponse(call: Call, response: Response) {
response.use {
listener.onDebug("queue add HTTP ${it.code}")
if (it.code == 403) {
listener.onError("Queue denied")
} else if (!it.isSuccessful) {
listener.onError("Queue failed: HTTP ${it.code}")
} else {
listener.onStatus(if (insertAt == null) "Queued ${trackIds.size}" else "Play next queued")
currentChannelFromQueuePatch(channelId)
}
}
}
})
}
fun removeTracksFromQueue(channelId: String, indices: List<Int>) {
if (indices.isEmpty()) return
listener.onDebug("PATCH /api/channels/$channelId/queue remove=${indices.size}")
val request = request("/api/channels/$channelId/queue")
.patch(
JSONObject()
.put("remove", JSONArray(indices))
.toString()
.toRequestBody("application/json".toMediaType())
)
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
listener.onDebug("queue remove failure: ${e.message ?: "network error"}")
listener.onError("Queue remove failed: ${e.message ?: "network error"}")
}
override fun onResponse(call: Call, response: Response) {
response.use {
listener.onDebug("queue remove HTTP ${it.code}")
if (it.code == 403) {
listener.onError("Queue denied")
} else if (!it.isSuccessful) {
listener.onError("Queue remove failed: HTTP ${it.code}")
} else {
listener.onStatus("Removed from queue")
currentChannelFromQueuePatch(channelId)
}
}
}
})
}
private fun loadChannelState(channelId: String) {
listener.onDebug("GET /api/channels/$channelId")
val request = request("/api/channels/$channelId").build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
listener.onDebug("channel state failure: ${e.message ?: "network error"}")
}
override fun onResponse(call: Call, response: Response) {
response.use {
if (!it.isSuccessful) {
listener.onDebug("channel state HTTP ${it.code}")
return
}
val json = JSONObject(it.body.string())
listener.onDebug("channel state HTTP ${it.code}; queue=${json.optJSONArray("queue")?.length() ?: 0}")
listener.onChannelState(json.toChannelState())
}
}
})
}
private fun currentChannelFromQueuePatch(channelId: String) {
loadChannelState(channelId)
loadChannels()
}
private fun handleSocketMessage(text: String) {
val json = JSONObject(text)
val type = json.optString("type", "")
val summary = when {
type.isNotBlank() -> "type=$type"
json.has("track") -> "state track=${json.optJSONObject("track")?.optString("title", "unknown") ?: "none"}"
else -> "state"
}
listener.onDebug("WS message $summary")
when (type) {
"" -> listener.onChannelState(json.toChannelState())
"channel_list" -> {
val channels = json.optJSONArray("channels")?.toChannels().orEmpty()
listener.onChannels(channels)
}
"switched" -> listener.onSwitched(json.optString("channelId"))
"kick" -> listener.onError("Disconnected: ${json.optString("reason", "kicked")}")
"error" -> listener.onError(json.optString("message", "Server error"))
else -> listener.onDebug("ignored server event type=$type")
}
}
private fun request(path: String): Request.Builder {
val builder = Request.Builder()
.url(SessionStore.httpUrl(path))
.header("User-Agent", SessionStore.userAgent)
if (SessionStore.cookieHeader.isNotBlank()) {
builder.header("Cookie", SessionStore.cookieHeader)
}
return builder
}
private fun captureCookie(response: Response) {
response.headers("Set-Cookie")
.map { it.substringBefore(";") }
.firstOrNull { it.startsWith("blastoise_session=") }
?.let {
SessionStore.cookieHeader = if (it == "blastoise_session=") "" else it
}
}
}

View File

@ -1,880 +0,0 @@
package com.peterino.blastoise
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import androidx.media3.common.AudioAttributes
import androidx.media3.common.C
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.datasource.DataSource
import androidx.media3.datasource.DefaultHttpDataSource
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import androidx.media3.session.MediaSession
import androidx.media3.session.MediaSessionService
import kotlin.math.abs
import kotlin.random.Random
@UnstableApi
class PlaybackService : MediaSessionService(), MusicRoomClient.Listener {
private val mainHandler = Handler(Looper.getMainLooper())
private lateinit var client: MusicRoomClient
private lateinit var player: ExoPlayer
private var mediaSession: MediaSession? = null
private var sourceMode = PlaybackSourceMode.RADIO
private var authState = AuthState.CHECKING
private var currentUser: UserSession? = null
private var lastStatus = "Starting"
private var lastChannels: List<ChannelInfo> = emptyList()
private var libraryTracks: List<Track> = emptyList()
private var libraryLoaded = false
private var myPlaylists: List<Playlist> = emptyList()
private var sharedPlaylists: List<Playlist> = emptyList()
private var playlistsLoaded = false
private var selectedPlaylistId: String? = null
private var selectedPlaylist: Playlist? = null
private var currentChannelId: String? = null
private var currentTrackId: String? = null
private var localLibraryIndex = -1
private var lastPaused = true
private var lastQueue: List<Track> = emptyList()
private var lastQueueLoaded = false
private var lastCurrentIndex = 0
private var playbackMode = "repeat-all"
private var lastChannelName = "No channel"
private var lastTrackTitle = "No track"
private var lastTrackDuration = 0.0
private var lastServerTimestampMs = 0L
private var lastStateRealtimeMs = 0L
private var intentionalDisconnect = false
private var reconnectAttempts = 0
private val debugEvents = mutableListOf<String>()
private val playbackModes = listOf("once", "repeat-all", "repeat-one", "shuffle")
private val ticker = object : Runnable {
override fun run() {
tickPlayback()
mainHandler.postDelayed(this, 500)
}
}
private val reconnectRunnable = Runnable {
val channelId = currentChannelId
if (sourceMode == PlaybackSourceMode.RADIO && channelId != null && !intentionalDisconnect) {
addDebugEvent("reconnect channel=$channelId attempt=${reconnectAttempts + 1}")
reconnectAttempts += 1
client.connectChannel(channelId)
publishSnapshot()
}
}
override fun onCreate() {
super.onCreate()
SessionStore.load(this)
client = MusicRoomClient(this, this)
val httpDataSourceFactory = DataSource.Factory {
val requestProperties = mutableMapOf<String, String>()
if (SessionStore.cookieHeader.isNotBlank()) {
requestProperties["Cookie"] = SessionStore.cookieHeader
}
DefaultHttpDataSource.Factory()
.setUserAgent(SessionStore.userAgent)
.setAllowCrossProtocolRedirects(true)
.setDefaultRequestProperties(requestProperties)
.createDataSource()
}
val mediaSourceFactory = DefaultMediaSourceFactory(this)
.setDataSourceFactory(httpDataSourceFactory)
val audioAttributes = AudioAttributes.Builder()
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
.setUsage(C.USAGE_MEDIA)
.build()
player = ExoPlayer.Builder(this)
.setMediaSourceFactory(mediaSourceFactory)
.setWakeMode(C.WAKE_MODE_LOCAL)
.setAudioAttributes(audioAttributes, true)
.setHandleAudioBecomingNoisy(true)
.build()
.apply {
addListener(object : Player.Listener {
override fun onPlaybackStateChanged(playbackState: Int) {
if (playbackState == Player.STATE_ENDED) {
handlePlaybackEnded()
}
publishSnapshot()
}
override fun onIsPlayingChanged(isPlaying: Boolean) {
if (sourceMode == PlaybackSourceMode.LIBRARY) {
lastPaused = !isPlaying
}
publishSnapshot()
}
})
}
mediaSession = MediaSession.Builder(this, player).build()
PlaybackBridge.service = this
addDebugEvent("service created")
publishSnapshot()
validateSession()
mainHandler.post(ticker)
}
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession? {
return mediaSession
}
override fun onTaskRemoved(rootIntent: Intent?) {
if (!player.isPlaying) {
pauseAllPlayersAndStopSelf()
}
}
override fun onDestroy() {
mainHandler.removeCallbacks(ticker)
mainHandler.removeCallbacks(reconnectRunnable)
intentionalDisconnect = true
client.close()
mediaSession?.run {
player.release()
release()
}
mediaSession = null
PlaybackBridge.service = null
super.onDestroy()
}
fun validateSession() = onMain {
if (SessionStore.cookieHeader.isBlank()) {
authState = AuthState.SIGNED_OUT
currentUser = null
lastStatus = "Sign in required"
publishSnapshot()
return@onMain
}
authState = AuthState.CHECKING
lastStatus = "Checking session"
publishSnapshot()
client.loadMe()
}
fun signIn(serverUrl: String, username: String, password: String) = onMain {
SessionStore.setBaseUrl(serverUrl)
SessionStore.save(this)
authState = AuthState.CHECKING
lastStatus = "Signing in"
publishSnapshot()
client.login(username.trim(), password)
}
fun signUp(serverUrl: String, username: String, password: String) = onMain {
SessionStore.setBaseUrl(serverUrl)
SessionStore.save(this)
authState = AuthState.CHECKING
lastStatus = "Signing up"
publishSnapshot()
client.signup(username.trim(), password)
}
fun signOut() = onMain {
authState = AuthState.SIGNED_OUT
currentUser = null
intentionalDisconnect = true
client.close()
player.pause()
player.clearMediaItems()
currentTrackId = null
myPlaylists = emptyList()
sharedPlaylists = emptyList()
playlistsLoaded = false
selectedPlaylistId = null
selectedPlaylist = null
lastTrackTitle = "Sign in to join a room"
lastTrackDuration = 0.0
lastPaused = true
lastStatus = "Signing out"
publishSnapshot()
client.logout()
}
fun connectToServer(input: String = SessionStore.serverBaseUrl) = onMain {
SessionStore.setBaseUrl(input)
SessionStore.save(this)
if (!hasSignedInUser()) {
validateSession()
return@onMain
}
intentionalDisconnect = true
client.close()
intentionalDisconnect = false
reconnectAttempts = 0
sourceMode = PlaybackSourceMode.RADIO
currentChannelId = null
currentTrackId = null
localLibraryIndex = -1
lastQueue = emptyList()
lastQueueLoaded = false
myPlaylists = emptyList()
sharedPlaylists = emptyList()
playlistsLoaded = false
selectedPlaylistId = null
selectedPlaylist = null
lastCurrentIndex = 0
playbackMode = "repeat-all"
lastChannelName = "No channel"
lastTrackTitle = "No track"
lastTrackDuration = 0.0
lastServerTimestampMs = 0L
lastStateRealtimeMs = 0L
lastStatus = "Loading"
addDebugEvent("connect ${SessionStore.serverBaseUrl}")
publishSnapshot()
client.loadChannels()
}
fun loadLibrary() = onMain {
if (!requireSignedIn("Sign in to browse the library")) return@onMain
client.loadLibrary()
}
fun loadPlaylists() = onMain {
if (!requireSignedIn("Sign in to load playlists")) return@onMain
client.loadPlaylists()
}
fun loadPlaylist(playlistId: String) = onMain {
if (!requireSignedIn("Sign in to load playlists")) return@onMain
selectedPlaylistId = playlistId
selectedPlaylist = null
publishSnapshot()
client.loadPlaylist(playlistId)
}
fun ensureRadioMode() = onMain {
if (!requireSignedIn("Sign in to join rooms")) return@onMain
sourceMode = PlaybackSourceMode.RADIO
intentionalDisconnect = false
val target = currentChannelId
?: lastChannels.firstOrNull { it.isDefault }?.id
?: lastChannels.firstOrNull()?.id
if (target != null && currentTrackId == null) {
joinChannel(target)
} else {
publishSnapshot()
}
}
fun enterLibraryMode() = onMain {
if (!requireSignedIn("Sign in to browse the library")) return@onMain
lastStatus = if (libraryLoaded) "Library search" else "Loading library"
publishSnapshot()
if (!libraryLoaded) client.loadLibrary()
}
fun joinChannel(channelId: String) = onMain {
if (!requireSignedIn("Sign in to join rooms")) return@onMain
sourceMode = PlaybackSourceMode.RADIO
intentionalDisconnect = false
mainHandler.removeCallbacks(reconnectRunnable)
currentChannelId = channelId
localLibraryIndex = -1
lastStatus = "Joining"
addDebugEvent("join channel=$channelId")
publishSnapshot()
client.connectChannel(channelId)
}
fun playLibraryTrack(index: Int) = onMain {
if (index !in libraryTracks.indices) return@onMain
sourceMode = PlaybackSourceMode.LIBRARY
intentionalDisconnect = true
client.close()
val track = libraryTracks[index]
localLibraryIndex = index
currentTrackId = track.id
lastTrackTitle = track.title
lastTrackDuration = track.duration
lastChannelName = "Library"
lastPaused = false
lastStatus = "Local"
lastServerTimestampMs = 0L
lastStateRealtimeMs = 0L
player.setMediaItem(buildMediaItem(track), 0L)
applyLocalPlaybackMode()
player.prepare()
player.play()
addDebugEvent("local play ${track.title.take(40)}")
publishSnapshot()
}
fun togglePlay() = onMain {
if (sourceMode == PlaybackSourceMode.RADIO) {
client.sendAction(if (lastPaused) "unpause" else "pause")
return@onMain
}
if (player.isPlaying) {
player.pause()
lastPaused = true
} else {
if (localLibraryIndex < 0 && libraryTracks.isNotEmpty()) {
playLibraryTrack(0)
return@onMain
}
player.play()
lastPaused = false
}
publishSnapshot()
}
fun previous() = onMain {
if (sourceMode == PlaybackSourceMode.RADIO) {
jumpRadio(-1)
} else if (libraryTracks.isNotEmpty()) {
val nextIndex = if (playbackMode == "shuffle" && libraryTracks.size > 1) {
randomLibraryIndex()
} else if (localLibraryIndex <= 0) {
libraryTracks.lastIndex
} else {
localLibraryIndex - 1
}
playLibraryTrack(nextIndex)
}
}
fun next() = onMain {
if (sourceMode == PlaybackSourceMode.RADIO) {
jumpRadio(1)
} else if (libraryTracks.isNotEmpty()) {
playLibraryTrack(nextLibraryIndex())
}
}
fun seekBy(seconds: Int) = onMain {
val durationMs = when {
player.duration > 0 -> player.duration
lastTrackDuration > 0.0 -> (lastTrackDuration * 1000.0).toLong()
else -> Long.MAX_VALUE
}
val targetMs = (player.currentPosition + seconds * 1000L)
.coerceAtLeast(0L)
.coerceAtMost(durationMs)
seekTo(targetMs / 1000.0)
}
fun seekTo(seconds: Double) = onMain {
val targetMs = (seconds * 1000.0).toLong().coerceAtLeast(0L)
if (sourceMode == PlaybackSourceMode.RADIO) {
client.sendSeek(seconds)
} else {
player.seekTo(targetMs)
}
addDebugEvent("seek ${"%.1f".format(seconds)}s")
publishSnapshot()
}
fun stopPlayback() = onMain {
player.pause()
player.seekTo(0L)
lastPaused = true
if (sourceMode == PlaybackSourceMode.RADIO) {
client.sendAction("pause")
client.sendSeek(0.0)
}
addDebugEvent("stop")
publishSnapshot()
}
fun stopPlaybackAndExit() = onMain {
intentionalDisconnect = true
mainHandler.removeCallbacks(reconnectRunnable)
client.close()
player.pause()
player.stop()
player.clearMediaItems()
currentTrackId = null
localLibraryIndex = -1
lastPaused = true
lastTrackTitle = "Stopped"
lastTrackDuration = 0.0
lastServerTimestampMs = 0L
lastStateRealtimeMs = 0L
lastStatus = "Stopped"
addDebugEvent("stop and exit")
publishSnapshot()
pauseAllPlayersAndStopSelf()
stopSelf()
}
fun cyclePlaybackMode() = onMain {
if (!requireSignedIn("Sign in to control rooms")) return@onMain
val currentIndex = playbackModes.indexOf(playbackMode).coerceAtLeast(0)
val nextMode = playbackModes[(currentIndex + 1) % playbackModes.size]
playbackMode = nextMode
if (sourceMode == PlaybackSourceMode.RADIO) {
val channelId = currentChannelId ?: run {
lastStatus = "No channel"
publishSnapshot()
return@onMain
}
client.setPlaybackMode(channelId, nextMode)
} else {
applyLocalPlaybackMode()
lastStatus = "Mode ${modeLabel(nextMode)}"
}
publishSnapshot()
}
fun queueCurrentTrack(playNext: Boolean) = onMain {
val trackId = currentTrackId ?: libraryTracks.getOrNull(localLibraryIndex)?.id
if (trackId == null) {
lastStatus = "No track"
publishSnapshot()
return@onMain
}
queueTrackIds(listOf(trackId), playNext)
}
fun queueTrack(track: Track, playNext: Boolean) = onMain {
queueTrackIds(listOf(track.id), playNext)
}
fun addPlaylistToQueue(playlist: Playlist, playNext: Boolean) = onMain {
if (playlist.trackIds.isEmpty()) {
lastStatus = "Empty playlist"
publishSnapshot()
return@onMain
}
queueTrackIds(playlist.trackIds, playNext)
}
fun queueTrackIds(trackIds: List<String>, playNext: Boolean) = onMain {
if (!requireSignedIn("Sign in to change the room queue")) return@onMain
val channelId = currentChannelId
?: lastChannels.firstOrNull { it.isDefault }?.id
?: lastChannels.firstOrNull()?.id
if (channelId == null) {
lastStatus = "No channel"
publishSnapshot()
return@onMain
}
currentChannelId = channelId
val insertAt = if (playNext) (lastCurrentIndex + 1).coerceAtLeast(0) else null
client.addTracksToQueue(channelId, trackIds, insertAt)
publishSnapshot()
}
fun jumpToQueueIndex(index: Int) = onMain {
if (!requireSignedIn("Sign in to control rooms")) return@onMain
if (index !in lastQueue.indices) return@onMain
client.sendJump(index)
lastStatus = "Jumping"
publishSnapshot()
}
fun removeQueueIndex(index: Int) = onMain {
if (!requireSignedIn("Sign in to change the room queue")) return@onMain
val channelId = currentChannelId ?: run {
lastStatus = "No room"
publishSnapshot()
return@onMain
}
if (index !in lastQueue.indices) return@onMain
client.removeTracksFromQueue(channelId, listOf(index))
publishSnapshot()
}
override fun onStatus(message: String) {
onMain {
if (sourceMode == PlaybackSourceMode.LIBRARY && message == "Disconnected") {
lastStatus = if (libraryLoaded) "Library" else "Loading library"
publishSnapshot()
return@onMain
}
lastStatus = message
if (message == "Disconnected" && sourceMode == PlaybackSourceMode.RADIO && !intentionalDisconnect) {
scheduleReconnect()
}
publishSnapshot()
}
}
override fun onDebug(message: String) {
onMain {
addDebugEvent(message)
publishSnapshot()
}
}
override fun onError(message: String) {
onMain {
if (sourceMode == PlaybackSourceMode.LIBRARY && intentionalDisconnect) {
lastStatus = if (libraryLoaded) "Library" else "Loading library"
addDebugEvent("local mode ignored websocket error: $message")
publishSnapshot()
return@onMain
}
lastStatus = if (
!hasSignedInUser() &&
(message.contains("401") || message.contains("403") || message.contains("denied", ignoreCase = true))
) {
"Sign in required"
} else {
message
}
addDebugEvent("ERROR $message")
if (sourceMode == PlaybackSourceMode.RADIO && !intentionalDisconnect) {
scheduleReconnect()
}
publishSnapshot()
}
}
override fun onChannels(channels: List<ChannelInfo>) {
onMain {
if (!hasSignedInUser()) {
lastStatus = "Sign in required"
publishSnapshot()
return@onMain
}
lastChannels = channels
val target = currentChannelId
?: channels.firstOrNull { it.isDefault }?.id
?: channels.firstOrNull()?.id
if (target != null && currentChannelId == null && sourceMode == PlaybackSourceMode.RADIO) {
joinChannel(target)
} else {
publishSnapshot()
}
}
}
override fun onLibrary(tracks: List<Track>) {
onMain {
libraryTracks = tracks
libraryLoaded = true
if (sourceMode == PlaybackSourceMode.LIBRARY && lastTrackTitle == "No track") {
lastStatus = "Library ready"
}
publishSnapshot()
}
}
override fun onPlaylists(playlists: PlaylistBundle) {
onMain {
myPlaylists = playlists.mine
sharedPlaylists = playlists.shared
playlistsLoaded = true
val all = allPlaylists()
if (selectedPlaylistId == null || all.none { it.id == selectedPlaylistId }) {
selectedPlaylistId = all.firstOrNull()?.id
selectedPlaylist = null
}
selectedPlaylistId?.let { client.loadPlaylist(it) }
publishSnapshot()
}
}
override fun onPlaylistDetail(playlist: Playlist) {
onMain {
selectedPlaylist = playlist
selectedPlaylistId = playlist.id
publishSnapshot()
}
}
override fun onSwitched(channelId: String) {
onMain {
currentChannelId = channelId
lastStatus = "Tuned"
publishSnapshot()
}
}
override fun onAuth(user: UserSession?) {
onMain {
currentUser = user
authState = when {
user == null -> AuthState.SIGNED_OUT
user.isGuest -> AuthState.GUEST
else -> AuthState.SIGNED_IN
}
if (!hasSignedInUser()) {
intentionalDisconnect = true
client.close()
player.pause()
lastPaused = true
lastStatus = if (authState == AuthState.GUEST) "Sign in required" else "Signed out"
addDebugEvent("auth blocked guest=${user?.isGuest == true}")
publishSnapshot()
return@onMain
}
val signedInUser = currentUser ?: return@onMain
intentionalDisconnect = false
reconnectAttempts = 0
lastStatus = "Signed in as ${signedInUser.username}"
addDebugEvent("auth user=${signedInUser.username}")
publishSnapshot()
client.loadChannels()
if (!libraryLoaded) client.loadLibrary()
if (!playlistsLoaded) client.loadPlaylists()
}
}
override fun onChannelState(state: ChannelState) {
onMain {
if (sourceMode != PlaybackSourceMode.RADIO) return@onMain
reconnectAttempts = 0
mainHandler.removeCallbacks(reconnectRunnable)
currentChannelId = state.channelId.ifBlank { currentChannelId }
lastChannelName = state.channelName.ifBlank { "Channel" }
lastPaused = state.paused
playbackMode = state.playbackMode.ifBlank { playbackMode }
if (state.queue != null) {
lastQueue = state.queue
lastQueueLoaded = true
}
lastCurrentIndex = state.currentIndex
lastServerTimestampMs = (state.currentTimestamp * 1000.0).toLong().coerceAtLeast(0L)
lastStateRealtimeMs = SystemClock.elapsedRealtime()
val track = state.track
if (track == null) {
currentTrackId = null
lastTrackTitle = "No track"
lastTrackDuration = 0.0
player.pause()
publishSnapshot()
return@onMain
}
lastTrackTitle = track.title
lastTrackDuration = track.duration
val targetPositionMs = lastServerTimestampMs
if (currentTrackId != track.id) {
currentTrackId = track.id
player.setMediaItem(buildMediaItem(track), targetPositionMs)
player.prepare()
if (state.paused) player.pause() else player.play()
publishSnapshot()
return@onMain
}
val driftMs = abs(player.currentPosition - targetPositionMs)
if (driftMs >= 2000L) {
player.seekTo(targetPositionMs)
}
if (state.paused && player.isPlaying) {
player.pause()
} else if (!state.paused && !player.isPlaying) {
player.play()
}
publishSnapshot()
}
}
private fun tickPlayback() {
if (sourceMode == PlaybackSourceMode.RADIO &&
currentTrackId != null &&
!lastPaused &&
player.playbackState == Player.STATE_READY
) {
val driftMs = player.currentPosition - expectedPositionMs()
if (abs(driftMs) >= 2000L) {
player.seekTo(expectedPositionMs())
addDebugEvent("service drift correction ${driftMs}ms")
}
}
if (sourceMode == PlaybackSourceMode.LIBRARY) {
lastPaused = !player.isPlaying
if (player.duration > 0 && lastTrackDuration <= 0.0) {
lastTrackDuration = player.duration / 1000.0
}
}
publishSnapshot()
}
private fun handlePlaybackEnded() {
if (sourceMode != PlaybackSourceMode.LIBRARY) return
when (playbackMode) {
"repeat-one" -> {
player.seekTo(0L)
player.play()
}
"repeat-all", "shuffle" -> {
if (libraryTracks.isNotEmpty()) playLibraryTrack(nextLibraryIndex())
}
else -> {
lastPaused = true
publishSnapshot()
}
}
}
private fun scheduleReconnect() {
if (currentChannelId == null) return
mainHandler.removeCallbacks(reconnectRunnable)
val delayMs = (3000L * (reconnectAttempts + 1)).coerceAtMost(30000L)
addDebugEvent("schedule reconnect in ${delayMs}ms")
mainHandler.postDelayed(reconnectRunnable, delayMs)
}
private fun jumpRadio(delta: Int) {
if (lastQueue.isEmpty()) return
val target = (lastCurrentIndex + delta + lastQueue.size) % lastQueue.size
client.sendJump(target)
}
private fun applyLocalPlaybackMode() {
player.repeatMode = if (playbackMode == "repeat-one") {
Player.REPEAT_MODE_ONE
} else {
Player.REPEAT_MODE_OFF
}
player.shuffleModeEnabled = playbackMode == "shuffle"
}
private fun nextLibraryIndex(): Int {
if (libraryTracks.isEmpty()) return -1
if (playbackMode == "shuffle" && libraryTracks.size > 1) return randomLibraryIndex()
return if (localLibraryIndex < 0) 0 else (localLibraryIndex + 1) % libraryTracks.size
}
private fun randomLibraryIndex(): Int {
if (libraryTracks.size <= 1) return 0
var next = Random.nextInt(libraryTracks.size)
while (next == localLibraryIndex) {
next = Random.nextInt(libraryTracks.size)
}
return next
}
private fun modeLabel(mode: String): String {
return when (mode) {
"once" -> "LOOP OFF"
"repeat-all" -> "LOOP ALL"
"repeat-one" -> "LOOP ONE"
"shuffle" -> "SHUFFLE"
else -> mode.uppercase()
}
}
private fun allPlaylists(): List<Playlist> {
return myPlaylists + sharedPlaylists
}
private fun hasSignedInUser(): Boolean {
return authState == AuthState.SIGNED_IN && currentUser?.isSignedIn == true
}
private fun requireSignedIn(message: String): Boolean {
if (hasSignedInUser()) return true
lastStatus = message
addDebugEvent("auth required")
publishSnapshot()
return false
}
private fun buildMediaItem(track: Track): MediaItem {
val metadata = MediaMetadata.Builder()
.setTitle(track.title)
.setArtist(if (sourceMode == PlaybackSourceMode.RADIO) lastChannelName else "Blastoise Library")
.build()
return MediaItem.Builder()
.setMediaId(track.id)
.setUri(SessionStore.trackUrl(track.id))
.setMediaMetadata(metadata)
.build()
}
private fun expectedPositionMs(): Long {
if (sourceMode == PlaybackSourceMode.LIBRARY) return player.currentPosition
if (lastStateRealtimeMs == 0L) return 0L
return if (lastPaused) {
lastServerTimestampMs
} else {
lastServerTimestampMs + (SystemClock.elapsedRealtime() - lastStateRealtimeMs)
}
}
private fun publishSnapshot() {
val expectedMs = expectedPositionMs()
val playerMs = player.currentPosition
val driftMs = if (sourceMode == PlaybackSourceMode.RADIO && currentTrackId != null) playerMs - expectedMs else 0L
PlaybackBridge.publish(
PlaybackSnapshot(
sourceMode = sourceMode,
authState = authState,
currentUser = currentUser,
status = lastStatus,
channels = lastChannels,
libraryTracks = libraryTracks,
libraryLoaded = libraryLoaded,
myPlaylists = myPlaylists,
sharedPlaylists = sharedPlaylists,
playlistsLoaded = playlistsLoaded,
selectedPlaylistId = selectedPlaylistId,
selectedPlaylist = selectedPlaylist,
currentChannelId = currentChannelId,
currentTrackId = currentTrackId,
localLibraryIndex = localLibraryIndex,
currentRoomListeners = lastChannels.firstOrNull { it.id == currentChannelId }?.listeners.orEmpty(),
paused = lastPaused,
queue = lastQueue,
queueLoaded = lastQueueLoaded,
currentIndex = lastCurrentIndex,
playbackMode = playbackMode,
channelName = lastChannelName,
trackTitle = lastTrackTitle,
trackDuration = lastTrackDuration,
serverTimestampMs = lastServerTimestampMs,
stateRealtimeMs = lastStateRealtimeMs,
expectedPositionMs = expectedMs,
playerPositionMs = playerMs,
driftMs = driftMs,
playbackState = playbackStateLabel(),
isPlaying = player.isPlaying,
debugEvents = debugEvents.toList(),
)
)
}
private fun playbackStateLabel(): String {
return when (player.playbackState) {
Player.STATE_IDLE -> "idle"
Player.STATE_BUFFERING -> "buffering"
Player.STATE_READY -> "ready"
Player.STATE_ENDED -> "ended"
else -> "none"
}
}
private fun addDebugEvent(message: String) {
val stamp = android.text.format.DateFormat.format("HH:mm:ss", System.currentTimeMillis()).toString()
debugEvents.add("$stamp $message")
while (debugEvents.size > 8) debugEvents.removeAt(0)
}
private fun onMain(block: () -> Unit) {
if (Looper.myLooper() == Looper.getMainLooper()) {
block()
} else {
mainHandler.post(block)
}
}
}

View File

@ -1,87 +0,0 @@
package com.peterino.blastoise
import android.os.Handler
import android.os.Looper
enum class PlaybackSourceMode {
RADIO,
LIBRARY,
}
enum class AuthState {
CHECKING,
SIGNED_OUT,
GUEST,
SIGNED_IN,
}
data class PlaybackSnapshot(
val sourceMode: PlaybackSourceMode = PlaybackSourceMode.RADIO,
val authState: AuthState = AuthState.CHECKING,
val currentUser: UserSession? = null,
val status: String = "Starting",
val channels: List<ChannelInfo> = emptyList(),
val libraryTracks: List<Track> = emptyList(),
val libraryLoaded: Boolean = false,
val myPlaylists: List<Playlist> = emptyList(),
val sharedPlaylists: List<Playlist> = emptyList(),
val playlistsLoaded: Boolean = false,
val selectedPlaylistId: String? = null,
val selectedPlaylist: Playlist? = null,
val currentChannelId: String? = null,
val currentTrackId: String? = null,
val localLibraryIndex: Int = -1,
val currentRoomListeners: List<String> = emptyList(),
val paused: Boolean = true,
val queue: List<Track> = emptyList(),
val queueLoaded: Boolean = false,
val currentIndex: Int = 0,
val playbackMode: String = "repeat-all",
val channelName: String = "No channel",
val trackTitle: String = "No track",
val trackDuration: Double = 0.0,
val serverTimestampMs: Long = 0L,
val stateRealtimeMs: Long = 0L,
val expectedPositionMs: Long = 0L,
val playerPositionMs: Long = 0L,
val driftMs: Long = 0L,
val playbackState: String = "none",
val isPlaying: Boolean = false,
val debugEvents: List<String> = emptyList(),
)
interface PlaybackSnapshotListener {
fun onPlaybackSnapshot(snapshot: PlaybackSnapshot)
}
object PlaybackBridge {
private val mainHandler = Handler(Looper.getMainLooper())
private val listeners = linkedSetOf<PlaybackSnapshotListener>()
@Volatile
var service: PlaybackService? = null
@Volatile
var snapshot: PlaybackSnapshot = PlaybackSnapshot()
private set
fun register(listener: PlaybackSnapshotListener) {
mainHandler.post {
listeners.add(listener)
listener.onPlaybackSnapshot(snapshot)
}
}
fun unregister(listener: PlaybackSnapshotListener) {
mainHandler.post {
listeners.remove(listener)
}
}
fun publish(next: PlaybackSnapshot) {
mainHandler.post {
snapshot = next
listeners.toList().forEach { it.onPlaybackSnapshot(next) }
}
}
}

View File

@ -1,67 +0,0 @@
package com.peterino.blastoise
import android.content.Context
import java.net.URI
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
object SessionStore {
const val defaultServerBaseUrl = "http://mhsgroove.peterino.com:3001"
const val userAgent = "BlastoiseAndroid/0.1"
@Volatile
var serverBaseUrl: String = defaultServerBaseUrl
@Volatile
var cookieHeader: String = ""
@Volatile
var themeKey: String = "seraph"
fun load(context: Context) {
val prefs = context.getSharedPreferences("blastoise", Context.MODE_PRIVATE)
serverBaseUrl = prefs.getString("serverBaseUrl", defaultServerBaseUrl) ?: defaultServerBaseUrl
if (serverBaseUrl.isBlank()) {
serverBaseUrl = defaultServerBaseUrl
}
cookieHeader = prefs.getString("cookieHeader", "") ?: ""
themeKey = prefs.getString("themeKey", "seraph") ?: "seraph"
}
fun save(context: Context) {
context.getSharedPreferences("blastoise", Context.MODE_PRIVATE)
.edit()
.putString("serverBaseUrl", serverBaseUrl)
.putString("cookieHeader", cookieHeader)
.putString("themeKey", themeKey)
.apply()
}
fun setBaseUrl(input: String) {
val trimmed = input.trim().trimEnd('/')
serverBaseUrl = when {
trimmed.isEmpty() -> defaultServerBaseUrl
trimmed.startsWith("http://") || trimmed.startsWith("https://") -> trimmed
else -> "http://$trimmed"
}
}
fun httpUrl(path: String): String {
return serverBaseUrl.trimEnd('/') + path
}
fun wsUrl(channelId: String): String {
val uri = URI(serverBaseUrl)
val scheme = if (uri.scheme == "https") "wss" else "ws"
val authority = uri.rawAuthority
return "$scheme://$authority/api/channels/${encodePath(channelId)}/ws"
}
fun trackUrl(trackId: String): String {
return httpUrl("/api/tracks/${encodePath(trackId)}")
}
private fun encodePath(value: String): String {
return URLEncoder.encode(value, StandardCharsets.UTF_8.toString()).replace("+", "%20")
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.9 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

View File

@ -1,12 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M4,10L7,4L10,8L14,8L17,4L20,10L20,17L17,20L7,20L4,17Z" />
<path
android:fillColor="#FFFFFFFF"
android:pathData="M2,14L6,14L6,16L2,16ZM18,14L22,14L22,16L18,16ZM3,18L7,17L7,19L3,20ZM17,17L21,18L21,20L17,19Z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M1,9l2,2c4.97,-4.97 13.03,-4.97 18,0l2,-2C16.93,2.93 7.08,2.93 1,9zM9,17l3,3 3,-3c-1.65,-1.66 -4.34,-1.66 -6,0zM5,13l2,2c2.76,-2.76 7.24,-2.76 10,0l2,-2C15.14,9.14 8.87,9.14 5,13z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M8,4h8l1,2h4v2H3V6h4l1,-2zM5,10h14l-1,10H6L5,10zM9,12v6h2v-6H9zM13,12v6h2v-6h-2z" />
</vector>

View File

@ -1,16 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#00000000"
android:pathData="M5,6.8C5,4.7 8.1,3 12,3C15.9,3 19,4.7 19,6.8C19,8.9 15.9,10.6 12,10.6C8.1,10.6 5,8.9 5,6.8Z"
android:strokeColor="#FFFFFFFF"
android:strokeLineCap="square"
android:strokeLineJoin="miter"
android:strokeWidth="2" />
<path
android:fillColor="#FFFFFFFF"
android:pathData="M7,13L11,13L11,21L7,21ZM13,13L17,13L17,21L13,21ZM4,15L2,18L5,18ZM20,15L22,18L19,18Z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M10,4l2,2h8v14H4V4h6zM6,8v10h12V8H6zM14,10v4.6c0,1.32 -1.08,2.4 -2.4,2.4S9.2,15.92 9.2,14.6s1.08,-2.4 2.4,-2.4c0.35,0 0.69,0.08 1,0.22V10h1.4z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M6,5h4v14H6V5zM14,5h4v14h-4V5z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M8,5a3,3 0,1 0,0 6a3,3 0,0 0,0 -6zM16.5,6a2.5,2.5 0,1 0,0 5a2.5,2.5 0,0 0,0 -5zM8,13c-3.3,0 -6,1.8 -6,4v2h12v-2c0,-2.2 -2.7,-4 -6,-4zM16.5,13c-0.9,0 -1.75,0.14 -2.5,0.4c1.25,0.95 2,2.18 2,3.6v2h6v-1.7c0,-2.02 -2.45,-3.3 -5.5,-3.3z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M8,5v14l11,-7L8,5z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M4,5h9v2H4V5zM4,9h9v2H4V9zM4,13h6v2H4v-2zM15,8l6,4 -6,4v-3h-3v-2h3V8z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M4,5h12v2H4V5zM4,10h12v2H4v-2zM4,15h8v2H4v-2zM17,14l5,3 -5,3v-6z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M11,3h2v10h-2V3zM7.05,6.05l1.42,1.42A6,6 0,1 0,15.53 7.47l1.42,-1.42A8,8 0,1 1,7.05 6.05z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M4,5h14v2H4V5zM4,10h14v2H4v-2zM4,15h10v2H4v-2zM18,14l4,2.5 -4,2.5v-5z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M3,5h12v2H3V5zM3,10h12v2H3v-2zM3,15h8v2H3v-2zM18,11v3h3v2h-3v3h-2v-3h-3v-2h3v-3h2z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M3.2,6.2L2.4,4.4 12,1l9.6,3.4 -0.8,1.8L12,3.1 3.2,6.2zM4,8h16v12H4V8zM6,10v8h12v-8H6zM8,12h5v2H8v-2zM8,15h8v2H8v-2z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M7,7h11v3l4,-4 -4,-4v3H5v6h2V7zM17,17H6v-3l-4,4 4,4v-3h13v-6h-2v4z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M10,4a6,6 0,1 0,0 12a6,6 0,0 0,0 -12zM10,6a4,4 0,1 1,0 8a4,4 0,0 1,0 -8zM15,15l5,5 -1.5,1.5 -5,-5L15,15z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M11,6V3L5,8l6,5V9c3.31,0 6,2.69 6,6 0,1.3 -0.41,2.5 -1.12,3.48l1.5,1.5C18.39,18.62 19,16.9 19,15c0,-4.97 -4.03,-9 -8,-9zM8.8,16H7v-3.2l-0.9,0.7 -0.8,-1.1 2,-1.4h1.5v5zM14.2,12.5h-2.4v0.9c0.28,-0.13 0.58,-0.2 0.95,-0.2 1.2,0 2,0.83 2,1.93 0,1.24 -0.93,2.07 -2.35,2.07 -1,0 -1.75,-0.33 -2.27,-0.98l1.03,-0.93c0.33,0.37 0.68,0.55 1.17,0.55 0.5,0 0.82,-0.28 0.82,-0.72 0,-0.45 -0.34,-0.72 -0.87,-0.72 -0.37,0 -0.68,0.12 -0.98,0.34l-0.95,-0.47 0.18,-3.05h3.67v1.28z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M13,6V3l6,5 -6,5V9c-3.31,0 -6,2.69 -6,6 0,1.3 0.41,2.5 1.12,3.48l-1.5,1.5C5.61,18.62 5,16.9 5,15c0,-4.97 4.03,-9 8,-9zM9.8,16H8v-3.2l-0.9,0.7 -0.8,-1.1 2,-1.4h1.5v5zM15.2,12.5h-2.4v0.9c0.28,-0.13 0.58,-0.2 0.95,-0.2 1.2,0 2,0.83 2,1.93 0,1.24 -0.93,2.07 -2.35,2.07 -1,0 -1.75,-0.33 -2.27,-0.98l1.03,-0.93c0.33,0.37 0.68,0.55 1.17,0.55 0.5,0 0.82,-0.28 0.82,-0.72 0,-0.45 -0.34,-0.72 -0.87,-0.72 -0.37,0 -0.68,0.12 -0.98,0.34l-0.95,-0.47 0.18,-3.05h3.67v1.28z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M10.59,9.17L5.41,4 4,5.41l5.17,5.17 1.42,-1.41zM14.5,4l2.04,2.04L4,18.59 5.41,20 17.96,7.46 20,9.5V4h-5.5zM14.83,13.41l-1.41,1.41 3.13,3.13L14.5,20H20v-5.5l-2.04,2.04 -3.13,-3.13z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M6,18l8.5,-6L6,6v12zM16,6v12h2V6h-2z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M18,6l-8.5,6L18,18V6zM6,6v12h2V6H6z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="480dp"
android:height="480dp"
android:viewportWidth="480"
android:viewportHeight="480">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M20,0 L132,0 L132,5 L32,5 L32,15 L10,15 L10,72 L0,72 L0,20 L20,20 Z M348,0 L460,0 L460,20 L480,20 L480,72 L470,72 L470,15 L448,15 L448,5 L348,5 Z M0,408 L10,408 L10,465 L32,465 L32,475 L132,475 L132,480 L20,480 L20,460 L0,460 Z M470,408 L480,408 L480,460 L460,460 L460,480 L348,480 L348,475 L448,475 L448,465 L470,465 Z M0,142 L8,142 L8,210 L0,210 Z M472,156 L480,156 L480,228 L472,228 Z M0,284 L8,284 L8,338 L0,338 Z M472,304 L480,304 L480,358 L472,358 Z M196,0 L284,0 L284,5 L196,5 Z M196,475 L284,475 L284,480 L196,480 Z" />
</vector>

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="480dp"
android:height="480dp"
android:viewportWidth="480"
android:viewportHeight="480">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M240,12 L256,28 L240,44 L224,28 Z M36,20 L104,20 L90,30 L36,30 Z M444,20 L376,20 L390,30 L444,30 Z M36,450 L104,450 L90,460 L36,460 Z M444,450 L376,450 L390,460 L444,460 Z M20,58 L30,58 L30,112 L20,112 Z M450,58 L460,58 L460,112 L450,112 Z M20,368 L30,368 L30,422 L20,422 Z M450,368 L460,368 L460,422 L450,422 Z" />
</vector>

View File

@ -1,3 +0,0 @@
<resources>
<string name="app_name">Blastoise</string>
</resources>

View File

@ -1,9 +0,0 @@
<resources>
<style name="AppTheme" parent="@android:style/Theme.Material.NoActionBar">
<item name="android:fontFamily">sans</item>
<item name="android:windowLightStatusBar">false</item>
<item name="android:statusBarColor">#101418</item>
<item name="android:navigationBarColor">#101418</item>
<item name="android:colorAccent">#58A6FF</item>
</style>
</resources>

View File

@ -1,3 +0,0 @@
plugins {
id("com.android.application") version "9.2.0" apply false
}

View File

@ -1,3 +0,0 @@
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
android.useAndroidX=true
android.nonTransitiveRClass=true

View File

@ -1,7 +0,0 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -1,248 +0,0 @@
#!/bin/sh
#
# Copyright © 2015 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
"$@"
# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

View File

@ -1,98 +0,0 @@
@REM Copyright (c) Meta Platforms, Inc. and affiliates.
@REM
@REM This source code is licensed under the MIT license found in the
@REM LICENSE file in the root directory of this source tree.
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:execute
@rem Setup the command line
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View File

@ -1,18 +0,0 @@
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "BlastoiseNative"
include(":app")

View File

@ -1,7 +1,7 @@
@echo off @echo off
echo ============================================ echo ============================================
echo Blastoise Native Android - Dev Setup echo MusicRoom Android - Dev Setup
echo Installs all prerequisites via Scoop echo Installs all prerequisites via Scoop
echo ============================================ echo ============================================
echo. echo.
@ -35,10 +35,16 @@ echo Done.
echo. echo.
:: Step 3: Install tools :: Step 3: Install tools
echo [3/5] Installing JDK 17 and Android CLI tools... echo [3/5] Installing Node.js, JDK 17, Android CLI tools, and Gradle...
echo This may take several minutes. echo This may take several minutes.
echo. echo.
call scoop install nodejs-lts
if errorlevel 1 (
echo ERROR: Failed to install Node.js
pause & exit /b 1
)
call scoop install temurin17-jdk call scoop install temurin17-jdk
if errorlevel 1 ( if errorlevel 1 (
echo ERROR: Failed to install JDK 17 echo ERROR: Failed to install JDK 17
@ -51,6 +57,12 @@ if errorlevel 1 (
pause & exit /b 1 pause & exit /b 1
) )
call scoop install gradle
if errorlevel 1 (
echo ERROR: Failed to install Gradle
pause & exit /b 1
)
echo All tools installed. echo All tools installed.
echo. echo.
@ -105,16 +117,8 @@ if not exist "%SDKMANAGER%" (
echo Accepting licenses... echo Accepting licenses...
echo y | call "%SDKMANAGER%" --licenses >nul 2>&1 echo y | call "%SDKMANAGER%" --licenses >nul 2>&1
echo Installing platform-tools, emulator, android-36, build-tools-36, and an API 36 system image... echo Installing platform-tools, android-34, build-tools-34...
call "%SDKMANAGER%" "platform-tools" "emulator" "platforms;android-36" "build-tools;36.0.0" "system-images;android-36;google_apis;x86_64" call "%SDKMANAGER%" "platform-tools" "platforms;android-34" "build-tools;34.0.0"
echo Done.
echo.
echo Creating CLI emulator if needed...
avdmanager list avd | findstr "Medium_Phone_API_36" >nul
if errorlevel 1 (
echo no | avdmanager create avd -n Medium_Phone_API_36 -k "system-images;android-36;google_apis;x86_64" -d "medium_phone"
)
echo Done. echo Done.
echo. echo.
@ -123,10 +127,10 @@ echo ============================================
echo Setup complete! echo Setup complete!
echo. echo.
echo Tools installed via Scoop: echo Tools installed via Scoop:
echo JDK 17, Android CLI echo Node.js LTS, JDK 17, Android CLI, Gradle
echo. echo.
echo Android SDK installed headlessly: echo Android SDK installed headlessly:
echo platform-tools, emulator, android-36, build-tools-36 echo platform-tools, android-34, build-tools-34
echo. echo.
echo Environment: echo Environment:
echo JAVA_HOME = %JAVA_HOME% echo JAVA_HOME = %JAVA_HOME%

View File

@ -1,7 +1,7 @@
@echo off @echo off
echo ============================================ echo ============================================
echo Blastoise Native Android - Run App echo MusicRoom Android - Run App
echo ============================================ echo ============================================
echo. echo.
@ -9,14 +9,13 @@ echo.
set "PATH=%PATH%;%USERPROFILE%\scoop\shims" set "PATH=%PATH%;%USERPROFILE%\scoop\shims"
set "JAVA_HOME=%USERPROFILE%\scoop\apps\temurin17-jdk\current" set "JAVA_HOME=%USERPROFILE%\scoop\apps\temurin17-jdk\current"
set "ANDROID_HOME=%LOCALAPPDATA%\Android\Sdk" set "ANDROID_HOME=%LOCALAPPDATA%\Android\Sdk"
set "PATH=%PATH%;%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\emulator;%ANDROID_HOME%\cmdline-tools\latest\bin" set "PATH=%PATH%;%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\emulator"
set "PROJECT_DIR=%~dp0BlastoiseNative"
:: Start emulator if not already running :: Start emulator if not already running
adb devices 2>nul | findstr "emulator" >nul adb devices 2>nul | findstr "emulator" >nul
if errorlevel 1 ( if errorlevel 1 (
echo Starting emulator... echo Starting emulator...
start "" "%ANDROID_HOME%\emulator\emulator.exe" -avd Medium_Phone_API_36 start "" "%ANDROID_HOME%\emulator\emulator.exe" -avd Medium_Phone_API_36.0
echo Waiting for emulator to boot... echo Waiting for emulator to boot...
adb wait-for-device adb wait-for-device
:wait_boot :wait_boot
@ -29,24 +28,16 @@ if errorlevel 1 (
echo. echo.
) )
echo Building and installing Blastoise... :: Build and install the app
::: Build, install, and run the app (starts Metro automatically)
echo Building and installing MusicRoom...
echo. echo.
pushd "%PROJECT_DIR%" call bunx react-native run-android
call gradlew.bat :app:installDebug
if errorlevel 1 (
popd
echo.
echo Build or install failed.
pause
exit /b 1
)
popd
echo Launching app...
adb shell am start -n com.peterino.blastoise/.MainActivity
echo. echo.
echo ============================================ echo ============================================
echo App is installed and launched. echo App is running. Leave this window open --
echo Metro is serving the JS bundle.
echo Press Ctrl+C to stop Metro when done.
echo ============================================ echo ============================================
pause pause

View File

@ -1,147 +0,0 @@
@echo off
setlocal EnableExtensions EnableDelayedExpansion
echo ============================================
echo Blastoise Native Android - Build and Launch
echo ============================================
echo.
set "ROOT_DIR=%~dp0"
set "PROJECT_DIR=%ROOT_DIR%android\BlastoiseNative"
set "APK_PATH=%PROJECT_DIR%\app\build\outputs\apk\debug\app-debug.apk"
set "PACKAGE_ACTIVITY=com.peterino.blastoise/.MainActivity"
if "%~1"=="" (
set "AVD_NAME=Medium_Phone_API_36.0"
) else (
set "AVD_NAME=%~1"
)
set "PATH=%USERPROFILE%\scoop\shims;%PATH%"
if not defined JAVA_HOME (
if exist "%USERPROFILE%\scoop\apps\temurin17-jdk\current" (
set "JAVA_HOME=%USERPROFILE%\scoop\apps\temurin17-jdk\current"
)
)
if not defined ANDROID_HOME set "ANDROID_HOME=%LOCALAPPDATA%\Android\Sdk"
set "PATH=%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\emulator;%ANDROID_HOME%\cmdline-tools\latest\bin;%PATH%"
set "ADB_EXE=%ANDROID_HOME%\platform-tools\adb.exe"
set "EMULATOR_EXE=%ANDROID_HOME%\emulator\emulator.exe"
if not exist "%PROJECT_DIR%\gradlew.bat" (
echo ERROR: Native Android project not found:
echo %PROJECT_DIR%
goto :fail
)
if not exist "%ADB_EXE%" (
echo ERROR: adb not found at:
echo %ADB_EXE%
echo Run android\dev-setup.bat first.
goto :fail
)
if not exist "%EMULATOR_EXE%" (
echo ERROR: Android emulator not found at:
echo %EMULATOR_EXE%
echo Run android\dev-setup.bat first.
goto :fail
)
"%EMULATOR_EXE%" -list-avds | findstr /X /C:"%AVD_NAME%" >nul
if errorlevel 1 (
echo ERROR: AVD "%AVD_NAME%" was not found.
echo.
echo Available AVDs:
"%EMULATOR_EXE%" -list-avds
echo.
echo Pass a different AVD name as the first argument, for example:
echo run-android-native.bat Your_AVD_Name
goto :fail
)
echo Building debug APK...
pushd "%PROJECT_DIR%"
call gradlew.bat :app:assembleDebug
if errorlevel 1 (
popd
echo.
echo ERROR: Gradle build failed.
goto :fail
)
popd
if not exist "%APK_PATH%" (
echo ERROR: APK was not created:
echo %APK_PATH%
goto :fail
)
"%ADB_EXE%" devices | findstr /R /C:"emulator-[0-9][0-9]*[ ]*device" >nul
if errorlevel 1 (
echo.
echo Starting emulator "%AVD_NAME%"...
start "" "%EMULATOR_EXE%" -avd "%AVD_NAME%"
echo Waiting for emulator device...
set /a WAIT_DEVICE_COUNT=0
:wait_device
"%ADB_EXE%" devices | findstr /R /C:"emulator-[0-9][0-9]*[ ]*device" >nul
if errorlevel 1 (
set /a WAIT_DEVICE_COUNT+=1
if !WAIT_DEVICE_COUNT! GEQ 90 (
echo ERROR: Emulator did not appear within 180 seconds.
goto :fail
)
timeout /t 2 /nobreak >nul
goto :wait_device
)
)
echo Waiting for Android boot completion...
set /a WAIT_BOOT_COUNT=0
:wait_boot
set "BOOT_DONE="
for /f "delims=" %%b in ('"%ADB_EXE%" shell getprop sys.boot_completed 2^>nul') do set "BOOT_DONE=%%b"
if not "%BOOT_DONE%"=="1" (
set /a WAIT_BOOT_COUNT+=1
if !WAIT_BOOT_COUNT! GEQ 120 (
echo ERROR: Android did not finish booting within 240 seconds.
goto :fail
)
timeout /t 2 /nobreak >nul
goto :wait_boot
)
echo Installing APK...
"%ADB_EXE%" install -r "%APK_PATH%"
if errorlevel 1 (
echo.
echo ERROR: APK install failed.
goto :fail
)
echo Launching app...
"%ADB_EXE%" shell am start -n %PACKAGE_ACTIVITY%
if errorlevel 1 (
echo.
echo ERROR: App launch failed.
goto :fail
)
echo.
echo ============================================
echo Blastoise is built, installed, and running.
echo ============================================
goto :done
:fail
echo.
echo ============================================
echo Failed.
echo ============================================
if not "%NO_PAUSE%"=="1" pause
exit /b 1
:done
if not "%NO_PAUSE%"=="1" pause
exit /b 0