Search in sources :

Example 91 with ServiceStatus

use of com.vodafone360.people.service.ServiceStatus 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 92 with ServiceStatus

use of com.vodafone360.people.service.ServiceStatus 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 93 with ServiceStatus

use of com.vodafone360.people.service.ServiceStatus 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)

Example 94 with ServiceStatus

use of com.vodafone360.people.service.ServiceStatus 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 95 with ServiceStatus

use of com.vodafone360.people.service.ServiceStatus in project 360-Engine-for-Android by 360.

the class ContactSummaryTable method syncSetNativeIds.

/**
 * Updates the native IDs for a list of contacts.
 *
 * @param contactIdList A list of ContactIdInfo objects. For each object,
 *            the local ID must match a local contact ID in the table. The
 *            Native ID will be used for the update. Other fields are
 *            unused.
 * @param writeableDb Writable SQLite database
 * @return SUCCESS or a suitable error code
 */
public static ServiceStatus syncSetNativeIds(List<ContactIdInfo> contactIdList, SQLiteDatabase writableDb) {
    DatabaseHelper.trace(true, "ContactSummaryTable.syncSetNativeIds()");
    if (contactIdList.size() == 0) {
        return ServiceStatus.SUCCESS;
    }
    final SQLiteStatement statement1 = writableDb.compileStatement("UPDATE " + TABLE_NAME + " SET " + Field.NATIVEID + "=? WHERE " + Field.LOCALCONTACTID + "=?");
    for (int i = 0; i < contactIdList.size(); i++) {
        final ContactIdInfo info = contactIdList.get(i);
        try {
            writableDb.beginTransaction();
            if (info.nativeId == null) {
                statement1.bindNull(1);
            } else {
                statement1.bindLong(1, info.nativeId);
            }
            statement1.bindLong(2, info.localId);
            statement1.execute();
            writableDb.setTransactionSuccessful();
        } catch (SQLException e) {
            LogUtils.logE("ContactSummaryTable.syncSetNativeIds() " + "SQLException - Unable to update contact native Ids", e);
            return ServiceStatus.ERROR_DATABASE_CORRUPT;
        } finally {
            writableDb.endTransaction();
        }
    }
    return ServiceStatus.SUCCESS;
}
Also used : ContactIdInfo(com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo) SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLException(android.database.SQLException)

Aggregations

ServiceStatus (com.vodafone360.people.service.ServiceStatus)190 Contact (com.vodafone360.people.datatypes.Contact)71 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)63 MediumTest (android.test.suitebuilder.annotation.MediumTest)62 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)43 ArrayList (java.util.ArrayList)43 Suppress (android.test.suitebuilder.annotation.Suppress)40 Cursor (android.database.Cursor)17 SmallTest (android.test.suitebuilder.annotation.SmallTest)17 ContactSummary (com.vodafone360.people.datatypes.ContactSummary)16 Bundle (android.os.Bundle)12 ContentValues (android.content.ContentValues)11 VCardHelper (com.vodafone360.people.datatypes.VCardHelper)11 SQLException (android.database.SQLException)9 ServerIdInfo (com.vodafone360.people.database.DatabaseHelper.ServerIdInfo)9 ContactsTable (com.vodafone360.people.database.tables.ContactsTable)9 ContactIdInfo (com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo)9 ActivityItem (com.vodafone360.people.datatypes.ActivityItem)8 LoginDetails (com.vodafone360.people.datatypes.LoginDetails)8 PersistSettings (com.vodafone360.people.service.PersistSettings)8