PhoneNumberContact.java

 1package eu.siacs.conversations.android;
 2
 3import android.Manifest;
 4import android.content.Context;
 5import android.content.pm.PackageManager;
 6import android.database.Cursor;
 7import android.net.Uri;
 8import android.os.Build;
 9import android.provider.ContactsContract;
10import android.util.Log;
11
12import java.util.Collection;
13import java.util.Collections;
14import java.util.HashMap;
15import java.util.Map;
16
17import eu.siacs.conversations.Config;
18import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
19import io.michaelrocks.libphonenumber.android.NumberParseException;
20
21public class PhoneNumberContact extends AbstractPhoneContact {
22
23    private String phoneNumber;
24
25    public String getPhoneNumber() {
26        return phoneNumber;
27    }
28
29    private PhoneNumberContact(Context context, Cursor cursor) throws IllegalArgumentException {
30        super(cursor);
31        try {
32            this.phoneNumber = PhoneNumberUtilWrapper.normalize(context,cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
33        } catch (NumberParseException | NullPointerException e) {
34            throw new IllegalArgumentException(e);
35        }
36    }
37
38    public static Map<String, PhoneNumberContact> load(Context context) {
39        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
40            return Collections.emptyMap();
41        }
42        final String[] PROJECTION = new String[]{ContactsContract.Data._ID,
43                ContactsContract.Data.DISPLAY_NAME,
44                ContactsContract.Data.PHOTO_URI,
45                ContactsContract.Data.LOOKUP_KEY,
46                ContactsContract.CommonDataKinds.Phone.NUMBER};
47        final Cursor cursor;
48        try {
49            cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, null);
50        } catch (Exception e) {
51            return Collections.emptyMap();
52        }
53        final HashMap<String, PhoneNumberContact> contacts = new HashMap<>();
54        while (cursor != null && cursor.moveToNext()) {
55            try {
56                final PhoneNumberContact contact = new PhoneNumberContact(context, cursor);
57                final PhoneNumberContact preexisting = contacts.get(contact.getPhoneNumber());
58                if (preexisting == null || preexisting.rating() < contact.rating()) {
59                    contacts.put(contact.getPhoneNumber(), contact);
60                }
61            } catch (IllegalArgumentException e) {
62                Log.d(Config.LOGTAG, "unable to create phone contact");
63            }
64        }
65        if (cursor != null) {
66            cursor.close();
67        }
68        return contacts;
69    }
70
71    public static PhoneNumberContact findByUri(Collection<PhoneNumberContact> haystack, Uri needle) {
72        for(PhoneNumberContact contact : haystack) {
73            if (needle.equals(contact.getLookupUri())) {
74                return contact;
75            }
76        }
77        return null;
78    }
79}