async log updates added

This commit is contained in:
Raphael Maenle 2022-03-11 12:27:57 +01:00
parent 625d5496e3
commit d5ceace59f
3 changed files with 40 additions and 33 deletions

View File

@ -8,12 +8,9 @@ import android.graphics.Bitmap
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.*
import android.widget.AdapterView.OnItemLongClickListener
import androidmads.library.qrgenearator.QRGContents
import androidmads.library.qrgenearator.QRGEncoder
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.google.firebase.messaging.FirebaseMessaging
@ -56,7 +53,8 @@ class FirstFragment : Fragment() {
subscribeTopic()
bump.callback = { log -> updateLogCallback(log) }
bump.logUpdateCallback = { log -> updateMessageList(log) }
updateMessageList(bump.log)
return binding.root
}
@ -78,6 +76,7 @@ class FirstFragment : Fragment() {
message?.let { binding.textviewScan.text = it.sender.replace("-", "-\u200b")}
}
}
message?.let { binding.textviewScan.text = it.sender.replace("-", "-\u200b")}
message?.let { binding.textviewEncryption.text = "-" + it.password.replace("-", "-\u200b")}
message?.let {binding.idIVQrcode.setImageBitmap(generateQrCode(it.code))}
@ -146,28 +145,20 @@ class FirstFragment : Fragment() {
return newBitmap
}
private fun updateLogCallback(log: JSONArray) {
private fun updateMessageList(log: JSONArray) {
val messages: MutableList<MessageItem> = mutableListOf()
val adapter = NotificationAdapter(requireContext(), messages)
binding.nrMessages.text = log.length().toString()
var breaker = 3
if (log.length() < breaker) {
breaker = log.length()
}
for (l in log.length() - 1 downTo log.length() - breaker) {
for (l in log.length() - 1 downTo 0) {
val m = log[l] as JSONObject
messages.add(
MessageItem(
m.get("title") as String,
message = bump.decryptMessage(m.get("data") as String)
)
)
val msg = MessageItem(m.get("title") as String, message = "...")
messages.add(msg)
bump.asyncDecryptMessage(m.get("data") as String, messages.size-1) { data, location ->
messages[location].message = data
adapter.notifyDataSetChanged()
}
}
for (l in log.length() - breaker downTo 0) {
val m = log[l] as JSONObject
messages.add(MessageItem(m.get("title") as String, encrypted = m.get("data") as String))
}
val adapter = NotificationAdapter(requireContext(), messages)
binding.notificationList.adapter = adapter
adapter.notifyDataSetChanged()
binding.notificationList.setOnItemClickListener { adapterView, view, i, l ->

View File

@ -8,18 +8,19 @@ import com.google.firebase.messaging.FirebaseMessaging
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import kotlin.concurrent.thread
class BumpProcessor constructor(context: Context) {
private val rest: RestSingleton = RestSingleton.getInstance(context)
private val local = LocalData(context)
private var log: JSONArray
private var _log: JSONArray
private val messenger = local.code?.let { MessageProcessor(it) }
var callback: ((JSONArray)->Unit)? = null
var logUpdateCallback: ((JSONArray)->Unit)? = null
init {
log = getLog(context)
_log = getLogFromFile(context)
startUpdateHandler(context)
}
@ -39,27 +40,43 @@ class BumpProcessor constructor(context: Context) {
}
val log: JSONArray
get() {
return _log
}
fun asyncDecryptMessage(message: String, location: Int, callback: (String, Int) -> Unit) {
thread(start = true) {
val decrypt = decryptMessage(message)
Handler(Looper.getMainLooper()).post {callback( decrypt, location)}
}
}
fun decryptMessage(message: String): String {
return messenger!!.decrypt(message)
}
private fun updateLog(context: Context, list: JSONArray) {
log = getLog(context)
_log = getLogFromFile(context)
var change = false
for(i in 0 until list.length()) {
var exists = false
for(j in 0 until log.length()) {
if (list[i].toString() == log[j].toString()) {
for(j in 0 until _log.length()) {
if (list[i].toString() == _log[j].toString()) {
exists = true
break
}
}
if(!exists) {
log.put(list[i])
_log.put(list[i])
addToLog(context, list[i] as JSONObject)
change = true
}
}
callback?.invoke(log)
if(change) {
logUpdateCallback?.invoke(_log)
}
}
private fun getSecret(context: Context):String? {
@ -109,7 +126,7 @@ class BumpProcessor constructor(context: Context) {
return newCode.length == validChars && newCode.length >= 32
}
private fun getLog(context: Context): JSONArray {
private fun getLogFromFile(context: Context): JSONArray {
val logFile = File(context.filesDir, ".bump_log")
val log = JSONArray()
!logFile.exists().let {

View File

@ -10,15 +10,14 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:layout_marginTop="20dp">
android:orientation="vertical">
<TextView
android:id="@+id/textview_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/current_sender"
android:layout_marginTop="40dp"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:layout_gravity="center"
app:layout_constraintEnd_toEndOf="parent"