From f31db97230237c80f29e561d36808662d3072290 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 16 Nov 2014 22:32:18 +0200 Subject: [PATCH] Minor code enhancements. --- .../activities/AddContactsActivity.scala | 14 +++++------ .../activities/EnsiChatActivity.scala | 6 ++--- .../ensichat/bluetooth/ChatService.scala | 2 +- .../nutomic/ensichat/bluetooth/Device.scala | 2 +- .../ensichat/bluetooth/TransferThread.scala | 7 +++--- .../ensichat/fragments/ChatFragment.scala | 24 +++++++++---------- .../com/nutomic/ensichat/util/Database.scala | 5 ++-- 7 files changed, 30 insertions(+), 30 deletions(-) diff --git a/app/src/main/scala/com/nutomic/ensichat/activities/AddContactsActivity.scala b/app/src/main/scala/com/nutomic/ensichat/activities/AddContactsActivity.scala index be6495d..9c1daec 100644 --- a/app/src/main/scala/com/nutomic/ensichat/activities/AddContactsActivity.scala +++ b/app/src/main/scala/com/nutomic/ensichat/activities/AddContactsActivity.scala @@ -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)) diff --git a/app/src/main/scala/com/nutomic/ensichat/activities/EnsiChatActivity.scala b/app/src/main/scala/com/nutomic/ensichat/activities/EnsiChatActivity.scala index 78ef79c..d9dbfc7 100644 --- a/app/src/main/scala/com/nutomic/ensichat/activities/EnsiChatActivity.scala +++ b/app/src/main/scala/com/nutomic/ensichat/activities/EnsiChatActivity.scala @@ -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 } /** diff --git a/app/src/main/scala/com/nutomic/ensichat/bluetooth/ChatService.scala b/app/src/main/scala/com/nutomic/ensichat/bluetooth/ChatService.scala index f826317..d7cfdd6 100644 --- a/app/src/main/scala/com/nutomic/ensichat/bluetooth/ChatService.scala +++ b/app/src/main/scala/com/nutomic/ensichat/bluetooth/ChatService.scala @@ -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 }) } diff --git a/app/src/main/scala/com/nutomic/ensichat/bluetooth/Device.scala b/app/src/main/scala/com/nutomic/ensichat/bluetooth/Device.scala index 836fcd4..170231b 100644 --- a/app/src/main/scala/com/nutomic/ensichat/bluetooth/Device.scala +++ b/app/src/main/scala/com/nutomic/ensichat/bluetooth/Device.scala @@ -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 } diff --git a/app/src/main/scala/com/nutomic/ensichat/bluetooth/TransferThread.scala b/app/src/main/scala/com/nutomic/ensichat/bluetooth/TransferThread.scala index 3f4f69a..433a1c8 100644 --- a/app/src/main/scala/com/nutomic/ensichat/bluetooth/TransferThread.scala +++ b/app/src/main/scala/com/nutomic/ensichat/bluetooth/TransferThread.scala @@ -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. @@ -46,7 +47,7 @@ class TransferThread(device: Device, socket: BluetoothSocket, service: ChatServi while (socket.isConnected) { try { val up = new ScalaMessagePack().createUnpacker(InStream) - val isEncrypted = up.readBoolean() + val isEncrypted = up.readBoolean() val plain = if (isEncrypted) { val encrypted = up.readByteArray() @@ -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) diff --git a/app/src/main/scala/com/nutomic/ensichat/fragments/ChatFragment.scala b/app/src/main/scala/com/nutomic/ensichat/fragments/ChatFragment.scala index f92af90..95bf645 100644 --- a/app/src/main/scala/com/nutomic/ensichat/fragments/ChatFragment.scala +++ b/app/src/main/scala/com/nutomic/ensichat/fragments/ChatFragment.scala @@ -95,20 +95,18 @@ class ChatFragment extends ListFragment with OnClickListener /** * Send message if send button was clicked. */ - override def onClick(view: View): Unit = { - view.getId match { - case R.id.send => - val text: String = messageText.getText.toString - if (!text.isEmpty) { - if (!chatService.isConnected(device)) { - Toast.makeText(getActivity, R.string.contact_offline_toast, Toast.LENGTH_SHORT).show() - return - } - chatService.send( - new TextMessage(chatService.localDeviceId, device, new Date(), text.toString)) - messageText.getText.clear() + override def onClick(view: View): Unit = view.getId match { + case R.id.send => + val text: String = messageText.getText.toString + if (!text.isEmpty) { + if (!chatService.isConnected(device)) { + Toast.makeText(getActivity, R.string.contact_offline_toast, Toast.LENGTH_SHORT).show() + return } - } + chatService.send( + new TextMessage(chatService.localDeviceId, device, new Date(), text.toString)) + messageText.getText.clear() + } } /** diff --git a/app/src/main/scala/com/nutomic/ensichat/util/Database.scala b/app/src/main/scala/com/nutomic/ensichat/util/Database.scala index 727baa8..578755f 100644 --- a/app/src/main/scala/com/nutomic/ensichat/util/Database.scala +++ b/app/src/main/scala/com/nutomic/ensichat/util/Database.scala @@ -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. } /**