com.vodafone360.people.database
Class DatabaseHelper

java.lang.Object
  extended by SQLiteOpenHelper
      extended by com.vodafone360.people.database.DatabaseHelper

public class DatabaseHelper
extends SQLiteOpenHelper

The main interface to the client database.

The DATABASE_VERSION field must be increased each time any change is made to the database schema. This includes any changes to the table name or fields in table classes and any change to persistent settings.

All database functionality should be implemented in one of the table Table or Utility sub classes

Version:
%I%, %G%

Nested Class Summary
static class DatabaseHelper.DatabaseChangeType
          An instance of this enum is passed to database change listeners to define the database change type.
static class DatabaseHelper.ServerIdInfo
          Used for passing server contact IDs around.
static class DatabaseHelper.ThumbnailInfo
          Used for passing contact avatar information around.
 
Field Summary
private static java.lang.String DATABASE_NAME
          The name of the database file.
static java.lang.String DATABASE_PRESENCE
          The name of the presence database file which is in memory.
private static int DATABASE_VERSION
          Contains the database version.
private static java.lang.String LOG_TAG
           
private  Context mContext
           
private  boolean mDbUpgradeRequired
           
private  boolean mMeProfileAvatarChangedFlag
           
private  java.util.List<Handler> mUiEventCallbackList
           
private static java.lang.String QUERY_NATIVE_SYNCABLE_CONTACTS_LOCAL_IDS
          SELECT DISTINCT LocalId FROM NativeChangeLog UNION SELECT DISTINCT LocalId FROM ContactDetails WHERE NativeSyncId IS NULL OR NativeSyncId <> -1 ORDER BY 1
 
Constructor Summary
DatabaseHelper(Context context)
          Public Constructor.
DatabaseHelper(Context context, java.lang.String name)
          Constructor.
 
Method Summary
 ServiceStatus addActivities(java.util.List<ActivityItem> activityList)
          Add a list of new activities to the Activities table.
 ServiceStatus addContact(Contact contact)
          Adds a contact to the database and fires an internal database change event.
 ServiceStatus addContactDetail(ContactDetail detail)
          Adds a contact detail to the database and fires an internal database change event.
 ServiceStatus addContactToGroup(long localContactId, long groupId)
          Puts a contact into a group.
 void addEventCallback(Handler uiHandler)
          Add a database change listener.
 boolean addNativeContact(ContactChange[] contact)
          Adds a native contact to the people database and makes sure that the related tables are updated (Contact, ContactDetail, ContactSummary and Activities).
 ServiceStatus addTimelineEvents(java.util.ArrayList<ActivitiesTable.TimelineSummaryItem> syncItemList, boolean isCallLog)
          Add timeline events to the database.
 ContactDetail convertContactChange(ContactChange change)
          Converts a ContactChange object into an equivalent ContactDetail object.
private  Contact convertNativeContactChanges(ContactChange[] contactChanges)
          Converts an array of ContactChange into a Contact object.
 java.lang.String copyDatabaseToSd(java.lang.String info)
          Copies a snapshot of the database to the SD Card - Used for testing only.
static java.lang.String createWhereClauseFromList(java.lang.String field, java.lang.Object[] itemList, java.lang.String clause)
          Utility function to create a where clause string from a list of conditions.
 ServiceStatus deleteActivities(java.lang.Integer flag)
          Removes all the status or timeline activities from the database.
 ServiceStatus deleteAllGroups()
          Removes all groups from the database.
 ServiceStatus deleteContact(long localContactID)
          Deletes a contact from the database and fires an internal database change event.
 boolean deleteContactChanges(java.util.List<ContactChangeLogTable.ContactChangeInfo> changeInfoList)
          Remove contact changes from the change log.
 ServiceStatus deleteContactDetail(long localContactDetailID)
          Deletes a contact detail from the database.
 ServiceStatus deleteContactFromGroup(long localContactId, long groupId)
          Removes a group from a contact.
private  void deleteDatabase()
          Deletes the database and then fires a Database Changed Event to the UI.
private static boolean deleteDirectory(java.io.File path)
          Deletes a directory and all its contents including sub-directories.
private  boolean deleteThumbnail(java.lang.Long localContactID)
          Deletes a given Thumbnail
 ServiceStatus deleteTimelineActivity(MainApplication mApplication, ActivitiesTable.TimelineSummaryItem timelineItem, boolean isTimelineAll)
          Removes the selected timeline activity from the database.
static boolean doDetailsMatch(ContactDetail d1, ContactDetail d2)
          Utility function which compares two contact details to determine if they refer to the same detail (the values may be different).
 ServiceStatus fetchActivitiesIds(java.util.List<java.lang.Long> activityIdList, java.lang.Long timeStamp)
          Fetches a list of activity IDs from a given time.
private  ServiceStatus fetchBaseContact(long localContactId, Contact baseContact, SQLiteDatabase mDb)
          Fetches the outer contact object information (no details, groups or sources are included).
 ServiceStatus fetchContact(long localContactId, Contact contact)
          Fetches a contact from the database by its localContactId.
 ServiceStatus fetchContactByServerId(java.lang.Long contactServerId, Contact contact)
          Fetches a contact, given a server Id.
 ServiceStatus fetchContactDetail(long localDetailId, ContactDetail detail)
          Fetches a contact detail from the database.
 ServiceStatus fetchContactInfo(java.lang.String phoneNumber, Contact contact, ContactDetail phoneDetail)
          Searches the database for a contact with a given phone number.
 java.util.List<Contact> fetchContactList()
           
 ServiceStatus fetchLogonCredentials(LoginDetails details)
          Fetches the user's logon credentials from the database.
 ServiceStatus fetchLogonCredentialsAndPublicKey(LoginDetails details, PublicKeyDetails pubKeyDetails)
          Fetches the user's logon credentials and public key information from the database.
 PersistSettings fetchOption(PersistSettings.Option option)
          Fetches a setting from the database.
 java.lang.Long fetchServerId(long localContactId)
          Fetches the server Id of a contact.
 int fetchThumbnailUrlCount()
          Fetches the number of Contact Avatars which have not yet been loaded.
 ServiceStatus fetchThumbnailUrls(java.util.List<DatabaseHelper.ThumbnailInfo> thumbInfoList, int firstIndex, int count)
          Fetches Avatar URLs from the database for all contacts which have an Avatar and have not yet been loaded.
 ServiceStatus fetchThumbnailUrlsForContacts(java.util.List<DatabaseHelper.ThumbnailInfo> thumbInfoList, java.util.List<Contact> contactList)
          Fetches Avatar URLs from the database for all contacts from contactList which have an Avatar and have not yet been loaded.
 Cursor fetchTimelineEvents(java.lang.Long timeStamp, ActivitiesTable.TimelineNativeTypes[] types)
          Fetches timeline events from a given time.
 boolean findNativeContact(Contact c)
          find the native contact in the database.
 void fireDatabaseChangedEvent(DatabaseHelper.DatabaseChangeType type, boolean isExternal)
          Fetches fires a database change event to the listeners.
private  void fireEventToUi(ServiceUiRequest event, int arg1, int arg2, java.lang.Object data)
          Internal function to send an event to all the listeners.
private  void fireSettingChangedEvent(PersistSettings setting)
          Internal function to fire a setting changed event to listeners.
 long[] getNativeSyncableContactsLocalIds()
           
static boolean hasDetailChanged(ContactDetail currentDetail, ContactDetail newDetail)
          Utility function which compares two contact details to determine if they have the same value.
 boolean isMeProfileAvatarChanged()
          Determines if the me profile avatar needs to be uploaded onto the server.
 void markMeProfileAvatarChanged()
          Stores a flag in the database indicating that the me profile avatar has changed.
 ServiceStatus modifyContactDetail(ContactDetail detail)
          Modifies an existing contact detail in the database.
 boolean modifyContactDetailServerId(java.lang.Long localDetailId, java.lang.Long serverDetailId)
          Modifies the Detail Id stored in the database for a specific contact detail.
 boolean modifyContactServerId(long localId, java.lang.Long serverId, java.lang.Long userId)
          Modifies the server Contact Id and User ID stored in the database for a specific contact.
 ServiceStatus modifyCredentials(LoginDetails details)
          Modifies the user's logon credentials.
 ServiceStatus modifyCredentialsAndPublicKey(LoginDetails details, PublicKeyDetails pubKeyDetails)
          Modifies the user's logon credentials and public key details.
 ServiceStatus modifyMeProfileAvatarChangedFlag(boolean avatarChanged)
          Modifies the Me Profile Avatar Changed Flag.
 void onCreate(SQLiteDatabase db)
          Called the first time the database is generated to create all tables.
 void onOpen(SQLiteDatabase db)
          Called whenever the database is opened.
 void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
          Delete and then recreate a newer database structure.
 Cursor openContactsCursor(java.lang.Long groupFilterId, java.lang.CharSequence constraint)
           
 Cursor openContactSummaryCursor(java.lang.Long groupFilterId, java.lang.CharSequence constraint)
          Fetches a cursor which can be used to iterate through the main contact list.
 void removeEventCallback(Handler uiHandler)
          Removes a database change listener.
 void removeUserData()
          Remove all user data (Thumbnails, Database, Flags) from the device and notifies the engine manager.
private  void setInternalContactId(ContactChange[] contact, long internalContactId)
          Sets the internalContactId for all the ContactChange provided.
 ServiceStatus setOption(PersistSettings setting)
          Modifies a setting in the database.
 void start()
          Called when the Application is first started.
 ServiceStatus syncAddContactDetailList(java.util.List<ContactDetail> detailList, boolean syncToServer, boolean syncToNative)
          Function used by the contact sync engine to add a list of contact details to the database.
 ServiceStatus syncAddContactList(java.util.List<Contact> contactList, boolean syncToServer, boolean syncToNative)
          Function used by the contact sync engine to add a list of contacts to the database.
 ServiceStatus syncDeleteContactDetailList(java.util.List<ContactDetail> contactDetailList, boolean syncToServer, boolean syncToNative)
          Function used by the contact sync engine to delete a list of contact details from the database.
 ServiceStatus syncDeleteContactList(java.util.List<ContactsTable.ContactIdInfo> contactIdList, boolean syncToServer, boolean syncToNative)
          Function used by the contact sync engine to delete a list of contacts from the database.
 ServiceStatus syncModifyContactDetailList(java.util.List<ContactDetail> contactDetailList, boolean syncToServer, boolean syncToNative)
          Function used by the contact sync engine to modify a list of contact details in the database.
 ServiceStatus syncModifyContactList(java.util.List<Contact> contactList, boolean syncToServer, boolean syncToNative)
          Function used by the contact sync engine to modify a list of contacts in the database.
static void trace(boolean write, java.lang.String input)
          Logs Database activity when the Settings.ENABLED_DATABASE_TRACE flag is set to true.
 ServiceStatus updateNameAndStatusInSummary(SQLiteDatabase writableDatabase, long localContactId)
          Updates the ContactSummary table with the new/changed Contact
private  boolean updateTimelineAndContactSummaryWithLegacyCode(Contact contact, SQLiteDatabase writableDb)
          Updates the Timeline and ContactSummary tables with a new contact.
private  void updateTimelineNames(ContactDetail cd, SQLiteDatabase db)
          Updates the contents of the activities table when a contact detail changes.
private  void updateTimelineNames(ContactDetail cd, java.lang.String contactFriendlyName, java.lang.Long serverId, SQLiteDatabase db)
          Updates the contents of the activities table when a contact detail changes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG_TAG

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

DATABASE_NAME

private static final java.lang.String DATABASE_NAME
The name of the database file.

See Also:
Constant Field Values

DATABASE_PRESENCE

public static final java.lang.String DATABASE_PRESENCE
The name of the presence database file which is in memory.

See Also:
Constant Field Values

DATABASE_VERSION

private static final int DATABASE_VERSION
Contains the database version. Must be increased each time the schema is changed.

See Also:
Constant Field Values

mUiEventCallbackList

private final java.util.List<Handler> mUiEventCallbackList

mContext

private Context mContext

mMeProfileAvatarChangedFlag

private boolean mMeProfileAvatarChangedFlag

mDbUpgradeRequired

private boolean mDbUpgradeRequired

QUERY_NATIVE_SYNCABLE_CONTACTS_LOCAL_IDS

private static final java.lang.String QUERY_NATIVE_SYNCABLE_CONTACTS_LOCAL_IDS
SELECT DISTINCT LocalId FROM NativeChangeLog UNION SELECT DISTINCT LocalId FROM ContactDetails WHERE NativeSyncId IS NULL OR NativeSyncId <> -1 ORDER BY 1

Constructor Detail

DatabaseHelper

public DatabaseHelper(Context context)
Public Constructor.

Parameters:
context - Android context

DatabaseHelper

public DatabaseHelper(Context context,
                      java.lang.String name)
Constructor.

Parameters:
context - the Context where to create the database
name - the name of the database
Method Detail

onCreate

public void onCreate(SQLiteDatabase db)
Called the first time the database is generated to create all tables.

Parameters:
db - An open SQLite database object

onOpen

public void onOpen(SQLiteDatabase db)
Called whenever the database is opened.

Parameters:
db - An open SQLite database object

onUpgrade

public void onUpgrade(SQLiteDatabase db,
                      int oldVersion,
                      int newVersion)
Delete and then recreate a newer database structure. Note: Only called from tests.

Parameters:
db - An open SQLite database object
oldVersion - The current database version on the device
newVersion - The required database version

deleteDatabase

private void deleteDatabase()
Deletes the database and then fires a Database Changed Event to the UI.


start

public void start()
Called when the Application is first started.


addContact

public ServiceStatus addContact(Contact contact)
Adds a contact to the database and fires an internal database change event.

Parameters:
contact - A Contact object which contains the details to be added
Returns:
SUCCESS or a suitable error code
See Also:
deleteContact(long), addContactDetail(ContactDetail), modifyContactDetail(ContactDetail), deleteContactDetail(long), addContactToGroup(long, long), deleteContactFromGroup(long, long)

deleteContact

public ServiceStatus deleteContact(long localContactID)
Deletes a contact from the database and fires an internal database change event.

Parameters:
localContactID - The local ID of the contact to delete
Returns:
SUCCESS or a suitable error code
See Also:
addContact(Contact), addContactDetail(ContactDetail), modifyContactDetail(ContactDetail), deleteContactDetail(long), addContactToGroup(long, long), deleteContactFromGroup(long, long)

addContactDetail

public ServiceStatus addContactDetail(ContactDetail detail)
Adds a contact detail to the database and fires an internal database change event.

Parameters:
detail - A ContactDetail object which contains the detail to add
Returns:
SUCCESS or a suitable error code
Throws:
java.lang.NullPointerException - When detail is NULL
See Also:
modifyContactDetail(ContactDetail), deleteContactDetail(long), addContact(Contact), deleteContact(long), addContactToGroup(long, long), deleteContactFromGroup(long, long)

modifyContactDetail

public ServiceStatus modifyContactDetail(ContactDetail detail)
Modifies an existing contact detail in the database. Also fires an internal database change event.

Parameters:
detail - A ContactDetail object which contains the detail to add
Returns:
SUCCESS or a suitable error code
See Also:
addContactDetail(ContactDetail), deleteContactDetail(long), addContact(Contact), deleteContact(long), addContactToGroup(long, long), deleteContactFromGroup(long, long)

deleteContactDetail

public ServiceStatus deleteContactDetail(long localContactDetailID)
Deletes a contact detail from the database. Also fires an internal database change event.

Parameters:
localContactDetailID - The local ID of the detail to delete
Returns:
SUCCESS or a suitable error code
See Also:
addContactDetail(ContactDetail), modifyContactDetail(ContactDetail), addContact(Contact), deleteContact(long), addContactToGroup(long, long), deleteContactFromGroup(long, long)

modifyContactServerId

public boolean modifyContactServerId(long localId,
                                     java.lang.Long serverId,
                                     java.lang.Long userId)
Modifies the server Contact Id and User ID stored in the database for a specific contact.

Parameters:
localId - The local Id of the contact to modify
serverId - The new server Id
userId - The new user Id
Returns:
true if successful
See Also:
fetchContactByServerId(Long, Contact), fetchServerId(long)

modifyContactDetailServerId

public boolean modifyContactDetailServerId(java.lang.Long localDetailId,
                                           java.lang.Long serverDetailId)
Modifies the Detail Id stored in the database for a specific contact detail.

Parameters:
localDetailId - The local Id of the contact detail to modify
serverDetailId - The new server Id
Returns:
true if successful

fetchLogonCredentials

public ServiceStatus fetchLogonCredentials(LoginDetails details)
Fetches the user's logon credentials from the database.

Parameters:
details - An empty LoginDetails object which will be filled on return
Returns:
SUCCESS or a suitable error code
See Also:
fetchLogonCredentialsAndPublicKey(LoginDetails, PublicKeyDetails), modifyCredentials(LoginDetails), modifyCredentialsAndPublicKey(LoginDetails, PublicKeyDetails)

fetchLogonCredentialsAndPublicKey

public ServiceStatus fetchLogonCredentialsAndPublicKey(LoginDetails details,
                                                       PublicKeyDetails pubKeyDetails)
Fetches the user's logon credentials and public key information from the database.

Parameters:
details - An empty LoginDetails object which will be filled on return
pubKeyDetails - An empty PublicKeyDetails object which will be filled on return
Returns:
SUCCESS or a suitable error code
See Also:
fetchLogonCredentials(LoginDetails), modifyCredentials(LoginDetails), modifyCredentialsAndPublicKey(LoginDetails, PublicKeyDetails)

modifyCredentials

public ServiceStatus modifyCredentials(LoginDetails details)
Modifies the user's logon credentials. Note: Only called from tests.

Parameters:
details - The login details to store
Returns:
SUCCESS or a suitable error code
See Also:
fetchLogonCredentials(LoginDetails), fetchLogonCredentialsAndPublicKey(LoginDetails, PublicKeyDetails), modifyCredentialsAndPublicKey(LoginDetails, PublicKeyDetails)

modifyCredentialsAndPublicKey

public ServiceStatus modifyCredentialsAndPublicKey(LoginDetails details,
                                                   PublicKeyDetails pubKeyDetails)
Modifies the user's logon credentials and public key details.

Parameters:
details - The login details to store
pubKeyDetails - The public key details to store
Returns:
SUCCESS or a suitable error code
See Also:
fetchLogonCredentials(LoginDetails), fetchLogonCredentialsAndPublicKey(LoginDetails, PublicKeyDetails), modifyCredentials(LoginDetails)

deleteContactChanges

public boolean deleteContactChanges(java.util.List<ContactChangeLogTable.ContactChangeInfo> changeInfoList)
Remove contact changes from the change log. This will be called once the changes have been sent to the server.

Parameters:
changeInfoList - A list of changeInfoIDs (none of the other fields in the ContactChangeLogTable.ContactChangeInfo object are required).
Returns:
true if successful

fetchOption

public PersistSettings fetchOption(PersistSettings.Option option)
Fetches a setting from the database.

Parameters:
option - The option required.
Returns:
A PersistSettings object which contains the setting data if successful, null otherwise
See Also:
setOption(PersistSettings)

setOption

public ServiceStatus setOption(PersistSettings setting)
Modifies a setting in the database.

Parameters:
setting - A PersistSetting object which is populated with an option set to a value.
Returns:
SUCCESS or a suitable error code
See Also:
fetchOption(com.vodafone360.people.service.PersistSettings.Option)

deleteAllGroups

public ServiceStatus deleteAllGroups()
Removes all groups from the database.

Returns:
SUCCESS or a suitable error code

fetchThumbnailUrls

public ServiceStatus fetchThumbnailUrls(java.util.List<DatabaseHelper.ThumbnailInfo> thumbInfoList,
                                        int firstIndex,
                                        int count)
Fetches Avatar URLs from the database for all contacts which have an Avatar and have not yet been loaded.

Parameters:
thumbInfoList - An empty list where the DatabaseHelper.ThumbnailInfo objects will be stored containing the URLs
firstIndex - The 0-based index of the first item to fetch from the database
count - The maximum number of items to fetch
Returns:
SUCCESS or a suitable error code
See Also:
DatabaseHelper.ThumbnailInfo, fetchThumbnailUrlCount()

fetchThumbnailUrlsForContacts

public ServiceStatus fetchThumbnailUrlsForContacts(java.util.List<DatabaseHelper.ThumbnailInfo> thumbInfoList,
                                                   java.util.List<Contact> contactList)
Fetches Avatar URLs from the database for all contacts from contactList which have an Avatar and have not yet been loaded.

Parameters:
thumbInfoList - An empty list where the DatabaseHelper.ThumbnailInfo objects will be stored containing the URLs
contactList - list of contacts to fetch the thumbnails for
Returns:
SUCCESS or a suitable error code
See Also:
DatabaseHelper.ThumbnailInfo, fetchThumbnailUrlCount()

fetchThumbnailUrlCount

public int fetchThumbnailUrlCount()
Fetches the number of Contact Avatars which have not yet been loaded.

Returns:
The number of Avatars
See Also:
DatabaseHelper.ThumbnailInfo, fetchThumbnailUrls(List, int, int)

modifyMeProfileAvatarChangedFlag

public ServiceStatus modifyMeProfileAvatarChangedFlag(boolean avatarChanged)
Modifies the Me Profile Avatar Changed Flag. When this flag is set to true, it indicates that the avatar needs to be synchronised with the server.

Parameters:
avatarChanged - true to set the flag, false to clear the flag
Returns:
SUCCESS or a suitable error code

openContactSummaryCursor

public Cursor openContactSummaryCursor(java.lang.Long groupFilterId,
                                       java.lang.CharSequence constraint)
Fetches a cursor which can be used to iterate through the main contact list.

The ContactSummaryTable.getQueryData static method can be used on the cursor returned by this method to create a ContactSummary object.

Parameters:
groupFilterId - The local ID of a group to filter, or null if no filter is required
constraint - A search string to filter the contact name, or null if no filter is required
Returns:
The cursor result

openContactsCursor

public Cursor openContactsCursor(java.lang.Long groupFilterId,
                                 java.lang.CharSequence constraint)

fetchContact

public ServiceStatus fetchContact(long localContactId,
                                  Contact contact)
Fetches a contact from the database by its localContactId. The method #fetchBaseContact(long, Contact) should be used if the contact details properties are not required.

Parameters:
localContactId - Local ID of the contact to fetch.
contact - Empty Contact object which will be populated with data.
Returns:
SUCCESS or a suitable ServiceStatus error code.

fetchContactDetail

public ServiceStatus fetchContactDetail(long localDetailId,
                                        ContactDetail detail)
Fetches a contact detail from the database.

Parameters:
localDetailId - The local ID of the detail to fetch
detail - A empty ContactDetail object which will be filled with the data
Returns:
SUCCESS or a suitable error code

fetchContactInfo

public ServiceStatus fetchContactInfo(java.lang.String phoneNumber,
                                      Contact contact,
                                      ContactDetail phoneDetail)
Searches the database for a contact with a given phone number.

Parameters:
phoneNumber - The telephone number to find
contact - An empty Contact object which will be filled if a contact is found
phoneDetail - An empty ContactDetail object which will be filled with the matching phone number detail
Returns:
SUCCESS or a suitable error code

addContactToGroup

public ServiceStatus addContactToGroup(long localContactId,
                                       long groupId)
Puts a contact into a group.

Parameters:
localContactId - The local Id of the contact
groupId - The local group Id
Returns:
SUCCESS or a suitable error code
See Also:
deleteContactFromGroup(long, long)

deleteContactFromGroup

public ServiceStatus deleteContactFromGroup(long localContactId,
                                            long groupId)
Removes a group from a contact.

Parameters:
localContactId - The local Id of the contact
groupId - The local group Id
Returns:
SUCCESS or a suitable error code
See Also:
addContactToGroup(long, long)

deleteActivities

public ServiceStatus deleteActivities(java.lang.Integer flag)
Removes all the status or timeline activities from the database. Note: Only called from tests.

Parameters:
flag - The type of activity to delete or null to delete all
Returns:
SUCCESS or a suitable error code
See Also:
addActivities(List), addTimelineEvents(ArrayList, boolean), fetchActivitiesIds(List, Long), fetchTimelineEvents(Long, com.vodafone360.people.database.tables.ActivitiesTable.TimelineNativeTypes[])

deleteTimelineActivity

public ServiceStatus deleteTimelineActivity(MainApplication mApplication,
                                            ActivitiesTable.TimelineSummaryItem timelineItem,
                                            boolean isTimelineAll)
Removes the selected timeline activity from the database.

Parameters:
mApplication - The MainApplication
timelineItem - TimelineSummaryItem to be deleted
Returns:
SUCCESS or a suitable error code

addActivities

public ServiceStatus addActivities(java.util.List<ActivityItem> activityList)
Add a list of new activities to the Activities table.

Parameters:
activityList - contains the list of activity item
Returns:
SUCCESS or a suitable error code
See Also:
deleteActivities(Integer), addTimelineEvents(ArrayList, boolean)

fetchActivitiesIds

public ServiceStatus fetchActivitiesIds(java.util.List<java.lang.Long> activityIdList,
                                        java.lang.Long timeStamp)
Fetches a list of activity IDs from a given time.

Parameters:
activityIdList - an empty list to be populated
timeStamp - The oldest time that should be included in the list
Returns:
SUCCESS or a suitable error code
See Also:
fetchTimelineEvents(Long, com.vodafone360.people.database.tables.ActivitiesTable.TimelineNativeTypes[])

fetchTimelineEvents

public Cursor fetchTimelineEvents(java.lang.Long timeStamp,
                                  ActivitiesTable.TimelineNativeTypes[] types)
Fetches timeline events from a given time.

Parameters:
timeStamp - The oldest time that should be included in the list
types - A list of required timeline types (or an empty list for all)
Returns:
SUCCESS or a suitable error code
See Also:
addTimelineEvents(ArrayList, boolean), fetchActivitiesIds(List, Long)

fireDatabaseChangedEvent

public void fireDatabaseChangedEvent(DatabaseHelper.DatabaseChangeType type,
                                     boolean isExternal)
Fetches fires a database change event to the listeners.

Parameters:
type - The type of database change (contacts, activity, etc)
isExternal - true if this change came from the server, false if the change is from the client
See Also:
addEventCallback(Handler), removeEventCallback(Handler), fireSettingChangedEvent(PersistSettings)

addEventCallback

public void addEventCallback(Handler uiHandler)
Add a database change listener. The listener will be notified each time the database is changed.

Parameters:
uiHandler - The handler which will be notified
See Also:
fireDatabaseChangedEvent(DatabaseChangeType, boolean), fireSettingChangedEvent(PersistSettings)

removeEventCallback

public void removeEventCallback(Handler uiHandler)
Removes a database change listener. This must be called before UI activities are destroyed.

Parameters:
uiHandler - The handler which will be notified
See Also:
addEventCallback(Handler)

fireSettingChangedEvent

private void fireSettingChangedEvent(PersistSettings setting)
Internal function to fire a setting changed event to listeners.

Parameters:
setting - The setting that has changed with the new data
See Also:
addEventCallback(Handler), removeEventCallback(Handler), fireDatabaseChangedEvent(DatabaseChangeType, boolean)

fireEventToUi

private void fireEventToUi(ServiceUiRequest event,
                           int arg1,
                           int arg2,
                           java.lang.Object data)
Internal function to send an event to all the listeners.

Parameters:
event - The type of event
arg1 - This value depends on the type of event
arg2 - This value depends on the type of event
data - This value depends on the type of event
See Also:
fireDatabaseChangedEvent(DatabaseChangeType, boolean), fireSettingChangedEvent(PersistSettings)

syncAddContactList

public ServiceStatus syncAddContactList(java.util.List<Contact> contactList,
                                        boolean syncToServer,
                                        boolean syncToNative)
Function used by the contact sync engine to add a list of contacts to the database.

Parameters:
contactList - The list of contacts received from the server
syncToServer - true if the contacts need to be sent to the server
syncToNative - true if the contacts need to be added to the native phonebook
Returns:
SUCCESS or a suitable error code
See Also:
addContact(Contact)

syncModifyContactList

public ServiceStatus syncModifyContactList(java.util.List<Contact> contactList,
                                           boolean syncToServer,
                                           boolean syncToNative)
Function used by the contact sync engine to modify a list of contacts in the database.

Parameters:
contactList - The list of contacts received from the server
syncToServer - true if the contacts need to be sent to the server
syncToNative - true if the contacts need to be modified in the native phonebook
Returns:
SUCCESS or a suitable error code

syncDeleteContactList

public ServiceStatus syncDeleteContactList(java.util.List<ContactsTable.ContactIdInfo> contactIdList,
                                           boolean syncToServer,
                                           boolean syncToNative)
Function used by the contact sync engine to delete a list of contacts from the database.

Parameters:
contactIdList - The list of contact IDs received from the server (at least localId should be set)
syncToServer - true if the contacts need to be deleted from the server
syncToNative - true if the contacts need to be deleted from the native phonebook
Returns:
SUCCESS or a suitable error code
See Also:
deleteContact(long)

syncAddContactDetailList

public ServiceStatus syncAddContactDetailList(java.util.List<ContactDetail> detailList,
                                              boolean syncToServer,
                                              boolean syncToNative)
Function used by the contact sync engine to add a list of contact details to the database.

Parameters:
detailList - The list of details received from the server
syncToServer - true if the details need to be sent to the server
syncToNative - true if the contacts need to be added to the native phonebook
Returns:
SUCCESS or a suitable error code
See Also:
addContactDetail(ContactDetail)

updateTimelineNames

private void updateTimelineNames(ContactDetail cd,
                                 SQLiteDatabase db)
Updates the contents of the activities table when a contact detail changes.

Parameters:
cd - The new or modified contact detail
db - Writable SQLite database for the update

updateTimelineNames

private void updateTimelineNames(ContactDetail cd,
                                 java.lang.String contactFriendlyName,
                                 java.lang.Long serverId,
                                 SQLiteDatabase db)
Updates the contents of the activities table when a contact detail changes.

Parameters:
cd - The new or modified contact detail
contactFriendlyName - Name of contact (if known)
serverId - if known
db - Writable SQLite database for the update

syncModifyContactDetailList

public ServiceStatus syncModifyContactDetailList(java.util.List<ContactDetail> contactDetailList,
                                                 boolean syncToServer,
                                                 boolean syncToNative)
Function used by the contact sync engine to modify a list of contact details in the database.

Parameters:
contactDetailList - The list of details received from the server
serverIdList - A list of server IDs if known, or null
syncToServer - true if the details need to be sent to the server
syncToNative - true if the contacts need to be added to the native phonebook
Returns:
SUCCESS or a suitable error code
See Also:
modifyContactDetail(ContactDetail)

syncDeleteContactDetailList

public ServiceStatus syncDeleteContactDetailList(java.util.List<ContactDetail> contactDetailList,
                                                 boolean syncToServer,
                                                 boolean syncToNative)
Function used by the contact sync engine to delete a list of contact details from the database.

Parameters:
contactDetailList - The list of details which has been deleted on the server
serverIdList - A list of server IDs if known, or null
syncToServer - true if the details need to be sent to the server
syncToNative - true if the contacts need to be added to the native phonebook
Returns:
SUCCESS or a suitable error code
See Also:
deleteContactDetail(long)

fetchBaseContact

private ServiceStatus fetchBaseContact(long localContactId,
                                       Contact baseContact,
                                       SQLiteDatabase mDb)
Fetches the outer contact object information (no details, groups or sources are included).

Parameters:
localContactId - The local ID of the contact to fetch
baseContact - An empty Contact object which will be filled with the data
Returns:
SUCCESS or a suitable error code
See Also:
fetchContact(long, Contact)

fetchServerId

public java.lang.Long fetchServerId(long localContactId)
Fetches the server Id of a contact.

Parameters:
localContactId - The local ID of the contact
Returns:
The server Id of the contact, or null the contact has not yet been synchronised
See Also:
fetchContactByServerId(Long, Contact), modifyContactServerId(long, Long, Long)

removeUserData

public void removeUserData()
Remove all user data (Thumbnails, Database, Flags) from the device and notifies the engine manager.


deleteThumbnail

private boolean deleteThumbnail(java.lang.Long localContactID)
Deletes a given Thumbnail

Parameters:
localContactID - The local Id of the contact with the Thumbnail

fetchContactByServerId

public ServiceStatus fetchContactByServerId(java.lang.Long contactServerId,
                                            Contact contact)
Fetches a contact, given a server Id.

Parameters:
contactServerId - The server ID of the contact to fetch
contact - An empty Contact object which will be filled with the data
Returns:
SUCCESS or a suitable error code
See Also:
modifyContactServerId(long, Long, Long), fetchServerId(long)

doDetailsMatch

public static boolean doDetailsMatch(ContactDetail d1,
                                     ContactDetail d2)
Utility function which compares two contact details to determine if they refer to the same detail (the values may be different). TODO: Move to utility class

Parameters:
d1 - The first contact detail to compare
d2 - The second contact detail to compare
Returns:
true if they are the same
See Also:
hasDetailChanged(ContactDetail, ContactDetail)

hasDetailChanged

public static boolean hasDetailChanged(ContactDetail currentDetail,
                                       ContactDetail newDetail)
Utility function which compares two contact details to determine if they have the same value. TODO: Move to utility class

Parameters:
currentDetail - The first contact detail to compare
newDetail - The second contact detail to compare
Returns:
true if they have the same value
See Also:
doDetailsMatch(ContactDetail, ContactDetail)

addTimelineEvents

public ServiceStatus addTimelineEvents(java.util.ArrayList<ActivitiesTable.TimelineSummaryItem> syncItemList,
                                       boolean isCallLog)
Add timeline events to the database.

Parameters:
syncItemList - The list of items to be added
isCallLog - true if the list has come from the call-log, false otherwise
Returns:
SUCCESS or a suitable error code
See Also:
addTimelineEvents(ArrayList, boolean), deleteActivities(Integer), fetchActivitiesIds(List, Long), fetchTimelineEvents(Long, com.vodafone360.people.database.tables.ActivitiesTable.TimelineNativeTypes[])

createWhereClauseFromList

public static java.lang.String createWhereClauseFromList(java.lang.String field,
                                                         java.lang.Object[] itemList,
                                                         java.lang.String clause)
Utility function to create a where clause string from a list of conditions. TODO: Move to utility class

Parameters:
field - The name of the table field to be compared
itemList - The list of items to be compared against the field
clause - This can be "AND", "OR" or any other SQL clause
Returns:
The WHERE clause string (without the WHERE)

isMeProfileAvatarChanged

public boolean isMeProfileAvatarChanged()
Determines if the me profile avatar needs to be uploaded onto the server.

Returns:
true if the avatar has changed and needs to be uploaded
See Also:
modifyMeProfileAvatarChangedFlag(boolean)

trace

public static void trace(boolean write,
                         java.lang.String input)
Logs Database activity when the Settings.ENABLED_DATABASE_TRACE flag is set to true.

Parameters:
write - true if this is debug trace, false otherwise
input - String to Log at Info level

copyDatabaseToSd

public java.lang.String copyDatabaseToSd(java.lang.String info)
Copies a snapshot of the database to the SD Card - Used for testing only.

Returns:
A string which contains a description of the result

deleteDirectory

private static boolean deleteDirectory(java.io.File path)
Deletes a directory and all its contents including sub-directories.

Parameters:
path - file location
Returns:
true if directory deleted otherwise false

findNativeContact

public boolean findNativeContact(Contact c)
find the native contact in the database.

Parameters:
c - contact
Returns:
contact details of the particular contact

markMeProfileAvatarChanged

public void markMeProfileAvatarChanged()
Stores a flag in the database indicating that the me profile avatar has changed. The avatar will be uploaded to the server shortly.


updateNameAndStatusInSummary

public ServiceStatus updateNameAndStatusInSummary(SQLiteDatabase writableDatabase,
                                                  long localContactId)
Updates the ContactSummary table with the new/changed Contact


fetchContactList

public java.util.List<Contact> fetchContactList()

addNativeContact

public boolean addNativeContact(ContactChange[] contact)
Adds a native contact to the people database and makes sure that the related tables are updated (Contact, ContactDetail, ContactSummary and Activities).

Parameters:
contact - the contact to add
Returns:
true if successful, false otherwise

updateTimelineAndContactSummaryWithLegacyCode

private boolean updateTimelineAndContactSummaryWithLegacyCode(Contact contact,
                                                              SQLiteDatabase writableDb)
Updates the Timeline and ContactSummary tables with a new contact. Note: this method assumes that it being called within a transaction

Parameters:
contact - the contact to take info from
writableDb - the db to use to write the updates
Returns:
true if successful, false otherwise

setInternalContactId

private void setInternalContactId(ContactChange[] contact,
                                  long internalContactId)
Sets the internalContactId for all the ContactChange provided.

Parameters:
contact - the array of ContactChange to update
internalContactId - the id to set

convertNativeContactChanges

private Contact convertNativeContactChanges(ContactChange[] contactChanges)
Converts an array of ContactChange into a Contact object.

Parameters:
contactChanges - the array of ContactChange to convert
Returns:
the equivalent Contact
See Also:
ContactChange, Contact

convertContactChange

public ContactDetail convertContactChange(ContactChange change)
Converts a ContactChange object into an equivalent ContactDetail object.

Parameters:
change - the ContactChange to convert
Returns:
the equivalent ContactDetail
See Also:
ContactChange, ContactDetail

getNativeSyncableContactsLocalIds

public long[] getNativeSyncableContactsLocalIds()
Returns:


JavaDoc