diff --git a/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/ComponentsImpl.java b/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/ComponentsImpl.java index ca19f5bf..286cca17 100644 --- a/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/ComponentsImpl.java +++ b/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/ComponentsImpl.java @@ -1,8 +1,5 @@ package com.launchdarkly.sdk.android; -import android.net.Uri; - -import com.launchdarkly.logging.LDLogger; import com.launchdarkly.sdk.EvaluationReason; import com.launchdarkly.sdk.LDUser; import com.launchdarkly.sdk.LDValue; @@ -131,7 +128,6 @@ static final class HttpConfigurationBuilderImpl extends HttpConfigurationBuilder implements DiagnosticDescription { @Override public HttpConfiguration build(ClientContext clientContext) { - LDLogger logger = clientContext.getBaseLogger(); // Build the default headers Map headers = new HashMap<>(); headers.put("Authorization", LDUtil.AUTH_SCHEME + clientContext.getMobileKey()); @@ -163,7 +159,7 @@ static final class PollingDataSourceBuilderImpl extends PollingDataSourceBuilder @Override public DataSource build(ClientContext clientContext) { return new DataSourceImpl(true, backgroundPollIntervalMillis, 0, - pollIntervalMillis); + pollIntervalMillis, false); } @Override @@ -200,7 +196,7 @@ static final class StreamingDataSourceBuilderImpl extends StreamingDataSourceBui @Override public DataSource build(ClientContext clientContext) { return new DataSourceImpl(false, backgroundPollIntervalMillis, - initialReconnectDelayMillis, 0); + initialReconnectDelayMillis, 0, streamEvenInBackground); } @Override @@ -218,17 +214,20 @@ private static final class DataSourceImpl implements DataSource { private final int backgroundPollIntervalMillis; private final int initialReconnectDelayMillis; private final int pollIntervalMillis; + private final boolean streamEvenInBackground; DataSourceImpl( boolean streamingDisabled, int backgroundPollIntervalMillis, int initialReconnectDelayMillis, - int pollIntervalMillis + int pollIntervalMillis, + boolean streamEvenInBackground ) { this.streamingDisabled = streamingDisabled; this.backgroundPollIntervalMillis = backgroundPollIntervalMillis; this.initialReconnectDelayMillis = initialReconnectDelayMillis; this.pollIntervalMillis = pollIntervalMillis; + this.streamEvenInBackground = streamEvenInBackground; } public boolean isStreamingDisabled() { @@ -246,5 +245,9 @@ public int getInitialReconnectDelayMillis() { public int getPollIntervalMillis() { return pollIntervalMillis; } + + public boolean isStreamEvenInBackground() { + return streamEvenInBackground; + } } } diff --git a/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/ConnectivityManager.java b/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/ConnectivityManager.java index ee271c0d..91295464 100644 --- a/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/ConnectivityManager.java +++ b/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/ConnectivityManager.java @@ -72,7 +72,12 @@ class ConnectivityManager { StandardEndpoints.selectBaseUri(ldConfig.serviceEndpoints.getStreamingBaseUri(), StandardEndpoints.DEFAULT_STREAMING_BASE_URI, "streaming", logger); - backgroundMode = ldConfig.isDisableBackgroundPolling() ? ConnectionMode.BACKGROUND_DISABLED : ConnectionMode.BACKGROUND_POLLING; + if (ldConfig.isDisableBackgroundPolling()) { + backgroundMode = ConnectionMode.BACKGROUND_DISABLED; + } else { + backgroundMode = dataSourceConfig.isStreamEvenInBackground() ? ConnectionMode.STREAMING : + ConnectionMode.BACKGROUND_POLLING; + } foregroundMode = dataSourceConfig.isStreamingDisabled() ? ConnectionMode.POLLING : ConnectionMode.STREAMING; // Currently the background polling interval is owned statically by PollingUpdater, even diff --git a/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/integrations/StreamingDataSourceBuilder.java b/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/integrations/StreamingDataSourceBuilder.java index e969dcce..8a94bc5b 100644 --- a/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/integrations/StreamingDataSourceBuilder.java +++ b/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/integrations/StreamingDataSourceBuilder.java @@ -30,6 +30,7 @@ public abstract class StreamingDataSourceBuilder implements ComponentConfigurer< protected int backgroundPollIntervalMillis = LDConfig.DEFAULT_BACKGROUND_POLL_INTERVAL_MILLIS; protected int initialReconnectDelayMillis = DEFAULT_INITIAL_RECONNECT_DELAY_MILLIS; + protected boolean streamEvenInBackground = false; /** * Sets the interval between feature flag updates when the application is running in the background. @@ -67,4 +68,24 @@ public StreamingDataSourceBuilder initialReconnectDelayMillis(int initialReconne initialReconnectDelayMillis; return this; } + + /** + * Sets whether streaming should be used even if the application is in the background. + *

+ * By default, this option is false, meaning that if the application is in the background then + * the SDK will turn off the stream connection and use infrequent polling, until the application + * is in the foreground again. + *

+ * If you set this option to {@code true}, the SDK will continue to use the stream connection + * regardless of foreground/background state. Use this option with caution, since normally it is + * preferable to limit network usage by backgrounded applications. + * + * @param streamEvenInBackground true if streaming should be used even in the background + * @return the builder + * @since 3.4.0 + */ + public StreamingDataSourceBuilder streamEvenInBackground(boolean streamEvenInBackground) { + this.streamEvenInBackground = streamEvenInBackground; + return this; + } } diff --git a/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/subsystems/DataSource.java b/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/subsystems/DataSource.java index 24427e82..0b88e9e4 100644 --- a/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/subsystems/DataSource.java +++ b/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/subsystems/DataSource.java @@ -39,4 +39,14 @@ public interface DataSource { * @return the foreground polling interval in milliseconds, or zero if streaming is enabled */ int getPollIntervalMillis(); + + /** + * Returns the option set by + * {@link com.launchdarkly.sdk.android.integrations.StreamingDataSourceBuilder#streamEvenInBackground(boolean)}. + * @return true if streaming mode can continue in the background (if not disabled) + * @since 3.4.0 + */ + default boolean isStreamEvenInBackground() { + return false; + } }