-
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 (#521)
* refactor: Migrate Internal messages package classes to kotlin * add support for kotlin classes * address review comments * Refactor code to use functional style
- Loading branch information
1 parent
6a9204c
commit 0a4d64b
Showing
6 changed files
with
251 additions
and
240 deletions.
There are no files selected for viewing
92 changes: 0 additions & 92 deletions
92
android-core/src/main/java/com/mparticle/internal/messages/BaseMPMessage.java
This file was deleted.
Oops, something went wrong.
144 changes: 0 additions & 144 deletions
144
android-core/src/main/java/com/mparticle/internal/messages/BaseMPMessageBuilder.java
This file was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
android-core/src/main/kotlin/com/mparticle/internal/messages/BaseMPMessage.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,75 @@ | ||
package com.mparticle.internal.messages | ||
|
||
import android.location.Location | ||
import com.mparticle.internal.Constants | ||
import com.mparticle.internal.InternalSession | ||
import com.mparticle.internal.MPUtility | ||
import org.json.JSONException | ||
import org.json.JSONObject | ||
|
||
open class BaseMPMessage : JSONObject { | ||
var mpId: Long = 0 | ||
private set | ||
|
||
protected constructor() | ||
|
||
constructor(builder: BaseMPMessageBuilder, session: InternalSession, location: Location?, mpId: Long) : super(builder, builder.keys) { | ||
this.mpId = mpId | ||
if (!has(Constants.MessageKey.TIMESTAMP)) { | ||
put(Constants.MessageKey.TIMESTAMP, session.mLastEventTime) | ||
} | ||
if (Constants.MessageType.SESSION_START == builder.messageType) { | ||
put(Constants.MessageKey.ID, session.mSessionID) | ||
} else { | ||
put(Constants.MessageKey.SESSION_ID, session.mSessionID) | ||
|
||
if (session.mSessionStartTime > 0) { | ||
put(Constants.MessageKey.SESSION_START_TIMESTAMP, session.mSessionStartTime) | ||
} | ||
} | ||
|
||
if (!(Constants.MessageType.ERROR == builder.messageType && | ||
Constants.MessageType.OPT_OUT != builder.messageType) | ||
) { | ||
if (location != null) { | ||
val locJSON = JSONObject() | ||
locJSON.put(Constants.MessageKey.LATITUDE, location.latitude) | ||
locJSON.put(Constants.MessageKey.LONGITUDE, location.longitude) | ||
locJSON.put(Constants.MessageKey.ACCURACY, location.accuracy.toDouble()) | ||
put(Constants.MessageKey.LOCATION, locJSON) | ||
} | ||
} | ||
} | ||
|
||
val attributes: JSONObject? | ||
get() = optJSONObject(Constants.MessageKey.ATTRIBUTES) | ||
|
||
val timestamp: Long | ||
get() { | ||
try { | ||
return getLong(Constants.MessageKey.TIMESTAMP) | ||
} catch (_: JSONException) { | ||
} | ||
return 0 | ||
} | ||
|
||
val sessionId: String | ||
get() = | ||
if (Constants.MessageType.SESSION_START == messageType) { | ||
optString(Constants.MessageKey.ID, Constants.NO_SESSION_ID) | ||
} else { | ||
optString(Constants.MessageKey.SESSION_ID, Constants.NO_SESSION_ID) | ||
} | ||
|
||
val messageType: String | ||
get() = optString(Constants.MessageKey.TYPE) | ||
|
||
val typeNameHash: Int | ||
get() = MPUtility.mpHash(messageType + name) | ||
|
||
|
||
val name: String | ||
get() = optString(Constants.MessageKey.NAME) | ||
|
||
class Builder(messageType: String) : BaseMPMessageBuilder(messageType) | ||
} |
Oops, something went wrong.