com.vodafone360.people.engine.contactsync
Class DownloadServerContacts

java.lang.Object
  extended by com.vodafone360.people.engine.contactsync.BaseSyncProcessor
      extended by com.vodafone360.people.engine.contactsync.DownloadServerContacts

public class DownloadServerContacts
extends BaseSyncProcessor

Handles download of contacts from People server.


Nested Class Summary
static class DownloadServerContacts.InternalState
          Enumeration which holds the various states for the download server processor.
 
Field Summary
private static boolean EXTRA_DEBUG_TRACE
          Set to true for extra logcat trace (debug only)
private  java.util.ArrayList<Contact> mAddContactList
          List of all contacts received from the server that need to be added to the database.
private  java.util.ArrayList<ContactDetail> mAddDetailList
          List of all contact details received from the server that need to be added to the local database.
private static int MAX_CONTACT_CHANGES_PER_PAGE
          Maximum number of database updates for each engine run (to ensure the processor doesn't block the worker thread for too long).
protected static int MAX_DOWN_PAGE_SIZE
          The maximum number of contacts that should be sent by the server for each page request.
private static int MAX_PAGES_PER_BATCH
          The number of pages that should be requested in a single RPG request (using batching).
private  int mBatchFirstPageNo
          First page number requested in the batch.
private  int mBatchNoOfPages
          Total number of pages requested by batch.
private  int mBatchPageReceivedCount
          Number of pages received so far from the batch
private  java.util.ArrayList<Contact> mContactsChangedList
          List of all contacts received from server which are also present in the local database.
private  long mDbSyncTime
          Used to monitor how much time is spent reading or writing to the NowPlus database.
private  java.util.ArrayList<ContactsTable.ContactIdInfo> mDeleteContactList
          List of all contacts received from the server that need to be deleted from the database.
private  java.util.ArrayList<ContactDetail> mDeleteDetailList
          List of all contact details received from the server that need to be deleted from the local database.
private  java.lang.Integer mFromRevision
          This will be set to the current revision of the contacts in the NowPlus database.
protected  DownloadServerContacts.InternalState mInternalState
          Current internal state of the processor.
private  boolean mIsComplete
          Set to true when there are no more pages to fetch from the server.
private  int mLastPageSize
          Number of contacts contained in the last page.
private  java.util.ArrayList<Contact> mModifyContactList
          List of all contacts received from the server that need to be modified in the database.
private  java.util.ArrayList<ContactDetail> mModifyDetailList
          List of all contact details received from the server that need to be modified in the local database.
private  int mNoOfPagesDone
          Total number of pages done (used with mTotalNoOfPages for calculating progress)
private  java.util.ArrayList<java.lang.Long> mOrderedServerIdList
          List of contact server IDs from the NowPlus database in ascending order.
protected  java.util.Map<java.lang.Integer,java.lang.Integer> mPageReqIds
          Maintains all the request IDs when sending a batch of requests.
private  boolean mSyncDataPending
          Flag indicating that there is some data in either the mAddContactList, mModifyContactList, mDeleteContactList, mAddDetailList, mModifyDetailList or mDeleteDetailList list.
private  java.lang.Integer mToRevision
          The revision that we require from the server.
private  long mTotalContactsAdded
          Counts the number of contacts added (for use when profiling)
private  java.lang.Integer mTotalNoOfPages
          Total number of pages we are expecting from the server.
private static long NANOSECONDS_IN_MS
          Helper constant for converting between milliseconds and nanoseconds.
private static long TIMEOUT_BETWEEN_PAGES_MS
          Timeout between each run of the engine.
 
Fields inherited from class com.vodafone360.people.engine.contactsync.BaseSyncProcessor
mCallback, mDb, mFailureList
 
Constructor Summary
protected DownloadServerContacts(IContactSyncCallback callback, DatabaseHelper db)
          Processor constructor.
 
Method Summary
private  boolean addContactList()
          Adds contacts received from the server (listed in the mAddContactList list) into the local database.
private  boolean addDetailList()
          Adds contact details to the local database based on changes received from the server (listed in the mAddDetailList list).
private  boolean checkContactMods(Contact srcContact, Contact destContact)
          Compares two contacts to check for any differences.
private  boolean deleteContactList()
          Deletes contacts from the local database based on changes received from the server (listed in the mDeleteContactList list).
private  boolean deleteDetailList()
          Deletes contact details from the local database based on changes received from the server (listed in the mDeleteDetailList list).
protected  void doCancel()
          Called by framework when contact sync is cancelled.
protected  void doStart()
          Called by framework to start the processor running.
private  void downloadSyncSuccessful()
          Called when the processor has finished downloading the contact changes.
private  ServiceStatus fetchFirstBatch()
          Requests first page of contact changes from the server.
private  ServiceStatus fetchNextBatch()
          Requests the next batch of contact change pages from the server
protected  void finalize()
          For debug only - to show when the garbage collector destroys the processor
private static int findIdInOrderedList(long id, java.util.List<java.lang.Long> list)
          Binary search function to find a contact server ID in an ordered list.
private  boolean modifyContactList()
          Updates the local database with contacts received from the server (listed in the mModifyContactList list).
private  boolean modifyDetailList()
          Modifies contact details in the local database based on changes received from the server (listed in the mModifyDetailList list).
 void onTimeoutEvent()
          Called by framework when a timeout expires.
 void processCommsResponse(ResponseQueue.Response resp)
          Called by framework when a response is received from the server.
private  void processContactChangesNextPage()
          Processes next page of modified contacts in the mContactsChangedList.
private  ServiceStatus syncContactChangesPage(ContactChanges changes)
          Processes the response from the server.
private  ServiceStatus syncDownContact(Contact srcContact, Contact destContact)
          Modifies a contact from the NowPlus database to match the one received from the server
private  void updateProgressUi(Contact contact, int incOfCurrentPage)
          Check the given contact to see if it has a valid name, and if so send it to be shown in the contacts sync progress UI.
 
Methods inherited from class com.vodafone360.people.engine.contactsync.BaseSyncProcessor
cancel, complete, getEngine, markDbChanged, onComplete, setReqId, setSyncStatus, setTimeout, start
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NANOSECONDS_IN_MS

private static final long NANOSECONDS_IN_MS
Helper constant for converting between milliseconds and nanoseconds.

See Also:
Constant Field Values

MAX_DOWN_PAGE_SIZE

protected static final int MAX_DOWN_PAGE_SIZE
The maximum number of contacts that should be sent by the server for each page request.

See Also:
Constant Field Values

MAX_PAGES_PER_BATCH

private static final int MAX_PAGES_PER_BATCH
The number of pages that should be requested in a single RPG request (using batching).

See Also:
Constant Field Values

MAX_CONTACT_CHANGES_PER_PAGE

private static final int MAX_CONTACT_CHANGES_PER_PAGE
Maximum number of database updates for each engine run (to ensure the processor doesn't block the worker thread for too long).

See Also:
Constant Field Values

TIMEOUT_BETWEEN_PAGES_MS

private static final long TIMEOUT_BETWEEN_PAGES_MS
Timeout between each run of the engine. Normally set to 0 to ensure the engine will run as soon as possible. This is used to allow other engines to be run between lengthy sync operations.

See Also:
Constant Field Values

EXTRA_DEBUG_TRACE

private static final boolean EXTRA_DEBUG_TRACE
Set to true for extra logcat trace (debug only)

See Also:
Constant Field Values

mFromRevision

private java.lang.Integer mFromRevision
This will be set to the current revision of the contacts in the NowPlus database. For first time sync will be set to 0. The current revision number is persisted in the database.


mToRevision

private java.lang.Integer mToRevision
The revision that we require from the server. For the first page this is set to -1 to indicate we want the latest revision. For subsequent pages will be set to the version anchor received from the server (to ensure all pages fetched are for the same version).


mTotalNoOfPages

private java.lang.Integer mTotalNoOfPages
Total number of pages we are expecting from the server. This is received as part of the server response for the first page.


mBatchFirstPageNo

private int mBatchFirstPageNo
First page number requested in the batch. The remaining page numbers will follow from this sequentially.


mBatchNoOfPages

private int mBatchNoOfPages
Total number of pages requested by batch.


mBatchPageReceivedCount

private int mBatchPageReceivedCount
Number of pages received so far from the batch


mNoOfPagesDone

private int mNoOfPagesDone
Total number of pages done (used with mTotalNoOfPages for calculating progress)


mLastPageSize

private int mLastPageSize
Number of contacts contained in the last page. This is only known once the last page have been received and is a major flaw in the protocol as it makes it difficult to calculate sync progress information.


mOrderedServerIdList

private final java.util.ArrayList<java.lang.Long> mOrderedServerIdList
List of contact server IDs from the NowPlus database in ascending order. Used to quickly determine if a contact received from the server is a new or modified contact.


mContactsChangedList

private final java.util.ArrayList<Contact> mContactsChangedList
List of all contacts received from server which are also present in the local database. This is a list of all contacts received from the server excluding new contacts.


mAddContactList

private final java.util.ArrayList<Contact> mAddContactList
List of all contacts received from the server that need to be added to the database.


mModifyContactList

private final java.util.ArrayList<Contact> mModifyContactList
List of all contacts received from the server that need to be modified in the database.


mDeleteContactList

private final java.util.ArrayList<ContactsTable.ContactIdInfo> mDeleteContactList
List of all contacts received from the server that need to be deleted from the database.


mAddDetailList

private final java.util.ArrayList<ContactDetail> mAddDetailList
List of all contact details received from the server that need to be added to the local database.


mModifyDetailList

private final java.util.ArrayList<ContactDetail> mModifyDetailList
List of all contact details received from the server that need to be modified in the local database.


mDeleteDetailList

private final java.util.ArrayList<ContactDetail> mDeleteDetailList
List of all contact details received from the server that need to be deleted from the local database.


mPageReqIds

protected final java.util.Map<java.lang.Integer,java.lang.Integer> mPageReqIds
Maintains all the request IDs when sending a batch of requests.


mSyncDataPending

private boolean mSyncDataPending
Flag indicating that there is some data in either the mAddContactList, mModifyContactList, mDeleteContactList, mAddDetailList, mModifyDetailList or mDeleteDetailList list.


mInternalState

protected DownloadServerContacts.InternalState mInternalState
Current internal state of the processor.


mDbSyncTime

private long mDbSyncTime
Used to monitor how much time is spent reading or writing to the NowPlus database. This is for displaying logcat information which can be used when profiling the contact sync engine.


mIsComplete

private boolean mIsComplete
Set to true when there are no more pages to fetch from the server.


mTotalContactsAdded

private long mTotalContactsAdded
Counts the number of contacts added (for use when profiling)

Constructor Detail

DownloadServerContacts

protected DownloadServerContacts(IContactSyncCallback callback,
                                 DatabaseHelper db)
Processor constructor.

Parameters:
callback - Callback interface for accessing the contact sync engine.
db - Database used for modifying contacts
Method Detail

finalize

protected void finalize()
For debug only - to show when the garbage collector destroys the processor

Overrides:
finalize in class java.lang.Object

doStart

protected void doStart()
Called by framework to start the processor running. Requests the first page of contact changes from the server.

Specified by:
doStart in class BaseSyncProcessor

fetchFirstBatch

private ServiceStatus fetchFirstBatch()
Requests first page of contact changes from the server.

Returns:
SUCCESS or a suitable error code.

fetchNextBatch

private ServiceStatus fetchNextBatch()
Requests the next batch of contact change pages from the server

Returns:
SUCCESS or a suitable error code

doCancel

protected void doCancel()
Called by framework when contact sync is cancelled. Not used, the server response will simply be ignored.

Specified by:
doCancel in class BaseSyncProcessor

processCommsResponse

public void processCommsResponse(ResponseQueue.Response resp)
Called by framework when a response is received from the server. In case of the first page this will only be called if the request ID matches. Processes the page and if the page is the last one in the batch, sends a new batch of requests to the server.

Specified by:
processCommsResponse in class BaseSyncProcessor
Parameters:
response - from server

updateProgressUi

private void updateProgressUi(Contact contact,
                              int incOfCurrentPage)
Check the given contact to see if it has a valid name, and if so send it to be shown in the contacts sync progress UI.

Parameters:
contact - Contact which has been downloaded.
incOfCurrentPage - Number of contacts that have so far been downloaded for the current page.

syncContactChangesPage

private ServiceStatus syncContactChangesPage(ContactChanges changes)
Processes the response from the server. First separates new contacts from existing ones, this is to optimise adding new contacts during first time sync.

Parameters:
changes - The contact change information received from the server
Returns:
SUCCESS or a suitable error code

processContactChangesNextPage

private void processContactChangesNextPage()
Processes next page of modified contacts in the mContactsChangedList.


syncDownContact

private ServiceStatus syncDownContact(Contact srcContact,
                                      Contact destContact)
Modifies a contact from the NowPlus database to match the one received from the server

Parameters:
srcContact - Contact received from the server
destContact - Contact as stored in the database
Returns:
SUCCESS or a suitable error code.

findIdInOrderedList

private static int findIdInOrderedList(long id,
                                       java.util.List<java.lang.Long> list)
Binary search function to find a contact server ID in an ordered list.

Parameters:
id - The server ID to find
list - An ordered list of server IDs from the database
Returns:
index of server ID if found, otherwise -1.

checkContactMods

private boolean checkContactMods(Contact srcContact,
                                 Contact destContact)
Compares two contacts to check for any differences. Implemented to avoid updating the database when nothing has changed (to improve performance).

Parameters:
srcContact - Contact received from the server
destContact - Contact as it is in the database.
Returns:
true if the contacts are different, false otherwise.

onTimeoutEvent

public void onTimeoutEvent()
Called by framework when a timeout expires. Timeouts are used by this processor to break up lengthy database update tasks. A timeout of 0 is normally used just to give other engines a chance to run.

Overrides:
onTimeoutEvent in class BaseSyncProcessor

addContactList

private boolean addContactList()
Adds contacts received from the server (listed in the mAddContactList list) into the local database.

Returns:
true if > 0 contacts were added, false otherwise.

modifyContactList

private boolean modifyContactList()
Updates the local database with contacts received from the server (listed in the mModifyContactList list).

Returns:
true if > 0 contacts were modified, false otherwise.

deleteContactList

private boolean deleteContactList()
Deletes contacts from the local database based on changes received from the server (listed in the mDeleteContactList list).

Returns:
true if > 0 contacts were deleted, false otherwise.

addDetailList

private boolean addDetailList()
Adds contact details to the local database based on changes received from the server (listed in the mAddDetailList list).

Returns:
true if > 0 contact details were added, false otherwise.

modifyDetailList

private boolean modifyDetailList()
Modifies contact details in the local database based on changes received from the server (listed in the mModifyDetailList list).

Returns:
true if > 0 contact details were modified, false otherwise.

deleteDetailList

private boolean deleteDetailList()
Deletes contact details from the local database based on changes received from the server (listed in the mDeleteDetailList list).

Returns:
true if > 0 contact details were deleted, false otherwise.

downloadSyncSuccessful

private void downloadSyncSuccessful()
Called when the processor has finished downloading the contact changes. Notifies the contact sync engine.



JavaDoc