Skip to content

Commit 710d46b

Browse files
committed
Merge pull request #126 from adjust/timer
Timer
2 parents c326914 + 30612fa commit 710d46b

27 files changed

+426
-437
lines changed

Adjust/adjust/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 9
99
targetSdkVersion 21
1010
versionCode 1
11-
versionName "4.0.7"
11+
versionName "4.0.8"
1212
}
1313
}
1414

Adjust/adjust/src/main/java/com/adjust/sdk/ActivityHandler.java

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,9 @@
2323
import org.json.JSONObject;
2424

2525
import java.lang.ref.WeakReference;
26-
import java.util.HashMap;
2726
import java.util.LinkedHashMap;
2827
import java.util.List;
2928
import java.util.Map;
30-
import java.util.concurrent.Executors;
31-
import java.util.concurrent.ScheduledExecutorService;
32-
import java.util.concurrent.TimeUnit;
3329

3430
import static com.adjust.sdk.Constants.ACTIVITY_STATE_FILENAME;
3531
import static com.adjust.sdk.Constants.ATTRIBUTION_FILENAME;
@@ -50,7 +46,7 @@ public class ActivityHandler extends HandlerThread implements IActivityHandler {
5046
private IPackageHandler packageHandler;
5147
private ActivityState activityState;
5248
private ILogger logger;
53-
private static ScheduledExecutorService timer;
49+
private TimerCycle timer;
5450
private boolean enabled;
5551
private boolean offline;
5652

@@ -129,6 +125,10 @@ public void trackSubsessionEnd() {
129125

130126
@Override
131127
public void trackEvent(AdjustEvent event) {
128+
if (activityState == null) {
129+
trackSubsessionStart();
130+
}
131+
132132
Message message = Message.obtain();
133133
message.arg1 = SessionHandler.EVENT;
134134
message.obj = event;
@@ -163,7 +163,7 @@ public void setEnabled(boolean enabled) {
163163
writeActivityState();
164164
}
165165
if (enabled) {
166-
if (toPause()) {
166+
if (paused()) {
167167
logger.info("Package and attribution handler remain paused due to the SDK is offline");
168168
} else {
169169
logger.info("Resuming package handler and attribution handler to enabled the SDK");
@@ -189,7 +189,7 @@ public void setOfflineMode(boolean offline) {
189189
if (offline) {
190190
logger.info("Pausing package and attribution handler to put in offline mode");
191191
} else {
192-
if (toPause()) {
192+
if (paused()) {
193193
logger.info("Package and attribution handler remain paused because the SDK is disabled");
194194
} else {
195195
logger.info("Resuming package handler and attribution handler to put in online mode");
@@ -299,6 +299,12 @@ private void updateStatus() {
299299
sessionHandler.sendMessage(message);
300300
}
301301

302+
private void timerFired() {
303+
Message message = Message.obtain();
304+
message.arg1 = SessionHandler.TIMER_FIRED;
305+
sessionHandler.sendMessage(message);
306+
}
307+
302308
private static final class SessionHandler extends Handler {
303309
private static final int BASE_ADDRESS = 72630;
304310
private static final int INIT = BASE_ADDRESS + 1;
@@ -309,6 +315,7 @@ private static final class SessionHandler extends Handler {
309315
private static final int DEEP_LINK = BASE_ADDRESS + 6;
310316
private static final int SEND_REFERRER = BASE_ADDRESS + 7;
311317
private static final int UPDATE_STATUS = BASE_ADDRESS + 8;
318+
private static final int TIMER_FIRED = BASE_ADDRESS + 9;
312319

313320
private final WeakReference<ActivityHandler> sessionHandlerReference;
314321

@@ -355,6 +362,9 @@ public void handleMessage(Message message) {
355362
case UPDATE_STATUS:
356363
sessionHandler.updateStatusInternal();
357364
break;
365+
case TIMER_FIRED:
366+
sessionHandler.timerFiredInternal();
367+
break;
358368
}
359369
}
360370
}
@@ -393,9 +403,20 @@ private void initInternal() {
393403
readAttribution();
394404
readActivityState();
395405

396-
packageHandler = AdjustFactory.getPackageHandler(this, adjustConfig.context, toPause());
406+
packageHandler = AdjustFactory.getPackageHandler(this, adjustConfig.context, paused());
397407

398-
startInternal();
408+
ActivityPackage attributionPackage = getAttributionPackage();
409+
attributionHandler = AdjustFactory.getAttributionHandler(this,
410+
attributionPackage,
411+
paused(),
412+
adjustConfig.hasListener());
413+
414+
timer = new TimerCycle(new Runnable() {
415+
@Override
416+
public void run() {
417+
timerFired();
418+
}
419+
},TIMER_START, TIMER_INTERVAL);
399420
}
400421

401422
private void startInternal() {
@@ -472,12 +493,12 @@ private void checkAttributionState() {
472493
return;
473494
}
474495

475-
getAttributionHandler().getAttribution();
496+
attributionHandler.getAttribution();
476497
}
477498

478499
private void endInternal() {
479500
packageHandler.pauseSending();
480-
getAttributionHandler().pauseSending();
501+
attributionHandler.pauseSending();
481502
stopTimer();
482503
if (updateActivityState(System.currentTimeMillis())) {
483504
writeActivityState();
@@ -513,7 +534,7 @@ private void finishedTrackingActivityInternal(JSONObject jsonResponse) {
513534

514535
String deeplink = jsonResponse.optString("deeplink", null);
515536
launchDeeplinkMain(deeplink);
516-
getAttributionHandler().checkAttribution(jsonResponse);
537+
attributionHandler.checkAttribution(jsonResponse);
517538
}
518539

519540
private void sendReferrerInternal(String referrer, long clickTime) {
@@ -524,8 +545,6 @@ private void sendReferrerInternal(String referrer, long clickTime) {
524545
return;
525546
}
526547

527-
getAttributionHandler().getAttribution();
528-
529548
packageHandler.sendClickPackage(clickPackage);
530549
}
531550

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

544-
getAttributionHandler().getAttribution();
545-
546563
packageHandler.sendClickPackage(clickPackage);
547564
}
548565

@@ -551,7 +568,6 @@ private ActivityPackage buildQueryStringClickPackage(String queryString, String
551568
return null;
552569
}
553570

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

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

588+
long now = System.currentTimeMillis();
572589
PackageBuilder builder = new PackageBuilder(adjustConfig, deviceInfo, activityState, now);
573590
builder.extraParameters = queryStringParameters;
574591
builder.attribution = queryStringAttribution;
@@ -634,7 +651,7 @@ private void updateAttributionHandlerStatus() {
634651
if (attributionHandler == null) {
635652
return;
636653
}
637-
if (toPause()) {
654+
if (paused()) {
638655
attributionHandler.pauseSending();
639656
} else {
640657
attributionHandler.resumeSending();
@@ -645,7 +662,7 @@ private void updatePackageHandlerStatus() {
645662
if (packageHandler == null) {
646663
return;
647664
}
648-
if (toPause()) {
665+
if (paused()) {
649666
packageHandler.pauseSending();
650667
} else {
651668
packageHandler.resumeSending();
@@ -707,33 +724,26 @@ private void transferSessionPackage(long now) {
707724
}
708725

709726
private void startTimer() {
710-
stopTimer();
711-
712-
if (!activityState.enabled) {
727+
// don't start the timer if it's disabled/offline
728+
if (paused()) {
713729
return;
714730
}
715-
timer = Executors.newSingleThreadScheduledExecutor();
716-
timer.scheduleWithFixedDelay(new Runnable() {
717-
@Override
718-
public void run() {
719-
timerFired();
720-
}
721-
}, TIMER_START, TIMER_INTERVAL, TimeUnit.MILLISECONDS);
731+
732+
timer.start();
722733
}
723734

724735
private void stopTimer() {
725-
if (timer != null) {
726-
timer.shutdown();
727-
timer = null;
728-
}
736+
timer.suspend();
729737
}
730738

731-
private void timerFired() {
732-
if (!activityState.enabled) {
739+
private void timerFiredInternal() {
740+
if (paused()) {
741+
// stop the timer cycle if it's disabled/offline
733742
stopTimer();
734743
return;
735744
}
736745

746+
logger.debug("Session timer fired");
737747
packageHandler.sendFirstPackage();
738748

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

752-
private void writeActivityState() {
762+
private synchronized void writeActivityState() {
753763
Util.writeObject(activityState, adjustConfig.context, ACTIVITY_STATE_FILENAME, ACTIVITY_STATE_NAME);
754764
}
755765

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

774-
// lazy initialization to prevent null activity state before first session
775-
private IAttributionHandler getAttributionHandler() {
776-
if (attributionHandler == null) {
777-
ActivityPackage attributionPackage = getAttributionPackage();
778-
attributionHandler = AdjustFactory.getAttributionHandler(this,
779-
attributionPackage,
780-
toPause(),
781-
adjustConfig.hasListener());
782-
}
783-
return attributionHandler;
784-
}
785-
786-
private boolean toPause() {
784+
private boolean paused() {
787785
return offline || !isEnabled();
788786
}
789787
}

Adjust/adjust/src/main/java/com/adjust/sdk/AdjustConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private static boolean checkContext(Context context) {
9292
private static boolean checkAppToken(String appToken) {
9393
ILogger logger = AdjustFactory.getLogger();
9494
if (appToken == null) {
95-
logger.error("Missing App Token.");
95+
logger.error("Missing App Token");
9696
return false;
9797
}
9898

Adjust/adjust/src/main/java/com/adjust/sdk/AttributionHandler.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class AttributionHandler implements IAttributionHandler {
2323
private IActivityHandler activityHandler;
2424
private ILogger logger;
2525
private ActivityPackage attributionPackage;
26-
private ScheduledFuture waitingTask;
26+
private TimerOnce timer;
2727
private HttpClient httpClient;
2828
private boolean paused;
2929
private boolean hasListener;
@@ -35,6 +35,12 @@ public AttributionHandler(IActivityHandler activityHandler,
3535
scheduler = Executors.newSingleThreadScheduledExecutor();
3636
logger = AdjustFactory.getLogger();
3737
httpClient = Util.getHttpClient();
38+
timer = new TimerOnce(scheduler, new Runnable() {
39+
@Override
40+
public void run() {
41+
getAttributionInternal();
42+
}
43+
});
3844
init(activityHandler, attributionPackage, startPaused, hasListener);
3945
}
4046

@@ -74,24 +80,18 @@ public void resumeSending() {
7480
paused = false;
7581
}
7682

77-
private void getAttribution(int delayInMilliseconds) {
78-
if (waitingTask != null) {
79-
if (waitingTask.getDelay(TimeUnit.MILLISECONDS) > delayInMilliseconds) {
80-
return;
81-
}
82-
waitingTask.cancel(false);
83+
private void getAttribution(long delayInMilliseconds) {
84+
// don't reset if new time is shorter than last one
85+
if (timer.getFireIn() > delayInMilliseconds) {
86+
return;
8387
}
8488

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

89-
waitingTask = scheduler.schedule(new Runnable() {
90-
@Override
91-
public void run() {
92-
getAttributionInternal();
93-
}
94-
}, delayInMilliseconds, TimeUnit.MILLISECONDS);
93+
// set the new time the timer will fire in
94+
timer.startIn(delayInMilliseconds);
9595
}
9696

9797
private void checkAttributionInternal(JSONObject jsonResponse) {
@@ -100,7 +100,7 @@ private void checkAttributionInternal(JSONObject jsonResponse) {
100100
JSONObject attributionJson = jsonResponse.optJSONObject("attribution");
101101
AdjustAttribution attribution = AdjustAttribution.fromJson(attributionJson);
102102

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

105105
// without ask_in attribute
106106
if (timerMilliseconds < 0) {
@@ -121,7 +121,7 @@ private void getAttributionInternal() {
121121
return;
122122
}
123123
if (paused) {
124-
logger.debug("Attribution Handler is paused");
124+
logger.debug("Attribution handler is paused");
125125
return;
126126
}
127127
logger.verbose("%s", attributionPackage.getExtendedString());

Adjust/adjust/src/main/java/com/adjust/sdk/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public interface Constants {
2727
String BASE_URL = "https://app.adjust.com";
2828
String SCHEME = "https";
2929
String AUTHORITY = "app.adjust.com";
30-
String CLIENT_SDK = "android4.0.7";
30+
String CLIENT_SDK = "android4.0.8";
3131
String LOGTAG = "Adjust";
3232

3333
String ACTIVITY_STATE_FILENAME = "AdjustIoActivityState";

0 commit comments

Comments
 (0)