Package com.google.android.exoplayer2.util

## Class SntpClient

- java.lang.Object
- - com.google.android.exoplayer2.util.SntpClient

-     public final class SntpClient
      extends Object

  Static utility to retrieve the device time offset using SNTP.

  Based on the Android framework SntpClient.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class | Description
    --- | --- | ---
    `static interface` | `SntpClient.InitializationCallback` | Callback for calls to `initialize(Loader, InitializationCallback)`.

  - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `static String` | `DEFAULT_NTP_HOST` | The default NTP host address used to retrieve `getElapsedRealtimeOffsetMs()`.

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `static long` | `getElapsedRealtimeOffsetMs()` | Returns the offset between `SystemClock.elapsedRealtime()` and the NTP server time in milliseconds, or `C.TIME_UNSET` if `isInitialized()` returns false.
    `static String` | `getNtpHost()` | Returns the NTP host address used to retrieve `getElapsedRealtimeOffsetMs()`.
    `static void` | `initialize​(Loader loader, SntpClient.InitializationCallback callback)` | Starts loading the device time offset.
    `static boolean` | `isInitialized()` | Returns whether the device time offset has already been loaded.
    `static void` | `setNtpHost​(String ntpHost)` | Sets the NTP host address used to retrieve `getElapsedRealtimeOffsetMs()`.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_NTP_HOST

          public static final String DEFAULT_NTP_HOST

      The default NTP host address used to retrieve `getElapsedRealtimeOffsetMs()`.
      See Also:
        Constant Field Values

  - ### Method Detail

    - #### getNtpHost

          public static String getNtpHost()

      Returns the NTP host address used to retrieve `getElapsedRealtimeOffsetMs()`.

    - #### setNtpHost

          public static void setNtpHost​(String ntpHost)

      Sets the NTP host address used to retrieve `getElapsedRealtimeOffsetMs()`.

      The default is `DEFAULT_NTP_HOST`.

      If the new host address is different from the previous one, the NTP client will be `isInitialized()` uninitialized} again.

      Parameters:
        `ntpHost` - The NTP host address.

    - #### isInitialized

          public static boolean isInitialized()

      Returns whether the device time offset has already been loaded.

      If `false`, use `initialize(Loader, InitializationCallback)` to start the initialization.

    - #### getElapsedRealtimeOffsetMs

          public static long getElapsedRealtimeOffsetMs()

      Returns the offset between `SystemClock.elapsedRealtime()` and the NTP server time in milliseconds, or `C.TIME_UNSET` if `isInitialized()` returns false.

      The offset is calculated as `ntpServerTime - deviceElapsedRealTime`.

    - #### initialize

          public static void initialize​(@Nullable
                                        Loader loader,
                                        @Nullable
                                        SntpClient.InitializationCallback callback)

      Starts loading the device time offset.
      Parameters:
        `loader` - A `Loader` to use for loading the time offset, or null to create a new one.
        `callback` - An optional `SntpClient.InitializationCallback` to be notified when the time offset has been initialized or initialization failed.