Skip to content

Commit

Permalink
Merge pull request #33 from adeven/disable_offline_tracking
Browse files Browse the repository at this point in the history
Add option to disable offline tracking
  • Loading branch information
wellle committed Jan 13, 2014
2 parents 3452388 + bb7bb9c commit 0c545af
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 23 deletions.
2 changes: 1 addition & 1 deletion AdjustIo/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 '2.1.5'
versionName '2.1.6'
minSdkVersion 8
targetSdkVersion 19
}
Expand Down
2 changes: 1 addition & 1 deletion AdjustIo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>adjustio-android</artifactId>
<groupId>com.adeven.adjustio</groupId>
<version>2.1.5</version>
<version>2.1.6</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
11 changes: 10 additions & 1 deletion AdjustIo/src/com/adeven/adjustio/ActivityHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class ActivityHandler extends HandlerThread {
private String environment;
private String defaultTracker;
private boolean eventBuffering;
private boolean dropOfflineActivities;

private String appToken;
private String macSha1;
Expand Down Expand Up @@ -206,7 +207,7 @@ private void initInternal(boolean fromBundle) {
fbAttributionId = Util.getAttributionId(context);
userAgent = Util.getUserAgent(context);

packageHandler = new PackageHandler(context);
packageHandler = new PackageHandler(context, dropOfflineActivities);
readActivityState();
}

Expand Down Expand Up @@ -507,6 +508,7 @@ private void processApplicationBundle() {
setDefaultTracker(bundle.getString("AdjustIoDefaultTracker"));
setEventBuffering(bundle.getBoolean("AdjustIoEventBuffering"));
Logger.setLogLevelString(bundle.getString("AdjustIoLogLevel"));
setDropOfflineActivities(bundle.getBoolean("AdjustIoDropOfflineActivities"));
}

private void setEnvironment(String env) {
Expand Down Expand Up @@ -543,6 +545,13 @@ private void setDefaultTracker(String tracker) {
}
}

private void setDropOfflineActivities(boolean drop) {
dropOfflineActivities = drop;
if (dropOfflineActivities) {
Logger.info("Offline activities will get dropped");
}
}

private Bundle getApplicationBundle() {
final ApplicationInfo applicationInfo;
try {
Expand Down
2 changes: 1 addition & 1 deletion AdjustIo/src/com/adeven/adjustio/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface Constants {
int THIRTY_MINUTES = 30 * ONE_MINUTE;

String BASE_URL = "https://app.adjust.io";
String CLIENT_SDK = "android2.1.5";
String CLIENT_SDK = "android2.1.6";
String LOGTAG = "AdjustIo";

String SESSION_STATE_FILENAME = "AdjustIoActivityState";
Expand Down
28 changes: 26 additions & 2 deletions AdjustIo/src/com/adeven/adjustio/PackageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ public class PackageHandler extends HandlerThread {
private AtomicBoolean isSending;
private boolean paused;
private final Context context;
private final boolean dropOfflineActivities;

protected PackageHandler(Context context) {
protected PackageHandler(Context context, boolean dropOfflineActivities) {
super(Constants.LOGTAG, MIN_PRIORITY);
setDaemon(true);
start();
this.internalHandler = new InternalHandler(getLooper(), this);

this.context = context;
this.dropOfflineActivities = dropOfflineActivities;

Message message = Message.obtain();
message.arg1 = InternalHandler.INIT;
Expand Down Expand Up @@ -79,7 +81,11 @@ protected void sendNextPackage() {

// close the package to retry in the future (after temporary failure)
protected void closeFirstPackage() {
isSending.set(false);
if (dropOfflineActivities) {
sendNextPackage();
} else {
isSending.set(false);
}
}

// interrupt the sending loop after the current request has finished
Expand All @@ -92,6 +98,15 @@ protected void resumeSending() {
paused = false;
}

// short info about how failing packages are handled
protected String getFailureMessage() {
if (dropOfflineActivities) {
return "Dropping offline activity.";
} else {
return "Will retry later.";
}
}

private static final class InternalHandler extends Handler {
private static final int INIT = 1;
private static final int ADD = 2;
Expand Down Expand Up @@ -175,6 +190,11 @@ private void sendNextInternal() {
}

private void readPackageQueue() {
if (dropOfflineActivities) {
packageQueue = new ArrayList<ActivityPackage>();
return; // don't read old packages when offline tracking is disabled
}

try {
FileInputStream inputStream = context.openFileInput(PACKAGE_QUEUE_FILENAME);
BufferedInputStream bufferedStream = new BufferedInputStream(inputStream);
Expand Down Expand Up @@ -209,6 +229,10 @@ private void readPackageQueue() {
}

private void writePackageQueue() {
if (dropOfflineActivities) {
return; // don't write packages when offline tracking is disabled
}

try {
FileOutputStream outputStream = context.openFileOutput(PACKAGE_QUEUE_FILENAME, Context.MODE_PRIVATE);
BufferedOutputStream bufferedStream = new BufferedOutputStream(outputStream);
Expand Down
27 changes: 13 additions & 14 deletions AdjustIo/src/com/adeven/adjustio/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,29 +148,28 @@ private String parseResponse(HttpResponse response) {
}

private void closePackage(ActivityPackage activityPackage, String message, Throwable throwable) {
final String failureMessage = activityPackage.getFailureMessage();
final String errorMessage;
if (throwable != null) {
errorMessage = String.format("%s. (%s: %s) Will retry later.", failureMessage, message, throwable);
} else {
errorMessage = String.format("%s. (%s) Will retry later.", failureMessage, message);
}
Logger.error(errorMessage);
final String packageMessage = activityPackage.getFailureMessage();
final String handlerMessage = packageHandler.getFailureMessage();
final String reasonString = getReasonString(message, throwable);
Logger.error(String.format("%s. (%s) %s", packageMessage, reasonString, handlerMessage));
packageHandler.closeFirstPackage();
}

private void sendNextPackage(ActivityPackage activityPackage, String message, Throwable throwable) {
String failureMessage = activityPackage.getFailureMessage();
final String failureMessage = activityPackage.getFailureMessage();
final String reasonString = getReasonString(message, throwable);
Logger.error(String.format("%s. (%s)", failureMessage, reasonString));
packageHandler.sendNextPackage();
}

private String getReasonString(String message, Throwable throwable) {
if (throwable != null) {
Logger.error(String.format("%s (%s: %s)", failureMessage, message, throwable));
return String.format("%s: %s", message, throwable);
} else {
Logger.error(String.format("%s (%s)", failureMessage, message));
return String.format("%s", message);
}

packageHandler.sendNextPackage();
}


private HttpUriRequest getRequest(ActivityPackage activityPackage) throws UnsupportedEncodingException {
String url = Constants.BASE_URL + activityPackage.getPath();
HttpPost request = new HttpPost(url);
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.5
2.1.6
4 changes: 2 additions & 2 deletions doc/migrate.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
## Migrate to AdjustIo SDK for Android v2.1.5
## Migrate to AdjustIo SDK for Android v2.1.6

0. In order to save some time later on you might want to check your current SDK
Version. You can find that value in a constant named `CLIENT_SDK` (or
`CLIENTSDK`) in `com.adeven.adjustio/Util.java`. It should look like
`android1.6` or similar.

1. Delete the old `AdjustIo` project from your `Package Explorer`. Download
version v2.1.5 and create a new `Android Project from Existing Code` as
version v2.1.6 and create a new `Android Project from Existing Code` as
described in the [README].

![][import]
Expand Down

0 comments on commit 0c545af

Please sign in to comment.