Skip to content

Commit

Permalink
Merge pull request #41 from adjust/disable_SDK
Browse files Browse the repository at this point in the history
V3.2.0 Disable SDK
  • Loading branch information
nonelse committed Apr 7, 2014
2 parents dda07be + f6d01a4 commit 1ac1e25
Show file tree
Hide file tree
Showing 15 changed files with 277 additions and 59 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ build/
#for oh-my-zsh jira plugin (https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins#jira)
.jira-url
atlassian-ide-plugin.xml

# add exception of google play services jar
!google-play-services.jar
Binary file added Adjust/libs/google-play-services.jar
Binary file not shown.
65 changes: 54 additions & 11 deletions Adjust/src/com/adjust/sdk/ActivityHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class ActivityHandler extends HandlerThread {
private String defaultTracker;
private boolean eventBuffering;
private boolean dropOfflineActivities;
private boolean enabled;

private String appToken;
private String macSha1;
Expand All @@ -80,6 +81,7 @@ public ActivityHandler(Activity activity) {
sessionHandler = new SessionHandler(getLooper(), this);
context = activity.getApplicationContext();
clientSdk = Constants.CLIENT_SDK;
enabled = true;

logger = AdjustFactory.getLogger();

Expand All @@ -99,6 +101,7 @@ public ActivityHandler(Activity activity, String appToken,
sessionHandler = new SessionHandler(getLooper(), this);
context = activity.getApplicationContext();
clientSdk = Constants.CLIENT_SDK;
enabled = true;

logger = AdjustFactory.getLogger();

Expand Down Expand Up @@ -133,7 +136,7 @@ public void trackSubsessionEnd() {
}

public void trackEvent(String eventToken, Map<String, String> parameters) {
PackageBuilder builder = new PackageBuilder();
PackageBuilder builder = new PackageBuilder(context);
builder.setEventToken(eventToken);
builder.setCallbackParameters(parameters);

Expand All @@ -144,7 +147,7 @@ public void trackEvent(String eventToken, Map<String, String> parameters) {
}

public void trackRevenue(double amountInCents, String eventToken, Map<String, String> parameters) {
PackageBuilder builder = new PackageBuilder();
PackageBuilder builder = new PackageBuilder(context);
builder.setAmountInCents(amountInCents);
builder.setEventToken(eventToken);
builder.setCallbackParameters(parameters);
Expand Down Expand Up @@ -173,6 +176,25 @@ public void run() {
handler.post(runnable);
}

public void setEnabled(Boolean enabled) {
this.enabled = enabled;
if (checkActivityState(activityState))
activityState.enabled = enabled;
if (enabled) {
this.trackSubsessionStart();
} else {
this.trackSubsessionEnd();
}
}

public Boolean isEnabled() {
if (checkActivityState(activityState)) {
return activityState.enabled;
} else {
return this.enabled;
}
}

private static final class SessionHandler extends Handler {
private static final int INIT_BUNDLE = 72630;
private static final int INIT_PRESET = 72633;
Expand Down Expand Up @@ -260,6 +282,11 @@ private void startInternal() {
return;
}

if (activityState != null
&& !activityState.enabled) {
return;
}

packageHandler.resumeSending();
startTimer();

Expand All @@ -273,6 +300,7 @@ private void startInternal() {

transferSessionPackage();
activityState.resetSessionAttributes(now);
activityState.enabled = this.enabled;
writeActivityState();
logger.info("First session");
return;
Expand Down Expand Up @@ -321,7 +349,7 @@ private void endInternal() {

packageHandler.pauseSending();
stopTimer();
updateActivityState();
updateActivityState(System.currentTimeMillis());
writeActivityState();
}

Expand All @@ -330,9 +358,14 @@ private void trackEventInternal(PackageBuilder eventBuilder) {
return;
}

activityState.createdAt = System.currentTimeMillis();
if (!activityState.enabled) {
return;
}

long now = System.currentTimeMillis();
activityState.createdAt = now;
activityState.eventCount++;
updateActivityState();
updateActivityState(now);

injectGeneralAttributes(eventBuilder);
activityState.injectEventAttributes(eventBuilder);
Expand All @@ -354,9 +387,15 @@ private void trackRevenueInternal(PackageBuilder revenueBuilder) {
return;
}

activityState.createdAt = System.currentTimeMillis();
if (!activityState.enabled) {
return;
}

long now = System.currentTimeMillis();

activityState.createdAt = now;
activityState.eventCount++;
updateActivityState();
updateActivityState(now);

injectGeneralAttributes(revenueBuilder);
activityState.injectEventAttributes(revenueBuilder);
Expand Down Expand Up @@ -385,12 +424,11 @@ && checkActivityState(activityState)
&& revenueBuilder.isValidForRevenue();
}

private void updateActivityState() {
private void updateActivityState(long now) {
if (!checkActivityState(activityState)) {
return;
}

long now = System.currentTimeMillis();
long lastInterval = now - activityState.lastActivity;
if (lastInterval < 0) {
logger.error(TIME_TRAVEL);
Expand Down Expand Up @@ -465,7 +503,7 @@ public static Boolean deleteActivityState(Context context) {
}

private void transferSessionPackage() {
PackageBuilder builder = new PackageBuilder();
PackageBuilder builder = new PackageBuilder(context);
injectGeneralAttributes(builder);
injectReferrer(builder);
activityState.injectSessionAttributes(builder);
Expand Down Expand Up @@ -518,9 +556,14 @@ private void stopTimer() {
}

private void timerFired() {
if (null != activityState
&& !activityState.enabled) {
return;
}

packageHandler.sendFirstPackage();

updateActivityState();
updateActivityState(System.currentTimeMillis());
writeActivityState();
}

Expand Down
4 changes: 4 additions & 0 deletions Adjust/src/com/adjust/sdk/ActivityState.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ActivityState implements Serializable {

// persistent data
protected String uuid;
protected Boolean enabled;

// global counters
protected int eventCount;
Expand All @@ -41,6 +42,7 @@ public class ActivityState implements Serializable {
protected ActivityState() {
// create UUID for new devices
uuid = Util.createUuid();
enabled = true;

eventCount = 0; // no events yet
sessionCount = 0; // the first session just started
Expand Down Expand Up @@ -71,6 +73,7 @@ protected void injectEventAttributes(PackageBuilder builder) {
builder.setEventCount(eventCount);
}

@Override
public String toString() {
return String.format(Locale.US,
"ec:%d sc:%d ssc:%d sl:%.1f ts:%.1f la:%s",
Expand All @@ -91,6 +94,7 @@ private void readObject(ObjectInputStream stream) throws NotActiveException, IOE
createdAt = fields.get("createdAt", -1l);
lastInterval = fields.get("lastInterval", -1l);
uuid = (String)fields.get("uuid", null);
enabled = fields.get("enabled", true);

// create UUID for migrating devices
if (uuid == null) {
Expand Down
28 changes: 28 additions & 0 deletions Adjust/src/com/adjust/sdk/Adjust.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ public static void trackRevenue(double amountInCents, String eventToken, Map<Str
}
}

/**
* Enable or disable the adjust SDK
*
* @param enabled The flag to enable or disable the adjust SDK
*/
public static void setEnabled(Boolean enabled) {
try {
activityHandler.setEnabled(enabled);
} catch (NullPointerException e) {
if (logger != null)
logger.error(NO_ACTIVITY_HANDLER_FOUND);
}
}

/**
* Check if the SDK is enabled or disabled
*/
public static Boolean isEnabled() {
try {
return activityHandler.isEnabled();
} catch (NullPointerException e) {
if (logger != null)
logger.error(NO_ACTIVITY_HANDLER_FOUND);
}
return false;
}


// 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) {
activityHandler = new ActivityHandler(activity, appToken, environment, logLevel, 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.0.0";
String CLIENT_SDK = "android3.2.0";
String LOGTAG = "Adjust";

String SESSION_STATE_FILENAME = "AdjustIoActivityState";
Expand Down
31 changes: 13 additions & 18 deletions Adjust/src/com/adjust/sdk/PackageBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@

package com.adjust.sdk;

import android.text.TextUtils;
import android.util.Base64;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;
import android.util.Base64;

public class PackageBuilder {

private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'Z";
private Context context;

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

private static SimpleDateFormat dateFormat;

public PackageBuilder()
{ }
public PackageBuilder(Context context)
{
this.context = context;
}

public void setAppToken(String appToken) {
this.appToken = appToken;
Expand Down Expand Up @@ -237,6 +238,8 @@ private Map<String, String> getDefaultParameters() {
addString(parameters, "android_uuid", uuid);
addString(parameters, "fb_id", fbAttributionId);
addString(parameters, "environment", environment);
String gpsAdid = Util.getGpsAdid(context);
addString(parameters, "gps_adid", gpsAdid);

// session related (used for events as well)
addInt(parameters, "session_count", sessionCount);
Expand Down Expand Up @@ -293,8 +296,7 @@ private void addDate(Map<String, String> parameters, String key, long value) {
return;
}

Date date = new Date(value);
String dateString = getDateFormat().format(date);
String dateString = Util.dateFormat(value);
addString(parameters, key, dateString);
}

Expand All @@ -318,11 +320,4 @@ private void addMap(Map<String, String> parameters, String key, Map<String, Stri

addString(parameters, key, encodedMap);
}

private SimpleDateFormat getDateFormat() {
if (null == dateFormat) {
dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
}
return dateFormat;
}
}
5 changes: 5 additions & 0 deletions Adjust/src/com/adjust/sdk/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ private HttpUriRequest getRequest(ActivityPackage activityPackage) throws Unsupp
pairs.add(pair);
}

long now = System.currentTimeMillis();
String dateString = Util.dateFormat(now);
NameValuePair sentAtPair = new BasicNameValuePair("sent_at", dateString);
pairs.add(sentAtPair);

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);
entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
request.setEntity(entity);
Expand Down
28 changes: 28 additions & 0 deletions Adjust/src/com/adjust/sdk/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.UUID;
import java.util.regex.Matcher;
Expand All @@ -47,12 +48,17 @@
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.google.android.gms.ads.identifier.AdvertisingIdClient;


/**
* Collects utility functions used by Adjust.
*/
public class Util {

private static SimpleDateFormat dateFormat;
private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'Z";

protected static String getUserAgent(final Context context) {
final Resources resources = context.getResources();
final DisplayMetrics displayMetrics = resources.getDisplayMetrics();
Expand Down Expand Up @@ -337,4 +343,26 @@ public static String quote(String string) {

return String.format("'%s'", string);
}

public static String dateFormat(long date) {
if (null == dateFormat) {
dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
}
return dateFormat.format(date);
}

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) {
Logger logger = AdjustFactory.getLogger();
logger.error(String.format("Error getting Google Play Services advertising ID, (%s)", e.getMessage()));
}

return gpsAdid;
}
}
Loading

0 comments on commit 1ac1e25

Please sign in to comment.