Class LDClient

    • Constructor Detail

      • LDClient

        protected LDClient​(@NonNull
                           com.launchdarkly.sdk.android.PlatformState platformState,
                           @NonNull
                           com.launchdarkly.sdk.android.TaskExecutor taskExecutor,
                           @NonNull
                           com.launchdarkly.sdk.android.PersistentDataStoreWrapper.PerEnvironmentData environmentStore,
                           @NonNull
                           LDContext initialContext,
                           @NonNull
                           LDConfig config,
                           @NonNull
                           java.lang.String mobileKey,
                           @NonNull
                           java.lang.String environmentName)
                    throws LaunchDarklyException
        Throws:
        LaunchDarklyException
    • Method Detail

      • init

        public static java.util.concurrent.Future<LDClient> init​(@NonNull
                                                                 android.app.Application application,
                                                                 @NonNull
                                                                 LDConfig config,
                                                                 @NonNull
                                                                 LDContext context)
        Initializes the singleton/primary instance. The result is a Future 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 call get()

        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 application
        config - configuration used to set up the client
        context - the initial evaluation context; see LDClient 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(Application, LDConfig, LDContext, int), init(Application, LDConfig, LDUser)
      • init

        public static LDClient init​(android.app.Application application,
                                    LDConfig config,
                                    LDContext context,
                                    int startWaitSeconds)
        Initializes the singleton instance and blocks for up to startWaitSeconds seconds until the client has been initialized. If the client does not initialize within startWaitSeconds seconds, it is returned anyway and can be used, but may not have fetched the most recent feature flag values.
        Parameters:
        application - your Android application
        config - configuration used to set up the client
        context - the initial evaluation context; see LDClient for more information about setting the context and optionally requesting a unique key for it
        startWaitSeconds - maximum number of seconds to wait for the client to initialize
        Returns:
        the primary LDClient instance
        Since:
        3.0.0
        See Also:
        init(Application, LDConfig, LDContext), init(Application, LDConfig, LDUser, int)
      • init

        @Deprecated
        public static LDClient init​(android.app.Application application,
                                    LDConfig config,
                                    LDUser user,
                                    int startWaitSeconds)
        Initializes the singleton instance and blocks for up to startWaitSeconds seconds until the client has been initialized. If the client does not initialize within startWaitSeconds seconds, it is returned anyway and can be used, but may not have fetched the most recent feature flag values.

        This is equivalent to init(Application, LDConfig, LDContext, int), but using the LDUser type instead of LDContext.

        Parameters:
        application - your Android application
        config - configuration used to set up the client
        user - the initial user attributes, which will be converted to an evaluation context; see LDClient for more information about setting the context and optionally requesting a unique key for it
        startWaitSeconds - maximum number of seconds to wait for the client to initialize
        Returns:
        the primary LDClient instance
        See Also:
        init(Application, LDConfig, LDUser), init(Application, LDConfig, LDContext, int)
      • trackMetric

        public void trackMetric​(java.lang.String eventName,
                                LDValue data,
                                double metricValue)
        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 interface LDClientInterface
        Parameters:
        eventName - the name of the event
        data - an LDValue containing additional data associated with the event; if not applicable, you may pass either null or LDValue.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:
        LDClientInterface.track(String), LDClientInterface.trackData(String, LDValue)
      • identify

        public java.util.concurrent.Future<java.lang.Void> identify​(LDContext context)
        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 returned Future 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 interface LDClientInterface
        Parameters:
        context - the new evaluation context; see LDClient 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
        See Also:
        LDClientInterface.identify(LDUser)
      • identify

        @Deprecated
        public java.util.concurrent.Future<java.lang.Void> identify​(LDUser user)
        Deprecated.
        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.

        This is equivalent to LDClientInterface.identify(LDContext), but using the LDUser type instead of LDContext.

        Specified by:
        identify in interface LDClientInterface
        Parameters:
        user - the new user attributes, which will be converted to an evaluation context; see LDClient 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
        See Also:
        LDClientInterface.identify(LDContext)
      • allFlags

        public java.util.Map<java.lang.String,​LDValue> 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 interface LDClientInterface
        Returns:
        a map of all feature flags
      • boolVariation

        public boolean boolVariation​(@NonNull
                                     java.lang.String key,
                                     boolean defaultValue)
        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 interface LDClientInterface
        Parameters:
        key - key for the flag to evaluate
        defaultValue - default value in case of errors evaluating the flag
        Returns:
        value of the flag or the default value
      • boolVariationDetail

        public EvaluationDetail<java.lang.Boolean> boolVariationDetail​(@NonNull
                                                                       java.lang.String key,
                                                                       boolean defaultValue)
        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 with LDConfig.Builder.evaluationReasons(boolean). Otherwise, the reason 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 interface LDClientInterface
        Parameters:
        key - key for the flag to evaluate
        defaultValue - default value in case of errors evaluating the flag (see LDClientInterface.boolVariation(String, boolean))
        Returns:
        an EvaluationDetail object containing the value and other information
      • intVariation

        public int intVariation​(@NonNull
                                java.lang.String key,
                                int defaultValue)
        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 interface LDClientInterface
        Parameters:
        key - key for the flag to evaluate
        defaultValue - default value in case of errors evaluating the flag
        Returns:
        value of the flag or the default value
      • doubleVariation

        public double doubleVariation​(java.lang.String flagKey,
                                      double defaultValue)
        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 interface LDClientInterface
        Parameters:
        flagKey - key for the flag to evaluate
        defaultValue - default value in case of errors evaluating the flag
        Returns:
        value of the flag or the default value
      • stringVariation

        public java.lang.String stringVariation​(@NonNull
                                                java.lang.String key,
                                                java.lang.String defaultValue)
        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 interface LDClientInterface
        Parameters:
        key - key for the flag to evaluate
        defaultValue - default value in case of errors evaluating the flag
        Returns:
        value of the flag or the default value
      • jsonValueVariation

        public LDValue jsonValueVariation​(@NonNull
                                          java.lang.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.

        The type LDValue is used to represent any of the value types that can exist in JSON. Use LDValue methods to examine its type and value.

        Specified by:
        jsonValueVariation in interface LDClientInterface
        Parameters:
        key - key for the flag to evaluate
        defaultValue - 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()
      • close

        public void close()
                   throws java.io.IOException
        Closes the client. This should only be called at the end of a client's lifecycle.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException - declared by the Closeable interface, but will not be thrown by the client
      • 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 interface LDClientInterface
        Returns:
        true if the client is initialized or offline
      • 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 until setOnline() is called.

        Note: The client automatically monitors the device's network connectivity and app foreground status, so calling setOffline() or setOnline() is normally unnecessary in most situations.

        Specified by:
        setOffline in interface LDClientInterface
      • 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() or setOnline() is normally unnecessary in most situations.

        Specified by:
        setOnline in interface LDClientInterface
      • getVersion

        public java.lang.String getVersion()
        Description copied from interface: LDClientInterface
        Returns the version of the SDK, for instance "2.7.0".
        Specified by:
        getVersion in interface LDClientInterface
        Returns:
        the version string