Minor code enhancements.

This commit is contained in:
Felix Ableitner 2014-11-16 22:32:18 +02:00
parent b12af56ea7
commit f31db97230
7 changed files with 30 additions and 30 deletions

View file

@ -123,15 +123,15 @@ class AddContactsActivity extends EnsiChatActivity with ChatService.OnConnection
* the user is in this activity. * the user is in this activity.
*/ */
override def onMessageReceived(messages: SortedSet[Message]): Unit = { override def onMessageReceived(messages: SortedSet[Message]): Unit = {
messages.foreach(m => { messages.foreach(msg => {
if (m.receiver == service.localDeviceId) { if (msg.receiver == service.localDeviceId) {
m.messageType match { msg match {
case Message.Type.RequestAddContact => case _: ResultAddContactMessage =>
// Remote device wants to add us as a contact, show dialog. // Remote device wants to add us as a contact, show dialog.
val sender = getDevice(m.sender) val sender = getDevice(msg.sender)
addDeviceDialog(sender) addDeviceDialog(sender)
case Message.Type.ResultAddContact => case m: ResultAddContactMessage =>
if (m.asInstanceOf[ResultAddContactMessage].Accepted) { if (m.Accepted) {
// Remote device accepted us as a contact, update state. // Remote device accepted us as a contact, update state.
currentlyAdding += (m.sender -> currentlyAdding += (m.sender ->
new AddContactInfo(true, currentlyAdding(m.sender).remoteConfirmed)) new AddContactInfo(true, currentlyAdding(m.sender).remoteConfirmed))

View file

@ -12,7 +12,7 @@ class EnsiChatActivity extends Activity with ServiceConnection {
var chatService: Option[ChatService] = None var chatService: Option[ChatService] = None
var listeners: List[() => Unit] = List.empty var listeners = Set[() => Unit]()
/** /**
* Starts service and connects to it. * Starts service and connects to it.
@ -40,7 +40,7 @@ class EnsiChatActivity extends Activity with ServiceConnection {
val binder = iBinder.asInstanceOf[ChatServiceBinder] val binder = iBinder.asInstanceOf[ChatServiceBinder]
chatService = Option(binder.getService) chatService = Option(binder.getService)
listeners.foreach(_()) listeners.foreach(_())
listeners = List.empty listeners = Set.empty
} }
override def onServiceDisconnected(componentName: ComponentName) = override def onServiceDisconnected(componentName: ComponentName) =
@ -52,7 +52,7 @@ class EnsiChatActivity extends Activity with ServiceConnection {
def runOnServiceConnected(l: () => Unit): Unit = def runOnServiceConnected(l: () => Unit): Unit =
chatService match { chatService match {
case Some(s) => l() case Some(s) => l()
case None => listeners :+= l case None => listeners += l
} }
/** /**

View file

@ -200,7 +200,7 @@ class ChatService extends Service {
} }
connectionListeners.foreach(l => l.get match { connectionListeners.foreach(l => l.get match {
case Some(_) => l.apply().onConnectionChanged(devices) case Some(x) => x.onConnectionChanged(devices)
case None => connectionListeners -= l case None => connectionListeners -= l
}) })
} }

View file

@ -15,7 +15,7 @@ object Device {
override def hashCode = Id.hashCode override def hashCode = Id.hashCode
override def equals(a: Any) = a match { override def equals(a: Any) = a match {
case other: Device.ID => Id == other.Id case o: Device.ID => Id == o.Id
case _ => false case _ => false
} }

View file

@ -6,6 +6,7 @@ import android.bluetooth.BluetoothSocket
import android.util.Log import android.util.Log
import com.nutomic.ensichat.messages.{Crypto, DeviceInfoMessage, Message} import com.nutomic.ensichat.messages.{Crypto, DeviceInfoMessage, Message}
import org.msgpack.ScalaMessagePack import org.msgpack.ScalaMessagePack
import com.nutomic.ensichat.messages.Message._
/** /**
* Transfers data between connnected devices. * Transfers data between connnected devices.
@ -46,7 +47,7 @@ class TransferThread(device: Device, socket: BluetoothSocket, service: ChatServi
while (socket.isConnected) { while (socket.isConnected) {
try { try {
val up = new ScalaMessagePack().createUnpacker(InStream) val up = new ScalaMessagePack().createUnpacker(InStream)
val isEncrypted = up.readBoolean() val isEncrypted = up.readBoolean()
val plain = val plain =
if (isEncrypted) { if (isEncrypted) {
val encrypted = up.readByteArray() val encrypted = up.readByteArray()
@ -107,13 +108,13 @@ class TransferThread(device: Device, socket: BluetoothSocket, service: ChatServi
val packer = new ScalaMessagePack().createPacker(OutStream) val packer = new ScalaMessagePack().createPacker(OutStream)
message.messageType match { message.messageType match {
case Message.Type.Text => case Type.Text =>
val (encrypted, key) = crypto.encrypt(message.receiver, plain) val (encrypted, key) = crypto.encrypt(message.receiver, plain)
// Message is encrypted. // Message is encrypted.
packer.write(true) packer.write(true)
.write(encrypted) .write(encrypted)
.write(key) .write(key)
case _ => case Type.DeviceInfo | Type.RequestAddContact | Type.ResultAddContact =>
// Message is not encrypted. // Message is not encrypted.
packer.write(false) packer.write(false)
.write(plain) .write(plain)

View file

@ -95,20 +95,18 @@ class ChatFragment extends ListFragment with OnClickListener
/** /**
* Send message if send button was clicked. * Send message if send button was clicked.
*/ */
override def onClick(view: View): Unit = { override def onClick(view: View): Unit = view.getId match {
view.getId match { case R.id.send =>
case R.id.send => val text: String = messageText.getText.toString
val text: String = messageText.getText.toString if (!text.isEmpty) {
if (!text.isEmpty) { if (!chatService.isConnected(device)) {
if (!chatService.isConnected(device)) { Toast.makeText(getActivity, R.string.contact_offline_toast, Toast.LENGTH_SHORT).show()
Toast.makeText(getActivity, R.string.contact_offline_toast, Toast.LENGTH_SHORT).show() return
return
}
chatService.send(
new TextMessage(chatService.localDeviceId, device, new Date(), text.toString))
messageText.getText.clear()
} }
} chatService.send(
new TextMessage(chatService.localDeviceId, device, new Date(), text.toString))
messageText.getText.clear()
}
} }
/** /**

View file

@ -5,7 +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 com.nutomic.ensichat.bluetooth.Device 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.SortedSet
import scala.collection.immutable.TreeSet 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("date", msg.date.getTime.toString)
cv.put("text", msg.text) cv.put("text", msg.text)
getWritableDatabase.insert("messages", null, cv) getWritableDatabase.insert("messages", null, cv)
case _ => // Never stored. case _: DeviceInfoMessage | _: RequestAddContactMessage | _: ResultAddContactMessage =>
// Never stored.
} }
/** /**