Skip to content

Commit

Permalink
Fixing apm foreground/background tracking bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtursKadikis committed Sep 18, 2023
1 parent 64eddc0 commit 1b9eb36
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 23.8.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

## 23.8.1
* Adding an additional testing call for retrieving AB test experiment information

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ org.gradle.configureondemand=true
android.useAndroidX=true
android.enableJetifier=true
# RELEASE FIELD SECTION
VERSION_NAME=23.8.1
VERSION_NAME=23.8.2
GROUP=ly.count.android
POM_URL=https://github.com/Countly/countly-sdk-android
POM_SCM_URL=https://github.com/Countly/countly-sdk-android
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ public void testPrepareCommonRequest() {
break;
case "sdk_version":
if (a == 0) {
Assert.assertTrue(pair[1].equals("23.8.1"));
Assert.assertTrue(pair[1].equals("23.8.2"));
} else if (a == 1) {
Assert.assertTrue(pair[1].equals("123sdf.v-213"));
}
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/main/java/ly/count/android/sdk/Countly.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ of this software and associated documentation files (the "Software"), to deal
*/
public class Countly {

private final String DEFAULT_COUNTLY_SDK_VERSION_STRING = "23.8.1";
private final String DEFAULT_COUNTLY_SDK_VERSION_STRING = "23.8.2";

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

//set global application listeners
if (config.application != null) {
L.d("[Countly] Calling registerActivityLifecycleCallbacks");
config.application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
Expand Down
19 changes: 13 additions & 6 deletions sdk/src/main/java/ly/count/android/sdk/ModuleAPM.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ public class ModuleAPM extends ModuleBase {

activitiesOpen = 0;

if (_cly.lifecycleStateAtLeastStarted()) {
L.d("[ModuleAPM] SDK detects that the app is in the foreground. Increasing the activity counter.");
activitiesOpen++;
}

useManualAppLoadedTrigger = config.appLoadedManualTrigger;
if (config.appStartTimestampOverride != null) {
//if there is a app start time override, use it
Expand Down Expand Up @@ -353,7 +348,7 @@ void calculateAppRunningTimes(int previousCount, int newCount) {
boolean goingToBackground = (previousCount == 1 && newCount == 0);
boolean goingToForeground = (previousCount == 0 && newCount == 1);

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

doForegroundBackgroundCalculations(goingToBackground, goingToForeground);
}
Expand Down Expand Up @@ -462,6 +457,18 @@ void onConsentChanged(@NonNull final List<String> consentChangeDelta, final bool
}
}

@Override
void initFinished(@NonNull CountlyConfig config) {
// we only do this adjustment if we track it automatically
if (!manualForegroundBackgroundTriggers && _cly.lifecycleStateAtLeastStarted()) {
L.d("[ModuleAPM] SDK detects that the app is in the foreground. Increasing the activity counter.");

calculateAppRunningTimes(activitiesOpen, activitiesOpen + 1);

activitiesOpen++;
}
}

public class Apm {
/**
* Start a trace of a action you want to track
Expand Down

0 comments on commit 1b9eb36

Please sign in to comment.