Skip to content

Commit

Permalink
Merge pull request #126 from adjust/timer
Browse files Browse the repository at this point in the history
Timer
  • Loading branch information
nonelse committed Jun 10, 2015
2 parents c326914 + 30612fa commit 710d46b
Show file tree
Hide file tree
Showing 27 changed files with 426 additions and 437 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.7"
versionName "4.0.8"
}
}

Expand Down
96 changes: 47 additions & 49 deletions Adjust/adjust/src/main/java/com/adjust/sdk/ActivityHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static com.adjust.sdk.Constants.ACTIVITY_STATE_FILENAME;
import static com.adjust.sdk.Constants.ATTRIBUTION_FILENAME;
Expand All @@ -50,7 +46,7 @@ public class ActivityHandler extends HandlerThread implements IActivityHandler {
private IPackageHandler packageHandler;
private ActivityState activityState;
private ILogger logger;
private static ScheduledExecutorService timer;
private TimerCycle timer;
private boolean enabled;
private boolean offline;

Expand Down Expand Up @@ -129,6 +125,10 @@ public void trackSubsessionEnd() {

@Override
public void trackEvent(AdjustEvent event) {
if (activityState == null) {
trackSubsessionStart();
}

Message message = Message.obtain();
message.arg1 = SessionHandler.EVENT;
message.obj = event;
Expand Down Expand Up @@ -163,7 +163,7 @@ public void setEnabled(boolean enabled) {
writeActivityState();
}
if (enabled) {
if (toPause()) {
if (paused()) {
logger.info("Package and attribution handler remain paused due to the SDK is offline");
} else {
logger.info("Resuming package handler and attribution handler to enabled the SDK");
Expand All @@ -189,7 +189,7 @@ public void setOfflineMode(boolean offline) {
if (offline) {
logger.info("Pausing package and attribution handler to put in offline mode");
} else {
if (toPause()) {
if (paused()) {
logger.info("Package and attribution handler remain paused because the SDK is disabled");
} else {
logger.info("Resuming package handler and attribution handler to put in online mode");
Expand Down Expand Up @@ -299,6 +299,12 @@ private void updateStatus() {
sessionHandler.sendMessage(message);
}

private void timerFired() {
Message message = Message.obtain();
message.arg1 = SessionHandler.TIMER_FIRED;
sessionHandler.sendMessage(message);
}

private static final class SessionHandler extends Handler {
private static final int BASE_ADDRESS = 72630;
private static final int INIT = BASE_ADDRESS + 1;
Expand All @@ -309,6 +315,7 @@ private static final class SessionHandler extends Handler {
private static final int DEEP_LINK = BASE_ADDRESS + 6;
private static final int SEND_REFERRER = BASE_ADDRESS + 7;
private static final int UPDATE_STATUS = BASE_ADDRESS + 8;
private static final int TIMER_FIRED = BASE_ADDRESS + 9;

private final WeakReference<ActivityHandler> sessionHandlerReference;

Expand Down Expand Up @@ -355,6 +362,9 @@ public void handleMessage(Message message) {
case UPDATE_STATUS:
sessionHandler.updateStatusInternal();
break;
case TIMER_FIRED:
sessionHandler.timerFiredInternal();
break;
}
}
}
Expand Down Expand Up @@ -393,9 +403,20 @@ private void initInternal() {
readAttribution();
readActivityState();

packageHandler = AdjustFactory.getPackageHandler(this, adjustConfig.context, toPause());
packageHandler = AdjustFactory.getPackageHandler(this, adjustConfig.context, paused());

startInternal();
ActivityPackage attributionPackage = getAttributionPackage();
attributionHandler = AdjustFactory.getAttributionHandler(this,
attributionPackage,
paused(),
adjustConfig.hasListener());

timer = new TimerCycle(new Runnable() {
@Override
public void run() {
timerFired();
}
},TIMER_START, TIMER_INTERVAL);
}

private void startInternal() {
Expand Down Expand Up @@ -472,12 +493,12 @@ private void checkAttributionState() {
return;
}

getAttributionHandler().getAttribution();
attributionHandler.getAttribution();
}

private void endInternal() {
packageHandler.pauseSending();
getAttributionHandler().pauseSending();
attributionHandler.pauseSending();
stopTimer();
if (updateActivityState(System.currentTimeMillis())) {
writeActivityState();
Expand Down Expand Up @@ -513,7 +534,7 @@ private void finishedTrackingActivityInternal(JSONObject jsonResponse) {

String deeplink = jsonResponse.optString("deeplink", null);
launchDeeplinkMain(deeplink);
getAttributionHandler().checkAttribution(jsonResponse);
attributionHandler.checkAttribution(jsonResponse);
}

private void sendReferrerInternal(String referrer, long clickTime) {
Expand All @@ -524,8 +545,6 @@ private void sendReferrerInternal(String referrer, long clickTime) {
return;
}

getAttributionHandler().getAttribution();

packageHandler.sendClickPackage(clickPackage);
}

Expand All @@ -541,8 +560,6 @@ private void readOpenUrlInternal(Uri url, long clickTime) {
return;
}

getAttributionHandler().getAttribution();

packageHandler.sendClickPackage(clickPackage);
}

Expand All @@ -551,7 +568,6 @@ private ActivityPackage buildQueryStringClickPackage(String queryString, String
return null;
}

long now = System.currentTimeMillis();
Map<String, String> queryStringParameters = new LinkedHashMap<String, String>();
AdjustAttribution queryStringAttribution = new AdjustAttribution();
boolean hasAdjustTags = false;
Expand All @@ -569,6 +585,7 @@ private ActivityPackage buildQueryStringClickPackage(String queryString, String

String reftag = queryStringParameters.remove("reftag");

long now = System.currentTimeMillis();
PackageBuilder builder = new PackageBuilder(adjustConfig, deviceInfo, activityState, now);
builder.extraParameters = queryStringParameters;
builder.attribution = queryStringAttribution;
Expand Down Expand Up @@ -634,7 +651,7 @@ private void updateAttributionHandlerStatus() {
if (attributionHandler == null) {
return;
}
if (toPause()) {
if (paused()) {
attributionHandler.pauseSending();
} else {
attributionHandler.resumeSending();
Expand All @@ -645,7 +662,7 @@ private void updatePackageHandlerStatus() {
if (packageHandler == null) {
return;
}
if (toPause()) {
if (paused()) {
packageHandler.pauseSending();
} else {
packageHandler.resumeSending();
Expand Down Expand Up @@ -707,33 +724,26 @@ private void transferSessionPackage(long now) {
}

private void startTimer() {
stopTimer();

if (!activityState.enabled) {
// don't start the timer if it's disabled/offline
if (paused()) {
return;
}
timer = Executors.newSingleThreadScheduledExecutor();
timer.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
timerFired();
}
}, TIMER_START, TIMER_INTERVAL, TimeUnit.MILLISECONDS);

timer.start();
}

private void stopTimer() {
if (timer != null) {
timer.shutdown();
timer = null;
}
timer.suspend();
}

private void timerFired() {
if (!activityState.enabled) {
private void timerFiredInternal() {
if (paused()) {
// stop the timer cycle if it's disabled/offline
stopTimer();
return;
}

logger.debug("Session timer fired");
packageHandler.sendFirstPackage();

if (updateActivityState(System.currentTimeMillis())) {
Expand All @@ -749,7 +759,7 @@ private void readAttribution() {
attribution = Util.readObject(adjustConfig.context, ATTRIBUTION_FILENAME, ATTRIBUTION_NAME);
}

private void writeActivityState() {
private synchronized void writeActivityState() {
Util.writeObject(activityState, adjustConfig.context, ACTIVITY_STATE_FILENAME, ACTIVITY_STATE_NAME);
}

Expand All @@ -771,19 +781,7 @@ private boolean checkEvent(AdjustEvent event) {
return true;
}

// lazy initialization to prevent null activity state before first session
private IAttributionHandler getAttributionHandler() {
if (attributionHandler == null) {
ActivityPackage attributionPackage = getAttributionPackage();
attributionHandler = AdjustFactory.getAttributionHandler(this,
attributionPackage,
toPause(),
adjustConfig.hasListener());
}
return attributionHandler;
}

private boolean toPause() {
private boolean paused() {
return offline || !isEnabled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static boolean checkContext(Context context) {
private static boolean checkAppToken(String appToken) {
ILogger logger = AdjustFactory.getLogger();
if (appToken == null) {
logger.error("Missing App Token.");
logger.error("Missing App Token");
return false;
}

Expand Down
30 changes: 15 additions & 15 deletions Adjust/adjust/src/main/java/com/adjust/sdk/AttributionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class AttributionHandler implements IAttributionHandler {
private IActivityHandler activityHandler;
private ILogger logger;
private ActivityPackage attributionPackage;
private ScheduledFuture waitingTask;
private TimerOnce timer;
private HttpClient httpClient;
private boolean paused;
private boolean hasListener;
Expand All @@ -35,6 +35,12 @@ public AttributionHandler(IActivityHandler activityHandler,
scheduler = Executors.newSingleThreadScheduledExecutor();
logger = AdjustFactory.getLogger();
httpClient = Util.getHttpClient();
timer = new TimerOnce(scheduler, new Runnable() {
@Override
public void run() {
getAttributionInternal();
}
});
init(activityHandler, attributionPackage, startPaused, hasListener);
}

Expand Down Expand Up @@ -74,24 +80,18 @@ public void resumeSending() {
paused = false;
}

private void getAttribution(int delayInMilliseconds) {
if (waitingTask != null) {
if (waitingTask.getDelay(TimeUnit.MILLISECONDS) > delayInMilliseconds) {
return;
}
waitingTask.cancel(false);
private void getAttribution(long delayInMilliseconds) {
// don't reset if new time is shorter than last one
if (timer.getFireIn() > delayInMilliseconds) {
return;
}

if (delayInMilliseconds != 0) {
logger.debug("Waiting to query attribution in %d milliseconds", delayInMilliseconds);
}

waitingTask = scheduler.schedule(new Runnable() {
@Override
public void run() {
getAttributionInternal();
}
}, delayInMilliseconds, TimeUnit.MILLISECONDS);
// set the new time the timer will fire in
timer.startIn(delayInMilliseconds);
}

private void checkAttributionInternal(JSONObject jsonResponse) {
Expand All @@ -100,7 +100,7 @@ private void checkAttributionInternal(JSONObject jsonResponse) {
JSONObject attributionJson = jsonResponse.optJSONObject("attribution");
AdjustAttribution attribution = AdjustAttribution.fromJson(attributionJson);

int timerMilliseconds = jsonResponse.optInt("ask_in", -1);
long timerMilliseconds = jsonResponse.optLong("ask_in", -1);

// without ask_in attribute
if (timerMilliseconds < 0) {
Expand All @@ -121,7 +121,7 @@ private void getAttributionInternal() {
return;
}
if (paused) {
logger.debug("Attribution Handler is paused");
logger.debug("Attribution handler is paused");
return;
}
logger.verbose("%s", attributionPackage.getExtendedString());
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.7";
String CLIENT_SDK = "android4.0.8";
String LOGTAG = "Adjust";

String ACTIVITY_STATE_FILENAME = "AdjustIoActivityState";
Expand Down
Loading

0 comments on commit 710d46b

Please sign in to comment.