Search in sources :

Example 1 with ParseException

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

the class AndroidImplementation method getLocalizationManager.

/**
 * @inheritDoc
 */
public L10NManager getLocalizationManager() {
    if (l10n == null) {
        Locale l = Locale.getDefault();
        l10n = new L10NManager(l.getLanguage(), l.getCountry()) {

            public double parseDouble(String localeFormattedDecimal) {
                try {
                    return NumberFormat.getNumberInstance().parse(localeFormattedDecimal).doubleValue();
                } catch (ParseException err) {
                    return Double.parseDouble(localeFormattedDecimal);
                }
            }

            public String format(int number) {
                return NumberFormat.getNumberInstance().format(number);
            }

            public String format(double number) {
                return NumberFormat.getNumberInstance().format(number);
            }

            public String formatCurrency(double currency) {
                return NumberFormat.getCurrencyInstance().format(currency);
            }

            public String formatDateLongStyle(Date d) {
                return DateFormat.getDateInstance(DateFormat.LONG).format(d);
            }

            public String formatDateShortStyle(Date d) {
                return DateFormat.getDateInstance(DateFormat.SHORT).format(d);
            }

            public String formatDateTime(Date d) {
                return DateFormat.getDateTimeInstance().format(d);
            }

            public String formatDateTimeMedium(Date d) {
                DateFormat dd = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
                return dd.format(d);
            }

            public String formatDateTimeShort(Date d) {
                DateFormat dd = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
                return dd.format(d);
            }

            public String getCurrencySymbol() {
                return NumberFormat.getInstance().getCurrency().getSymbol();
            }

            public void setLocale(String locale, String language) {
                super.setLocale(locale, language);
                Locale l = new Locale(language, locale);
                Locale.setDefault(l);
            }
        };
    }
    return l10n;
}
Also used : Locale(java.util.Locale) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) Paint(android.graphics.Paint) Date(java.util.Date) L10NManager(com.codename1.l10n.L10NManager)

Example 2 with ParseException

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

the class JavaSEPort method getLocalizationManager.

/**
 * @inheritDoc
 */
public L10NManager getLocalizationManager() {
    if (l10n == null) {
        Locale l = Locale.getDefault();
        l10n = new L10NManager(l.getLanguage(), l.getCountry()) {

            public double parseDouble(String localeFormattedDecimal) {
                try {
                    return NumberFormat.getNumberInstance().parse(localeFormattedDecimal).doubleValue();
                } catch (ParseException err) {
                    return Double.parseDouble(localeFormattedDecimal);
                }
            }

            public String format(int number) {
                return NumberFormat.getNumberInstance().format(number);
            }

            public String format(double number) {
                return NumberFormat.getNumberInstance().format(number);
            }

            public String formatCurrency(double currency) {
                return NumberFormat.getCurrencyInstance().format(currency);
            }

            public String formatDateLongStyle(Date d) {
                return DateFormat.getDateInstance(DateFormat.LONG).format(d);
            }

            public String formatDateShortStyle(Date d) {
                return DateFormat.getDateInstance(DateFormat.SHORT).format(d);
            }

            public String formatDateTime(Date d) {
                return DateFormat.getDateTimeInstance().format(d);
            }

            public String formatDateTimeMedium(Date d) {
                DateFormat dd = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
                return dd.format(d);
            }

            public String formatDateTimeShort(Date d) {
                DateFormat dd = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
                return dd.format(d);
            }

            public String getCurrencySymbol() {
                return NumberFormat.getInstance().getCurrency().getSymbol();
            }

            public void setLocale(String locale, String language) {
                super.setLocale(locale, language);
                Locale l = new Locale(language, locale);
                Locale.setDefault(l);
            }
        };
    }
    return l10n;
}
Also used : Locale(java.util.Locale) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) Point(java.awt.Point) L10NManager(com.codename1.l10n.L10NManager)

Example 3 with ParseException

use of com.codename1.l10n.ParseException 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 4 with ParseException

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

ParseException (java.text.ParseException)4 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 Cursor (android.database.Cursor)2 Address (com.codename1.contacts.Address)2 Contact (com.codename1.contacts.Contact)2 L10NManager (com.codename1.l10n.L10NManager)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 DateFormat (java.text.DateFormat)2 Hashtable (java.util.Hashtable)2 Locale (java.util.Locale)2 Paint (android.graphics.Paint)1 Point (java.awt.Point)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1