Class LDClient
- All Implemented Interfaces:
LDClientInterface
,Closeable
,AutoCloseable
init(Application, LDConfig, LDContext)
method.
Like all client-side LaunchDarkly SDKs, the LDClient
always has a single current
LDContext
(evaluation context). You specify this context at initialization time, and you
can change it later with identify(LDContext)
. All subsequent calls to evaluation
methods like boolVariation(java.lang.String, boolean)
refer to the flag values for the current context.
Normally, the SDK uses the exact context that you have specified in th LDContext
.
However, you can also tell the SDK to generate a randomized identifier and use this as the
context's key
; see LDConfig.Builder.generateAnonymousKeys(boolean)
.
-
Constructor Summary
ModifierConstructorDescriptionprotected
LDClient
(com.launchdarkly.sdk.android.PlatformState platformState, IEnvironmentReporter environmentReporter, com.launchdarkly.sdk.android.TaskExecutor taskExecutor, com.launchdarkly.sdk.android.PersistentDataStoreWrapper.PerEnvironmentData environmentStore, LDContext initialContext, LDConfig config, String mobileKey, String environmentName) -
Method Summary
Modifier and TypeMethodDescriptionallFlags()
Returns a map of all feature flags for the current evaluation context.boolean
boolVariation
(String key, boolean defaultValue) Returns the boolean value of a feature flag for the current evaluation context.boolVariationDetail
(String key, boolean defaultValue) Returns the boolean value of a feature flag for the current evaluation context, along with information about how it was calculated.void
close()
Closes the client.double
doubleVariation
(String flagKey, double defaultValue) Returns the double-precision floating-point numeric value of a feature flag for the current evaluation context.doubleVariationDetail
(String flagKey, double defaultValue) Returns the double-precision floating-point numeric value of a feature flag for the current evaluation context, along with information about how it was calculated.void
flush()
Sends all pending events to LaunchDarkly.static LDClient
get()
Returns theLDClient
instance that was previously created withinit(Application, LDConfig, LDContext, int)
orinit(Application, LDConfig, LDContext)
.Gets aConnectionInformation
object from the client representing the current state of the clients connection.static LDClient
getForMobileKey
(String keyName) Returns theLDClient
instance that was previously created withinit(Application, LDConfig, LDContext, int)
orinit(Application, LDConfig, LDContext)
, for a specific environment.Returns the version of the SDK, for instance "2.7.0".Changes the current evaluation context, requests flags for that context from LaunchDarkly if we are online, and generates an analytics event to tell LaunchDarkly about the context.Deprecated.static LDClient
Initializes the singleton instance and blocks for up tostartWaitSeconds
seconds until the client has been initialized.int
intVariation
(String key, int defaultValue) Returns the integer value of a feature flag for the current evaluation context.intVariationDetail
(String key, int defaultValue) Returns the integer value of a feature flag for the current evaluation context, along with information about how it was calculated.boolean
Checks whetherLDConfig.Builder.disableBackgroundUpdating(boolean)
was set totrue
in the configuration.boolean
Checks whether the client is ready to return feature flag values.boolean
Checks whether the client has been put into offline mode.jsonValueVariation
(String key, LDValue defaultValue) Returns the value of a feature flag for the current evaluation context, which may be of any type.jsonValueVariationDetail
(String key, LDValue defaultValue) Returns the value of a feature flag for the current evaluation context, which may be of any type, along with information about how it was calculated.void
registerAllFlagsListener
(LDAllFlagsListener allFlagsListener) Registers aLDAllFlagsListener
to be called when a flag update is processed by the SDK.void
registerFeatureFlagListener
(String flagKey, FeatureFlagChangeListener listener) Registers aFeatureFlagChangeListener
to be called when theflagKey
changes from its current value.void
registerStatusListener
(LDStatusListener statusListener) Registers aLDStatusListener
to be called on connection status updates.void
Shuts down any network connections maintained by the client and puts the client in offline mode, preventing the client from opening new network connections untilsetOnline()
is called.void
Restores network connectivity for the client, if the client was previously in offline mode.stringVariation
(String key, String defaultValue) Returns the string value of a feature flag for the current evaluation context.stringVariationDetail
(String key, String defaultValue) Returns the string value of a feature flag for the current evaluation context, along with information about how it was calculated.void
Tracks that an application-defined event occurred.void
Tracks that an application-defined event occurred, and provides additional custom data.void
trackMetric
(String eventName, LDValue data, double metricValue) Tracks that an application-defined event occurred, and provides an additional numeric value for custom metrics.void
unregisterAllFlagsListener
(LDAllFlagsListener allFlagsListener) Unregisters aLDAllFlagsListener
so it will no longer be called on flag updates.void
unregisterFeatureFlagListener
(String flagKey, FeatureFlagChangeListener listener) Unregisters aFeatureFlagChangeListener
for theflagKey
.void
unregisterStatusListener
(LDStatusListener statusListener) Unregisters aLDStatusListener
so it will no longer be called on connection status updates.
-
Constructor Details
-
LDClient
protected LDClient(@NonNull com.launchdarkly.sdk.android.PlatformState platformState, @NonNull IEnvironmentReporter environmentReporter, @NonNull com.launchdarkly.sdk.android.TaskExecutor taskExecutor, @NonNull com.launchdarkly.sdk.android.PersistentDataStoreWrapper.PerEnvironmentData environmentStore, @NonNull LDContext initialContext, @NonNull LDConfig config, @NonNull String mobileKey, @NonNull String environmentName) throws LaunchDarklyException - Parameters:
platformState
- the platform stateenvironmentReporter
- the environment reportertaskExecutor
- the task executorenvironmentStore
- the environment storeinitialContext
- initial contextconfig
- the configmobileKey
- the mobile keyenvironmentName
- the environment name- Throws:
LaunchDarklyException
- ifLDClient
cannot be created
-
-
Method Details
-
init
@Deprecated public static Future<LDClient> init(@NonNull android.app.Application application, @NonNull LDConfig config, @NonNull LDContext context) Deprecated.Initializing theLDClient
without a timeout is no longer permitted to help prevent consumers from blocking their application execution by mistake when connectivity is poor. Please useinit(Application, LDConfig, LDContext, int)
and specify a timeout instead.Initializes the singleton/primary instance. The result is aFuture
which will complete once the client has been initialized with the latest feature flag values. For immediate access to the Client (possibly with out of date feature flags), it is safe to ignore the return value of this method, and afterward callget()
If the client has already been initialized, is configured for offline mode, or the device is not connected to the internet, this method will return a
Future
that is already in the completed state.- Parameters:
application
- your Android applicationconfig
- configuration used to set up the clientcontext
- the initial evaluation context; seeLDClient
for more information about setting the context and optionally requesting a unique key for it- Returns:
- a
Future
which will complete once the client has been initialized - Since:
- 3.0.0
- See Also:
-
init
public static LDClient init(android.app.Application application, LDConfig config, LDContext context, int startWaitSeconds) Initializes the singleton instance and blocks for up tostartWaitSeconds
seconds until the client has been initialized. If the client does not initialize withinstartWaitSeconds
seconds, it is returned anyway and can be used, but may not have fetched the most recent feature flag values.- Parameters:
application
- your Android applicationconfig
- configuration used to set up the clientcontext
- the initial evaluation context; seeLDClient
for more information about setting the context and optionally requesting a unique key for itstartWaitSeconds
- maximum number of seconds to wait for the client to initialize. Determines when this function will return if no flags have been fetched. If you use a large timeout, then any network delays could cause your application to wait a long time before continuing execution.- Returns:
- the primary LDClient instance
- Since:
- 3.0.0
- See Also:
-
get
Returns theLDClient
instance that was previously created withinit(Application, LDConfig, LDContext, int)
orinit(Application, LDConfig, LDContext)
.If you have configured multiple environments, this method returns the instance for the primary environment.
- Returns:
- the singleton instance
- Throws:
LaunchDarklyException
- ifinit
has not been called- See Also:
-
getForMobileKey
Returns theLDClient
instance that was previously created withinit(Application, LDConfig, LDContext, int)
orinit(Application, LDConfig, LDContext)
, for a specific environment.This method is only relevant if you have configured multiple environments with
LDConfig.Builder.secondaryMobileKeys(Map)
.- Parameters:
keyName
- the name you gave to this environment (this must be one of the keys in the map you passed toLDConfig.Builder.secondaryMobileKeys(Map)
)- Returns:
- the singleton instance for the environment associated with the given name
- Throws:
LaunchDarklyException
- ifinit
has not been called- See Also:
-
trackMetric
Description copied from interface:LDClientInterface
Tracks that an application-defined event occurred, and provides an additional numeric value for custom metrics.This method creates a "custom" analytics event containing the specified event name (key) the current evaluation context, optional custom data, and a numeric metric value.
Note that event delivery is asynchronous, so the event may not actually be sent until later; see
LDClientInterface.flush()
.- Specified by:
trackMetric
in interfaceLDClientInterface
- Parameters:
eventName
- the name of the eventdata
- anLDValue
containing additional data associated with the event; if not applicable, you may pass eithernull
orLDValue.ofNull()
metricValue
- a numeric value used by the LaunchDarkly experimentation feature in numeric custom metrics; this field will also be returned as part of the custom event for Data Export- See Also:
-
trackData
Description copied from interface:LDClientInterface
Tracks that an application-defined event occurred, and provides additional custom data.This method creates a "custom" analytics event containing the specified event name (key) the current evaluation context, and optional custom data. To specify a numeric metric, use
LDClientInterface.trackMetric(String, LDValue, double)
instead.Note that event delivery is asynchronous, so the event may not actually be sent until later; see
LDClientInterface.flush()
.- Specified by:
trackData
in interfaceLDClientInterface
- Parameters:
eventName
- the name of the eventdata
- anLDValue
containing additional data associated with the event; if not applicable, you may pass eithernull
orLDValue.ofNull()
- See Also:
-
track
Description copied from interface:LDClientInterface
Tracks that an application-defined event occurred.This method creates a "custom" analytics event containing the specified event name (key) and the current evaluation context. You may attach other data to the event by calling
LDClientInterface.trackData(String, LDValue)
orLDClientInterface.trackMetric(String, LDValue, double)
instead.Note that event delivery is asynchronous, so the event may not actually be sent until later; see
LDClientInterface.flush()
.- Specified by:
track
in interfaceLDClientInterface
- Parameters:
eventName
- the name of the event- See Also:
-
identify
Description copied from interface:LDClientInterface
Changes the current evaluation context, requests flags for that context from LaunchDarkly if we are online, and generates an analytics event to tell LaunchDarkly about the context.If the SDK is online, the returned
Future
is completed once the SDK has received feature flag values for the new context from LaunchDarkly, or received an unrecoverable error. If the SDK is offline, the returnedFuture
is completed immediately.The SDK normally caches flag settings for recently used evaluation contexts; this behavior can be configured with
LDConfig.Builder.maxCachedContexts(int)
.- Specified by:
identify
in interfaceLDClientInterface
- Parameters:
context
- the new evaluation context; seeLDClient
for more about setting the context and optionally requesting a unique key for it- Returns:
- a Future whose success indicates the flag values for the new evaluation context have been stored locally and are ready for use
-
allFlags
Description copied from interface:LDClientInterface
Returns a map of all feature flags for the current evaluation context. No events are sent to LaunchDarkly.- Specified by:
allFlags
in interfaceLDClientInterface
- Returns:
- a map of all feature flags
-
boolVariation
Description copied from interface:LDClientInterface
Returns the boolean value of a feature flag for the current evaluation context.If the flag variation does not have a boolean value, or if an error makes it impossible to evaluate the flag (for instance, if
flagKey
does not match any existing flag),defaultValue
is returned.- Specified by:
boolVariation
in interfaceLDClientInterface
- Parameters:
key
- key for the flag to evaluatedefaultValue
- default value in case of errors evaluating the flag- Returns:
- value of the flag or the default value
-
boolVariationDetail
Description copied from interface:LDClientInterface
Returns the boolean value of a feature flag for the current evaluation context, along with information about how it was calculated.Note that this will only work if you have set
evaluationReasons
to true withLDConfig.Builder.evaluationReasons(boolean)
. Otherwise, thereason
property of the result will be null.The evaluation reason will also be included in analytics events, if you are capturing detailed event data for this flag.
- Specified by:
boolVariationDetail
in interfaceLDClientInterface
- Parameters:
key
- key for the flag to evaluatedefaultValue
- default value in case of errors evaluating the flag (seeLDClientInterface.boolVariation(String, boolean)
)- Returns:
- an
EvaluationDetail
object containing the value and other information
-
intVariation
Description copied from interface:LDClientInterface
Returns the integer value of a feature flag for the current evaluation context.If the flag variation has a numeric value that is not an integer, it is rounded toward zero.
If the flag variation does not have a numeric value, or if an error makes it impossible to evaluate the flag (for instance, if
flagKey
does not match any existing flag),defaultValue
is returned.- Specified by:
intVariation
in interfaceLDClientInterface
- Parameters:
key
- key for the flag to evaluatedefaultValue
- default value in case of errors evaluating the flag- Returns:
- value of the flag or the default value
-
intVariationDetail
Description copied from interface:LDClientInterface
Returns the integer value of a feature flag for the current evaluation context, along with information about how it was calculated.Note that this will only work if you have set
evaluationReasons
to true withLDConfig.Builder.evaluationReasons(boolean)
. Otherwise, thereason
property of the result will be null.The evaluation reason will also be included in analytics events, if you are capturing detailed event data for this flag.
The behavior is otherwise identical to
LDClientInterface.intVariation(java.lang.String, int)
.- Specified by:
intVariationDetail
in interfaceLDClientInterface
- Parameters:
key
- key for the flag to evaluatedefaultValue
- default value in case of errors evaluating the flag (seeLDClientInterface.intVariation(String, int)
)- Returns:
- an
EvaluationDetail
object containing the value and other information
-
doubleVariation
Description copied from interface:LDClientInterface
Returns the double-precision floating-point numeric value of a feature flag for the current evaluation context.If the flag variation does not have a numeric value, or if an error makes it impossible to evaluate the flag (for instance, if
flagKey
does not match any existing flag),defaultValue
is returned.- Specified by:
doubleVariation
in interfaceLDClientInterface
- Parameters:
flagKey
- key for the flag to evaluatedefaultValue
- default value in case of errors evaluating the flag- Returns:
- value of the flag or the default value
-
doubleVariationDetail
Description copied from interface:LDClientInterface
Returns the double-precision floating-point numeric value of a feature flag for the current evaluation context, along with information about how it was calculated. Note that this will only work if you have setevaluationReasons
to true withLDConfig.Builder.evaluationReasons(boolean)
. Otherwise, thereason
property of the result will be null.The evaluation reason will also be included in analytics events, if you are capturing detailed event data for this flag.
The behavior is otherwise identical to
LDClientInterface.doubleVariation(java.lang.String, double)
.- Specified by:
doubleVariationDetail
in interfaceLDClientInterface
- Parameters:
flagKey
- key for the flag to evaluatedefaultValue
- default value in case of errors evaluating the flag (seeLDClientInterface.doubleVariation(String, double)
)- Returns:
- an
EvaluationDetail
object containing the value and other information.
-
stringVariation
Description copied from interface:LDClientInterface
Returns the string value of a feature flag for the current evaluation context.If the flag variation does not have a string value, or if an error makes it impossible to evaluate the flag (for instance, if
flagKey
does not match any existing flag),defaultValue
is returned.- Specified by:
stringVariation
in interfaceLDClientInterface
- Parameters:
key
- key for the flag to evaluatedefaultValue
- default value in case of errors evaluating the flag- Returns:
- value of the flag or the default value
-
stringVariationDetail
Description copied from interface:LDClientInterface
Returns the string value of a feature flag for the current evaluation context, along with information about how it was calculated.Note that this will only work if you have set
evaluationReasons
to true withLDConfig.Builder.evaluationReasons(boolean)
. Otherwise, thereason
property of the result will be null.The evaluation reason will also be included in analytics events, if you are capturing detailed event data for this flag.
The behavior is otherwise identical to
LDClientInterface.stringVariation(java.lang.String, java.lang.String)
.- Specified by:
stringVariationDetail
in interfaceLDClientInterface
- Parameters:
key
- key for the flag to evaluatedefaultValue
- default value in case of errors evaluating the flag (seeLDClientInterface.stringVariation(String, String)
)- Returns:
- an
EvaluationDetail
object containing the value and other information.
-
jsonValueVariation
Description copied from interface:LDClientInterface
Returns the value of a feature flag for the current evaluation context, which may be of any type.The type
LDValue
is used to represent any of the value types that can exist in JSON. UseLDValue
methods to examine its type and value.- Specified by:
jsonValueVariation
in interfaceLDClientInterface
- Parameters:
key
- key for the flag to evaluatedefaultValue
- default value in case of errors evaluating the flag- Returns:
- value of the flag or the default value. Result will never be null, but may be LDValue#ofNull()
-
jsonValueVariationDetail
public EvaluationDetail<LDValue> jsonValueVariationDetail(@NonNull String key, LDValue defaultValue) Description copied from interface:LDClientInterface
Returns the value of a feature flag for the current evaluation context, which may be of any type, along with information about how it was calculated.The type
LDValue
is used to represent any of the value types that can exist in JSON. UseLDValue
methods to examine its type and value.Note that this will only work if you have set
evaluationReasons
to true withLDConfig.Builder.evaluationReasons(boolean)
. Otherwise, thereason
property of the result will be null.The evaluation reason will also be included in analytics events, if you are capturing detailed event data for this flag.
The behavior is otherwise identical to
LDClientInterface.jsonValueVariation(java.lang.String, com.launchdarkly.sdk.LDValue)
.- Specified by:
jsonValueVariationDetail
in interfaceLDClientInterface
- Parameters:
key
- key for the flag to evaluatedefaultValue
- default value in case of errors evaluating the flag (seeLDClientInterface.jsonValueVariation(String, LDValue)
)- Returns:
- an
EvaluationDetail
object containing the value and other information.
-
close
Closes the client. This should only be called at the end of a client's lifecycle.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- declared by the Closeable interface, but will not be thrown by the client
-
flush
public void flush()Description copied from interface:LDClientInterface
Sends all pending events to LaunchDarkly.- Specified by:
flush
in interfaceLDClientInterface
-
isInitialized
public boolean isInitialized()Description copied from interface:LDClientInterface
Checks whether the client is ready to return feature flag values. This is true if either the client has successfully connected to LaunchDarkly and received feature flags, or the client has been put into offline mode (in which case it will return only default flag values).- Specified by:
isInitialized
in interfaceLDClientInterface
- Returns:
- true if the client is initialized or offline
-
isOffline
public boolean isOffline()Description copied from interface:LDClientInterface
Checks whether the client has been put into offline mode. This is true only ifLDClientInterface.setOffline()
was called, or if the configuration hadLDConfig.Builder.offline(boolean)
set to true, not if the client is simply offline due to a loss of network connectivity.- Specified by:
isOffline
in interfaceLDClientInterface
- Returns:
- true if the client is in offline mode
-
setOffline
public void setOffline()Description copied from interface:LDClientInterface
Shuts down any network connections maintained by the client and puts the client in offline mode, preventing the client from opening new network connections untilsetOnline()
is called.Note: The client automatically monitors the device's network connectivity and app foreground status, so calling
setOffline()
orsetOnline()
is normally unnecessary in most situations.- Specified by:
setOffline
in interfaceLDClientInterface
-
setOnline
public void setOnline()Description copied from interface:LDClientInterface
Restores network connectivity for the client, if the client was previously in offline mode. This operation may be throttled if it is called too frequently.Note: The client automatically monitors the device's network connectivity and app foreground status, so calling
setOffline()
orsetOnline()
is normally unnecessary in most situations.- Specified by:
setOnline
in interfaceLDClientInterface
-
registerFeatureFlagListener
Description copied from interface:LDClientInterface
Registers aFeatureFlagChangeListener
to be called when theflagKey
changes from its current value. If the feature flag is deleted, thelistener
will be unregistered.- Specified by:
registerFeatureFlagListener
in interfaceLDClientInterface
- Parameters:
flagKey
- the flag key to attach the listener tolistener
- the listener to attach to the flag key- See Also:
-
unregisterFeatureFlagListener
Description copied from interface:LDClientInterface
Unregisters aFeatureFlagChangeListener
for theflagKey
.- Specified by:
unregisterFeatureFlagListener
in interfaceLDClientInterface
- Parameters:
flagKey
- the flag key to remove the listener fromlistener
- the listener to remove from the flag key- See Also:
-
isDisableBackgroundPolling
public boolean isDisableBackgroundPolling()Description copied from interface:LDClientInterface
Checks whetherLDConfig.Builder.disableBackgroundUpdating(boolean)
was set totrue
in the configuration.- Specified by:
isDisableBackgroundPolling
in interfaceLDClientInterface
- Returns:
- true if background polling is disabled
-
getConnectionInformation
Description copied from interface:LDClientInterface
Gets aConnectionInformation
object from the client representing the current state of the clients connection.- Specified by:
getConnectionInformation
in interfaceLDClientInterface
- Returns:
- An object representing the status of the connection to LaunchDarkly.
-
registerStatusListener
Description copied from interface:LDClientInterface
Registers aLDStatusListener
to be called on connection status updates.- Specified by:
registerStatusListener
in interfaceLDClientInterface
- Parameters:
statusListener
- the listener to be called on a connection status update
-
unregisterStatusListener
Description copied from interface:LDClientInterface
Unregisters aLDStatusListener
so it will no longer be called on connection status updates.- Specified by:
unregisterStatusListener
in interfaceLDClientInterface
- Parameters:
statusListener
- the listener to be removed
-
registerAllFlagsListener
Description copied from interface:LDClientInterface
Registers aLDAllFlagsListener
to be called when a flag update is processed by the SDK.- Specified by:
registerAllFlagsListener
in interfaceLDClientInterface
- Parameters:
allFlagsListener
- the listener to be called with a list of flag keys on a flag update
-
unregisterAllFlagsListener
Description copied from interface:LDClientInterface
Unregisters aLDAllFlagsListener
so it will no longer be called on flag updates.- Specified by:
unregisterAllFlagsListener
in interfaceLDClientInterface
- Parameters:
allFlagsListener
- the listener to be removed
-
getVersion
Description copied from interface:LDClientInterface
Returns the version of the SDK, for instance "2.7.0".- Specified by:
getVersion
in interfaceLDClientInterface
- Returns:
- the version string
-
LDClient
without a timeout is no longer permitted to help prevent consumers from blocking their application execution by mistake when connectivity is poor.