Skip to content

Commit 1b9eb36

Browse files
committed
Fixing apm foreground/background tracking bug
1 parent 64eddc0 commit 1b9eb36

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 23.8.2
2+
* Fixed APM bug where automatic foreground, background tracking would track wrong if the SDK was not initialized while the app was not in the foreground
3+
14
## 23.8.1
25
* Adding an additional testing call for retrieving AB test experiment information
36

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ org.gradle.configureondemand=true
2222
android.useAndroidX=true
2323
android.enableJetifier=true
2424
# RELEASE FIELD SECTION
25-
VERSION_NAME=23.8.1
25+
VERSION_NAME=23.8.2
2626
GROUP=ly.count.android
2727
POM_URL=https://github.com/Countly/countly-sdk-android
2828
POM_SCM_URL=https://github.com/Countly/countly-sdk-android

sdk/src/androidTest/java/ly/count/android/sdk/ConnectionQueueTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ public void testPrepareCommonRequest() {
502502
break;
503503
case "sdk_version":
504504
if (a == 0) {
505-
Assert.assertTrue(pair[1].equals("23.8.1"));
505+
Assert.assertTrue(pair[1].equals("23.8.2"));
506506
} else if (a == 1) {
507507
Assert.assertTrue(pair[1].equals("123sdf.v-213"));
508508
}

sdk/src/main/java/ly/count/android/sdk/Countly.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ of this software and associated documentation files (the "Software"), to deal
4949
*/
5050
public class Countly {
5151

52-
private final String DEFAULT_COUNTLY_SDK_VERSION_STRING = "23.8.1";
52+
private final String DEFAULT_COUNTLY_SDK_VERSION_STRING = "23.8.2";
5353

5454
/**
5555
* Used as request meta data on every request
@@ -664,6 +664,7 @@ public synchronized Countly init(CountlyConfig config) {
664664

665665
//set global application listeners
666666
if (config.application != null) {
667+
L.d("[Countly] Calling registerActivityLifecycleCallbacks");
667668
config.application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
668669
@Override
669670
public void onActivityCreated(Activity activity, Bundle bundle) {

sdk/src/main/java/ly/count/android/sdk/ModuleAPM.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ public class ModuleAPM extends ModuleBase {
3939

4040
activitiesOpen = 0;
4141

42-
if (_cly.lifecycleStateAtLeastStarted()) {
43-
L.d("[ModuleAPM] SDK detects that the app is in the foreground. Increasing the activity counter.");
44-
activitiesOpen++;
45-
}
46-
4742
useManualAppLoadedTrigger = config.appLoadedManualTrigger;
4843
if (config.appStartTimestampOverride != null) {
4944
//if there is a app start time override, use it
@@ -353,7 +348,7 @@ void calculateAppRunningTimes(int previousCount, int newCount) {
353348
boolean goingToBackground = (previousCount == 1 && newCount == 0);
354349
boolean goingToForeground = (previousCount == 0 && newCount == 1);
355350

356-
L.v("[ModuleAPM] calculateAppRunningTimes, toBG[" + goingToBackground + "] toFG[" + goingToForeground + "]");
351+
L.v("[ModuleAPM] calculateAppRunningTimes, going toBG[" + goingToBackground + "] going toFG[" + goingToForeground + "] | [" + previousCount + "][" + newCount + "]");
357352

358353
doForegroundBackgroundCalculations(goingToBackground, goingToForeground);
359354
}
@@ -462,6 +457,18 @@ void onConsentChanged(@NonNull final List<String> consentChangeDelta, final bool
462457
}
463458
}
464459

460+
@Override
461+
void initFinished(@NonNull CountlyConfig config) {
462+
// we only do this adjustment if we track it automatically
463+
if (!manualForegroundBackgroundTriggers && _cly.lifecycleStateAtLeastStarted()) {
464+
L.d("[ModuleAPM] SDK detects that the app is in the foreground. Increasing the activity counter.");
465+
466+
calculateAppRunningTimes(activitiesOpen, activitiesOpen + 1);
467+
468+
activitiesOpen++;
469+
}
470+
}
471+
465472
public class Apm {
466473
/**
467474
* Start a trace of a action you want to track

0 commit comments

Comments
 (0)