Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.
Kotlin alternativeandroidandroid-applicationclientglassmorphisminnertubelibrespotliquid-glassspotifyspotify-connectspotifyapiyoutube-musicyoutube-playerytdlnis
Hand downloads to YTDLnis
Lelonio42f07d12de14
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xmlindex 19c0873..1703a5b 100644--- a/app/src/main/AndroidManifest.xml+++ b/app/src/main/AndroidManifest.xml@@ -17,6 +17,20 @@--><uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />+ <!--+ Package visibility, required from Android 11 to see any of this. Without+ it a downloader is invisible to resolve and to setPackage alike, so the+ hand-off cannot tell an installed app from an absent one and every link+ falls back to the chooser.+ -->+ <queries>+ <package android:name="com.deniscerri.ytdl" />+ <intent>+ <action android:name="android.intent.action.SEND" />+ <data android:mimeType="text/plain" />+ </intent>+ </queries>+<applicationandroid:name=".SquareApplication"android:allowBackup="false"diff --git a/app/src/main/java/dev/lelonio/square/ui/DownloadHandoff.kt b/app/src/main/java/dev/lelonio/square/ui/DownloadHandoff.ktnew file mode 100644index 0000000..fd9196f--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/ui/DownloadHandoff.kt@@ -0,0 +1,103 @@+package dev.lelonio.square.ui++import android.content.Context+import android.content.Intent+import android.content.pm.PackageManager+import dev.lelonio.square.backend.youtube.YouTubeBackend+import dev.lelonio.square.data.CatalogPlaylist++/**+ * Downloading, handed to whoever the user already trusts with it.+ *+ * Square does not fetch the file. Two attempts went the other way and both are+ * worth recording, because the reasons are the argument for this one.+ *+ * The first fetched the audio stream itself. It worked and it was not worth+ * having: one file at a time with no resuming, no notification while it ran, no+ * way to queue a playlist, no tags written into what came out, and nowhere in+ * the app to find the results. Most of that is not extraction, it is a download+ * manager, and writing a second-rate one inside a music player is a permanent+ * cost for a feature other people have finished.+ *+ * The second was going to bundle yt-dlp through youtubedl-android, which is+ * what the good downloaders use. Measured before writing any logic: the three+ * modules alone take the APK from 22.3 MB to 81.0 MB, most of it ffmpeg at+ * 35.6 MB and Python at 14.3 MB. That is roughly four times the whole app, paid+ * by everyone on every update, since the in-app updater fetches the APK whole.+ *+ * So the app sends a link. Where it goes is answered by what is installed.+ */++/** The public address of a playlist, album or artist, for something else to open. */+fun CatalogPlaylist.openLink(): String {+ val id = uri.substringAfterLast(':')+ return when {+ uri.startsWith(YouTubeBackend.PLAYLIST_PREFIX) ->+ "https://music.youtube.com/playlist?list=$id"+ uri.startsWith(YouTubeBackend.ARTIST_PREFIX) ->+ "https://music.youtube.com/channel/$id"+ uri.startsWith("spotify:album:") -> "https://open.spotify.com/album/$id"+ uri.startsWith("spotify:artist:") -> "https://open.spotify.com/artist/$id"+ else -> "https://open.spotify.com/playlist/$id"+ }+}++/**+ * The downloader this app hands links to.+ *+ * Named, rather than "whatever takes a link". The menu says YTDLnis and means+ * it: a chooser in front of someone who has already installed a downloader is+ * asking them to confirm a decision they made when they installed it, and a+ * chooser in front of someone who has not is a list of apps that cannot do the+ * job. Being specific lets the entry answer both cases properly.+ *+ * Must be declared in the manifest's `queries` block or it is invisible from+ * Android 11 on, and the lookup below can never find it.+ */+private const val YTDLNIS = "com.deniscerri.ytdl"++/**+ * Where to get it.+ *+ * F-Droid rather than the Play Store, where it is not and cannot be: it is+ * built on yt-dlp. The project lists GitHub releases, F-Droid and IzzyOnDroid+ * as its only trusted sources; this is the one that is an install page rather+ * than a list of files.+ */+private const val YTDLNIS_PAGE = "https://f-droid.org/en/packages/com.deniscerri.ytdl"++/** Whether the link will go straight there, or to the install page instead. */+fun downloaderInstalled(context: Context): Boolean =+ runCatching {+ context.packageManager.getPackageInfo(YTDLNIS, PackageManager.GET_ACTIVITIES)+ }.isSuccess++/**+ * Sends the link to YTDLnis, or offers to go and install it.+ *+ * Installed is not enough on its own to send straight in: an app can be present+ * and not handle the intent, and `setPackage` to one that does not throws+ * rather than falling back. The resolve is what makes the direct path safe.+ */+fun sendForDownload(context: Context, url: String) {+ val share = Intent(Intent.ACTION_SEND)+ .setType("text/plain")+ .putExtra(Intent.EXTRA_TEXT, url)++ val handled = context.packageManager+ .queryIntentActivities(Intent(share).setPackage(YTDLNIS), 0)+ .isNotEmpty()++ val intent = if (handled) {+ Intent(share).setPackage(YTDLNIS)+ } else {+ // Nothing is downloaded here and nothing is installed behind the user's+ // back: this opens the page and stops, and the rest is their decision.+ Intent(Intent.ACTION_VIEW, android.net.Uri.parse(YTDLNIS_PAGE))+ }++ runCatching { context.startActivity(intent) }+ .onFailure { android.util.Log.w(TAG, "could not hand off the link: $it") }+}++private const val TAG = "DownloadHandoff"diff --git a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt b/app/src/main/java/dev/lelonio/square/ui/SquareApp.ktindex f4b274c..f6fd71e 100644--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt@@ -98,6 +98,7 @@ import dev.lelonio.square.ui.components.TrackSheetActionimport dev.lelonio.square.ui.components.UpdateDialogimport dev.lelonio.square.update.Updaterimport dev.lelonio.square.ui.library.openLink+import com.adamglin.phosphoricons.regular.Downloadimport com.adamglin.phosphoricons.regular.LinkSimpleimport com.adamglin.phosphoricons.regular.PencilSimpleimport com.adamglin.phosphoricons.regular.Plus@@ -900,6 +901,20 @@ fun SquareApp(naming = NamingRequest(shownPlaylist)playlistMenu = null}+ // The whole playlist in one link. yt-dlp and the apps+ // built on it expand a playlist URL themselves, so this+ // is the entire "download an album" feature.+ if (backend == dev.lelonio.square.backend.BackendId.YOUTUBE_MUSIC) {+ val label = if (downloaderInstalled(context)) {+ stringResource(R.string.download_with)+ } else {+ stringResource(R.string.download_get_app)+ }+ TrackSheetAction(label, PhosphorIcons.Regular.Download) {+ playlistMenu = null+ sendForDownload(context, shownPlaylist.openLink())+ }+ }TrackSheetAction(stringResource(R.string.delete),PhosphorIcons.Regular.Trash,@@ -971,6 +986,24 @@ fun SquareApp(trackMenu = nullclipboard.setText(AnnotatedString(menu.track.openLink()))}+ // YouTube only. A Spotify link handed to a downloader+ // is a link it cannot do anything with, so offering the+ // action there would be offering a failure.+ if (backend == dev.lelonio.square.backend.BackendId.YOUTUBE_MUSIC) {+ // Says which app it will open, and says the other+ // thing when there is no app to open: a row that+ // promised a download and delivered a web page+ // would be the entry lying about what it does.+ val label = if (downloaderInstalled(context)) {+ stringResource(R.string.download_with)+ } else {+ stringResource(R.string.download_get_app)+ }+ TrackSheetAction(label, PhosphorIcons.Regular.Download) {+ trackMenu = null+ sendForDownload(context, menu.track.openLink())+ }+ }if (menu.removable) {TrackSheetAction(stringResource(R.string.remove_from_playlist),diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xmlindex 96525c8..1186476 100644--- a/app/src/main/res/values-de/strings.xml+++ b/app/src/main/res/values-de/strings.xml@@ -210,4 +210,6 @@<string name="update_prompt_size">Jetzt herunterladen und installieren? %1$s.</string><string name="update_later">Später</string><string name="update_install">Installieren</string>+ <string name="download_get_app">YTDLnis installieren</string>+ <string name="download_with">Mit YTDLnis herunterladen</string></resources>diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xmlindex dc1a2cc..2de847b 100644--- a/app/src/main/res/values-es/strings.xml+++ b/app/src/main/res/values-es/strings.xml@@ -210,4 +210,6 @@<string name="update_prompt_size">¿Descargarlo e instalarlo ahora? %1$s.</string><string name="update_later">Más tarde</string><string name="update_install">Instalar</string>+ <string name="download_get_app">Instalar YTDLnis</string>+ <string name="download_with">Descargar con YTDLnis</string></resources>diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xmlindex 6c1d47b..c10d261 100644--- a/app/src/main/res/values-fr/strings.xml+++ b/app/src/main/res/values-fr/strings.xml@@ -210,4 +210,6 @@<string name="update_prompt_size">La télécharger et l\'installer maintenant ? %1$s.</string><string name="update_later">Plus tard</string><string name="update_install">Installer</string>+ <string name="download_get_app">Installer YTDLnis</string>+ <string name="download_with">Télécharger avec YTDLnis</string></resources>diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xmlindex f385278..4c20984 100644--- a/app/src/main/res/values-it/strings.xml+++ b/app/src/main/res/values-it/strings.xml@@ -211,4 +211,6 @@<string name="update_prompt_size">Scaricarlo e installarlo adesso? %1$s.</string><string name="update_later">Più tardi</string><string name="update_install">Installa</string>+ <string name="download_get_app">Installa YTDLnis</string>+ <string name="download_with">Scarica con YTDLnis</string></resources>diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xmlindex fa95f14..4b75ad1 100644--- a/app/src/main/res/values-pt/strings.xml+++ b/app/src/main/res/values-pt/strings.xml@@ -210,4 +210,6 @@<string name="update_prompt_size">Transferir e instalar agora? %1$s.</string><string name="update_later">Mais tarde</string><string name="update_install">Instalar</string>+ <string name="download_get_app">Instalar YTDLnis</string>+ <string name="download_with">Transferir com YTDLnis</string></resources>diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xmlindex 192e341..146c11d 100644--- a/app/src/main/res/values/strings.xml+++ b/app/src/main/res/values/strings.xml@@ -231,4 +231,6 @@<string name="update_prompt_size">Download and install it now? %1$s.</string><string name="update_later">Later</string><string name="update_install">Install</string>+ <string name="download_get_app">Get YTDLnis</string>+ <string name="download_with">Download with YTDLnis</string></resources>