Skip to content

Commit

Permalink
refactor: Migrate Internal BaseHandler class to kotlin (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansi-mParticle authored Dec 12, 2024
1 parent b0c0fdb commit 35860b9
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 101 deletions.
100 changes: 0 additions & 100 deletions android-core/src/main/java/com/mparticle/internal/BaseHandler.java

This file was deleted.

90 changes: 90 additions & 0 deletions android-core/src/main/java/com/mparticle/internal/BaseHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.mparticle.internal

import android.os.Handler
import android.os.Looper
import android.os.Message
import com.mparticle.internal.listeners.InternalListenerManager.Companion.isEnabled
import com.mparticle.internal.listeners.InternalListenerManager.Companion.listener
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CountDownLatch

open class BaseHandler : Handler {
@Volatile
var isDisabled: Boolean = false
private set

@Volatile
private var handling = false
private val messageQueue: ConcurrentHashMap<Message, Boolean> = ConcurrentHashMap<Message, Boolean>()

fun getMessageQueue(): Set<Message> {
return messageQueue.keys
}

constructor()

constructor(looper: Looper) : super(looper)

fun disable(disable: Boolean) {
this.isDisabled = disable
removeCallbacksAndMessages(null)
while (handling) {
}
}

fun await(latch: CountDownLatch?) {
this.sendMessage(obtainMessage(-1, latch))
}

override fun handleMessage(msg: Message) {
if (isDisabled) {
Logger.error("Handler: " + javaClass.name + " is destroyed! Message: \"" + msg.toString() + "\" will not be processed")
return
}
handling = true
try {
messageQueue.remove(msg)
if (msg.what == -1 && msg.obj is CountDownLatch) {
(msg.obj as CountDownLatch).countDown()
} else {
if (isEnabled) {
listener.onThreadMessage(javaClass.name, msg, true)
}
try {
handleMessageImpl(msg)
} catch (error: OutOfMemoryError) {
Logger.error("Out of memory")
}
}
} finally {
handling = false
}
}

override fun sendMessageAtTime(msg: Message, uptimeMillis: Long): Boolean {
if (isDisabled) {
return false
}
if (isEnabled) {
listener.onThreadMessage(javaClass.name, msg, false)
}

messageQueue[msg] = true

return super.sendMessageAtTime(msg, uptimeMillis)
}

fun removeMessage(what: Int) {
val messages: Set<Message> = messageQueue.keys
for (message in messages) {
if (message.what == what) {
messageQueue.remove(message)
}
}
super.removeMessages(what)
}

// Override this in order to handle messages
open fun handleMessageImpl(msg: Message?) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.mockito.stubbing.Answer
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
Expand Down Expand Up @@ -114,7 +115,15 @@ class UploadHandlerTest {
@Test
@Throws(Exception::class)
fun testRampSampling() {
handler.handleMessage(Message())
val message = Mockito.mock(Message::class.java)

Mockito.`when`(message.toString()).thenAnswer(
Answer {
"Mocked Message"
}
)

handler.handleMessage(message)
val apiClient = Mockito.mock(
MParticleApiClientImpl::class.java
)
Expand Down

0 comments on commit 35860b9

Please sign in to comment.