Class UserAgent


  • public class UserAgent
    extends Object
    Instances of this class are used to construct the user agent that is used for HTTP requests.

    Additional key/value properties can be registered using register(String, String).

    Since version 4.0.1 this class strips non-ASCII characters from the User-Agent by default. You can disable this behaviour by setting STRIP_NON_ASCII_CHARACTERS to false

    Default fields can also be enabled or disabled through the setFields(int) method.
    Since:
    3.0.0
    • Field Detail

      • STRIP_NON_ASCII_CHARACTERS

        public static boolean STRIP_NON_ASCII_CHARACTERS
        By default, the UserAgent string will be stripped and all non-ASCII characters will be removed. You can set this flag to true to disable the filter. Please note that some HTTP stacks, especially the one used by OkHttp do not allow non-ASCII characters in the User Agent and you will get an Exception if you change the behaviour.
        Since:
        4.0.1
      • FLAG_ENABLE_MODEL

        public static final int FLAG_ENABLE_MODEL
        Flag to include the device model in the user-agent.
        See Also:
        Constant Field Values
      • FLAG_ENABLE_API_LEVEL

        public static final int FLAG_ENABLE_API_LEVEL
        Flag to include the Android API level in the user-agent.
        See Also:
        Constant Field Values
      • FLAG_ENABLE_EXO_VERSION

        public static final int FLAG_ENABLE_EXO_VERSION
        Flag to include the Exoplayer version in the user-agent.
        See Also:
        Constant Field Values
      • FLAG_ENABLE_DEVICE_ID

        public static final int FLAG_ENABLE_DEVICE_ID
        Flag to include the device ID in the user-agent. Due to privacy reasons this is omitted by default. You can still enable it manually by adding it to the setFields(int)
        See Also:
        Constant Field Values
    • Constructor Detail

      • UserAgent

        public UserAgent()
    • Method Detail

      • register

        public static void register​(@NonNull
                                    String key,
                                    @NonNull
                                    String value)
        Register an additional key value pair that will be added to the user agent string representation
        Parameters:
        key - the key
        value - the value
      • setFields

        public static void setFields​(@UserAgentFlags
                                     int enabledFields)
        Sets the currently enabled fields to include in the user-agent.
        Parameters:
        enabledFields - fields to enable
      • enableFields

        public static void enableFields​(@UserAgentFlags
                                        int fields)
        Enable user-agent fields
        Parameters:
        fields - the uset-agent fields to enable
      • disableFields

        public static void disableFields​(@UserAgentFlags
                                         int fields)
        Disable user-agent fields
        Parameters:
        fields - the uset-agent fields to disable
      • getEnabledFields

        public static int getEnabledFields()
        Returns the currently enabled fields to include in the user-agent
        Returns:
        the currently enabled fields to include in the user-agent