feat: Stay connected, an opt-in service that holds the relays open (notifications, Phase 10)
A specialUse foreground service keeps AppLifecycle.holdConnections true and activates the default profile when no screen has, so the app's own live subscriptions deliver within seconds while closed. Off by default, a switch on the notification settings, started only from the foreground. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c9faf62f09
commit
a60f6b937e
5 files changed
+180
-1
No files matched your search
@@ -8,6 +8,12 @@
|
||||
first launch. docs/notifications-and-background-sync.md, Phase 6. -->
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
|
||||
|
||||
<!-- Stay connected: an opt-in service that holds the relay connections while the app is
|
||||
closed. specialUse, because dataSync is capped at six hours a day from Android 15 and
|
||||
nothing else fits. docs/notifications-and-background-sync.md, Phase 0 check 6 and Phase 10. -->
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
|
||||
|
||||
<!-- No backups and no device-to-device transfer: aux.db holds FROST shares in
|
||||
plaintext, and a copied share cannot be rotated away from. See
|
||||
res/xml/data_extraction_rules.xml and docs/frost-share-refresh.md, Phase 5. -->
|
||||
@@ -30,6 +36,14 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<service
|
||||
android:name="mobi.giza.android.background.StayConnectedService"
|
||||
android:exported="false"
|
||||
android:foregroundServiceType="specialUse">
|
||||
<property
|
||||
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
|
||||
android:value="Holds the connections to the user's Nostr relays open so that end-to-end encrypted messages arrive while the app is closed, at the user's request." />
|
||||
</service>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -6,6 +6,8 @@ import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import mobi.giza.android.background.StayConnectedService
|
||||
import mobi.giza.compose.ExperimentalFeatures
|
||||
import mobi.giza.compose.GizaApp
|
||||
import mobi.giza.compose.notifications.NotificationTaps
|
||||
|
||||
@@ -21,6 +23,12 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
tapped(intent)
|
||||
|
||||
// From here, in front, because Android will not start a foreground service from the
|
||||
// background: a reboot or a killed process brings it back on the next launch.
|
||||
if (ExperimentalFeatures.backgroundSync && StayConnectedService.isOn(this)) {
|
||||
StayConnectedService.start(this)
|
||||
}
|
||||
|
||||
setContent {
|
||||
val navController = rememberNavController()
|
||||
|
||||
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
package mobi.giza.android.background
|
||||
|
||||
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 androidx.core.app.ServiceCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import mobi.giza.android.GizaApplication
|
||||
import mobi.giza.android.MainActivity
|
||||
import mobi.giza.android.R
|
||||
import mobi.giza.compose.AppLifecycle
|
||||
import mobi.giza.compose.identity.DefaultIdentity
|
||||
import mobi.giza.compose.identity.Identity
|
||||
import mobi.giza.compose.resources.Res
|
||||
import mobi.giza.compose.resources.stay_connected_channel
|
||||
import mobi.giza.compose.resources.stay_connected_notification
|
||||
import org.jetbrains.compose.resources.getString
|
||||
|
||||
/**
|
||||
* Stay connected: the live subscriptions, held open while the app is closed, for as long as the
|
||||
* user leaves this on. docs/notifications-and-background-sync.md, D10 and Phase 10.
|
||||
*
|
||||
* It starts nothing of its own. It keeps `AppLifecycle.holdConnections` true, and if no screen has
|
||||
* set an identity it activates the default profile in the runtime, as the background pass does --
|
||||
* after which the app's own engines are what run.
|
||||
*/
|
||||
class StayConnectedService : Service() {
|
||||
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||
|
||||
@Volatile
|
||||
private var activatedHere: Identity? = null
|
||||
|
||||
override fun onBind(intent: Intent?): IBinder? = null
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
ServiceCompat.startForeground(
|
||||
this,
|
||||
NOTIFICATION_ID,
|
||||
notification(),
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE else 0,
|
||||
)
|
||||
AppLifecycle.keepConnected(AppLifecycle.KEEP_STAY_CONNECTED)
|
||||
|
||||
val runtime = (application as GizaApplication).runtime
|
||||
scope.launch {
|
||||
runtime.synchronization
|
||||
runtime.notary
|
||||
runtime.notifications
|
||||
if (runtime.activeIdentity.value == null) {
|
||||
DefaultIdentity.load(runtime.phoenixGlobal)?.let { identity ->
|
||||
activatedHere = identity
|
||||
runtime.activeIdentity.compareAndSet(null, identity)
|
||||
}
|
||||
}
|
||||
}
|
||||
return START_STICKY
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
AppLifecycle.release(AppLifecycle.KEEP_STAY_CONNECTED)
|
||||
val runtime = (application as GizaApplication).runtime
|
||||
activatedHere?.let { identity ->
|
||||
if (!AppLifecycle.isVisible.value) runtime.activeIdentity.compareAndSet(identity, null)
|
||||
}
|
||||
scope.cancel()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private fun notification(): android.app.Notification {
|
||||
// Read once, here, before startForeground's deadline; a resource read is not slow.
|
||||
val (channelName, text) = runBlocking {
|
||||
getString(Res.string.stay_connected_channel) to getString(Res.string.stay_connected_notification)
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
getSystemService(NotificationManager::class.java)?.createNotificationChannel(
|
||||
NotificationChannel(CHANNEL, channelName, NotificationManager.IMPORTANCE_MIN)
|
||||
)
|
||||
}
|
||||
val open = PendingIntent.getActivity(
|
||||
this, 0, Intent(this, MainActivity::class.java),
|
||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
||||
)
|
||||
return NotificationCompat.Builder(this, CHANNEL)
|
||||
.setSmallIcon(R.drawable.ic_launcher_monochrome)
|
||||
.setContentTitle(getString(R.string.app_name))
|
||||
.setContentText(text)
|
||||
.setPriority(NotificationCompat.PRIORITY_MIN)
|
||||
.setOngoing(true)
|
||||
.setContentIntent(open)
|
||||
.build()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val CHANNEL = "connection"
|
||||
private const val NOTIFICATION_ID = 2
|
||||
private const val PREFERENCES = "giza_background"
|
||||
private const val KEY_STAY_CONNECTED = "stay_connected"
|
||||
|
||||
/** Whether the user turned it on. A device's choice, not a profile's. */
|
||||
fun isOn(context: Context): Boolean =
|
||||
context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE).getBoolean(KEY_STAY_CONNECTED, false)
|
||||
|
||||
fun set(context: Context, on: Boolean) {
|
||||
context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE).edit().putBoolean(KEY_STAY_CONNECTED, on).apply()
|
||||
if (on) start(context) else stop(context)
|
||||
}
|
||||
|
||||
/** From the foreground only: Android refuses a foreground service started from the background. */
|
||||
fun start(context: Context) {
|
||||
ContextCompat.startForegroundService(context, Intent(context, StayConnectedService::class.java))
|
||||
}
|
||||
|
||||
fun stop(context: Context) {
|
||||
context.stopService(Intent(context, StayConnectedService::class.java))
|
||||
}
|
||||
}
|
||||
}
|
||||
+23
-1
@@ -3,8 +3,17 @@ package mobi.giza.compose.notifications
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import mobi.giza.android.background.StayConnectedService
|
||||
import mobi.giza.compose.resources.Res
|
||||
import mobi.giza.compose.resources.background_android_explanation
|
||||
import mobi.giza.compose.resources.stay_connected
|
||||
import mobi.giza.compose.resources.stay_connected_supporting
|
||||
import mobi.giza.compose.ui.composable.SwitchRow
|
||||
import org.jetbrains.compose.resources.stringResource
|
||||
|
||||
@Composable
|
||||
@@ -12,5 +21,18 @@ actual fun PlatformBackgroundExplanation() {
|
||||
Text(stringResource(Res.string.background_android_explanation), style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
|
||||
/** Stay connected, which only Android offers: the others either cannot or need not. Phase 10. */
|
||||
@Composable
|
||||
actual fun PlatformBackgroundSettings() = Unit
|
||||
actual fun PlatformBackgroundSettings() {
|
||||
val context = LocalContext.current
|
||||
var on by remember { mutableStateOf(StayConnectedService.isOn(context)) }
|
||||
SwitchRow(
|
||||
headline = stringResource(Res.string.stay_connected),
|
||||
supporting = stringResource(Res.string.stay_connected_supporting),
|
||||
checked = on,
|
||||
enabled = true,
|
||||
) { checked ->
|
||||
on = checked
|
||||
StayConnectedService.set(context.applicationContext, checked)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user