Search in sources :

Example 51 with ContactDetail

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

the class DatabaseHelper method syncModifyContactDetailList.

/**
 * Function used by the contact sync engine to modify a list of contact
 * details in the database.
 *
 * @param contactDetailList The list of details received from the server
 * @param serverIdList A list of server IDs if known, or null
 * @param syncToServer true if the details 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 #modifyContactDetail(ContactDetail)
 */
public ServiceStatus syncModifyContactDetailList(List<ContactDetail> contactDetailList, boolean syncToServer, boolean syncToNative) {
    if (Settings.ENABLED_DATABASE_TRACE)
        trace(false, "DatabaseHelper.syncModifyContactDetailList() syncToServer[" + syncToServer + "] syncToNative[" + syncToNative + "]");
    if (!Settings.ENABLE_SERVER_CONTACT_SYNC) {
        syncToServer = false;
    }
    if (!Settings.ENABLE_UPDATE_NATIVE_CONTACTS) {
        syncToNative = false;
    }
    boolean needFireDbUpdate = false;
    SQLiteDatabase db = getWritableDatabase();
    for (ContactDetail contactDetail : contactDetailList) {
        ContactsTable.ContactIdInfo contactIdInfo = ContactsTable.validateContactId(contactDetail.localContactID, db);
        if (contactIdInfo == null) {
            return ServiceStatus.ERROR_NOT_FOUND;
        }
        contactDetail.serverContactId = contactIdInfo.serverId;
        if (contactIdInfo.syncToPhone) {
            contactDetail.syncNativeContactId = contactIdInfo.nativeId;
        } else {
            contactDetail.syncNativeContactId = -1;
        }
        try {
            db.beginTransaction();
            if (contactDetail.order != null && contactDetail.order.equals(ContactDetail.ORDER_PREFERRED)) {
                ContactDetailsTable.removePreferred(contactDetail.localContactID, contactDetail.key, db);
            }
            ServiceStatus status = ContactDetailsTable.modifyDetail(contactDetail, syncToServer, syncToNative, db);
            if (ServiceStatus.SUCCESS != status) {
                return status;
            }
            // in order not to override the new picture before it is uploaded.
            if (ContactDetail.DetailKeys.PHOTO == contactDetail.key && TextUtils.isEmpty(contactDetail.photo_url)) {
                ContactSummaryTable.modifyPictureLoadedFlag(contactDetail.localContactID, false, db);
            }
            String displayName = updateContactNameInSummary(db, contactDetail.localContactID, SyncMeDbUtils.isMeProfile(this, contactDetail.localContactID));
            if (null == displayName) {
                return ServiceStatus.ERROR_DATABASE_CORRUPT;
            }
            if (updateTimelineNames(contactDetail, displayName, null, db)) {
                needFireDbUpdate = true;
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
    if (needFireDbUpdate) {
        fireDatabaseChangedEvent(DatabaseChangeType.ACTIVITIES, false);
    }
    return ServiceStatus.SUCCESS;
}
Also used : ContactDetail(com.vodafone360.people.datatypes.ContactDetail) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ContactsTable(com.vodafone360.people.database.tables.ContactsTable) ServiceStatus(com.vodafone360.people.service.ServiceStatus)

Example 52 with ContactDetail

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

the class DatabaseHelper method modifyContactDetail.

/**
 * Modifies an existing contact detail in the database. Also fires an
 * internal database change event.
 *
 * @param detail A {@link ContactDetail} object which contains the detail to add
 *
 * @return SUCCESS or a suitable error code
 * @see #addContactDetail(ContactDetail)
 * @see #deleteContactDetail(long)
 * @see #addContact(Contact)
 * @see #deleteContact(long)
 * @see #addContactToGroup(long, long)
 * @see #deleteContactFromGroup(long, long)
 */
public ServiceStatus modifyContactDetail(ContactDetail detail) {
    if (Settings.ENABLED_DATABASE_TRACE) {
        trace(false, "DatabaseHelper.modifyContactDetail() name[" + detail.getName() + "]");
    }
    boolean isMeProfile = SyncMeDbUtils.isMeProfile(this, detail.localContactID);
    List<ContactDetail> detailList = new ArrayList<ContactDetail>();
    detailList.add(detail);
    ServiceStatus status = syncModifyContactDetailList(detailList, !isMeProfile, !isMeProfile);
    if (ServiceStatus.SUCCESS == status) {
        fireDatabaseChangedEvent(DatabaseChangeType.CONTACTS, false);
        if (isMeProfile) {
            WidgetUtils.kickWidgetUpdateNow(mContext);
        }
    }
    return status;
}
Also used : ContactDetail(com.vodafone360.people.datatypes.ContactDetail) ServiceStatus(com.vodafone360.people.service.ServiceStatus) ArrayList(java.util.ArrayList)

Example 53 with ContactDetail

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

the class DatabaseHelper method syncMergeContactList.

/**
 * Function used by the contact sync engine to merge contacts which are
 * marked as duplicate by the server. This involves moving native
 * information from one contact to the other, before deleting it.
 *
 * @param contactIdList The list of contact IDs (localId, serverId and
 *            mergedLocalId should be set)
 * @return SUCCESS or a suitable error code
 */
public ServiceStatus syncMergeContactList(List<ContactsTable.ContactIdInfo> contactIdList) {
    if (Settings.ENABLED_DATABASE_TRACE)
        trace(false, "DatabaseHelper.syncMergeContactList()");
    List<ContactDetail> detailInfoList = new ArrayList<ContactDetail>();
    SQLiteDatabase writableDb = getWritableDatabase();
    SQLiteStatement contactStatement = null, contactSummaryStatement = null;
    try {
        contactStatement = ContactsTable.mergeContactStatement(writableDb);
        contactSummaryStatement = ContactSummaryTable.mergeContactStatement(writableDb);
        writableDb.beginTransaction();
        for (int i = 0; i < contactIdList.size(); i++) {
            ContactsTable.ContactIdInfo contactIdInfo = contactIdList.get(i);
            if (contactIdInfo.mergedLocalId != null) {
                contactIdInfo.nativeId = ContactsTable.fetchNativeFromLocalId(writableDb, contactIdInfo.localId);
                LogUtils.logI("DatabaseHelper.syncMergeContactList - Copying native Ids from duplicate to original contact: Dup ID " + contactIdInfo.localId + ", Org ID " + contactIdInfo.mergedLocalId + ", Nat ID " + contactIdInfo.nativeId);
                ServiceStatus status = ContactsTable.mergeContact(contactIdInfo, contactStatement);
                if (status != ServiceStatus.SUCCESS) {
                    return status;
                }
                status = ContactSummaryTable.mergeContact(contactIdInfo, contactSummaryStatement);
                if (status != ServiceStatus.SUCCESS) {
                    return status;
                }
                status = ContactDetailsTable.fetchNativeInfo(contactIdInfo.localId, detailInfoList, writableDb);
                if (status != ServiceStatus.SUCCESS) {
                    return status;
                }
                status = ContactDetailsTable.mergeContactDetails(contactIdInfo, detailInfoList, writableDb);
                if (status != ServiceStatus.SUCCESS) {
                    return status;
                }
            }
        }
        writableDb.setTransactionSuccessful();
    } finally {
        writableDb.endTransaction();
        if (contactStatement != null) {
            contactStatement.close();
            contactStatement = null;
        }
        if (contactSummaryStatement != null) {
            contactSummaryStatement.close();
            contactSummaryStatement = null;
        }
    }
    LogUtils.logI("DatabaseHelper.syncMergeContactList - Deleting duplicate contacts");
    return syncDeleteContactList(contactIdList, false, true);
}
Also used : ContactDetail(com.vodafone360.people.datatypes.ContactDetail) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteStatement(android.database.sqlite.SQLiteStatement) ContactsTable(com.vodafone360.people.database.tables.ContactsTable) ServiceStatus(com.vodafone360.people.service.ServiceStatus) ArrayList(java.util.ArrayList)

Example 54 with ContactDetail

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

the class DatabaseHelper method addContactDetail.

/**
 * Adds a contact detail to the database and fires an internal database
 * change event.
 *
 * @param detail A {@link ContactDetail} object which contains the detail to
 *            add
 * @return SUCCESS or a suitable error code
 * @see #modifyContactDetail(ContactDetail)
 * @see #deleteContactDetail(long)
 * @see #addContact(Contact)
 * @see #deleteContact(long)
 * @see #addContactToGroup(long, long)
 * @see #deleteContactFromGroup(long, long)
 * @throws NullPointerException When detail is NULL
 */
public ServiceStatus addContactDetail(ContactDetail detail) {
    if (Settings.ENABLED_DATABASE_TRACE) {
        trace(false, "DatabaseHelper.addContactDetail() name[" + detail.getName() + "]");
    }
    if (detail == null) {
        throw new NullPointerException("DatabaseHelper.addContactDetail() detail should not be NULL");
    }
    boolean isMeProfile = (SyncMeDbUtils.getMeProfileLocalContactId(this) != null && detail.localContactID != null && detail.localContactID.equals(SyncMeDbUtils.getMeProfileLocalContactId(this)));
    List<ContactDetail> detailList = new ArrayList<ContactDetail>();
    detailList.add(detail);
    ServiceStatus status = syncAddContactDetailList(detailList, !isMeProfile, !isMeProfile);
    if (status == ServiceStatus.SUCCESS) {
        fireDatabaseChangedEvent(DatabaseChangeType.CONTACTS, false);
        if (isMeProfile) {
            WidgetUtils.kickWidgetUpdateNow(mContext);
        }
    }
    return status;
}
Also used : ContactDetail(com.vodafone360.people.datatypes.ContactDetail) ServiceStatus(com.vodafone360.people.service.ServiceStatus) ArrayList(java.util.ArrayList)

Example 55 with ContactDetail

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

the class DatabaseHelper method syncModifyContactList.

/**
 * Function used by the contact sync engine to modify a list of contacts in
 * 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 modified in the
 *            native phonebook
 * @return SUCCESS or a suitable error code
 */
public ServiceStatus syncModifyContactList(List<Contact> contactList, boolean syncToServer, boolean syncToNative) {
    if (Settings.ENABLED_DATABASE_TRACE)
        trace(false, "DatabaseHelper.syncModifyContactList() 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) {
        if (syncToServer) {
            contact.updated = null;
        }
        try {
            writableDb.beginTransaction();
            ServiceStatus status = ContactsTable.modifyContact(contact, writableDb);
            if (ServiceStatus.SUCCESS != status) {
                LogUtils.logE("DatabaseHelper.syncModifyContactList() Unable to modify contact, due to a database error");
                return status;
            }
            status = ContactSummaryTable.modifyContact(contact, writableDb);
            if (ServiceStatus.SUCCESS != status) {
                return ServiceStatus.ERROR_DATABASE_CORRUPT;
            }
            if (contact.groupList != null) {
                status = ContactGroupsTable.modifyContact(contact, writableDb);
                if (ServiceStatus.SUCCESS != status) {
                    return status;
                }
            }
            if (contact.sources != null) {
                if (!ContactSourceTable.deleteAllContactSources(contact.localContactID, writableDb)) {
                    return ServiceStatus.ERROR_DATABASE_CORRUPT;
                }
                for (String source : contact.sources) {
                    if (!ContactSourceTable.addContactSource(contact.localContactID, source, writableDb)) {
                        return ServiceStatus.ERROR_DATABASE_CORRUPT;
                    }
                }
            }
            // END updating timeline events
            // Update the summary with the new contact
            String displayName = updateContactNameInSummary(writableDb, contact.localContactID, SyncMeDbUtils.isMeProfile(this, contact.localContactID));
            if (null == displayName) {
                return ServiceStatus.ERROR_DATABASE_CORRUPT;
            }
            // updating phone no
            for (ContactDetail detail : contact.details) {
                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) 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