|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.vodafone360.people.engine.BaseEngine com.vodafone360.people.engine.contactsync.ContactSyncEngine
public class ContactSyncEngine
Implementation of engine handling Contact-sync. Contact sync is a multi-stage process, involving sync of contacts from the native database, sync of server contacts, fetching of groups and thumbnails. Each phase is handled by a separate processor created and managed by the Contact sync engine.
Nested Class Summary | |
---|---|
static interface |
ContactSyncEngine.IContactSyncObserver
Observer interface allowing interested parties to receive notification of changes in Contact sync state. |
static class |
ContactSyncEngine.Mode
Defines the contact sync mode. |
static class |
ContactSyncEngine.State
Definition of states for Contact sync. |
private static class |
ContactSyncEngine.SyncParams
Holds parameters for the UI sync request |
Nested classes/interfaces inherited from class com.vodafone360.people.engine.BaseEngine |
---|
BaseEngine.IEngineEventCallback |
Field Summary | |
---|---|
private static long |
FULL_SYNC_RETRIES
Number of retries when first time sync fails |
private BaseSyncProcessor |
mActiveProcessor
Currently active processor (the processor which is running) or null |
private ServiceUiRequest |
mActiveUiRequestBackup
Backup of the previous active request before processing the new one. |
private ApplicationCache |
mCache
|
private boolean |
mCancelSync
True if the current contact sync is being cancelled. |
private Context |
mContext
The context of the People service |
private ContentResolver |
mCr
The content resolver object mainly used for accessing the native database |
private int |
mCurrentProgressPercent
Current progress value (used to check if the progress has changed) |
private boolean |
mDatabaseChanged
Database changed flag. |
private DatabaseHelper |
mDb
DatabaseHelper object used for accessing NowPlus database. |
private boolean |
mDbChangedByProcessor
Flag which is set when the current processor changes the database |
private Handler |
mDbChangeHandler
Used to listen for NowPlus database change events. |
private java.util.ArrayList<ContactSyncEngine.IContactSyncObserver> |
mEventCallbackList
Maintains a list of contact sync observers |
private java.lang.String |
mFailureList
A failure list (currently containing unlocalised English strings) of contacts which could not be sync'ed to the server. |
private java.lang.Long |
mFetchNativeSyncTimeout
Specifies the time that a fetch native sync should be started relative to current time in milliseconds. |
private boolean |
mFirstTimeNativeSyncComplete
Flag which matches the persisted equivalent in the NowPlus database state table. |
private boolean |
mFirstTimeSyncComplete
Flag which matches the persisted equivalent in the NowPlus database state table. |
private boolean |
mFirstTimeSyncStarted
Flag which matches the persisted equivalent in the NowPlus database state table. |
private boolean |
mFullSyncRequired
True if a full sync should be started as soon as possible |
private int |
mFullSyncRetryCount
Counter for first time sync failures |
private java.lang.Long |
mLastDbUpdateTime
Last time the database was updated (in milliseconds) |
private java.lang.Long |
mLastServerSyncTime
Keeps track of the last time a server sync happened. |
private ContactSyncEngine.Mode |
mMode
Current mode (or stragegy) the contact sync engine is in. |
private java.lang.Object |
mMutex
Mutex for thread synchronization |
private NativeContactsApi |
mNativeContactsApi
Native Contacts API access. |
private boolean |
mNativeFetchSyncRequired
True if a native fetch sync should be started as soon as possible |
private boolean |
mNativeUpdateSyncRequired
True if a native update sync should be started as soon as possible |
private ProcessorFactory |
mProcessorFactory
The factory class which is used for creating processors for a particular state. |
private boolean |
mRemoveUserData
The following flag is set when the user initiates a remove user data. |
private boolean |
mServerSyncRequired
True if a server sync should be started as soon as possible |
private java.lang.Long |
mServerSyncTimeout
Specifies the time that a server sync should be started relative to current time in milliseconds. |
private ContactSyncEngine.State |
mState
Current state of the contact sync engine (determines which processor is currently active) |
private boolean |
mThumbnailSyncRequired
True if a server thumbnail (avatar) sync should be started as soon as possible |
private UiAgent |
mUiAgent
|
private java.lang.Long |
mUpdateNativeSyncTimeout
Specifies the time that a update native sync should be started relative to current time in milliseconds. |
private static long |
NATIVE_CONTACT_SYNC_TIMEOUT_MS
Time to wait after the user modifies a contact before a contact sync with the native database will be initiated (in milliseconds). |
private static long |
SERVER_CONTACT_SYNC_TIMEOUT_MS
Time to wait after the user modifies a contact before a contact sync with the server will be initiated (in milliseconds). |
private static long |
UI_REFRESH_WAIT_TIME_NANO
Determines the time that should be waited between publishing database change events. |
private static long |
USER_ACTIVITY_SERVER_SYNC_TIMEOUT_MS
The time to wait before requesting a new server sync when the user is using the application. |
Fields inherited from class com.vodafone360.people.engine.BaseEngine |
---|
mActiveUiRequest, mCurrentTimeout, mEngineId, mEventCallback |
Constructor Summary | |
---|---|
ContactSyncEngine(BaseEngine.IEngineEventCallback eventCallback,
Context context,
DatabaseHelper db,
ProcessorFactory processorFactory)
ContactSyncEngine constructor. |
Method Summary | |
---|---|
void |
addEventCallback(ContactSyncEngine.IContactSyncObserver observer)
Add observer of Contact-sync. |
void |
addUiStartFullSync()
Triggers a full contact sync from the UI (via the service interface). |
void |
addUiStartServerSync(long delay)
Triggers a server contact sync from the UI (via service interface). |
private void |
cancelSync()
Called by the run function when the mCancelSync flag is set to
true. |
private void |
clearCurrentSyncAndPatchBaseEngine()
Clears the current sync and make sure that if we cancel a previous sync, it doesn't notify a wrong UI request. |
private void |
completeSync(ServiceStatus status)
Called when the current mode has finished all the sync tasks. |
private void |
fireProgressEvent(ContactSyncEngine.State currentState,
int percent)
Notifies observers when the progress value changes for the current sync. |
private void |
fireStateChangeEvent(ContactSyncEngine.Mode mode,
ContactSyncEngine.State previousState,
ContactSyncEngine.State newState)
Notifies observers when a state or mode change occurs. |
private void |
fireSyncCompleteEvent(ServiceStatus status)
Notifies observers when a contact sync complete event occurs. |
BaseEngine |
getEngine()
Used by processors to fetch this engine. |
long |
getNextRunTime()
Framework function to determine when the contact sync engine next needs to run. |
boolean |
isFirstTimeSyncComplete()
Determines if the first time contact sync has been completed. |
private void |
newState(ContactSyncEngine.State newState)
Changes the state of the engine and informs the observers. |
private void |
nextTaskFetchNativeContacts()
Moves to the next state for the fetch native contacts mode, and runs the appropriate processor. |
private void |
nextTaskFullSyncFirstTime()
Moves to the next state for the full sync first time mode, and runs the appropriate processor. |
private void |
nextTaskFullSyncNormal()
Moves to the next state for the full sync normal mode, and runs the appropriate processor. |
private void |
nextTaskServerSync()
Moves to the next state for the server sync mode, and runs the appropriate processor. |
private void |
nextTaskThumbnailSync()
Moves to the next state for the thumbnail sync mode, and runs the appropriate processor. |
private void |
nextTaskUpdateNativeContacts()
Moves to the next state for the update native contacts mode, and runs the appropriate processor. |
void |
onChange()
Call-back to notify that a change happened in the native address book. |
void |
onCreate()
Called after the engine has been created to do some extra initialisation. |
void |
onDatabaseChanged()
Called by the active processor to indicate that the NowPlus database has changed. |
void |
onDestroy()
Called just before engine is about to be closed. |
void |
onProcessorComplete(ServiceStatus status,
java.lang.String failureList,
java.lang.Object data)
Called by a processor when it has completed. |
protected void |
onRequestComplete()
Called by base class when a contact sync UI request has been completed. |
void |
onReset()
Called by framework to warn the engine that a remove user data is about to start. |
protected void |
onTimeoutEvent()
Called by base class when a timeout has been completed. |
void |
pingUserActivity()
Tells the ContactSyncEngine that the user is actively using the service and adjust sync timeout accordingly. |
protected void |
processCommsResponse(ResponseQueue.Response resp)
Called when a response to a request or a push message is received from the server. |
private void |
processDbMessage(Message message)
Called when a database change event is received from the DatabaseHelper. |
private boolean |
processPushEvent(ResponseQueue.Response resp)
Determines if a given response is a push message and processes in this case TODO: we need the check for Me Profile be migrated to he new engine |
protected void |
processUiRequest(ServiceUiRequest requestId,
java.lang.Object data)
Called by base class to process a NOWPLUSSYNC UI request. |
private boolean |
readyToStartFetchNativeSync()
Checks if a fetch native sync can be started based on network conditions and engine state |
private boolean |
readyToStartServerSync()
Checks if a server sync can be started based on network conditions and engine state |
private boolean |
readyToStartUpdateNativeSync()
Checks if a update native sync can be started based on network conditions and engine state |
void |
run()
Called by framework when getNextRunTime() reports that the
engine needs to run, to carry out the next task. |
void |
setActiveRequestId(int reqId)
Called by active processor when issuing a request to store the request id in the base engine class. |
private ServiceStatus |
setFirstTimeNativeSyncComplete(boolean value)
Helper function to update the database when the state of the mFirstTimeNativeSyncComplete flag changes. |
private ServiceStatus |
setFirstTimeSyncComplete(boolean value)
Helper function to update the database when the state of the mFirstTimeSyncComplete flag changes. |
private ServiceStatus |
setFirstTimeSyncStarted(boolean value)
Helper function to update the database when the state of the mFirstTimeSyncStarted flag changes. |
void |
setSyncStatus(SyncStatus syncStatus)
Used by active processor to set the current progress. |
void |
setTimeout(long timeout)
Used by active processor to set a timeout. |
private void |
setTimeoutIfRequired()
Sets the current timeout to the next pending timer and kicks the engine if necessary. |
private boolean |
startDownloadServerContacts()
Helper function to start the download server contacts processor |
private boolean |
startDownloadServerThumbnails()
Helper function to start the download thumbnails processor |
private boolean |
startFetchNativeContacts()
Helper function to start the fetch native contacts processor |
private void |
startFetchNativeContactSyncTimer()
Starts a timer to trigger a fetch native contact sync in a short while (normally around 30 seconds). |
private void |
startFetchNativeSync()
Starts a background fetch native contacts sync |
void |
startFullSync()
Starts a full sync. |
private void |
startProcessor(BaseSyncProcessor processor)
Helper function to start a processor running. |
private void |
startServerContactSyncTimer(long delay)
Starts a timer to trigger a server contact sync in a short while (normally around 30 seconds). |
private void |
startServerSync()
Starts a server sync. |
private void |
startSyncIfRequired()
Based on current timeout values schedules a new sync if required. |
private void |
startThumbnailSync()
Starts a background thumbnail sync |
private boolean |
startUpdateNativeContacts()
Helper function to start the update native contacts processor |
private void |
startUpdateNativeContactSyncTimer()
Starts a timer to trigger a update native contact sync in a short while (normally around 30 seconds). |
private void |
startUpdateNativeSync()
Starts a background update native contacts sync |
private boolean |
startUploadServerContacts()
Helper function to start the upload server contacts processor |
private boolean |
startUploadServerThumbnails()
Helper function to start the upload thumbnails processor |
Methods inherited from class com.vodafone360.people.engine.BaseEngine |
---|
addUiRequestToQueue, clearTimeout, completeUiRequest, completeUiRequest, deactivateEngine, emptyUiRequestQueue, engineId, genericHandleResponseType, getCurrentTimeout, isCommsResponseOutstanding, isDeactivated, isUiRequestOutstanding, onCommsInMessage, processCommsInQueue, processTimeout, processUiQueue, setReqId |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private final java.lang.Object mMutex
private final UiAgent mUiAgent
private final ApplicationCache mCache
private static final long FULL_SYNC_RETRIES
private int mFullSyncRetryCount
private ContactSyncEngine.State mState
private ContactSyncEngine.Mode mMode
private java.lang.String mFailureList
private boolean mDatabaseChanged
private java.lang.Long mLastDbUpdateTime
private DatabaseHelper mDb
private BaseSyncProcessor mActiveProcessor
private ProcessorFactory mProcessorFactory
private static final long SERVER_CONTACT_SYNC_TIMEOUT_MS
private static final long NATIVE_CONTACT_SYNC_TIMEOUT_MS
private static final long USER_ACTIVITY_SERVER_SYNC_TIMEOUT_MS
private static final long UI_REFRESH_WAIT_TIME_NANO
private volatile java.lang.Long mServerSyncTimeout
private volatile java.lang.Long mFetchNativeSyncTimeout
private volatile java.lang.Long mUpdateNativeSyncTimeout
private java.lang.Long mLastServerSyncTime
private ContentResolver mCr
private Context mContext
private boolean mFirstTimeSyncComplete
private boolean mFirstTimeSyncStarted
private volatile boolean mFirstTimeNativeSyncComplete
private boolean mFullSyncRequired
private boolean mServerSyncRequired
private boolean mNativeFetchSyncRequired
private boolean mNativeUpdateSyncRequired
private boolean mThumbnailSyncRequired
private boolean mCancelSync
private boolean mRemoveUserData
private final java.util.ArrayList<ContactSyncEngine.IContactSyncObserver> mEventCallbackList
private int mCurrentProgressPercent
private boolean mDbChangedByProcessor
private ServiceUiRequest mActiveUiRequestBackup
private final NativeContactsApi mNativeContactsApi
private final Handler mDbChangeHandler
Constructor Detail |
---|
public ContactSyncEngine(BaseEngine.IEngineEventCallback eventCallback, Context context, DatabaseHelper db, ProcessorFactory processorFactory)
eventCallback
- Engine-event callback interface allowing engine to
report back to client on request completion.context
- Context.db
- Handle to People database.processorFactory
- the processor factoryMethod Detail |
---|
public void onCreate()
onCreate
in class BaseEngine
public void onDestroy()
onDestroy
in class BaseEngine
public void addUiStartFullSync()
ServiceUiRequest.NOWPLUSSYNC
event will be
sent to notify the UI when the sync has completed.
public void addUiStartServerSync(long delay)
delay
- the delay in milliseconds from now when the sync should
startpublic void pingUserActivity()
public boolean isFirstTimeSyncComplete()
public void addEventCallback(ContactSyncEngine.IContactSyncObserver observer)
observer
- IContactSyncObserver handle.private void startServerContactSyncTimer(long delay)
private void startFetchNativeContactSyncTimer()
private void startUpdateNativeContactSyncTimer()
private void startProcessor(BaseSyncProcessor processor)
processor
- Processor which was created by processor factory.public long getNextRunTime()
getNextRunTime
in class BaseEngine
public void run()
getNextRunTime()
reports that the
engine needs to run, to carry out the next task. Each task should not
take longer than a second to complete.
run
in class BaseEngine
protected void onRequestComplete()
onRequestComplete
in class BaseEngine
protected void onTimeoutEvent()
onTimeoutEvent
in class BaseEngine
private void startSyncIfRequired()
protected void processCommsResponse(ResponseQueue.Response resp)
processCommsResponse
in class BaseEngine
resp
- Response or push message receivedprivate boolean processPushEvent(ResponseQueue.Response resp)
resp
- Response to check and process
protected void processUiRequest(ServiceUiRequest requestId, java.lang.Object data)
processUiRequest
in class BaseEngine
requestId
- ID of the request to process, only
ServiceUiRequest.NOWPLUSSYNC is currently supported.data
- Type is determined by request ID, in case of NOWPLUSSYNC this
is a flag which determines if a full sync is required (true =
full sync, false = server sync).private void cancelSync()
mCancelSync
flag is set to
true. Cancels the active processor and completes the current UI request.
private void clearCurrentSyncAndPatchBaseEngine()
private boolean readyToStartServerSync()
private boolean readyToStartFetchNativeSync()
private boolean readyToStartUpdateNativeSync()
public void startFullSync()
private void startServerSync()
private void startThumbnailSync()
private void startFetchNativeSync()
private void startUpdateNativeSync()
private boolean startFetchNativeContacts()
private boolean startUpdateNativeContacts()
private boolean startDownloadServerContacts()
private boolean startUploadServerContacts()
private boolean startDownloadServerThumbnails()
private boolean startUploadServerThumbnails()
public void onProcessorComplete(ServiceStatus status, java.lang.String failureList, java.lang.Object data)
onProcessorComplete
in interface IContactSyncCallback
status
- Status of the sync from the processor, any error codes will
stop the sync.failureList
- Contains a list of sync failure information which can
be used as a summary at the end. Otherwise should be an empty
string.data
- Any processor specific data to pass back to the engine. Not
currently used.private void nextTaskFullSyncFirstTime()
private void nextTaskFullSyncNormal()
private void nextTaskFetchNativeContacts()
private void nextTaskUpdateNativeContacts()
private void nextTaskServerSync()
private void nextTaskThumbnailSync()
private void newState(ContactSyncEngine.State newState)
newState
- The new stateprivate void completeSync(ServiceStatus status)
status
- The overall status of the contact sync.private void setTimeoutIfRequired()
public void onDatabaseChanged()
onDatabaseChanged
in interface IContactSyncCallback
public BaseEngine getEngine()
getEngine
in interface IContactSyncCallback
public void setTimeout(long timeout)
setTimeout
in interface IContactSyncCallback
setTimeout
in class BaseEngine
timeout
- Timeout value based on current time in millisecondspublic void setSyncStatus(SyncStatus syncStatus)
setSyncStatus
in interface IContactSyncCallback
SyncStatus
- Status of the processor, must not be NULL.
java.security.InvalidParameterException
- when SyncStatus is NULL.public void setActiveRequestId(int reqId)
setActiveRequestId
in interface IContactSyncCallback
reqId
- Request ID received from one of the API functions.private ServiceStatus setFirstTimeSyncStarted(boolean value)
mFirstTimeSyncStarted
flag changes.
value
- New value to the flag. True indicates that first time sync
has been started. The flag is never set to false again by the
engine, it will be only set to false when a remove user data
is done (and the database is deleted).
private ServiceStatus setFirstTimeSyncComplete(boolean value)
mFirstTimeSyncComplete
flag changes.
value
- New value to the flag. True indicates that first time sync
has been completed. The flag is never set to false again by
the engine, it will be only set to false when a remove user
data is done (and the database is deleted).
private ServiceStatus setFirstTimeNativeSyncComplete(boolean value)
mFirstTimeNativeSyncComplete
flag changes.
value
- New value to the flag. True indicates that the native fetch
part of the first time sync has been completed. The flag is
never set to false again by the engine, it will be only set to
false when a remove user data is done (and the database is
deleted).
private void processDbMessage(Message message)
msg
- The message indicating the type of eventprivate void fireStateChangeEvent(ContactSyncEngine.Mode mode, ContactSyncEngine.State previousState, ContactSyncEngine.State newState)
mode
- Current modepreviousState
- State before the changenewState
- State after the change.private void fireSyncCompleteEvent(ServiceStatus status)
status
- SUCCESS or a suitable error codeprivate void fireProgressEvent(ContactSyncEngine.State currentState, int percent)
currentState
- Current sync task being processedpercent
- Progress of task (between 0 and 100 percent)public void onReset()
onReset
in class BaseEngine
public void onChange()
NativeContactsApi.ContactsObserver
onChange
in interface NativeContactsApi.ContactsObserver
NativeContactsApi.ContactsObserver.onChange()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
JavaDoc