Skip to content

Commit

Permalink
Add switching to main thread, if we're on another thread
Browse files Browse the repository at this point in the history
  • Loading branch information
DeKaN committed Apr 26, 2024
1 parent 2e35a4b commit d2cb874
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ internal class SessionStorage(
internal inline fun getCurrentTimestamp() = System.currentTimeMillis()

// for mocking in tests
internal fun addLifecycleObserver(observer: DefaultLifecycleObserver) =
internal fun addLifecycleObserver(observer: DefaultLifecycleObserver) = runOnMainThread {
ProcessLifecycleOwner.get().lifecycle.addObserver(observer)
}
}
13 changes: 13 additions & 0 deletions piano-analytics/src/main/java/io/piano/android/analytics/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.piano.android.analytics

import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import timber.log.Timber

internal fun String.wildcardMatches(string: String): Boolean {
Expand All @@ -21,3 +23,14 @@ private fun getProperty(key: String): String? = runCatching {
}.getOrNull()

private const val LOG_HTTP_KEY = "debug.piano.sdk"

internal fun runOnMainThread(action: () -> Unit) {
val mainLooper = Looper.getMainLooper()
if (mainLooper.thread == Thread.currentThread()) {
action()
} else {
Handler(mainLooper).post {
action()
}
}
}

0 comments on commit d2cb874

Please sign in to comment.