Skip to content

Commit

Permalink
calculate cache size only for log dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Mar 28, 2024
1 parent 37e333f commit d7194db
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,13 @@ class AlertHandler @Inject constructor(
private fun checkStrikes(strikes: Strikes?, location: Location?) {
lastStrikes = strikes

Log.v(
Main.LOG_TAG,
"AlertHandler.checkStrikes() strikes: ${strikes != null}, location: ${locationHandler.location != null}"
)
val alertResult = if (alertEnabled && location != null && strikes != null) {
val checkStrikes = alertDataHandler.checkStrikes(strikes, location, alertParameters)
checkStrikes
alertDataHandler.checkStrikes(strikes, location, alertParameters)
} else {
Log.v(
Main.LOG_TAG,
"AlertHandler.checkStrikes() strikes: ${strikes != null}, location: ${locationHandler.location != null}"
)
null
}

Expand All @@ -194,7 +193,7 @@ class AlertHandler @Inject constructor(

private fun broadcastResult(alertResult: AlertResult?) {
val alertEvent = if (alertResult != null) AlertResultEvent(alertResult) else ALERT_CANCEL_EVENT
Log.v(Main.LOG_TAG, "AlertHandler.broadcastResult(${alertEvent})")
Log.d(Main.LOG_TAG, "AlertHandler.broadcastResult(${alertEvent})")
alertConsumerContainer.storeAndBroadcast(alertEvent)
}

Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/org/blitzortung/android/app/AppService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class AppService : Service(), OnSharedPreferenceChangeListener {
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.i(Main.LOG_TAG, "AppService.onStartCommand() startId: $startId $intent")
Log.v(Main.LOG_TAG, "AppService.onStartCommand() startId: $startId $intent")

if (!locationHandler.backgroundMode) {
locationHandler.shutdown()
Expand All @@ -114,17 +114,17 @@ class AppService : Service(), OnSharedPreferenceChangeListener {
currentTimeSeconds - it
}
if (timeDifference == null || timeDifference > 0.6 * backgroundPeriod) {
Log.v(Main.LOG_TAG, "AppService.onStartCommand() with time difference ${timeDifference ?: 0} s")
Log.i(Main.LOG_TAG, "AppService.onStartCommand() with time difference ${timeDifference ?: 0} s")
lastUpdateTime = currentTimeSeconds
dataHandler.updateData()
} else {
Log.v(
Log.d(
Main.LOG_TAG,
"AppService.onStartCommand() skip with insufficient time passed: ${currentTimeSeconds - lastUpdateTime!!} s < $backgroundPeriod s"
)
}
} else {
Log.v(Main.LOG_TAG, "AppService.onStartCommand() intent ${intent?.action}")
Log.d(Main.LOG_TAG, "AppService.onStartCommand() non matching intent ${intent?.action}")
}

return START_STICKY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class MainPopupMenu(
alertHandler
)

R.id.menu_log -> LogDialog(context, buildVersion)
R.id.menu_log -> LogDialog(context, dataHandler.calculateTotalCacheSize(), buildVersion)

R.id.menu_changelog -> changeLogComponent.getChangeLogDialog(context)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ internal class FetchBackgroundDataTask(
if (wakeLock.isHeld) {
try {
wakeLock.release()
Log.v(Main.LOG_TAG, "FetchBackgroundDataTask.postExecute() released wakelock $wakeLock")
if (wakeLock.isHeld) {
Log.v(Main.LOG_TAG, "FetchBackgroundDataTask.postExecute() released wakelock $wakeLock")
}
} catch (e: RuntimeException) {
Log.e(Main.LOG_TAG, "FetchBackgroundDataTask.postExecute() release wakelock failed ", e)
}
Expand All @@ -43,7 +45,7 @@ internal class FetchBackgroundDataTask(
wakeLock.acquire()
}
if (wakeLock.isHeld) {
Log.v(Main.LOG_TAG, "FetchBackgroundDataTask aquired wakelock $wakeLock")
//Log.v(Main.LOG_TAG, "FetchBackgroundDataTask aquired wakelock $wakeLock")

val updatedParameters =
parameters.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ class MainDataHandler @Inject constructor(
val event = it.copy(flags = flags)
if (!it.containsRealtimeData()) {
cache.put(event.parameters, event)
} else {
cache.logStats()
}
sendEvent(event)
}, ::toast).execute(parameters = parameters, history = history)
Expand Down Expand Up @@ -445,6 +443,8 @@ class MainDataHandler @Inject constructor(
}
}

fun calculateTotalCacheSize(): Int = cache.calculateTotalSize()


}

Expand Down
24 changes: 12 additions & 12 deletions app/src/main/java/org/blitzortung/android/data/cache/DataCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ class DataCache @Inject constructor() {
cache[parameters] = Timestamped(dataEvent)
}

fun logStats() {
var totalSize = 0L
cache.entries.forEach {
val baos = ByteArrayOutputStream()
val oos = ObjectOutputStream(baos)
oos.writeObject(it.value)
oos.close()
val bytes = baos.toByteArray()
Log.v(LOG_TAG, "${it.key} -> ${bytes.size}")
totalSize += bytes.size
}
Log.v(LOG_TAG, "total size: %.2f MB".format(totalSize/1024f/1024f))
fun calculateTotalSize(): Int = cache.entries.fold(0) { acc, entry ->
val size = determineObjectSize(entry.value)
Log.v(LOG_TAG, "${entry.key} -> ${size}")
acc + size
}

private fun determineObjectSize(obj: Any): Int {
val baos = ByteArrayOutputStream()
val oos = ObjectOutputStream(baos)
oos.writeObject(obj)
oos.close()
return baos.size()
}

fun clear() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.blitzortung.android.dialogs.log.LogProvider

class LogDialog(
context: Context,
private val cacheSize: Int,
private val buildVersion: BuildVersion,
private val logProvider: LogProvider = LogProvider()
) : android.app.AlertDialog(context) {
Expand All @@ -48,9 +49,10 @@ class LogDialog(

val versionText = getVersionString()
val deviceText = getDeviceString()
val cacheText = getCacheString()
val logLines = logProvider.getLogLines()

val logText = versionText + "\n\n" + deviceText + "\n\n" + logLines.joinToString("\n")
val logText = versionText + "\n\n" + deviceText + "\n\n" + cacheText + "\n\n" + logLines.joinToString("\n")

with(findViewById<TextView>(R.id.log_text)) {
setHorizontallyScrolling(true)
Expand All @@ -62,6 +64,10 @@ class LogDialog(
}
}

private fun getCacheString(): String {
return "Cache size: %.3f MB".format(cacheSize/1024f/1024f)
}

private fun getDeviceString(): String {
val device = Build.DEVICE // Device
val model = Build.MODEL // Model
Expand Down

0 comments on commit d7194db

Please sign in to comment.