Show number of connected devices in notification.
This commit is contained in:
parent
ec6aeeb78f
commit
9b19158adf
4 changed files with 32 additions and 7 deletions
|
@ -116,4 +116,10 @@
|
|||
<!-- Notification text for incoming message -->
|
||||
<string name="notification_message">New message!</string>
|
||||
|
||||
<!-- Info text for persistent notification -->
|
||||
<plurals name="notification_connections">
|
||||
<item quantity="one">Connected to %1$s device</item>
|
||||
<item quantity="other">Connected to %1$s devices</item>
|
||||
</plurals>
|
||||
|
||||
</resources>
|
||||
|
|
|
@ -18,22 +18,24 @@ object CallbackHandler {
|
|||
/**
|
||||
* Receives events from [[ConnectionHandler]] and sends them as local broadcasts.
|
||||
*/
|
||||
class CallbackHandler(context: Context, notificationHandler: NotificationHandler)
|
||||
class CallbackHandler(chatService: ChatService, notificationHandler: NotificationHandler)
|
||||
extends CallbackInterface {
|
||||
|
||||
def onMessageReceived(msg: Message): Unit = {
|
||||
notificationHandler.onMessageReceived(msg)
|
||||
val i = new Intent(ActionMessageReceived)
|
||||
i.putExtra(ExtraMessage, msg)
|
||||
LocalBroadcastManager.getInstance(context)
|
||||
LocalBroadcastManager.getInstance(chatService)
|
||||
.sendBroadcast(i)
|
||||
|
||||
}
|
||||
|
||||
def onConnectionsChanged(): Unit = {
|
||||
val i = new Intent(ActionConnectionsChanged)
|
||||
LocalBroadcastManager.getInstance(context)
|
||||
LocalBroadcastManager.getInstance(chatService)
|
||||
.sendBroadcast(i)
|
||||
notificationHandler
|
||||
.updatePersistentNotification(chatService.getConnectionHandler.connections().size)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ class ChatService extends Service {
|
|||
*/
|
||||
override def onCreate(): Unit = {
|
||||
super.onCreate()
|
||||
notificationHandler.showPersistentNotification()
|
||||
notificationHandler.updatePersistentNotification(getConnectionHandler.connections().size)
|
||||
if (Option(BluetoothAdapter.getDefaultAdapter).isDefined) {
|
||||
connectionHandler.addTransmissionInterface(new BluetoothInterface(this, new Handler(),
|
||||
connectionHandler))
|
||||
|
@ -62,7 +62,7 @@ class ChatService extends Service {
|
|||
}
|
||||
|
||||
override def onDestroy(): Unit = {
|
||||
notificationHandler.cancelPersistentNotification()
|
||||
notificationHandler.stopPersistentNotification()
|
||||
connectionHandler.stop()
|
||||
unregisterReceiver(networkReceiver)
|
||||
}
|
||||
|
|
|
@ -27,11 +27,20 @@ class NotificationHandler(context: Context) {
|
|||
private lazy val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE)
|
||||
.asInstanceOf[NotificationManager]
|
||||
|
||||
def showPersistentNotification(): Unit = {
|
||||
private var persistentNotificationShutdown = false
|
||||
|
||||
def updatePersistentNotification(connections: Int): Unit = {
|
||||
if (persistentNotificationShutdown)
|
||||
return
|
||||
|
||||
val intent = PendingIntent.getActivity(context, 0, new Intent(context, classOf[MainActivity]), 0)
|
||||
val info = context.getResources
|
||||
.getQuantityString(R.plurals.notification_connections, connections, connections.toString)
|
||||
|
||||
val notification = new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.drawable.ic_launcher)
|
||||
.setContentTitle(context.getString(R.string.app_name))
|
||||
.setContentText(info)
|
||||
.setContentIntent(intent)
|
||||
.setOngoing(true)
|
||||
.setPriority(Notification.PRIORITY_MIN)
|
||||
|
@ -39,7 +48,15 @@ class NotificationHandler(context: Context) {
|
|||
notificationManager.notify(NotificationIdRunning, notification)
|
||||
}
|
||||
|
||||
def cancelPersistentNotification() = notificationManager.cancel(NotificationIdRunning)
|
||||
/**
|
||||
* Cancels the persistent notification.
|
||||
*
|
||||
* After calling this method, [[updatePersistentNotification()]] will have no effect.
|
||||
*/
|
||||
def stopPersistentNotification() = {
|
||||
persistentNotificationShutdown = true
|
||||
notificationManager.cancel(NotificationIdRunning)
|
||||
}
|
||||
|
||||
def onMessageReceived(msg: Message): Unit = msg.body match {
|
||||
case text: Text =>
|
||||
|
|
Reference in a new issue