Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Keep the app alive while signing in to Spotify in the browser

Lelonio0960eb4470a2
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 903a544..0a05a60 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -125,6 +125,15 @@
android:exported="false"
android:foregroundServiceType="dataSync" />
+ <!--
+ Held while the listener signs in to Spotify in the browser, so the
+ process is not frozen before the redirect to 127.0.0.1 arrives.
+ -->
+ <service
+ android:name=".auth.LoginKeepAlive"
+ android:exported="false"
+ android:foregroundServiceType="dataSync" />
+
<service
android:name=".playback.PlaybackService"
android:exported="true"
diff --git a/app/src/main/java/dev/lelonio/square/auth/LoginKeepAlive.kt b/app/src/main/java/dev/lelonio/square/auth/LoginKeepAlive.kt
new file mode 100644
index 0000000..a6cc9af
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/auth/LoginKeepAlive.kt
@@ -0,0 +1,73 @@
+package dev.lelonio.square.auth
+
+import android.app.Notification
+import android.app.NotificationChannel
+import android.app.NotificationManager
+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 dev.lelonio.square.R
+
+/**
+ * Keeps the app awake while the listener signs in to Spotify in the browser.
+ *
+ * The sign-in comes back as a request to the app's own listener on
+ * 127.0.0.1, and the app is in the background the whole time the browser is
+ * up. Android freezes a background process with nothing in the foreground —
+ * by default since Android 14, on some phones before — and a frozen process
+ * still has its socket accept the browser's connection but never answers it:
+ * the Spotify page loaded for ever, then gave up with ERR_CONNECTION_TIMED_OUT,
+ * and the app came back to a spinner. A foreground service is what Android
+ * leaves running, so one is held for exactly as long as the sign-in waits.
+ */
+class LoginKeepAlive : Service() {
+
+ override fun onBind(intent: Intent?): IBinder? = null
+
+ override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
+ channel()
+ val notification = Notification.Builder(this, CHANNEL_ID)
+ .setContentTitle(getString(R.string.login_in_progress))
+ .setSmallIcon(android.R.drawable.stat_notify_sync)
+ .setOngoing(true)
+ .build()
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
+ startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
+ } else {
+ startForeground(NOTIFICATION_ID, notification)
+ }
+ return START_NOT_STICKY
+ }
+
+ private fun channel() {
+ val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
+ if (manager.getNotificationChannel(CHANNEL_ID) != null) return
+ manager.createNotificationChannel(
+ NotificationChannel(CHANNEL_ID, getString(R.string.login_channel), NotificationManager.IMPORTANCE_LOW),
+ )
+ }
+
+ companion object {
+ private const val CHANNEL_ID = "square_login"
+ private const val NOTIFICATION_ID = 4_301
+
+ /**
+ * Held from before the browser opens. Failing to start is not a
+ * reason to refuse the sign-in: it still works wherever the process
+ * is not frozen, as it did before this existed.
+ */
+ fun start(context: Context) {
+ runCatching {
+ context.startForegroundService(Intent(context, LoginKeepAlive::class.java))
+ }.onFailure { android.util.Log.w("SpotOAuth", "sign-in keep-alive not started: $it") }
+ }
+
+ /** Allowed from the background, unlike starting one. */
+ fun stop(context: Context) {
+ runCatching { context.stopService(Intent(context, LoginKeepAlive::class.java)) }
+ }
+ }
+}
diff --git a/app/src/main/java/dev/lelonio/square/auth/SpotifyOAuth.kt b/app/src/main/java/dev/lelonio/square/auth/SpotifyOAuth.kt
index 669eb81..8103229 100644
--- a/app/src/main/java/dev/lelonio/square/auth/SpotifyOAuth.kt
+++ b/app/src/main/java/dev/lelonio/square/auth/SpotifyOAuth.kt
@@ -153,9 +153,15 @@ object SpotifyOAuth {
activeReceiver = receiver
}
+ // Awake while the browser is up; see LoginKeepAlive. Started from
+ // here, while the app is still in the foreground the tap came from.
+ val appContext = context.applicationContext
+ LoginKeepAlive.start(appContext)
+
// When the coroutine completes or is cancelled, immediately unblock accept() by closing the socket
coroutineContext.job.invokeOnCompletion {
receiver.close()
+ LoginKeepAlive.stop(appContext)
}
receiver.use {
diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml
index a836097..c408726 100644
--- a/app/src/main/res/values-de/strings.xml
+++ b/app/src/main/res/values-de/strings.xml
@@ -517,4 +517,6 @@
<string name="recap_artists">Meistgehörte Künstler</string>
<string name="recap_partial">Gespeicherte Wiedergaben und der aktuelle Spotify-Verlauf. Frühere oder nicht synchronisierte Wiedergaben können fehlen. Minuten werden nur in Square gemessen.</string>
<string name="telegram_community">Telegram-Community</string>
+ <string name="login_in_progress">Anmeldung bei Spotify läuft…</string>
+ <string name="login_channel">Anmeldung</string>
</resources>
diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml
index 1a2edae..9b3cc3c 100644
--- a/app/src/main/res/values-es/strings.xml
+++ b/app/src/main/res/values-es/strings.xml
@@ -523,4 +523,6 @@
<string name="recap_artists">Artistas más escuchados</string>
<string name="recap_partial">Incluye las escuchas guardadas y el historial reciente de Spotify. Pueden faltar escuchas anteriores o sin sincronizar. Los minutos solo se miden en Square.</string>
<string name="telegram_community">Comunidad de Telegram</string>
+ <string name="login_in_progress">Iniciando sesión en Spotify…</string>
+ <string name="login_channel">Inicio de sesión</string>
</resources>
diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml
index 06c5582..d44d425 100644
--- a/app/src/main/res/values-fr/strings.xml
+++ b/app/src/main/res/values-fr/strings.xml
@@ -517,4 +517,6 @@
<string name="recap_artists">Artistes les plus écoutés</string>
<string name="recap_partial">Inclut les écoutes enregistrées et le récent historique Spotify. Certaines écoutes anciennes ou non synchronisées peuvent manquer. Les minutes sont mesurées uniquement dans Square.</string>
<string name="telegram_community">Communauté Telegram</string>
+ <string name="login_in_progress">Connexion à Spotify en cours…</string>
+ <string name="login_channel">Connexion</string>
</resources>
diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xml
index e06fb14..3feb62f 100644
--- a/app/src/main/res/values-hi/strings.xml
+++ b/app/src/main/res/values-hi/strings.xml
@@ -532,4 +532,6 @@
<string name="recap_artists">सबसे अधिक सुने गए कलाकार</string>
<string name="recap_partial">सहेजी गई सुनने की गतिविधि और Spotify का हाल का इतिहास शामिल है। पुरानी या सिंक नहीं हुई गतिविधि छूट सकती है। मिनट केवल Square में मापे जाते हैं।</string>
<string name="telegram_community">Telegram समुदाय</string>
+ <string name="login_in_progress">Spotify में साइन इन हो रहा है…</string>
+ <string name="login_channel">साइन इन</string>
</resources>
diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml
index 2b40807..d339811 100644
--- a/app/src/main/res/values-it/strings.xml
+++ b/app/src/main/res/values-it/strings.xml
@@ -554,4 +554,6 @@
<string name="smart_shuffle">Smart Shuffle</string>
<string name="smart_shuffle_unavailable">Smart Shuffle non è disponibile per questa playlist al momento.</string>
<string name="telegram_community">Community Telegram</string>
+ <string name="login_in_progress">Accesso a Spotify in corso…</string>
+ <string name="login_channel">Accesso</string>
</resources>
diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml
index 95f0095..5ac4018 100644
--- a/app/src/main/res/values-pt/strings.xml
+++ b/app/src/main/res/values-pt/strings.xml
@@ -517,4 +517,6 @@
<string name="recap_artists">Artistas mais ouvidos</string>
<string name="recap_partial">Inclui as reproduções salvas e o histórico recente do Spotify. Reproduções anteriores ou não sincronizadas podem estar ausentes. Os minutos são medidos apenas no Square.</string>
<string name="telegram_community">Comunidade do Telegram</string>
+ <string name="login_in_progress">Entrando no Spotify…</string>
+ <string name="login_channel">Entrar</string>
</resources>
diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml
index 0982b2b..108bd58 100644
--- a/app/src/main/res/values-ru/strings.xml
+++ b/app/src/main/res/values-ru/strings.xml
@@ -544,4 +544,6 @@
<string name="recap_artists">Любимые исполнители</string>
<string name="recap_partial">Учитываются сохранённые прослушивания и недавняя история Spotify. Старые или несинхронизированные прослушивания могут отсутствовать. Минуты измеряются только в Square.</string>
<string name="telegram_community">Сообщество Telegram</string>
+ <string name="login_in_progress">Вход в Spotify…</string>
+ <string name="login_channel">Вход</string>
</resources>
diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml
index d61cf88..916176b 100644
--- a/app/src/main/res/values-tr/strings.xml
+++ b/app/src/main/res/values-tr/strings.xml
@@ -530,4 +530,6 @@
<string name="recap_artists">En çok dinlenen sanatçılar</string>
<string name="recap_partial">Kaydedilen dinlemeler ve yakın tarihli Spotify geçmişi dahildir. Eski veya eşitlenmemiş dinlemeler eksik olabilir. Dakikalar yalnızca Square içinde ölçülür.</string>
<string name="telegram_community">Telegram topluluğu</string>
+ <string name="login_in_progress">Spotify’da oturum açılıyor…</string>
+ <string name="login_channel">Oturum açma</string>
</resources>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index eb83117..bf506ac 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -581,4 +581,6 @@
<string name="smart_shuffle">Smart Shuffle</string>
<string name="smart_shuffle_unavailable">Smart Shuffle is not available for this playlist right now.</string>
<string name="telegram_community">Telegram community</string>
+ <string name="login_in_progress">Signing in to Spotify…</string>
+ <string name="login_channel">Signing in</string>
</resources>