From 24d6a815d1dff75c68b54bebbbc1129e4b920e89 Mon Sep 17 00:00:00 2001 From: Mansi-mParticle <159845845+Mansi-mParticle@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:46:13 -0500 Subject: [PATCH] refactor: Migrate Internal messages package classes to kotlin Part 2 (#522) --- .../internal/messages/MPAliasMessage.java | 78 ------------------- .../internal/messages/MPEventMessage.java | 39 ---------- .../internal/messages/MPAliasMessage.kt | 62 +++++++++++++++ .../internal/messages/MPEventMessage.kt | 26 +++++++ 4 files changed, 88 insertions(+), 117 deletions(-) delete mode 100644 android-core/src/main/java/com/mparticle/internal/messages/MPAliasMessage.java delete mode 100644 android-core/src/main/java/com/mparticle/internal/messages/MPEventMessage.java create mode 100644 android-core/src/main/kotlin/com/mparticle/internal/messages/MPAliasMessage.kt create mode 100644 android-core/src/main/kotlin/com/mparticle/internal/messages/MPEventMessage.kt diff --git a/android-core/src/main/java/com/mparticle/internal/messages/MPAliasMessage.java b/android-core/src/main/java/com/mparticle/internal/messages/MPAliasMessage.java deleted file mode 100644 index 1c5cc3b20..000000000 --- a/android-core/src/main/java/com/mparticle/internal/messages/MPAliasMessage.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.mparticle.internal.messages; - -import static com.mparticle.internal.Constants.MessageKey.ALIAS_REQUEST_TYPE; -import static com.mparticle.internal.Constants.MessageKey.API_KEY; -import static com.mparticle.internal.Constants.MessageKey.DATA; -import static com.mparticle.internal.Constants.MessageKey.DESTINATION_MPID; -import static com.mparticle.internal.Constants.MessageKey.DEVICE_APPLICATION_STAMP_ALIAS; -import static com.mparticle.internal.Constants.MessageKey.END_TIME; -import static com.mparticle.internal.Constants.MessageKey.ENVIRONMENT_ALIAS; -import static com.mparticle.internal.Constants.MessageKey.REQUEST_ID; -import static com.mparticle.internal.Constants.MessageKey.REQUEST_TYPE; -import static com.mparticle.internal.Constants.MessageKey.SOURCE_MPID; -import static com.mparticle.internal.Constants.MessageKey.START_TIME; - -import com.mparticle.MParticle; -import com.mparticle.identity.AliasRequest; -import com.mparticle.internal.ConfigManager; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.UUID; - -public class MPAliasMessage extends JSONObject { - - public MPAliasMessage(String jsonString) throws JSONException { - super(jsonString); - } - - public MPAliasMessage(AliasRequest request, String deviceApplicationStamp, String apiKey) throws JSONException { - String environment = getStringValue(ConfigManager.getEnvironment()); - String requestId = UUID.randomUUID().toString(); - - JSONObject dataJson = new JSONObject() - .put(SOURCE_MPID, request.getSourceMpid()) - .put(DESTINATION_MPID, request.getDestinationMpid()) - .put(DEVICE_APPLICATION_STAMP_ALIAS, deviceApplicationStamp); - - if (request.getStartTime() != 0) { - dataJson.put(START_TIME, request.getStartTime()); - } - if (request.getEndTime() != 0) { - dataJson.put(END_TIME, request.getEndTime()); - } - - put(DATA, dataJson); - put(REQUEST_TYPE, ALIAS_REQUEST_TYPE); - put(REQUEST_ID, requestId); - put(ENVIRONMENT_ALIAS, environment); - put(API_KEY, apiKey); - } - - public AliasRequest getAliasRequest() throws JSONException { - JSONObject data = getJSONObject(DATA); - return AliasRequest.builder() - .destinationMpid(data.getLong(DESTINATION_MPID)) - .sourceMpid(data.getLong(SOURCE_MPID)) - .endTime(data.optLong(END_TIME, 0)) - .startTime(data.optLong(START_TIME, 0)) - .build(); - } - - public String getRequestId() throws JSONException { - return getString(REQUEST_ID); - } - - - protected String getStringValue(MParticle.Environment environment) { - switch (environment) { - case Development: - return "development"; - case Production: - return "production"; - default: - return ""; - } - } -} \ No newline at end of file diff --git a/android-core/src/main/java/com/mparticle/internal/messages/MPEventMessage.java b/android-core/src/main/java/com/mparticle/internal/messages/MPEventMessage.java deleted file mode 100644 index 77dd81c98..000000000 --- a/android-core/src/main/java/com/mparticle/internal/messages/MPEventMessage.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.mparticle.internal.messages; - -import android.location.Location; - -import androidx.annotation.Nullable; - -import com.mparticle.MParticle; -import com.mparticle.internal.Constants; -import com.mparticle.internal.InternalSession; - -import org.json.JSONException; - -public class MPEventMessage extends BaseMPMessage { - - protected MPEventMessage(Builder builder, InternalSession session, @Nullable Location location, long mpId) throws JSONException { - super(builder, session, location, mpId); - } - - public static class Builder extends BaseMPMessageBuilder { - - public Builder(String messageType) { - super(messageType); - } - - public BaseMPMessageBuilder customEventType(MParticle.EventType eventType) { - try { - put(Constants.MessageKey.EVENT_TYPE, eventType); - } catch (JSONException e) { - e.printStackTrace(); - } - return this; - } - - @Override - public BaseMPMessage build(InternalSession session, Location location, long mpId) throws JSONException { - return new MPEventMessage(this, session, location, mpId); - } - } -} \ No newline at end of file diff --git a/android-core/src/main/kotlin/com/mparticle/internal/messages/MPAliasMessage.kt b/android-core/src/main/kotlin/com/mparticle/internal/messages/MPAliasMessage.kt new file mode 100644 index 000000000..1eb38bc71 --- /dev/null +++ b/android-core/src/main/kotlin/com/mparticle/internal/messages/MPAliasMessage.kt @@ -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 -> "" + } + } +} \ No newline at end of file diff --git a/android-core/src/main/kotlin/com/mparticle/internal/messages/MPEventMessage.kt b/android-core/src/main/kotlin/com/mparticle/internal/messages/MPEventMessage.kt new file mode 100644 index 000000000..47b9d7181 --- /dev/null +++ b/android-core/src/main/kotlin/com/mparticle/internal/messages/MPEventMessage.kt @@ -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) + } + } +} \ No newline at end of file