@@ -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 /*
0 commit comments