Skip to content
Permalink
Browse files

Updated Back4App, Nexmo samples to latest e3kit version

  • Loading branch information...
BuddahLD committed Jun 19, 2019
1 parent 284c953 commit cd47ed0aa545f5139347a233f4fdfd7dba050536
@@ -18,6 +18,7 @@ import com.android.virgilsecurity.virgilback4app.util.Const
import com.android.virgilsecurity.virgilback4app.util.Utils
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.parse.*
import com.virgilsecurity.android.common.data.model.LookupResult
import com.virgilsecurity.sdk.client.exceptions.VirgilCardIsNotFoundException
import com.virgilsecurity.sdk.client.exceptions.VirgilCardServiceException
import com.virgilsecurity.sdk.crypto.PublicKey
@@ -270,9 +271,9 @@ class ChatThreadFragment : BaseFragment<ChatThreadActivity>() {
Utils.toast(this, Utils.resolveError(t))
}

private fun onGetPublicKeySuccess(publicKey: PublicKey) {
private fun onGetPublicKeySuccess(lookupResult: LookupResult) {
showProgress(false)
adapter.interlocutorPublicKey = publicKey
adapter.interlocutorLookupResult = lookupResult
presenter.requestMessages(thread,
50,
page,
@@ -6,6 +6,7 @@ import com.android.virgilsecurity.virgilback4app.model.ChatThread
import com.android.virgilsecurity.virgilback4app.model.Message
import com.android.virgilsecurity.virgilback4app.util.RxEthree
import com.android.virgilsecurity.virgilback4app.util.RxParse
import com.virgilsecurity.android.common.data.model.LookupResult
import com.virgilsecurity.sdk.crypto.PublicKey
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
@@ -25,7 +26,7 @@ class ChatThreadPresenter(context: Context) {
private lateinit var sortCriteria: String
private val compositeDisposable = CompositeDisposable()
private val eThree = AppVirgil.eThree
private lateinit var publicKey: PublicKey
private lateinit var lookupResult: LookupResult
private val rxEthree = RxEthree(context)

fun requestMessages(thread: ChatThread, limit: Int,
@@ -75,7 +76,7 @@ class ChatThreadPresenter(context: Context) {
onSuccess: () -> Unit,
onError: (Throwable) -> Unit) {

val encryptedText = eThree.encrypt(text, listOf(publicKey))
val encryptedText = eThree.encrypt(text, lookupResult)
val disposable = RxParse.sendMessage(encryptedText, thread)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
@@ -92,15 +93,15 @@ class ChatThreadPresenter(context: Context) {
}

fun requestPublicKey(identity: String,
onSuccess: (PublicKey) -> Unit,
onSuccess: (LookupResult) -> Unit,
onError: (Throwable) -> Unit) {

val disposable = rxEthree.findPublicKey(identity)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onSuccess = {
publicKey = it
lookupResult = it
onSuccess(it)
},
onError = {
@@ -8,6 +8,7 @@ import com.android.virgilsecurity.virgilback4app.AppVirgil
import com.android.virgilsecurity.virgilback4app.R
import com.android.virgilsecurity.virgilback4app.model.Message
import com.parse.ParseUser
import com.virgilsecurity.android.common.data.model.LookupResult
import com.virgilsecurity.android.ethree.kotlin.interaction.EThree
import com.virgilsecurity.sdk.crypto.PublicKey

@@ -20,7 +21,7 @@ class ChatThreadRVAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private var items: MutableList<Message> = mutableListOf()
private var eThree: EThree = AppVirgil.eThree
lateinit var interlocutorPublicKey: PublicKey
lateinit var interlocutorLookupResult: LookupResult

@IntDef(MessageType.ME, MessageType.YOU)
private annotation class MessageType {
@@ -49,7 +50,8 @@ class ChatThreadRVAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
}

override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
val decryptedText = eThree.decrypt(items[position].body, interlocutorPublicKey)
val decryptedText = eThree.decrypt(items[position].body,
interlocutorLookupResult.entries.first().value)

when (viewHolder) {
is HolderMessageMe -> viewHolder.bind(decryptedText)
@@ -2,8 +2,11 @@ package com.android.virgilsecurity.virgilback4app.util

import android.content.Context
import com.android.virgilsecurity.virgilback4app.AppVirgil
import com.virgilsecurity.android.common.data.model.LookupResult
import com.virgilsecurity.android.ethree.kotlin.callback.OnCompleteListener
import com.virgilsecurity.android.ethree.kotlin.callback.OnGetTokenCallback
import com.virgilsecurity.android.ethree.kotlin.callback.OnResultListener
import com.virgilsecurity.android.ethree.kotlin.interaction.EThree
import com.virgilsecurity.sdk.crypto.PublicKey
import io.reactivex.Completable
import io.reactivex.Single

@@ -16,24 +19,23 @@ class RxEthree(val context: Context) {

fun initEthree(): Single<EThree> = Single.create<EThree> { e ->
EThree.initialize(context,
object : EThree.OnGetTokenCallback {
object : OnGetTokenCallback {
override fun onGetToken(): String {
return preferences.virgilToken()!!
}
},
object : EThree.OnResultListener<EThree> {
override fun onSuccess(result: EThree) {
e.onSuccess(result)
}
}).addCallback(object : OnResultListener<EThree> {
override fun onSuccess(result: EThree) {
e.onSuccess(result)
}

override fun onError(throwable: Throwable) {
e.onError(throwable)
}
})
override fun onError(throwable: Throwable) {
e.onError(throwable)
}
})
}

fun registerEthree(): Completable = Completable.create { e ->
AppVirgil.eThree.register(object : EThree.OnCompleteListener {
AppVirgil.eThree.register().addCallback(object : OnCompleteListener {
override fun onSuccess() {
e.onComplete()
}
@@ -44,22 +46,17 @@ class RxEthree(val context: Context) {
})
}

fun findPublicKey(identity: String): Single<PublicKey> = Single.create<PublicKey> { e ->
AppVirgil.eThree.lookupPublicKeys(listOf(identity),
object : EThree.OnResultListener<Map<String, PublicKey>> {
override fun onSuccess(result: Map<String, PublicKey>) {
val publicKey = result[identity]

if (publicKey != null)
e.onSuccess(publicKey)
else
e.onError(Throwable("Public key for identity " +
"\"$identity\" was not found"))
}

override fun onError(throwable: Throwable) {
e.onError(throwable)
}
})
fun findPublicKey(identity: String): Single<LookupResult> = Single.create<LookupResult> { e ->
AppVirgil.eThree
.lookupPublicKeys(identity)
.addCallback(object : OnResultListener<LookupResult> {
override fun onSuccess(result: LookupResult) {
e.onSuccess(result)
}

override fun onError(throwable: Throwable) {
e.onError(throwable)
}
})
}
}
}
@@ -42,7 +42,7 @@ buildscript {
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.android.tools.build:gradle:3.4.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"

// NOTE: Do not place your application dependencies here; they belong
@@ -67,7 +67,7 @@ task clean(type: Delete) {
ext {
supportLibrary = "28.0.0"
constraintLayout = "1.1.3"
ethree = "0.3.8"
ethree = "0.5.0"
rxKotlin = "2.3.0"
rxAndroid = "2.1.5"
retrofit = "2.3.0"
@@ -12,6 +12,7 @@ import com.android.virgilsecurity.ethreenexmodemo.R
import com.android.virgilsecurity.ethreenexmodemo.data.model.chat.NexmoMessage
import com.android.virgilsecurity.ethreenexmodemo.ui.chatControl.ChatControlActivity
import com.nexmo.client.NexmoConversation
import com.virgilsecurity.android.common.data.model.LookupResult
import com.virgilsecurity.android.common.exceptions.PublicKeyNotFoundException
import com.virgilsecurity.sdk.crypto.PublicKey
import kotlinx.android.synthetic.main.fragment_thread.*
@@ -83,18 +84,18 @@ class ThreadFragment : Fragment() {
presenter.stopMessagesListener(thread)
}

private fun onGetPublicKeySuccess(publicKey: PublicKey) {
private fun onGetPublicKeySuccess(lookupResult: LookupResult) {
activity!!.runOnUiThread {
pbLoading.visibility = View.INVISIBLE
adapter.setPublicKey(publicKey)
adapter.setLookupResult(lookupResult)
}
}

private fun onGetPublicKeyError(throwable: Throwable) {
activity!!.runOnUiThread {
pbLoading.visibility = View.INVISIBLE
if (throwable is PublicKeyNotFoundException)
Toast.makeText(activity!!, "No public key was found", Toast.LENGTH_SHORT).show()
Toast.makeText(activity!!, "No public key was found", Toast.LENGTH_SHORT).show()
else
Toast.makeText(activity!!, throwable.message, Toast.LENGTH_SHORT).show()
}
@@ -125,8 +126,8 @@ class ThreadFragment : Fragment() {
private const val KEY_THREAD = "KEY_THREAD"

fun instance(thread: NexmoConversation): ThreadFragment =
with(Bundle().apply { putParcelable(KEY_THREAD, thread) }) {
ThreadFragment().apply { arguments = this@with }
}
with(Bundle().apply { putParcelable(KEY_THREAD, thread) }) {
ThreadFragment().apply { arguments = this@with }
}
}
}
@@ -7,6 +7,8 @@ import com.android.virgilsecurity.ethreenexmodemo.data.model.chat.NexmoMessage
import com.nexmo.client.*
import com.nexmo.client.request_listener.NexmoApiError
import com.nexmo.client.request_listener.NexmoRequestListener
import com.virgilsecurity.android.common.data.model.LookupResult
import com.virgilsecurity.android.ethree.kotlin.callback.OnResultListener
import com.virgilsecurity.android.ethree.kotlin.interaction.EThree
import com.virgilsecurity.sdk.crypto.PublicKey

@@ -16,7 +18,7 @@ import com.virgilsecurity.sdk.crypto.PublicKey
class ThreadPresenter(context: Context) {

private val preferences = Preferences.instance(context)
private lateinit var publicKey: PublicKey
private lateinit var lookupResult: LookupResult
private lateinit var messageEventListener: NexmoMessageEventListener

fun startMessagesListener(thread: NexmoConversation, onNewMessage: (NexmoMessage) -> Unit) {
@@ -32,7 +34,7 @@ class ThreadPresenter(context: Context) {
override fun onTextEvent(textEvent: NexmoTextEvent) {
if (textEvent.member.user.name != preferences.username()) {
val sender = thread.allMembers.first { it.user.name != preferences.username() }
.user.name // TODO get right name
.user.name // TODO get right name
onNewMessage(NexmoMessage(textEvent.text, sender))
}
}
@@ -55,12 +57,12 @@ class ThreadPresenter(context: Context) {
}

fun requestSendMessage(
thread: NexmoConversation,
text: String,
onSuccess: (NexmoMessage) -> Unit,
onError: (Throwable) -> Unit
thread: NexmoConversation,
text: String,
onSuccess: (NexmoMessage) -> Unit,
onError: (Throwable) -> Unit
) {
val encryptedText = EThreeNexmoApp.eThree.encrypt(text, listOf(publicKey))
val encryptedText = EThreeNexmoApp.eThree.encrypt(text, lookupResult)

thread.sendText(encryptedText, object : NexmoRequestListener<Void> {
override fun onSuccess(p0: Void?) {
@@ -74,21 +76,21 @@ class ThreadPresenter(context: Context) {
})
}

fun requestPublicKey(thread: NexmoConversation, onSuccess: (PublicKey) -> Unit, onError: (Throwable) -> Unit) {
fun requestPublicKey(thread: NexmoConversation, onSuccess: (LookupResult) -> Unit, onError: (Throwable) -> Unit) {
val interlocutor = thread.allMembers.first { it.user.name != preferences.username() }.user.name

EThreeNexmoApp.eThree.lookupPublicKeys(
listOf(interlocutor),
object : EThree.OnResultListener<Map<String, PublicKey>> {
override fun onSuccess(result: Map<String, PublicKey>) {
publicKey = result.getValue(interlocutor)
onSuccess(publicKey)
}

override fun onError(throwable: Throwable) {
onError(throwable)
}
})
EThreeNexmoApp.eThree
.lookupPublicKeys(interlocutor)
.addCallback(object : OnResultListener<LookupResult> {
override fun onSuccess(result: LookupResult) {
lookupResult = result
onSuccess(lookupResult)
}

override fun onError(throwable: Throwable) {
onError(throwable)
}
})
}

fun stopMessagesListener(thread: NexmoConversation) {

0 comments on commit cd47ed0

Please sign in to comment.
You can’t perform that action at this time.