diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 1f80bb372b..e0f1038414 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -8,6 +8,8 @@
+
+
diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt
index da9f63bf0b..45ac349622 100644
--- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt
+++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt
@@ -167,6 +167,8 @@ class MainActivity : ComponentActivity() {
private var gameStoresEnabled by mutableStateOf(false)
private var gameStoresSpeedTier by mutableStateOf("fast")
private var storesOpenTab by mutableStateOf(SessionPrefs.STORES_OPEN_LIBRARY)
+ /** A page a notification asked for ("stores:downloads", "app:"), until the front end has gone there. */
+ private var navRequest by mutableStateOf(null)
private var storesShowMature by mutableStateOf(false)
private var mic by mutableStateOf(false)
private var wifiDiscovery by mutableStateOf(false)
@@ -441,6 +443,7 @@ class MainActivity : ComponentActivity() {
}
private fun readGameIntent(request: Intent) {
+ request.getStringExtra(com.droiddeck.launcher.stores.download.StoreDownloadService.EXTRA_NAV)?.let { navRequest = it; request.removeExtra(com.droiddeck.launcher.stores.download.StoreDownloadService.EXTRA_NAV) }
shortcutPicker = request.action == Intent.ACTION_CREATE_SHORTCUT
if (shortcutPicker) {
// The shortcut picker must only offer games from the scan started for this request.
@@ -587,6 +590,7 @@ class MainActivity : ComponentActivity() {
gameStoresEnabled = gameStoresEnabled,
gameStoresSpeedTier = gameStoresSpeedTier,
storesOpenTab = storesOpenTab,
+ navigate = navRequest,
storesShowMature = storesShowMature,
storeDownloadsActive = com.droiddeck.launcher.stores.StoresState.activeDownloads,
),
@@ -719,6 +723,7 @@ class MainActivity : ComponentActivity() {
onGameStoresEnabled = { on -> SessionPrefs.setGameStoresEnabled(this, on); gameStoresEnabled = on; if (on) com.droiddeck.launcher.stores.StoresState.refresh(this) },
onGameStoresSpeedTier = { tier -> SessionPrefs.setGameStoresSpeedTier(this, tier); gameStoresSpeedTier = tier },
onStoresOpenTab = { tab -> SessionPrefs.setStoresOpenTab(this, tab); storesOpenTab = tab },
+ onNavigated = { navRequest = null },
onStoresShowMature = { show -> SessionPrefs.setStoresShowMature(this, show); storesShowMature = show },
onLibraryChanged = { refreshAddedGames(); refresh() },
onAnimationsEnabled = { on ->
diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt
index c0be147ce6..b6d0ec2a1f 100644
--- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt
+++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt
@@ -1417,6 +1417,9 @@ class SessionService : Service() {
SessionState.guestPid = -1
com.droiddeck.launcher.agent.AgentGuest.stop()
com.droiddeck.launcher.stores.StoreLaunchRequests.stop()
+ // Cloud saves of a store game played this session, if the compat tool's exit request did
+ // not upload them (a session closed from the drawer never reaches it).
+ Thread({ com.droiddeck.launcher.stores.CloudSaves.uploadAllDirty(applicationContext, "session-end", running = false) }, "cloud-session-end").start()
runCatching { com.droiddeck.launcher.agent.AgentEnv.endSession(this) }
.onFailure { Log.w(TAG, "clearing the agent's session environment", it) }
releaseLocks()
diff --git a/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt b/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt
index e3958da2fb..3d877784fd 100644
--- a/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt
+++ b/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt
@@ -315,6 +315,49 @@ object CloudSaves {
return Result("ok", send.size, send.sumOf { files.getValue(it).length() }, if (send.isEmpty()) "up-to-date" else "uploaded", left.size)
}
+ // ---- uploads that do not depend on one hook ---------------------------------------------------
+
+ /**
+ * A game launched with cloud saves is marked dirty in the app's storage when its pre-launch step
+ * runs. The upload happens on the first of: the compat tool's exit request (`exit`), the end of
+ * the session (`session-end`), or the app's next start for a mark a killed app left (`recovery`)
+ * - so a session closed from the drawer, a game Steam killed, or a crash still uploads. The mark
+ * goes only after an upload that went through, or one skipped on purpose with its reason logged.
+ */
+ private fun marks(context: Context) = context.applicationContext.getSharedPreferences("stores.cloud", Context.MODE_PRIVATE)
+
+ fun markDirty(context: Context, store: Store, id: String) {
+ marks(context).edit().putLong("dirty:${store.id}:$id", System.currentTimeMillis()).apply()
+ }
+
+ fun dirty(context: Context): List> = marks(context).all.keys.mapNotNull { k ->
+ val parts = k.split(':', limit = 3)
+ if (parts.size == 3 && parts[0] == "dirty") Store.byId(parts[1])?.let { it to parts[2] } else null
+ }
+
+ private val markLock = Any()
+
+ /** Why a skip is final: retrying cannot change it, so the mark goes. */
+ private val FINAL_SKIPS = setOf("off", "no-cloud-saves", "no-baseline", "not-installed", "nothing-local")
+
+ /** Uploads [id] if it is marked dirty; null when it is not (another trigger got there first). */
+ fun uploadDirty(context: Context, store: Store, id: String, trigger: String): Result? = synchronized(markLock) {
+ val key = "dirty:${store.id}:$id"
+ if (!marks(context).contains(key)) return null
+ val r = sync(context, store, id, "up trigger=$trigger") { dir, cloud -> upWith(context, store, id, dir, cloud, force = false) }
+ if (r.result == "ok" || (r.result == "skipped" && r.reason in FINAL_SKIPS)) marks(context).edit().remove(key).apply()
+ r
+ }
+
+ /**
+ * Every dirty game, uploaded; [running] says a game session is up, in which case `recovery` waits
+ * - a marked game may be the one running.
+ */
+ fun uploadAllDirty(context: Context, trigger: String, running: Boolean = com.droiddeck.launcher.session.SessionState.running) {
+ if (trigger == "recovery" && running) return
+ for ((store, id) in dirty(context)) uploadDirty(context, store, id, trigger)
+ }
+
private fun backupRoot(context: Context, store: Store, id: String) =
File(context.filesDir, "stores/cloud-backups/${store.id}-${id.replace(Regex("[^A-Za-z0-9._-]"), "_")}")
diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt
index 37beee100a..ee8c623627 100644
--- a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt
+++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt
@@ -69,12 +69,16 @@ object StoreLaunchRequests {
val store = Store.byId(request.optString("store"))
val id = request.optString("id")
if (store == null || id.isEmpty()) JSONObject().put("ok", false).put("reason", "no-game")
- else CloudSaves.downloadOrDefer(app, store, id).let { JSONObject().put("ok", it.ok).put("result", it.result).put("files", it.files).put("reason", it.reason) }
+ else {
+ // Marked before the game runs: whatever ends it, an upload follows (CloudSaves.uploadDirty).
+ CloudSaves.markDirty(app, store, id)
+ CloudSaves.downloadOrDefer(app, store, id).let { JSONObject().put("ok", it.ok).put("result", it.result).put("files", it.files).put("reason", it.reason) }
+ }
}
"cloud-up" -> {
val store = Store.byId(request.optString("store"))
val id = request.optString("id")
- if (store != null && id.isNotEmpty()) Thread({ CloudSaves.upload(app, store, id) }, "cloud-up").start()
+ if (store != null && id.isNotEmpty()) Thread({ CloudSaves.uploadDirty(app, store, id, "exit") }, "cloud-up").start()
JSONObject().put("ok", true)
}
// A browser a game opens inside the session (an EOS device sign-in, a store page):
diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt
index 0b6ed682b7..02111bed57 100644
--- a/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt
+++ b/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt
@@ -170,6 +170,23 @@ object StoresState {
}, "stores-signout").start()
}
+ /** A notification asked for the Downloads chip; the Stores page takes it and clears it. */
+ var openDownloads by mutableStateOf(false)
+
+ /**
+ * Android 13+: notifications need permission. Asked once, the first time a download starts from
+ * a screen; refused, downloads still run, without their notification.
+ */
+ private fun askForNotifications(context: Context) {
+ val activity = context as? android.app.Activity ?: return
+ if (android.os.Build.VERSION.SDK_INT < 33) return
+ if (activity.checkSelfPermission("android.permission.POST_NOTIFICATIONS") == android.content.pm.PackageManager.PERMISSION_GRANTED) return
+ val p = activity.getSharedPreferences("stores", Context.MODE_PRIVATE)
+ if (p.getBoolean("asked_notifications", false)) return
+ p.edit().putBoolean("asked_notifications", true).apply()
+ runCatching { activity.requestPermissions(arrayOf("android.permission.POST_NOTIFICATIONS"), 7102) }
+ }
+
/** An install waiting for the user to say where: the page shows the choice while this is set. */
var pendingInstall by mutableStateOf(null)
@@ -179,6 +196,7 @@ object StoresState {
* storage at once.
*/
fun requestInstall(context: Context, item: CatalogItem) {
+ askForNotifications(context)
// A resume goes where the files already are; there is nothing to choose.
if (isUnfinished(item)) { install(context, item); return }
val targets = StoreInstallRoot.targets(context)
@@ -229,6 +247,9 @@ object StoresState {
// The EOS overlay an earlier build downloaded (656 MB) is gone with the feature.
val overlay = java.io.File(com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(context), "root/.local/share/droiddeck/epic-overlay")
if (overlay.exists()) Thread({ overlay.deleteRecursively(); Log.i(TAG, "removed the old EOS overlay download") }, "epic-overlay-cleanup").start()
+ // Cloud saves a killed app never uploaded; not while a session (and maybe that game) runs.
+ val app = context.applicationContext
+ Thread({ CloudSaves.uploadAllDirty(app, "recovery") }, "cloud-recovery").start()
}
internal fun post(block: () -> Unit) = main.post(block)
diff --git a/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadQueue.kt b/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadQueue.kt
index 7076c850e3..7b6abd8f22 100644
--- a/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadQueue.kt
+++ b/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadQueue.kt
@@ -353,8 +353,15 @@ object DownloadQueue {
StoresState.logLine("installed \"${item.entry.name}\" → ${result}")
// The folder is on disk and registered: the Installed tab and the Games list follow.
StoresState.notifyLibraryChanged(context)
+ val games = runCatching {
+ com.droiddeck.launcher.frontend.Library.launchableGames(context).firstOrNull { it.gameFiles?.absolutePath == result }?.let { "app:${it.appId}" }
+ }.getOrNull()
+ StoreDownloadService.ended(context, item.entry, installed = true, gamesKey = games)
+ }
+ DownloadState.FAILED -> {
+ StoresState.logLine("${item.entry.store.label}: \"${item.entry.name}\" failed: $error")
+ StoreDownloadService.ended(context, item.entry, installed = false, gamesKey = null)
}
- DownloadState.FAILED -> StoresState.logLine("${item.entry.store.label}: \"${item.entry.name}\" failed: $error")
DownloadState.CANCELLED -> StoresState.notifyLibraryChanged(context)
else -> {}
}
diff --git a/app/src/main/java/com/droiddeck/launcher/stores/download/StoreDownloadService.kt b/app/src/main/java/com/droiddeck/launcher/stores/download/StoreDownloadService.kt
index 6e1ee292fb..2ea30d37b8 100644
--- a/app/src/main/java/com/droiddeck/launcher/stores/download/StoreDownloadService.kt
+++ b/app/src/main/java/com/droiddeck/launcher/stores/download/StoreDownloadService.kt
@@ -1,5 +1,6 @@
package com.droiddeck.launcher.stores.download
+import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
@@ -7,76 +8,167 @@ import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
+import android.net.wifi.WifiManager
import android.os.Build
+import android.os.Handler
import android.os.IBinder
+import android.os.Looper
+import android.os.PowerManager
import android.util.Log
import com.droiddeck.launcher.MainActivity
import com.droiddeck.launcher.R
import com.droiddeck.launcher.stores.StoresState
import com.droiddeck.launcher.stores.formatBytes
+import com.droiddeck.launcher.stores.formatSpeed
import java.util.concurrent.ConcurrentHashMap
/**
- * Keeps the process alive while a store download runs, with one quiet notification for the queue.
- * Without it Android demotes the app as soon as the launcher leaves the screen and a multi-gigabyte
- * install dies half way; the session has its own service for the same reason.
+ * Keeps store downloads going with the screen off or the app in the background: a foreground
+ * service (dataSync) with a notification showing the active download's progress, Pause / Resume
+ * and a two-step Cancel; a Wi-Fi lock and a partial wake lock while anything is running, so the
+ * radio and the CPU do not sleep under a multi-gigabyte install; and a notification when a game
+ * has installed or failed.
*
- * [start] is called when a download begins and [finish] when one ends; the notification is rebuilt
- * from the queue's live rows, and the service stops itself when nothing is active.
+ * [start] is called when a download begins and [finish] when one ends; the service stops itself when
+ * nothing is running.
*/
class StoreDownloadService : Service() {
+ private var wifi: WifiManager.WifiLock? = null
+ private var wake: PowerManager.WakeLock? = null
+ private var wakeAt = 0L
+ /** The download whose Cancel was tapped once: its actions read Delete / Keep until [confirmUntil]. */
+ private var confirmKey: String? = null
+ private var confirmUntil = 0L
+ private val main = Handler(Looper.getMainLooper())
+
override fun onCreate() {
super.onCreate()
instance = this
- val nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
- if (nm.getNotificationChannel(CHANNEL) == null) {
- nm.createNotificationChannel(NotificationChannel(CHANNEL, getString(R.string.stores_notification_channel), NotificationManager.IMPORTANCE_LOW).apply {
- description = getString(R.string.stores_notification_channel_hint)
- setShowBadge(false)
- })
- }
+ channels(this)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
instance = this
startForegroundCompat(build())
- if (active.isEmpty()) stopNow()
+ val key = intent?.getStringExtra(EXTRA_KEY)
+ when (intent?.action) {
+ ACTION_PAUSE -> key?.let {
+ DownloadQueue.pause(it)
+ // The progress notification goes with the last running download; this one stays to resume from.
+ pausedNote(this, it)
+ }
+ ACTION_RESUME -> key?.let {
+ runCatching { getSystemService(NotificationManager::class.java)?.cancel(pausedId(it)) }
+ DownloadQueue.resume(this, it)
+ }
+ ACTION_CANCEL -> key?.let {
+ confirmKey = it; confirmUntil = System.currentTimeMillis() + CONFIRM_MS
+ main.postDelayed({ if (System.currentTimeMillis() >= confirmUntil) { confirmKey = null; refresh() } }, CONFIRM_MS + 100)
+ }
+ ACTION_DELETE -> key?.let { confirmKey = null; DownloadQueue.cancel(this, it) }
+ ACTION_KEEP -> confirmKey = null
+ }
+ refresh()
+ if (active.isEmpty() && StoresState.latestDownloads.none { it.state == DownloadState.RUNNING }) stopNow()
return START_NOT_STICKY
}
override fun onDestroy() {
+ releaseLocks()
if (instance === this) instance = null
super.onDestroy()
}
override fun onBind(intent: Intent?): IBinder? = null
- private fun build(): Notification {
- val entries = StoresState.latestDownloads.filter { it.state == DownloadState.RUNNING }
- val first = entries.firstOrNull()
- val title = if (active.size > 1) getString(R.string.stores_notification_many, active.size) else getString(R.string.stores_notification_one)
- val text = when {
- first == null -> getString(R.string.stores_notification_waiting)
- first.stage == com.droiddeck.launcher.stores.download.DownloadStage.DOWNLOAD && first.bytesTotal > 0 -> "${first.name} · ${first.percent}% (${formatBytes(first.bytesDone)} / ${formatBytes(first.bytesTotal)})"
- first.stageFraction >= 0f -> "${first.name} · ${first.percent}%"
- else -> first.name
- }
- val tap = PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java), PendingIntent.FLAG_IMMUTABLE)
- return Notification.Builder(this, CHANNEL)
- .setSmallIcon(android.R.drawable.stat_sys_download)
- .setContentTitle(title).setContentText(text).setOngoing(true).setContentIntent(tap).build()
+ private fun pending(action: String, key: String, code: Int): PendingIntent {
+ val i = Intent(this, StoreDownloadService::class.java).setAction(action).putExtra(EXTRA_KEY, key)
+ return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) PendingIntent.getForegroundService(this, code, i, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
+ else PendingIntent.getService(this, code, i, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
}
+ private fun build(): Notification {
+ val rows = StoresState.latestDownloads.filter { it.isActive }
+ val first = rows.firstOrNull { it.state == DownloadState.RUNNING } ?: rows.firstOrNull()
+ val title = if (rows.size > 1) getString(R.string.stores_notification_many, rows.size) else first?.name ?: getString(R.string.stores_notification_one)
+ val b = Notification.Builder(this, CHANNEL)
+ .setSmallIcon(android.R.drawable.stat_sys_download)
+ .setContentTitle(title)
+ .setOngoing(true).setOnlyAlertOnce(true)
+ .setContentIntent(openDownloads(this, 1))
+ if (first == null) return b.setContentText(getString(R.string.stores_notification_waiting)).setProgress(0, 0, true).build()
+ b.setContentText(DownloadNotificationText.line(first, stageName(first.stage), getString(R.string.stores_dl_paused), getString(R.string.stores_dl_queued)))
+ val fraction = first.stageFraction
+ if (fraction >= 0f) b.setProgress(1000, (fraction * 1000).toInt(), false) else b.setProgress(0, 0, true)
+ val code = first.key.hashCode()
+ if (confirmKey == first.key && System.currentTimeMillis() < confirmUntil) {
+ b.addAction(Notification.Action.Builder(null, getString(R.string.stores_notification_delete), pending(ACTION_DELETE, first.key, code + 3)).build())
+ b.addAction(Notification.Action.Builder(null, getString(R.string.stores_notification_keep), pending(ACTION_KEEP, first.key, code + 4)).build())
+ } else {
+ if (first.state == DownloadState.PAUSED) b.addAction(Notification.Action.Builder(null, getString(R.string.stores_dl_resume), pending(ACTION_RESUME, first.key, code + 1)).build())
+ else b.addAction(Notification.Action.Builder(null, getString(R.string.stores_dl_pause), pending(ACTION_PAUSE, first.key, code)).build())
+ b.addAction(Notification.Action.Builder(null, getString(R.string.stores_dl_cancel), pending(ACTION_CANCEL, first.key, code + 2)).build())
+ }
+ return b.build()
+ }
+
+ private fun stageName(stage: DownloadStage): String = getString(when (stage) {
+ DownloadStage.MANIFEST -> R.string.stores_stage_manifest
+ DownloadStage.DOWNLOAD -> R.string.stores_stage_downloading
+ DownloadStage.VERIFY -> R.string.stores_stage_verifying
+ DownloadStage.INSTALL -> R.string.stores_stage_installing
+ DownloadStage.DONE -> R.string.stores_stage_done
+ })
+
private fun refresh() {
+ locks()
runCatching { (getSystemService(NOTIFICATION_SERVICE) as NotificationManager).notify(ID, build()) }
}
+ /**
+ * While a download runs: a Wi-Fi lock (high-performance before Android 14, low-latency from it,
+ * where the old mode is deprecated) and a partial wake lock, taken with a timeout and renewed
+ * as progress comes in rather than held indefinitely. Released as soon as nothing runs.
+ */
+ @SuppressLint("WakelockTimeout")
+ private fun locks() {
+ val running = StoresState.latestDownloads.any { it.state == DownloadState.RUNNING }
+ if (!running) { releaseLocks(); return }
+ if (wifi == null) {
+ runCatching {
+ val wm = applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
+ @Suppress("DEPRECATION")
+ val mode = if (Build.VERSION.SDK_INT >= 34) WifiManager.WIFI_MODE_FULL_LOW_LATENCY else WifiManager.WIFI_MODE_FULL_HIGH_PERF
+ wifi = wm.createWifiLock(mode, "DroidDeck:store-download").apply { setReferenceCounted(false); acquire() }
+ Log.i(TAG, "wifi lock held")
+ }
+ }
+ val now = System.currentTimeMillis()
+ if (wake == null) {
+ wake = (getSystemService(POWER_SERVICE) as PowerManager).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DroidDeck:store-download").apply { setReferenceCounted(false) }
+ Log.i(TAG, "wake lock held")
+ }
+ if (now - wakeAt > WAKE_RENEW_MS) {
+ wake?.acquire(WAKE_TIMEOUT_MS)
+ wakeAt = now
+ }
+ }
+
+ private fun releaseLocks() {
+ wifi?.let { runCatching { if (it.isHeld) it.release() }; Log.i(TAG, "wifi lock released") }
+ wifi = null
+ wake?.let { runCatching { if (it.isHeld) it.release() }; Log.i(TAG, "wake lock released") }
+ wake = null
+ wakeAt = 0L
+ }
+
private fun startForegroundCompat(n: Notification) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) startForeground(ID, n, android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
else startForeground(ID, n)
}
private fun stopNow() {
+ releaseLocks()
stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf()
}
@@ -84,10 +176,41 @@ class StoreDownloadService : Service() {
companion object {
private const val TAG = "StoreDownloadService"
private const val CHANNEL = "store_downloads"
+ private const val DONE_CHANNEL = "store_downloads_done"
private const val ID = 9102
+ private const val CONFIRM_MS = 10_000L
+ private const val WAKE_TIMEOUT_MS = 10 * 60_000L
+ private const val WAKE_RENEW_MS = 5 * 60_000L
+ private const val EXTRA_KEY = "key"
+ private const val ACTION_PAUSE = "droiddeck.stores.PAUSE"
+ private const val ACTION_RESUME = "droiddeck.stores.RESUME"
+ private const val ACTION_CANCEL = "droiddeck.stores.CANCEL"
+ private const val ACTION_DELETE = "droiddeck.stores.DELETE"
+ private const val ACTION_KEEP = "droiddeck.stores.KEEP"
+ /** MainActivity: which page to open ("stores:downloads", or a Games selection such as "app:"). */
+ const val EXTRA_NAV = "droiddeck.nav"
private val active = ConcurrentHashMap.newKeySet()
@Volatile private var instance: StoreDownloadService? = null
+ private fun channels(context: Context) {
+ val nm = context.getSystemService(NotificationManager::class.java) ?: return
+ if (nm.getNotificationChannel(CHANNEL) == null) {
+ nm.createNotificationChannel(NotificationChannel(CHANNEL, context.getString(R.string.stores_notification_channel), NotificationManager.IMPORTANCE_LOW).apply {
+ description = context.getString(R.string.stores_notification_channel_hint)
+ setShowBadge(false)
+ })
+ }
+ if (nm.getNotificationChannel(DONE_CHANNEL) == null) {
+ nm.createNotificationChannel(NotificationChannel(DONE_CHANNEL, context.getString(R.string.stores_notification_done_channel), NotificationManager.IMPORTANCE_DEFAULT))
+ }
+ }
+
+ private fun openPage(context: Context, nav: String, code: Int): PendingIntent =
+ PendingIntent.getActivity(context, code, Intent(context, MainActivity::class.java).putExtra(EXTRA_NAV, nav)
+ .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP), PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
+
+ private fun openDownloads(context: Context, code: Int) = openPage(context, "stores:downloads", code)
+
/** A download started: the service is up (idempotent). */
fun start(context: Context) {
StoresState.latestDownloads.filter { it.state == DownloadState.RUNNING }.forEach { active.add(it.key) }
@@ -100,12 +223,12 @@ class StoreDownloadService : Service() {
}
}
- /** Progress moved: redraw the notification line (StoresState calls it at most once a second). */
+ /** Progress moved: redraw the notification (StoresState calls it at most once a second). */
fun tick() {
instance?.refresh()
}
- /** The queue changed: redraw the notification line. */
+ /** The queue changed: redraw the notification. */
fun sync(context: Context) {
instance?.refresh()
}
@@ -116,5 +239,61 @@ class StoreDownloadService : Service() {
val inst = instance ?: return
if (active.isEmpty() && StoresState.latestDownloads.none { it.state == DownloadState.RUNNING }) inst.stopNow() else inst.refresh()
}
+
+ private fun pausedId(key: String) = ID + 0x20000 + (key.hashCode() and 0xFFFF)
+
+ /** " paused" with Resume, once the progress notification has gone with the service. */
+ private fun pausedNote(context: Context, key: String) {
+ val entry = StoresState.latestDownloads.firstOrNull { it.key == key } ?: return
+ val i = Intent(context, StoreDownloadService::class.java).setAction(ACTION_RESUME).putExtra(EXTRA_KEY, key)
+ val resume = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) PendingIntent.getForegroundService(context, key.hashCode() + 5, i, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
+ else PendingIntent.getService(context, key.hashCode() + 5, i, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
+ val n = Notification.Builder(context, DONE_CHANNEL)
+ .setSmallIcon(android.R.drawable.stat_sys_download)
+ .setContentTitle(entry.name).setContentText(context.getString(R.string.stores_dl_paused))
+ .setContentIntent(resume).setAutoCancel(true)
+ .addAction(Notification.Action.Builder(null, context.getString(R.string.stores_dl_resume), resume).build())
+ .build()
+ runCatching { context.getSystemService(NotificationManager::class.java)?.notify(pausedId(key), n) }
+ }
+
+ /** " installed" (opens it in Games) or " download failed" (opens Downloads); dismissible. */
+ fun ended(context: Context, entry: DownloadEntry, installed: Boolean, gamesKey: String?) {
+ val app = context.applicationContext
+ channels(app)
+ val code = entry.key.hashCode()
+ val n = Notification.Builder(app, DONE_CHANNEL)
+ .setSmallIcon(if (installed) android.R.drawable.stat_sys_download_done else android.R.drawable.stat_notify_error)
+ .setContentTitle(app.getString(if (installed) R.string.stores_notification_installed else R.string.stores_notification_failed, entry.name))
+ .setAutoCancel(true)
+ .setContentIntent(if (installed && gamesKey != null) openPage(app, gamesKey, code + 10) else openDownloads(app, code + 11))
+ .build()
+ runCatching { app.getSystemService(NotificationManager::class.java)?.notify(ID + 1 + (code and 0xFFFF), n) }
+ }
+ }
+}
+
+/** The notification's one line for a download. Pure, for the test. */
+object DownloadNotificationText {
+ fun line(d: DownloadEntry, stageName: String, paused: String, queued: String): String {
+ val parts = ArrayList()
+ parts += d.name
+ when {
+ d.state == DownloadState.PAUSED -> parts += paused
+ d.state == DownloadState.QUEUED -> parts += queued
+ d.stage == DownloadStage.DOWNLOAD -> {
+ if (d.bytesTotal > 0) { parts += "${d.percent}%"; parts += "${formatBytes(d.bytesDone)}/${formatBytes(d.bytesTotal)}" }
+ if (d.speedBps > 0) parts += formatSpeed(d.speedBps)
+ if (d.etaSeconds >= 0) parts += eta(d.etaSeconds)
+ }
+ else -> parts += if (d.stageFraction >= 0f) "$stageName ${d.percent}%" else stageName
+ }
+ return parts.joinToString(" · ")
+ }
+
+ fun eta(seconds: Long): String = when {
+ seconds < 60 -> "<1 min"
+ seconds < 3600 -> "${seconds / 60} min"
+ else -> "${seconds / 3600} h ${(seconds % 3600) / 60} min"
}
}
diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt
index 534979d164..e9717dc651 100644
--- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt
+++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt
@@ -135,6 +135,8 @@ class FrontEndState(
val gameStoresSpeedTier: String = "fast",
/** Setup › Stores: the tab a signed-in store opens on (SessionPrefs.STORES_OPEN_LIBRARY / STORE). */
val storesOpenTab: String = SessionPrefs.STORES_OPEN_LIBRARY,
+ /** A page to open once: "stores:downloads", or a selection such as "app:" (a notification's tap). */
+ val navigate: String? = null,
val storesShowMature: Boolean = false,
/** Downloads queued, running or paused across the three stores; the rail item's badge. */
val storeDownloadsActive: Int = 0,
@@ -191,6 +193,7 @@ class FrontEndActions(
val onGameStoresEnabled: (Boolean) -> Unit = {},
val onGameStoresSpeedTier: (String) -> Unit = {},
val onStoresOpenTab: (String) -> Unit = {},
+ val onNavigated: () -> Unit = {},
val onStoresShowMature: (Boolean) -> Unit = {},
/** The Games list rebuilt now: a store install or removal changed what is on disk. */
val onLibraryChanged: () -> Unit = {},
@@ -371,6 +374,11 @@ fun FrontEndScreen(s: FrontEndState, a: FrontEndActions, page: (@Composable () -
private fun FrontEndScreenBody(s: FrontEndState, a: FrontEndActions, page: (@Composable () -> Unit)?, frontFocus: FrontFocus) {
var selected by rememberSaveable { mutableStateOf(if (s.shortcutPicker) "games" else "steam") }
LaunchedEffect(s.shortcutPicker) { if (s.shortcutPicker) selected = "games" }
+ LaunchedEffect(s.navigate) {
+ val target = s.navigate ?: return@LaunchedEffect
+ if (target == "stores:downloads") { com.droiddeck.launcher.stores.StoresState.openDownloads = true; selected = "stores" } else selected = target
+ a.onNavigated()
+ }
var showWirelessAdbFix by rememberSaveable { mutableStateOf(false) }
var showDeveloperDisplayChoice by rememberSaveable { mutableStateOf(false) }
var wirelessAdbDesiredEnabled by rememberSaveable { mutableStateOf(false) }
diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt
index b5c8e53b50..defad024a2 100644
--- a/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt
+++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt
@@ -108,6 +108,7 @@ internal fun StoresPage(s: FrontEndState, a: FrontEndActions, modifier: Modifier
var query by rememberSaveable { mutableStateOf("") }
var openGame by rememberSaveable { mutableStateOf(null) }
var settings by rememberSaveable { mutableStateOf(false) }
+ LaunchedEffect(StoresState.openDownloads) { if (StoresState.openDownloads) { chip = DOWNLOADS; StoresState.openDownloads = false } }
val store = Store.byId(chip)
// A tab this store does not have (Store or All on Amazon) reads as Library.
val shownTab = tabFor(store, tab)
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index f1b4120a72..6492e8bc36 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -2098,6 +2098,11 @@
Downloading
%1$d downloads
Preparing…
+ Finished store downloads
+ %1$s installed
+ %1$s download failed
+ Delete
+ Keep
Sign in to %1$s
Sign in with your provider
Reload
diff --git a/app/src/test/java/com/droiddeck/launcher/stores/CloudPlanTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/CloudPlanTest.kt
index 117518a911..854c04a9c6 100644
--- a/app/src/test/java/com/droiddeck/launcher/stores/CloudPlanTest.kt
+++ b/app/src/test/java/com/droiddeck/launcher/stores/CloudPlanTest.kt
@@ -6,8 +6,13 @@ import com.droiddeck.launcher.stores.CloudPlan.Remote
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import org.robolectric.annotation.Config
-/** The rules that keep a cloud sync from replacing real saves. */
+/** The rules that keep a cloud sync from replacing real saves. (Robolectric for org.json.) */
+@RunWith(RobolectricTestRunner::class)
+@Config(sdk = [33])
class CloudPlanTest {
@Test fun noUploadBeforeADownloadHasSetTheBaseline() {
val plan = CloudPlan.up(mapOf("slot0.sav" to Local("new")), mapOf("slot0.sav" to Remote("real", 1000)), null)
diff --git a/app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt
index 58fd405f17..a85ee7c183 100644
--- a/app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt
+++ b/app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt
@@ -65,6 +65,28 @@ class CloudSavesTest {
assertTrue(CloudSaves.conflicts(app, Store.GOG, game).isEmpty())
}
+ @Test fun aDirtyMarkStaysUntilAnUploadGoesThroughOrIsSkippedForGood() {
+ val gameId = "1207664643"
+ val folder = File(StoreInstallRoot.storeDir(StoreInstallRoot.internalRoot(app), Store.GOG), "ELDERBORN-mark").apply { mkdirs() }
+ File(folder, "ELDERBORN.exe").writeText("MZ")
+ StoreGameSidecar(Store.GOG, gameId, "ELDERBORN", "ELDERBORN.exe").write(folder)
+ // A save location the store gave, but no prefix yet: the upload cannot run, the mark stays.
+ com.droiddeck.launcher.stores.gog.GogPrefs.get(app).edit().putString("cloud_template_$gameId", "/ELDERBORN").apply()
+ CloudSaves.markDirty(app, Store.GOG, gameId)
+ assertTrue(CloudSaves.dirty(app).contains(Store.GOG to gameId))
+ val first = CloudSaves.uploadDirty(app, Store.GOG, gameId, "session-end")!!
+ assertEquals("no-prefix", first.reason)
+ assertTrue(CloudSaves.dirty(app).contains(Store.GOG to gameId))
+ // Recovery waits while a session runs.
+ CloudSaves.uploadAllDirty(app, "recovery", running = true)
+ assertTrue(CloudSaves.dirty(app).contains(Store.GOG to gameId))
+ // The store has no cloud saves for it: a final skip, the mark goes; a second trigger finds nothing.
+ com.droiddeck.launcher.stores.gog.GogPrefs.get(app).edit().putString("cloud_template_$gameId", "").apply()
+ assertEquals("no-cloud-saves", CloudSaves.uploadDirty(app, Store.GOG, gameId, "exit")!!.reason)
+ assertTrue(CloudSaves.dirty(app).none { it.second == gameId })
+ assertEquals(null, CloudSaves.uploadDirty(app, Store.GOG, gameId, "session-end"))
+ }
+
@Test fun aFirstLaunchWithoutAPrefixIsDeferredNotBlocked() {
val folder = File(StoreInstallRoot.storeDir(StoreInstallRoot.internalRoot(app), Store.GOG), "ELDERBORN").apply { mkdirs() }
File(folder, "ELDERBORN.exe").writeText("MZ")
diff --git a/app/src/test/java/com/droiddeck/launcher/stores/download/DownloadNotificationTextTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/download/DownloadNotificationTextTest.kt
new file mode 100644
index 0000000000..8bd9870ad4
--- /dev/null
+++ b/app/src/test/java/com/droiddeck/launcher/stores/download/DownloadNotificationTextTest.kt
@@ -0,0 +1,21 @@
+package com.droiddeck.launcher.stores.download
+
+import com.droiddeck.launcher.stores.Store
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+/** The notification's line: what the Downloads row says, on one line. */
+class DownloadNotificationTextTest {
+ private val base = DownloadEntry(Store.EPIC, "m", "Metalstorm", state = DownloadState.RUNNING)
+
+ @Test fun downloadingShowsPercentSizesSpeedAndTime() {
+ val d = base.copy(stage = DownloadStage.DOWNLOAD, bytesDone = 1_288_490_189, bytesTotal = 2_684_354_560, speedBps = 12_897_484, etaSeconds = 185)
+ assertEquals("Metalstorm · 48% · 1.2 GB/2.5 GB · 12.3 MB/s · 3 min", DownloadNotificationText.line(d, "Downloading", "Paused", "Queued"))
+ }
+
+ @Test fun installShowsTheStageNotTheSpeed() {
+ val d = base.copy(stage = DownloadStage.INSTALL, stageDone = 42, stageTotal = 100, speedBps = 50_000_000)
+ assertEquals("Metalstorm · Installing 42%", DownloadNotificationText.line(d, "Installing", "Paused", "Queued"))
+ assertEquals("Metalstorm · Paused", DownloadNotificationText.line(d.copy(state = DownloadState.PAUSED), "Installing", "Paused", "Queued"))
+ }
+}
diff --git a/docs/development/stores.md b/docs/development/stores.md
index dbd87addb8..9f06255c1e 100644
--- a/docs/development/stores.md
+++ b/docs/development/stores.md
@@ -154,6 +154,12 @@ the prefix's user folder appears, within a minute. Every path logs
to 15 s; for a store game with cloud saves it returns 10, and the compat tool then waits for
Proton instead of exec'ing it and runs `droiddeck-store-launch --exited` after, which asks for
`cloud-up` without waiting. Each sync logs `cloud down|up files= bytes= result=`.
+- **Uploads that do not depend on one hook:** the pre-launch step marks the game dirty in the app's
+ storage; the upload runs on the first of the compat tool's exit request (`trigger=exit`), the
+ session's end (`trigger=session-end`, after teardown, reading the prefix directly) or the app's
+ next start for a mark a killed app left (`trigger=recovery`, not while a session runs). The mark
+ goes only after an upload that went through or a final skip (off, no cloud saves, no baseline,
+ not installed, nothing local).
- **Per game:** the store card in the Games tab's launch settings has "Cloud saves" (on), kept in the
sidecar (`"cloud"`) and read by both sides; Manage saves shows the last sync with Upload and
Download, or "No cloud saves".
diff --git a/docs/progress-log.md b/docs/progress-log.md
index 67b7bb6426..aa242f5e53 100644
--- a/docs/progress-log.md
+++ b/docs/progress-log.md
@@ -150,6 +150,16 @@ without it and Setup says "Stores engine not built").
native start takes the whole set (`cdnBases`; the engine spreads its window across the hosts) and
its `onBytes` - every 250 ms, including files still in flight - drives the bar and the speed, so a
large file no longer moves the bar in one jump.
+- **Background downloads:** while a download runs the service holds a Wi-Fi lock and a partial wake
+ lock (timed, renewed with the progress, released when nothing runs). The notification shows a
+ real progress bar and " · 42% · 1.2 GB/2.5 GB · 12.3 MB/s · 3 min" (the stage while
+ installing), with Pause / Resume and a two-step Cancel (Delete / Keep, ten seconds); a paused
+ download leaves a notice to resume from; " installed" opens it in Games, " download
+ failed" opens Downloads. Notifications are asked for once on Android 13+. The Android 15 dataSync
+ time limit (and its `onTimeout`) applies only to apps targeting 35+: this app targets 28 and
+ compiles against 34, so there is nothing to handle yet.
+- **Cloud uploads do not depend on the exit hook:** a dirty mark per game launch; uploaded on the
+ first of exit request, session end, or the next app start (`trigger=exit|session-end|recovery`).
- **Cloud saves, device round 1 (ELDERBORN, `b323a0b`): nothing ran** - a GOG game without arguments
gets no launcher .bat, Steam runs its exe, and `droiddeck-store-launch` only recognised the .bat.
It now recognises the exe too (searching the folders above it for the sidecar), and every path