Added persistent notification while app is running (fixes #12).

This commit is contained in:
Felix Ableitner 2015-07-21 16:03:27 +02:00
parent bee5e45093
commit 9bb5870344
3 changed files with 36 additions and 8 deletions

View file

@ -19,8 +19,7 @@
<activity
android:name=".activities.FirstStartActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize"
android:launchMode="singleTop" >
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
@ -29,7 +28,8 @@
<activity
android:name=".activities.MainActivity"
android:label="@string/app_name" />
android:label="@string/app_name"
android:launchMode="singleTop" />
<activity
android:name=".activities.AddContactsActivity"

View file

@ -2,12 +2,14 @@ package com.nutomic.ensichat.protocol
import java.util.Date
import android.app.Service
import android.content.Intent
import android.app.Notification.Builder
import android.app.{Notification, NotificationManager, PendingIntent, Service}
import android.content.{Context, Intent}
import android.os.Handler
import android.preference.PreferenceManager
import android.util.Log
import com.nutomic.ensichat.R
import com.nutomic.ensichat.activities.MainActivity
import com.nutomic.ensichat.bluetooth.BluetoothInterface
import com.nutomic.ensichat.fragments.SettingsFragment
import com.nutomic.ensichat.protocol.ChatService.{OnConnectionsChangedListener, OnMessageReceivedListener}
@ -21,6 +23,8 @@ import scala.concurrent.Future
object ChatService {
val ActionStopService = "stop_service"
abstract class InterfaceHandler {
def create(): Unit
@ -82,6 +86,9 @@ class ChatService extends Service {
private var messageListeners = new mutable.WeakHashMap[OnMessageReceivedListener, Unit].keySet
private lazy val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE).asInstanceOf[NotificationManager]
/**
* Holds all known users.
*
@ -95,6 +102,8 @@ class ChatService extends Service {
override def onCreate(): Unit = {
super.onCreate()
showPersistentNotification()
Future {
crypto.generateLocalKeys()
registerMessageListener(database)
@ -106,7 +115,20 @@ class ChatService extends Service {
}
}
def showPersistentNotification(): Unit = {
val openIntent = PendingIntent.getActivity(this, 0, new Intent(this, classOf[MainActivity]), 0)
val notification = new Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentIntent(openIntent)
.setOngoing(true)
.setPriority(Notification.PRIORITY_MIN)
.build()
notificationManager.notify(NotificationHandler.NotificationIdRunning, notification)
}
override def onDestroy(): Unit = {
notificationManager.cancel(NotificationHandler.NotificationIdRunning)
btInterface.destroy()
}

View file

@ -10,13 +10,19 @@ import com.nutomic.ensichat.protocol.ChatService.OnMessageReceivedListener
import com.nutomic.ensichat.protocol.body.Text
import com.nutomic.ensichat.protocol.{Crypto, Message}
object NotificationHandler {
val NotificationIdRunning = 1
val NotificationIdNewMessage = 2
}
/**
* 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)
@ -34,7 +40,7 @@ class NotificationHandler(context: Context) extends OnMessageReceivedListener {
val nm = context.getSystemService(Context.NOTIFICATION_SERVICE)
.asInstanceOf[NotificationManager]
nm.notify(notificationIdNewMessage, notification)
nm.notify(NotificationHandler.NotificationIdNewMessage, notification)
case _ =>
}