Synched call history handling

This commit is contained in:
Juha Heinanen
2026-05-25 08:50:47 +03:00
parent 1656bf8228
commit 5e54a7e03b
@@ -18,12 +18,14 @@ class CallHistoryNew(val aor: String, val peerUri: String, val direction: String
var recording = arrayOf("", "") // Encoder and decoder recording files, merged file is in [0] var recording = arrayOf("", "") // Encoder and decoder recording files, merged file is in [0]
fun add() { fun add() {
BaresipService.callHistory.add(this) synchronized(BaresipService.callHistory) {
val aorSpecificHistory = BaresipService.callHistory.filter { it.aor == this.aor } BaresipService.callHistory.add(this)
if (aorSpecificHistory.size > CALL_HISTORY_SIZE) { val aorSpecificHistory = BaresipService.callHistory.filter { it.aor == this.aor }
val oldestToRemove = aorSpecificHistory.first() if (aorSpecificHistory.size > CALL_HISTORY_SIZE) {
deleteRecordingFiles(oldestToRemove.recording) val oldestToRemove = aorSpecificHistory.first()
BaresipService.callHistory.remove(oldestToRemove) deleteRecordingFiles(oldestToRemove.recording)
BaresipService.callHistory.remove(oldestToRemove)
}
} }
save() save()
} }
@@ -35,41 +37,50 @@ class CallHistoryNew(val aor: String, val peerUri: String, val direction: String
private const val CALL_HISTORY_SIZE = 256 private const val CALL_HISTORY_SIZE = 256
fun aorLatestPeerUri(aor: String): String? { fun aorLatestPeerUri(aor: String): String? {
for (h in BaresipService.callHistory.reversed()) synchronized(BaresipService.callHistory) {
if (h.aor == aor) return h.peerUri for (h in BaresipService.callHistory.reversed())
if (h.aor == aor) return h.peerUri
}
return null return null
} }
fun clear(aor: String) { fun clear(aor: String) {
for (i in BaresipService.callHistory.indices.reversed()) { synchronized(BaresipService.callHistory) {
val h = BaresipService.callHistory[i] for (i in BaresipService.callHistory.indices.reversed()) {
if (h.aor == aor) { val h = BaresipService.callHistory[i]
deleteRecordingFiles(h.recording) if (h.aor == aor) {
BaresipService.callHistory.removeAt(i) deleteRecordingFiles(h.recording)
BaresipService.callHistory.removeAt(i)
}
} }
} }
save() save()
} }
fun remove(startTime: GregorianCalendar?, stopTime: GregorianCalendar) { fun remove(startTime: GregorianCalendar?, stopTime: GregorianCalendar) {
val iterator = BaresipService.callHistory.iterator() synchronized(BaresipService.callHistory) {
while (iterator.hasNext()) { val iterator = BaresipService.callHistory.iterator()
val h = iterator.next() while (iterator.hasNext()) {
if (h.startTime == startTime && h.stopTime == stopTime) { val h = iterator.next()
deleteRecordingFiles(h.recording) if (h.startTime == startTime && h.stopTime == stopTime) {
iterator.remove() deleteRecordingFiles(h.recording)
iterator.remove()
}
} }
} }
save() save()
} }
fun save() { fun save() {
Log.d(TAG, "Saving history of ${BaresipService.callHistory.size} calls") val historyCopy = synchronized(BaresipService.callHistory) {
ArrayList(BaresipService.callHistory)
}
Log.d(TAG, "Saving history of ${historyCopy.size} calls")
val file = File(BaresipService.filesPath + "/call_history") val file = File(BaresipService.filesPath + "/call_history")
try { try {
val fos = FileOutputStream(file) val fos = FileOutputStream(file)
val oos = ObjectOutputStream(fos) val oos = ObjectOutputStream(fos)
oos.writeObject(BaresipService.callHistory) oos.writeObject(historyCopy)
oos.close() oos.close()
fos.close() fos.close()
} catch (e: IOException) { } catch (e: IOException) {
@@ -103,8 +114,10 @@ class CallHistoryNew(val aor: String, val peerUri: String, val direction: String
} }
fun clearRecordings() { fun clearRecordings() {
for (h in BaresipService.callHistory) synchronized(BaresipService.callHistory) {
h.recording = arrayOf("", "") for (h in BaresipService.callHistory)
h.recording = arrayOf("", "")
}
} }
@Suppress("UNUSED") @Suppress("UNUSED")