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 Cursor cursor;
50        try {
51            cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, null);
52        } catch (final Exception e) {
53            return ImmutableMap.of();
54        }
55        final HashMap<String, PhoneNumberContact> contacts = new HashMap<>();
56        while (cursor != null && cursor.moveToNext()) {
57            try {
58                final PhoneNumberContact contact = new PhoneNumberContact(context, cursor);
59                final PhoneNumberContact preexisting = contacts.get(contact.getPhoneNumber());
60                if (preexisting == null || preexisting.rating() < contact.rating()) {
61                    contacts.put(contact.getPhoneNumber(), contact);
62                }
63            } catch (final IllegalArgumentException e) {
64                Log.d(Config.LOGTAG, e.getMessage());
65            }
66        }
67        if (cursor != null) {
68            cursor.close();
69        }
70        return ImmutableMap.copyOf(contacts);
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}