Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b9da43366b | ||
|
|
b96354c40f | ||
|
|
4017c71ca7 |
No files matched your search
@@ -5,6 +5,75 @@ Toutes les évolutions notables de Cachette sont documentées ici.
|
||||
Le format suit [Keep a Changelog](https://keepachangelog.com/fr/1.1.0/)
|
||||
et le versionnage [SemVer](https://semver.org/lang/fr/).
|
||||
|
||||
## [1.5.0] — 2026-09-13
|
||||
|
||||
### Ajouté
|
||||
|
||||
- **Titre en cours par station.** Aucun annuaire ne l'expose : le morceau diffusé est lu dans le
|
||||
flux lui-même. Les douze premières stations affichées sont sondées quatre à la fois, et le
|
||||
résultat conservé — au-delà, le nombre de connexions ne se justifierait plus. Les stations qui
|
||||
n'annoncent rien gardent leur description.
|
||||
- **Guet d'un titre.** On indique un morceau ou un artiste, et l'enregistrement se déclenche dès
|
||||
que la station l'annonce, puis s'arrête à la fin du morceau. Le flux est lu sans être écrit
|
||||
pendant l'attente. Une station sans métadonnées refuse la programmation plutôt que d'attendre
|
||||
indéfiniment.
|
||||
- **Catalogue Underground.** Restreint aux collections `netlabels` et `freemusicarchive`, où tout
|
||||
est publié sous licence libre par les artistes : environ 94 000 morceaux indépendants, que la
|
||||
recherche généraliste noyait.
|
||||
- **Suppression individuelle d'un enregistrement** depuis l'écran Hors ligne : fichier, entrée
|
||||
MediaStore et référence en bibliothèque sont retirés ensemble.
|
||||
|
||||
### Modifié
|
||||
|
||||
- Les enregistrements sont désormais écrits dans `Music/Cachette` et déclarés dans MediaStore
|
||||
(Android 10 et plus) : ils deviennent visibles des autres lecteurs et survivent à la
|
||||
désinstallation. Le scan local ignore ce dossier, faute de quoi chaque enregistrement
|
||||
apparaîtrait deux fois.
|
||||
- Le nom de fichier porte le titre du morceau. En publiant le fichier, MediaStore le relit et
|
||||
réécrit le champ `TITLE` depuis ce nom : l'y inscrire directement ne survivait pas.
|
||||
|
||||
## [1.4.0] — 2026-09-13
|
||||
|
||||
### Ajouté
|
||||
|
||||
- Titrage automatique des enregistrements radio. Le service demande désormais les métadonnées
|
||||
ICY, ce qui fait annoncer par la station le morceau diffusé : l'enregistrement prend son nom,
|
||||
la station passe en sous-titre, et le titre en cours s'affiche pendant la capture comme dans
|
||||
la notification. Ces octets de métadonnées sont retirés avant écriture — laissés dans le
|
||||
fichier, ils le rendraient illisible. Les stations qui n'en diffusent pas gardent le nom
|
||||
« Station — date ».
|
||||
|
||||
### Corrigé
|
||||
|
||||
- Les enregistrements radio n'apparaissaient pas dans l'onglet « Hors ligne », qui ne listait que
|
||||
ce qui porte un état de téléchargement. L'écran annonçait donc un nombre d'éléments disponibles
|
||||
sans réseau en omettant précisément ceux qui l'étaient déjà. La taille affichée additionne
|
||||
maintenant le cache et les enregistrements, et « Tout libérer » précise qu'il ne les touche pas.
|
||||
- Les titres comportant une apostrophe étaient tronqués à la première rencontrée : « Ain't That a
|
||||
Kick in the Head » devenait « Ain ». La valeur est lue jusqu'à la fin du champ ICY.
|
||||
|
||||
## [1.3.0] — 2026-09-13
|
||||
|
||||
### Ajouté
|
||||
|
||||
- **Radio.** Les stations les plus écoutées via l'annuaire communautaire Radio Browser, avec
|
||||
filtres par genre et recherche par nom. Chaque station s'écoute en direct, ou s'enregistre :
|
||||
un service de premier plan copie le flux dans un fichier jusqu'à l'arrêt demandé, et le verse
|
||||
dans la bibliothèque — déjà hors connexion, sans rien à télécharger ensuite.
|
||||
- Relance automatique des téléchargements en échec, après une pause croissante de 5, 15 puis
|
||||
45 secondes. Une relance manuelle repart d'un compteur neuf.
|
||||
|
||||
### Notes
|
||||
|
||||
- Les flux HLS sont écartés de la liste des stations : ils se lisent, mais ne s'enregistrent pas
|
||||
par copie d'octets, et un bouton qui échouerait vaudrait moins que pas de bouton.
|
||||
- La relance et la reprise sur coupure réseau sont deux mécanismes distincts, et c'est voulu :
|
||||
sans réseau, Media3 met le téléchargement en attente et le reprend de lui-même ; la relance,
|
||||
elle, ne traite que les vrais échecs serveur.
|
||||
- Le repli automatique vers une autre source n'a pas été retenu : rien ne garantit qu'un même
|
||||
titre trouvé sur deux catalogues soit le même enregistrement, et l'application téléchargerait
|
||||
silencieusement autre chose que ce qui a été choisi.
|
||||
|
||||
## [1.2.0] — 2026-09-13
|
||||
|
||||
### Ajouté
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
[](https://kotlinlang.org)
|
||||
[](https://developer.android.com/jetpack/compose)
|
||||
[](https://developer.android.com/media/media3)
|
||||
[](VERSION)
|
||||
[](VERSION)
|
||||
[](#-comment-ça-marche)
|
||||
|
||||
Cachette est un lecteur audio et vidéo pensé pour les trajets, les tunnels, les avions et les
|
||||
@@ -41,7 +41,11 @@ sans la moindre connexion.
|
||||
| 🎛️ **Lecture en arrière-plan** | Notification système, casque, Bluetooth, écran verrouillé |
|
||||
| ⏯️ **Contrôles complets** | Lecture, pause, arrêt, piste précédente et suivante, ±15 s |
|
||||
| 🌙 **Thème clair et sombre** | Material 3, palette de marque, contraste garanti |
|
||||
| 🔁 **Reprise automatique** | Un téléchargement coupé repart tout seul via WorkManager |
|
||||
| 🔁 **Reprise automatique** | Réseau coupé : mise en attente et reprise. Échec serveur : relance seule après 5 s, 15 s, 45 s |
|
||||
| 📻 **Radio** | Les stations les plus écoutées, en direct — et enregistrables d'un appui |
|
||||
| 🏷️ **Titrage automatique** | L'enregistrement prend le nom du morceau annoncé par la station |
|
||||
| 👀 **Titre en cours** | Ce que diffuse chaque station, lu dans son flux |
|
||||
| ⏰ **Guet d'un titre** | L'enregistrement se déclenche quand le morceau voulu passe, et s'arrête à sa fin |
|
||||
| 🆕 **Mise à jour intégrée** | Surveille les releases du dépôt, télécharge l'APK en Wi-Fi et propose l'installation |
|
||||
|
||||
---
|
||||
@@ -56,6 +60,8 @@ sans la moindre connexion.
|
||||
| 🏛️ Internet Archive | ✅ | Domaine public / Creative Commons |
|
||||
| 📚 LibriVox | ✅ | Livres audio du domaine public, filtrables par langue |
|
||||
| 🎼 Wikimedia Commons | ✅ | Enregistrements classiques et archives sonores libres |
|
||||
| 📻 Radio | ✅ | Annuaire Radio Browser, direct et enregistrement (flux HLS exclus) |
|
||||
| 🎧 Underground | ✅ | Netlabels et Free Music Archive : ~94 000 morceaux d'artistes indépendants |
|
||||
|
||||
### 🔎 Catalogues évalués puis écartés
|
||||
|
||||
@@ -188,6 +194,22 @@ sortantes sont celles des URL que vous ajoutez et des catalogues que vous interr
|
||||
|
||||
---
|
||||
|
||||
## 📼 Où vont les enregistrements
|
||||
|
||||
À partir d'Android 10, dans `Music/Cachette/`, déclarés dans MediaStore : ils sont donc visibles
|
||||
de tous les lecteurs de l'appareil et survivent à la désinstallation. En deçà, ils restent dans le
|
||||
dossier privé de l'application, MediaStore y réclamant une permission de stockage dont Cachette n'a
|
||||
pas besoin par ailleurs.
|
||||
|
||||
Le nom du fichier porte le titre du morceau — et pas seulement la colonne `TITLE` : en publiant le
|
||||
fichier, MediaStore le relit et réécrit ce champ à partir du nom. Le scan local, lui, ignore
|
||||
`Music/Cachette` pour ne pas afficher deux fois ce que la bibliothèque connaît déjà.
|
||||
|
||||
Chaque enregistrement se supprime individuellement depuis l'écran **Hors ligne** : fichier, entrée
|
||||
MediaStore et référence en bibliothèque partent ensemble. « Tout libérer » ne les touche pas.
|
||||
|
||||
---
|
||||
|
||||
## 🆕 Mises à jour
|
||||
|
||||
Cachette étant distribuée hors magasin, elle surveille elle-même les releases du dépôt.
|
||||
@@ -230,6 +252,7 @@ app/src/main/java/fr/tuxgyver/cachette/
|
||||
├── sources/ Import URL et RSS, catalogues Internet Archive et LibriVox
|
||||
├── offline/ Cache Media3, gestionnaire et service de téléchargement
|
||||
├── playback/ MediaSessionService, ExoPlayer, contrôleur d'interface
|
||||
├── radio/ Annuaire des stations, service d'enregistrement du direct
|
||||
├── update/ Vérification des releases, téléchargement, notification
|
||||
└── ui/ Compose — thème, bibliothèque, hors ligne, playlists, sources,
|
||||
parcours des catalogues, choix des pistes, lecteur
|
||||
|
||||
@@ -59,6 +59,11 @@
|
||||
</intent-filter>
|
||||
</service>
|
||||
|
||||
<service
|
||||
android:name=".radio.RecordingService"
|
||||
android:exported="false"
|
||||
android:foregroundServiceType="dataSync" />
|
||||
|
||||
<provider
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
android:authorities="${applicationId}.fileprovider"
|
||||
|
||||
@@ -9,6 +9,7 @@ import fr.tuxgyver.cachette.library.PlaylistStore
|
||||
import fr.tuxgyver.cachette.offline.DownloadTracker
|
||||
import fr.tuxgyver.cachette.offline.OfflineModule
|
||||
import fr.tuxgyver.cachette.playback.PlayerController
|
||||
import fr.tuxgyver.cachette.radio.RadioDirectory
|
||||
import fr.tuxgyver.cachette.sources.ArchiveOrgCatalog
|
||||
import fr.tuxgyver.cachette.sources.Catalog
|
||||
import fr.tuxgyver.cachette.sources.CatalogId
|
||||
@@ -40,11 +41,17 @@ class AppContainer(context: Context) {
|
||||
val library = LibraryRepository(LocalMediaScanner(context), imported, scope)
|
||||
val playlists = PlaylistStore(context, scope)
|
||||
|
||||
val radio = RadioDirectory()
|
||||
|
||||
val urlImporter = UrlImporter()
|
||||
val rssImporter = RssImporter()
|
||||
|
||||
val catalogs: Map<CatalogId, Catalog> = listOf(
|
||||
ArchiveOrgCatalog(),
|
||||
ArchiveOrgCatalog(
|
||||
id = CatalogId.UNDERGROUND,
|
||||
collections = listOf("netlabels", "freemusicarchive"),
|
||||
),
|
||||
LibriVoxCatalog(rssImporter),
|
||||
WikimediaCatalog(),
|
||||
).associateBy { it.id }
|
||||
|
||||
@@ -55,7 +55,7 @@ class LocalMediaScanner(private val context: Context) {
|
||||
context.contentResolver.query(
|
||||
collection,
|
||||
projection,
|
||||
"${MediaStore.Audio.Media.IS_MUSIC} != 0 OR ${MediaStore.Audio.Media.IS_PODCAST} != 0",
|
||||
audioSelection(),
|
||||
null,
|
||||
null,
|
||||
)?.use { cursor ->
|
||||
@@ -123,6 +123,19 @@ class LocalMediaScanner(private val context: Context) {
|
||||
return entries
|
||||
}
|
||||
|
||||
/**
|
||||
* Musique et podcasts, hors enregistrements de Cachette : ceux-ci sont déjà décrits par la
|
||||
* bibliothèque, et les relire ici les afficherait en double.
|
||||
*/
|
||||
private fun audioSelection(): String {
|
||||
val base = "(${MediaStore.Audio.Media.IS_MUSIC} != 0 OR ${MediaStore.Audio.Media.IS_PODCAST} != 0)"
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
"$base AND ${MediaStore.Audio.Media.RELATIVE_PATH} NOT LIKE 'Music/Cachette%'"
|
||||
} else {
|
||||
base
|
||||
}
|
||||
}
|
||||
|
||||
private fun albumArtUri(albumId: Long): Uri =
|
||||
ContentUris.withAppendedId("content://media/external/audio/albumart".toUri(), albumId)
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ enum class SourceKind(val label: String) {
|
||||
LOCAL("Appareil"),
|
||||
URL("Lien direct"),
|
||||
PODCAST("Podcast"),
|
||||
RECORDING("Enregistrement"),
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,6 +32,6 @@ data class MediaEntry(
|
||||
/** Taille du fichier quand la source l'annonce, pour choisir avant de télécharger. */
|
||||
val sizeBytes: Long? = null,
|
||||
) {
|
||||
/** Un média local est déjà disponible hors connexion : il n'a rien à télécharger. */
|
||||
val isRemote: Boolean get() = source != SourceKind.LOCAL
|
||||
/** Local ou enregistré : le fichier est déjà sur l'appareil, il n'y a rien à télécharger. */
|
||||
val isRemote: Boolean get() = source != SourceKind.LOCAL && source != SourceKind.RECORDING
|
||||
}
|
||||
@@ -28,13 +28,15 @@ data class OfflineState(
|
||||
val state: Int,
|
||||
val percent: Float,
|
||||
val bytesDownloaded: Long,
|
||||
/** Une relance automatique est programmée : l'échec n'est pas définitif. */
|
||||
val retrying: Boolean = false,
|
||||
) {
|
||||
val isComplete: Boolean get() = state == Download.STATE_COMPLETED
|
||||
val isFailed: Boolean get() = state == Download.STATE_FAILED
|
||||
val isFailed: Boolean get() = state == Download.STATE_FAILED && !retrying
|
||||
val isPending: Boolean
|
||||
get() = state == Download.STATE_QUEUED || state == Download.STATE_RESTARTING
|
||||
val isRunning: Boolean get() = state == Download.STATE_DOWNLOADING
|
||||
val isActive: Boolean get() = isPending || isRunning
|
||||
val isActive: Boolean get() = isPending || isRunning || retrying
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,10 +50,13 @@ data class OfflineState(
|
||||
class DownloadTracker(
|
||||
private val context: Context,
|
||||
private val downloadManager: DownloadManager,
|
||||
scope: CoroutineScope,
|
||||
private val scope: CoroutineScope,
|
||||
) {
|
||||
private val indexed = MutableStateFlow<Map<String, OfflineState>>(emptyMap())
|
||||
|
||||
/** Identifiants dont une relance est programmée. */
|
||||
private val retrying = MutableStateFlow(emptySet<String>())
|
||||
|
||||
private val ticker = flow {
|
||||
while (true) {
|
||||
emit(Unit)
|
||||
@@ -60,20 +65,58 @@ class DownloadTracker(
|
||||
}
|
||||
|
||||
val states: StateFlow<Map<String, OfflineState>> =
|
||||
indexed.combine(ticker) { snapshot, _ ->
|
||||
snapshot + downloadManager.currentDownloads.associate { it.request.id to it.toOfflineState() }
|
||||
combine(indexed, ticker, retrying) { snapshot, _, pending ->
|
||||
val live = snapshot +
|
||||
downloadManager.currentDownloads.associate { it.request.id to it.toOfflineState() }
|
||||
live.mapValues { (id, state) -> state.copy(retrying = id in pending) }
|
||||
}.stateIn(scope, SharingStarted.WhileSubscribed(5_000), emptyMap())
|
||||
|
||||
/**
|
||||
* Tentatives déjà consommées par téléchargement.
|
||||
*
|
||||
* Un serveur qui coupe, un réseau qui vacille : l'échec est le plus souvent passager. On
|
||||
* relance donc seul, en espaçant les essais, plutôt que d'attendre un geste de l'utilisateur.
|
||||
* Le compteur vit en mémoire — au redémarrage, WorkManager reprend les téléchargements et le
|
||||
* décompte repart, ce qui est le comportement voulu.
|
||||
*/
|
||||
private val attempts = mutableMapOf<String, Int>()
|
||||
|
||||
private val listener = object : DownloadManager.Listener {
|
||||
override fun onDownloadChanged(manager: DownloadManager, download: Download, finalException: Exception?) {
|
||||
when (download.state) {
|
||||
Download.STATE_COMPLETED -> attempts.remove(download.request.id)
|
||||
Download.STATE_FAILED -> scheduleRetry(download)
|
||||
}
|
||||
scope.launch { reload() }
|
||||
}
|
||||
|
||||
override fun onDownloadRemoved(manager: DownloadManager, download: Download) {
|
||||
attempts.remove(download.request.id)
|
||||
scope.launch { reload() }
|
||||
}
|
||||
}
|
||||
|
||||
/** Relance après une pause croissante : 5 s, 15 s, puis 45 s. Au-delà, l'échec est signalé. */
|
||||
private fun scheduleRetry(download: Download) {
|
||||
val id = download.request.id
|
||||
val used = attempts.getOrDefault(id, 0)
|
||||
if (used >= RETRY_DELAYS_MS.size) return
|
||||
|
||||
attempts[id] = used + 1
|
||||
retrying.value = retrying.value + id
|
||||
scope.launch {
|
||||
delay(RETRY_DELAYS_MS[used])
|
||||
retrying.value = retrying.value - id
|
||||
// Ré-ajouter la même requête relance un téléchargement en échec.
|
||||
DownloadService.sendAddDownload(
|
||||
context,
|
||||
CachetteDownloadService::class.java,
|
||||
download.request,
|
||||
false,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
downloadManager.addListener(listener)
|
||||
scope.launch { reload() }
|
||||
@@ -81,6 +124,8 @@ class DownloadTracker(
|
||||
|
||||
fun download(entry: MediaEntry) {
|
||||
if (!entry.isRemote) return
|
||||
// Une relance manuelle repart d'un compteur neuf.
|
||||
attempts.remove(entry.id)
|
||||
val request = DownloadRequest.Builder(entry.id, entry.uri.toUri())
|
||||
.setData(entry.title.toByteArray())
|
||||
.build()
|
||||
@@ -108,6 +153,10 @@ class DownloadTracker(
|
||||
indexed.value = snapshot
|
||||
}
|
||||
|
||||
private companion object {
|
||||
val RETRY_DELAYS_MS = longArrayOf(5_000, 15_000, 45_000)
|
||||
}
|
||||
|
||||
private fun Download.toOfflineState() = OfflineState(
|
||||
id = request.id,
|
||||
state = state,
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package fr.tuxgyver.cachette.radio
|
||||
|
||||
import fr.tuxgyver.cachette.sources.openConnection
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonArray
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import java.net.URLEncoder
|
||||
|
||||
/**
|
||||
* Annuaire des stations, via l'API communautaire Radio Browser (sans clé).
|
||||
*
|
||||
* Les flux HLS sont écartés : ils se lisent, mais ne s'enregistrent pas en copiant les octets,
|
||||
* et proposer un bouton qui échouerait vaudrait moins que ne pas le proposer.
|
||||
*/
|
||||
class RadioDirectory {
|
||||
|
||||
private val json = Json { ignoreUnknownKeys = true }
|
||||
|
||||
/** Les stations les plus écoutées, éventuellement restreintes à un genre. */
|
||||
suspend fun popular(genre: String? = null): Result<List<RadioStation>> =
|
||||
query(buildString {
|
||||
append("?limit=60&hidebroken=true&order=clickcount&reverse=true")
|
||||
genre?.let { append("&tag=${URLEncoder.encode(it, "UTF-8")}") }
|
||||
})
|
||||
|
||||
suspend fun search(name: String): Result<List<RadioStation>> =
|
||||
query("?limit=60&hidebroken=true&order=clickcount&reverse=true" +
|
||||
"&name=${URLEncoder.encode(name.trim(), "UTF-8")}")
|
||||
|
||||
/**
|
||||
* Le morceau diffusé à l'instant par une station.
|
||||
*
|
||||
* Aucun annuaire ne l'expose : il faut ouvrir le flux, lire le premier bloc de métadonnées
|
||||
* ICY, puis refermer. C'est une connexion par station — d'où l'appel par petits paquets et
|
||||
* la mise en cache par l'appelant plutôt qu'un balayage systématique.
|
||||
*/
|
||||
suspend fun nowPlaying(streamUrl: String): String? = withContext(Dispatchers.IO) {
|
||||
runCatching {
|
||||
val connection = openConnection(streamUrl, readTimeoutMs = 8_000).apply {
|
||||
setRequestProperty("Icy-MetaData", "1")
|
||||
}
|
||||
try {
|
||||
if (connection.responseCode !in 200..299) return@runCatching null
|
||||
val interval = connection.getHeaderFieldInt("icy-metaint", 0)
|
||||
if (interval <= 0) return@runCatching null
|
||||
|
||||
connection.inputStream.use { input ->
|
||||
val skip = ByteArray(interval)
|
||||
var filled = 0
|
||||
while (filled < interval) {
|
||||
val read = input.read(skip, filled, interval - filled)
|
||||
if (read < 0) return@runCatching null
|
||||
filled += read
|
||||
}
|
||||
val length = input.read().takeIf { it >= 0 }?.times(16) ?: return@runCatching null
|
||||
if (length == 0) return@runCatching null
|
||||
val meta = ByteArray(length)
|
||||
filled = 0
|
||||
while (filled < length) {
|
||||
val read = input.read(meta, filled, length - filled)
|
||||
if (read < 0) return@runCatching null
|
||||
filled += read
|
||||
}
|
||||
STREAM_TITLE.find(String(meta, Charsets.UTF_8))
|
||||
?.groupValues?.get(1)?.trim()?.takeIf { it.isNotBlank() }
|
||||
}
|
||||
} finally {
|
||||
connection.disconnect()
|
||||
}
|
||||
}.getOrNull()
|
||||
}
|
||||
|
||||
private suspend fun query(suffix: String): Result<List<RadioStation>> = withContext(Dispatchers.IO) {
|
||||
runCatching {
|
||||
val connection = openConnection("$BASE$suffix", readTimeoutMs = 30_000)
|
||||
val body = try {
|
||||
if (connection.responseCode !in 200..299) {
|
||||
throw IllegalStateException("L'annuaire a répondu ${connection.responseCode}")
|
||||
}
|
||||
connection.inputStream.bufferedReader().readText()
|
||||
} finally {
|
||||
connection.disconnect()
|
||||
}
|
||||
|
||||
(json.parseToJsonElement(body) as? JsonArray).orEmpty()
|
||||
.mapNotNull { it as? JsonObject }
|
||||
.filter { it.number("hls") == 0L }
|
||||
.mapNotNull { station ->
|
||||
val url = station.string("url_resolved")?.takeIf { it.startsWith("http") }
|
||||
?: return@mapNotNull null
|
||||
RadioStation(
|
||||
id = station.string("stationuuid") ?: url,
|
||||
name = station.string("name")?.trim() ?: "Station",
|
||||
streamUrl = url,
|
||||
codec = station.string("codec").orEmpty(),
|
||||
bitrate = station.number("bitrate")?.toInt() ?: 0,
|
||||
country = station.string("country").orEmpty(),
|
||||
tags = station.string("tags").orEmpty(),
|
||||
faviconUrl = station.string("favicon")?.takeIf { it.startsWith("http") },
|
||||
)
|
||||
}
|
||||
.distinctBy { it.streamUrl }
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val BASE = "https://de1.api.radio-browser.info/json/stations/search"
|
||||
|
||||
/** Le titre court jusqu'à la fin du champ ICY : les apostrophes y sont fréquentes. */
|
||||
val STREAM_TITLE = Regex("StreamTitle='(.*?)';")
|
||||
|
||||
/** Genres proposés d'emblée, parmi les plus fournis de l'annuaire. */
|
||||
fun JsonArray?.orEmpty(): JsonArray = this ?: JsonArray(emptyList())
|
||||
|
||||
fun JsonObject.string(key: String): String? =
|
||||
(this[key] as? JsonPrimitive)?.content?.takeIf { it.isNotBlank() && it != "null" }
|
||||
|
||||
fun JsonObject.number(key: String): Long? =
|
||||
(this[key] as? JsonPrimitive)?.content?.substringBefore('.')?.toLongOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
/** Genres offerts en filtre rapide. */
|
||||
val RADIO_GENRES = listOf("pop", "rock", "jazz", "classical", "electronic", "news", "chanson")
|
||||
@@ -0,0 +1,20 @@
|
||||
package fr.tuxgyver.cachette.radio
|
||||
|
||||
/** Une station de radio et son flux direct. */
|
||||
data class RadioStation(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val streamUrl: String,
|
||||
val codec: String,
|
||||
val bitrate: Int,
|
||||
val country: String,
|
||||
val tags: String,
|
||||
val faviconUrl: String?,
|
||||
) {
|
||||
val subtitle: String
|
||||
get() = listOfNotNull(
|
||||
country.takeIf { it.isNotBlank() },
|
||||
tags.split(',').firstOrNull()?.trim()?.takeIf { it.isNotBlank() }?.replaceFirstChar(Char::uppercase),
|
||||
bitrate.takeIf { it > 0 }?.let { "$it kbps" },
|
||||
).joinToString(" · ")
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package fr.tuxgyver.cachette.radio
|
||||
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
|
||||
data class RecordingStatus(
|
||||
val station: RadioStation? = null,
|
||||
val startedAt: Long = 0L,
|
||||
val bytes: Long = 0L,
|
||||
/** Titre annoncé par le flux, quand la station en diffuse un. */
|
||||
val currentTitle: String? = null,
|
||||
/** Titre attendu avant de commencer à écrire, si l'enregistrement a été programmé. */
|
||||
val waitingFor: String? = null,
|
||||
) {
|
||||
val isRecording: Boolean get() = station != null
|
||||
val elapsedMs: Long get() = if (startedAt == 0L) 0L else System.currentTimeMillis() - startedAt
|
||||
}
|
||||
|
||||
/**
|
||||
* État de l'enregistrement en cours.
|
||||
*
|
||||
* Seul [RecordingService] y écrit ; l'interface ne fait que l'observer. Un singleton se justifie
|
||||
* ici parce que l'enregistrement est unique par nature : on n'enregistre qu'une station à la fois.
|
||||
*/
|
||||
object Recorder {
|
||||
private val _status = MutableStateFlow(RecordingStatus())
|
||||
val status: StateFlow<RecordingStatus> = _status.asStateFlow()
|
||||
|
||||
internal fun started(station: RadioStation, waitingFor: String? = null) {
|
||||
_status.value = RecordingStatus(station, System.currentTimeMillis(), 0L, waitingFor = waitingFor)
|
||||
}
|
||||
|
||||
/** Le morceau attendu commence : l'écriture démarre maintenant. */
|
||||
internal fun armed(title: String) {
|
||||
_status.value = _status.value.copy(
|
||||
waitingFor = null,
|
||||
currentTitle = title,
|
||||
startedAt = System.currentTimeMillis(),
|
||||
)
|
||||
}
|
||||
|
||||
internal fun progress(bytes: Long) {
|
||||
_status.value = _status.value.copy(bytes = bytes)
|
||||
}
|
||||
|
||||
internal fun title(title: String) {
|
||||
_status.value = _status.value.copy(currentTitle = title)
|
||||
}
|
||||
|
||||
internal fun stopped() {
|
||||
_status.value = RecordingStatus()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,380 @@
|
||||
package fr.tuxgyver.cachette.radio
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.ServiceInfo
|
||||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
import androidx.core.app.NotificationCompat
|
||||
import fr.tuxgyver.cachette.CachetteApp
|
||||
import fr.tuxgyver.cachette.MainActivity
|
||||
import fr.tuxgyver.cachette.R
|
||||
import fr.tuxgyver.cachette.model.MediaEntry
|
||||
import fr.tuxgyver.cachette.model.MediaKind
|
||||
import fr.tuxgyver.cachette.model.SourceKind
|
||||
import fr.tuxgyver.cachette.sources.openConnection
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
* Enregistre un flux radio en copiant ses octets dans un fichier.
|
||||
*
|
||||
* Un direct n'a pas de fin : `DownloadManager`, qui attend une taille et une complétion, ne peut
|
||||
* pas s'en charger. L'enregistrement tourne donc en service de premier plan jusqu'à ce que
|
||||
* l'utilisateur l'arrête, et le fichier obtenu rejoint la bibliothèque comme média local.
|
||||
*/
|
||||
class RecordingService : Service() {
|
||||
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
private var job: Job? = null
|
||||
private var sink: RecordingSink? = null
|
||||
private var sinkTitle: String = ""
|
||||
|
||||
private var station: RadioStation? = null
|
||||
private var firstTitle: String? = null
|
||||
private var trigger: String? = null
|
||||
|
||||
override fun onBind(intent: Intent?): IBinder? = null
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
when (intent?.action) {
|
||||
ACTION_STOP -> {
|
||||
finish()
|
||||
return START_NOT_STICKY
|
||||
}
|
||||
|
||||
ACTION_START -> {
|
||||
val incoming = intent.toStation() ?: run {
|
||||
stopSelf()
|
||||
return START_NOT_STICKY
|
||||
}
|
||||
if (job?.isActive == true) return START_STICKY
|
||||
trigger = intent.getStringExtra(EXTRA_TRIGGER)?.takeIf { it.isNotBlank() }
|
||||
begin(incoming)
|
||||
}
|
||||
}
|
||||
return START_STICKY
|
||||
}
|
||||
|
||||
private fun begin(incoming: RadioStation) {
|
||||
station = incoming
|
||||
firstTitle = null
|
||||
Recorder.started(incoming, trigger)
|
||||
startForegroundCompat(notification(incoming, 0L))
|
||||
|
||||
val stamp = SimpleDateFormat("yyyy-MM-dd-HHmm", Locale.FRANCE).format(Date())
|
||||
val extension = incoming.extension()
|
||||
sinkTitle = "${incoming.name} — ${SimpleDateFormat("d MMM HH:mm", Locale.FRANCE).format(Date())}"
|
||||
val output = RecordingSink(
|
||||
context = this,
|
||||
fileName = "${incoming.name.sanitized()}-$stamp.$extension",
|
||||
mimeType = if (extension == "aac") "audio/aac" else "audio/mpeg",
|
||||
)
|
||||
sink = output
|
||||
|
||||
job = scope.launch {
|
||||
runCatching {
|
||||
// « Icy-MetaData: 1 » demande à la station d'intercaler le titre en cours dans le
|
||||
// flux. Ces octets ne sont pas de l'audio : ils doivent être retirés avant écriture,
|
||||
// sinon le fichier obtenu est corrompu.
|
||||
val connection = openConnection(incoming.streamUrl, readTimeoutMs = 30_000).apply {
|
||||
setRequestProperty("Icy-MetaData", "1")
|
||||
}
|
||||
try {
|
||||
if (connection.responseCode !in 200..299) {
|
||||
throw IllegalStateException("Flux indisponible (${connection.responseCode})")
|
||||
}
|
||||
val metaInterval = connection.getHeaderFieldInt("icy-metaint", 0)
|
||||
connection.inputStream.use { input ->
|
||||
output.open().use { stream ->
|
||||
if (metaInterval > 0) {
|
||||
copyWithTitles(input, stream, metaInterval, incoming)
|
||||
} else if (trigger != null) {
|
||||
// Sans métadonnées, impossible de savoir quand le morceau passe.
|
||||
throw IllegalStateException(
|
||||
"${incoming.name} n'annonce pas ses titres : l'attente est impossible"
|
||||
)
|
||||
} else {
|
||||
copyPlain(input, stream, incoming)
|
||||
}
|
||||
stream.flush()
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
connection.disconnect()
|
||||
}
|
||||
}
|
||||
// Fin du flux ou coupure réseau : ce qui a été capté reste valable.
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
/** Flux sans métadonnées : simple recopie. */
|
||||
private suspend fun copyPlain(
|
||||
input: java.io.InputStream,
|
||||
output: java.io.OutputStream,
|
||||
station: RadioStation,
|
||||
) {
|
||||
val buffer = ByteArray(32 * 1024)
|
||||
var total = 0L
|
||||
var lastNotified = 0L
|
||||
while (currentCoroutineContext().isActive) {
|
||||
val read = input.read(buffer)
|
||||
if (read <= 0) break
|
||||
output.write(buffer, 0, read)
|
||||
total += read
|
||||
if (total - lastNotified > NOTIFY_EVERY_BYTES) {
|
||||
lastNotified = total
|
||||
Recorder.progress(total)
|
||||
notifyProgress(station, total)
|
||||
}
|
||||
}
|
||||
Recorder.progress(total)
|
||||
}
|
||||
|
||||
/**
|
||||
* Flux ICY : `interval` octets d'audio, puis un bloc de métadonnées à retirer.
|
||||
* Le premier titre rencontré nomme l'enregistrement ; les suivants alimentent la notification.
|
||||
*/
|
||||
private suspend fun copyWithTitles(
|
||||
input: java.io.InputStream,
|
||||
output: java.io.OutputStream,
|
||||
interval: Int,
|
||||
station: RadioStation,
|
||||
) {
|
||||
val wanted = trigger
|
||||
val audio = ByteArray(interval)
|
||||
var total = 0L
|
||||
var lastNotified = 0L
|
||||
// Sans titre attendu, on écrit dès la première seconde ; sinon on patiente.
|
||||
var writing = wanted == null
|
||||
var capturedTitle: String? = null
|
||||
|
||||
while (currentCoroutineContext().isActive) {
|
||||
if (!input.readFully(audio, interval)) break
|
||||
if (writing) {
|
||||
output.write(audio)
|
||||
total += interval
|
||||
}
|
||||
|
||||
val lengthByte = input.read()
|
||||
if (lengthByte < 0) break
|
||||
val metaLength = lengthByte * 16
|
||||
if (metaLength > 0) {
|
||||
val meta = ByteArray(metaLength)
|
||||
if (!input.readFully(meta, metaLength)) break
|
||||
val announced = STREAM_TITLE
|
||||
.find(String(meta, Charsets.UTF_8))
|
||||
?.groupValues?.get(1)
|
||||
?.trim()
|
||||
?.takeIf { it.isNotBlank() }
|
||||
|
||||
if (announced != null) {
|
||||
Recorder.title(announced)
|
||||
when {
|
||||
wanted == null -> if (firstTitle == null) firstTitle = announced
|
||||
|
||||
!writing && announced.contains(wanted, ignoreCase = true) -> {
|
||||
// Le morceau attendu commence : on enregistre à partir d'ici.
|
||||
writing = true
|
||||
capturedTitle = announced
|
||||
firstTitle = announced
|
||||
Recorder.armed(announced)
|
||||
}
|
||||
|
||||
writing && capturedTitle != null && announced != capturedTitle -> {
|
||||
// Le morceau est terminé : on s'arrête sans attendre le suivant.
|
||||
break
|
||||
}
|
||||
}
|
||||
notifyProgress(station, total)
|
||||
}
|
||||
}
|
||||
|
||||
if (total - lastNotified > NOTIFY_EVERY_BYTES) {
|
||||
lastNotified = total
|
||||
Recorder.progress(total)
|
||||
notifyProgress(station, total)
|
||||
}
|
||||
}
|
||||
Recorder.progress(total)
|
||||
}
|
||||
|
||||
/** Lit exactement [count] octets, ou renvoie false si le flux s'interrompt avant. */
|
||||
private fun java.io.InputStream.readFully(target: ByteArray, count: Int): Boolean {
|
||||
var filled = 0
|
||||
while (filled < count) {
|
||||
val read = read(target, filled, count - filled)
|
||||
if (read < 0) return false
|
||||
filled += read
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/** Clôt l'enregistrement et verse le fichier obtenu dans la bibliothèque. */
|
||||
private fun finish() {
|
||||
job?.cancel()
|
||||
job = null
|
||||
|
||||
val output = sink
|
||||
val recorded = station
|
||||
val captured = firstTitle
|
||||
sink = null
|
||||
station = null
|
||||
trigger = null
|
||||
|
||||
if (output != null && recorded != null) {
|
||||
val elapsed = Recorder.status.value.elapsedMs
|
||||
// Le titre annoncé par la station prime : c'est lui qu'on cherchait en enregistrant.
|
||||
val title = captured ?: sinkTitle
|
||||
val published = output.finish(title, elapsed)
|
||||
if (published != null) {
|
||||
val (uri, size) = published
|
||||
val container = (application as CachetteApp).container
|
||||
val entry = MediaEntry(
|
||||
id = "rec:${uri.hashCode()}",
|
||||
source = SourceKind.RECORDING,
|
||||
kind = MediaKind.AUDIO,
|
||||
title = title,
|
||||
subtitle = if (captured != null) recorded.name else "Enregistrement radio",
|
||||
uri = uri,
|
||||
artworkUri = recorded.faviconUrl,
|
||||
durationMs = elapsed,
|
||||
addedAt = System.currentTimeMillis(),
|
||||
sizeBytes = size,
|
||||
)
|
||||
CoroutineScope(Dispatchers.IO).launch { container.imported.add(listOf(entry)) }
|
||||
}
|
||||
}
|
||||
|
||||
firstTitle = null
|
||||
Recorder.stopped()
|
||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||
stopSelf()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
scope.cancel()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private fun startForegroundCompat(notification: Notification) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
|
||||
} else {
|
||||
startForeground(NOTIFICATION_ID, notification)
|
||||
}
|
||||
}
|
||||
|
||||
private fun notifyProgress(station: RadioStation, bytes: Long) {
|
||||
getSystemService(NotificationManager::class.java)
|
||||
.notify(NOTIFICATION_ID, notification(station, bytes))
|
||||
}
|
||||
|
||||
private fun notification(station: RadioStation, bytes: Long): Notification {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
getSystemService(NotificationManager::class.java).createNotificationChannel(
|
||||
NotificationChannel(CHANNEL_ID, "Enregistrement radio", NotificationManager.IMPORTANCE_LOW)
|
||||
)
|
||||
}
|
||||
val open = PendingIntent.getActivity(
|
||||
this,
|
||||
0,
|
||||
Intent(this, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
|
||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
||||
)
|
||||
val stop = PendingIntent.getService(
|
||||
this,
|
||||
1,
|
||||
Intent(this, RecordingService::class.java).setAction(ACTION_STOP),
|
||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
||||
)
|
||||
return NotificationCompat.Builder(this, CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_offline)
|
||||
.setContentTitle(
|
||||
if (Recorder.status.value.waitingFor != null) "En attente sur ${station.name}"
|
||||
else "Enregistrement de ${station.name}"
|
||||
)
|
||||
.setContentText(
|
||||
Recorder.status.value.currentTitle
|
||||
?: if (bytes > 0) "%.1f Mo captés".format(bytes / 1_000_000.0) else "Démarrage…"
|
||||
)
|
||||
.setContentIntent(open)
|
||||
.addAction(0, "Arrêter", stop)
|
||||
.setOngoing(true)
|
||||
.setPriority(NotificationCompat.PRIORITY_LOW)
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun Intent.toStation(): RadioStation? {
|
||||
val url = getStringExtra(EXTRA_URL) ?: return null
|
||||
return RadioStation(
|
||||
id = getStringExtra(EXTRA_ID).orEmpty(),
|
||||
name = getStringExtra(EXTRA_NAME) ?: "Radio",
|
||||
streamUrl = url,
|
||||
codec = getStringExtra(EXTRA_CODEC).orEmpty(),
|
||||
bitrate = getIntExtra(EXTRA_BITRATE, 0),
|
||||
country = "",
|
||||
tags = "",
|
||||
faviconUrl = getStringExtra(EXTRA_FAVICON),
|
||||
)
|
||||
}
|
||||
|
||||
private fun RadioStation.extension(): String =
|
||||
if (codec.equals("AAC", ignoreCase = true) || codec.startsWith("AAC", true)) "aac" else "mp3"
|
||||
|
||||
private fun String.sanitized(): String =
|
||||
replace(Regex("[^\\p{L}\\p{N} _-]"), "").trim().replace(' ', '_').take(40).ifBlank { "radio" }
|
||||
|
||||
companion object {
|
||||
private const val ACTION_START = "fr.tuxgyver.cachette.RECORD_START"
|
||||
private const val ACTION_STOP = "fr.tuxgyver.cachette.RECORD_STOP"
|
||||
private const val EXTRA_ID = "id"
|
||||
private const val EXTRA_NAME = "name"
|
||||
private const val EXTRA_URL = "url"
|
||||
private const val EXTRA_CODEC = "codec"
|
||||
private const val EXTRA_BITRATE = "bitrate"
|
||||
private const val EXTRA_FAVICON = "favicon"
|
||||
private const val EXTRA_TRIGGER = "trigger"
|
||||
private const val NOTIFICATION_ID = 4401
|
||||
private const val CHANNEL_ID = "cachette_recording"
|
||||
private const val NOTIFY_EVERY_BYTES = 256 * 1024L
|
||||
private const val MIN_USEFUL_BYTES = 16 * 1024L
|
||||
private val STREAM_TITLE = Regex("StreamTitle='(.*?)';")
|
||||
|
||||
fun start(context: Context, station: RadioStation, whenTitleContains: String? = null) {
|
||||
val intent = Intent(context, RecordingService::class.java)
|
||||
.setAction(ACTION_START)
|
||||
.putExtra(EXTRA_ID, station.id)
|
||||
.putExtra(EXTRA_NAME, station.name)
|
||||
.putExtra(EXTRA_URL, station.streamUrl)
|
||||
.putExtra(EXTRA_CODEC, station.codec)
|
||||
.putExtra(EXTRA_BITRATE, station.bitrate)
|
||||
.putExtra(EXTRA_FAVICON, station.faviconUrl)
|
||||
.putExtra(EXTRA_TRIGGER, whenTitleContains)
|
||||
context.startForegroundService(intent)
|
||||
}
|
||||
|
||||
fun stop(context: Context) {
|
||||
context.startService(
|
||||
Intent(context, RecordingService::class.java).setAction(ACTION_STOP)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package fr.tuxgyver.cachette.radio
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.provider.MediaStore
|
||||
import fr.tuxgyver.cachette.model.MediaEntry
|
||||
import java.io.File
|
||||
import java.io.OutputStream
|
||||
|
||||
/**
|
||||
* Destination d'un enregistrement radio.
|
||||
*
|
||||
* À partir d'Android 10, le fichier est écrit dans `Music/Cachette` via MediaStore : il devient
|
||||
* visible de tous les lecteurs de l'appareil et survit à la désinstallation. En deçà, l'écriture
|
||||
* dans MediaStore réclamerait une permission de stockage que l'application n'a pas besoin de
|
||||
* demander par ailleurs, donc le fichier reste dans le dossier privé.
|
||||
*/
|
||||
class RecordingSink(
|
||||
private val context: Context,
|
||||
private val fileName: String,
|
||||
private val mimeType: String,
|
||||
) {
|
||||
private val useMediaStore = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
|
||||
|
||||
private var uri: Uri? = null
|
||||
private var file: File? = null
|
||||
|
||||
fun open(): OutputStream = if (useMediaStore) openMediaStore() else openPrivateFile()
|
||||
|
||||
private fun openMediaStore(): OutputStream {
|
||||
val collection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
|
||||
val values = ContentValues().apply {
|
||||
put(MediaStore.Audio.Media.DISPLAY_NAME, fileName)
|
||||
put(MediaStore.Audio.Media.MIME_TYPE, mimeType)
|
||||
put(MediaStore.Audio.Media.RELATIVE_PATH, RELATIVE_PATH)
|
||||
// Tant que l'écriture dure, le fichier reste invisible des autres applications.
|
||||
put(MediaStore.Audio.Media.IS_PENDING, 1)
|
||||
}
|
||||
val created = context.contentResolver.insert(collection, values)
|
||||
?: throw IllegalStateException("Impossible de créer le fichier dans la bibliothèque système")
|
||||
uri = created
|
||||
return context.contentResolver.openOutputStream(created)
|
||||
?: throw IllegalStateException("Impossible d'écrire dans la bibliothèque système")
|
||||
}
|
||||
|
||||
private fun openPrivateFile(): OutputStream {
|
||||
val directory = File(context.getExternalFilesDir(null) ?: context.filesDir, "recordings")
|
||||
.apply { mkdirs() }
|
||||
val created = File(directory, fileName)
|
||||
file = created
|
||||
return created.outputStream()
|
||||
}
|
||||
|
||||
/** Publie l'enregistrement sous son titre définitif, ou renvoie null s'il est vide. */
|
||||
fun finish(title: String, durationMs: Long): Pair<String, Long>? {
|
||||
val target = uri
|
||||
if (target != null) {
|
||||
val size = context.contentResolver.openFileDescriptor(target, "r")
|
||||
?.use { it.statSize } ?: 0L
|
||||
if (size < MIN_USEFUL_BYTES) {
|
||||
abort()
|
||||
return null
|
||||
}
|
||||
// En levant IS_PENDING, MediaStore relit le fichier et réécrit TITLE depuis le nom :
|
||||
// c'est donc le nom de fichier, et lui seul, qui doit porter le titre du morceau.
|
||||
// (Vérifié : un TITLE posé ici est écrasé par ce balayage.)
|
||||
val extension = fileName.substringAfterLast('.', "mp3")
|
||||
context.contentResolver.update(
|
||||
target,
|
||||
ContentValues().apply {
|
||||
put(MediaStore.Audio.Media.IS_PENDING, 0)
|
||||
put(MediaStore.Audio.Media.DISPLAY_NAME, "${title.asFileName()}.$extension")
|
||||
if (durationMs > 0) put(MediaStore.Audio.Media.DURATION, durationMs)
|
||||
},
|
||||
null,
|
||||
null,
|
||||
)
|
||||
return target.toString() to size
|
||||
}
|
||||
|
||||
val created = file ?: return null
|
||||
if (created.length() < MIN_USEFUL_BYTES) {
|
||||
created.delete()
|
||||
return null
|
||||
}
|
||||
return created.toURI().toString() to created.length()
|
||||
}
|
||||
|
||||
fun abort() {
|
||||
uri?.let { context.contentResolver.delete(it, null, null) }
|
||||
file?.delete()
|
||||
uri = null
|
||||
file = null
|
||||
}
|
||||
|
||||
/** Un titre de morceau n'est pas un nom de fichier : on en retire ce qui gênerait. */
|
||||
private fun String.asFileName(): String =
|
||||
replace(Regex("[^\\p{L}\\p{N} ._-]"), "").trim().take(80).ifBlank { "enregistrement" }
|
||||
|
||||
companion object {
|
||||
const val RELATIVE_PATH = "Music/Cachette"
|
||||
private const val MIN_USEFUL_BYTES = 16 * 1024L
|
||||
|
||||
/** Supprime un enregistrement, quel que soit l'endroit où il avait été écrit. */
|
||||
fun delete(context: Context, entry: MediaEntry): Boolean = runCatching {
|
||||
val uri = Uri.parse(entry.uri)
|
||||
when (uri.scheme) {
|
||||
"content" -> context.contentResolver.delete(uri, null, null) > 0
|
||||
"file" -> File(uri.path!!).delete()
|
||||
else -> false
|
||||
}
|
||||
}.getOrDefault(false)
|
||||
}
|
||||
}
|
||||
@@ -19,9 +19,16 @@ import java.net.URLEncoder
|
||||
* qu'au moment où l'utilisateur choisit un résultat, pour ne pas interroger les métadonnées de
|
||||
* trente éléments dont vingt-neuf seront ignorés.
|
||||
*/
|
||||
class ArchiveOrgCatalog : Catalog {
|
||||
|
||||
override val id = CatalogId.ARCHIVE
|
||||
class ArchiveOrgCatalog(
|
||||
override val id: CatalogId = CatalogId.ARCHIVE,
|
||||
/**
|
||||
* Collections auxquelles se restreindre. Vide : tout archive.org.
|
||||
* `netlabels` et `freemusicarchive` ne contiennent que des publications sous licence libre,
|
||||
* ce qui donne un fonds indépendant sans les dépôts de titres commerciaux qu'on croise
|
||||
* ailleurs sur le site.
|
||||
*/
|
||||
private val collections: List<String> = emptyList(),
|
||||
) : Catalog {
|
||||
|
||||
override val languages = COMMON_LANGUAGES
|
||||
|
||||
@@ -33,8 +40,13 @@ class ArchiveOrgCatalog : Catalog {
|
||||
// Les éléments « access-restricted » répondent 401 au téléchargement : on les écarte
|
||||
// dès la recherche plutôt que de les proposer puis d'échouer.
|
||||
val languageClause = language?.let { " AND language:($it)" }.orEmpty()
|
||||
val collectionClause =
|
||||
collections.takeIf { it.isNotEmpty() }
|
||||
?.joinToString(" OR ")
|
||||
?.let { " AND collection:($it)" }
|
||||
.orEmpty()
|
||||
val q = URLEncoder.encode(
|
||||
"($query) AND mediatype:(audio OR movies)$languageClause" +
|
||||
"($query) AND mediatype:(audio OR movies)$languageClause$collectionClause" +
|
||||
" AND NOT access-restricted-item:true",
|
||||
"UTF-8",
|
||||
)
|
||||
|
||||
@@ -52,6 +52,12 @@ enum class CatalogId(
|
||||
license = "Domaine public",
|
||||
suggestion = "Jules Verne",
|
||||
),
|
||||
UNDERGROUND(
|
||||
title = "Underground",
|
||||
description = "Netlabels et Free Music Archive : des dizaines de milliers de morceaux d'artistes indépendants, publiés sous licence libre.",
|
||||
license = "Creative Commons / netlabels",
|
||||
suggestion = "ambient",
|
||||
),
|
||||
WIKIMEDIA(
|
||||
title = "Wikimedia Commons",
|
||||
description = "Enregistrements classiques, archives sonores et musique libre hébergés par Wikimedia.",
|
||||
|
||||
@@ -45,6 +45,7 @@ import fr.tuxgyver.cachette.ui.browse.SourceBrowseScreen
|
||||
import fr.tuxgyver.cachette.ui.library.LibraryScreen
|
||||
import fr.tuxgyver.cachette.ui.offline.DownloadsScreen
|
||||
import fr.tuxgyver.cachette.ui.player.MiniPlayer
|
||||
import fr.tuxgyver.cachette.ui.radio.RadioScreen
|
||||
import fr.tuxgyver.cachette.ui.playlists.AddToPlaylistSheet
|
||||
import fr.tuxgyver.cachette.ui.playlists.PlaylistDetailScreen
|
||||
import fr.tuxgyver.cachette.ui.playlists.PlaylistsScreen
|
||||
@@ -63,6 +64,7 @@ private const val BROWSE_ROUTE = "browse/{source}"
|
||||
private const val CATALOG_ROUTE = "catalog/{catalog}"
|
||||
private const val CATALOG_ITEM_ROUTE = "catalog/{catalog}/item"
|
||||
private const val PLAYLIST_ROUTE = "playlist/{playlist}"
|
||||
private const val RADIO_ROUTE = "radio"
|
||||
|
||||
@Composable
|
||||
fun CachetteRoot(container: AppContainer) {
|
||||
@@ -143,6 +145,16 @@ fun CachetteRoot(container: AppContainer) {
|
||||
)
|
||||
}
|
||||
|
||||
composable(RADIO_ROUTE) {
|
||||
RadioScreen(
|
||||
container = container,
|
||||
currentMediaId = playerState.mediaId,
|
||||
onBack = navController::popBackStack,
|
||||
onMessage = notify,
|
||||
contentPadding = padding,
|
||||
)
|
||||
}
|
||||
|
||||
composable(PLAYLIST_ROUTE) { entry ->
|
||||
PlaylistDetailScreen(
|
||||
container = container,
|
||||
@@ -161,6 +173,7 @@ fun CachetteRoot(container: AppContainer) {
|
||||
onMessage = notify,
|
||||
onBrowseSource = { navController.navigate("browse/${it.name}") },
|
||||
onBrowseCatalog = { navController.navigate("catalog/${it.name}") },
|
||||
onBrowseRadio = { navController.navigate(RADIO_ROUTE) },
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,10 @@ import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.CloudOff
|
||||
import androidx.compose.material.icons.rounded.DeleteOutline
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.LinearProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
@@ -27,17 +29,22 @@ import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableLongStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import fr.tuxgyver.cachette.AppContainer
|
||||
import fr.tuxgyver.cachette.model.MediaEntry
|
||||
import fr.tuxgyver.cachette.model.SourceKind
|
||||
import fr.tuxgyver.cachette.radio.RecordingSink
|
||||
import fr.tuxgyver.cachette.ui.components.MediaRow
|
||||
import fr.tuxgyver.cachette.ui.components.formatBytes
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
@@ -51,19 +58,30 @@ fun DownloadsScreen(
|
||||
val entries by container.library.entries.collectAsStateWithLifecycle()
|
||||
val offlineStates by container.downloads.states.collectAsStateWithLifecycle()
|
||||
var confirmClear by remember { mutableStateOf(false) }
|
||||
var pendingRecordingDelete by remember { mutableStateOf<MediaEntry?>(null) }
|
||||
val context = LocalContext.current
|
||||
val scope = rememberCoroutineScope()
|
||||
var cacheSize by remember { mutableLongStateOf(0L) }
|
||||
|
||||
LaunchedEffect(offlineStates) {
|
||||
cacheSize = withContext(Dispatchers.IO) { container.offline.cacheSizeBytes() }
|
||||
}
|
||||
|
||||
// Un enregistrement radio n'a pas d'état de téléchargement : le fichier est déjà là.
|
||||
// L'écran annoncerait sinon « disponibles sans réseau » en omettant ce qui l'est le plus.
|
||||
val cached = remember(entries, offlineStates) {
|
||||
entries.filter { offlineStates[it.id] != null }
|
||||
entries.filter { offlineStates[it.id] != null || it.source == SourceKind.RECORDING }
|
||||
.sortedByDescending { offlineStates[it.id]?.isActive == true }
|
||||
}
|
||||
val completed = cached.count { offlineStates[it.id]?.isComplete == true }
|
||||
val recordings = cached.filter { it.source == SourceKind.RECORDING }
|
||||
val completed = cached.count {
|
||||
offlineStates[it.id]?.isComplete == true || it.source == SourceKind.RECORDING
|
||||
}
|
||||
val running = cached.count { offlineStates[it.id]?.isActive == true }
|
||||
|
||||
LaunchedEffect(offlineStates, recordings) {
|
||||
cacheSize = withContext(Dispatchers.IO) {
|
||||
container.offline.cacheSizeBytes() + recordings.sumOf { it.sizeBytes ?: 0L }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LazyColumn(
|
||||
contentPadding = contentPadding,
|
||||
verticalArrangement = Arrangement.spacedBy(6.dp),
|
||||
@@ -92,6 +110,8 @@ fun DownloadsScreen(
|
||||
items(cached, key = { it.id }) { entry ->
|
||||
val state = offlineStates[entry.id]
|
||||
Column {
|
||||
val isRecording = entry.source == SourceKind.RECORDING
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
MediaRow(
|
||||
entry = entry,
|
||||
offline = state,
|
||||
@@ -100,7 +120,17 @@ fun DownloadsScreen(
|
||||
onDownload = { container.downloads.download(entry) },
|
||||
onRemoveOffline = { container.downloads.remove(entry.id) },
|
||||
onLongClick = { onAddToPlaylist(entry) },
|
||||
modifier = Modifier.weight(1f),
|
||||
)
|
||||
if (isRecording) {
|
||||
IconButton(onClick = { pendingRecordingDelete = entry }) {
|
||||
Icon(
|
||||
Icons.Rounded.DeleteOutline,
|
||||
contentDescription = "Supprimer cet enregistrement",
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (state?.isActive == true) {
|
||||
LinearProgressIndicator(
|
||||
progress = { (state.percent / 100f).coerceIn(0f, 1f) },
|
||||
@@ -113,12 +143,32 @@ fun DownloadsScreen(
|
||||
}
|
||||
}
|
||||
|
||||
pendingRecordingDelete?.let { entry ->
|
||||
AlertDialog(
|
||||
onDismissRequest = { pendingRecordingDelete = null },
|
||||
title = { Text("Supprimer cet enregistrement ?") },
|
||||
text = { Text("« ${entry.title} » sera effacé de l'appareil. Cette action est définitive : un enregistrement ne se retélécharge pas.") },
|
||||
confirmButton = {
|
||||
TextButton(onClick = {
|
||||
scope.launch {
|
||||
RecordingSink.delete(context, entry)
|
||||
container.library.forget(entry.id)
|
||||
}
|
||||
pendingRecordingDelete = null
|
||||
}) { Text("Supprimer") }
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = { pendingRecordingDelete = null }) { Text("Annuler") }
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
if (confirmClear) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { confirmClear = false },
|
||||
title = { Text("Vider la cachette ?") },
|
||||
text = {
|
||||
Text("Tout le contenu mis de côté sera supprimé de l'appareil (${formatBytes(cacheSize)}). Les fichiers déjà présents sur l'appareil ne sont pas touchés.")
|
||||
Text("Le contenu téléchargé sera supprimé de l'appareil. Les fichiers déjà présents et vos enregistrements radio ne sont pas touchés.")
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(onClick = {
|
||||
|
||||
@@ -0,0 +1,439 @@
|
||||
package fr.tuxgyver.cachette.ui.radio
|
||||
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.horizontalScroll
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.rounded.ArrowBack
|
||||
import androidx.compose.material.icons.rounded.FiberManualRecord
|
||||
import androidx.compose.material.icons.rounded.Alarm
|
||||
import androidx.compose.material.icons.rounded.GraphicEq
|
||||
import androidx.compose.material.icons.rounded.PlayCircleOutline
|
||||
import androidx.compose.material.icons.rounded.Search
|
||||
import androidx.compose.material.icons.rounded.Stop
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.FilterChip
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import fr.tuxgyver.cachette.AppContainer
|
||||
import fr.tuxgyver.cachette.model.MediaEntry
|
||||
import fr.tuxgyver.cachette.model.MediaKind
|
||||
import fr.tuxgyver.cachette.model.SourceKind
|
||||
import fr.tuxgyver.cachette.radio.RADIO_GENRES
|
||||
import fr.tuxgyver.cachette.radio.RadioStation
|
||||
import fr.tuxgyver.cachette.radio.Recorder
|
||||
import fr.tuxgyver.cachette.radio.RecordingService
|
||||
import fr.tuxgyver.cachette.ui.components.Artwork
|
||||
import fr.tuxgyver.cachette.ui.components.formatBytes
|
||||
import fr.tuxgyver.cachette.ui.components.formatDuration
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* Les stations les plus écoutées, à écouter en direct ou à enregistrer.
|
||||
*
|
||||
* L'enregistrement produit un fichier local qui rejoint la bibliothèque : il n'a donc rien à
|
||||
* télécharger ensuite, il est déjà hors connexion.
|
||||
*/
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun RadioScreen(
|
||||
container: AppContainer,
|
||||
currentMediaId: String?,
|
||||
onBack: () -> Unit,
|
||||
onMessage: (String) -> Unit,
|
||||
contentPadding: PaddingValues,
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val scope = rememberCoroutineScope()
|
||||
val status by Recorder.status.collectAsStateWithLifecycle()
|
||||
|
||||
var query by rememberSaveable { mutableStateOf("") }
|
||||
var genre by rememberSaveable { mutableStateOf<String?>(null) }
|
||||
var stations by remember { mutableStateOf<List<RadioStation>>(emptyList()) }
|
||||
var loading by remember { mutableStateOf(true) }
|
||||
var error by remember { mutableStateOf<String?>(null) }
|
||||
var nowPlaying by remember { mutableStateOf(emptyMap<String, String>()) }
|
||||
var scheduleFor by remember { mutableStateOf<RadioStation?>(null) }
|
||||
|
||||
fun load() {
|
||||
loading = true
|
||||
error = null
|
||||
scope.launch {
|
||||
val outcome = if (query.isBlank()) container.radio.popular(genre)
|
||||
else container.radio.search(query)
|
||||
outcome.fold(
|
||||
onSuccess = { stations = it; if (it.isEmpty()) error = "Aucune station" },
|
||||
onFailure = { error = it.message ?: "Annuaire indisponible" },
|
||||
)
|
||||
loading = false
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(genre) { load() }
|
||||
|
||||
// Aucun annuaire n'expose le morceau en cours : il faut ouvrir chaque flux. On s'en tient
|
||||
// donc aux premières stations affichées, quatre à la fois, et le résultat est conservé.
|
||||
LaunchedEffect(stations) {
|
||||
nowPlaying = emptyMap()
|
||||
stations.take(PROBED_STATIONS).chunked(4).forEach { group ->
|
||||
val found = group.map { station ->
|
||||
async { station.id to container.radio.nowPlaying(station.streamUrl) }
|
||||
}.awaitAll()
|
||||
nowPlaying = nowPlaying + found.mapNotNull { (id, title) ->
|
||||
title?.let { id to it }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val listPadding = PaddingValues(bottom = contentPadding.calculateBottomPadding())
|
||||
|
||||
Column(Modifier.fillMaxSize()) {
|
||||
TopAppBar(
|
||||
title = { Text("Radio") },
|
||||
navigationIcon = {
|
||||
IconButton(onClick = onBack) {
|
||||
Icon(Icons.AutoMirrored.Rounded.ArrowBack, contentDescription = "Retour")
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
if (status.isRecording) {
|
||||
// La progression n'est publiée que tous les 256 Ko : le chronomètre se rafraîchit
|
||||
// donc de lui-même, sinon il semblerait figé entre deux paliers.
|
||||
var tick by remember { mutableStateOf(0L) }
|
||||
LaunchedEffect(status.startedAt) {
|
||||
while (true) {
|
||||
tick = System.currentTimeMillis()
|
||||
delay(1_000)
|
||||
}
|
||||
}
|
||||
RecordingBanner(
|
||||
station = status.station!!,
|
||||
title = status.currentTitle,
|
||||
waitingFor = status.waitingFor,
|
||||
elapsedMs = (tick - status.startedAt).coerceAtLeast(0L),
|
||||
bytes = status.bytes,
|
||||
onStop = {
|
||||
RecordingService.stop(context)
|
||||
onMessage("Enregistrement versé dans la bibliothèque")
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
OutlinedTextField(
|
||||
value = query,
|
||||
onValueChange = { query = it },
|
||||
placeholder = { Text("Rechercher une station") },
|
||||
leadingIcon = { Icon(Icons.Rounded.Search, contentDescription = null) },
|
||||
singleLine = true,
|
||||
shape = MaterialTheme.shapes.large,
|
||||
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
|
||||
keyboardActions = KeyboardActions(onSearch = { load() }),
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(16.dp),
|
||||
)
|
||||
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.horizontalScroll(rememberScrollState())
|
||||
.padding(horizontal = 16.dp),
|
||||
) {
|
||||
FilterChip(
|
||||
selected = genre == null,
|
||||
onClick = { genre = null; query = "" },
|
||||
label = { Text("Les plus écoutées") },
|
||||
)
|
||||
RADIO_GENRES.forEach { option ->
|
||||
FilterChip(
|
||||
selected = genre == option,
|
||||
onClick = { genre = option; query = "" },
|
||||
label = { Text(option.replaceFirstChar(Char::uppercase)) },
|
||||
)
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.size(8.dp))
|
||||
|
||||
when {
|
||||
loading -> Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
||||
CircularProgressIndicator()
|
||||
}
|
||||
|
||||
error != null -> Box(
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(32.dp),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
Text(
|
||||
text = error!!,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
textAlign = TextAlign.Center,
|
||||
)
|
||||
}
|
||||
|
||||
else -> LazyColumn(
|
||||
contentPadding = listPadding,
|
||||
verticalArrangement = Arrangement.spacedBy(6.dp),
|
||||
modifier = Modifier.padding(horizontal = 16.dp),
|
||||
) {
|
||||
items(stations, key = { it.id }) { station ->
|
||||
StationRow(
|
||||
station = station,
|
||||
nowPlaying = nowPlaying[station.id],
|
||||
playing = station.asEntry().id == currentMediaId,
|
||||
recording = status.station?.id == station.id,
|
||||
onSchedule = { scheduleFor = station },
|
||||
onListen = {
|
||||
val entry = station.asEntry()
|
||||
container.library.preview(listOf(entry))
|
||||
container.player.play(listOf(entry), 0)
|
||||
},
|
||||
onRecord = {
|
||||
if (status.isRecording) {
|
||||
onMessage("Un enregistrement est déjà en cours")
|
||||
} else {
|
||||
RecordingService.start(context, station)
|
||||
onMessage("Enregistrement de ${station.name} démarré")
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
item { Spacer(Modifier.size(24.dp)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scheduleFor?.let { station ->
|
||||
ScheduleDialog(
|
||||
station = station,
|
||||
onDismiss = { scheduleFor = null },
|
||||
onConfirm = { wanted ->
|
||||
RecordingService.start(context, station, whenTitleContains = wanted)
|
||||
onMessage("Cachette guette « $wanted » sur ${station.name}")
|
||||
scheduleFor = null
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** Au-delà, le nombre de connexions simultanées ne se justifierait plus. */
|
||||
private const val PROBED_STATIONS = 12
|
||||
|
||||
/** Une station écoutée en direct est un média éphémère : pas de durée, pas de mise en cache. */
|
||||
private fun RadioStation.asEntry() = MediaEntry(
|
||||
id = "radio:$id",
|
||||
source = SourceKind.URL,
|
||||
kind = MediaKind.AUDIO,
|
||||
title = name,
|
||||
subtitle = subtitle.ifBlank { "Radio en direct" },
|
||||
uri = streamUrl,
|
||||
artworkUri = faviconUrl,
|
||||
)
|
||||
|
||||
/** Programme un enregistrement : il démarrera dès que la station annoncera ce titre. */
|
||||
@Composable
|
||||
private fun ScheduleDialog(
|
||||
station: RadioStation,
|
||||
onDismiss: () -> Unit,
|
||||
onConfirm: (String) -> Unit,
|
||||
) {
|
||||
var wanted by remember { mutableStateOf("") }
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = { Text("Guetter un titre") },
|
||||
text = {
|
||||
Column {
|
||||
Text(
|
||||
text = "Cachette écoutera ${station.name} et déclenchera l'enregistrement dès " +
|
||||
"qu'un titre contenant ce texte sera annoncé, puis s'arrêtera à la fin du morceau.",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
)
|
||||
Spacer(Modifier.size(12.dp))
|
||||
OutlinedTextField(
|
||||
value = wanted,
|
||||
onValueChange = { wanted = it },
|
||||
placeholder = { Text("Titre ou artiste") },
|
||||
singleLine = true,
|
||||
)
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(enabled = wanted.isNotBlank(), onClick = { onConfirm(wanted.trim()) }) {
|
||||
Text("Guetter")
|
||||
}
|
||||
},
|
||||
dismissButton = { TextButton(onClick = onDismiss) { Text("Annuler") } },
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun RecordingBanner(
|
||||
station: RadioStation,
|
||||
title: String?,
|
||||
waitingFor: String?,
|
||||
elapsedMs: Long,
|
||||
bytes: Long,
|
||||
onStop: () -> Unit,
|
||||
) {
|
||||
Surface(
|
||||
color = MaterialTheme.colorScheme.errorContainer,
|
||||
shape = MaterialTheme.shapes.medium,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp, vertical = 4.dp),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.padding(12.dp),
|
||||
) {
|
||||
Icon(
|
||||
Icons.Rounded.FiberManualRecord,
|
||||
contentDescription = null,
|
||||
tint = Color(0xFFD32F2F),
|
||||
)
|
||||
Spacer(Modifier.width(12.dp))
|
||||
Column(Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = waitingFor?.let { "En attente de « $it »" } ?: title ?: station.name,
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
Text(
|
||||
text = listOfNotNull(
|
||||
title?.let { station.name },
|
||||
formatDuration(elapsedMs),
|
||||
formatBytes(bytes),
|
||||
).joinToString(" · "),
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
Button(onClick = onStop) {
|
||||
Icon(Icons.Rounded.Stop, contentDescription = null, modifier = Modifier.size(18.dp))
|
||||
Spacer(Modifier.size(6.dp))
|
||||
Text("Arrêter")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun StationRow(
|
||||
station: RadioStation,
|
||||
nowPlaying: String?,
|
||||
playing: Boolean,
|
||||
recording: Boolean,
|
||||
onListen: () -> Unit,
|
||||
onRecord: () -> Unit,
|
||||
onSchedule: () -> Unit,
|
||||
) {
|
||||
Surface(
|
||||
color = if (playing) MaterialTheme.colorScheme.primaryContainer
|
||||
else MaterialTheme.colorScheme.surface,
|
||||
shape = MaterialTheme.shapes.medium,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable(onClick = onListen),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.padding(10.dp),
|
||||
) {
|
||||
Artwork(
|
||||
uri = station.faviconUrl,
|
||||
title = station.name,
|
||||
kind = MediaKind.AUDIO,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
modifier = Modifier.size(48.dp),
|
||||
)
|
||||
Spacer(Modifier.width(14.dp))
|
||||
Column(Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = station.name,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
Text(
|
||||
text = nowPlaying ?: station.subtitle,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = if (nowPlaying != null) MaterialTheme.colorScheme.secondary
|
||||
else MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
Icon(
|
||||
imageVector = if (playing) Icons.Rounded.GraphicEq else Icons.Rounded.PlayCircleOutline,
|
||||
contentDescription = if (playing) "En écoute" else "Écouter en direct",
|
||||
tint = if (playing) MaterialTheme.colorScheme.primary
|
||||
else MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
IconButton(onClick = onSchedule) {
|
||||
Icon(
|
||||
Icons.Rounded.Alarm,
|
||||
contentDescription = "Enregistrer quand un titre passe",
|
||||
tint = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
IconButton(onClick = onRecord) {
|
||||
Icon(
|
||||
Icons.Rounded.FiberManualRecord,
|
||||
contentDescription = "Enregistrer cette station",
|
||||
tint = if (recording) Color(0xFFD32F2F) else MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,10 @@ import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.rounded.KeyboardArrowRight
|
||||
import androidx.compose.material.icons.automirrored.rounded.MenuBook
|
||||
import androidx.compose.material.icons.rounded.Headphones
|
||||
import androidx.compose.material.icons.rounded.LibraryMusic
|
||||
import androidx.compose.material.icons.rounded.Public
|
||||
import androidx.compose.material.icons.rounded.Radio
|
||||
import androidx.compose.material.icons.rounded.Search
|
||||
import androidx.compose.material.icons.rounded.Info
|
||||
import androidx.compose.material.icons.rounded.Link
|
||||
@@ -59,6 +61,7 @@ fun SourcesScreen(
|
||||
onMessage: (String) -> Unit,
|
||||
onBrowseSource: (SourceKind) -> Unit,
|
||||
onBrowseCatalog: (CatalogId) -> Unit,
|
||||
onBrowseRadio: () -> Unit,
|
||||
) {
|
||||
val entries by container.library.entries.collectAsStateWithLifecycle()
|
||||
var sheet by remember { mutableStateOf<ImportKind?>(null) }
|
||||
@@ -122,6 +125,19 @@ fun SourcesScreen(
|
||||
)
|
||||
}
|
||||
|
||||
item {
|
||||
val recordings = entries.count { it.source == SourceKind.RECORDING }
|
||||
SourceCard(
|
||||
icon = Icons.Rounded.Radio,
|
||||
title = "Radio",
|
||||
status = if (recordings > 0) "$recordings enregistrement(s)" else "Direct et enregistrement",
|
||||
description = "Les stations les plus écoutées. À suivre en direct, ou à enregistrer pour l'écouter plus tard.",
|
||||
actionLabel = "Ouvrir la radio",
|
||||
onAction = onBrowseRadio,
|
||||
onOpen = onBrowseRadio,
|
||||
)
|
||||
}
|
||||
|
||||
item {
|
||||
Spacer(Modifier.size(8.dp))
|
||||
Text("Catalogues libres de droits", style = MaterialTheme.typography.titleMedium)
|
||||
@@ -137,6 +153,7 @@ fun SourcesScreen(
|
||||
icon = when (catalog) {
|
||||
CatalogId.LIBRIVOX -> Icons.AutoMirrored.Rounded.MenuBook
|
||||
CatalogId.WIKIMEDIA -> Icons.Rounded.LibraryMusic
|
||||
CatalogId.UNDERGROUND -> Icons.Rounded.Headphones
|
||||
else -> Icons.Rounded.Public
|
||||
},
|
||||
title = catalog.title,
|
||||
|
||||
Reference in new issue
Block a user