Class UserAgent

java.lang.Object
com.castlabs.android.network.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
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static @interface 
    The flags that enable or disable field presence in the User-Agent.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Flag to include the Android API level in the user-agent.
    static final int
    Flag to include the device ID in the user-agent.
    static final int
    Flag to include the Exoplayer version in the user-agent.
    static final int
    Flag to include the device model in the user-agent.
    static boolean
    By default, the UserAgent string will be stripped and all non-ASCII characters will be removed.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    disableFields(int fields)
    Disable user-agent fields
    static void
    enableFields(int fields)
    Enable user-agent fields
    static int
    Returns the currently enabled fields to include in the user-agent
    static void
    register(String key, String value)
    Register an additional key value pair that will be added to the user agent string representation
    static void
    setFields(int enabledFields)
    Sets the currently enabled fields to include in the user-agent.
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • 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:
    • 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:
    • FLAG_ENABLE_EXO_VERSION

      public static final int FLAG_ENABLE_EXO_VERSION
      Flag to include the Exoplayer version in the user-agent.
      See Also:
    • 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:
  • Constructor Details

    • UserAgent

      public UserAgent()
  • Method Details

    • 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
    • toString

      public String toString()
      Overrides:
      toString in class Object