Minor code enhancements.
This commit is contained in:
parent
b12af56ea7
commit
f31db97230
7 changed files with 30 additions and 30 deletions
|
@ -123,15 +123,15 @@ class AddContactsActivity extends EnsiChatActivity with ChatService.OnConnection
|
|||
* the user is in this activity.
|
||||
*/
|
||||
override def onMessageReceived(messages: SortedSet[Message]): Unit = {
|
||||
messages.foreach(m => {
|
||||
if (m.receiver == service.localDeviceId) {
|
||||
m.messageType match {
|
||||
case Message.Type.RequestAddContact =>
|
||||
messages.foreach(msg => {
|
||||
if (msg.receiver == service.localDeviceId) {
|
||||
msg match {
|
||||
case _: ResultAddContactMessage =>
|
||||
// Remote device wants to add us as a contact, show dialog.
|
||||
val sender = getDevice(m.sender)
|
||||
val sender = getDevice(msg.sender)
|
||||
addDeviceDialog(sender)
|
||||
case Message.Type.ResultAddContact =>
|
||||
if (m.asInstanceOf[ResultAddContactMessage].Accepted) {
|
||||
case m: ResultAddContactMessage =>
|
||||
if (m.Accepted) {
|
||||
// Remote device accepted us as a contact, update state.
|
||||
currentlyAdding += (m.sender ->
|
||||
new AddContactInfo(true, currentlyAdding(m.sender).remoteConfirmed))
|
||||
|
|
|
@ -12,7 +12,7 @@ class EnsiChatActivity extends Activity with ServiceConnection {
|
|||
|
||||
var chatService: Option[ChatService] = None
|
||||
|
||||
var listeners: List[() => Unit] = List.empty
|
||||
var listeners = Set[() => Unit]()
|
||||
|
||||
/**
|
||||
* Starts service and connects to it.
|
||||
|
@ -40,7 +40,7 @@ class EnsiChatActivity extends Activity with ServiceConnection {
|
|||
val binder = iBinder.asInstanceOf[ChatServiceBinder]
|
||||
chatService = Option(binder.getService)
|
||||
listeners.foreach(_())
|
||||
listeners = List.empty
|
||||
listeners = Set.empty
|
||||
}
|
||||
|
||||
override def onServiceDisconnected(componentName: ComponentName) =
|
||||
|
@ -52,7 +52,7 @@ class EnsiChatActivity extends Activity with ServiceConnection {
|
|||
def runOnServiceConnected(l: () => Unit): Unit =
|
||||
chatService match {
|
||||
case Some(s) => l()
|
||||
case None => listeners :+= l
|
||||
case None => listeners += l
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -200,7 +200,7 @@ class ChatService extends Service {
|
|||
}
|
||||
|
||||
connectionListeners.foreach(l => l.get match {
|
||||
case Some(_) => l.apply().onConnectionChanged(devices)
|
||||
case Some(x) => x.onConnectionChanged(devices)
|
||||
case None => connectionListeners -= l
|
||||
})
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ object Device {
|
|||
override def hashCode = Id.hashCode
|
||||
|
||||
override def equals(a: Any) = a match {
|
||||
case other: Device.ID => Id == other.Id
|
||||
case o: Device.ID => Id == o.Id
|
||||
case _ => false
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import android.bluetooth.BluetoothSocket
|
|||
import android.util.Log
|
||||
import com.nutomic.ensichat.messages.{Crypto, DeviceInfoMessage, Message}
|
||||
import org.msgpack.ScalaMessagePack
|
||||
import com.nutomic.ensichat.messages.Message._
|
||||
|
||||
/**
|
||||
* Transfers data between connnected devices.
|
||||
|
@ -107,13 +108,13 @@ class TransferThread(device: Device, socket: BluetoothSocket, service: ChatServi
|
|||
val packer = new ScalaMessagePack().createPacker(OutStream)
|
||||
|
||||
message.messageType match {
|
||||
case Message.Type.Text =>
|
||||
case Type.Text =>
|
||||
val (encrypted, key) = crypto.encrypt(message.receiver, plain)
|
||||
// Message is encrypted.
|
||||
packer.write(true)
|
||||
.write(encrypted)
|
||||
.write(key)
|
||||
case _ =>
|
||||
case Type.DeviceInfo | Type.RequestAddContact | Type.ResultAddContact =>
|
||||
// Message is not encrypted.
|
||||
packer.write(false)
|
||||
.write(plain)
|
||||
|
|
|
@ -95,8 +95,7 @@ class ChatFragment extends ListFragment with OnClickListener
|
|||
/**
|
||||
* Send message if send button was clicked.
|
||||
*/
|
||||
override def onClick(view: View): Unit = {
|
||||
view.getId match {
|
||||
override def onClick(view: View): Unit = view.getId match {
|
||||
case R.id.send =>
|
||||
val text: String = messageText.getText.toString
|
||||
if (!text.isEmpty) {
|
||||
|
@ -109,7 +108,6 @@ class ChatFragment extends ListFragment with OnClickListener
|
|||
messageText.getText.clear()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays new messages in UI.
|
||||
|
|
|
@ -5,7 +5,7 @@ import java.util.Date
|
|||
import android.content.{ContentValues, Context}
|
||||
import android.database.sqlite.{SQLiteDatabase, SQLiteOpenHelper}
|
||||
import com.nutomic.ensichat.bluetooth.Device
|
||||
import com.nutomic.ensichat.messages.{Message, TextMessage}
|
||||
import com.nutomic.ensichat.messages._
|
||||
|
||||
import scala.collection.SortedSet
|
||||
import scala.collection.immutable.TreeSet
|
||||
|
@ -78,7 +78,8 @@ class Database(context: Context) extends SQLiteOpenHelper(context, Database.Data
|
|||
cv.put("date", msg.date.getTime.toString)
|
||||
cv.put("text", msg.text)
|
||||
getWritableDatabase.insert("messages", null, cv)
|
||||
case _ => // Never stored.
|
||||
case _: DeviceInfoMessage | _: RequestAddContactMessage | _: ResultAddContactMessage =>
|
||||
// Never stored.
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Reference in a new issue