-
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 messages package classes to kotlin Part 2 (#…
…522)
- Loading branch information
1 parent
0a4d64b
commit 24d6a81
Showing
4 changed files
with
88 additions
and
117 deletions.
There are no files selected for viewing
78 changes: 0 additions & 78 deletions
78
android-core/src/main/java/com/mparticle/internal/messages/MPAliasMessage.java
This file was deleted.
Oops, something went wrong.
39 changes: 0 additions & 39 deletions
39
android-core/src/main/java/com/mparticle/internal/messages/MPEventMessage.java
This file was deleted.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
android-core/src/main/kotlin/com/mparticle/internal/messages/MPAliasMessage.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,62 @@ | ||
package com.mparticle.internal.messages | ||
|
||
import com.mparticle.MParticle | ||
import com.mparticle.identity.AliasRequest | ||
import com.mparticle.internal.ConfigManager | ||
import com.mparticle.internal.Constants.MessageKey | ||
import org.json.JSONException | ||
import org.json.JSONObject | ||
import java.util.UUID | ||
|
||
class MPAliasMessage : JSONObject { | ||
constructor(jsonString: String) : super(jsonString) | ||
|
||
@Throws(JSONException::class) | ||
constructor(request: AliasRequest, deviceApplicationStamp: String, apiKey: String) { | ||
val environment = getStringValue(ConfigManager.getEnvironment()) | ||
val requestId = UUID.randomUUID().toString() | ||
|
||
val dataJson = JSONObject() | ||
.put(MessageKey.SOURCE_MPID, request.sourceMpid) | ||
.put(MessageKey.DESTINATION_MPID, request.destinationMpid) | ||
.put(MessageKey.DEVICE_APPLICATION_STAMP_ALIAS, deviceApplicationStamp) | ||
|
||
if (request.startTime != 0L) { | ||
dataJson.put(MessageKey.START_TIME, request.startTime) | ||
} | ||
if (request.endTime != 0L) { | ||
dataJson.put(MessageKey.END_TIME, request.endTime) | ||
} | ||
|
||
put(MessageKey.DATA, dataJson) | ||
put(MessageKey.REQUEST_TYPE, MessageKey.ALIAS_REQUEST_TYPE) | ||
put(MessageKey.REQUEST_ID, requestId) | ||
put(MessageKey.ENVIRONMENT_ALIAS, environment) | ||
put(MessageKey.API_KEY, apiKey) | ||
} | ||
|
||
@get:Throws(JSONException::class) | ||
val aliasRequest: AliasRequest | ||
get() { | ||
val data = getJSONObject(MessageKey.DATA) | ||
return AliasRequest.builder() | ||
.destinationMpid(data.getLong(MessageKey.DESTINATION_MPID)) | ||
.sourceMpid(data.getLong(MessageKey.SOURCE_MPID)) | ||
.endTime(data.optLong(MessageKey.END_TIME, 0)) | ||
.startTime(data.optLong(MessageKey.START_TIME, 0)) | ||
.build() | ||
} | ||
|
||
@get:Throws(JSONException::class) | ||
val requestId: String | ||
get() = getString(MessageKey.REQUEST_ID) | ||
|
||
|
||
protected fun getStringValue(environment: MParticle.Environment): String { | ||
return when (environment) { | ||
MParticle.Environment.Development -> "development" | ||
MParticle.Environment.Production -> "production" | ||
else -> "" | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
android-core/src/main/kotlin/com/mparticle/internal/messages/MPEventMessage.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,26 @@ | ||
package com.mparticle.internal.messages | ||
|
||
import android.location.Location | ||
import com.mparticle.MParticle | ||
import com.mparticle.internal.Constants | ||
import com.mparticle.internal.InternalSession | ||
import org.json.JSONException | ||
|
||
class MPEventMessage protected constructor(builder: Builder, session: InternalSession, location: Location?, mpId: Long) : | ||
BaseMPMessage(builder, session, location, mpId) { | ||
class Builder(messageType: String) : BaseMPMessageBuilder(messageType) { | ||
fun customEventType(eventType: MParticle.EventType): BaseMPMessageBuilder { | ||
try { | ||
put(Constants.MessageKey.EVENT_TYPE, eventType) | ||
} catch (e: JSONException) { | ||
e.printStackTrace() | ||
} | ||
return this | ||
} | ||
|
||
@Throws(JSONException::class) | ||
override fun build(session: InternalSession, location: Location?, mpId: Long): BaseMPMessage { | ||
return MPEventMessage(this, session, location, mpId) | ||
} | ||
} | ||
} |