Search in sources :

Example 6 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class NativeContactsApiTest method testUpdateContacts.

@MediumTest
@Suppress
public void testUpdateContacts() {
    Account account = null;
    if (mUsing2xApi) {
        // Add Account for the case where we are in 2.X
        mNabApi.addPeopleAccount(PEOPLE_USERNAME);
        account = s360PeopleAccount;
        threadWait(100);
    }
    long[] ids = getContactIdsForAllAccounts();
    assertNull(ids);
    final int numRandomContacts = 10;
    for (int i = 0; i < numRandomContacts; i++) {
        long id = i;
        final ContactChange[] newContactCcList = ContactChangeHelper.randomContact(id, id, -1);
        mNabApi.addContact(account, newContactCcList);
        //expectedContactId = newIds[0].getNabContactId() + 1;
        // GET CONTACT
        ids = getContactIdsForAllAccounts();
        assertNotNull(ids);
        assertEquals(i + 1, ids.length);
        final ContactChange[] fetchedContactCcList = mNabApi.getContact(ids[i]);
        assertNotNull(fetchedContactCcList);
        // UPDATE
        final ContactChange[] updateCcList = ContactChangeHelper.randomContactUpdate(fetchedContactCcList);
        assertNotNull(updateCcList);
        assertTrue(updateCcList.length > 0);
        final ContactChange[] updatedIdsCcList = mNabApi.updateContact(updateCcList);
        verifyUpdateContactIds(fetchedContactCcList, updateCcList, updatedIdsCcList);
        final ContactChange[] updatedContactCcList = ContactChangeHelper.generatedUpdatedContact(newContactCcList, updateCcList);
        ids = getContactIdsForAllAccounts();
        assertNotNull(ids);
        assertEquals(i + 1, ids.length);
        final ContactChange[] fetchedUpdatedContactCcList = mNabApi.getContact(ids[i]);
        assertNotNull(fetchedUpdatedContactCcList);
        if (!ContactChangeHelper.areUnsortedChangeListsEqual(updatedContactCcList, fetchedUpdatedContactCcList, false)) {
            // Print update 
            Log.e(LOG_TAG, "UPDATE FAILED: Print of initial contact follows");
            ContactChangeHelper.printContactChangeList(fetchedContactCcList);
            Log.e(LOG_TAG, "UPDATE FAILED: Print of failed update follows:");
            ContactChangeHelper.printContactChangeList(updateCcList);
            // fail test at this point
            assertFalse(true);
        }
    }
    // DELETE
    final int idCount = ids.length;
    for (int i = 0; i < idCount; i++) {
        mNabApi.removeContact(ids[i]);
    }
    ids = getContactIdsForAllAccounts();
    assertNull(ids);
}
Also used : Account(com.vodafone360.people.engine.contactsync.NativeContactsApi.Account) ContactChange(com.vodafone360.people.engine.contactsync.ContactChange) Suppress(android.test.suitebuilder.annotation.Suppress) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 7 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class NativeContactsApi2 method updateOrganization.

/**
     * Updates the Organization detail in the context of a Contact Update
     * operation. The end of result of this is that the Organization may be
     * inserted, updated or deleted depending on the update data. For example,
     * if the title is deleted but there is also a company name then the
     * Organization is just updated. However, if there was no company name then
     * the detail should be deleted altogether.
     * 
     * @param ccList {@link ContactChange} list where Organization and Title may
     *            be found
     * @param nabContactId The NAB ID of the Contact
     */
private void updateOrganization(ContactChange[] ccList, long nabContactId) {
    if (mMarkedOrganizationIndex < 0 && mMarkedTitleIndex < 0) {
        // no organization or title to update - do nothing
        return;
    }
    // First we check if there is an existing Organization detail in NAB
    final Uri uri = Uri.withAppendedPath(ContentUris.withAppendedId(RawContacts.CONTENT_URI, nabContactId), RawContacts.Data.CONTENT_DIRECTORY);
    Cursor cursor = mCr.query(uri, null, ORGANIZATION_DETAIL_WHERE_CLAUSE, null, RawContacts.Data._ID);
    String company = null;
    String department = null;
    String title = null;
    int flags = ContactChange.FLAG_NONE;
    try {
        if (cursor != null && cursor.moveToNext()) {
            // Found an organization detail
            company = CursorUtils.getString(cursor, Organization.COMPANY);
            department = CursorUtils.getString(cursor, Organization.DEPARTMENT);
            title = CursorUtils.getString(cursor, Organization.TITLE);
            flags = mapFromNabOrganizationType(CursorUtils.getInt(cursor, Organization.TYPE));
            final boolean isPrimary = CursorUtils.getInt(cursor, Organization.IS_PRIMARY) > 0;
            if (isPrimary) {
                flags |= ContactChange.FLAG_PREFERRED;
            }
            mExistingOrganizationId = CursorUtils.getLong(cursor, Organization._ID);
        }
    } finally {
        CursorUtils.closeCursor(cursor);
        // make it a candidate for the GC
        cursor = null;
    }
    if (mMarkedOrganizationIndex >= 0) {
        // Have an Organization (Company + Department) to update
        final ContactChange cc = ccList[mMarkedOrganizationIndex];
        if (cc.getType() != ContactChange.TYPE_DELETE_DETAIL) {
            final String value = cc.getValue();
            if (value != null) {
                final Organisation organization = VCardHelper.getOrg(value);
                company = organization.name;
                if (organization.unitNames.size() > 0) {
                    department = organization.unitNames.get(0);
                }
            }
            flags = cc.getFlags();
        } else {
            // Delete case
            company = null;
            department = null;
        }
    }
    if (mMarkedTitleIndex >= 0) {
        // Have a Title to update
        final ContactChange cc = ccList[mMarkedTitleIndex];
        title = cc.getValue();
        if (cc.getType() != ContactChange.TYPE_UPDATE_DETAIL) {
            flags = cc.getFlags();
        }
    }
    if (company != null || department != null || title != null) {
        /*
             * If any of the above are present we assume a insert or update is
             * needed.
             */
        mValues.clear();
        mValues.put(Organization.LABEL, (String) null);
        mValues.put(Organization.COMPANY, company);
        mValues.put(Organization.DEPARTMENT, department);
        mValues.put(Organization.TITLE, title);
        mValues.put(Organization.TYPE, mapToNabOrganizationType(flags));
        mValues.put(Organization.IS_PRIMARY, flags & ContactChange.FLAG_PREFERRED);
        mValues.put(Organization.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
        if (mExistingOrganizationId != ContactChange.INVALID_ID) {
            // update is needed
            addUpdateValuesToBatch(mExistingOrganizationId);
        } else {
            // insert is needed
            // not a new contact
            addValuesToBatch(nabContactId);
        }
    } else if (mExistingOrganizationId != ContactChange.INVALID_ID) {
        /*
             * Had an Organization but now all values are null, delete is in
             * order.
             */
        addDeleteDetailToBatch(mExistingOrganizationId);
    }
}
Also used : Organisation(com.vodafone360.people.datatypes.VCardHelper.Organisation) Cursor(android.database.Cursor) Uri(android.net.Uri)

Example 8 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class NativeContactsApi2 method putOrganization.

// PHOTO NOT USED
// /**
// * Do a GET request and retrieve up to maxBytes bytes
// *
// * @param url
// * @param maxBytes
// * @return
// * @throws IOException
// */
// public static byte[] doGetAndReturnBytes(URL url, int maxBytes) throws
// IOException {
// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// conn.setRequestMethod("GET");
// InputStream istr = null;
// try {
// int rc = conn.getResponseCode();
// if (rc != 200) {
// throw new IOException("code " + rc + " '" + conn.getResponseMessage() +
// "'");
// }
// istr = new BufferedInputStream(conn.getInputStream(), 512);
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// copy(istr, baos, maxBytes);
// return baos.toByteArray();
// } finally {
// if (istr != null) {
// istr.close();
// }
// }
// }
//    
// /**
// * Copy maxBytes from an input stream to an output stream.
// * @param in
// * @param out
// * @param maxBytes
// * @return
// * @throws IOException
// */
// private static int copy(InputStream in, OutputStream out, int maxBytes)
// throws IOException {
// byte[] buf = new byte[512];
// int bytesRead = 1;
// int totalBytes = 0;
// while (bytesRead > 0) {
// bytesRead = in.read(buf, 0, Math.min(512, maxBytes - totalBytes));
// if (bytesRead > 0) {
// out.write(buf, 0, bytesRead);
// totalBytes += bytesRead;
// }
// }
// return totalBytes;
// }
//  
// /**
// * Put Photo detail into the values
// * @param cc {@link ContactChange} to read values from
// */
// private void putPhoto(ContactChange cc) {
// try {
// // File file = new File(cc.getValue());
// // InputStream is = new FileInputStream(file);
// // byte[] bytes = new byte[(int) file.length()];
// // is.read(bytes);
// // is.close();
// final URL url = new URL(cc.getValue());
// byte[] bytes = doGetAndReturnBytes(url, 1024 * 100);
// mValues.put(Photo.PHOTO, bytes);
// mValues.put(Photo.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
// } catch(Exception ex) {
// LogUtils.logE("Unable to put Photo detail because of exception:"+ex);
// }
// }
/**
     * Put Organization detail into the values
     * 
     * @param cc {@link ContactChange} to read values from
     */
private void putOrganization(ContactChange[] ccList) {
    mValues.clear();
    int flags = ContactChange.FLAG_NONE;
    if (mMarkedOrganizationIndex > -1) {
        final ContactChange cc = ccList[mMarkedOrganizationIndex];
        flags |= cc.getFlags();
        final Organisation organization = VCardHelper.getOrg(cc.getValue());
        if (organization != null) {
            mValues.put(Organization.COMPANY, organization.name);
            if (organization.unitNames.size() > 0) {
                // Only considering one unit name (department) as that's all
                // we support
                mValues.put(Organization.DEPARTMENT, organization.unitNames.get(0));
            } else {
                mValues.putNull(Organization.DEPARTMENT);
            }
        }
    }
    if (mMarkedTitleIndex > -1) {
        final ContactChange cc = ccList[mMarkedTitleIndex];
        flags |= cc.getFlags();
        // No need to check for empty values as there is only one
        mValues.put(Organization.TITLE, cc.getValue());
    }
    if (mValues.size() > 0) {
        mValues.put(Organization.LABEL, (String) null);
        mValues.put(Organization.TYPE, mapToNabOrganizationType(flags));
        mValues.put(Organization.IS_PRIMARY, flags & ContactChange.FLAG_PREFERRED);
        mValues.put(Organization.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
    }
}
Also used : Organisation(com.vodafone360.people.datatypes.VCardHelper.Organisation)

Example 9 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class NativeContactsApi2 method readOrganization.

/**
     * Reads an organization detail as a {@link ContactChange} from the provided
     * cursor. For this type of detail we need to use a VCARD (semicolon
     * separated) value. In reality two different changes may be read if a title
     * is also present.
     * 
     * @param cursor Cursor to read from
     * @param ccList List of Contact Changes to add read detail data
     * @param nabContactId ID of the NAB Contact
     */
private void readOrganization(Cursor cursor, List<ContactChange> ccList, long nabContactId) {
    final int type = CursorUtils.getInt(cursor, Organization.TYPE);
    int flags = mapFromNabOrganizationType(type);
    final boolean isPrimary = CursorUtils.getInt(cursor, Organization.IS_PRIMARY) != 0;
    if (isPrimary) {
        flags |= ContactChange.FLAG_PREFERRED;
    }
    final long nabDetailId = CursorUtils.getLong(cursor, Organization._ID);
    if (!mHaveReadOrganization) {
        // VCard Helper data type (CAB)
        final Organisation organization = new Organisation();
        // Company
        organization.name = CursorUtils.getString(cursor, Organization.COMPANY);
        // Department
        final String department = CursorUtils.getString(cursor, Organization.DEPARTMENT);
        if (!TextUtils.isEmpty(department)) {
            organization.unitNames.add(department);
        }
        if ((organization.unitNames != null && organization.unitNames.size() > 0) || !TextUtils.isEmpty(organization.name)) {
            final ContactChange cc = new ContactChange(ContactChange.KEY_VCARD_ORG, VCardHelper.makeOrg(organization), flags);
            cc.setNabContactId(nabContactId);
            cc.setNabDetailId(nabDetailId);
            ccList.add(cc);
            mHaveReadOrganization = true;
        }
        // Title
        final String title = CursorUtils.getString(cursor, Organization.TITLE);
        if (!TextUtils.isEmpty(title)) {
            final ContactChange cc = new ContactChange(ContactChange.KEY_VCARD_TITLE, title, flags);
            cc.setNabContactId(nabContactId);
            cc.setNabDetailId(nabDetailId);
            ccList.add(cc);
            mHaveReadOrganization = true;
        }
    }
}
Also used : Organisation(com.vodafone360.people.datatypes.VCardHelper.Organisation)

Example 10 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class NativeContactsApi2 method putAddress.

/**
     * Put Address detail into the values
     * 
     * @param cc {@link ContactChange} to read values from
     */
private void putAddress(ContactChange cc) {
    final PostalAddress address = VCardHelper.getPostalAddress(cc.getValue());
    if (address == null) {
        // Nothing to do
        return;
    }
    mValues.put(StructuredPostal.STREET, address.addressLine1);
    mValues.put(StructuredPostal.POBOX, address.postOfficeBox);
    mValues.put(StructuredPostal.NEIGHBORHOOD, address.addressLine2);
    mValues.put(StructuredPostal.CITY, address.city);
    mValues.put(StructuredPostal.REGION, address.county);
    mValues.put(StructuredPostal.POSTCODE, address.postCode);
    mValues.put(StructuredPostal.COUNTRY, address.country);
    final int flags = cc.getFlags();
    mValues.put(StructuredPostal.TYPE, mapToNabAddressType(flags));
    mValues.put(StructuredPostal.IS_PRIMARY, flags & ContactChange.FLAG_PREFERRED);
    mValues.put(StructuredPostal.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
}
Also used : PostalAddress(com.vodafone360.people.datatypes.VCardHelper.PostalAddress)

Aggregations

ContactChange (com.vodafone360.people.engine.contactsync.ContactChange)33 Name (com.vodafone360.people.datatypes.VCardHelper.Name)6 Organisation (com.vodafone360.people.datatypes.VCardHelper.Organisation)6 PeopleContactsApi (com.vodafone360.people.engine.contactsync.PeopleContactsApi)6 Cursor (android.database.Cursor)5 Suppress (android.test.suitebuilder.annotation.Suppress)5 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)5 ArrayList (java.util.ArrayList)5 SQLException (android.database.SQLException)4 SQLiteException (android.database.sqlite.SQLiteException)4 Uri (android.net.Uri)4 PostalAddress (com.vodafone360.people.datatypes.VCardHelper.PostalAddress)4 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 Contact (com.vodafone360.people.datatypes.Contact)3 ContentValues (android.content.ContentValues)2 StructuredName (android.provider.ContactsContract.CommonDataKinds.StructuredName)2 MediumTest (android.test.suitebuilder.annotation.MediumTest)2 SmallTest (android.test.suitebuilder.annotation.SmallTest)2 ContactIdInfo (com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo)2 NativeContactsApi (com.vodafone360.people.engine.contactsync.NativeContactsApi)2