Skip to content

Commit 063035c

Browse files
committed
Merge branch 'feature/update_message_collector'
2 parents a6cc415 + 0d9a7f9 commit 063035c

File tree

3 files changed

+53
-105
lines changed

3 files changed

+53
-105
lines changed

tracker/src/main/java/com/openreplay/tracker/ORTracker.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ object OpenReplay {
200200
}
201201

202202
fun stop() {
203-
ScreenshotManager.stopCapturing()
203+
ScreenshotManager.stop()
204204
Analytics.stop()
205205
LogsListener.stop()
206206
PerformanceListener.getInstance(appContext!!).stop()

tracker/src/main/java/com/openreplay/tracker/managers/MessageCollector.kt

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,11 @@ import java.util.concurrent.Executors
1313
import java.util.concurrent.ScheduledFuture
1414
import java.util.concurrent.TimeUnit
1515

16-
data class BatchArch(
17-
var name: String,
18-
var data: ByteArray
19-
) {
20-
override fun equals(other: Any?): Boolean {
21-
if (this === other) return true
22-
if (javaClass != other?.javaClass) return false
23-
24-
other as BatchArch
25-
26-
if (name != other.name) return false
27-
if (!data.contentEquals(other.data)) return false
28-
29-
return true
30-
}
31-
32-
override fun hashCode(): Int {
33-
var result = name.hashCode()
34-
result = 31 * result + data.contentHashCode()
35-
return result
36-
}
37-
}
38-
3916
object MessageCollector {
40-
private val filesWaiting = mutableListOf<File>()
41-
private val filesSending = mutableListOf<File>()
4217
private var messagesWaiting = mutableListOf<ByteArray>()
4318
private val messagesWaitingBackup = mutableListOf<ByteArray>()
4419
private var nextMessageIndex = 0
4520
private var sendingLastMessages = false
46-
private var sendingLastImages = false
4721
private val maxMessagesSize = 500_000
4822
private var lateMessagesFile: File? = null
4923
private var sendInterval: Handler? = null
@@ -72,11 +46,9 @@ object MessageCollector {
7246

7347
fun start() {
7448
this.lateMessagesFile = OpenReplay.getLateMessagesFile()
75-
7649
sendIntervalFuture = executorService.scheduleWithFixedDelay({
7750
executorService.execute {
7851
flushMessages()
79-
flushImages()
8052
}
8153
}, 0, 5, TimeUnit.SECONDS)
8254

@@ -143,34 +115,6 @@ object MessageCollector {
143115
}
144116
}
145117

146-
private fun flushImages() {
147-
synchronized(filesWaiting) {
148-
if (filesWaiting.isNotEmpty()) {
149-
val fileArchive = filesWaiting.removeAt(0)
150-
filesSending.add(fileArchive)
151-
152-
DebugUtils.log("Sending images in archive ${fileArchive.name} }")
153-
154-
NetworkManager.sendImages(
155-
projectKey = OpenReplay.projectKey!!,
156-
images = fileArchive.readBytes(),
157-
name = fileArchive.name
158-
) { success ->
159-
filesSending.removeAll { it.name == fileArchive.name }
160-
fileArchive.delete()
161-
if (!success) {
162-
filesWaiting.add(
163-
0,
164-
fileArchive
165-
) // Re-add to the start of the queue if not successful
166-
} else if (sendingLastImages) {
167-
sendingLastImages = false
168-
}
169-
}
170-
}
171-
}
172-
}
173-
174118
fun sendMessage(message: ORMessage) {
175119
if (OpenReplay.bufferingMode) {
176120
ConditionsManager.processMessage(message)?.let { trigger ->
@@ -265,22 +209,9 @@ object MessageCollector {
265209

266210
private fun terminate() {
267211
if (sendingLastMessages) return
268-
269212
executorService.execute {
270213
sendingLastMessages = true
271214
flushMessages()
272215
}
273-
274-
if (sendingLastImages) return
275-
276-
executorService.execute {
277-
sendingLastImages = true
278-
flushImages()
279-
}
280-
}
281-
282-
fun sendImagesBatch(archive: File) {
283-
filesWaiting.add(archive)
284-
executorService.execute { flushImages() }
285216
}
286217
}

tracker/src/main/java/com/openreplay/tracker/managers/ScreenshotManager.kt

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ import androidx.compose.ui.platform.AbstractComposeView
1919
import androidx.compose.ui.platform.ComposeView
2020
import com.openreplay.tracker.OpenReplay
2121
import com.openreplay.tracker.SanitizableViewGroup
22-
import kotlinx.coroutines.CoroutineScope
2322
import kotlinx.coroutines.DelicateCoroutinesApi
2423
import kotlinx.coroutines.Dispatchers
2524
import kotlinx.coroutines.GlobalScope
2625
import kotlinx.coroutines.Job
27-
import kotlinx.coroutines.cancelAndJoin
26+
import kotlinx.coroutines.asCoroutineDispatcher
2827
import kotlinx.coroutines.coroutineScope
2928
import kotlinx.coroutines.delay
3029
import kotlinx.coroutines.launch
30+
import kotlinx.coroutines.runBlocking
3131
import kotlinx.coroutines.withContext
3232
import org.apache.commons.compress.archivers.tar.TarArchiveEntry
3333
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream
@@ -37,6 +37,7 @@ import java.io.ByteArrayOutputStream
3737
import java.io.File
3838
import java.io.FileOutputStream
3939
import java.lang.ref.WeakReference
40+
import java.util.concurrent.Executors
4041
import java.util.zip.GZIPOutputStream
4142
import kotlin.coroutines.suspendCoroutine
4243

@@ -51,6 +52,7 @@ object ScreenshotManager {
5152
private var quality: Int = 10
5253
private var minResolution: Int = 320
5354

55+
5456
private var isStoping = false
5557
fun setSettings(settings: Triple<Int, Int, Int>) {
5658
val (_, quality, resolution) = settings
@@ -59,42 +61,36 @@ object ScreenshotManager {
5961
}
6062

6163
@OptIn(DelicateCoroutinesApi::class)
62-
fun start(context: Context, startTs: Long, scope: CoroutineScope = GlobalScope) {
64+
fun start(context: Context, startTs: Long) {
6365
uiContext = WeakReference(context)
6466
firstTs = startTs
6567
isStoping = false
6668
// endless job to perform capturing
67-
screenShotJob = scope.launch {
69+
70+
screenShotJob = GlobalScope.launch {
6871
val intervalMillis =
6972
OpenReplay.options.screenshotFrequency.millis / OpenReplay.options.fps.toLong()
7073
while (true) {
71-
if (isStoping) {
72-
if (screenshots.isNotEmpty()) {
73-
sendScreenshots(chunkSize = screenshots.size)
74-
}
75-
screenShotJob?.cancelAndJoin()
76-
} else {
77-
delay(intervalMillis)
78-
val screenShot = withContext(Dispatchers.Main) { captureScreenshot() }
79-
withContext(Dispatchers.IO) {
80-
try {
81-
val byteScreenShot = compress(screenShot)
82-
// add screen shot to storage
83-
addToScreenShots(byteScreenShot)
84-
// while we are doing our job we gather and send data
85-
sendScreenshots(chunkSize = 10)
86-
} catch (e: Exception) {
87-
e.printStackTrace()
88-
}
74+
delay(intervalMillis)
75+
val screenShot = withContext(Dispatchers.Main) { captureScreenshot() }
76+
withContext(Dispatchers.IO) {
77+
try {
78+
val byteScreenShot = compress(screenShot)
79+
// add screen shot to storage
80+
addToScreenShots(byteScreenShot)
81+
// while we are doing our job we gather and send data
82+
sendScreenshots(chunkSize = 10)
83+
} catch (e: Exception) {
84+
e.printStackTrace()
8985
}
9086
}
9187
}
9288
}
9389
}
9490

95-
96-
fun stopCapturing() {
97-
isStoping = true
91+
fun stop() {
92+
screenShotJob?.cancel()
93+
terminate()
9894
}
9995

10096
fun addSanitizedElement(view: View) {
@@ -112,7 +108,8 @@ object ScreenshotManager {
112108
}
113109

114110
private suspend fun captureScreenshot(): Bitmap {
115-
val activity = uiContext.get() as? Activity ?: throw IllegalStateException("No Activity")
111+
val activity =
112+
uiContext.get() as? Activity ?: throw IllegalStateException("No Activity")
116113
return suspendCoroutine { coroutine ->
117114
activity.screenShot { shot ->
118115
coroutine.resumeWith(Result.success(shot))
@@ -253,7 +250,8 @@ object ScreenshotManager {
253250

254251
private fun createCrossStripedPatternBitmap(): Bitmap {
255252
val patternSize = 80
256-
val patternBitmap = Bitmap.createBitmap(patternSize, patternSize, Bitmap.Config.ARGB_8888)
253+
val patternBitmap =
254+
Bitmap.createBitmap(patternSize, patternSize, Bitmap.Config.ARGB_8888)
257255
val patternCanvas = Canvas(patternBitmap)
258256
val paint = Paint().apply {
259257
color = Color.DKGRAY
@@ -303,7 +301,8 @@ object ScreenshotManager {
303301
val aspectRatio = originalBitmap.height.toFloat() / originalBitmap.width.toFloat()
304302
val newHeight = (minResolution * aspectRatio).toInt()
305303

306-
val updated = Bitmap.createScaledBitmap(originalBitmap, minResolution, newHeight, true)
304+
val updated =
305+
Bitmap.createScaledBitmap(originalBitmap, minResolution, newHeight, true)
307306

308307
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
309308
updated.compress(Bitmap.CompressFormat.WEBP_LOSSLESS, quality, outputStream)
@@ -316,10 +315,13 @@ object ScreenshotManager {
316315

317316
private suspend fun sendScreenshots(chunkSize: Int) {
318317
coroutineScope {
319-
val sessionId = NetworkManager.sessionId ?: throw IllegalStateException("No session")
318+
val sessionId =
319+
NetworkManager.sessionId ?: throw IllegalStateException("No session")
320320
if (screenshots.size < chunkSize) {
321-
DebugUtils.log("buffering screenshots")
321+
DebugUtils.log("buffering screenshots ${screenshots.size}")
322322
} else {
323+
DebugUtils.log("archiving screenshots ${screenshots.size}")
324+
323325
val archiveName = "$sessionId-$lastTs.tar.gz"
324326
// prepare data
325327
val entries = screenshots.map { (imageFile, timestamp) ->
@@ -349,11 +351,12 @@ object ScreenshotManager {
349351
bytes = combinedData.toByteArray(),
350352
filename = archiveName
351353
)
352-
353-
try {
354-
MessageCollector.sendImagesBatch(archive)
355-
} catch (e: Exception) {
356-
e.printStackTrace()
354+
NetworkManager.sendImages(
355+
projectKey = OpenReplay.projectKey!!,
356+
images = archive.readBytes(),
357+
name = archive.name
358+
) { success ->
359+
archive.delete()
357360
}
358361
}
359362
}
@@ -376,4 +379,18 @@ object ScreenshotManager {
376379
}
377380
return file
378381
}
382+
383+
private fun terminate() {
384+
if (screenshots.isNotEmpty()) {
385+
Executors.newSingleThreadExecutor()
386+
.asCoroutineDispatcher()
387+
.use { dispatcher ->
388+
runBlocking {
389+
launch(dispatcher) {
390+
sendScreenshots(chunkSize = screenshots.size)
391+
}
392+
}
393+
}
394+
}
395+
}
379396
}

0 commit comments

Comments
 (0)