Skip to content

Commit

Permalink
Add draft set sessionId implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Anastasia Senyushina committed Nov 10, 2021
1 parent 04d60ae commit 9ba1247
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public class Analytics extends AbstractAppCenterService {
*/
private boolean mAutoPageTrackingEnabled = false;

/**
* Stores the value of whether automatic session generation value was set, null by default.
*/
private Boolean isAutomaticSessionGenerationDisabled = null;

/**
* Init.
*/
Expand Down Expand Up @@ -358,6 +363,22 @@ public static void trackEvent(String name, Map<String, String> properties) {
getInstance().trackEventAsync(name, convertProperties(properties), null, Flags.DEFAULTS);
}

/**
* Disable automatic session generation.
*
* @param isDisabled true - if automatic session generation should be disabled, otherwise false.
*/
public static void disableAutomaticSessionGeneration(boolean isDisabled) {
getInstance().disableAutomaticSessionGenerationAsync(isDisabled);
}

/**
* Start a new session if automatic session generation was disabled.
*/
public static void startSession() {
getInstance().startSessionAsync();
}

/**
* Track a custom event with name and optional string properties.
* <p>
Expand Down Expand Up @@ -742,6 +763,9 @@ private void startAppLevelFeatures() {

/* Start session tracker. */
mSessionTracker = new SessionTracker(mChannel, ANALYTICS_GROUP);
if(isAutomaticSessionGenerationDisabled != null) {
mSessionTracker.disableAutomaticSessionGeneration(isAutomaticSessionGenerationDisabled);
}
mChannel.addListener(mSessionTracker);

/* If we are in foreground, make sure we send start session log now (and track page). */
Expand Down Expand Up @@ -794,6 +818,32 @@ private void queuePage(String name, Map<String, String> properties) {
mChannel.enqueue(pageLog, ANALYTICS_GROUP, Flags.DEFAULTS);
}

/**
* Implements {@link #disableAutomaticSessionGeneration(boolean)}.
*/
private synchronized void disableAutomaticSessionGenerationAsync(boolean isDisabled) {
if (mChannel != null) {
AppCenterLog.error(AppCenterLog.LOG_TAG, "The automatic session generation value should be installed before the App Center start.");
return;
}
if (mSessionTracker == null) {
isAutomaticSessionGenerationDisabled = isDisabled;
return;
}
mSessionTracker.disableAutomaticSessionGeneration(isDisabled);
}

/**
* Implements {@link #startSession()}.
*/
private synchronized void startSessionAsync() {
if (mSessionTracker != null) {
mSessionTracker.startSession();
} else {
AppCenterLog.debug(AppCenterLog.LOG_TAG, "Start session should be called after the App Center start.");
}
}

/**
* Send an event.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class SessionTracker extends AbstractChannelListener {
*/
private final Channel mChannel;

/**
* Stores the value of whether automatic session generation is enabled, false by default.
*/
private boolean isAutomaticSessionGenerationDisabled;

/**
* Group name used to send generated logs.
*/
Expand Down Expand Up @@ -110,6 +115,16 @@ public void onPreparingLog(@NonNull Log log, @NonNull String groupName) {
}
}

/**
* Disable automatic session generation.
*
* @param isDisabled true - if automatic session generation should be disabled, otherwise false.
*/
public void disableAutomaticSessionGeneration(boolean isDisabled) {
isAutomaticSessionGenerationDisabled = isDisabled;
AppCenterLog.debug(Analytics.LOG_TAG, String.format("Automatic session generation is %s.", isDisabled ? "disabled" : "enabled"));
}

/**
* Generate a new session identifier if the first time or
* we went in background for more X seconds before resume or
Expand All @@ -123,33 +138,43 @@ public void onPreparingLog(@NonNull Log log, @NonNull String groupName) {
private void sendStartSessionIfNeeded() {
if (mSid == null || hasSessionTimedOut()) {

/* New session: generate a new identifier. */
mSid = UUID.randomUUID();

/* Update session storage. */
SessionContext.getInstance().addSession(mSid);

/*
* Record queued time for the session log itself to avoid double log if resuming
* from background after timeout and sending a log at same time we resume like a page.
*/
mLastQueuedLogTime = SystemClock.elapsedRealtime();

/* Enqueue a start session log. */
StartSessionLog startSessionLog = new StartSessionLog();
startSessionLog.setSid(mSid);
mChannel.enqueue(startSessionLog, mGroupName, Flags.DEFAULTS);
/* Generate and send start session log. */
sendStartSession();
}
}

/**
* Generate session and send start session log.
*/
private void sendStartSession() {
mSid = UUID.randomUUID();

/* Update session storage. */
SessionContext.getInstance().addSession(mSid);

/* Enqueue a start session log. */
StartSessionLog startSessionLog = new StartSessionLog();
startSessionLog.setSid(mSid);
mChannel.enqueue(startSessionLog, mGroupName, Flags.DEFAULTS);
}

/**
* Call this whenever an activity is resumed to update session tracker state.
*/
@WorkerThread
public void onActivityResumed() {
if (isAutomaticSessionGenerationDisabled) {
AppCenterLog.debug(Analytics.LOG_TAG, "Automatic session generation is disabled. Skip tracking a session status request.");
return;
}

/* Record resume time for session timeout management. */
AppCenterLog.debug(Analytics.LOG_TAG, "onActivityResumed");
mLastResumedTime = SystemClock.elapsedRealtime();
sendStartSessionIfNeeded();
}
Expand All @@ -159,9 +184,12 @@ public void onActivityResumed() {
*/
@WorkerThread
public void onActivityPaused() {
if (isAutomaticSessionGenerationDisabled) {
AppCenterLog.debug(Analytics.LOG_TAG, "Automatic session generation is disabled. Skip tracking a session status request.");
return;
}

/* Record pause time for session timeout management. */
AppCenterLog.debug(Analytics.LOG_TAG, "onActivityPaused");
mLastPausedTime = SystemClock.elapsedRealtime();
}

Expand Down Expand Up @@ -195,4 +223,16 @@ private boolean hasSessionTimedOut() {
AppCenterLog.debug(Analytics.LOG_TAG, "noLogSentForLong=" + noLogSentForLong + " wasBackgroundForLong=" + wasBackgroundForLong);
return noLogSentForLong && wasBackgroundForLong;
}

/**
* Start a new session if automatic session generation was disabled, otherwise nothing.
*/
public void startSession() {
if(!isAutomaticSessionGenerationDisabled) {
AppCenterLog.debug(Analytics.LOG_TAG, "Automatic session generation is enabled. Skip start a new session request.");
return;
}
sendStartSession();
AppCenterLog.debug(Analytics.LOG_TAG, String.format("Start a new session with id: %s.", mSid));
}
}

0 comments on commit 9ba1247

Please sign in to comment.