Search in sources :

Example 1 with SimpleDateFormat

use of com.codename1.l10n.SimpleDateFormat in project CodenameOne by codenameone.

the class TestComponent method testSimpleDateFormat.

public void testSimpleDateFormat() {
    try {
        SimpleDateFormat format = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
        format.parse("mon, 20-nov-2017 19:49:58 gmt");
        format = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss z");
        format.parse("mon 20-nov-2017 19:49:58 gmt");
        format = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss z");
        java.util.Date dt = format.parse("tue 21-nov-2017 17:04:27 gmt");
        TestUtils.assertBool(dt.getTime() / 1000L == 1511283867L, "Incorrect date for simple date format parse");
        format = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
        com.codename1.l10n.ParseException pex = null;
        try {
            format.parse("tue 21-nov-2017 17:04:27 gmt");
        } catch (com.codename1.l10n.ParseException pex2) {
            pex = pex2;
        }
        TestUtils.assertTrue(pex != null, "Parsing date with wrong format should give parse exception");
        format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
        dt = format.parse("sun, 22 nov 2037 13:20:46 -0000");
        // 2142508846
        // Log.p("Difference = "+(dt.getTime() - 2142508846000L));
        TestUtils.assertEqual(dt.getTime() / 1000L, 2142508846L, "Failed to parse RFC822 datetime.  " + dt);
        String dateStr = "sun 22 nov 2037 13:20:46 -0000";
        format = new SimpleDateFormat("EEE dd MMM yyyy HH:mm:ss Z");
        dt = format.parse("sun 22 nov 2037 13:20:46 -0000");
        TestUtils.assertEqual(dt.getTime() / 1000L, 2142508846L, "Failed to parse RFC822 datetime no comma.  " + dt);
    } catch (Throwable t) {
        Log.e(t);
        throw new RuntimeException("Failed to parse date mon 20-nov-2017 19:49:58 gmt: " + t.getMessage());
    }
}
Also used : SimpleDateFormat(com.codename1.l10n.SimpleDateFormat)

Example 2 with SimpleDateFormat

use of com.codename1.l10n.SimpleDateFormat 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 3 with SimpleDateFormat

use of com.codename1.l10n.SimpleDateFormat 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

Cursor (android.database.Cursor)2 Address (com.codename1.contacts.Address)2 Contact (com.codename1.contacts.Contact)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Hashtable (java.util.Hashtable)2 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1