Database should implement OnMessageReceivedListener.
This commit is contained in:
parent
b1f59eaaff
commit
64e8b06543
5 changed files with 44 additions and 39 deletions
|
@ -27,9 +27,9 @@ class DatabaseTest extends AndroidTestCase {
|
|||
|
||||
override def setUp(): Unit = {
|
||||
Database = new Database(new TestContext(getContext))
|
||||
Database.addMessage(m1)
|
||||
Database.addMessage(m2)
|
||||
Database.addMessage(m3)
|
||||
Database.onMessageReceived(m1)
|
||||
Database.onMessageReceived(m2)
|
||||
Database.onMessageReceived(m3)
|
||||
}
|
||||
|
||||
override def tearDown(): Unit = {
|
||||
|
|
|
@ -121,15 +121,17 @@ class AddContactsActivity extends EnsiChatActivity with ChatService.OnConnection
|
|||
* These are only handled here and require user action, so contacts can only be added if
|
||||
* the user is in this activity.
|
||||
*/
|
||||
override def onMessageReceived(messages: SortedSet[Message]): Unit = {
|
||||
messages.filter(_.Header.Target == Crypto.getLocalAddress)
|
||||
.foreach{
|
||||
case m if m.Body.isInstanceOf[RequestAddContact] =>
|
||||
Log.i(Tag, "Remote device " + m.Header.Origin + " wants to add us as a contact, showing dialog")
|
||||
service.getConnections.find(_.Address == m.Header.Origin).foreach(addDeviceDialog)
|
||||
case m if m.Body.isInstanceOf[ResultAddContact] =>
|
||||
currentlyAdding.keys.find(_.Address == m.Header.Origin)foreach(contact =>
|
||||
if (m.Body.asInstanceOf[ResultAddContact].Accepted) {
|
||||
override def onMessageReceived(msg: Message): Unit = {
|
||||
if (msg.Header.Target != Crypto.getLocalAddress)
|
||||
return
|
||||
|
||||
msg.Body match {
|
||||
case _: RequestAddContact =>
|
||||
Log.i(Tag, "Remote device " + msg.Header.Origin + " wants to add us as a contact, showing dialog")
|
||||
service.getConnections.find(_.Address == msg.Header.Origin).foreach(addDeviceDialog)
|
||||
case _: ResultAddContact =>
|
||||
currentlyAdding.keys.find(_.Address == msg.Header.Origin).foreach(contact =>
|
||||
if (msg.Body.asInstanceOf[ResultAddContact].Accepted) {
|
||||
Log.i(Tag, contact.toString + " accepted us as a contact, updating state")
|
||||
currentlyAdding += (contact ->
|
||||
new AddContactInfo(true, currentlyAdding(contact).remoteConfirmed))
|
||||
|
|
|
@ -55,7 +55,7 @@ class ChatFragment extends ListFragment with OnClickListener
|
|||
// Read local device ID from service,
|
||||
adapter = new MessagesAdapter(getActivity, address)
|
||||
chatService.registerMessageListener(ChatFragment.this)
|
||||
onMessageReceived(chatService.Database.getMessages(address, 15))
|
||||
chatService.Database.getMessages(address, 15).foreach(adapter.add)
|
||||
|
||||
if (listView != null) {
|
||||
listView.setAdapter(adapter)
|
||||
|
@ -112,9 +112,9 @@ class ChatFragment extends ListFragment with OnClickListener
|
|||
/**
|
||||
* Displays new messages in UI.
|
||||
*/
|
||||
override def onMessageReceived(messages: SortedSet[Message]): Unit = {
|
||||
messages.filter(m => Set(m.Header.Origin, m.Header.Target).contains(address))
|
||||
.foreach(adapter.add)
|
||||
override def onMessageReceived(msg: Message): Unit = {
|
||||
if (Set(msg.Header.Origin, msg.Header.Target).contains(address))
|
||||
adapter.add(msg)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ object ChatService {
|
|||
}
|
||||
|
||||
trait OnMessageReceivedListener {
|
||||
def onMessageReceived(messages: SortedSet[Message]): Unit
|
||||
def onMessageReceived(messages: Message): Unit
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -88,6 +88,8 @@ class ChatService extends Service {
|
|||
pm.edit().putString(SettingsFragment.KeyUserName,
|
||||
BluetoothAdapter.getDefaultAdapter.getName).apply()
|
||||
|
||||
registerMessageListener(Database)
|
||||
|
||||
Future {
|
||||
Crypto.generateLocalKeys()
|
||||
|
||||
|
@ -159,12 +161,11 @@ class ChatService extends Service {
|
|||
|
||||
callConnectionListeners()
|
||||
case _ =>
|
||||
Database.addMessage(msg)
|
||||
MainHandler.post(new Runnable {
|
||||
override def run(): Unit =
|
||||
messageListeners
|
||||
.filter(_.get.nonEmpty)
|
||||
.foreach(_.apply().onMessageReceived(SortedSet(msg)(Message.Ordering)))
|
||||
.foreach(_.apply().onMessageReceived(msg))
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.util.Date
|
|||
import android.content.{ContentValues, Context}
|
||||
import android.database.sqlite.{SQLiteDatabase, SQLiteOpenHelper}
|
||||
import android.util.Log
|
||||
import com.nutomic.ensichat.protocol.ChatService.OnMessageReceivedListener
|
||||
import com.nutomic.ensichat.protocol._
|
||||
import com.nutomic.ensichat.protocol.messages._
|
||||
|
||||
|
@ -34,8 +35,9 @@ object Database {
|
|||
/**
|
||||
* Stores all messages and contacts in SQL database.
|
||||
*/
|
||||
class Database(context: Context) extends SQLiteOpenHelper(context, Database.DatabaseName,
|
||||
null, Database.DatabaseVersion) {
|
||||
class Database(context: Context)
|
||||
extends SQLiteOpenHelper(context, Database.DatabaseName, null, Database.DatabaseVersion)
|
||||
with OnMessageReceivedListener {
|
||||
|
||||
private var contactsUpdatedListeners = Set[() => Unit]()
|
||||
|
||||
|
@ -72,16 +74,16 @@ class Database(context: Context) extends SQLiteOpenHelper(context, Database.Data
|
|||
/**
|
||||
* Inserts the given new message into the database.
|
||||
*/
|
||||
def addMessage(message: Message): Unit = message.Body match {
|
||||
override def onMessageReceived(msg: Message): Unit = msg.Body match {
|
||||
case text: Text =>
|
||||
val cv = new ContentValues()
|
||||
cv.put("origin", message.Header.Origin.toString)
|
||||
cv.put("target", message.Header.Target.toString)
|
||||
cv.put("origin", msg.Header.Origin.toString)
|
||||
cv.put("target", msg.Header.Target.toString)
|
||||
// toString used as workaround for compile error with Long.
|
||||
cv.put("date", text.time.getTime.toString)
|
||||
cv.put("text", text.text)
|
||||
getWritableDatabase.insert("messages", null, cv)
|
||||
case _: ConnectionInfo | _: RequestAddContact | _: ResultAddContact | _: UserName =>
|
||||
case _: RequestAddContact | _: ResultAddContact =>
|
||||
// Never stored.
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue