Search in sources :

Example 6 with Contact

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;
}
Also used : ContactDetail(com.vodafone360.people.datatypes.ContactDetail) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ServiceStatus(com.vodafone360.people.service.ServiceStatus) ArrayList(java.util.ArrayList) DetailKeys(com.vodafone360.people.datatypes.ContactDetail.DetailKeys) Contact(com.vodafone360.people.datatypes.Contact)

Example 7 with Contact

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;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ServiceStatus(com.vodafone360.people.service.ServiceStatus) ArrayList(java.util.ArrayList) Contact(com.vodafone360.people.datatypes.Contact)

Example 8 with Contact

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;
}
Also used : ContactSummary(com.vodafone360.people.datatypes.ContactSummary)

Example 9 with Contact

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;
}
Also used : ContentValues(android.content.ContentValues) ActivityContact(com.vodafone360.people.datatypes.ActivityContact)

Example 10 with Contact

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;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ServiceStatus(com.vodafone360.people.service.ServiceStatus) Contact(com.vodafone360.people.datatypes.Contact)

Aggregations

Contact (com.vodafone360.people.datatypes.Contact)109 ServiceStatus (com.vodafone360.people.service.ServiceStatus)107 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)100 ArrayList (java.util.ArrayList)62 MediumTest (android.test.suitebuilder.annotation.MediumTest)50 Suppress (android.test.suitebuilder.annotation.Suppress)39 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)34 Cursor (android.database.Cursor)31 ContactChange (com.vodafone360.people.engine.contactsync.ContactChange)22 SmallTest (android.test.suitebuilder.annotation.SmallTest)19 ContactSummary (com.vodafone360.people.datatypes.ContactSummary)18 SQLException (android.database.SQLException)15 ContentValues (android.content.ContentValues)14 VCardHelper (com.vodafone360.people.datatypes.VCardHelper)13 Uri (android.net.Uri)11 ContactChangeInfo (com.vodafone360.people.database.tables.ContactChangeLogTable.ContactChangeInfo)11 ContactIdInfo (com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo)11 ContactChanges (com.vodafone360.people.datatypes.ContactChanges)11 SQLiteException (android.database.sqlite.SQLiteException)9 ContactsTable (com.vodafone360.people.database.tables.ContactsTable)9