Skip to content

Commit

Permalink
[fix]: logs selection and ANRs
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Jul 7, 2024
1 parent fc558d2 commit fd60c2a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.f0x1d.logfox.feature.logging.core.store

import com.f0x1d.logfox.arch.di.DefaultDispatcher
import com.f0x1d.logfox.model.logline.LogLine
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -12,17 +15,19 @@ import javax.inject.Singleton
interface LoggingStore {
val logs: Flow<List<LogLine>>

fun updateLogs(logs: List<LogLine>)
suspend fun updateLogs(logs: List<LogLine>)
}

@Singleton
internal class LoggingStoreImpl @Inject constructor() : LoggingStore {
internal class LoggingStoreImpl @Inject constructor(
@DefaultDispatcher private val defaultDispatcher: CoroutineDispatcher,
) : LoggingStore {

private val mutableLogs = MutableStateFlow(emptyList<LogLine>())

override val logs: Flow<List<LogLine>> = mutableLogs

override fun updateLogs(logs: List<LogLine>) {
override suspend fun updateLogs(logs: List<LogLine>) = withContext(defaultDispatcher) {
mutableLogs.update { logs.toMutableList() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LogsAdapter(
): BaseListAdapter<LogLine, ItemLogBinding>(diffCallback<LogLine>()) {

val expandedStates = mutableMapOf<Long, Boolean>()
var selectedItems = emptyList<LogLine>()
var selectedItems = emptySet<LogLine>()
set(value) {
field = value
notifyItemRangeChanged(0, itemCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import com.f0x1d.logfox.arch.di.DefaultDispatcher
import com.f0x1d.logfox.context.LOGGING_STATUS_CHANNEL_ID
import com.f0x1d.logfox.context.activityManager
import com.f0x1d.logfox.context.toast
Expand All @@ -28,6 +29,7 @@ import com.f0x1d.logfox.terminals.DefaultTerminal
import com.f0x1d.logfox.terminals.base.Terminal
import com.f0x1d.logfox.ui.Icons
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Job
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.cancelAndJoin
Expand Down Expand Up @@ -79,6 +81,10 @@ class LoggingService : LifecycleService() {
@Inject
lateinit var mainActivityPendingIntentProvider: MainActivityPendingIntentProvider

@Inject
@DefaultDispatcher
lateinit var defaultDispatcher: CoroutineDispatcher

private val logs = LinkedList<LogLine>()
private val logsMutex = Mutex()
private var loggingJob: Job? = null
Expand Down Expand Up @@ -121,6 +127,16 @@ class LoggingService : LifecycleService() {

loggingJob = lifecycleScope.launch {
try {
launch {
while (true) {
delay(appPreferences.logsUpdateInterval)

logsMutex.withLock {
loggingStore.updateLogs(logs)
}
}
}

while (true) {
loggingRepository.startLogging(
terminal = loggingTerminal,
Expand All @@ -138,23 +154,25 @@ class LoggingService : LifecycleService() {
} else {
toast(getString(Strings.error, throwable.localizedMessage))
throwable.printStackTrace()
}
}.collect { logLine ->
logsMutex.withLock {
logs.add(logLine)

while (logs.size > appPreferences.logsDisplayLimit)
logs.removeFirst()
delay(10000) // waiting for 10sec before new attempt
}
}.collect { logLine ->
withContext(defaultDispatcher) {
logsMutex.withLock {
logs.add(logLine)

loggingStore.updateLogs(logs)
while (logs.size > appPreferences.logsDisplayLimit)
logs.removeFirst()
}

crashesController.readers.forEach {
it(logLine)
}
crashesController.readers.forEach {
it(logLine)
}

if (logLine.suits(filtersState.value)) {
recordingController.reader(logLine)
if (logLine.suits(filtersState.value)) {
recordingController.reader(logLine)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class LogsFragment: BaseViewModelFragment<LogsViewModel, FragmentLogsBinding>()

clearSelectionOnBackPressedCallback.isEnabled = selecting

//adapter.selectedItems = it
adapter.selectedItems = it
setupToolbarForSelection(selecting, it.size)
}

Expand Down Expand Up @@ -247,9 +247,9 @@ class LogsFragment: BaseViewModelFragment<LogsViewModel, FragmentLogsBinding>()
setupCloseButton()

setNavigationOnClickListener {
/*viewModel.selectedItems.update {
emptyList()
}*/
viewModel.selectedItems.update {
emptySet()
}
}
} else if (viewModel.viewingFile)
setupBackButtonForNavController()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SetupViewModel @Inject constructor(
application = application,
) {

val adbCommand get() = "adb shell ${command.joinToString(" ")}"
private val adbCommand get() = "adb shell ${command.joinToString(" ")}"
private val command get() = arrayOf("pm", "grant", ctx.packageName, Manifest.permission.READ_LOGS)

fun root() = launchCatching {
Expand Down

0 comments on commit fd60c2a

Please sign in to comment.