From 4a6df90f0c3198ec9c198d35d3ba528d5965b970 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 3 Oct 2021 17:26:24 +0200 Subject: [PATCH] attempt to read both jabber and xmpp IM fields from address book --- .../android/JabberIdContact.java | 65 ++++++++++--------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/android/JabberIdContact.java b/src/main/java/eu/siacs/conversations/android/JabberIdContact.java index 195891d22..0fd46148b 100644 --- a/src/main/java/eu/siacs/conversations/android/JabberIdContact.java +++ b/src/main/java/eu/siacs/conversations/android/JabberIdContact.java @@ -17,6 +17,21 @@ import eu.siacs.conversations.xmpp.Jid; 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 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) { return Collections.emptyMap(); } - final String[] PROJECTION = new String[]{ContactsContract.Data._ID, - ContactsContract.Data.DISPLAY_NAME, - ContactsContract.Data.PHOTO_URI, - ContactsContract.Data.LOOKUP_KEY, - ContactsContract.CommonDataKinds.Im.DATA}; - - final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\"" - + ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE - + "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL - + "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER - + "\")"; - final Cursor cursor; - try { - cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, null); - } catch (Exception e) { + try (final Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, SELECTION_ARGS, null)) { + if (cursor == null) { + return Collections.emptyMap(); + } + final HashMap contacts = new HashMap<>(); + while (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 (final IllegalArgumentException e) { + Log.d(Config.LOGTAG, "unable to create jabber id contact"); + } + } + return contacts; + } catch (final Exception e) { + Log.d(Config.LOGTAG, "unable to query", e); return Collections.emptyMap(); } - final HashMap 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; } }