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 com.google.common.collect.ImmutableMap;
13
14import java.util.Collection;
15import java.util.Collections;
16import java.util.HashMap;
17import java.util.Map;
18
19import eu.siacs.conversations.Config;
20import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
21import io.michaelrocks.libphonenumber.android.NumberParseException;
22
23public class PhoneNumberContact extends AbstractPhoneContact {
24
25    private final String phoneNumber;
26
27    public String getPhoneNumber() {
28        return phoneNumber;
29    }
30
31    private PhoneNumberContact(Context context, Cursor cursor) throws IllegalArgumentException {
32        super(cursor);
33        try {
34            this.phoneNumber = PhoneNumberUtilWrapper.normalize(context, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
35        } catch (NumberParseException | NullPointerException e) {
36            throw new IllegalArgumentException(e);
37        }
38    }
39
40    public static ImmutableMap<String, PhoneNumberContact> load(Context context) {
41        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
42            return ImmutableMap.of();
43        }
44        final String[] PROJECTION = new String[]{ContactsContract.Data._ID,
45                ContactsContract.Data.DISPLAY_NAME,
46                ContactsContract.Data.PHOTO_URI,
47                ContactsContract.Data.LOOKUP_KEY,
48                ContactsContract.CommonDataKinds.Phone.NUMBER};
49        final HashMap<String, PhoneNumberContact> contacts = new HashMap<>();
50        try (final Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, null)){
51            while (cursor != null && cursor.moveToNext()) {
52                try {
53                    final PhoneNumberContact contact = new PhoneNumberContact(context, cursor);
54                    final PhoneNumberContact preexisting = contacts.get(contact.getPhoneNumber());
55                    if (preexisting == null || preexisting.rating() < contact.rating()) {
56                        contacts.put(contact.getPhoneNumber(), contact);
57                    }
58                } catch (final IllegalArgumentException ignored) {
59
60                }
61            }
62        } catch (final Exception e) {
63            return ImmutableMap.of();
64        }
65        return ImmutableMap.copyOf(contacts);
66    }
67
68    public static PhoneNumberContact findByUriOrNumber(Collection<PhoneNumberContact> haystack, Uri uri, String number) {
69        final PhoneNumberContact byUri = findByUri(haystack, uri);
70        return byUri != null || number == null ? byUri : findByNumber(haystack, number);
71    }
72
73    public static PhoneNumberContact findByUri(Collection<PhoneNumberContact> haystack, Uri needle) {
74        for (PhoneNumberContact contact : haystack) {
75            if (needle.equals(contact.getLookupUri())) {
76                return contact;
77            }
78        }
79        return null;
80    }
81
82    private static PhoneNumberContact findByNumber(Collection<PhoneNumberContact> haystack, String needle) {
83        for (PhoneNumberContact contact : haystack) {
84            if (needle.equals(contact.getPhoneNumber())) {
85                return contact;
86            }
87        }
88        return null;
89    }
90}