Search in sources :

Example 36 with ContactChange

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

the class NativeContactsApi1 method putContactMethodValues.

/**
     * Put Contact Methods detail into the values
     * 
     * @param cc {@link ContactChange} to read values from
     * @param kind The kind of contact method (email or address)
     * @param nabContactId ID of the NAB Contact
     */
private void putContactMethodValues(ContactChange cc, int kind, long nabContactId) {
    mValues.put(ContactMethods.PERSON_ID, nabContactId);
    if (kind == CONTACT_METHODS_KIND_EMAIL) {
        mValues.put(ContactMethods.DATA, cc.getValue());
    } else {
        // Must be Address, once again need to use VCardHelper to extract
        // address
        PostalAddress address = VCardHelper.getPostalAddress(cc.getValue());
        mValues.put(ContactMethods.DATA, address.toString());
    }
    mValues.put(ContactMethods.KIND, kind);
    int flags = cc.getFlags();
    mValues.put(ContactMethods.TYPE, mapToNabContactMethodType(flags));
    mValues.put(ContactMethods.ISPRIMARY, flags & ContactChange.FLAG_PREFERRED);
}
Also used : PostalAddress(com.vodafone360.people.datatypes.VCardHelper.PostalAddress)

Example 37 with ContactChange

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

the class NativeContactsApi1 method writeOrganization.

/**
     * Write Organization detail to NAB
     * 
     * @param ccList {@link ContactChange} list where Organization and Title are
     *            found
     * @param nabContactId ID of the NAB Contact
     * @return ID of the NAB Contact
     */
private long writeOrganization(ContactChange[] ccList, long nabContactId) {
    mValues.clear();
    int flags = ContactChange.FLAG_NONE;
    if (mMarkedOrganizationIndex >= 0) {
        final ContactChange cc = ccList[mMarkedOrganizationIndex];
        final Organisation organization = VCardHelper.getOrg(cc.getValue());
        if (!TextUtils.isEmpty(organization.name)) {
            mValues.put(Organizations.COMPANY, organization.name);
            flags |= cc.getFlags();
        }
    }
    if (mMarkedTitleIndex >= 0) {
        final ContactChange cc = ccList[mMarkedTitleIndex];
        // No need to check for empty values as there is only one
        flags |= cc.getFlags();
        mValues.put(Organizations.TITLE, cc.getValue());
    }
    if (mValues.size() > 0) {
        mValues.put(Organizations.PERSON_ID, nabContactId);
        mValues.put(Organizations.TYPE, mapToNabOrganizationType(flags));
        final Uri uri = mCr.insert(Organizations.CONTENT_URI, mValues);
        if (uri != null) {
            return ContentUris.parseId(uri);
        }
    }
    return ContactChange.INVALID_ID;
}
Also used : Organisation(com.vodafone360.people.datatypes.VCardHelper.Organisation) Uri(android.net.Uri)

Example 38 with ContactChange

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

the class NativeContactsApi1 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
     * @return In the Organization insertion case this should contain the new ID
     *         and in the update case should contain the existing ID
     */
private long updateOrganization(ContactChange[] ccList, long nabContactId) {
    if (mMarkedOrganizationIndex < 0 && mMarkedTitleIndex < 0) {
        // no organization or title to update - do nothing
        return ContactChange.INVALID_ID;
    }
    long detailId = ContactChange.INVALID_ID;
    int flags = ContactChange.FLAG_NONE;
    String company = null;
    String title = null;
    final Uri organizationUri = Uri.withAppendedPath(ContentUris.withAppendedId(People.CONTENT_URI, nabContactId), Contacts.Organizations.CONTENT_DIRECTORY);
    final Cursor cursor = mCr.query(organizationUri, null, null, null, Organizations._ID);
    // assuming that the lowest id is the one in CAB
    try {
        if (cursor != null && cursor.moveToNext()) {
            company = CursorUtils.getString(cursor, Organizations.COMPANY);
            title = CursorUtils.getString(cursor, Organizations.TITLE);
            detailId = CursorUtils.getLong(cursor, Organizations._ID);
            flags = mapFromNabOrganizationType(CursorUtils.getInt(cursor, Organizations.TYPE));
            final boolean isPrimary = CursorUtils.getInt(cursor, Organizations.ISPRIMARY) > 0;
            if (isPrimary) {
                flags |= ContactChange.FLAG_PREFERRED;
            }
        }
    } finally {
        CursorUtils.closeCursor(cursor);
    }
    if (mMarkedOrganizationIndex >= 0) {
        final ContactChange cc = ccList[mMarkedOrganizationIndex];
        if (cc.getType() != ContactChange.TYPE_DELETE_DETAIL) {
            final String value = cc.getValue();
            if (value != null) {
                final VCardHelper.Organisation organization = VCardHelper.getOrg(value);
                if (!TextUtils.isEmpty(organization.name)) {
                    company = organization.name;
                }
            }
            flags = cc.getFlags();
        } else {
            // Delete case
            company = null;
        }
    }
    if (mMarkedTitleIndex >= 0) {
        final ContactChange cc = ccList[mMarkedTitleIndex];
        title = cc.getValue();
        if (cc.getType() != ContactChange.TYPE_DELETE_DETAIL) {
            flags = cc.getFlags();
        }
    }
    if (company != null || title != null) {
        mValues.clear();
        /*
             * Forcing the label field to be null because we don't support
             * custom labels and in case we replace from a custom label to home
             * or private type without setting label to null, mCr.update() will
             * throw a SQL constraint exception.
             */
        mValues.put(Organizations.LABEL, (String) null);
        mValues.put(Organizations.COMPANY, company);
        mValues.put(Organizations.TITLE, title);
        mValues.put(Organizations.TYPE, mapToNabOrganizationType(flags));
        mValues.put(Organizations.ISPRIMARY, flags & ContactChange.FLAG_PREFERRED);
        final Uri existingUri = ContentUris.withAppendedId(Contacts.Organizations.CONTENT_URI, detailId);
        if (detailId != ContactChange.INVALID_ID) {
            mCr.update(existingUri, mValues, null, null);
        } else {
            // insert
            final Uri idUri = mCr.insert(organizationUri, mValues);
            if (idUri != null) {
                return ContentUris.parseId(idUri);
            }
        }
    } else if (detailId != ContactChange.INVALID_ID) {
        final Uri existingUri = ContentUris.withAppendedId(Contacts.Organizations.CONTENT_URI, detailId);
        mCr.delete(existingUri, null, null);
    } else {
        mMarkedOrganizationIndex = mMarkedTitleIndex = -1;
    }
    // Updated detail id or ContactChange.INVALID_ID if deleted
    return detailId;
}
Also used : Organisation(com.vodafone360.people.datatypes.VCardHelper.Organisation) VCardHelper(com.vodafone360.people.datatypes.VCardHelper) Cursor(android.database.Cursor) Uri(android.net.Uri)

Example 39 with ContactChange

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

the class NativeContactsApi2 method readAddress.

/**
     * Reads an address detail as a {@link ContactChange} from the provided
     * cursor. For this type of detail we need to use a VCARD (semicolon
     * separated) value.
     * 
     * @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 readAddress(Cursor cursor, List<ContactChange> ccList, long nabContactId) {
    // Using formatted address only to check if there is a valid address to
    // read
    final String formattedAddress = CursorUtils.getString(cursor, StructuredPostal.FORMATTED_ADDRESS);
    if (!TextUtils.isEmpty(formattedAddress)) {
        final long nabDetailId = CursorUtils.getLong(cursor, StructuredPostal._ID);
        final int type = CursorUtils.getInt(cursor, StructuredPostal.TYPE);
        int flags = mapFromNabAddressType(type);
        final boolean isPrimary = CursorUtils.getInt(cursor, StructuredPostal.IS_PRIMARY) != 0;
        if (isPrimary) {
            flags |= ContactChange.FLAG_PREFERRED;
        }
        // VCard Helper data type (CAB)
        final PostalAddress address = new PostalAddress();
        // NAB: Street -> CAB: AddressLine1
        address.addressLine1 = CursorUtils.getString(cursor, StructuredPostal.STREET);
        // NAB: PO Box -> CAB: postOfficeBox
        address.postOfficeBox = CursorUtils.getString(cursor, StructuredPostal.POBOX);
        // NAB: Neighborhood -> CAB: AddressLine2
        address.addressLine2 = CursorUtils.getString(cursor, StructuredPostal.NEIGHBORHOOD);
        // NAB: City -> CAB: City
        address.city = CursorUtils.getString(cursor, StructuredPostal.CITY);
        // NAB: Region -> CAB: County
        address.county = CursorUtils.getString(cursor, StructuredPostal.REGION);
        // NAB: Post code -> CAB: Post code
        address.postCode = CursorUtils.getString(cursor, StructuredPostal.POSTCODE);
        // NAB: Country -> CAB: Country
        address.country = CursorUtils.getString(cursor, StructuredPostal.COUNTRY);
        final ContactChange cc = new ContactChange(ContactChange.KEY_VCARD_ADDRESS, VCardHelper.makePostalAddress(address), flags);
        cc.setNabContactId(nabContactId);
        cc.setNabDetailId(nabDetailId);
        ccList.add(cc);
    }
}
Also used : PostalAddress(com.vodafone360.people.datatypes.VCardHelper.PostalAddress)

Example 40 with ContactChange

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

the class PeopleContactsApi method getContact.

/**
     * Gets a contact from its native id.
     * 
     * @param nativeId the native id of the contact
     * @return an array of ContactChange representing the contact, null if not found
     */
public ContactChange[] getContact(long nativeId) {
    try {
        final SQLiteDatabase readableDb = mDbh.getReadableDatabase();
        if (NativeChangeLogTable.isContactChangeInList(nativeId, ContactChangeType.DELETE_CONTACT, readableDb)) {
            // the contact exists as a deleted contact in the NativeChangeLogTable
            // return one ContactChange with the deleted contact flag
            ContactChange[] changes = new ContactChange[1];
            changes[0] = new ContactChange(ContactChange.TYPE_DELETE_CONTACT);
            return changes;
        } else {
            // get the corresponding local contact id
            final ContactIdInfo info = ContactsTable.fetchContactIdFromNative((int) nativeId, readableDb);
            if (info != null) {
                // we found the contact on CAB, let's get the details
                final ContactChange[] existingDetails = ContactDetailsTable.getContactChanges((long) info.localId, false, readableDb);
                // get also the deleted details if any
                final ContactChange[] deletedDetails = NativeChangeLogTable.getDeletedDetails((long) info.localId, readableDb);
                if (existingDetails != null && deletedDetails != null) {
                    // merge both arrays
                    ContactChange[] mergedDetails = new ContactChange[existingDetails.length + deletedDetails.length];
                    System.arraycopy(existingDetails, 0, mergedDetails, 0, existingDetails.length);
                    System.arraycopy(deletedDetails, 0, mergedDetails, existingDetails.length, deletedDetails.length);
                    return mergedDetails;
                } else if (existingDetails != null) {
                    return existingDetails;
                } else {
                    return deletedDetails;
                }
            }
        }
    } catch (Exception e) {
        LogUtils.logE("getContact(" + nativeId + "), error: " + e);
    }
    // no contact found
    return null;
}
Also used : ContactIdInfo(com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

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