Skip to content

Commit 3c716dd

Browse files
refactor: Migrate Internal BaseHandler class to kotlin (#527)
1 parent 581687c commit 3c716dd

File tree

3 files changed

+100
-101
lines changed

3 files changed

+100
-101
lines changed

android-core/src/main/java/com/mparticle/internal/BaseHandler.java

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.mparticle.internal
2+
3+
import android.os.Handler
4+
import android.os.Looper
5+
import android.os.Message
6+
import com.mparticle.internal.listeners.InternalListenerManager.Companion.isEnabled
7+
import com.mparticle.internal.listeners.InternalListenerManager.Companion.listener
8+
import java.util.concurrent.ConcurrentHashMap
9+
import java.util.concurrent.CountDownLatch
10+
11+
open class BaseHandler : Handler {
12+
@Volatile
13+
var isDisabled: Boolean = false
14+
private set
15+
16+
@Volatile
17+
private var handling = false
18+
private val messageQueue: ConcurrentHashMap<Message, Boolean> = ConcurrentHashMap<Message, Boolean>()
19+
20+
fun getMessageQueue(): Set<Message> {
21+
return messageQueue.keys
22+
}
23+
24+
constructor()
25+
26+
constructor(looper: Looper) : super(looper)
27+
28+
fun disable(disable: Boolean) {
29+
this.isDisabled = disable
30+
removeCallbacksAndMessages(null)
31+
while (handling) {
32+
}
33+
}
34+
35+
fun await(latch: CountDownLatch?) {
36+
this.sendMessage(obtainMessage(-1, latch))
37+
}
38+
39+
override fun handleMessage(msg: Message) {
40+
if (isDisabled) {
41+
Logger.error("Handler: " + javaClass.name + " is destroyed! Message: \"" + msg.toString() + "\" will not be processed")
42+
return
43+
}
44+
handling = true
45+
try {
46+
messageQueue.remove(msg)
47+
if (msg.what == -1 && msg.obj is CountDownLatch) {
48+
(msg.obj as CountDownLatch).countDown()
49+
} else {
50+
if (isEnabled) {
51+
listener.onThreadMessage(javaClass.name, msg, true)
52+
}
53+
try {
54+
handleMessageImpl(msg)
55+
} catch (error: OutOfMemoryError) {
56+
Logger.error("Out of memory")
57+
}
58+
}
59+
} finally {
60+
handling = false
61+
}
62+
}
63+
64+
override fun sendMessageAtTime(msg: Message, uptimeMillis: Long): Boolean {
65+
if (isDisabled) {
66+
return false
67+
}
68+
if (isEnabled) {
69+
listener.onThreadMessage(javaClass.name, msg, false)
70+
}
71+
72+
messageQueue[msg] = true
73+
74+
return super.sendMessageAtTime(msg, uptimeMillis)
75+
}
76+
77+
fun removeMessage(what: Int) {
78+
val messages: Set<Message> = messageQueue.keys
79+
for (message in messages) {
80+
if (message.what == what) {
81+
messageQueue.remove(message)
82+
}
83+
}
84+
super.removeMessages(what)
85+
}
86+
87+
// Override this in order to handle messages
88+
open fun handleMessageImpl(msg: Message?) {
89+
}
90+
}

android-core/src/test/kotlin/com/mparticle/internal/UploadHandlerTest.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.junit.Before
2424
import org.junit.Test
2525
import org.junit.runner.RunWith
2626
import org.mockito.Mockito
27+
import org.mockito.stubbing.Answer
2728
import org.powermock.api.mockito.PowerMockito
2829
import org.powermock.core.classloader.annotations.PrepareForTest
2930
import org.powermock.modules.junit4.PowerMockRunner
@@ -114,7 +115,15 @@ class UploadHandlerTest {
114115
@Test
115116
@Throws(Exception::class)
116117
fun testRampSampling() {
117-
handler.handleMessage(Message())
118+
val message = Mockito.mock(Message::class.java)
119+
120+
Mockito.`when`(message.toString()).thenAnswer(
121+
Answer {
122+
"Mocked Message"
123+
}
124+
)
125+
126+
handler.handleMessage(message)
118127
val apiClient = Mockito.mock(
119128
MParticleApiClientImpl::class.java
120129
)

0 commit comments

Comments
 (0)