Skip to content

Commit

Permalink
Merge pull request #43 from adjust/development
Browse files Browse the repository at this point in the history
Added deep link parameters
Fixed new fields in migrating devices
  • Loading branch information
nonelse committed Apr 16, 2014
2 parents c012aad + 2d1b273 commit 297acea
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Adjust/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ android {
compileSdkVersion 19
defaultConfig {
versionCode 11
versionName '3.2.0'
versionName '3.3.0'
minSdkVersion 8
targetSdkVersion 19
}
Expand Down
2 changes: 1 addition & 1 deletion Adjust/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>adjust-android</artifactId>
<groupId>com.adjust.sdk</groupId>
<version>3.2.0</version>
<version>3.3.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
61 changes: 60 additions & 1 deletion Adjust/src/com/adjust/sdk/ActivityHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Executors;
Expand All @@ -36,6 +37,7 @@
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
Expand All @@ -49,6 +51,7 @@ public class ActivityHandler extends HandlerThread {
private static long SESSION_INTERVAL;
private static long SUBSESSION_INTERVAL;
private static final String TIME_TRAVEL = "Time travel!";
private static final String ADJUST_PREFIX = "adjust_";

private final SessionHandler sessionHandler;
private IPackageHandler packageHandler;
Expand Down Expand Up @@ -195,13 +198,22 @@ public Boolean isEnabled() {
}
}

public void readOpenUrl(Uri url) {
Message message = Message.obtain();
message.arg1 = SessionHandler.DEEP_LINK;
message.obj = url;
sessionHandler.sendMessage(message);
}

private static final class SessionHandler extends Handler {
private static final int INIT_BUNDLE = 72630;
private static final int INIT_PRESET = 72633;
private static final int START = 72640;
private static final int END = 72650;
private static final int EVENT = 72660;
private static final int REVENUE = 72670;
private static final int DEEP_LINK = 72680;


private final WeakReference<ActivityHandler> sessionHandlerReference;

Expand Down Expand Up @@ -240,6 +252,10 @@ public void handleMessage(Message message) {
PackageBuilder revenueBuilder = (PackageBuilder) message.obj;
sessionHandler.trackRevenueInternal(revenueBuilder);
break;
case DEEP_LINK:
Uri url = (Uri) message.obj;
sessionHandler.readOpenUrlInternal(url);
break;
}
}
}
Expand Down Expand Up @@ -412,6 +428,49 @@ private void trackRevenueInternal(PackageBuilder revenueBuilder) {
logger.debug(String.format(Locale.US, "Event %d (revenue)", activityState.eventCount));
}

private void readOpenUrlInternal(Uri url) {
if (url == null) {
return;
}

String queryString = url.getQuery();
if (queryString == null) {
return;
}

Map<String, String> adjustDeepLinks = new HashMap<String, String>();

String[] queryPairs = queryString.split("&");
for (String pair : queryPairs) {
String[] pairComponents = pair.split("=");
if (pairComponents.length != 2) continue;

String key = pairComponents[0];
if (!key.startsWith(ADJUST_PREFIX)) continue;

String value = pairComponents[1];
if (value.length() == 0) continue;

String keyWOutPrefix = key.substring(ADJUST_PREFIX.length());
if (keyWOutPrefix.length() == 0) continue;

adjustDeepLinks.put(keyWOutPrefix, value);
}

if (adjustDeepLinks.size() == 0) {
return;
}

PackageBuilder builder = new PackageBuilder(context);
builder.setDeepLinkParameters(adjustDeepLinks);
injectGeneralAttributes(builder);
ActivityPackage reattributionPackage = builder.buildReattributionPackage();
packageHandler.addPackage(reattributionPackage);
packageHandler.sendFirstPackage();

logger.debug(String.format("Reattribution %s", adjustDeepLinks.toString()));
}

private boolean canTrackEvent(PackageBuilder revenueBuilder) {
return checkAppTokenNotNull(appToken)
&& checkActivityState(activityState)
Expand Down Expand Up @@ -486,7 +545,7 @@ private void writeActivityState() {

try {
objectStream.writeObject(activityState);
logger.verbose(String.format("Wrote activity state: %s", activityState));
logger.debug(String.format("Wrote activity state: %s", activityState));
} catch (NotSerializableException e) {
logger.error("Failed to serialize activity state");
} finally {
Expand Down
14 changes: 9 additions & 5 deletions Adjust/src/com/adjust/sdk/ActivityKind.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.adjust.sdk;

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

public static ActivityKind fromString(String string) {
if ("session".equals(string)) {
Expand All @@ -10,17 +10,21 @@ public static ActivityKind fromString(String string) {
return EVENT;
} else if ("revenue".equals(string)) {
return REVENUE;
} else if ("reattribution".equals(string)) {
return REATTRIBUTION;
} else {
return UNKNOWN;
}
}

@Override
public String toString() {
switch(this) {
case SESSION: return "session";
case EVENT: return "event";
case REVENUE: return "revenue";
default: return "unknown";
case SESSION: return "session";
case EVENT: return "event";
case REVENUE: return "revenue";
case REATTRIBUTION: return "reattribution";
default: return "unknown";
}
}
}
16 changes: 14 additions & 2 deletions Adjust/src/com/adjust/sdk/ActivityState.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,20 @@ private void readObject(ObjectInputStream stream) throws NotActiveException, IOE
lastActivity = fields.get("lastActivity", -1l);
createdAt = fields.get("createdAt", -1l);
lastInterval = fields.get("lastInterval", -1l);
uuid = (String)fields.get("uuid", null);
enabled = fields.get("enabled", true);

// default values for migrating devices
uuid = null;
enabled = true;
// try to read in order of less recent new fields
try {
uuid = (String)fields.get("uuid", null);
enabled = fields.get("enabled", true);
// add new fields here
} catch (Exception e) {
Logger logger = AdjustFactory.getLogger();
logger.debug(String.format("Unable to read new field in migration device with error (%s)",
e.getMessage()));
}

// create UUID for migrating devices
if (uuid == null) {
Expand Down
11 changes: 11 additions & 0 deletions Adjust/src/com/adjust/sdk/Adjust.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Map;

import android.app.Activity;
import android.net.Uri;

/**
* The main interface to Adjust.
Expand Down Expand Up @@ -148,6 +149,16 @@ public static Boolean isEnabled() {
return false;
}

public static void appWillOpenUrl(Uri url) {
try {
activityHandler.readOpenUrl(url);
} catch (NullPointerException e) {
if (logger != null)
logger.error(NO_ACTIVITY_HANDLER_FOUND);
}

}


// Special appDidLaunch method used by SDK wrappers such as our Adobe Air SDK.
protected static void appDidLaunch(Activity activity, String appToken, String environment, String logLevel, boolean eventBuffering) {
Expand Down
2 changes: 1 addition & 1 deletion Adjust/src/com/adjust/sdk/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface Constants {
int THIRTY_MINUTES = 30 * ONE_MINUTE;

String BASE_URL = "https://app.adjust.io";
String CLIENT_SDK = "android3.2.0";
String CLIENT_SDK = "android3.3.0";
String LOGTAG = "Adjust";

String SESSION_STATE_FILENAME = "AdjustIoActivityState";
Expand Down
37 changes: 34 additions & 3 deletions Adjust/src/com/adjust/sdk/PackageBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

public class PackageBuilder {

private Context context;
private Context context;

// general
private String appToken;
Expand Down Expand Up @@ -50,6 +50,9 @@ public class PackageBuilder {
private double amountInCents;
private Map<String, String> callbackParameters;

// reattributions
private Map<String, String> deepLinkParameters;

public PackageBuilder(Context context)
{
this.context = context;
Expand Down Expand Up @@ -147,6 +150,10 @@ public void setCallbackParameters(Map<String, String> callbackParameters) {
this.callbackParameters = callbackParameters;
}

public void setDeepLinkParameters(Map<String, String> deepLinkParameters) {
this.deepLinkParameters = deepLinkParameters;
}

public boolean isValidForEvent() {
if (null == eventToken) {
Logger logger = AdjustFactory.getLogger();
Expand Down Expand Up @@ -210,6 +217,19 @@ public ActivityPackage buildRevenuePackage() {
return revenuePackage;
}

public ActivityPackage buildReattributionPackage() {
Map<String, String> parameters = getDefaultParameters();
addMapJson(parameters, "deeplink_parameters", deepLinkParameters);

ActivityPackage reattributionPackage = getDefaultActivityPackage();
reattributionPackage.setPath("/reattribute");
reattributionPackage.setActivityKind(ActivityKind.REATTRIBUTION);
reattributionPackage.setSuffix("");
reattributionPackage.setParameters(parameters);

return reattributionPackage;
}

private boolean isEventTokenValid() {
if (6 != eventToken.length()) {
Logger logger = AdjustFactory.getLogger();
Expand Down Expand Up @@ -253,7 +273,7 @@ private Map<String, String> getDefaultParameters() {
private void injectEventParameters(Map<String, String> parameters) {
addInt(parameters, "event_count", eventCount);
addString(parameters, "event_token", eventToken);
addMap(parameters, "params", callbackParameters);
addMapBase64(parameters, "params", callbackParameters);
}

private String getAmountString() {
Expand Down Expand Up @@ -309,7 +329,7 @@ private void addDuration(Map<String, String> parameters, String key, long durati
addInt(parameters, key, durationInSeconds);
}

private void addMap(Map<String, String> parameters, String key, Map<String, String> map) {
private void addMapBase64(Map<String, String> parameters, String key, Map<String, String> map) {
if (null == map) {
return;
}
Expand All @@ -320,4 +340,15 @@ private void addMap(Map<String, String> parameters, String key, Map<String, Stri

addString(parameters, key, encodedMap);
}

private void addMapJson(Map<String, String> parameters, String key, Map<String, String> map) {
if (null == map) {
return;
}

JSONObject jsonObject = new JSONObject(map);
String jsonString = jsonObject.toString();

addString(parameters, key, jsonString);
}
}
48 changes: 24 additions & 24 deletions Adjust/src/com/adjust/sdk/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ protected static String getUserAgent(final Context context) {
final int screenLayout = configuration.screenLayout;

final String[] parts = {
getPackageName(context),
getAppVersion(context),
getDeviceType(screenLayout),
getDeviceName(),
getOsName(),
getOsVersion(),
getLanguage(locale),
getCountry(locale),
getScreenSize(screenLayout),
getScreenFormat(screenLayout),
getScreenDensity(displayMetrics),
getDisplayWidth(displayMetrics),
getDisplayHeight(displayMetrics)
getPackageName(context),
getAppVersion(context),
getDeviceType(screenLayout),
getDeviceName(),
getOsName(),
getOsVersion(),
getLanguage(locale),
getCountry(locale),
getScreenSize(screenLayout),
getScreenFormat(screenLayout),
getScreenDensity(displayMetrics),
getDisplayWidth(displayMetrics),
getDisplayHeight(displayMetrics)
};
return TextUtils.join(" ", parts);
}
Expand Down Expand Up @@ -351,18 +351,18 @@ public static String dateFormat(long date) {
return dateFormat.format(date);
}

public static String getGpsAdid(Context context) {
String gpsAdid = null;
try {
AdvertisingIdClient.Info info = AdvertisingIdClient.getAdvertisingIdInfo(context);
if (!info.isLimitAdTrackingEnabled()) {
public static String getGpsAdid(Context context) {
String gpsAdid = null;
try {
AdvertisingIdClient.Info info = AdvertisingIdClient.getAdvertisingIdInfo(context);
if (!info.isLimitAdTrackingEnabled()) {
gpsAdid = info.getId();
}
} catch (Exception e) {
}
} catch (Exception e) {
Logger logger = AdjustFactory.getLogger();
logger.error(String.format("Error getting Google Play Services advertising ID, (%s)", e.getMessage()));
}
logger.error(String.format("Error getting Google Play Services advertising ID, (%s)", e.getMessage()));
}

return gpsAdid;
}
return gpsAdid;
}
}
Loading

0 comments on commit 297acea

Please sign in to comment.