Class LDClient

  • All Implemented Interfaces:
    LDClientInterface, java.io.Closeable, java.lang.AutoCloseable

    public class LDClient
    extends java.lang.Object
    implements LDClientInterface, java.io.Closeable
    Client for accessing LaunchDarkly's Feature Flag system. This class enforces a singleton pattern. The main entry point is the init(Application, LDConfig, LDUser) method.
    • Constructor Detail

      • LDClient

        protected LDClient​(android.app.Application application,
                           @NonNull
                           LDConfig config)
      • LDClient

        protected LDClient​(android.app.Application application,
                           @NonNull
                           LDConfig config,
                           java.lang.String environmentName)
    • Method Detail

      • init

        public static java.util.concurrent.Future<LDClient> init​(@NonNull
                                                                 android.app.Application application,
                                                                 @NonNull
                                                                 LDConfig config,
                                                                 @NonNull
                                                                 LDUser user)
        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
        user - The user used in evaluating feature flags
        Returns:
        a Future which will complete once the client has been initialized.
      • init

        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.
        Parameters:
        application - Your Android application.
        config - Configuration used to set up the client
        user - The user used in evaluating feature flags
        startWaitSeconds - Maximum number of seconds to wait for the client to initialize
        Returns:
        The primary LDClient instance
      • trackMetric

        public void trackMetric​(java.lang.String eventName,
                                LDValue data,
                                double metricValue)
        Description copied from interface: LDClientInterface
        Tracks that a user performed an event, and provides an additional numeric value for custom metrics.
        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.
      • trackData

        public void trackData​(java.lang.String eventName,
                              LDValue data)
        Description copied from interface: LDClientInterface
        Tracks that a user performed an event, and provides additional custom data.
        Specified by:
        trackData in interface LDClientInterface
        Parameters:
        eventName - the name of the event
        data - an LDValue containing additional data associated with the event
      • track

        public void track​(java.lang.String eventName)
        Description copied from interface: LDClientInterface
        Tracks that a user performed an event.
        Specified by:
        track in interface LDClientInterface
        Parameters:
        eventName - the name of the event
      • identify

        public java.util.concurrent.Future<java.lang.Void> identify​(LDUser user)
        Description copied from interface: LDClientInterface
        Sets the current user, retrieves flags for that user, then sends an Identify Event to LaunchDarkly. The 5 most recent users' flag settings are kept locally.
        Specified by:
        identify in interface LDClientInterface
        Parameters:
        user - The user for evaluation and event reporting
        Returns:
        Future whose success indicates this user's flag settings have been stored locally and are ready for evaluation.
      • 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 user. 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 flag value for the current user. Returns defaultValue when one of the following occurs:
        1. Flag is missing
        2. The flag is not of a boolean type
        3. Any other error
        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
      • intVariation

        public int intVariation​(@NonNull
                                java.lang.String key,
                                int defaultValue)
        Description copied from interface: LDClientInterface
        Returns the flag value for the current user. Returns defaultValue when one of the following occurs:
        1. Flag is missing
        2. The flag is not of a numeric type
        3. Any other error
        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 flag value for the current user. Returns defaultValue when one of the following occurs:
        1. Flag is missing
        2. The flag is not of a numeric type
        3. Any other error
        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 flag value for the current user. Returns default when one of the following occurs:
        1. Flag is missing
        2. The flag is not of a string type
        3. Any other error
        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 flag value for the current user. Returns defualtValue when one of the following occurs:
        1. Flag is missing
        2. Any other error
        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
      • alias

        public void alias​(LDUser user,
                          LDUser previousUser)
        Alias associates two users for analytics purposes.
        Parameters:
        user - The first user
        previousUser - The second user
      • 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