Skip to content

Commit

Permalink
Merge pull request #93 from adjust/serializable
Browse files Browse the repository at this point in the history
Serializable
  • Loading branch information
nonelse committed Mar 23, 2015
2 parents be5cc65 + 1d97c50 commit 269dc69
Show file tree
Hide file tree
Showing 26 changed files with 548 additions and 368 deletions.
2 changes: 1 addition & 1 deletion Adjust/adjust/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "4.0.0"
versionName "4.0.1"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.adjust.sdk;

public enum ActivityKind {
UNKNOWN, SESSION, EVENT, CLICK, ATTRIBUTION;
UNKNOWN, SESSION, EVENT, CLICK, ATTRIBUTION, REVENUE, REATTRIBUTION;

public static ActivityKind fromString(String string) {
if ("session".equals(string)) {
Expand Down
81 changes: 64 additions & 17 deletions Adjust/adjust/src/main/java/com/adjust/sdk/ActivityPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@

package com.adjust.sdk;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.Serializable;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class ActivityPackage implements Serializable {
private static final long serialVersionUID = -35935556512024097L;

private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("path", String.class),
new ObjectStreamField("clientSdk", String.class),
new ObjectStreamField("parameters", (Class<Map<String,String>>)((Class)Map.class)),
new ObjectStreamField("activityKind", ActivityKind.class),
new ObjectStreamField("suffix", String.class)
};

private transient int hashCode;

// data
private String path;
private String clientSdk;
Expand Down Expand Up @@ -52,10 +68,6 @@ public ActivityKind getActivityKind() {
return activityKind;
}

public void setActivityKind(ActivityKind activityKind) {
this.activityKind = activityKind;
}

public String getSuffix() {
return suffix;
}
Expand All @@ -64,6 +76,10 @@ public void setSuffix(String suffix) {
this.suffix = suffix;
}

public ActivityPackage(ActivityKind activityKind) {
this.activityKind = activityKind;
}

public String toString() {
return String.format("%s%s", activityKind.toString(), suffix);
}
Expand All @@ -75,26 +91,57 @@ public String getExtendedString() {

if (parameters != null) {
builder.append("Parameters:");
for (Map.Entry<String, String> entry : parameters.entrySet()) {
builder.append(String.format("\n\t%-16s %s", entry.getKey(), entry.getValue()));
SortedMap<String,String> sortedParameters = new TreeMap<String,String>(parameters);
for (Map.Entry<String,String> entry : sortedParameters.entrySet() ) {
builder.append(String.format("\n\t%-16s %s", entry.getKey(), entry.getValue()));
}
}
return builder.toString();
}

protected String getSuccessMessage() {
try {
return String.format("Tracked %s%s", activityKind.toString(), suffix);
} catch (NullPointerException e) {
return "Tracked ???";
}
protected String getFailureMessage() {
return String.format("Failed to track %s%s", (activityKind == null? "unknown":activityKind.toString()), suffix);
}

protected String getFailureMessage() {
try {
return String.format("Failed to track %s%s", activityKind.toString(), suffix);
} catch (NullPointerException e) {
return "Failed to track ???";
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
}

private void readObject(ObjectInputStream stream) throws ClassNotFoundException, IOException {
ObjectInputStream.GetField fields = stream.readFields();

path = Util.readStringField(fields, "path", null);
clientSdk = Util.readStringField(fields, "clientSdk", null);
parameters = Util.readObjectField(fields, "parameters", null);
activityKind = Util.readObjectField(fields, "activityKind", ActivityKind.UNKNOWN);
suffix = Util.readStringField(fields, "suffix", null);
}

@Override
public boolean equals(Object other) {
if (other == this) return true;
if (other == null) return false;
if (getClass() != other.getClass()) return false;
ActivityPackage otherActivityPackage = (ActivityPackage) other;

if (!Util.equalString(path, otherActivityPackage.path)) return false;
if (!Util.equalString(clientSdk, otherActivityPackage.clientSdk)) return false;
if (!Util.equalsMap(parameters, otherActivityPackage.parameters)) return false;
if (!Util.equalEnum(activityKind, otherActivityPackage.activityKind)) return false;
if (!Util.equalString(suffix, otherActivityPackage.suffix)) return false;
return true;
}

@Override
public int hashCode() {
if (hashCode == 0) {
hashCode = 17;
hashCode = 37 * hashCode + Util.hashString(path);
hashCode = 37 * hashCode + Util.hashString(clientSdk);
hashCode = 37 * hashCode + Util.hashMap(parameters);
hashCode = 37 * hashCode + Util.hashEnum(activityKind);
hashCode = 37 * hashCode + Util.hashString(suffix);
}
return hashCode;
}
}
107 changes: 61 additions & 46 deletions Adjust/adjust/src/main/java/com/adjust/sdk/ActivityState.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,27 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectInputStream.GetField;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Locale;

public class ActivityState implements Serializable, Cloneable {
private static final long serialVersionUID = 9039439291143138148L;
private transient String readErrorMessage = "Unable to read '%s' field in migration device with message (%s)";
private transient ILogger logger;
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("uuid", String.class),
new ObjectStreamField("enabled", boolean.class),
new ObjectStreamField("askingAttribution", boolean.class),
new ObjectStreamField("eventCount", int.class),
new ObjectStreamField("sessionCount", int.class),
new ObjectStreamField("subsessionCount", int.class),
new ObjectStreamField("sessionLength", long.class),
new ObjectStreamField("timeSpent", long.class),
new ObjectStreamField("lastActivity", long.class),
new ObjectStreamField("lastInterval", long.class)
};

// persistent data
protected String uuid;
Expand Down Expand Up @@ -80,64 +93,66 @@ public ActivityState clone() {
}
}

@Override
public boolean equals(Object other) {
if (other == this) return true;
if (other == null) return false;
if (getClass() != other.getClass()) return false;
ActivityState otherActivityState = (ActivityState) other;

if (!Util.equalString(uuid, otherActivityState.uuid)) return false;
if (!Util.equalBoolean(enabled, otherActivityState.enabled)) return false;
if (!Util.equalBoolean(askingAttribution, otherActivityState.askingAttribution)) return false;
if (!Util.equalInt(eventCount, otherActivityState.eventCount)) return false;
if (!Util.equalInt(sessionCount, otherActivityState.sessionCount)) return false;
if (!Util.equalInt(subsessionCount, otherActivityState.subsessionCount)) return false;
if (!Util.equalLong(sessionLength, otherActivityState.sessionLength)) return false;
if (!Util.equalLong(timeSpent, otherActivityState.timeSpent)) return false;
if (!Util.equalLong(lastInterval, otherActivityState.lastInterval)) return false;
return true;
}

@Override
public int hashCode() {
int hashCode = 17;
hashCode = 37 * hashCode + Util.hashString(uuid);
hashCode = 37 * hashCode + Util.hashBoolean(enabled);
hashCode = 37 * hashCode + Util.hashBoolean(askingAttribution);
hashCode = 37 * hashCode + eventCount;
hashCode = 37 * hashCode + sessionCount;
hashCode = 37 * hashCode + subsessionCount;
hashCode = 37 * hashCode + Util.hashLong(sessionLength);
hashCode = 37 * hashCode + Util.hashLong(timeSpent);
hashCode = 37 * hashCode + Util.hashLong(lastInterval);
return hashCode;
}


private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
GetField fields = stream.readFields();

eventCount = readIntField(fields, "eventCount", 0);
sessionCount = readIntField(fields, "sessionCount", 0);
subsessionCount = readIntField(fields, "subsessionCount", -1);
sessionLength = readLongField(fields, "sessionLength", -1l);
timeSpent = readLongField(fields, "timeSpent", -1l);
lastActivity = readLongField(fields, "lastActivity", -1l);
lastInterval = readLongField(fields, "lastInterval", -1l);
eventCount = Util.readIntField(fields, "eventCount", 0);
sessionCount = Util.readIntField(fields, "sessionCount", 0);
subsessionCount = Util.readIntField(fields, "subsessionCount", -1);
sessionLength = Util.readLongField(fields, "sessionLength", -1l);
timeSpent = Util.readLongField(fields, "timeSpent", -1l);
lastActivity = Util.readLongField(fields, "lastActivity", -1l);
lastInterval = Util.readLongField(fields, "lastInterval", -1l);

// new fields
uuid = readStringField(fields, "uuid", null);
enabled = readBooleanField(fields, "enabled", true);
askingAttribution = readBooleanField(fields, "askingAttribution", false);
uuid = Util.readStringField(fields, "uuid", null);
enabled = Util.readBooleanField(fields, "enabled", true);
askingAttribution = Util.readBooleanField(fields, "askingAttribution", false);

// create UUID for migrating devices
if (uuid == null) {
uuid = Util.createUuid();
}
}

private String readStringField(GetField fields, String name, String defaultValue) {
try {
return (String) fields.get(name, defaultValue);
} catch (Exception e) {
logger.debug(readErrorMessage, name, e.getMessage());
return defaultValue;
}
}

private boolean readBooleanField(GetField fields, String name, boolean defaultValue) {
try {
return fields.get(name, defaultValue);
} catch (Exception e) {
logger.debug(readErrorMessage, name, e.getMessage());
return defaultValue;
}
}

private int readIntField(GetField fields, String name, int defaultValue) {
try {
return fields.get(name, defaultValue);
} catch (Exception e) {
logger.debug(readErrorMessage, name, e.getMessage());
return defaultValue;
}
}

private long readLongField(GetField fields, String name, long defaultValue) {
try {
return fields.get(name, defaultValue);
} catch (Exception e) {
logger.debug(readErrorMessage, name, e.getMessage());
return defaultValue;
}
}
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
}

private static String stamp(long dateMillis) {
Calendar calendar = Calendar.getInstance();
Expand Down
50 changes: 39 additions & 11 deletions Adjust/adjust/src/main/java/com/adjust/sdk/AdjustAttribution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@

import org.json.JSONObject;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.Serializable;

/**
* Created by pfms on 07/11/14.
*/
public class AdjustAttribution implements Serializable {
private static final long serialVersionUID = 1L;
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("trackerToken", String.class),
new ObjectStreamField("trackerName", String.class),
new ObjectStreamField("network", String.class),
new ObjectStreamField("campaign", String.class),
new ObjectStreamField("adgroup", String.class),
new ObjectStreamField("creative", String.class)
};

public String trackerToken;
public String trackerName;
Expand All @@ -32,31 +44,47 @@ public static AdjustAttribution fromJson(JSONObject jsonObject) {
return attribution;
}

@Override
public boolean equals(Object other) {
if (other == this) return true;
if (other == null) return false;
if (getClass() != other.getClass()) return false;
AdjustAttribution otherAttribution = (AdjustAttribution) other;

if (!equalString(trackerToken, otherAttribution.trackerToken)) return false;
if (!equalString(trackerName, otherAttribution.trackerName)) return false;
if (!equalString(network, otherAttribution.network)) return false;
if (!equalString(campaign, otherAttribution.campaign)) return false;
if (!equalString(adgroup, otherAttribution.adgroup)) return false;
if (!equalString(creative, otherAttribution.creative)) return false;
if (!Util.equalString(trackerToken, otherAttribution.trackerToken)) return false;
if (!Util.equalString(trackerName, otherAttribution.trackerName)) return false;
if (!Util.equalString(network, otherAttribution.network)) return false;
if (!Util.equalString(campaign, otherAttribution.campaign)) return false;
if (!Util.equalString(adgroup, otherAttribution.adgroup)) return false;
if (!Util.equalString(creative, otherAttribution.creative)) return false;
return true;
}

private boolean equalString(String first, String second) {
if (first == null || second == null) {
return first == null && second == null;
}
return first.equals(second);
@Override
public int hashCode() {
int hashCode = 17;
hashCode = 37 * hashCode + Util.hashString(trackerToken);
hashCode = 37 * hashCode + Util.hashString(trackerName);
hashCode = 37 * hashCode + Util.hashString(network);
hashCode = 37 * hashCode + Util.hashString(campaign);
hashCode = 37 * hashCode + Util.hashString(adgroup);
hashCode = 37 * hashCode + Util.hashString(creative);
return hashCode;
}


@Override
public String toString() {
return String.format("tt:%s tn:%s net:%s cam:%s adg:%s cre:%s",
trackerToken, trackerName, network, campaign, adgroup, creative);
}

private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
}

private void readObject(ObjectInputStream stream) throws ClassNotFoundException, IOException {
stream.defaultReadObject();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private void getAttributionInternal() {
return;
}

JSONObject jsonResponse = Util.parseJsonResponse(httpResponse, logger);
JSONObject jsonResponse = Util.parseJsonResponse(httpResponse);

checkAttributionInternal(jsonResponse);
}
Expand Down
2 changes: 1 addition & 1 deletion Adjust/adjust/src/main/java/com/adjust/sdk/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface Constants {
String BASE_URL = "https://app.adjust.com";
String SCHEME = "https";
String AUTHORITY = "app.adjust.com";
String CLIENT_SDK = "android4.0.0";
String CLIENT_SDK = "android4.0.1";
String LOGTAG = "Adjust";

String ACTIVITY_STATE_FILENAME = "AdjustIoActivityState";
Expand Down
Loading

0 comments on commit 269dc69

Please sign in to comment.