de.unikassel.android.sdcframework.util
Class TimeProvider

Package class diagram package TimeProvider
java.lang.Object
  extended by de.unikassel.android.sdcframework.util.ObservableEventSourceImpl<TimeProviderEvent>
      extended by de.unikassel.android.sdcframework.util.TimeProvider
All Implemented Interfaces:
ObservableEventSource<TimeProviderEvent>, ObserverRegistration<TimeProviderEvent>

public final class TimeProvider
extends ObservableEventSourceImpl<TimeProviderEvent>

The internal time provider.

This class does encapsulate the frameworks time service. It does request an actual time from an ntp server and stores the offset to correct the local system time.


Nested Class Summary
(package private)  class TimeProvider.UpdateThread
          Thread for asynchronous time updates
 
Field Summary
private static int CNT_MAX_SYNC_ATTEMPTS
          The maximum count for asynchronous synchronization retires
private static long CONNECTION_WAIT_TIME
          Time to wait for network available
private  String[] INITIAL_PROVIDERS
          The list with the providers to use
private static TimeProvider instance
          The singleton instance of the time provider
private  AtomicLong lastUpdateTs
          The time stamp of the last sync update
private  AtomicLong offset
          The internal time offset
private  List<String> providers
          The list with time providers
private  AtomicBoolean synced
          Flag if time is initialized
private  AtomicBoolean updateInProgress
          Flag for update in progress
private  TimeProvider.UpdateThread updateThread
          The instance of a running update thread
 
Constructor Summary
private TimeProvider()
          Constructor
 
Method Summary
 void asynchronousUpdateTime(Context context)
          Method to update the internal time offset asynchronously
 TimeInformation getAccurateTimeInformation()
          Getter for the accurate time stamp
static Calendar getDayBegin(Calendar calValue)
          Getter for the day day start time stamp of a given calendar date
static TimeProvider getInstance()
          Access to the global time provider instance
 long getOffset()
          Getter for the offset
 List<String> getProviders()
          Getter for the providers
 long getTimeStamp()
          Getter for the time ( maybe out of sync )
static long getUTCDayTimeMillis()
          Getter for the currents day time milliseconds
static long getUTCDayTimeMillis(long ts)
          Getter for the day time milliseconds for a given time stamp
 boolean isSynced()
          Test method for time provider synchronization state
static boolean isUTCDayChange(Long curUTCTime)
          Test method for day changes
protected  void notifyOutOfSync(long ts)
          Method to send an sync notification
protected  void notifySyncError(long ts)
          Method to send an sync notification
protected  void notifyUpdated()
          Method to send an sync notification
protected  void onObserverRegistration(EventObserver<? extends TimeProviderEvent> observer)
          Handler for registered observers ( to be overloaded by extending classes if necessary )
private  void setOffset(long offset)
          Setter for the offset
 void stopAsynchronousUpdate(Context context)
          Method to stop an update asynchronous update of the internal time offset
private  boolean syncTime(Context context)
          Method to synchronize time with a NTP server
static String toUTCDate(long timeStamp)
          Does create a date string representation of the given time stamp
static String toUTCString(long timeStamp)
          Does create a long string representation of the given time stamp
static String toUTCTime(long timeStamp)
          Does create a time string representation of the given time stamp
 void updateProviders(List<String> newProviders)
          Method to update the NTP provider list
 boolean updateTime(Context context)
          Method to update the internal time offset
 
Methods inherited from class de.unikassel.android.sdcframework.util.ObservableEventSourceImpl
getObservers, hasObservers, notify, onObserverUnregistration, registerEventObserver, removeAllObservers, unregisterEventObserver
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INITIAL_PROVIDERS

private final String[] INITIAL_PROVIDERS
The list with the providers to use


CNT_MAX_SYNC_ATTEMPTS

private static final int CNT_MAX_SYNC_ATTEMPTS
The maximum count for asynchronous synchronization retires

See Also:
Constant Field Values

CONNECTION_WAIT_TIME

private static final long CONNECTION_WAIT_TIME
Time to wait for network available

See Also:
Constant Field Values

offset

private AtomicLong offset
The internal time offset


lastUpdateTs

private AtomicLong lastUpdateTs
The time stamp of the last sync update


synced

private final AtomicBoolean synced
Flag if time is initialized


updateInProgress

private final AtomicBoolean updateInProgress
Flag for update in progress


instance

private static TimeProvider instance
The singleton instance of the time provider


providers

private final List<String> providers
The list with time providers


updateThread

private TimeProvider.UpdateThread updateThread
The instance of a running update thread

Constructor Detail

TimeProvider

private TimeProvider()
Constructor

Method Detail

getInstance

public static final TimeProvider getInstance()
Access to the global time provider instance

Returns:
the global time provider instance

getOffset

public long getOffset()
Getter for the offset

Returns:
the offset

setOffset

private void setOffset(long offset)
Setter for the offset

Parameters:
offset - the offset to set

updateProviders

public final void updateProviders(List<String> newProviders)
Method to update the NTP provider list

Parameters:
newProviders - the new provider list

getProviders

public List<String> getProviders()
Getter for the providers

Returns:
the providers

getTimeStamp

public final long getTimeStamp()
Getter for the time ( maybe out of sync )

Returns:
the current UTC time as time stamp

getAccurateTimeInformation

public final TimeInformation getAccurateTimeInformation()
Getter for the accurate time stamp

Returns:
the current UTC time as time stamp, or null if out of sync

updateTime

public final boolean updateTime(Context context)
Method to update the internal time offset

Parameters:
context - the context
Returns:
true if successful, false otherwise

asynchronousUpdateTime

public final void asynchronousUpdateTime(Context context)
Method to update the internal time offset asynchronously

Parameters:
context - the context

stopAsynchronousUpdate

public final void stopAsynchronousUpdate(Context context)
Method to stop an update asynchronous update of the internal time offset

Parameters:
context - the context

syncTime

private final boolean syncTime(Context context)
Method to synchronize time with a NTP server

Returns:
true if successful, false otherwise

toUTCString

public static final String toUTCString(long timeStamp)
Does create a long string representation of the given time stamp

Parameters:
timeStamp - the milliseconds since 01.01.1970
Returns:
the time stamp string

toUTCTime

public static final String toUTCTime(long timeStamp)
Does create a time string representation of the given time stamp

Parameters:
timeStamp - the milliseconds since 01.01.1970
Returns:
the time string

toUTCDate

public static final String toUTCDate(long timeStamp)
Does create a date string representation of the given time stamp

Parameters:
timeStamp - the milliseconds since 01.01.1970
Returns:
the date string

onObserverRegistration

protected final void onObserverRegistration(EventObserver<? extends TimeProviderEvent> observer)
Description copied from class: ObservableEventSourceImpl
Handler for registered observers ( to be overloaded by extending classes if necessary )

Overrides:
onObserverRegistration in class ObservableEventSourceImpl<TimeProviderEvent>
Parameters:
observer - the registered observer

notifyUpdated

protected void notifyUpdated()
Method to send an sync notification


notifyOutOfSync

protected void notifyOutOfSync(long ts)
Method to send an sync notification

Parameters:
ts - the time stamp

notifySyncError

protected void notifySyncError(long ts)
Method to send an sync notification

Parameters:
ts - the time stamp

getUTCDayTimeMillis

public static final long getUTCDayTimeMillis()
Getter for the currents day time milliseconds

Returns:
the currents day time stamp

getUTCDayTimeMillis

public static final long getUTCDayTimeMillis(long ts)
Getter for the day time milliseconds for a given time stamp

Parameters:
ts - the time stamp to get day time milliseconds for ( at 00:00:00.0 Midnight )
Returns:
the day time stamp

getDayBegin

public static final Calendar getDayBegin(Calendar calValue)
Getter for the day day start time stamp of a given calendar date

Parameters:
calValue - the calendar date time to get day start for ( same day at 00:00:00.0 Midnight )
Returns:
the time stamp of the day

isUTCDayChange

public static final boolean isUTCDayChange(Long curUTCTime)
Test method for day changes

Parameters:
curUTCTime - current time provider time
Returns:
true if day has just changed

isSynced

public boolean isSynced()
Test method for time provider synchronization state

Returns:
true if synchronized with NTP time