com.vodafone360.people.database.tables
Class ActivitiesTable

java.lang.Object
  extended by com.vodafone360.people.database.tables.ActivitiesTable

public abstract class ActivitiesTable
extends java.lang.Object

Contains all the functionality related to the activities database table. This class is never instantiated hence all methods must be static.

Version:
%I%, %G%

Nested Class Summary
static class ActivitiesTable.Field
          An enumeration of all the field names in the database.
static class ActivitiesTable.TimelineNativeTypes
          An enumeration of supported timeline types.
static class ActivitiesTable.TimelineSummaryItem
          This class encapsulates a timeline activity item.
 
Field Summary
private static int CLEANUP_MAX_AGE_DAYS
          Database cleanup will delete any activity older than X days.
private static int CLEANUP_MAX_QUANTITY
          Database cleanup will delete older activities after the first X.
private static int NUMBER_OF_MS_IN_A_DAY
          Number of milliseconds in a day.
private static int NUMBER_OF_MS_IN_A_SECOND
          Number of milliseconds in a second.
private static java.lang.String TABLE_INDEX_NAME
           
private static java.lang.String TABLE_NAME
          The name of the table as it appears in the database.
 
Constructor Summary
private ActivitiesTable()
          Private constructor to prevent instantiation.
 
Method Summary
static ServiceStatus addActivities(java.util.List<ActivityItem> actList, SQLiteDatabase writableDb)
          Adds a list of activities to table.
static long addChatTimelineEvent(ActivitiesTable.TimelineSummaryItem item, boolean read, SQLiteDatabase writableDb)
          The method returns the ROW_ID i.e. the INTEGER PRIMARY KEY AUTOINCREMENT field value for the inserted row, i.e.
static ServiceStatus addTimelineEvents(java.util.ArrayList<ActivitiesTable.TimelineSummaryItem> itemList, boolean isCallLog, SQLiteDatabase writableDb)
          Adds a list of timeline events to the database.
static void cleanupActivityTable(SQLiteDatabase writableDb)
          Cleanup the Activity Table by deleting anything older than CLEANUP_MAX_AGE_DAYS, or preventing the total size from exceeding CLEANUP_MAX_QUANTITY.
static void create(SQLiteDatabase writeableDb)
          Create Activities Table.
static ServiceStatus deleteActivities(java.lang.Integer flag, SQLiteDatabase writableDb)
          Deletes all activities from the table.
static ServiceStatus deleteTimelineActivities(Context context, ActivitiesTable.TimelineSummaryItem latestTimelineItem, SQLiteDatabase writableDb, SQLiteDatabase readableDb)
          Deletes all the timeline activities for a particular contact from the table.
static ServiceStatus deleteTimelineActivity(Context context, ActivitiesTable.TimelineSummaryItem timelineItem, SQLiteDatabase writableDb, SQLiteDatabase readableDb)
          Deletes specified timeline activity from the table.
static ServiceStatus fetchActivitiesIds(java.util.List<java.lang.Long> actIdList, java.lang.Long timeStamp, SQLiteDatabase readableDb)
          Returns a list of activity IDs already synced, in reverse chronological order Fetches from the given timestamp.
static long fetchLatestStatusTimestampForContact(long localContactId, SQLiteDatabase readableDb)
          Returns the timestamp for the newest status event for a given server contact.
private static java.lang.String fetchNameWhereClause(java.lang.Long localContactId, java.lang.String name)
          Returns a string which can be added to the where clause in an SQL query on the activities table, to filter the result for a specific contact or name.
static ServiceStatus fetchNativeIdsFromLocalContactId(java.util.List<java.lang.Integer> nativeItemIdList, long localContactId, SQLiteDatabase readableDb)
           
static Cursor fetchStatusEventList(long timeStamp, SQLiteDatabase readableDb)
          Fetches a list of status items from the given time stamp.
static ActivitiesTable.TimelineSummaryItem fetchTimeLineDataFromNativeThreadId(java.lang.String threadId, SQLiteDatabase readableDb)
          Returns the TimelineSummaryItem for the corresponding native thread Id.
static Cursor fetchTimelineEventList(java.lang.Long minTimeStamp, ActivitiesTable.TimelineNativeTypes[] nativeTypes, SQLiteDatabase readableDb)
          Fetches timeline events grouped by local contact ID, name or contact address.
static Cursor fetchTimelineEventsForContact(java.lang.Long timeStamp, java.lang.Long localContactId, java.lang.String name, ActivitiesTable.TimelineNativeTypes[] nativeTypes, java.lang.String networkName, SQLiteDatabase readableDb)
          Fetches timeline events for a specific contact identified by local contact ID, name or address.
private static ContentValues fillUpdateData(ActivityItem item, java.lang.Integer contactIdx)
          Provides a ContentValues object that can be used to update the table.
private static boolean findNativeActivity(int nativeId, int type, SQLiteDatabase readableDb)
          Checks if an activity exists in the database.
private static java.lang.String getFullQueryList()
          Fetches a comma separated list of table fields which can be used in an SQL SELECT statement as the query projection.
static ActivitiesTable.TimelineSummaryItem getNewestOutgoingChatMessage(SQLiteDatabase readableDb)
          Returns the newest outgoing chat message.
static ActivitiesTable.TimelineSummaryItem getNewestUnreadChatMessage(SQLiteDatabase readableDb)
          Returns the newest unread chat message.
static int getNumberOfUnreadChatMessages(SQLiteDatabase readableDb)
          Returns the number of unread chat messages.
static int getNumberOfUnreadChatMessagesForContactAndNetwork(long localContactId, java.lang.String network, SQLiteDatabase readableDb)
          Returns the number of unread chat messages for this contact besides this network.
static int getNumberOfUnreadChatUsers(SQLiteDatabase readableDb)
          Returns the number of users have currently have unread chat messages.
static void getQueryData(Cursor cursor, ActivityContact activityContact)
          Fetches activities information from a cursor at the current position.
static void getQueryData(Cursor cursor, ActivityItem activityItem, ActivityContact activityContact)
          Fetches activities information from a cursor at the current position.
static ActivitiesTable.TimelineSummaryItem getTimelineData(Cursor cursor)
          Returns the timeline summary data from the current location of the given cursor.
static int markChatTimelineEventsForContactAsRead(java.lang.Long localContactId, java.lang.String networkName, SQLiteDatabase writableDb)
          Mark the chat timeline events for a given contact as read.
static void removeChatTimelineExceptForContact(java.lang.Long localContactId, SQLiteDatabase writeableDb)
          Removes items from the chat timeline that are not for the given contact.
private static int removeContactGroup(java.lang.Long localContactId, java.lang.String name, java.lang.Long newUpdateTime, int flag, ActivitiesTable.TimelineNativeTypes[] activityTypes, SQLiteDatabase writableDb)
          Clears the grouping of an activity in the database, if the given activity is newer.
static void removeTimelineContactData(java.lang.Long localContactId, SQLiteDatabase writeableDb)
          Updates the timeline entries in the activities table to remove deleted contact info.
static void updateMessageAsUnsent(long localContactId, java.lang.String networkId, SQLiteDatabase writableDb)
          This method updates the chat message to a contact with indicated localContactId on the particular networkId as unsent.
static void updateTimelineContactNameAndId(java.lang.String newName, java.lang.Long localContactId, SQLiteDatabase witeableDb)
          Updates the timeline when a contact name is modified in the database.
static void updateTimelineContactNameAndId(java.lang.String oldName, java.lang.String newName, java.lang.Long newLocalContactId, java.lang.Long newContactId, SQLiteDatabase witeableDb)
          Updates timeline when a new contact is added to the People database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TABLE_NAME

private static final java.lang.String TABLE_NAME
The name of the table as it appears in the database.

See Also:
Constant Field Values

TABLE_INDEX_NAME

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

CLEANUP_MAX_AGE_DAYS

private static final int CLEANUP_MAX_AGE_DAYS
Database cleanup will delete any activity older than X days.

See Also:
Constant Field Values

CLEANUP_MAX_QUANTITY

private static final int CLEANUP_MAX_QUANTITY
Database cleanup will delete older activities after the first X.

See Also:
Constant Field Values

NUMBER_OF_MS_IN_A_DAY

private static final int NUMBER_OF_MS_IN_A_DAY
Number of milliseconds in a day.

See Also:
Constant Field Values

NUMBER_OF_MS_IN_A_SECOND

private static final int NUMBER_OF_MS_IN_A_SECOND
Number of milliseconds in a second.

See Also:
Constant Field Values
Constructor Detail

ActivitiesTable

private ActivitiesTable()
Private constructor to prevent instantiation.

Method Detail

create

public static void create(SQLiteDatabase writeableDb)
Create Activities Table.

Parameters:
writeableDb - A writable SQLite database.

getFullQueryList

private static java.lang.String getFullQueryList()
Fetches a comma separated list of table fields which can be used in an SQL SELECT statement as the query projection. One of the getQueryData(Cursor, com.vodafone360.people.datatypes.ActivityItem, com.vodafone360.people.datatypes.ActivityContact) methods can used to fetch data from the cursor.

Returns:
SQL string

getQueryData

public static void getQueryData(Cursor cursor,
                                ActivityItem activityItem,
                                ActivityContact activityContact)
Fetches activities information from a cursor at the current position. The getFullQueryList() method should be used to make the query.

Parameters:
cursor - The cursor returned by the query
activityItem - An empty activity object that will be filled with the result
activityContact - An empty activity contact object that will be filled

getQueryData

public static void getQueryData(Cursor cursor,
                                ActivityContact activityContact)
Fetches activities information from a cursor at the current position. The getFullQueryList() method should be used to make the query.

Parameters:
cursor - The cursor returned by the query.
activityContact - An empty activity contact object that will be filled

fillUpdateData

private static ContentValues fillUpdateData(ActivityItem item,
                                            java.lang.Integer contactIdx)
Provides a ContentValues object that can be used to update the table.

Parameters:
item - The source activity item
contactIdx - The index of the contact to use for the update, or null to exclude contact specific information.
Returns:
ContentValues for use in an SQL update or insert.

fetchStatusEventList

public static Cursor fetchStatusEventList(long timeStamp,
                                          SQLiteDatabase readableDb)
Fetches a list of status items from the given time stamp.

Parameters:
timeStamp - Time stamp in milliseconds
readableDb - Readable SQLite database
Returns:
A cursor (use one of the getQueryData(Cursor, com.vodafone360.people.datatypes.ActivityItem, com.vodafone360.people.datatypes.ActivityContact) methods to read the data)

fetchActivitiesIds

public static ServiceStatus fetchActivitiesIds(java.util.List<java.lang.Long> actIdList,
                                               java.lang.Long timeStamp,
                                               SQLiteDatabase readableDb)
Returns a list of activity IDs already synced, in reverse chronological order Fetches from the given timestamp.

Parameters:
actIdList - An empty list which will be filled with the result
timeStamp - The time stamp to start the fetch
readableDb - Readable SQLite database
Returns:
SUCCESS or a suitable error code

addActivities

public static ServiceStatus addActivities(java.util.List<ActivityItem> actList,
                                          SQLiteDatabase writableDb)
Adds a list of activities to table. The activities added will be grouped in the database, based on local contact Id, name or contact address (see removeContactGroup(Long, String, Long, int, TimelineNativeTypes[], SQLiteDatabase) for more information on how the grouping works.

Parameters:
actList - The list of activities
writableDb - Writable SQLite database
Returns:
SUCCESS or a suitable error code

deleteActivities

public static ServiceStatus deleteActivities(java.lang.Integer flag,
                                             SQLiteDatabase writableDb)
Deletes all activities from the table.

Parameters:
flag - Can be a bitmap of:
writableDb - Writable SQLite database
Returns:
SUCCESS or a suitable error code

fetchNativeIdsFromLocalContactId

public static ServiceStatus fetchNativeIdsFromLocalContactId(java.util.List<java.lang.Integer> nativeItemIdList,
                                                             long localContactId,
                                                             SQLiteDatabase readableDb)

deleteTimelineActivity

public static ServiceStatus deleteTimelineActivity(Context context,
                                                   ActivitiesTable.TimelineSummaryItem timelineItem,
                                                   SQLiteDatabase writableDb,
                                                   SQLiteDatabase readableDb)
Deletes specified timeline activity from the table.

Parameters:
Context -
timelineItem - TimelineSummaryItem to be deleted
writableDb - Writable SQLite database
readableDb - Readable SQLite database
Returns:
SUCCESS or a suitable error code

deleteTimelineActivities

public static ServiceStatus deleteTimelineActivities(Context context,
                                                     ActivitiesTable.TimelineSummaryItem latestTimelineItem,
                                                     SQLiteDatabase writableDb,
                                                     SQLiteDatabase readableDb)
Deletes all the timeline activities for a particular contact from the table.

Parameters:
Context -
timelineItem - TimelineSummaryItem to be deleted
writableDb - Writable SQLite database
readableDb - Readable SQLite database
Returns:
SUCCESS or a suitable error code

fetchTimelineEventList

public static Cursor fetchTimelineEventList(java.lang.Long minTimeStamp,
                                            ActivitiesTable.TimelineNativeTypes[] nativeTypes,
                                            SQLiteDatabase readableDb)
Fetches timeline events grouped by local contact ID, name or contact address. Events returned will be in reverse-chronological order. If a native type list is provided the result will be filtered by the list.

Parameters:
minTimeStamp - Only timeline events from this date will be returned
nativeTypes - A list of native types to filter the result, or null to return all.
readableDb - Readable SQLite database
Returns:
A cursor containing the result. The getTimelineData(Cursor) method should be used for reading the cursor.

addTimelineEvents

public static ServiceStatus addTimelineEvents(java.util.ArrayList<ActivitiesTable.TimelineSummaryItem> itemList,
                                              boolean isCallLog,
                                              SQLiteDatabase writableDb)
Adds a list of timeline events to the database. Each event is grouped by contact and grouped by contact + native type.

Parameters:
itemList - List of timeline events
isCallLog - true to group all activities with call logs, false to group with messaging
writableDb - Writable SQLite database
Returns:
SUCCESS or a suitable error

addChatTimelineEvent

public static long addChatTimelineEvent(ActivitiesTable.TimelineSummaryItem item,
                                        boolean read,
                                        SQLiteDatabase writableDb)
The method returns the ROW_ID i.e. the INTEGER PRIMARY KEY AUTOINCREMENT field value for the inserted row, i.e. LOCAL_ID.

Parameters:
item - TimelineSummaryItem.
read - - TRUE if the chat message is outgoing or gets into the timeline history view for a contact with LocalContactId.
writableDb - Writable SQLite database.
Returns:
LocalContactID or -1 if the row was not inserted.

removeContactGroup

private static int removeContactGroup(java.lang.Long localContactId,
                                      java.lang.String name,
                                      java.lang.Long newUpdateTime,
                                      int flag,
                                      ActivitiesTable.TimelineNativeTypes[] activityTypes,
                                      SQLiteDatabase writableDb)
Clears the grouping of an activity in the database, if the given activity is newer. This is so that only the latest activity is returned for a particular group. Each activity is associated with two groups:
  1. All group - Grouped by contact only
  2. Native group - Grouped by contact and native type (call log or messaging)
The group to be removed is determined by the activityTypes parameter. Grouping must also work for timeline events that are not associated with a contact. The following fields are used to do identify a contact for the grouping (in order of priority):
  1. localContactId - If it not null
  2. name - If this is a valid telephone number, the match will be done to ensure that the same phone number written in different ways will be included in the group. See fetchNameWhereClause(Long, String) for more information.

Parameters:
localContactId - Local contact Id or NULL if the activity is not associated with a contact.
name - Name of contact or contact address (telephone number, email, etc).
newUpdateTime - The time that the given activity has occurred
flag - Bitmap of types including:
activityTypes - A list of native types to include in the grouping. Currently, only two groups are supported (see above). If this parameter is null the contact will be added to the "all group", otherwise the contact is added to the native group.
writableDb - Writable SQLite database
Returns:
The latest contact status value which should be added to the current activities grouping.

findNativeActivity

private static boolean findNativeActivity(int nativeId,
                                          int type,
                                          SQLiteDatabase readableDb)
Checks if an activity exists in the database.

Parameters:
nativeId - The native ID which links the activity with the record in the native table.
type - The native type (An ordinal from the #TimelineNativeTypes enumeration)
readableDb - Readable SQLite database
Returns:
true if the activity was found, false otherwise

fetchNameWhereClause

private static java.lang.String fetchNameWhereClause(java.lang.Long localContactId,
                                                     java.lang.String name)
Returns a string which can be added to the where clause in an SQL query on the activities table, to filter the result for a specific contact or name. The clause will prioritise in the following way:
  1. Use localContactId - If it not null
  2. Use name - If this is a valid telephone number, the match will be done to ensure that the same phone number written in different ways will be included in the group.

Parameters:
localContactId - The local contact ID, or null if the contact does not exist in the People database.
name - A string containing the name, or a telephone number/email identifying the contact.
Returns:
The where clause string

getTimelineData

public static ActivitiesTable.TimelineSummaryItem getTimelineData(Cursor cursor)
Returns the timeline summary data from the current location of the given cursor. The cursor can be obtained using fetchTimelineEventList(Long, TimelineNativeTypes[], SQLiteDatabase) or #fetchTimelineEventsForContact(Long, Long, String, TimelineNativeTypes[], SQLiteDatabase).

Parameters:
cursor - Cursor in the required position.
Returns:
A filled out TimelineSummaryItem object

fetchTimelineEventsForContact

public static Cursor fetchTimelineEventsForContact(java.lang.Long timeStamp,
                                                   java.lang.Long localContactId,
                                                   java.lang.String name,
                                                   ActivitiesTable.TimelineNativeTypes[] nativeTypes,
                                                   java.lang.String networkName,
                                                   SQLiteDatabase readableDb)
Fetches timeline events for a specific contact identified by local contact ID, name or address. Events returned will be in reverse-chronological order. If a native type list is provided the result will be filtered by the list.

Parameters:
timeStamp - Only events from this time will be returned
localContactId - The local contact ID if the contact is in the People database, or null.
name - The name or address of the contact (required if local contact ID is NULL).
nativeTypes - A list of required native types to filter the result, or null to return all timeline events for the contact.
readableDb - Readable SQLite database
networkName - The name of the network the contacts belongs to (required in order to provide appropriate chat messages filtering) If the parameter is null messages from all networks will be returned. The values are the SocialNetwork.VODAFONE.toString(), SocialNetwork.GOOGLE.toString(), or SocialNetwork.MICROSOFT.toString() results.
Returns:
The cursor that can be read using getTimelineData(Cursor).

markChatTimelineEventsForContactAsRead

public static int markChatTimelineEventsForContactAsRead(java.lang.Long localContactId,
                                                         java.lang.String networkName,
                                                         SQLiteDatabase writableDb)
Mark the chat timeline events for a given contact as read.

Parameters:
localContactId - Local contact ID.
networkName - Name of the SNS.
writableDb - Writable SQLite reference.
Returns:
Number of rows affected by database update.

fetchLatestStatusTimestampForContact

public static long fetchLatestStatusTimestampForContact(long localContactId,
                                                        SQLiteDatabase readableDb)
Returns the timestamp for the newest status event for a given server contact.

Parameters:
local - contact id Server contact ID
readableDb - Readable SQLite database
Returns:
The timestamp in milliseconds, or 0 if not found.

updateTimelineContactNameAndId

public static void updateTimelineContactNameAndId(java.lang.String oldName,
                                                  java.lang.String newName,
                                                  java.lang.Long newLocalContactId,
                                                  java.lang.Long newContactId,
                                                  SQLiteDatabase witeableDb)
Updates timeline when a new contact is added to the People database. Updates all timeline events that are not associated with a contact and have a phone number that matches the oldName parameter.

Parameters:
oldName - The telephone number (since is the name of an activity that is not associated with a contact)
newName - The new name
newLocalContactId - The local Contact Id for the added contact.
newContactId - The server Contact Id for the added contact (or null if the contact has not yet been synced).
witeableDb - Writable SQLite database

updateTimelineContactNameAndId

public static void updateTimelineContactNameAndId(java.lang.String newName,
                                                  java.lang.Long localContactId,
                                                  SQLiteDatabase witeableDb)
Updates the timeline when a contact name is modified in the database.

Parameters:
newName - The new name.
localContactId - Local contact Id which was modified.
witeableDb - Writable SQLite database

removeTimelineContactData

public static void removeTimelineContactData(java.lang.Long localContactId,
                                             SQLiteDatabase writeableDb)
Updates the timeline entries in the activities table to remove deleted contact info.

Parameters:
localContactId - - the contact id that has been deleted
writeableDb - - reference to the database

removeChatTimelineExceptForContact

public static void removeChatTimelineExceptForContact(java.lang.Long localContactId,
                                                      SQLiteDatabase writeableDb)
Removes items from the chat timeline that are not for the given contact.

Parameters:
localContactId - Given contact ID.
writeableDb - Writable SQLite database.

getNumberOfUnreadChatUsers

public static int getNumberOfUnreadChatUsers(SQLiteDatabase readableDb)
Returns the number of users have currently have unread chat messages.

Parameters:
readableDb - Reference to a readable database.
Returns:
Number of users with unread chat messages.

getNumberOfUnreadChatMessages

public static int getNumberOfUnreadChatMessages(SQLiteDatabase readableDb)
Returns the number of unread chat messages.

Parameters:
readableDb - Reference to a readable database.
Returns:
Number of unread chat messages.

getNumberOfUnreadChatMessagesForContactAndNetwork

public static int getNumberOfUnreadChatMessagesForContactAndNetwork(long localContactId,
                                                                    java.lang.String network,
                                                                    SQLiteDatabase readableDb)
Returns the number of unread chat messages for this contact besides this network.

Parameters:
localContactId - Given contact ID.
network - SNS name.
readableDb - Reference to a readable database.
Returns:
Number of unread chat messages.

getNewestUnreadChatMessage

public static ActivitiesTable.TimelineSummaryItem getNewestUnreadChatMessage(SQLiteDatabase readableDb)
Returns the newest unread chat message.

Parameters:
readableDb - Reference to a readable database.
Returns:
TimelineSummaryItem of the newest unread chat message, or NULL if none are found.

getNewestOutgoingChatMessage

public static ActivitiesTable.TimelineSummaryItem getNewestOutgoingChatMessage(SQLiteDatabase readableDb)
Returns the newest outgoing chat message.

Parameters:
readableDb - Reference to a readable database.
Returns:
TimelineSummaryItem of the newest unread chat message, or NULL if none are found.

updateMessageAsUnsent

public static void updateMessageAsUnsent(long localContactId,
                                         java.lang.String networkId,
                                         SQLiteDatabase writableDb)
This method updates the chat message to a contact with indicated localContactId on the particular networkId as unsent.

Parameters:
localContactId - long - the localContactId of the recipient
networkId - String - the network id, @see SocialNetwork
writableDb - SQLiteDatabase - database

cleanupActivityTable

public static void cleanupActivityTable(SQLiteDatabase writableDb)
Cleanup the Activity Table by deleting anything older than CLEANUP_MAX_AGE_DAYS, or preventing the total size from exceeding CLEANUP_MAX_QUANTITY.

Parameters:
writableDb - Reference to a writable SQLite Database.

fetchTimeLineDataFromNativeThreadId

public static ActivitiesTable.TimelineSummaryItem fetchTimeLineDataFromNativeThreadId(java.lang.String threadId,
                                                                                      SQLiteDatabase readableDb)
Returns the TimelineSummaryItem for the corresponding native thread Id.

Parameters:
threadId - native thread id
readableDb - Readable SQLite database
Returns:
The TimelineSummaryItem of the matching native thread id, or NULL if none are found.


JavaDoc