Search in sources :

Example 1 with ContactDetail

use of com.vodafone360.people.datatypes.ContactDetail in project 360-Engine-for-Android by 360.

the class DatabaseHelper method convertNativeContactChanges.

/**
 * Converts an array of ContactChange into a Contact object.
 *
 * @see ContactChange
 * @see Contact
 * @param contactChanges the array of ContactChange to convert
 * @return the equivalent Contact
 */
private Contact convertNativeContactChanges(ContactChange[] contactChanges) {
    if (contactChanges == null || contactChanges.length <= 0)
        return null;
    final Contact contact = new Contact();
    contact.localContactID = contactChanges[0].getInternalContactId();
    // coming from native
    contact.nativeContactId = new Integer((int) contactChanges[0].getNabContactId());
    contact.synctophone = true;
    // fill the contact with all the details
    for (int i = 0; i < contactChanges.length; i++) {
        final ContactDetail detail = convertContactChange(contactChanges[i]);
        // setting it to -1 means that it does not need to be synced back to
        // native
        detail.syncNativeContactId = -1;
        contact.details.add(detail);
    }
    return contact;
}
Also used : ContactDetail(com.vodafone360.people.datatypes.ContactDetail) Contact(com.vodafone360.people.datatypes.Contact)

Example 2 with ContactDetail

use of com.vodafone360.people.datatypes.ContactDetail 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 3 with ContactDetail

use of com.vodafone360.people.datatypes.ContactDetail in project 360-Engine-for-Android by 360.

the class DatabaseHelper method addContact.

/**
 * Adds a contact to the database and fires an internal database change
 * event.
 *
 * @param contact A {@link Contact} object which contains the details to be
 *            added
 * @return SUCCESS or a suitable error code
 * @see #deleteContact(long)
 * @see #addContactDetail(ContactDetail)
 * @see #modifyContactDetail(ContactDetail)
 * @see #deleteContactDetail(long)
 * @see #addContactToGroup(long, long)
 * @see #deleteContactFromGroup(long, long)
 */
public ServiceStatus addContact(Contact contact) {
    if (Settings.ENABLED_DATABASE_TRACE) {
        trace(false, "DatabaseHelper.addContact() contactID[" + contact.contactID + "] nativeContactId[" + contact.nativeContactId + "]");
    }
    List<Contact> contactList = new ArrayList<Contact>();
    contactList.add(contact);
    ServiceStatus status = syncAddContactList(contactList, true, true);
    if (ServiceStatus.SUCCESS == status) {
        fireDatabaseChangedEvent(DatabaseChangeType.CONTACTS, false);
    }
    return status;
}
Also used : ServiceStatus(com.vodafone360.people.service.ServiceStatus) ArrayList(java.util.ArrayList) Contact(com.vodafone360.people.datatypes.Contact)

Example 4 with ContactDetail

use of com.vodafone360.people.datatypes.ContactDetail in project 360-Engine-for-Android by 360.

the class DatabaseHelper method deleteContactDetail.

/**
 * Deletes a contact detail from the database. Also fires an internal
 * database change event.
 *
 * @param localContactDetailID The local ID of the detail to delete
 * @return SUCCESS or a suitable error code
 * @see #addContactDetail(ContactDetail)
 * @see #modifyContactDetail(ContactDetail)
 * @see #addContact(Contact)
 * @see #deleteContact(long)
 * @see #addContactToGroup(long, long)
 * @see #deleteContactFromGroup(long, long)
 */
public ServiceStatus deleteContactDetail(long localContactDetailID) {
    if (Settings.ENABLED_DATABASE_TRACE) {
        trace(false, "DatabaseHelper.deleteContactDetail() localContactDetailID[" + localContactDetailID + "]");
    }
    SQLiteDatabase db = getReadableDatabase();
    ContactDetail detail = ContactDetailsTable.fetchDetail(localContactDetailID, db);
    if (detail == null) {
        LogUtils.logE("Database.deleteContactDetail() Unable to find detail for deletion");
        return ServiceStatus.ERROR_NOT_FOUND;
    }
    boolean isMeProfile = false;
    if (detail.localContactID.equals(SyncMeDbUtils.getMeProfileLocalContactId(this))) {
        isMeProfile = true;
    }
    List<ContactDetail> detailList = new ArrayList<ContactDetail>();
    detailList.add(detail);
    ServiceStatus status = syncDeleteContactDetailList(detailList, true, !isMeProfile);
    if (ServiceStatus.SUCCESS == status) {
        fireDatabaseChangedEvent(DatabaseChangeType.CONTACTS, false);
        if (isMeProfile) {
            WidgetUtils.kickWidgetUpdateNow(mContext);
        }
    }
    return status;
}
Also used : ContactDetail(com.vodafone360.people.datatypes.ContactDetail) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ServiceStatus(com.vodafone360.people.service.ServiceStatus) ArrayList(java.util.ArrayList)

Example 5 with ContactDetail

use of com.vodafone360.people.datatypes.ContactDetail 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)

Aggregations

ContactDetail (com.vodafone360.people.datatypes.ContactDetail)109 Contact (com.vodafone360.people.datatypes.Contact)60 ServiceStatus (com.vodafone360.people.service.ServiceStatus)57 ArrayList (java.util.ArrayList)33 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)24 MediumTest (android.test.suitebuilder.annotation.MediumTest)24 Cursor (android.database.Cursor)20 Suppress (android.test.suitebuilder.annotation.Suppress)16 VCardHelper (com.vodafone360.people.datatypes.VCardHelper)15 ContentValues (android.content.ContentValues)11 SmallTest (android.test.suitebuilder.annotation.SmallTest)10 ContactSummary (com.vodafone360.people.datatypes.ContactSummary)10 SQLException (android.database.SQLException)8 Uri (android.net.Uri)7 SQLiteException (android.database.sqlite.SQLiteException)6 ContactsTable (com.vodafone360.people.database.tables.ContactsTable)6 ContactChanges (com.vodafone360.people.datatypes.ContactChanges)6 ServerIdInfo (com.vodafone360.people.database.DatabaseHelper.ServerIdInfo)4 ContactChangeInfo (com.vodafone360.people.database.tables.ContactChangeLogTable.ContactChangeInfo)4 Date (java.util.Date)4