mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-01-04 06:06:17 +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
|
* Create and return a notification (creating the channel if applicable) that can be used in
|
||||||
* {@code Service#startForeground()}
|
* {@code Service#startForeground()}
|
||||||
*/
|
*/
|
||||||
fun buildNotification(context: Context): Notification
|
fun buildNotification(context: Context, pendingShutdownIntent: PendingIntent): Notification
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the notification ID to use
|
* Return the notification ID to use
|
||||||
|
|
|
@ -21,10 +21,13 @@ android {
|
||||||
|
|
||||||
// Single point of truth for these names.
|
// Single point of truth for these names.
|
||||||
def serviceAction = "org.freedesktop.monado.ipc.CONNECT"
|
def serviceAction = "org.freedesktop.monado.ipc.CONNECT"
|
||||||
|
def shutdownAction = "org.freedesktop.monado.ipc.SHUTDOWN"
|
||||||
manifestPlaceholders = [
|
manifestPlaceholders = [
|
||||||
serviceActionName : serviceAction,
|
serviceActionName : serviceAction,
|
||||||
|
shutdownActionName: shutdownAction
|
||||||
]
|
]
|
||||||
buildConfigField("String", "SERVICE_ACTION", "\"${serviceAction}\"")
|
buildConfigField("String", "SERVICE_ACTION", "\"${serviceAction}\"")
|
||||||
|
buildConfigField("String", "SHUTDOWN_ACTION", "\"${shutdownAction}\"")
|
||||||
buildConfigField("Long", "WATCHDOG_TIMEOUT_MILLISECONDS", "1500L")
|
buildConfigField("Long", "WATCHDOG_TIMEOUT_MILLISECONDS", "1500L")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
|
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="${serviceActionName}" />
|
<action android:name="${serviceActionName}" />
|
||||||
|
<action android:name="${shutdownActionName}" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</service>
|
</service>
|
||||||
</application>
|
</application>
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
*/
|
*/
|
||||||
package org.freedesktop.monado.ipc
|
package org.freedesktop.monado.ipc
|
||||||
|
|
||||||
|
import android.app.PendingIntent
|
||||||
import android.app.Service
|
import android.app.Service
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.ServiceInfo
|
import android.content.pm.ServiceInfo
|
||||||
|
@ -48,7 +49,9 @@ class MonadoService : Service(), Watchdog.ShutdownListener {
|
||||||
watchdog.startMonitor()
|
watchdog.startMonitor()
|
||||||
|
|
||||||
// start the service so it could be foregrounded
|
// 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() {
|
override fun onDestroy() {
|
||||||
|
@ -62,7 +65,13 @@ class MonadoService : Service(), Watchdog.ShutdownListener {
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
Log.d(TAG, "onStartCommand")
|
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
|
return START_STICKY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +82,14 @@ class MonadoService : Service(), Watchdog.ShutdownListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleStart() {
|
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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
startForeground(
|
startForeground(
|
||||||
|
@ -106,6 +122,10 @@ class MonadoService : Service(), Watchdog.ShutdownListener {
|
||||||
|
|
||||||
override fun onShutdown() {
|
override fun onShutdown() {
|
||||||
Log.d(TAG, "onShutdown")
|
Log.d(TAG, "onShutdown")
|
||||||
|
handleShutdown()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleShutdown() {
|
||||||
stopForeground(true)
|
stopForeground(true)
|
||||||
stopSelf()
|
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
|
* Create and return a notification (creating the channel if applicable) that can be used in
|
||||||
* {@code Service#startForeground()}
|
* {@code Service#startForeground()}
|
||||||
*/
|
*/
|
||||||
override fun buildNotification(context: Context): Notification {
|
override fun buildNotification(context: Context, pendingShutdownIntent: PendingIntent): Notification {
|
||||||
createChannel(context)
|
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
|
// Make a notification for our foreground service
|
||||||
// When selected it will open the "About" activity
|
// When selected it will open the "About" activity
|
||||||
val builder = makeNotificationBuilder(context)
|
val builder = makeNotificationBuilder(context)
|
||||||
|
@ -82,6 +87,7 @@ class ServiceNotificationImpl @Inject constructor() : IServiceNotification {
|
||||||
R.string.notif_text,
|
R.string.notif_text,
|
||||||
nameAndLogoProvider.getLocalizedRuntimeName()))
|
nameAndLogoProvider.getLocalizedRuntimeName()))
|
||||||
.setShowWhen(false)
|
.setShowWhen(false)
|
||||||
|
.setActions(action)
|
||||||
.setContentIntent(uiProvider.makeAboutActivityPendingIntent())
|
.setContentIntent(uiProvider.makeAboutActivityPendingIntent())
|
||||||
|
|
||||||
// Notification icon is optional
|
// Notification icon is optional
|
||||||
|
|
Loading…
Reference in a new issue