use of com.vodafone360.people.datatypes.ContactDetail in project 360-Engine-for-Android by 360.
the class ContactDetailsTable method fetchDetail.
/**
* Fetches the first contact detail found for a contact and key.
*
* @param localContactId The local contact ID
* @param key The contact detail key value
* @param readableDb A readable SQLite database object.
* @return The contact detail, or NULL if it could not be found.
*/
public static ContactDetail fetchDetail(long localContactId, DetailKeys key, SQLiteDatabase readableDb) {
DatabaseHelper.trace(false, "ContactDetailsTable.fetchDetail()");
String[] args = { String.format("%d", localContactId), String.format("%d", key.ordinal()) };
ContactDetail detail = null;
Cursor c = null;
try {
c = readableDb.rawQuery(getQueryStringSql(Field.LOCALCONTACTID + "=? AND " + Field.KEY + "=?"), args);
if (c.moveToFirst()) {
detail = getQueryData(c);
}
} catch (SQLiteException e) {
LogUtils.logE("ContactDetailsTable.fetchDetail() Exception - Unable to fetch contact detail", e);
return null;
} finally {
CloseUtils.close(c);
c = null;
}
return detail;
}
use of com.vodafone360.people.datatypes.ContactDetail in project 360-Engine-for-Android by 360.
the class ContactDetailsTable method syncServerGetNextNewContactDetails.
/**
* Returns the next batch of contacts which need to be added on the server.
* The {@link #syncServerFetchContactChanges(SQLiteDatabase, boolean)}
* method is used to retrieve the cursor initially, then this function can
* be called many times until all the contacts have been fetched. When the
* list returned from this method is empty the cursor has reached the end
* and the sync is complete.
*
* @param c The cursor (see description above)
* @param contactList Will be filled with contacts that need to be added to
* the server
* @param maxContactsToFetch Maximum number of contacts to return in the
* list. The function can be called in a loop until all the
* contacts have been retrieved.
*/
public static void syncServerGetNextNewContactDetails(Cursor c, List<Contact> contactList, int maxContactsToFetch) {
final int QUERY_COLUMN_LOCALCONTACTID = 0;
final int QUERY_COLUMN_SERVERSYNCCONTACTID = 1;
final int QUERY_COLUMN_LOCALDETAILID = 2;
final int QUERY_COLUMN_SERVERDETAILID = 3;
final int QUERY_COLUMN_KEY = 4;
final int QUERY_COLUMN_KEYTYPE = 5;
final int QUERY_COLUMN_VAL = 6;
final int QUERY_COLUMN_ORDER = 7;
final int QUERY_COLUMN_PHOTOURL = 8;
contactList.clear();
Contact currentContact = null;
while (c.moveToNext()) {
final ContactDetail detail = new ContactDetail();
if (!c.isNull(QUERY_COLUMN_LOCALCONTACTID)) {
detail.localContactID = c.getLong(QUERY_COLUMN_LOCALCONTACTID);
}
if (!c.isNull(QUERY_COLUMN_SERVERSYNCCONTACTID)) {
detail.serverContactId = c.getLong(QUERY_COLUMN_SERVERSYNCCONTACTID);
}
if (currentContact == null || !currentContact.localContactID.equals(detail.localContactID)) {
if (contactList.size() >= maxContactsToFetch) {
if (currentContact != null) {
c.moveToPrevious();
}
break;
}
currentContact = new Contact();
currentContact.localContactID = detail.localContactID;
if (detail.serverContactId == null) {
currentContact.synctophone = true;
}
currentContact.contactID = detail.serverContactId;
contactList.add(currentContact);
}
if (!c.isNull(QUERY_COLUMN_LOCALDETAILID)) {
detail.localDetailID = c.getLong(QUERY_COLUMN_LOCALDETAILID);
}
if (!c.isNull(QUERY_COLUMN_SERVERDETAILID)) {
detail.unique_id = c.getLong(QUERY_COLUMN_SERVERDETAILID);
}
detail.key = ContactDetail.DetailKeys.values()[c.getInt(QUERY_COLUMN_KEY)];
if (!c.isNull(QUERY_COLUMN_KEYTYPE)) {
detail.keyType = ContactDetail.DetailKeyTypes.values()[c.getInt(QUERY_COLUMN_KEYTYPE)];
}
detail.value = c.getString(QUERY_COLUMN_VAL);
if (!c.isNull(QUERY_COLUMN_ORDER)) {
detail.order = c.getInt(QUERY_COLUMN_ORDER);
}
if (!c.isNull(QUERY_COLUMN_PHOTOURL)) {
detail.photo_url = c.getString(QUERY_COLUMN_PHOTOURL);
}
currentContact.details.add(detail);
}
}
use of com.vodafone360.people.datatypes.ContactDetail in project 360-Engine-for-Android by 360.
the class ContactDetailsTable method mergeContactDetails.
/**
* Moves native details ownership from the duplicate contact to the original
* contact.
*
* @param info the info for duplicated and original contacts
* @param nativeInfoList the list of native details from the duplicated
* contact
* @param writableDb A writable SQLite database object
* @return SUCCESS or a suitable error code.
*/
public static ServiceStatus mergeContactDetails(ContactIdInfo info, List<ContactDetail> nativeInfoList, SQLiteDatabase writableDb) {
DatabaseHelper.trace(true, "ContactDetailsTable.mergeContactDetails()");
/**
* Used to hold some contact details info.
*/
final class DetailsInfo {
/**
* The detail local id.
*/
public Long localId;
/**
* The detail server id.
*/
public Long serverId;
public DetailsInfo(Long localId, Long serverId) {
this.localId = localId;
this.serverId = serverId;
}
}
// the list of details from the original contact
List<DetailsInfo> detailLocalIds = new ArrayList<DetailsInfo>();
// the result cursor from the original contact details query
Cursor cursor = null;
try {
// Retrieve a list of detail local IDs from the merged contact
// (original one)
final String[] args = { String.valueOf(info.mergedLocalId) };
cursor = writableDb.rawQuery(QUERY_DETAIL_LOCAL_AND_SERVER_IDS_BY_LOCAL_CONTACT_ID, args);
while (cursor.moveToNext()) {
if (!cursor.isNull(0) && !cursor.isNull(1)) {
// only adding details with a detailServerId (Name and
// Nickname don't have detailServerIds)
detailLocalIds.add(new DetailsInfo(cursor.getLong(0), cursor.getLong(1)));
}
}
DatabaseHelper.trace(true, "ContactDetailsTable.mergeContactDetails(): detailLocalIds.size()=" + detailLocalIds.size());
} catch (Exception e) {
LogUtils.logE("ContactDetailsTable.mergeContactDetails() Exception - " + "Unable to query merged contact details list", e);
return ServiceStatus.ERROR_DATABASE_CORRUPT;
} finally {
if (cursor != null) {
cursor.close();
cursor = null;
}
}
try {
final ContentValues cv = new ContentValues();
// duplicated contact to the original contact
for (int infoListIndex = 0; infoListIndex < nativeInfoList.size(); infoListIndex++) {
final ContactDetail detailInfo = nativeInfoList.get(infoListIndex);
// Change the ownership
for (int detailsIndex = 0; detailsIndex < detailLocalIds.size(); detailsIndex++) {
final DetailsInfo currentDetails = detailLocalIds.get(detailsIndex);
if (currentDetails.serverId.equals(detailInfo.unique_id)) {
cv.put(Field.LOCALCONTACTID.toString(), info.mergedLocalId);
cv.put(Field.NATIVECONTACTID.toString(), detailInfo.nativeContactId);
cv.put(Field.NATIVEDETAILID.toString(), detailInfo.nativeDetailId);
cv.put(Field.NATIVEDETAILVAL1.toString(), detailInfo.nativeVal1);
cv.put(Field.NATIVEDETAILVAL2.toString(), detailInfo.nativeVal2);
cv.put(Field.NATIVEDETAILVAL3.toString(), detailInfo.nativeVal3);
cv.put(Field.NATIVESYNCCONTACTID.toString(), detailInfo.syncNativeContactId);
DatabaseHelper.trace(true, "ContactDetailsTable.mergeContactDetails():" + " changing ownership for duplicated detail: " + detailInfo);
writableDb.update(TABLE_NAME, cv, Field.DETAILLOCALID + "=" + currentDetails.localId, null);
cv.clear();
detailLocalIds.remove(detailsIndex);
break;
}
}
}
return ServiceStatus.SUCCESS;
} catch (SQLException e) {
LogUtils.logE("ContactDetailsTable.mergeContactDetails() SQLException - " + "Unable to merge contact detail native info", e);
return ServiceStatus.ERROR_DATABASE_CORRUPT;
}
}
use of com.vodafone360.people.datatypes.ContactDetail in project 360-Engine-for-Android by 360.
the class ContactSummaryTable method updateContactDisplayName.
/**
* Updates the summary for a contact Replaces the complex logic of updating
* the summary with a new contactdetail. Instead the method gets a whole
* contact after it has been modified and builds the summary infos.
*
* @param contact A Contact object that has been modified
* @param writeableDb Writable SQLite database
* @param isMeProfile Specifies if the contact in question is the Me Contact or not
* @return String - the contact name to display and null in case of database error.
*/
public static String updateContactDisplayName(Contact contact, SQLiteDatabase writableDb, boolean isMeProfile) {
ContactDetail name = getDisplayNameDetail(contact);
String nameString = null;
if (isVcardNameDetail(name)) {
nameString = name.getName().toString();
} else if (!isMeProfile) {
if (name != null) {
// Apply non VCard name
nameString = name.getValue();
}
if (nameString == null) {
// Unknown name
nameString = ContactDetail.UNKNOWN_NAME;
}
} else {
// Me Profile with no name - set the default name
nameString = SyncMeDbUtils.ME_PROFILE_DEFAULT_NAME;
}
// Start updating the table
SQLiteStatement statement = null;
try {
final StringBuffer updateQuery = StringBufferPool.getStringBuffer(SQLKeys.UPDATE);
updateQuery.append(TABLE_NAME).append(SQLKeys.SET).append(Field.DISPLAYNAME).append("=?, ").append(Field.SEARCHNAME).append("=?").append(" WHERE ").append(Field.LOCALCONTACTID).append("=?");
statement = writableDb.compileStatement(StringBufferPool.toStringThenRelease(updateQuery));
writableDb.beginTransaction();
statement.bindString(1, nameString);
// need to update the Field.SEARCHNAME too.
statement.bindString(2, nameString.toLowerCase());
statement.bindLong(3, contact.localContactID);
statement.execute();
writableDb.setTransactionSuccessful();
} catch (SQLException e) {
LogUtils.logE("ContactSummaryTable.updateNameAndStatus() " + "SQLException - Unable to update contact native Ids", e);
return null;
} finally {
writableDb.endTransaction();
if (statement != null) {
statement.close();
statement = null;
}
}
return nameString;
}
use of com.vodafone360.people.datatypes.ContactDetail in project 360-Engine-for-Android by 360.
the class FetchNativeContactsTest method testRunWithOneNewContact.
@MediumTest
@Suppress
public // Breaks tests
void testRunWithOneNewContact() {
final String fnName = "testRunWithOneNewContact";
Log.i(LOG_TAG, "***** EXECUTING " + fnName + " *****");
mTestStep = 1;
startSubTest(fnName, "Checking people database is empty");
Cursor peopleCursor = mDb.openContactSummaryCursor(null, null);
assertEquals(0, peopleCursor.getCount());
assertTrue(Settings.ENABLE_UPDATE_NATIVE_CONTACTS);
startSubTest(fnName, "Checking native database is empty");
Cursor nativeCursor = mCr.query(People.CONTENT_URI, new String[] { People._ID, People.NAME, People.NOTES }, null, null, null);
assertEquals(0, nativeCursor.getCount());
startSubTest(fnName, "Add one dummy native contact");
ContentValues peopleValues = new ContentValues();
peopleValues.put(Contacts.People.NAME, ADD_CONTACT_TEST_NAME);
peopleValues.put(Contacts.People.NOTES, ADD_CONTACT_TEST_NOTE);
Uri personUri1 = mCr.insert(Contacts.People.CONTENT_URI, peopleValues);
assertTrue("Unable to insert contact into native people table", personUri1 != null);
final long personId = ContentUris.parseId(personUri1);
ContentValues phoneValues = new ContentValues();
phoneValues.put(Contacts.Phones.PERSON_ID, personId);
phoneValues.put(Contacts.Phones.NUMBER, ADD_PHONE_TEST1);
phoneValues.put(Contacts.Phones.TYPE, ADD_PHONE_TYPE_TEST1);
Uri phoneUri1 = mCr.insert(Contacts.Phones.CONTENT_URI, phoneValues);
assertTrue("Unable to insert contact into native phone table 1", phoneUri1 != null);
final long phoneId1 = (int) ContentUris.parseId(phoneUri1);
phoneValues.put(Contacts.Phones.NUMBER, ADD_PHONE_TEST2);
phoneValues.put(Contacts.Phones.TYPE, ADD_PHONE_TYPE_TEST2);
phoneValues.put(Contacts.Phones.ISPRIMARY, 1);
Uri phoneUri2 = mCr.insert(Contacts.Phones.CONTENT_URI, phoneValues);
assertTrue("Unable to insert contact into native phone table 2", phoneUri2 != null);
final long phoneId2 = ContentUris.parseId(phoneUri2);
phoneValues.put(Contacts.Phones.NUMBER, ADD_PHONE_TEST3);
phoneValues.put(Contacts.Phones.TYPE, ADD_PHONE_TYPE_TEST3);
phoneValues.remove(Contacts.Phones.ISPRIMARY);
Uri phoneUri3 = mCr.insert(Contacts.Phones.CONTENT_URI, phoneValues);
assertTrue("Unable to insert contact into native phone table 3", phoneUri3 != null);
final long phoneId3 = ContentUris.parseId(phoneUri3);
ContentValues cmValues = new ContentValues();
cmValues.put(Contacts.ContactMethods.PERSON_ID, personId);
cmValues.put(Contacts.ContactMethods.DATA, ADD_CM_TEST1);
cmValues.put(Contacts.ContactMethods.TYPE, ADD_CM_TYPE_TEST1);
cmValues.put(Contacts.ContactMethods.KIND, ADD_CM_KIND_TEST1);
Uri cmUri1 = mCr.insert(Contacts.ContactMethods.CONTENT_URI, cmValues);
assertTrue("Unable to insert contact into native contact methods table 1", cmUri1 != null);
final long cmId1 = ContentUris.parseId(cmUri1);
cmValues.put(Contacts.ContactMethods.DATA, ADD_CM_TEST2);
cmValues.put(Contacts.ContactMethods.TYPE, ADD_CM_TYPE_TEST2);
cmValues.put(Contacts.ContactMethods.KIND, ADD_CM_KIND_TEST2);
cmValues.put(Contacts.ContactMethods.ISPRIMARY, 1);
Uri cmUri2 = mCr.insert(Contacts.ContactMethods.CONTENT_URI, cmValues);
assertTrue("Unable to insert contact into native contact methods table 2", cmUri2 != null);
final long cmId2 = ContentUris.parseId(cmUri2);
ContentValues orgValues = new ContentValues();
orgValues.put(Contacts.Organizations.PERSON_ID, personId);
orgValues.put(Contacts.Organizations.COMPANY, ADD_ORG_COMPANY_TEST1);
orgValues.put(Contacts.Organizations.TITLE, ADD_ORG_TITLE_TEST1);
orgValues.put(Contacts.Organizations.TYPE, ADD_ORG_TYPE_TEST1);
orgValues.put(Contacts.Organizations.LABEL, ADD_ORG_LABEL_TEST1);
Uri orgUri = mCr.insert(Contacts.Organizations.CONTENT_URI, orgValues);
assertTrue("Unable to insert contact into native organizations table 1", orgUri != null);
final long orgId1 = ContentUris.parseId(orgUri);
startSubTest(fnName, "Running processor");
runProcessor();
startSubTest(fnName, "Checking contact has been synced to people");
peopleCursor.requery();
assertEquals(1, peopleCursor.getCount());
assertTrue(peopleCursor.moveToFirst());
ContactSummary summary = ContactSummaryTable.getQueryData(peopleCursor);
assertTrue(summary != null);
Contact newContact = new Contact();
ServiceStatus status = mDb.fetchContact(summary.localContactID, newContact);
assertEquals(ServiceStatus.SUCCESS, status);
boolean doneName = false;
boolean doneNickname = false;
boolean doneNote = false;
boolean donePhone1 = false;
boolean donePhone2 = false;
boolean donePhone3 = false;
boolean doneCm1 = false;
boolean doneCm2 = false;
boolean doneOrg1 = false;
boolean doneTitle1 = false;
assertTrue(newContact.synctophone);
assertEquals(personId, newContact.nativeContactId.longValue());
assertEquals(personId, summary.nativeContactId.longValue());
for (ContactDetail detail : newContact.details) {
assertEquals(personId, detail.nativeContactId.longValue());
detail.syncNativeContactId = fetchSyncNativeId(detail.localDetailID);
assertEquals("No sync marker, ID = " + detail.nativeDetailId + ", key = " + detail.key, Integer.valueOf(-1), detail.syncNativeContactId);
Integer detailId = detail.nativeDetailId;
assertTrue(detailId != null);
switch(detail.key) {
case VCARD_NAME:
assertEquals(personId, detailId.longValue());
assertEquals(ADD_CONTACT_TEST_NAME, detail.nativeVal1);
VCardHelper.Name name = detail.getName();
assertTrue(name != null);
assertEquals(ADD_CONTACT_TEST_NAME, name.toString());
doneName = true;
break;
case VCARD_NICKNAME:
assertEquals(personId, detailId.longValue());
assertEquals(ADD_CONTACT_TEST_NAME, detail.nativeVal1);
assertEquals(ADD_CONTACT_TEST_NAME, detail.getValue());
doneNickname = true;
break;
case VCARD_NOTE:
assertEquals(personId, detailId.longValue());
assertEquals(ADD_CONTACT_TEST_NOTE, detail.nativeVal1);
assertEquals(ADD_CONTACT_TEST_NOTE, detail.getValue());
doneNote = true;
break;
case VCARD_PHONE:
if (detailId.longValue() == phoneId1) {
donePhone1 = true;
assertEquals(ADD_PHONE_TEST1, detail.nativeVal1);
assertEquals(ADD_PHONE_TEST1, detail.getValue());
assertEquals(ADD_PHONE_PEOPLE_TYPE_TEST1, detail.keyType);
assertEquals(Integer.valueOf(ContactDetail.ORDER_NORMAL), detail.order);
} else if (detailId.longValue() == phoneId2) {
donePhone2 = true;
assertEquals(ADD_PHONE_TEST2, detail.nativeVal1);
assertEquals(ADD_PHONE_TEST2, detail.getValue());
assertEquals(ADD_PHONE_PEOPLE_TYPE_TEST2, detail.keyType);
assertEquals(Integer.valueOf(ContactDetail.ORDER_PREFERRED), detail.order);
} else if (detailId.longValue() == phoneId3) {
donePhone3 = true;
assertEquals(ADD_PHONE_TEST3, detail.nativeVal1);
assertEquals(ADD_PHONE_TEST3, detail.getValue());
assertEquals(ADD_PHONE_PEOPLE_TYPE_TEST3, detail.keyType);
assertEquals(Integer.valueOf(ContactDetail.ORDER_NORMAL), detail.order);
} else {
fail("Unknown phone number in people contact: ID:" + detailId + " does not match " + phoneId1 + "," + phoneId2 + "," + phoneId3);
}
break;
case VCARD_EMAIL:
assertTrue(detailId != null);
if (detailId.longValue() == cmId1) {
doneCm1 = true;
assertEquals(ADD_CM_TEST1, detail.nativeVal1);
assertEquals(String.valueOf(ADD_CM_TYPE_TEST1), detail.nativeVal2);
assertEquals(String.valueOf(ADD_CM_KIND_TEST1), detail.nativeVal3);
assertEquals(ADD_CM_TEST1, detail.getValue());
assertEquals(ADD_CM_PEOPLE_TYPE_TEST1, detail.keyType);
} else {
fail("Unknown email in people contact");
}
break;
case VCARD_ADDRESS:
assertTrue(detailId != null);
if (detailId.longValue() == cmId2) {
doneCm2 = true;
assertEquals(ADD_CM_TEST2, detail.nativeVal1);
assertEquals(String.valueOf(ADD_CM_TYPE_TEST2), detail.nativeVal2);
assertEquals(String.valueOf(ADD_CM_KIND_TEST2), detail.nativeVal3);
VCardHelper.PostalAddress address = detail.getPostalAddress();
assertTrue(address != null);
assertEquals(ADD_CM_TEST2_ADDRESS1, address.addressLine1);
assertEquals(ADD_CM_TEST2_ADDRESS2, address.addressLine2);
assertEquals(ADD_CM_TEST2_ADDRESS_CITY, address.city);
assertEquals(ADD_CM_TEST2_ADDRESS_COUNTY, address.county);
assertEquals(ADD_CM_TEST2_ADDRESS_POSTCODE, address.postCode);
assertEquals(ADD_CM_TEST2_ADDRESS_COUNTRY, address.country);
assertEquals(ADD_CM_PEOPLE_TYPE_TEST2, detail.keyType);
} else {
fail("Unknown address in people contact");
}
break;
case VCARD_ORG:
assertTrue(detailId != null);
if (detailId.longValue() == orgId1) {
doneOrg1 = true;
assertEquals(ADD_ORG_COMPANY_TEST1, detail.nativeVal1);
assertEquals(String.valueOf(ADD_ORG_TYPE_TEST1), detail.nativeVal3);
VCardHelper.Organisation org = detail.getOrg();
assertTrue(org != null);
assertEquals(0, org.unitNames.size());
assertEquals(ADD_ORG_COMPANY_TEST1, org.name);
assertEquals(ADD_ORG_PEOPLE_TYPE_TEST1, detail.keyType);
} else {
fail("Unknown organisation in people contact");
}
break;
case VCARD_TITLE:
assertTrue(detailId != null);
if (detailId.longValue() == orgId1) {
doneTitle1 = true;
assertEquals(ADD_ORG_TITLE_TEST1, detail.nativeVal1);
assertEquals(ADD_ORG_TITLE_TEST1, detail.getValue());
} else {
fail("Unknown title in people contact");
}
break;
default:
fail("Unexpected detail in people contact: " + detail.key);
}
}
assertTrue("Name was missing", doneName);
assertTrue("Nickname was missing", doneNickname);
assertTrue("Note was missing", doneNote);
assertTrue("Phone1 was missing", donePhone1);
assertTrue("Phone2 was missing", donePhone2);
assertTrue("Phone3 was missing", donePhone3);
assertTrue("Email was missing", doneCm1);
assertTrue("Address was missing", doneCm2);
assertTrue("Organisation was missing", doneOrg1);
assertTrue("Title was missing", doneTitle1);
nativeCursor.close();
peopleCursor.close();
Log.i(LOG_TAG, "*************************************************************************");
Log.i(LOG_TAG, fnName + " has completed successfully");
Log.i(LOG_TAG, "*************************************************************************");
Log.i(LOG_TAG, "");
}
Aggregations