Search in sources :

Example 1 with Address

use of com.codename1.contacts.Address in project CodenameOne by codenameone.

the class BlackBerryImplementation method sendMessage.

public void sendMessage(String[] recipients, String subject, Message msg) {
    Folder[] folders = Session.getDefaultInstance().getStore().list(Folder.SENT);
    net.rim.blackberry.api.mail.Message message = new net.rim.blackberry.api.mail.Message(folders[0]);
    try {
        Address[] toAdds = new Address[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            Address address = new Address(recipients[i], "");
            toAdds[i] = address;
        }
        message.addRecipients(net.rim.blackberry.api.mail.Message.RecipientType.TO, toAdds);
        message.setSubject(subject);
    } catch (Exception e) {
        EventLog.getInstance().logErrorEvent("err " + e.getMessage());
    }
    try {
        if (msg.getAttachment() != null && msg.getAttachment().length() > 0) {
            Multipart content = new Multipart();
            TextBodyPart tbp = new TextBodyPart(content, msg.getContent());
            content.addBodyPart(tbp);
            InputStream stream = com.codename1.io.FileSystemStorage.getInstance().openInputStream(msg.getAttachment());
            byte[] buf;
            buf = IOUtilities.streamToBytes(stream);
            stream.close();
            String name = msg.getAttachment();
            name = name.substring(name.lastIndexOf(getFileSystemSeparator()) + 1, name.length());
            SupportedAttachmentPart sap = new SupportedAttachmentPart(content, msg.getAttachmentMimeType(), name, buf);
            content.addBodyPart(sap);
            message.setContent(content);
        } else {
            message.setContent(msg.getContent());
        }
        app.setWaitingForReply(true);
        Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments(message));
    } catch (Exception ex) {
        EventLog.getInstance().logErrorEvent("err " + ex.getMessage());
    }
}
Also used : Multipart(net.rim.blackberry.api.mail.Multipart) TextMessage(javax.wireless.messaging.TextMessage) Message(com.codename1.messaging.Message) Address(net.rim.blackberry.api.mail.Address) BufferedInputStream(com.codename1.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) TextBodyPart(net.rim.blackberry.api.mail.TextBodyPart) Folder(net.rim.blackberry.api.mail.Folder) IOException(java.io.IOException) RecordStoreException(javax.microedition.rms.RecordStoreException) MediaException(javax.microedition.media.MediaException) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) MessageArguments(net.rim.blackberry.api.invoke.MessageArguments) SupportedAttachmentPart(net.rim.blackberry.api.mail.SupportedAttachmentPart)

Example 2 with Address

use of com.codename1.contacts.Address in project CodenameOne by codenameone.

the class IOSImplementation method getContactById.

@Override
public Contact getContactById(String id, boolean includesFullName, boolean includesPicture, boolean includesNumbers, boolean includesEmail, boolean includeAddress) {
    if (!nativeInstance.checkContactsUsage()) {
        throw new RuntimeException("Please add the ios.NSContactsUsageDescription build hint");
    }
    int recId = Integer.parseInt(id);
    Contact c = new Contact();
    c.setId(id);
    c.setAddresses(new Hashtable());
    if (includeAddress) {
        // This is a hack to make sure that
        // Address and its methods aren't stripped out by the BytecodeCompiler
        Address tmp = new Address();
    }
    c.setEmails(new Hashtable());
    c.setPhoneNumbers(new Hashtable());
    nativeInstance.updatePersonWithRecordID(recId, c, includesFullName, includesPicture, includesNumbers, includesEmail, includeAddress);
    return c;
}
Also used : Address(com.codename1.contacts.Address) Hashtable(java.util.Hashtable) Contact(com.codename1.contacts.Contact)

Example 3 with Address

use of com.codename1.contacts.Address in project CodenameOne by codenameone.

the class JavaSEPort method initContacts.

private Hashtable initContacts() {
    checkContactsUsageDescription();
    Hashtable retVal = new Hashtable();
    Image img = null;
    try {
        img = Image.createImage(getClass().getResourceAsStream("/com/codename1/impl/javase/user.jpg"));
    } catch (IOException ex) {
    }
    Contact contact = new Contact();
    contact.setId("1");
    contact.setDisplayName("Chen Fishbein");
    contact.setFirstName("Chen");
    contact.setFamilyName("Fishbein");
    contact.setPhoto(img);
    Hashtable phones = new Hashtable();
    phones.put("mobile", "+111111");
    phones.put("home", "+222222");
    contact.setPhoneNumbers(phones);
    Hashtable emails = new Hashtable();
    emails.put("work", "----");
    contact.setEmails(emails);
    Hashtable addresses = new Hashtable();
    Address addr = new Address();
    addr.setCountry("IL");
    addr.setStreetAddress("Sapir 20");
    addresses.put("home", addr);
    contact.setAddresses(addresses);
    retVal.put(contact.getId(), contact);
    contact = new Contact();
    contact.setId("2");
    contact.setDisplayName("Shai Almog");
    contact.setFirstName("Shai");
    contact.setFamilyName("Almog");
    contact.setPhoto(img);
    phones = new Hashtable();
    phones.put("mobile", "+111111");
    phones.put("home", "+222222");
    contact.setPhoneNumbers(phones);
    emails = new Hashtable();
    emails.put("work", "----");
    contact.setEmails(emails);
    addresses = new Hashtable();
    addr = new Address();
    addr.setCountry("IL");
    addr.setStreetAddress("lev 1");
    addresses.put("home", addr);
    contact.setAddresses(addresses);
    retVal.put(contact.getId(), contact);
    contact = new Contact();
    contact.setId("3");
    contact.setDisplayName("Eric Cartman");
    contact.setFirstName("Eric");
    contact.setFamilyName("Cartman");
    phones = new Hashtable();
    phones.put("mobile", "+111111");
    phones.put("home", "+222222");
    contact.setPhoneNumbers(phones);
    emails = new Hashtable();
    emails.put("work", "Eric.Cartman@codenameone.com");
    contact.setEmails(emails);
    addresses = new Hashtable();
    addr = new Address();
    addr.setCountry("US");
    addr.setStreetAddress("South Park");
    addresses.put("home", addr);
    contact.setAddresses(addresses);
    retVal.put(contact.getId(), contact);
    contact = new Contact();
    contact.setId("4");
    contact.setDisplayName("Kyle Broflovski");
    contact.setFirstName("Kyle");
    contact.setFamilyName("Broflovski");
    phones = new Hashtable();
    phones.put("mobile", "+111111");
    phones.put("home", "+222222");
    contact.setPhoneNumbers(phones);
    emails = new Hashtable();
    emails.put("work", "Kyle.Broflovski@codenameone.com");
    contact.setEmails(emails);
    addresses = new Hashtable();
    addr = new Address();
    addr.setCountry("US");
    addr.setStreetAddress("South Park");
    addresses.put("home", addr);
    contact.setAddresses(addresses);
    retVal.put(contact.getId(), contact);
    contact = new Contact();
    contact.setId("5");
    contact.setDisplayName("Kenny McCormick");
    contact.setFirstName("Kenny");
    contact.setFamilyName("McCormick");
    contact.setPhoto(img);
    phones = new Hashtable();
    phones.put("mobile", "+111111");
    phones.put("home", "+222222");
    contact.setPhoneNumbers(phones);
    emails = new Hashtable();
    emails.put("work", "Kenny.McCormick@codenameone.com");
    contact.setEmails(emails);
    addresses = new Hashtable();
    addr = new Address();
    addr.setCountry("US");
    addr.setStreetAddress("South Park");
    addresses.put("home", addr);
    contact.setAddresses(addresses);
    retVal.put(contact.getId(), contact);
    return retVal;
}
Also used : Address(com.codename1.contacts.Address) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) BufferedImage(java.awt.image.BufferedImage) Contact(com.codename1.contacts.Contact)

Example 4 with Address

use of com.codename1.contacts.Address in project CodenameOne by codenameone.

the class AndroidContactsManager method getAllContacts.

public Contact[] getAllContacts(Context activity, boolean withNumbers, boolean includesFullName, boolean includesPicture, boolean includesNumbers, boolean includesEmail, boolean includeAddress) {
    HashMap<String, Contact> contacts = new HashMap<String, Contact>();
    ArrayList sortedContacts = new ArrayList();
    String selection = null;
    if (withNumbers) {
        selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";
    }
    String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_ID, ContactsContract.Contacts.HAS_PHONE_NUMBER };
    ContentResolver contentResolver = activity.getContentResolver();
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, projection, selection, null, "upper(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
    while (cursor.moveToNext()) {
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        Contact contact = new Contact();
        contact.setId(contactId);
        contact.setEmails(new Hashtable());
        // the contacts hash is for faster lookups and the sortedContacts will keep the order sorted
        Contact old = contacts.put(contactId, contact);
        sortedContacts.add(contact);
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        contact.setDisplayName(name);
        if (includesPicture) {
            String photoID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
            if (photoID != null) {
                InputStream input = loadContactPhoto(contentResolver, Long.parseLong(contactId), Long.parseLong(photoID));
                if (input != null) {
                    try {
                        contact.setPhoto(Image.createImage(input));
                    } catch (IOException ex) {
                        Logger.getLogger(AndroidContactsManager.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }
    cursor.close();
    if (includesNumbers) {
        projection = new String[] { ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.IS_PRIMARY };
        Cursor pCur = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
        while (pCur.moveToNext()) {
            String id = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
            Contact contact = contacts.get(id);
            if (contact == null) {
                continue;
            }
            Hashtable phones = contact.getPhoneNumbers();
            if (phones == null) {
                phones = new Hashtable();
                contact.setPhoneNumbers(phones);
            }
            String type = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
            String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            boolean isPrimary = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.IS_PRIMARY)) != 0;
            if (String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME).equals(type)) {
                type = "home";
            } else if (String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).equals(type)) {
                type = "mobile";
            } else if (String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_WORK).equals(type)) {
                type = "work";
            } else if (String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME).equals(type)) {
                type = "fax";
            } else {
                type = "other";
            }
            if (isPrimary) {
                contact.setPrimaryPhoneNumber(phone);
            }
            phones.put(type, phone);
        }
        pCur.close();
    }
    if (includesEmail) {
        projection = new String[] { ContactsContract.CommonDataKinds.Email.CONTACT_ID, ContactsContract.CommonDataKinds.Email.DATA, ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.IS_PRIMARY };
        Cursor emailCur = contentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, null, null, null);
        while (emailCur.moveToNext()) {
            String id = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID));
            Contact contact = contacts.get(id);
            if (contact == null) {
                continue;
            }
            Hashtable emails = contact.getEmails();
            if (emails == null) {
                emails = new Hashtable();
                contact.setEmails(emails);
            }
            // This would allow you get several email addresses
            // if the email addresses were stored in an array
            String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            String type = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
            boolean isPrimary = emailCur.getInt(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.IS_PRIMARY)) != 0;
            if (String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_HOME).equals(type)) {
                type = "home";
            } else if (String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_MOBILE).equals(type)) {
                type = "mobile";
            } else if (String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_WORK).equals(type)) {
                type = "work";
            } else {
                type = "other";
            }
            if (isPrimary) {
                contact.setPrimaryEmail(email);
            }
            emails.put(type, email);
        }
        emailCur.close();
    }
    if (includesFullName) {
        String birthWhere = ContactsContract.Data.MIMETYPE + "= ? AND " + ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
        String[] birthWhereParams = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
        Cursor birthCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, birthWhere, birthWhereParams, null);
        while (birthCur.moveToNext()) {
            String id = birthCur.getString(birthCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.CONTACT_ID));
            Contact contact = contacts.get(id);
            if (contact == null) {
                continue;
            }
            String birth = birthCur.getString(birthCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
            Date bd = null;
            try {
                bd = new SimpleDateFormat("yyyy-MM-dd").parse(birth);
                contact.setBirthday(bd.getTime());
            } catch (ParseException ex) {
            }
        }
        birthCur.close();
        String nameWhere = ContactsContract.Data.MIMETYPE + " = ?";
        String[] nameWhereParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
        projection = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME };
        Cursor nameCursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, projection, nameWhere, nameWhereParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
        while (nameCursor.moveToNext()) {
            String id = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID));
            Contact contact = contacts.get(id);
            if (contact == null) {
                continue;
            }
            String given = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
            String family = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
            String display = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
            if (given != null)
                contact.setFirstName(given);
            if (family != null)
                contact.setFamilyName(family);
            if (display != null)
                contact.setDisplayName(display);
        }
        nameCursor.close();
        String noteWhere = ContactsContract.Data.MIMETYPE + " = ?";
        String[] noteWhereParams = new String[] { ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE };
        projection = new String[] { ContactsContract.CommonDataKinds.Note.CONTACT_ID, ContactsContract.CommonDataKinds.Note.NOTE };
        Cursor noteCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, projection, noteWhere, noteWhereParams, null);
        while (noteCur.moveToNext()) {
            String id = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.CONTACT_ID));
            Contact contact = contacts.get(id);
            if (contact == null) {
                continue;
            }
            String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
            contact.setNote(note);
        }
        noteCur.close();
    }
    if (includeAddress) {
        String addrWhere = ContactsContract.Data.MIMETYPE + " = ?";
        String[] addrWhereParams = new String[] { ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };
        projection = new String[] { ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID, ContactsContract.CommonDataKinds.StructuredPostal.POBOX, ContactsContract.CommonDataKinds.StructuredPostal.STREET, ContactsContract.CommonDataKinds.StructuredPostal.CITY, ContactsContract.CommonDataKinds.StructuredPostal.REGION, ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, ContactsContract.CommonDataKinds.StructuredPostal.TYPE };
        Cursor addrCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, projection, addrWhere, addrWhereParams, null);
        while (addrCur.moveToNext()) {
            String id = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID));
            Contact contact = contacts.get(id);
            if (contact == null) {
                continue;
            }
            Hashtable addresses = contact.getAddresses();
            if (addresses == null) {
                addresses = new Hashtable();
                contact.setAddresses(addresses);
            }
            Address address = new Address();
            // String poBox = addrCur.getString(
            // addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
            String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
            String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
            String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
            String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
            String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
            String type = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
            address.setCountry(country);
            address.setLocality(city);
            address.setPostalCode(postalCode);
            address.setRegion(state);
            address.setStreetAddress(street);
            if (String.valueOf(ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME).equals(type)) {
                type = "home";
            } else if (String.valueOf(ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK).equals(type)) {
                type = "work";
            } else {
                type = "other";
            }
            addresses.put(type, address);
            contact.setAddresses(addresses);
        }
        addrCur.close();
    }
    Contact[] contactsArray = new Contact[sortedContacts.size()];
    sortedContacts.toArray(contactsArray);
    return contactsArray;
}
Also used : Address(com.codename1.contacts.Address) HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Cursor(android.database.Cursor) Date(java.util.Date) Contact(com.codename1.contacts.Contact) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with Address

use of com.codename1.contacts.Address in project CodenameOne by codenameone.

the class AndroidContactsManager method getContact.

public Contact getContact(Context activity, String id, boolean includesFullName, boolean includesPicture, boolean includesNumbers, boolean includesEmail, boolean includeAddress) {
    Contact retVal = new Contact();
    retVal.setId(id);
    ContentResolver cr = activity.getContentResolver();
    String[] projection = new String[] { ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_ID, ContactsContract.Contacts.HAS_PHONE_NUMBER };
    Cursor result = cr.query(ContactsContract.Contacts.CONTENT_URI, projection, ContactsContract.Contacts._ID + " = ?", new String[] { id }, null);
    if (result.moveToFirst()) {
        String name = result.getString(result.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        retVal.setDisplayName(name);
        if (includesPicture) {
            String photoID = result.getString(result.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
            if (photoID != null) {
                InputStream input = loadContactPhoto(cr, Long.parseLong(id), Long.parseLong(photoID));
                if (input != null) {
                    try {
                        retVal.setPhoto(Image.createImage(input));
                    } catch (IOException ex) {
                        Logger.getLogger(AndroidContactsManager.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }
    if (includesNumbers) {
        Hashtable phones = new Hashtable();
        if (Integer.parseInt(result.getString(result.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            // String[] whereParameters = new String[]{id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
            projection = new String[] { ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.IS_PRIMARY };
            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
            while (pCur.moveToNext()) {
                String type = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                boolean isPrimary = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.IS_PRIMARY)) != 0;
                if (String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME).equals(type)) {
                    type = "home";
                } else if (String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).equals(type)) {
                    type = "mobile";
                } else if (String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_WORK).equals(type)) {
                    type = "work";
                } else if (String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME).equals(type)) {
                    type = "fax";
                } else {
                    type = "other";
                }
                if (isPrimary) {
                    retVal.setPrimaryPhoneNumber(phone);
                }
                phones.put(type, phone);
            }
            retVal.setPhoneNumbers(phones);
            pCur.close();
        }
    }
    if (includesEmail) {
        Hashtable emails = new Hashtable();
        projection = new String[] { ContactsContract.CommonDataKinds.Email.DATA, ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.IS_PRIMARY };
        Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { id }, null);
        while (emailCur.moveToNext()) {
            // This would allow you get several email addresses
            // if the email addresses were stored in an array
            String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            String type = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
            boolean isPrimary = emailCur.getInt(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.IS_PRIMARY)) != 0;
            if (String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_HOME).equals(type)) {
                type = "home";
            } else if (String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_MOBILE).equals(type)) {
                type = "mobile";
            } else if (String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_WORK).equals(type)) {
                type = "work";
            } else {
                type = "other";
            }
            if (isPrimary) {
                retVal.setPrimaryEmail(email);
            }
            emails.put(type, email);
        }
        retVal.setEmails(emails);
        emailCur.close();
    }
    if (includesFullName) {
        String birthWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + "= ? AND " + ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
        String[] birthWhereParams = new String[] { id, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
        Cursor birthCur = cr.query(ContactsContract.Data.CONTENT_URI, null, birthWhere, birthWhereParams, null);
        if (birthCur.moveToFirst()) {
            String birth = birthCur.getString(birthCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
            Date bd = null;
            try {
                bd = new SimpleDateFormat("yyyy-MM-dd").parse(birth);
                retVal.setBirthday(bd.getTime());
            } catch (ParseException ex) {
            }
        }
        birthCur.close();
        String nameWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
        String[] nameWhereParams = new String[] { id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
        projection = new String[] { ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME };
        Cursor nameCursor = cr.query(ContactsContract.Data.CONTENT_URI, projection, nameWhere, nameWhereParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
        while (nameCursor.moveToNext()) {
            String given = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
            String family = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
            String display = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
            retVal.setFirstName(given);
            retVal.setFamilyName(family);
            retVal.setDisplayName(display);
        }
        nameCursor.close();
        String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
        String[] noteWhereParams = new String[] { id, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE };
        projection = new String[] { ContactsContract.CommonDataKinds.Note.NOTE };
        Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, projection, noteWhere, noteWhereParams, null);
        if (noteCur.moveToFirst()) {
            String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
            retVal.setNote(note);
        }
        noteCur.close();
    }
    if (includeAddress) {
        Hashtable addresses = new Hashtable();
        String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
        String[] addrWhereParams = new String[] { id, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };
        projection = new String[] { ContactsContract.CommonDataKinds.StructuredPostal.POBOX, ContactsContract.CommonDataKinds.StructuredPostal.STREET, ContactsContract.CommonDataKinds.StructuredPostal.CITY, ContactsContract.CommonDataKinds.StructuredPostal.REGION, ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, ContactsContract.CommonDataKinds.StructuredPostal.TYPE };
        Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI, projection, addrWhere, addrWhereParams, null);
        while (addrCur.moveToNext()) {
            Address address = new Address();
            String poBox = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
            String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
            String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
            String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
            String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
            String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
            String type = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
            address.setCountry(country);
            address.setLocality(city);
            address.setPostalCode(postalCode);
            address.setRegion(state);
            address.setStreetAddress(street);
            if (String.valueOf(ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME).equals(type)) {
                type = "home";
            } else if (String.valueOf(ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK).equals(type)) {
                type = "work";
            } else {
                type = "other";
            }
            addresses.put(type, address);
        }
        retVal.setAddresses(addresses);
        addrCur.close();
    }
    result.close();
    return retVal;
}
Also used : Address(com.codename1.contacts.Address) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Hashtable(java.util.Hashtable) IOException(java.io.IOException) Cursor(android.database.Cursor) Date(java.util.Date) Contact(com.codename1.contacts.Contact) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

Address (com.codename1.contacts.Address)4 Contact (com.codename1.contacts.Contact)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Hashtable (java.util.Hashtable)3 Cursor (android.database.Cursor)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 BufferedInputStream (com.codename1.io.BufferedInputStream)1 Message (com.codename1.messaging.Message)1 EncodedImage (com.codename1.ui.EncodedImage)1 Image (com.codename1.ui.Image)1 BufferedImage (java.awt.image.BufferedImage)1 DataInputStream (java.io.DataInputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)1 MediaException (javax.microedition.media.MediaException)1