use of com.vodafone360.people.datatypes.Contact in project 360-Engine-for-Android by 360.
the class DatabaseHelper method syncAddContactList.
/***
* Function used by the contact sync engine to add a list of contacts to the
* database.
*
* @param contactList The list of contacts received from the server
* @param syncToServer true if the contacts need to be sent to the server
* @param syncToNative true if the contacts need to be added to the native
* phonebook
* @return SUCCESS or a suitable error code
* @see #addContact(Contact)
*/
public ServiceStatus syncAddContactList(List<Contact> contactList, boolean syncToServer, boolean syncToNative) {
if (Settings.ENABLED_DATABASE_TRACE)
trace(false, "DatabaseHelper.syncAddContactList() syncToServer[" + syncToServer + "] syncToNative[" + syncToNative + "]");
if (!Settings.ENABLE_SERVER_CONTACT_SYNC) {
syncToServer = false;
}
if (!Settings.ENABLE_UPDATE_NATIVE_CONTACTS) {
syncToNative = false;
}
SQLiteDatabase writableDb = getWritableDatabase();
boolean needFireDbUpdate = false;
for (Contact contact : contactList) {
contact.deleted = null;
contact.localContactID = null;
if (syncToNative) {
contact.nativeContactId = null;
}
if (syncToServer) {
contact.contactID = null;
contact.updated = null;
contact.synctophone = true;
}
try {
writableDb.beginTransaction();
ServiceStatus status = ContactsTable.addContact(contact, writableDb);
if (ServiceStatus.SUCCESS != status) {
LogUtils.logE("DatabaseHelper.syncAddContactList() Unable to add contact to contacts table, due to a database error");
return status;
}
List<ContactDetail.DetailKeys> orderList = new ArrayList<ContactDetail.DetailKeys>();
for (int i = 0; i < contact.details.size(); i++) {
final ContactDetail detail = contact.details.get(i);
detail.localContactID = contact.localContactID;
detail.localDetailID = null;
if (syncToServer) {
detail.unique_id = null;
}
if (detail.order != null && (detail.order.equals(ContactDetail.ORDER_PREFERRED))) {
if (orderList.contains(detail.key)) {
detail.order = ContactDetail.ORDER_NORMAL;
} else {
orderList.add(detail.key);
}
}
status = ContactDetailsTable.addContactDetail(detail, syncToServer, (syncToNative && contact.synctophone), writableDb);
if (ServiceStatus.SUCCESS != status) {
LogUtils.logE("DatabaseHelper.syncAddContactList() Unable to add contact detail (for new contact), " + "due to a database error. Contact ID[" + contact.localContactID + "]");
return status;
}
}
// details are not stored
if (!contact.details.isEmpty()) {
status = ContactSummaryTable.addContact(contact, writableDb);
if (ServiceStatus.SUCCESS != status) {
return status;
}
}
if (contact.groupList != null) {
for (Long groupId : contact.groupList) {
if (groupId != -1 && !ContactGroupsTable.addContactToGroup(contact.localContactID, groupId, writableDb)) {
return ServiceStatus.ERROR_DATABASE_CORRUPT;
}
}
}
if (contact.sources != null) {
for (String source : contact.sources) {
if (!ContactSourceTable.addContactSource(contact.localContactID, source, writableDb)) {
return ServiceStatus.ERROR_DATABASE_CORRUPT;
}
}
}
if (syncToServer) {
if (contact.groupList != null) {
for (Long groupId : contact.groupList) {
if (!ContactChangeLogTable.addGroupRel(contact.localContactID, contact.contactID, groupId, writableDb)) {
return ServiceStatus.ERROR_DATABASE_CORRUPT;
}
}
}
}
/*
* FIXME: Hacking a check for me profile here using syncToNative and syncToServer
* The me contact does not use a static local contact id
* which is ridiculous. Basically we have to check the syncToNative and syncToServer
* flags together with isMeProfile
* because luckily as of yet the they are only both false when its me profile
* in case that's the contact being added.
*/
String displayName = updateContactNameInSummary(writableDb, contact.localContactID, (!syncToNative && !syncToServer) || SyncMeDbUtils.isMeProfile(this, contact.localContactID));
if (null == displayName) {
return ServiceStatus.ERROR_DATABASE_CORRUPT;
}
// updating timeline
for (ContactDetail detail : contact.details) {
// we already have name, don't need to get it again
if (detail.key != ContactDetail.DetailKeys.VCARD_NAME) {
detail.localContactID = contact.localContactID;
detail.nativeContactId = contact.nativeContactId;
if (updateTimelineNames(detail, displayName, contact.contactID, writableDb)) {
needFireDbUpdate = true;
}
}
}
writableDb.setTransactionSuccessful();
} finally {
writableDb.endTransaction();
}
}
if (needFireDbUpdate) {
fireDatabaseChangedEvent(DatabaseChangeType.ACTIVITIES, false);
}
return ServiceStatus.SUCCESS;
}
use of com.vodafone360.people.datatypes.Contact in project 360-Engine-for-Android by 360.
the class DatabaseHelper method addContactToGroup.
/***
* Puts a contact into a group.
*
* @param localContactId The local Id of the contact
* @param groupId The local group Id
* @return SUCCESS or a suitable error code
* @see #deleteContactFromGroup(long, long)
*/
public ServiceStatus addContactToGroup(long localContactId, long groupId) {
if (Settings.ENABLED_DATABASE_TRACE) {
trace(false, "DatabaseHelper.addContactToGroup() localContactId[" + localContactId + "] " + "groupId[" + groupId + "]");
}
SQLiteDatabase db = getWritableDatabase();
List<Long> groupIds = new ArrayList<Long>();
ContactGroupsTable.fetchContactGroups(localContactId, groupIds, db);
if (groupIds.contains(groupId)) {
// group is already in db than it's ok
return ServiceStatus.SUCCESS;
}
boolean syncToServer = true;
boolean isMeProfile = false;
if (SyncMeDbUtils.getMeProfileLocalContactId(this) != null && SyncMeDbUtils.getMeProfileLocalContactId(this).longValue() == localContactId) {
isMeProfile = true;
syncToServer = false;
}
Contact contact = new Contact();
ServiceStatus status = fetchContact(localContactId, contact);
if (ServiceStatus.SUCCESS != status) {
return status;
}
try {
db.beginTransaction();
if (!ContactGroupsTable.addContactToGroup(localContactId, groupId, db)) {
return ServiceStatus.ERROR_DATABASE_CORRUPT;
}
if (syncToServer) {
if (!ContactChangeLogTable.addGroupRel(localContactId, contact.contactID, groupId, db)) {
return ServiceStatus.ERROR_DATABASE_CORRUPT;
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (syncToServer && !isMeProfile) {
fireDatabaseChangedEvent(DatabaseChangeType.CONTACTS, false);
}
return ServiceStatus.SUCCESS;
}
use of com.vodafone360.people.datatypes.Contact in project 360-Engine-for-Android by 360.
the class DatabaseHelper method updateTimelineNames.
/**
* Updates the contents of the activities table when a contact detail
* changes.
*
* @param cd The new or modified contact detail
* @param contactFriendlyName Name of contact (if known)
* @param serverId if known
* @param db Writable SQLite database for the update
* @return true if the Activities table was updated, false otherwise
*/
private boolean updateTimelineNames(ContactDetail cd, String contactFriendlyName, Long serverId, SQLiteDatabase db) {
if (cd.key == ContactDetail.DetailKeys.VCARD_NAME) {
if (contactFriendlyName != null) {
ActivitiesTable.updateTimelineContactNameAndId(contactFriendlyName, cd.localContactID, db);
return true;
}
}
if (cd.key == ContactDetail.DetailKeys.VCARD_PHONE) {
if (contactFriendlyName == null) {
ContactSummary cs = new ContactSummary();
if (ContactSummaryTable.fetchSummaryItem(cd.localContactID, cs, db) == ServiceStatus.SUCCESS) {
contactFriendlyName = cs.formattedName;
}
}
if (contactFriendlyName != null) {
Long cId = serverId;
if (cId == null) {
cId = ContactsTable.fetchServerId(cd.localContactID, db);
}
ActivitiesTable.updateTimelineContactNameAndId(cd.getTel(), contactFriendlyName, cd.localContactID, cId, db);
return true;
} else {
LogUtils.logE("updateTimelineNames() failed to fetch summary Item");
}
}
return false;
}
use of com.vodafone360.people.datatypes.Contact in project 360-Engine-for-Android by 360.
the class ActivitiesTable method fillUpdateData.
/***
* Provides a ContentValues object that can be used to update the table.
*
* @param item The source activity item
* @param contactIdx The index of the contact to use for the update, or null
* to exclude contact specific information.
* @return ContentValues for use in an SQL update or insert.
* @note Items that are NULL will be not modified in the database.
*/
private static ContentValues fillUpdateData(final ActivityItem item, final Integer contactIdx) {
DatabaseHelper.trace(false, "DatabaseHelper.fillUpdateData()");
ContentValues activityItemValues = new ContentValues();
ActivityContact ac = null;
if (contactIdx != null) {
ac = item.contactList.get(contactIdx);
}
activityItemValues.put(Field.ACTIVITY_ID.toString(), item.activityId);
activityItemValues.put(Field.TIMESTAMP.toString(), item.time);
if (item.type != null) {
activityItemValues.put(Field.TYPE.toString(), item.type.getTypeCode());
}
if (item.uri != null) {
activityItemValues.put(Field.URI.toString(), item.uri);
}
/** TODO: Not sure if we need this. **/
// activityItemValues.put(Field.INCOMING.toString(), false);
activityItemValues.put(Field.TITLE.toString(), item.title);
activityItemValues.put(Field.DESCRIPTION.toString(), item.description);
if (item.previewUrl != null) {
activityItemValues.put(Field.PREVIEW_URL.toString(), item.previewUrl);
}
if (item.store != null) {
activityItemValues.put(Field.STORE.toString(), item.store);
}
if (item.activityFlags != null) {
activityItemValues.put(Field.FLAG.toString(), item.activityFlags);
}
if (item.parentActivity != null) {
activityItemValues.put(Field.PARENT_ACTIVITY.toString(), item.parentActivity);
}
if (item.hasChildren != null) {
activityItemValues.put(Field.HAS_CHILDREN.toString(), item.hasChildren);
}
if (item.visibilityFlags != null) {
activityItemValues.put(Field.VISIBILITY.toString(), item.visibilityFlags);
}
if (ac != null) {
activityItemValues.put(Field.CONTACT_ID.toString(), ac.mContactId);
activityItemValues.put(Field.USER_ID.toString(), ac.mUserId);
activityItemValues.put(Field.CONTACT_NAME.toString(), ac.mName);
activityItemValues.put(Field.LOCAL_CONTACT_ID.toString(), ac.mLocalContactId);
if (ac.mNetwork != null) {
activityItemValues.put(Field.CONTACT_NETWORK.toString(), ac.mNetwork);
}
if (ac.mAddress != null) {
activityItemValues.put(Field.CONTACT_ADDRESS.toString(), ac.mAddress);
}
if (ac.mAvatarUrl != null) {
activityItemValues.put(Field.CONTACT_AVATAR_URL.toString(), ac.mAvatarUrl);
}
}
return activityItemValues;
}
use of com.vodafone360.people.datatypes.Contact in project 360-Engine-for-Android by 360.
the class DatabaseHelper method deleteContactFromGroup.
/***
* Removes a group from a contact.
*
* @param localContactId The local Id of the contact
* @param groupId The local group Id
* @return SUCCESS or a suitable error code
* @see #addContactToGroup(long, long)
*/
public ServiceStatus deleteContactFromGroup(long localContactId, long groupId) {
if (Settings.ENABLED_DATABASE_TRACE)
trace(false, "DatabaseHelper.deleteContactFromGroup() localContactId[" + localContactId + "] groupId[" + groupId + "]");
boolean syncToServer = true;
boolean meProfile = false;
if (SyncMeDbUtils.getMeProfileLocalContactId(this) != null && SyncMeDbUtils.getMeProfileLocalContactId(this).longValue() == localContactId) {
meProfile = true;
syncToServer = false;
}
Contact contact = new Contact();
ServiceStatus status = fetchContact(localContactId, contact);
if (ServiceStatus.SUCCESS != status) {
return status;
}
if (contact.contactID == null) {
return ServiceStatus.ERROR_NOT_READY;
}
SQLiteDatabase db = getWritableDatabase();
try {
db.beginTransaction();
boolean result = ContactGroupsTable.deleteContactFromGroup(localContactId, groupId, db);
if (result && syncToServer) {
if (!ContactChangeLogTable.deleteGroupRel(localContactId, contact.contactID, groupId, db)) {
return ServiceStatus.ERROR_DATABASE_CORRUPT;
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (syncToServer && !meProfile) {
fireDatabaseChangedEvent(DatabaseChangeType.CONTACTS, false);
}
return ServiceStatus.SUCCESS;
}
Aggregations