Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1.  
  2. /**
  3. * Gestionnaire des données du carnet d'adresse.
  4. */
  5. public class ContactsCache {
  6.  
  7. private static class Holder {
  8. static final ContactsCache INSTANCE = new ContactsCache();
  9. }
  10.  
  11. private Context mContext;
  12.  
  13. private Map<String, Contact> mContactsMap;
  14. private List<Contact> mContactsList;
  15.  
  16. public ContactsCache() {
  17. }
  18.  
  19. public static ContactsCache getInstance() {
  20. return Holder.INSTANCE;
  21. }
  22.  
  23. /**
  24. * Valorise le context.
  25. * @param context
  26. */
  27. public void init(Context context) {
  28. this.mContext = context;
  29. }
  30.  
  31. /**
  32. * Retourne un contact particulier à partir de son id.
  33. * @param contactId L'id du contact.
  34. * @return
  35. */
  36. public Contact getContact(String contactId) {
  37. if(mContactsMap == null) {
  38. mContactsMap = loadContacts();
  39. }
  40. return mContactsMap.get(contactId);
  41. }
  42.  
  43. /**
  44. * Retourne la liste des contacts.
  45. * @return
  46. */
  47. public List<Contact> getContactsList() {
  48. if(mContactsList == null) {
  49. mContactsList = new ArrayList<>(getContacts().values());
  50. }
  51. Collections.sort(mContactsList);
  52.  
  53. return mContactsList;
  54. }
  55.  
  56. /**
  57. * Retourne la table des contacts.
  58. * @return
  59. */
  60. private Map<String, Contact> getContacts() {
  61. if(mContactsMap == null) {
  62. mContactsMap = loadContacts();
  63. }
  64. return mContactsMap;
  65. }
  66.  
  67. /**
  68. * Charge les contacts depuis le carnet d'adresse.
  69. * @return
  70. */
  71. protected Map<String, Contact> loadContacts() {
  72. Map<String, Contact> contactsMap = new HashMap<>();
  73. Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
  74.  
  75. if(phones != null) {
  76. while (phones.moveToNext()) {
  77. String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
  78. String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  79. String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  80. int phoneType = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
  81. int phoneTypeStringRes = ContactsContract.CommonDataKinds.Phone.getTypeLabelResource(phoneType);
  82.  
  83. Contact tmpContact = new Contact(id, name, phoneNumber, mContext.getString(phoneTypeStringRes));
  84. contactsMap.put(id, tmpContact);
  85. }
  86.  
  87. phones.close();
  88. }
  89.  
  90. return contactsMap;
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement