Search in sources :

Example 1 with VCard

use of ezvcard.VCard in project qksms by moezbhatti.

the class MessageListFragment method openVcard.

private void openVcard(MessageItem messageItem) {
    Log.d(TAG, "Vcard: " + messageItem.mBody);
    VCard vCard = Ezvcard.parse(messageItem.mBody).first();
    ContactOperations operations = new ContactOperations(mContext);
    try {
        operations.insertContact(vCard);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ContactOperations(com.moez.QKSMS.common.vcard.ContactOperations) VCard(ezvcard.VCard) SQLiteException(android.database.sqlite.SQLiteException)

Example 2 with VCard

use of ezvcard.VCard in project android by nextcloud.

the class ContactsImportJob method onRunJob.

@NonNull
@Override
protected Result onRunJob(@NonNull Params params) {
    PersistableBundleCompat bundle = params.getExtras();
    String vCardFilePath = bundle.getString(VCARD_FILE_PATH, "");
    String accountName = bundle.getString(ACCOUNT_NAME, "");
    String accountType = bundle.getString(ACCOUNT_TYPE, "");
    int[] intArray = bundle.getIntArray(CHECKED_ITEMS_ARRAY);
    File file = new File(vCardFilePath);
    ArrayList<VCard> vCards = new ArrayList<>();
    Cursor cursor = null;
    try {
        ContactOperations operations = new ContactOperations(getContext(), accountName, accountType);
        vCards.addAll(Ezvcard.parse(file).all());
        Collections.sort(vCards, new ContactListFragment.VCardComparator());
        cursor = getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        TreeMap<VCard, Long> ownContactList = new TreeMap<>(new ContactListFragment.VCardComparator());
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            for (int i = 0; i < cursor.getCount(); i++) {
                VCard vCard = getContactFromCursor(cursor);
                if (vCard != null) {
                    ownContactList.put(vCard, cursor.getLong(cursor.getColumnIndex("NAME_RAW_CONTACT_ID")));
                }
                cursor.moveToNext();
            }
        }
        for (int i = 0; i < intArray.length; i++) {
            VCard vCard = vCards.get(intArray[i]);
            if (ContactListFragment.getDisplayName(vCard).length() != 0) {
                if (!ownContactList.containsKey(vCard)) {
                    operations.insertContact(vCard);
                } else {
                    operations.updateContact(vCard, ownContactList.get(vCard));
                }
            } else {
                // Insert All the contacts without name
                operations.insertContact(vCard);
            }
        }
    } catch (Exception e) {
        Log_OC.e(TAG, e.getMessage());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return Result.SUCCESS;
}
Also used : PersistableBundleCompat(com.evernote.android.job.util.support.PersistableBundleCompat) ArrayList(java.util.ArrayList) ContactOperations(third_parties.ezvcard_android.ContactOperations) Cursor(android.database.Cursor) TreeMap(java.util.TreeMap) IOException(java.io.IOException) ContactListFragment(com.owncloud.android.ui.fragment.contactsbackup.ContactListFragment) File(java.io.File) VCard(ezvcard.VCard) NonNull(android.support.annotation.NonNull)

Example 3 with VCard

use of ezvcard.VCard in project android by nextcloud.

the class ContactsImportJob method getContactFromCursor.

private VCard getContactFromCursor(Cursor cursor) {
    String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
    VCard vCard = null;
    try {
        InputStream inputStream = getContext().getContentResolver().openInputStream(uri);
        ArrayList<VCard> vCardList = new ArrayList<>();
        vCardList.addAll(Ezvcard.parse(inputStream).all());
        if (vCardList.size() > 0) {
            vCard = vCardList.get(0);
        }
    } catch (IOException e) {
        Log_OC.d(TAG, e.getMessage());
    }
    return vCard;
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Uri(android.net.Uri) VCard(ezvcard.VCard)

Example 4 with VCard

use of ezvcard.VCard in project data-transfer-project by google.

the class GoogleContactsExporter method exportContacts.

private ExportResult<ContactsModelWrapper> exportContacts(TokensAndUrlAuthData authData, Optional<PaginationData> pageData) {
    try {
        // Set up connection
        Connections.List connectionsListRequest = getOrCreatePeopleService(authData).people().connections().list(SELF_RESOURCE);
        // Get next page, if we have a page token
        if (pageData.isPresent()) {
            StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
            connectionsListRequest.setPageToken(paginationToken.getToken());
        }
        // Get list of connections (nb: not a list containing full info of each Person)
        ListConnectionsResponse response = connectionsListRequest.setPersonFields(PERSON_FIELDS).execute();
        List<Person> peopleList = response.getConnections();
        // Get list of resource names, then get list of Persons
        List<String> resourceNames = peopleList.stream().map(Person::getResourceName).collect(Collectors.toList());
        GetPeopleResponse batchResponse = getOrCreatePeopleService(authData).people().getBatchGet().setResourceNames(resourceNames).setPersonFields(PERSON_FIELDS).execute();
        List<PersonResponse> personResponseList = batchResponse.getResponses();
        // Convert Persons to VCards
        List<VCard> vCards = personResponseList.stream().map(a -> convert(a.getPerson())).collect(Collectors.toList());
        // Determine if there's a next page
        StringPaginationToken nextPageData = null;
        if (response.getNextPageToken() != null) {
            nextPageData = new StringPaginationToken(response.getNextPageToken());
        }
        ContinuationData continuationData = new ContinuationData(nextPageData);
        ContactsModelWrapper wrapper = new ContactsModelWrapper(makeVCardString(vCards));
        // Get result type
        ResultType resultType = ResultType.CONTINUE;
        if (nextPageData == null) {
            resultType = ResultType.END;
        }
        return new ExportResult<ContactsModelWrapper>(resultType, wrapper, continuationData);
    } catch (IOException e) {
        return new ExportResult<ContactsModelWrapper>(ResultType.ERROR, e.getMessage());
    }
}
Also used : Connections(com.google.api.services.people.v1.PeopleService.People.Connections) EmailAddress(com.google.api.services.people.v1.model.EmailAddress) Connections(com.google.api.services.people.v1.PeopleService.People.Connections) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult) LoggerFactory(org.slf4j.LoggerFactory) PhoneNumber(com.google.api.services.people.v1.model.PhoneNumber) StructuredName(ezvcard.property.StructuredName) TokensAndUrlAuthData(org.dataportabilityproject.types.transfer.auth.TokensAndUrlAuthData) Person(com.google.api.services.people.v1.model.Person) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) Credential(com.google.api.client.auth.oauth2.Credential) VCARD_PRIMARY_PREF(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.VCARD_PRIMARY_PREF) LinkedList(java.util.LinkedList) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) SELF_RESOURCE(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.SELF_RESOURCE) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) VCard(ezvcard.VCard) PeopleService(com.google.api.services.people.v1.PeopleService) ExportInformation(org.dataportabilityproject.spi.transfer.types.ExportInformation) Exporter(org.dataportabilityproject.spi.transfer.provider.Exporter) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) Logger(org.slf4j.Logger) BearerToken(com.google.api.client.auth.oauth2.BearerToken) StringWriter(java.io.StringWriter) Telephone(ezvcard.property.Telephone) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) IOException(java.io.IOException) UUID(java.util.UUID) JCardWriter(ezvcard.io.json.JCardWriter) Collectors(java.util.stream.Collectors) SOURCE_PARAM_NAME_TYPE(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.SOURCE_PARAM_NAME_TYPE) List(java.util.List) PERSON_FIELDS(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.PERSON_FIELDS) FieldMetadata(com.google.api.services.people.v1.model.FieldMetadata) PersonResponse(com.google.api.services.people.v1.model.PersonResponse) Email(ezvcard.property.Email) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Name(com.google.api.services.people.v1.model.Name) GoogleStaticObjects(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) IOException(java.io.IOException) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) Person(com.google.api.services.people.v1.model.Person) VCard(ezvcard.VCard) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) PersonResponse(com.google.api.services.people.v1.model.PersonResponse) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Example 5 with VCard

use of ezvcard.VCard in project data-transfer-project by google.

the class GoogleContactsExporter method makeVCardString.

@VisibleForTesting
static String makeVCardString(List<VCard> vCardList) throws IOException {
    StringWriter stringWriter = new StringWriter();
    JCardWriter jCardWriter = new JCardWriter(stringWriter);
    for (VCard vCardProperties : vCardList) {
        // needs to be loop so error can be thrown
        jCardWriter.write(vCardProperties);
    }
    jCardWriter.flush();
    return stringWriter.toString();
}
Also used : StringWriter(java.io.StringWriter) JCardWriter(ezvcard.io.json.JCardWriter) VCard(ezvcard.VCard) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VCard (ezvcard.VCard)192 Test (org.junit.Test)134 StringWriter (java.io.StringWriter)29 StructuredName (ezvcard.property.StructuredName)23 Person (com.google.api.services.people.v1.model.Person)22 Telephone (ezvcard.property.Telephone)21 Photo (ezvcard.property.Photo)17 Email (ezvcard.property.Email)16 List (java.util.List)16 EmailAddress (com.google.api.services.people.v1.model.EmailAddress)15 Document (org.w3c.dom.Document)15 VCardVersion (ezvcard.VCardVersion)14 XCardDocumentStreamWriter (ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter)14 Address (ezvcard.property.Address)14 PhoneNumber (com.google.api.services.people.v1.model.PhoneNumber)13 Collectors (java.util.stream.Collectors)12 Name (com.google.api.services.people.v1.model.Name)11 File (java.io.File)11 Truth.assertThat (com.google.common.truth.Truth.assertThat)10 Pair (com.google.gdata.util.common.base.Pair)10