t/android_common: Implement IServiceNotification

This commit is contained in:
Ryan Pavlik 2020-11-13 14:50:28 -06:00 committed by Lubosz Sarnecki
parent 94d747cac9
commit fe6507d64e
4 changed files with 180 additions and 0 deletions

View file

@ -0,0 +1,115 @@
// Copyright 2020, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Implementation of service notification.
* @author Ryan Pavlik <ryan.pavlik@collabora.com>
*/
package org.freedesktop.monado.android_common
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.graphics.drawable.Icon
import android.os.Build
import androidx.core.app.NotificationManagerCompat
import org.freedesktop.monado.auxiliary.IServiceNotification
import org.freedesktop.monado.auxiliary.NameAndLogoProvider
import org.freedesktop.monado.auxiliary.UiProvider
import javax.inject.Inject
class ServiceNotificationImpl @Inject constructor() : IServiceNotification {
companion object {
private const val CHANNEL_ID = "org.freedesktop.monado.ipc.CHANNEL"
// guaranteed random by fair die roll ;)
private const val NOTIFICATION_ID = 8562
}
@Inject
lateinit var uiProvider: UiProvider
@Inject
lateinit var nameAndLogoProvider: NameAndLogoProvider
/**
* Start creating a Notification.Builder in a version-compatible way, including our
* notification channel if applicable.
*/
private fun makeNotificationBuilder(context: Context): Notification.Builder {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder(context, CHANNEL_ID)
} else {
@Suppress("DEPRECATION")
Notification.Builder(context)
}
}
private fun createChannel(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(CHANNEL_ID,
nameAndLogoProvider.getLocalizedRuntimeName(),
NotificationManager.IMPORTANCE_LOW)
notificationChannel.description = context.getString(
R.string.channel_desc,
nameAndLogoProvider.getLocalizedRuntimeName())
notificationChannel.setShowBadge(false)
notificationChannel.enableLights(false)
notificationChannel.enableVibration(false)
NotificationManagerCompat.from(context)
.createNotificationChannel(notificationChannel)
}
}
/**
* Create and return a notification (creating the channel if applicable) that can be used in
* {@code Service#startForeground()}
*/
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)
.setOngoing(true)
.setContentTitle(nameAndLogoProvider.getLocalizedRuntimeName())
.setContentText(context.getString(
R.string.notif_text,
nameAndLogoProvider.getLocalizedRuntimeName()))
.setShowWhen(false)
.addAction(action)
.setContentIntent(uiProvider.makeAboutActivityPendingIntent())
// Notification icon is optional
uiProvider.getNotificationIcon()?.let { icon: Icon ->
builder.setSmallIcon(icon)
}
// Configure intent is optional
uiProvider.makeConfigureActivityPendingIntent()?.let { pendingIntent: PendingIntent ->
val configureAction = Notification.Action.Builder(
Icon.createWithResource(context, R.drawable.ic_feathericons_settings),
context.getString(R.string.notifConfigure),
pendingIntent)
.build()
builder.addAction(configureAction)
}
return builder.build()
}
/**
* Return the notification ID to use
*/
override fun getNotificationId(): Int = NOTIFICATION_ID
}

View file

@ -0,0 +1,25 @@
<!--
Copyright (c) 2013-2017 Cole Bemis
SPDX-License-Identifier: MIT
From https://feathericons.com
Modified by adding this header and changing the stroke color to an actual color,
to avoid confusing Android's tools.
-->
<!--suppress ALL -->
<svg class="feather feather-settings"
fill="none"
height="24"
stroke="#000000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
viewBox="0 0 24 24"
width="24"
xmlns="http://www.w3.org/2000/svg">
<circle
cx="12"
cy="12"
r="3"></circle>
<path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"></path>
</svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

View file

@ -0,0 +1,30 @@
<!--
Copyright (c) 2013-2017 Cole Bemis
SPDX-License-Identifier: MIT
From https://feathericons.com
Modified by adding this header and changing the stroke color to an actual color,
to avoid confusing Android's tools.
-->
<!--suppress ALL -->
<svg class="feather feather-x"
fill="none"
height="24"
stroke="#000000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
viewBox="0 0 24 24"
width="24"
xmlns="http://www.w3.org/2000/svg">
<line
x1="18"
x2="6"
y1="6"
y2="18"></line>
<line
x1="6"
x2="18"
y1="6"
y2="18"></line>
</svg>

After

Width:  |  Height:  |  Size: 648 B

View file

@ -0,0 +1,10 @@
<resources>
<!--
Copyright 2020, Collabora, Ltd.
SPDX-License-Identifier: BSL-1.0
-->
<string name="channel_desc">%1$s Runtime</string>
<string name="notif_text">An application is currently running that is using the %1$s service.</string>
<string name="notifExitRuntime">Exit Runtime</string>
<string name="notifConfigure">Settings</string>
</resources>