diff --git a/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/Analytics.java b/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/Analytics.java index 611bda1a2..8873c2a96 100644 --- a/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/Analytics.java +++ b/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/Analytics.java @@ -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. */ @@ -358,6 +363,22 @@ public static void trackEvent(String name, Map 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. *

@@ -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). */ @@ -794,6 +818,32 @@ private void queuePage(String name, Map 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. * diff --git a/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/channel/SessionTracker.java b/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/channel/SessionTracker.java index a066244f6..20c6d6f68 100644 --- a/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/channel/SessionTracker.java +++ b/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/channel/SessionTracker.java @@ -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. */ @@ -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 @@ -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(); } @@ -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(); } @@ -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)); + } }