ipc/android: Add shutdown mechanism back

This commit is contained in:
Jarvis Huang 2021-12-09 18:28:54 +08:00 committed by Ryan Pavlik
parent a3af3d82bd
commit 97100821d2
5 changed files with 35 additions and 5 deletions

View file

@ -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

View file

@ -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")
}

View file

@ -15,6 +15,7 @@
<intent-filter>
<action android:name="${serviceActionName}" />
<action android:name="${shutdownActionName}" />
</intent-filter>
</service>
</application>

View file

@ -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()
}

View file

@ -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