attempt to read both jabber and xmpp IM fields from address book

This commit is contained in:
Daniel Gultsch 2021-10-03 17:26:24 +02:00
parent fdaab1c27e
commit 4a6df90f0c
1 changed files with 34 additions and 31 deletions

View File

@ -17,6 +17,21 @@ import eu.siacs.conversations.xmpp.Jid;
public class JabberIdContact extends AbstractPhoneContact { public class JabberIdContact extends AbstractPhoneContact {
private static final String[] PROJECTION = new String[]{ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.PHOTO_URI,
ContactsContract.Data.LOOKUP_KEY,
ContactsContract.CommonDataKinds.Im.DATA
};
private static final String SELECTION = ContactsContract.Data.MIMETYPE + "=? AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL + "=? or (" + ContactsContract.CommonDataKinds.Im.PROTOCOL + "=? and " + ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL + "=?))";
private static final String[] SELECTION_ARGS = {
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER),
String.valueOf(ContactsContract.CommonDataKinds.Im.PROTOCOL_CUSTOM),
"XMPP"
};
private final Jid jid; private final Jid jid;
private JabberIdContact(Cursor cursor) throws IllegalArgumentException { private JabberIdContact(Cursor cursor) throws IllegalArgumentException {
@ -36,38 +51,26 @@ public class JabberIdContact extends AbstractPhoneContact {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
return Collections.emptyMap(); return Collections.emptyMap();
} }
final String[] PROJECTION = new String[]{ContactsContract.Data._ID, try (final Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, SELECTION_ARGS, null)) {
ContactsContract.Data.DISPLAY_NAME, if (cursor == null) {
ContactsContract.Data.PHOTO_URI, return Collections.emptyMap();
ContactsContract.Data.LOOKUP_KEY, }
ContactsContract.CommonDataKinds.Im.DATA}; final HashMap<Jid, JabberIdContact> contacts = new HashMap<>();
while (cursor.moveToNext()) {
final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\"" try {
+ ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE final JabberIdContact contact = new JabberIdContact(cursor);
+ "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL final JabberIdContact preexisting = contacts.put(contact.getJid(), contact);
+ "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER if (preexisting == null || preexisting.rating() < contact.rating()) {
+ "\")"; contacts.put(contact.getJid(), contact);
final Cursor cursor; }
try { } catch (final IllegalArgumentException e) {
cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, null); Log.d(Config.LOGTAG, "unable to create jabber id contact");
} catch (Exception e) { }
}
return contacts;
} catch (final Exception e) {
Log.d(Config.LOGTAG, "unable to query", e);
return Collections.emptyMap(); return Collections.emptyMap();
} }
final HashMap<Jid, JabberIdContact> contacts = new HashMap<>();
while (cursor != null && cursor.moveToNext()) {
try {
final JabberIdContact contact = new JabberIdContact(cursor);
final JabberIdContact preexisting = contacts.put(contact.getJid(), contact);
if (preexisting == null || preexisting.rating() < contact.rating()) {
contacts.put(contact.getJid(), contact);
}
} catch (IllegalArgumentException e) {
Log.d(Config.LOGTAG,"unable to create jabber id contact");
}
}
if (cursor != null) {
cursor.close();
}
return contacts;
} }
} }