Skip to content

Commit

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

This file was deleted.

93 changes: 93 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,93 @@
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) {
message?.let {
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
@@ -1,6 +1,9 @@
package com.mparticle.internal

import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import com.mparticle.MParticle
import com.mparticle.MockMParticle
Expand All @@ -24,6 +27,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 +118,13 @@ 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 f6cb98f

Please sign in to comment.