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