mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-01-01 12:46:12 +00:00
ipc/android: Add shutdown mechanism back
This commit is contained in:
parent
a3af3d82bd
commit
97100821d2
|
@ -24,7 +24,7 @@ interface IServiceNotification {
|
|||
* Create and return a notification (creating the channel if applicable) that can be used in
|
||||
* {@code Service#startForeground()}
|
||||
*/
|
||||
fun buildNotification(context: Context): Notification
|
||||
fun buildNotification(context: Context, pendingShutdownIntent: PendingIntent): Notification
|
||||
|
||||
/**
|
||||
* Return the notification ID to use
|
||||
|
|
|
@ -21,10 +21,13 @@ android {
|
|||
|
||||
// Single point of truth for these names.
|
||||
def serviceAction = "org.freedesktop.monado.ipc.CONNECT"
|
||||
def shutdownAction = "org.freedesktop.monado.ipc.SHUTDOWN"
|
||||
manifestPlaceholders = [
|
||||
serviceActionName : serviceAction,
|
||||
shutdownActionName: shutdownAction
|
||||
]
|
||||
buildConfigField("String", "SERVICE_ACTION", "\"${serviceAction}\"")
|
||||
buildConfigField("String", "SHUTDOWN_ACTION", "\"${shutdownAction}\"")
|
||||
buildConfigField("Long", "WATCHDOG_TIMEOUT_MILLISECONDS", "1500L")
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
<intent-filter>
|
||||
<action android:name="${serviceActionName}" />
|
||||
<action android:name="${shutdownActionName}" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
</application>
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
*/
|
||||
package org.freedesktop.monado.ipc
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.app.Service
|
||||
import android.content.Intent
|
||||
import android.content.pm.ServiceInfo
|
||||
|
@ -48,7 +49,9 @@ class MonadoService : Service(), Watchdog.ShutdownListener {
|
|||
watchdog.startMonitor()
|
||||
|
||||
// start the service so it could be foregrounded
|
||||
startService(Intent(this, javaClass))
|
||||
val intent = Intent(this, javaClass)
|
||||
intent.action = BuildConfig.SERVICE_ACTION
|
||||
startService(intent)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
@ -62,7 +65,13 @@ class MonadoService : Service(), Watchdog.ShutdownListener {
|
|||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
Log.d(TAG, "onStartCommand")
|
||||
handleStart()
|
||||
// if this isn't a restart
|
||||
if (intent != null) {
|
||||
when (intent.action) {
|
||||
BuildConfig.SERVICE_ACTION -> handleStart()
|
||||
BuildConfig.SHUTDOWN_ACTION -> handleShutdown()
|
||||
}
|
||||
}
|
||||
return START_STICKY
|
||||
}
|
||||
|
||||
|
@ -73,7 +82,14 @@ class MonadoService : Service(), Watchdog.ShutdownListener {
|
|||
}
|
||||
|
||||
private fun handleStart() {
|
||||
val notification = serviceNotification.buildNotification(this)
|
||||
val pendingShutdownIntent = PendingIntent.getForegroundService(
|
||||
this,
|
||||
0,
|
||||
Intent(BuildConfig.SHUTDOWN_ACTION).setPackage(packageName),
|
||||
0
|
||||
)
|
||||
|
||||
val notification = serviceNotification.buildNotification(this, pendingShutdownIntent)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
startForeground(
|
||||
|
@ -106,6 +122,10 @@ class MonadoService : Service(), Watchdog.ShutdownListener {
|
|||
|
||||
override fun onShutdown() {
|
||||
Log.d(TAG, "onShutdown")
|
||||
handleShutdown()
|
||||
}
|
||||
|
||||
private fun handleShutdown() {
|
||||
stopForeground(true)
|
||||
stopSelf()
|
||||
}
|
||||
|
|
|
@ -70,9 +70,14 @@ class ServiceNotificationImpl @Inject constructor() : IServiceNotification {
|
|||
* Create and return a notification (creating the channel if applicable) that can be used in
|
||||
* {@code Service#startForeground()}
|
||||
*/
|
||||
override fun buildNotification(context: Context): Notification {
|
||||
override fun buildNotification(context: Context, pendingShutdownIntent: PendingIntent): Notification {
|
||||
createChannel(context)
|
||||
|
||||
val action = Notification.Action.Builder(
|
||||
Icon.createWithResource(context, R.drawable.ic_feathericons_x),
|
||||
context.getString(R.string.notifExitRuntime),
|
||||
pendingShutdownIntent)
|
||||
.build()
|
||||
// Make a notification for our foreground service
|
||||
// When selected it will open the "About" activity
|
||||
val builder = makeNotificationBuilder(context)
|
||||
|
@ -82,6 +87,7 @@ class ServiceNotificationImpl @Inject constructor() : IServiceNotification {
|
|||
R.string.notif_text,
|
||||
nameAndLogoProvider.getLocalizedRuntimeName()))
|
||||
.setShowWhen(false)
|
||||
.setActions(action)
|
||||
.setContentIntent(uiProvider.makeAboutActivityPendingIntent())
|
||||
|
||||
// Notification icon is optional
|
||||
|
|
Loading…
Reference in a new issue