Skip to content

Commit

Permalink
Added manual foreground/background triggers for APM
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtursKadikis committed Nov 26, 2020
1 parent 5eebe25 commit 8f4c828
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Added a default way to acquire app start timestamp for APM
* Added a way to override the current app start timestamp for APM
* Added manual trigger for when app has finished loading for APM
* Added manual foreground/background triggers for APM

## 20.11.0
* !! Consent change !! To record orientation you now need to give "user" consent
Expand Down
11 changes: 11 additions & 0 deletions sdk/src/main/java/ly/count/android/sdk/CountlyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public class CountlyConfig {

boolean appLoadedManualTrigger = false;

boolean manualForegroundBackgroundTrigger = false;

public CountlyConfig() {
}

Expand Down Expand Up @@ -552,4 +554,13 @@ public synchronized CountlyConfig enableManualAppLoadedTrigger() {
appLoadedManualTrigger = true;
return this;
}

/**
* Set this in case you want to control these triggers manually
* @return
*/
public synchronized CountlyConfig enableManualForegroundBackgroundTriggerAPM() {
manualForegroundBackgroundTrigger = true;
return this;
}
}
74 changes: 71 additions & 3 deletions sdk/src/main/java/ly/count/android/sdk/ModuleAPM.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ly.count.android.sdk;

import android.app.Activity;
import android.os.SystemClock;
import android.util.Log;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -27,6 +26,9 @@ public class ModuleAPM extends ModuleBase {
boolean useManualAppLoadedTrigger = false;
long appStartTimestamp;

boolean manualForegroundBackgroundTriggers = false;
boolean manualOverrideInForeground = false;//app starts in background

ModuleAPM(Countly cly, CountlyConfig config) {
super(cly);

Expand Down Expand Up @@ -56,6 +58,11 @@ public class ModuleAPM extends ModuleBase {
Log.d(Countly.TAG, "[ModuleAPM] Using manual app finished loading trigger for app start");
}

manualForegroundBackgroundTriggers = config.manualForegroundBackgroundTrigger;
if (_cly.isLoggingEnabled() && manualForegroundBackgroundTriggers) {
Log.d(Countly.TAG, "[ModuleAPM] Using manual foreground/background triggers");
}

apmInterface = new Apm();
}

Expand Down Expand Up @@ -411,7 +418,12 @@ void calculateAppRunningTimes(int previousCount, int newCount) {
Log.v(Countly.TAG, "[ModuleAPM] calculateAppRunningTimes, toBG[" + goingToBackground + "] toFG[" + goingToForeground + "]");
}

doForegroundBackgroundCalculations(goingToBackground, goingToForeground);
}

void doForegroundBackgroundCalculations(boolean goingToBackground, boolean goingToForeground) {
if (goingToBackground || goingToForeground) {

long currentTimeMs = UtilsTime.currentTimestampMs();

if (lastScreenSwitchTime != -1) {
Expand All @@ -433,6 +445,24 @@ void calculateAppRunningTimes(int previousCount, int newCount) {
}
}

void goToForeground() {
if(manualOverrideInForeground) {
//if we already are in foreground, do nothing
return;
}
manualOverrideInForeground = true;
doForegroundBackgroundCalculations(false, true);
}

void goToBackground() {
if(!manualOverrideInForeground) {
//if we already are in background, do nothing
return;
}
manualOverrideInForeground = false;
doForegroundBackgroundCalculations(true, false);
}

@Override
void halt() {
codeTraces = null;
Expand All @@ -452,7 +482,9 @@ void callbackOnActivityResumed(Activity activity) {

Long currentTimestamp = System.currentTimeMillis();

calculateAppRunningTimes(activitiesOpen, activitiesOpen + 1);
if(!manualForegroundBackgroundTriggers) {
calculateAppRunningTimes(activitiesOpen, activitiesOpen + 1);
}
activitiesOpen++;

if (!hasFirstOnResumeHappened) {
Expand All @@ -474,7 +506,9 @@ void callbackOnActivityStopped(Activity activity) {
Log.d(Countly.TAG, "[Apm] Calling 'callbackOnActivityStopped', [" + activitiesOpen + "] -> [" + (activitiesOpen - 1) + "]");
}

calculateAppRunningTimes(activitiesOpen, activitiesOpen - 1);
if(!manualForegroundBackgroundTriggers) {
calculateAppRunningTimes(activitiesOpen, activitiesOpen - 1);
}
activitiesOpen--;
}

Expand Down Expand Up @@ -609,5 +643,39 @@ public void setAppIsLoaded() {
recordAppStart(timestamp);
}
}

public void triggerForeground() {
synchronized (_cly) {
if (_cly.isLoggingEnabled()) {
Log.i(Countly.TAG, "[Apm] Calling 'triggerForeground'");
}

if(!manualForegroundBackgroundTriggers) {
if (_cly.isLoggingEnabled()) {
Log.w(Countly.TAG, "[Apm] trying to use manual foreground/background triggers without enabling them");
}
return;
}

goToForeground();
}
}

public void triggerBackground() {
synchronized (_cly) {
if (_cly.isLoggingEnabled()) {
Log.i(Countly.TAG, "[Apm] Calling 'triggerBackground'");
}

if(!manualForegroundBackgroundTriggers) {
if (_cly.isLoggingEnabled()) {
Log.w(Countly.TAG, "[Apm] trying to use manual foreground/background triggers without enabling them");
}
return;
}

goToBackground();
}
}
}
}

0 comments on commit 8f4c828

Please sign in to comment.