Added basic, dumb notifications.

This commit is contained in:
Felix Ableitner 2015-03-02 22:53:40 +01:00
parent 61f123c309
commit c4a45e2c49
3 changed files with 47 additions and 4 deletions

View file

@ -83,4 +83,7 @@
<!-- Notification text for incoming friend request -->
<string name="notification_friend_request">New friend request!</string>
<!-- Notification text for incoming message -->
<string name="notification_message">New message!</string>
</resources>

View file

@ -7,12 +7,12 @@ import android.os.Handler
import android.preference.PreferenceManager
import android.util.Log
import com.nutomic.ensichat.R
import com.nutomic.ensichat.activities.ConfirmAddContactDialog
import com.nutomic.ensichat.activities.{MainActivity, ConfirmAddContactDialog}
import com.nutomic.ensichat.bluetooth.BluetoothInterface
import com.nutomic.ensichat.fragments.SettingsFragment
import com.nutomic.ensichat.protocol.ChatService.{OnConnectionsChangedListener, OnMessageReceivedListener}
import com.nutomic.ensichat.protocol.messages._
import com.nutomic.ensichat.util.Database
import com.nutomic.ensichat.util.{NotificationHandler, Database}
import scala.collection.mutable
import scala.concurrent.ExecutionContext.Implicits.global
@ -62,11 +62,13 @@ class ChatService extends Service {
private lazy val btInterface = new BluetoothInterface(this, mainHandler,
onMessageReceived, callConnectionListeners, onConnectionOpened)
private lazy val notificationHandler = new NotificationHandler(this)
private lazy val router = new Router(connections, sendVia)
private lazy val seqNumGenerator = new SeqNumGenerator(this)
private val notificationIdGenerator = Stream.from(100).iterator
private val notificationIdAddContactGenerator = Stream.from(100).iterator
/**
* For this (and [[messageListeners]], functions would be useful instead of instances,
@ -96,6 +98,7 @@ class ChatService extends Service {
BluetoothAdapter.getDefaultAdapter.getName).apply()
registerMessageListener(database)
registerMessageListener(notificationHandler)
Future {
crypto.generateLocalKeys()
@ -192,7 +195,7 @@ class ChatService extends Service {
.setAutoCancel(true)
.build()
val nm = getSystemService(Context.NOTIFICATION_SERVICE).asInstanceOf[NotificationManager]
nm.notify(notificationIdGenerator.next(), notification)
nm.notify(notificationIdAddContactGenerator.next(), notification)
case _ =>
mainHandler.post(new Runnable {
override def run(): Unit =

View file

@ -0,0 +1,37 @@
package com.nutomic.ensichat.util
import android.app.{NotificationManager, Notification, PendingIntent}
import android.content.{Context, Intent}
import com.nutomic.ensichat.R
import com.nutomic.ensichat.activities.MainActivity
import com.nutomic.ensichat.protocol.ChatService.OnMessageReceivedListener
import com.nutomic.ensichat.protocol.Crypto
import com.nutomic.ensichat.protocol.messages.{Text, Message}
/**
* Displays notifications for new messages.
*/
class NotificationHandler(context: Context) extends OnMessageReceivedListener {
private val notificationIdNewMessage = 1
def onMessageReceived(msg: Message): Unit = msg.Body match {
case text: Text =>
if (msg.Header.origin == new Crypto(context).localAddress)
return
val pi = PendingIntent.getActivity(context, 0, new Intent(context, classOf[MainActivity]), 0)
val notification = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(context.getString(R.string.notification_message))
.setContentText(text.text)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pi)
.setAutoCancel(true)
.build()
val nm = context.getSystemService(Context.NOTIFICATION_SERVICE)
.asInstanceOf[NotificationManager]
nm.notify(notificationIdNewMessage, notification)
}
}