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 private final String typeLabel;
27
28 public String getPhoneNumber() {
29 return phoneNumber;
30 }
31
32 public String getTypeLabel() {
33 return typeLabel;
34 }
35
36 private PhoneNumberContact(Context context, Cursor cursor) throws IllegalArgumentException {
37 super(cursor);
38 try {
39 this.phoneNumber = PhoneNumberUtilWrapper.normalize(context, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
40 this.typeLabel = ContactsContract.CommonDataKinds.Phone.getTypeLabel(
41 context.getResources(),
42 cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)),
43 cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL))
44 ).toString();
45 } catch (NumberParseException | NullPointerException e) {
46 throw new IllegalArgumentException(e);
47 }
48 }
49
50 public static ImmutableMap<String, PhoneNumberContact> load(Context context) {
51 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
52 return ImmutableMap.of();
53 }
54 final String[] PROJECTION = new String[]{ContactsContract.Data._ID,
55 ContactsContract.Data.DISPLAY_NAME,
56 ContactsContract.Data.PHOTO_URI,
57 ContactsContract.Data.LOOKUP_KEY,
58 ContactsContract.CommonDataKinds.Phone.TYPE,
59 ContactsContract.CommonDataKinds.Phone.LABEL,
60 ContactsContract.CommonDataKinds.Phone.NUMBER};
61 final HashMap<String, PhoneNumberContact> contacts = new HashMap<>();
62 try (final Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, null)){
63 while (cursor != null && cursor.moveToNext()) {
64 try {
65 final PhoneNumberContact contact = new PhoneNumberContact(context, cursor);
66 final PhoneNumberContact preexisting = contacts.get(contact.getPhoneNumber());
67 if (preexisting == null || preexisting.rating() < contact.rating()) {
68 contacts.put(contact.getPhoneNumber(), contact);
69 }
70 } catch (final IllegalArgumentException ignored) {
71
72 }
73 }
74 } catch (final Exception e) {
75 return ImmutableMap.of();
76 }
77 return ImmutableMap.copyOf(contacts);
78 }
79
80 public static PhoneNumberContact findByUriOrNumber(Collection<PhoneNumberContact> haystack, Uri uri, String number) {
81 final PhoneNumberContact byUri = findByUri(haystack, uri);
82 return byUri != null || number == null ? byUri : findByNumber(haystack, number);
83 }
84
85 public static PhoneNumberContact findByUri(Collection<PhoneNumberContact> haystack, Uri needle) {
86 for (PhoneNumberContact contact : haystack) {
87 if (needle.equals(contact.getLookupUri())) {
88 return contact;
89 }
90 }
91 return null;
92 }
93
94 private static PhoneNumberContact findByNumber(Collection<PhoneNumberContact> haystack, String needle) {
95 for (PhoneNumberContact contact : haystack) {
96 if (needle.equals(contact.getPhoneNumber())) {
97 return contact;
98 }
99 }
100 return null;
101 }
102}