com.vodafone360.people.engine.contactsync
Class NativeImporter

java.lang.Object
  extended by com.vodafone360.people.engine.contactsync.NativeImporter

public class NativeImporter
extends java.lang.Object

The NativeImporter class is responsible for importing contacts from the native address book to the people database. To do so, it compares the native address book and the people database to find new changes and update the people database accordingly. Usage: - instantiate the class - call the tick() method until isDone() returns true - current progress can be obtained via getPosition() / getCount() - check the getResult() to get a status (OK, KO...)


Nested Class Summary
private static class NativeImporter.NCCComparator
          Native ContactChange Comparator class.
 
Field Summary
private  java.lang.String GOOGLE_ACCOUNT_TYPE
           
private  NativeContactsApi.Account[] mAccounts
          The array of accounts from where to import the native contacts.
private static int MAX_CONTACTS_OPERATION_COUNT
          Number of contacts to be processed "per tick".
private  int mCurrentDeletedId
          The index of the current deleted people id.
private  int mCurrentNativeId
          The index of the current native id.
private  int mCurrentPeopleId
          The index in the current people id.
private  DynamicArrayLong mDeletedIds
          Array to store the people ids of contacts to delete.
private  boolean mIsFirstImportOn2X
          Boolean that tracks if the first time Import for 2.X is ongoing.
private  NativeContactsApi mNativeContactsApi
          Handler to the Native Contacts API.
private  long[] mNativeContactsIds
          The list of native ids from the native side.
private  NativeImporter.NCCComparator mNCCC
          Instance of a ContactChange comparator.
private  PeopleContactsApi mPeopleContactsApi
          Handler to the People Contacts API.
private  long[] mPeopleNativeContactsIds
          The list of native ids from the people side.
private  int mProcessedIds
          The current count of processed ids.
private  int mResult
          The result status.
private  int mState
          The current state.
private  int mTotalIds
          The total count of ids to process (native database + people database).
static int RESULT_ERROR
          The undefined result when the NativeImporter has not been run yet.
static int RESULT_OK
          The ok result when the NativeImporter has finished successfully.
static int RESULT_UNDEFINED
          The undefined result when the NativeImporter has not been run yet.
private static int STATE_DONE
          Internal state representing the task to perform: final state, nothing else to perform.
private static int STATE_GET_IDS_LISTS
          Internal state representing the task to perform: gets the list of native contacts ids and people contacts ids.
private static int STATE_ITERATE_THROUGH_IDS
          Internal state representing the task to perform: iterates through the list of ids to find differences (i.e. added contact, modified contact, deleted contact).
private static int STATE_PROCESS_DELETED
          Internal state representing the task to perform: process the list of deleted contacts (i.e. delete the contacts from the people database).
 
Constructor Summary
NativeImporter(PeopleContactsApi pca, NativeContactsApi nca, boolean firstTimeImport)
          Constructor.
 
Method Summary
private  void addNewContact(long nativeId)
          Adds a new contact to the people database.
private  boolean areContactChangeValuesEqualsPlusFix(ContactChange[] masterChanges, int masterIndex, ContactChange[] newChanges, int newIndex)
          Tells whether or not a master change is equal to a new change and performs some modifications on the provided new change in the specific case of KEY_VCARD_ORG on Android 1.X.
private  void checkExistingContact(long nativeId)
          Check changes between an existing contact on both native and people database.
private  void complete(int result)
          Sets the internal state to DONE with the provided result status.
private  ContactChange[] computeDelta(ContactChange[] masterChanges, ContactChange[] newChanges)
          Computes the difference between the provided arrays of ContactChange.
private  void forceNabDetailId(ContactChange[] contact)
          Forces a native detail id to detail that do not have one by setting it to the native contact id.
 int getCount()
          Gets the total number of ids to process.
private  void getIdsLists()
          Gets the list of native and people contacts ids.
 int getPosition()
          Gets the current position in the list of ids.
 int getResult()
          Gets the import result.
private  void initAccounts(boolean firstTimeImport)
          Sets the accounts used to import the native contacs.
private  boolean isContactChangeKeySupported(ContactChange change)
          Determines whether or not a ContactChange key is supported on native side.
 boolean isDone()
          Returns the import state.
private  void iterateThroughNativeIds()
          Iterates through the list of native and People ids to detect changes.
private  void processDeleted()
          Deletes the contacts that were added the deleted array.
private  void removeNativeIds(ContactChange[] contact)
          Removes the native ids in case of a first time import on the Android 2.X platform.
private  void setDetailForDeleteOrUpdate(ContactChange[] contactChanges, int index)
          Sets a detail as to be deleted or updated.
private  void sortContactChanges(ContactChange[] changes)
          Sorts the provided array of ContactChange by key and native id.
 boolean tick()
          Tick method to call each time there is time for processing the native contacts import.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GOOGLE_ACCOUNT_TYPE

private final java.lang.String GOOGLE_ACCOUNT_TYPE
See Also:
Constant Field Values

RESULT_UNDEFINED

public static final int RESULT_UNDEFINED
The undefined result when the NativeImporter has not been run yet.

See Also:
getResult(), Constant Field Values

RESULT_OK

public static final int RESULT_OK
The ok result when the NativeImporter has finished successfully.

See Also:
getResult(), Constant Field Values

RESULT_ERROR

public static final int RESULT_ERROR
The undefined result when the NativeImporter has not been run yet.

See Also:
getResult(), Constant Field Values

MAX_CONTACTS_OPERATION_COUNT

private static final int MAX_CONTACTS_OPERATION_COUNT
Number of contacts to be processed "per tick".

See Also:
tick(), Constant Field Values

mPeopleContactsApi

private PeopleContactsApi mPeopleContactsApi
Handler to the People Contacts API.


mNativeContactsApi

private NativeContactsApi mNativeContactsApi
Handler to the Native Contacts API.


STATE_GET_IDS_LISTS

private static final int STATE_GET_IDS_LISTS
Internal state representing the task to perform: gets the list of native contacts ids and people contacts ids.

See Also:
Constant Field Values

STATE_ITERATE_THROUGH_IDS

private static final int STATE_ITERATE_THROUGH_IDS
Internal state representing the task to perform: iterates through the list of ids to find differences (i.e. added contact, modified contact, deleted contact).

See Also:
Constant Field Values

STATE_PROCESS_DELETED

private static final int STATE_PROCESS_DELETED
Internal state representing the task to perform: process the list of deleted contacts (i.e. delete the contacts from the people database). TODO: remove this state

See Also:
Constant Field Values

STATE_DONE

private static final int STATE_DONE
Internal state representing the task to perform: final state, nothing else to perform.

See Also:
Constant Field Values

mState

private int mState
The current state.


mNativeContactsIds

private long[] mNativeContactsIds
The list of native ids from the native side.


mPeopleNativeContactsIds

private long[] mPeopleNativeContactsIds
The list of native ids from the people side.


mTotalIds

private int mTotalIds
The total count of ids to process (native database + people database).


mProcessedIds

private int mProcessedIds
The current count of processed ids.


mCurrentNativeId

private int mCurrentNativeId
The index of the current native id.

See Also:
mNativeContactsIds

mCurrentPeopleId

private int mCurrentPeopleId
The index in the current people id.

See Also:
mPeopleNativeContactsIds

mCurrentDeletedId

private int mCurrentDeletedId
The index of the current deleted people id.

See Also:
mDeletedIds

mDeletedIds

private DynamicArrayLong mDeletedIds
Array to store the people ids of contacts to delete.


mResult

private int mResult
The result status.


mNCCC

private NativeImporter.NCCComparator mNCCC
Instance of a ContactChange comparator.


mAccounts

private NativeContactsApi.Account[] mAccounts
The array of accounts from where to import the native contacts. Note: if null, will import from the platform default account.


mIsFirstImportOn2X

private boolean mIsFirstImportOn2X
Boolean that tracks if the first time Import for 2.X is ongoing.

Constructor Detail

NativeImporter

public NativeImporter(PeopleContactsApi pca,
                      NativeContactsApi nca,
                      boolean firstTimeImport)
Constructor.

Parameters:
pca - handler to the People contacts Api
nca - handler to the Native contacts Api
firstTimeImport - true if we import from native for the first time, false if not
Method Detail

initAccounts

private void initAccounts(boolean firstTimeImport)
Sets the accounts used to import the native contacs.


complete

private void complete(int result)
Sets the internal state to DONE with the provided result status.

Parameters:
result - the result status to set

tick

public boolean tick()
Tick method to call each time there is time for processing the native contacts import. Note: the method will block for some time to process a certain amount of contact then will return. It will have to be called until it returns true meaning that the import is over.

Returns:
true when the import task is finished, false if not

isDone

public boolean isDone()
Returns the import state.

Returns:
true if the import is finished, false if not

getResult

public int getResult()
Gets the import result.

Returns:
the import result
See Also:
RESULT_OK, RESULT_ERROR, RESULT_UNDEFINED

getPosition

public int getPosition()
Gets the current position in the list of ids. This can be used to track the current progress.

Returns:
the last processed id position in the list of ids
See Also:
getCount()

getCount

public int getCount()
Gets the total number of ids to process.

Returns:
the number of ids to process

getIdsLists

private void getIdsLists()
Gets the list of native and people contacts ids.


iterateThroughNativeIds

private void iterateThroughNativeIds()
Iterates through the list of native and People ids to detect changes.


processDeleted

private void processDeleted()
Deletes the contacts that were added the deleted array.


addNewContact

private void addNewContact(long nativeId)
Adds a new contact to the people database.


checkExistingContact

private void checkExistingContact(long nativeId)
Check changes between an existing contact on both native and people database.

Parameters:
nativeId - the native id of the contact to check

sortContactChanges

private void sortContactChanges(ContactChange[] changes)
Sorts the provided array of ContactChange by key and native id. Note: the method will rearrange the provided array.

Parameters:
changes - the ContactChange array to sort

computeDelta

private ContactChange[] computeDelta(ContactChange[] masterChanges,
                                     ContactChange[] newChanges)
Computes the difference between the provided arrays of ContactChange. The delta are the changes to apply to the master ContactChange array to make it similar to the new changes ContactChange array. (i.e. delete details, add details or modify details) NOTE: to help the GC, some provided ContactChange may be modified and returned by the method instead of creating new ones.

Parameters:
masterChanges - the master array of ContactChange (i.e. the original one)
newChanges - the new ContactChange array (i.e. contains the new version of a contact)
Returns:
null if there are no changes or the contact is being deleted, an array for ContactChange to apply to the master contact if differences where found

removeNativeIds

private void removeNativeIds(ContactChange[] contact)
Removes the native ids in case of a first time import on the Android 2.X platform.

Parameters:
contact - the contact to clear from native ids

forceNabDetailId

private void forceNabDetailId(ContactChange[] contact)
Forces a native detail id to detail that do not have one by setting it to the native contact id.

Parameters:
contact - the contact to fix

areContactChangeValuesEqualsPlusFix

private boolean areContactChangeValuesEqualsPlusFix(ContactChange[] masterChanges,
                                                    int masterIndex,
                                                    ContactChange[] newChanges,
                                                    int newIndex)
Tells whether or not a master change is equal to a new change and performs some modifications on the provided new change in the specific case of KEY_VCARD_ORG on Android 1.X.

Parameters:
masterChanges - the array of master changes
masterIndex - the index of the change within the array of master changes
newChanges - the array of new changes
newIndex - the index of the change within the array of new changes
Returns:
true if the changes are equals, false otherwise

setDetailForDeleteOrUpdate

private void setDetailForDeleteOrUpdate(ContactChange[] contactChanges,
                                        int index)
Sets a detail as to be deleted or updated. Note: in the general case, the detail has to be deleted but in the specific situation of KEY_VCARD_ORG and Android 1.X, it may have to be updated instead

Parameters:
contactChanges - the array of ContactChange
index - the index of the ContactChange to set in the array of ContactChange

isContactChangeKeySupported

private boolean isContactChangeKeySupported(ContactChange change)
Determines whether or not a ContactChange key is supported on native side. Note: it also handle the non generic case of KEY_VCARD_ORG on Android 1.X

Parameters:
change - the ContactChange to check
Returns:
true if supported, false if not


JavaDoc