use of com.vodafone360.people.database.tables.ContactDetailsTable.NativeIdInfo in project 360-Engine-for-Android by 360.
the class NowPlusContactDetailsTableTest method testContactsMatch.
@MediumTest
public void testContactsMatch() {
final String fnName = "testContactsMatch";
mTestStep = 1;
Log.i(LOG_TAG, "***** EXECUTING " + fnName + "*****");
Log.i(LOG_TAG, "Validates doContactsMatch method");
SQLiteDatabase writeableDb = mTestDatabase.getWritableDatabase();
SQLiteDatabase readableDb = mTestDatabase.getReadableDatabase();
Contact contact = mTestModule.createDummyContactData();
contact.localContactID = TestModule.generateRandomLong();
startSubTest(fnName, "Creating table");
createTable();
List<NativeIdInfo> detailIdList = new ArrayList<NativeIdInfo>();
assertFalse(ContactDetailsTable.doContactsMatch(contact, contact.localContactID, detailIdList, readableDb));
ServiceStatus status;
for (ContactDetail cd : contact.details) {
cd.localContactID = contact.localContactID;
status = ContactDetailsTable.addContactDetail(cd, true, true, writeableDb);
assertEquals(ServiceStatus.SUCCESS, status);
}
// assertTrue(ContactDetailsTable.doContactsMatch(contact,
// contact.localContactID, detailIdList, readableDb));
Log.i(LOG_TAG, "***********************************************");
Log.i(LOG_TAG, fnName + " has completed successfully");
Log.i(LOG_TAG, "***********************************************");
}
use of com.vodafone360.people.database.tables.ContactDetailsTable.NativeIdInfo in project 360-Engine-for-Android by 360.
the class NowPlusContactDetailsTableTest method testSyncSetNativeIds.
/*
* test syncNativeIds
*/
public void testSyncSetNativeIds() {
final String fnName = "testSyncSetNativeIds";
mTestStep = 1;
Log.i(LOG_TAG, "***** EXECUTING " + fnName + "*****");
Log.i(LOG_TAG, "Validates syncSetNativeIds details");
SQLiteDatabase writeableDb = mTestDatabase.getWritableDatabase();
SQLiteDatabase readableDb = mTestDatabase.getReadableDatabase();
startSubTest(fnName, "Creating table");
createTable();
List<NativeIdInfo> nativeIdList = new ArrayList<NativeIdInfo>();
List<ContactDetail> detailsList = new ArrayList<ContactDetail>();
for (int i = 0; i < NUM_OF_CONTACTS; i++) {
ContactDetail detail = new ContactDetail();
detail.localContactID = TestModule.generateRandomLong();
mTestModule.createDummyDetailsData(detail);
ContactDetailsTable.addContactDetail(detail, true, true, writeableDb);
NativeIdInfo nativeIdInfo = new NativeIdInfo();
nativeIdInfo.nativeContactId = TestModule.generateRandomInt();
nativeIdInfo.localId = detail.localDetailID;
nativeIdList.add(nativeIdInfo);
detailsList.add(detail);
}
ServiceStatus status = ContactDetailsTable.syncSetNativeIds(nativeIdList, writeableDb);
assertEquals(ServiceStatus.SUCCESS, status);
for (int i = 0; i < NUM_OF_CONTACTS; i++) {
ContactDetail fetchedDetail = ContactDetailsTable.fetchDetail(nativeIdList.get(i).localId, readableDb);
assertNotNull(fetchedDetail);
assertEquals(nativeIdList.get(i).nativeContactId, fetchedDetail.nativeContactId);
}
Log.i(LOG_TAG, "***********************************************");
Log.i(LOG_TAG, fnName + " has completed successfully");
Log.i(LOG_TAG, "***********************************************");
}
use of com.vodafone360.people.database.tables.ContactDetailsTable.NativeIdInfo in project 360-Engine-for-Android by 360.
the class ContactDetailsTable method doContactsMatch.
/**
* Compares the details given with a specific contact in the database. If
* the contacts match a list of native sync information is returned.
*
* @param c Contact to compare
* @param localContactId Contact in the database to compare with
* @param detailIdList A list which will be populated if a match is found.
* @param readableDb A readable SQLite database object
* @return true if a match is found, false otherwise
*/
public static boolean doContactsMatch(Contact c, long localContactId, List<NativeIdInfo> detailIdList, SQLiteDatabase readableDb) {
List<ContactDetail> srcDetails = new ArrayList<ContactDetail>();
ServiceStatus status = fetchContactDetails(localContactId, srcDetails, readableDb);
if (ServiceStatus.SUCCESS != status) {
return false;
}
detailIdList.clear();
for (int i = 0; i < c.details.size(); i++) {
final ContactDetail destDetail = c.details.get(i);
Long foundDetailId = null;
for (int j = 0; j < srcDetails.size(); j++) {
final ContactDetail srcDetail = srcDetails.get(j);
if (srcDetail.changeID == null && srcDetail.key != null && srcDetail.key.equals(destDetail.key)) {
if (srcDetail.value != null && srcDetail.value.equals(destDetail.value)) {
foundDetailId = srcDetail.localDetailID;
srcDetail.changeID = 1L;
break;
}
if (srcDetail.value == null && destDetail.value == null) {
foundDetailId = srcDetail.localDetailID;
srcDetail.changeID = 1L;
break;
}
if (srcDetail.key == ContactDetail.DetailKeys.VCARD_NAME && srcDetail.value != null && srcDetail.value.indexOf(VCardHelper.LIST_SEPARATOR) < 0) {
VCardHelper.Name name1 = srcDetail.getName();
VCardHelper.Name name2 = destDetail.getName();
if (name1 != null && name2 != null && name1.toString().equals(name2.toString())) {
foundDetailId = srcDetail.localDetailID;
srcDetail.changeID = 1L;
}
}
if (srcDetail.key == ContactDetail.DetailKeys.VCARD_ADDRESS && srcDetail.value != null && srcDetail.value.indexOf(VCardHelper.LIST_SEPARATOR) < 0) {
VCardHelper.PostalAddress addr1 = srcDetail.getPostalAddress();
VCardHelper.PostalAddress addr2 = destDetail.getPostalAddress();
if (addr1 != null && addr2 != null && addr1.toString().equals(addr2.toString())) {
foundDetailId = srcDetail.localDetailID;
srcDetail.changeID = 1L;
}
}
}
}
if (foundDetailId == null) {
if (destDetail.value != null && destDetail.value.length() > 0) {
LogUtils.logD("ContactDetailTable.doContactsMatch - The detail " + destDetail.key + ", <" + destDetail.value + "> was not found");
for (int j = 0; j < srcDetails.size(); j++) {
final ContactDetail srcDetail = srcDetails.get(j);
if (srcDetail.key != null && srcDetail.key.equals(destDetail.key)) {
LogUtils.logD("ContactDetailTable.doContactsMatch - No Match Key: " + srcDetail.key + ", Value: <" + srcDetail.value + ">, Used: " + srcDetail.changeID);
}
}
return false;
}
} else {
NativeIdInfo nativeIdInfo = new NativeIdInfo();
nativeIdInfo.localId = foundDetailId;
nativeIdInfo.nativeContactId = c.nativeContactId;
nativeIdInfo.nativeDetailId = destDetail.nativeDetailId;
nativeIdInfo.nativeVal1 = destDetail.nativeVal1;
nativeIdInfo.nativeVal2 = destDetail.nativeVal2;
nativeIdInfo.nativeVal3 = destDetail.nativeVal3;
detailIdList.add(nativeIdInfo);
}
}
for (int j = 0; j < srcDetails.size(); j++) {
final ContactDetail srcDetail = srcDetails.get(j);
if (srcDetail.nativeContactId == null) {
boolean found = false;
for (int i = 0; i < detailIdList.size(); i++) {
NativeIdInfo info = detailIdList.get(i);
if (info.localId == srcDetail.localDetailID.longValue()) {
found = true;
break;
}
}
if (!found) {
NativeIdInfo nativeIdInfo = new NativeIdInfo();
nativeIdInfo.localId = srcDetail.localDetailID;
nativeIdInfo.nativeContactId = srcDetail.nativeContactId;
nativeIdInfo.nativeDetailId = srcDetail.nativeDetailId;
nativeIdInfo.nativeVal1 = srcDetail.nativeVal1;
nativeIdInfo.nativeVal2 = srcDetail.nativeVal2;
nativeIdInfo.nativeVal3 = srcDetail.nativeVal3;
nativeIdInfo.syncNativeContactId = c.nativeContactId;
detailIdList.add(nativeIdInfo);
}
}
}
return true;
}
use of com.vodafone360.people.database.tables.ContactDetailsTable.NativeIdInfo 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;
}
Aggregations