-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Migrate Internal BaseHandler class to kotlin
- Loading branch information
1 parent
b0c0fdb
commit f6cb98f
Showing
3 changed files
with
104 additions
and
101 deletions.
There are no files selected for viewing
100 changes: 0 additions & 100 deletions
100
android-core/src/main/java/com/mparticle/internal/BaseHandler.java
This file was deleted.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
android-core/src/main/java/com/mparticle/internal/BaseHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters