2323import org .json .JSONObject ;
2424
2525import java .lang .ref .WeakReference ;
26- import java .util .HashMap ;
2726import java .util .LinkedHashMap ;
2827import java .util .List ;
2928import java .util .Map ;
30- import java .util .concurrent .Executors ;
31- import java .util .concurrent .ScheduledExecutorService ;
32- import java .util .concurrent .TimeUnit ;
3329
3430import static com .adjust .sdk .Constants .ACTIVITY_STATE_FILENAME ;
3531import 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}
0 commit comments