Search in sources :

Example 16 with ContactIdInfo

use of com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo in project 360-Engine-for-Android by 360.

the class ContactsTable method syncSetServerIds.

/**
 * Updates the server and user IDs for a list of contacts. Also prepares a
 * list of duplicates which will be filled with the Ids for contacts already
 * present in the table (i.e. server ID has already been used). In the case
 * that a duplicate is found, the ContactIdInfo object will also include the
 * local ID of the original contact (see {@link ContactIdInfo#mergedLocalId}
 * ).
 *
 * @param serverIdList A list of ServerIdInfo objects. For each object, the
 *            local ID must match a local contact ID in the table. The
 *            Server ID and User ID will be used for the update.
 * @param dupList On return this will be populated with a list of contacts
 *            which have server IDs already present in the table.
 * @param writeableDb Writeable SQLite database
 * @return SUCCESS or a suitable error code
 */
public static ServiceStatus syncSetServerIds(List<ServerIdInfo> serverIdList, List<ContactIdInfo> dupList, SQLiteDatabase writableDb) {
    DatabaseHelper.trace(true, "ContactsTable.syncSetServerIds()");
    if (serverIdList.size() == 0) {
        return ServiceStatus.SUCCESS;
    }
    SQLiteStatement statement1 = null;
    SQLiteStatement statement2 = null;
    try {
        writableDb.beginTransaction();
        for (int i = 0; i < serverIdList.size(); i++) {
            final ServerIdInfo info = serverIdList.get(i);
            try {
                if (info.serverId != null) {
                    if (info.userId == null) {
                        if (statement2 == null) {
                            statement2 = writableDb.compileStatement("UPDATE " + TABLE_NAME + " SET " + Field.SERVERID + "=? WHERE " + Field.LOCALID + "=?");
                        }
                        statement2.bindLong(1, info.serverId);
                        statement2.bindLong(2, info.localId);
                        statement2.execute();
                    } else {
                        if (statement1 == null) {
                            statement1 = writableDb.compileStatement("UPDATE " + TABLE_NAME + " SET " + Field.SERVERID + "=?," + Field.USERID + "=? WHERE " + Field.LOCALID + "=?");
                        }
                        statement1.bindLong(1, info.serverId);
                        statement1.bindLong(2, info.userId);
                        statement1.bindLong(3, info.localId);
                        statement1.execute();
                    }
                }
            } catch (SQLiteConstraintException e) {
                // server ID is not unique
                ContactIdInfo contactInfo = new ContactIdInfo();
                contactInfo.localId = info.localId;
                contactInfo.serverId = info.serverId;
                if (!fetchLocalIDFromServerID(writableDb, contactInfo)) {
                    writableDb.endTransaction();
                    return ServiceStatus.ERROR_DATABASE_CORRUPT;
                }
                dupList.add(contactInfo);
            } catch (SQLException e) {
                LogUtils.logE("ContactsTable.syncSetServerIds() SQLException - " + "Unable to update contact server Ids", e);
                return ServiceStatus.ERROR_DATABASE_CORRUPT;
            }
        }
        writableDb.setTransactionSuccessful();
    } finally {
        writableDb.endTransaction();
        if (statement1 != null) {
            statement1.close();
            statement1 = null;
        }
        if (statement2 != null) {
            statement2.close();
            statement2 = null;
        }
    }
    return ServiceStatus.SUCCESS;
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLException(android.database.SQLException) ServerIdInfo(com.vodafone360.people.database.DatabaseHelper.ServerIdInfo) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException)

Example 17 with ContactIdInfo

use of com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo in project 360-Engine-for-Android by 360.

the class ContactDetailsTable method findNativeContact.

/**
 * Searches the contact details table for a contact from the native
 * phonebook. If a match is found, the native sync information is transfered
 * from the given contact into the matching database contact. Tries to match
 * in the following sequence: 1) If there is a name, match by name 2)
 * Otherwise, if there is a phone number, match by number 3) Otherwise, if
 * there is an email, match by email 4) Otherwise return false For a match
 * to occur, all given contact details must be identical to those in the
 * database. There may be more details in the database but this won't change
 * the result.
 *
 * @param c The contact to match
 * @param writableDb A writable SQLite database object
 * @return true if the contact was found, false otherwise
 */
public static boolean findNativeContact(Contact c, SQLiteDatabase writableDb) {
    String name = null;
    String phone = null;
    String email = null;
    List<ContactIdInfo> contactIdList = new ArrayList<ContactIdInfo>();
    List<NativeIdInfo> detailIdList = new ArrayList<NativeIdInfo>();
    for (int i = 0; i < c.details.size(); i++) {
        if (c.details.get(i).key == ContactDetail.DetailKeys.VCARD_NICKNAME) {
            name = c.details.get(i).getValue();
            if (name != null && name.length() > 0) {
                break;
            }
        }
        if (c.details.get(i).key == ContactDetail.DetailKeys.VCARD_PHONE) {
            if (phone == null || phone.length() > 0) {
                phone = c.details.get(i).getValue();
            }
        }
        if (c.details.get(i).key == ContactDetail.DetailKeys.VCARD_EMAIL) {
            if (email == null || email.length() > 0) {
                email = c.details.get(i).getValue();
            }
        }
    }
    Cursor candidateListCursor = null;
    if (name != null && name.length() > 0) {
        LogUtils.logD("ContactDetailsTable.findNativeContact - " + "Searching for contact called " + name);
        candidateListCursor = findDetailByKey(name, ContactDetail.DetailKeys.VCARD_NICKNAME, writableDb);
    } else if (phone != null && phone.length() > 0) {
        LogUtils.logD("ContactDetailsTable.findNativeContact - " + "Searching for contact with phone " + phone);
        candidateListCursor = findDetailByKey(phone, ContactDetail.DetailKeys.VCARD_PHONE, writableDb);
    } else if (email != null && email.length() > 0) {
        LogUtils.logD("ContactDetailsTable.findNativeContact - " + "Searching for contact with email " + email);
        candidateListCursor = findDetailByKey(email, ContactDetail.DetailKeys.VCARD_EMAIL, writableDb);
    }
    List<NativeIdInfo> tempDetailIdList = new ArrayList<NativeIdInfo>();
    List<NativeIdInfo> currentDetailIdList = new ArrayList<NativeIdInfo>();
    Integer minNoOfDetails = null;
    Long chosenContactId = null;
    if (candidateListCursor != null) {
        while (candidateListCursor.moveToNext()) {
            long localContactId = candidateListCursor.getLong(1);
            tempDetailIdList.clear();
            if (doContactsMatch(c, localContactId, tempDetailIdList, writableDb)) {
                if (minNoOfDetails == null || minNoOfDetails.intValue() > tempDetailIdList.size()) {
                    if (ContactsTable.fetchSyncToPhone(localContactId, writableDb)) {
                        minNoOfDetails = tempDetailIdList.size();
                        chosenContactId = localContactId;
                        currentDetailIdList.clear();
                        currentDetailIdList.addAll(tempDetailIdList);
                    }
                }
            }
        }
        candidateListCursor.close();
        if (chosenContactId != null) {
            LogUtils.logD("ContactDetailsTable.findNativeContact - " + "Found contact (no need to add)");
            ContactIdInfo contactIdInfo = new ContactIdInfo();
            contactIdInfo.localId = chosenContactId;
            contactIdInfo.nativeId = c.nativeContactId;
            contactIdList.add(contactIdInfo);
            detailIdList.addAll(currentDetailIdList);
            // Update contact IDs of the contacts which are already in the
            // database
            ServiceStatus status = ContactsTable.syncSetNativeIds(contactIdList, writableDb);
            if (ServiceStatus.SUCCESS != status) {
                return false;
            }
            status = ContactSummaryTable.syncSetNativeIds(contactIdList, writableDb);
            if (ServiceStatus.SUCCESS != status) {
                return false;
            }
            status = ContactDetailsTable.syncSetNativeIds(detailIdList, writableDb);
            if (ServiceStatus.SUCCESS != status) {
                return false;
            }
            return true;
        }
    }
    LogUtils.logD("ContactDetailsTable.findNativeContact - Contact not found (will be added)");
    return false;
}
Also used : ContactIdInfo(com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo) ServiceStatus(com.vodafone360.people.service.ServiceStatus) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Example 18 with ContactIdInfo

use of com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo in project 360-Engine-for-Android by 360.

the class DatabaseHelper method deleteContact.

/**
 * Deletes a contact from the database and fires an internal database change
 * event.
 *
 * @param localContactID The local ID of the contact to delete
 * @return SUCCESS or a suitable error code
 * @see #addContact(Contact)
 * @see #addContactDetail(ContactDetail)
 * @see #modifyContactDetail(ContactDetail)
 * @see #deleteContactDetail(long)
 * @see #addContactToGroup(long, long)
 * @see #deleteContactFromGroup(long, long)
 */
public ServiceStatus deleteContact(long localContactID) {
    if (Settings.ENABLED_DATABASE_TRACE) {
        trace(false, "DatabaseHelper.deleteContact() localContactID[" + localContactID + "]");
    }
    if (SyncMeDbUtils.getMeProfileLocalContactId(this) != null && SyncMeDbUtils.getMeProfileLocalContactId(this).longValue() == localContactID) {
        LogUtils.logW("DatabaseHelper.deleteContact() Can not delete the Me profile contact");
        return ServiceStatus.ERROR_NOT_FOUND;
    }
    ContactsTable.ContactIdInfo contactIdInfo = ContactsTable.validateContactId(localContactID, getWritableDatabase());
    List<ContactsTable.ContactIdInfo> idList = new ArrayList<ContactsTable.ContactIdInfo>();
    idList.add(contactIdInfo);
    ServiceStatus status = syncDeleteContactList(idList, true, true);
    if (ServiceStatus.SUCCESS == status) {
        fireDatabaseChangedEvent(DatabaseChangeType.CONTACTS, false);
    }
    return status;
}
Also used : ContactsTable(com.vodafone360.people.database.tables.ContactsTable) ServiceStatus(com.vodafone360.people.service.ServiceStatus) ArrayList(java.util.ArrayList)

Example 19 with ContactIdInfo

use of com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo in project 360-Engine-for-Android by 360.

the class DatabaseHelper method syncAddContactDetailList.

/**
 * Function used by the contact sync engine to add a list of contact details
 * to the database.
 *
 * @param detailList The list of details received from the server
 * @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 #addContactDetail(ContactDetail)
 */
public ServiceStatus syncAddContactDetailList(List<ContactDetail> detailList, boolean syncToServer, boolean syncToNative) {
    if (Settings.ENABLED_DATABASE_TRACE) {
        trace(false, "DatabaseHelper.syncAddContactDetailList() 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 : detailList) {
        contactDetail.localDetailID = null;
        if (syncToServer) {
            contactDetail.unique_id = null;
        }
        if (syncToNative) {
            contactDetail.nativeDetailId = null;
        }
        if (contactDetail.localContactID == null) {
            return ServiceStatus.ERROR_NOT_FOUND;
        }
        try {
            db.beginTransaction();
            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;
            }
            if (contactDetail.order != null && contactDetail.order.equals(ContactDetail.ORDER_PREFERRED)) {
                ContactDetailsTable.removePreferred(contactDetail.localContactID, contactDetail.key, db);
            }
            ServiceStatus status = ContactDetailsTable.addContactDetail(contactDetail, syncToServer, syncToNative, db);
            if (ServiceStatus.SUCCESS != status) {
                return status;
            }
            // in order not to override the new picture before it is uploaded.
            if (contactDetail.key == ContactDetail.DetailKeys.PHOTO && 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)

Aggregations

ServiceStatus (com.vodafone360.people.service.ServiceStatus)13 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)11 ContactIdInfo (com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo)10 ArrayList (java.util.ArrayList)9 ContactsTable (com.vodafone360.people.database.tables.ContactsTable)7 Contact (com.vodafone360.people.datatypes.Contact)7 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)7 SQLiteStatement (android.database.sqlite.SQLiteStatement)5 MediumTest (android.test.suitebuilder.annotation.MediumTest)4 SQLException (android.database.SQLException)3 ServerIdInfo (com.vodafone360.people.database.DatabaseHelper.ServerIdInfo)3 Cursor (android.database.Cursor)2 ContentValues (android.content.ContentValues)1 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)1 SQLiteException (android.database.sqlite.SQLiteException)1 ContactChanges (com.vodafone360.people.datatypes.ContactChanges)1 VCardHelper (com.vodafone360.people.datatypes.VCardHelper)1 ContactChange (com.vodafone360.people.engine.contactsync.ContactChange)1 PeopleContactsApi (com.vodafone360.people.engine.contactsync.PeopleContactsApi)1 NativeContactDetails (com.vodafone360.people.tests.TestModule.NativeContactDetails)1