Skip to content

Commit f4a5b0a

Browse files
committed
Fix Bugs
Fix bug where application crashes on QR scan Change deletion time for forgotten contacts to be 24 hours rather than 0
1 parent 00698b4 commit f4a5b0a

2 files changed

Lines changed: 55 additions & 35 deletions

File tree

android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/MainActivity.kt

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ class MainActivity : Activity() {
380380
var currentTime = System.currentTimeMillis()
381381

382382
//Add number of hours to the current time
383-
currentTime += (hours - 24) * 3600000
383+
currentTime += (hours) * 3600000
384384

385385
val formattedTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Date(currentTime))
386386

@@ -421,42 +421,56 @@ class MainActivity : Activity() {
421421
}
422422

423423
private fun executeThisTask() {
424-
// Add your task logic here
425424
Log.d("MyRealWorker", "Worker is working...")
426425

427-
// write in a txt file the time at which the worker was executing
426+
// Write the current time to output.txt
428427
val currentTime = System.currentTimeMillis()
429-
430-
// Format the time into a readable string
431428
val formattedTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Date(currentTime))
432-
433429
applicationContext.openFileOutput("output.txt", Context.MODE_APPEND).use {
434-
it.write("$formattedTime\n".toByteArray()) // Appending the formatted time with a newline
430+
it.write("$formattedTime\n".toByteArray())
435431
}
436432

437-
//if json file does not exist, return
438-
val jsonFile = File(filesDir, "contacts.json")
439-
if (!jsonFile.exists()) {
433+
// Check if contacts.json exists. If not, return.
434+
val contactsFile = File(applicationContext.filesDir, "contacts.json")
435+
if (!contactsFile.exists()) {
436+
Log.d("MyRealWorker", "contacts.json does not exist, returning...")
440437
return
441438
}
442439

443-
//prepare kill list file if it doesn't exist
440+
// Read existing JSON from contacts.json
441+
val jsonString = applicationContext.openFileInput("contacts.json").bufferedReader().use { it.readText() }
442+
val json = if (jsonString.isNotEmpty()) JSONObject(jsonString) else JSONObject()
443+
Log.d("MyRealWorker", "contacts.json is $json")
444+
445+
// Prepare killList file in append mode
444446
val killListFile = applicationContext.openFileOutput("killList.txt", Context.MODE_APPEND)
445447

446-
//Open JSON file and check all keys (contacts) and their value (time)
447-
val jsonFileNew = File(filesDir, "contacts.json").bufferedReader().use { it.readText() }
448-
val json = JSONObject(jsonFileNew)
448+
// Parse and check all keys, record which to remove
449449
val keys = json.keys()
450-
for (key in keys) {
450+
val keysToRemove = mutableListOf<String>()
451+
val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
452+
453+
while (keys.hasNext()) {
454+
val key = keys.next()
451455
val value = json.getString(key)
452-
//check if the value is older than currentTime, value has following format "yyyy-MM-dd HH:mm:ss"
453-
if (SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).parse(value).time < currentTime) {
454-
//add key to kill list
455-
killListFile.write("$key\n".toByteArray())
456-
//remove key from contacts.json
457-
removeEntryFromJsonFile(key)
456+
// Check if the stored time is older than currentTime
457+
val contactTime = dateFormat.parse(value)?.time ?: 0L
458+
if (contactTime < currentTime) {
459+
keysToRemove.add(key)
458460
}
459461
}
462+
463+
// Remove old keys and write them into killList
464+
keysToRemove.forEach { key ->
465+
killListFile.write("$key\n".toByteArray())
466+
json.remove(key)
467+
}
468+
killListFile.close()
469+
470+
// Write updated JSON back to contacts.json (overwrite, not append)
471+
applicationContext.openFileOutput("contacts.json", Context.MODE_PRIVATE).use { out ->
472+
out.write(json.toString().toByteArray())
473+
}
460474
}
461475

462476
// Function to delete all contacts in the kill list
@@ -485,6 +499,7 @@ class MainActivity : Activity() {
485499
override fun onResume() {
486500
Log.d("onResume", "")
487501
registerUntrusted()
502+
executeThisTask()
488503
deleteFromKillList()
489504
super.onResume()
490505
/*

android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/WebAppInterface.kt

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -380,22 +380,27 @@ class WebAppInterface(val act: MainActivity, val webView: WebView) {
380380
Log.d("decodedEntry", decodedEntry.toString())
381381

382382
//get bytes from the decoded entry
383-
val entryBytes = decodedEntry?.let {it.getBytes()}
384-
Log.d("entryBytes1", entryBytes.toString())
385-
386-
//decrypt the bytes to get the clear text
387-
val clear = entryBytes?.let { act.idStore.identity.decryptPrivateMessage(it) }
388-
Log.d("clear", clear.toString())
389-
390-
val entryType = clear?.let { getFromEntry(it, 0) }
391-
if (entryType == "TRT") {
392-
if (clear is ByteArray) {
393-
val contactID = getFromEntry(clear, 2)
394-
val trustLevel = getFromEntry(clear, 3)
395-
if (contactID != null && trustLevel != null) {
396-
contactsTrustLevel[contactID.toString()] = trustLevel.toString().toInt()
383+
try {
384+
val entryBytes = decodedEntry?.let {it.getBytes()}
385+
Log.d("entryBytes1", entryBytes.toString())
386+
//decrypt the bytes to get the clear text
387+
val clear = entryBytes?.let { act.idStore.identity.decryptPrivateMessage(it) }
388+
Log.d("clear", clear.toString())
389+
390+
val entryType = clear?.let { getFromEntry(it, 0) }
391+
if (entryType == "TRT") {
392+
if (clear is ByteArray) {
393+
val contactID = getFromEntry(clear, 2)
394+
val trustLevel = getFromEntry(clear, 3)
395+
if (contactID != null && trustLevel != null) {
396+
contactsTrustLevel[contactID.toString()] = trustLevel.toString().toInt()
397+
}
397398
}
398399
}
400+
} catch (e: Exception) {
401+
Log.d("entryBytes1", "null")
402+
//skip the entry if it is null
403+
continue
399404
}
400405
}
401406
}

0 commit comments

Comments
 (0)