Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Gestionnaire des données du carnet d'adresse.
- */
- public class ContactsCache {
- private static class Holder {
- static final ContactsCache INSTANCE = new ContactsCache();
- }
- private Context mContext;
- private Map<String, Contact> mContactsMap;
- private List<Contact> mContactsList;
- public ContactsCache() {
- }
- public static ContactsCache getInstance() {
- return Holder.INSTANCE;
- }
- /**
- * Valorise le context.
- * @param context
- */
- public void init(Context context) {
- this.mContext = context;
- }
- /**
- * Retourne un contact particulier à partir de son id.
- * @param contactId L'id du contact.
- * @return
- */
- public Contact getContact(String contactId) {
- if(mContactsMap == null) {
- mContactsMap = loadContacts();
- }
- return mContactsMap.get(contactId);
- }
- /**
- * Retourne la liste des contacts.
- * @return
- */
- public List<Contact> getContactsList() {
- if(mContactsList == null) {
- mContactsList = new ArrayList<>(getContacts().values());
- }
- Collections.sort(mContactsList);
- return mContactsList;
- }
- /**
- * Retourne la table des contacts.
- * @return
- */
- private Map<String, Contact> getContacts() {
- if(mContactsMap == null) {
- mContactsMap = loadContacts();
- }
- return mContactsMap;
- }
- /**
- * Charge les contacts depuis le carnet d'adresse.
- * @return
- */
- protected Map<String, Contact> loadContacts() {
- Map<String, Contact> contactsMap = new HashMap<>();
- Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
- if(phones != null) {
- while (phones.moveToNext()) {
- String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
- String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
- String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
- int phoneType = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
- int phoneTypeStringRes = ContactsContract.CommonDataKinds.Phone.getTypeLabelResource(phoneType);
- Contact tmpContact = new Contact(id, name, phoneNumber, mContext.getString(phoneTypeStringRes));
- contactsMap.put(id, tmpContact);
- }
- phones.close();
- }
- return contactsMap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement