Search in sources :

Example 1 with BulkOperationsHandle

use of org.thoughtcrime.securesms.database.RecipientDatabase.BulkOperationsHandle in project Signal-Android by WhisperSystems.

the class DirectoryHelper method syncRecipientInfoWithSystemContacts.

private static void syncRecipientInfoWithSystemContacts(@NonNull Context context, @NonNull Map<String, String> rewrites) {
    RecipientDatabase recipientDatabase = SignalDatabase.recipients();
    BulkOperationsHandle handle = recipientDatabase.beginBulkSystemContactUpdate();
    try (Cursor cursor = ContactAccessor.getInstance().getAllSystemContacts(context)) {
        while (cursor != null && cursor.moveToNext()) {
            String mimeType = getMimeType(cursor);
            if (!isPhoneMimeType(mimeType)) {
                continue;
            }
            String lookupKey = getLookupKey(cursor);
            ContactHolder contactHolder = new ContactHolder(lookupKey);
            while (!cursor.isAfterLast() && getLookupKey(cursor).equals(lookupKey) && isPhoneMimeType(getMimeType(cursor))) {
                String number = CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.Phone.NUMBER);
                if (isValidContactNumber(number)) {
                    String formattedNumber = PhoneNumberFormatter.get(context).format(number);
                    String realNumber = Util.getFirstNonEmpty(rewrites.get(formattedNumber), formattedNumber);
                    PhoneNumberRecord.Builder builder = new PhoneNumberRecord.Builder();
                    builder.withRecipientId(Recipient.externalContact(context, realNumber).getId());
                    builder.withDisplayName(CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    builder.withContactPhotoUri(CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
                    builder.withContactLabel(CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.Phone.LABEL));
                    builder.withPhoneType(CursorUtil.requireInt(cursor, ContactsContract.CommonDataKinds.Phone.TYPE));
                    builder.withContactUri(ContactsContract.Contacts.getLookupUri(CursorUtil.requireLong(cursor, ContactsContract.CommonDataKinds.Phone._ID), CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY)));
                    contactHolder.addPhoneNumberRecord(builder.build());
                } else {
                    Log.w(TAG, "Skipping phone entry with invalid number");
                }
                cursor.moveToNext();
            }
            if (!cursor.isAfterLast() && getLookupKey(cursor).equals(lookupKey)) {
                if (isStructuredNameMimeType(getMimeType(cursor))) {
                    StructuredNameRecord.Builder builder = new StructuredNameRecord.Builder();
                    builder.withGivenName(CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                    builder.withFamilyName(CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                    contactHolder.setStructuredNameRecord(builder.build());
                } else {
                    Log.i(TAG, "Skipping invalid mimeType " + mimeType);
                }
            } else {
                Log.i(TAG, "No structured name for user, rolling back cursor.");
                cursor.moveToPrevious();
            }
            contactHolder.commit(handle);
        }
    } catch (IllegalStateException e) {
        Log.w(TAG, "Hit an issue with the cursor while reading!", e);
    } finally {
        handle.finish();
    }
    if (NotificationChannels.supported()) {
        try (RecipientDatabase.RecipientReader recipients = SignalDatabase.recipients().getRecipientsWithNotificationChannels()) {
            Recipient recipient;
            while ((recipient = recipients.getNext()) != null) {
                NotificationChannels.updateContactChannelName(context, recipient);
            }
        }
    }
}
Also used : BulkOperationsHandle(org.thoughtcrime.securesms.database.RecipientDatabase.BulkOperationsHandle) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Cursor(android.database.Cursor) RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase)

Example 2 with BulkOperationsHandle

use of org.thoughtcrime.securesms.database.RecipientDatabase.BulkOperationsHandle in project Signal-Android by signalapp.

the class DirectoryHelper method syncRecipientInfoWithSystemContacts.

private static void syncRecipientInfoWithSystemContacts(@NonNull Context context, @NonNull Map<String, String> rewrites) {
    RecipientDatabase recipientDatabase = SignalDatabase.recipients();
    BulkOperationsHandle handle = recipientDatabase.beginBulkSystemContactUpdate();
    try (Cursor cursor = ContactAccessor.getInstance().getAllSystemContacts(context)) {
        while (cursor != null && cursor.moveToNext()) {
            String mimeType = getMimeType(cursor);
            if (!isPhoneMimeType(mimeType)) {
                continue;
            }
            String lookupKey = getLookupKey(cursor);
            ContactHolder contactHolder = new ContactHolder(lookupKey);
            while (!cursor.isAfterLast() && getLookupKey(cursor).equals(lookupKey) && isPhoneMimeType(getMimeType(cursor))) {
                String number = CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.Phone.NUMBER);
                if (isValidContactNumber(number)) {
                    String formattedNumber = PhoneNumberFormatter.get(context).format(number);
                    String realNumber = Util.getFirstNonEmpty(rewrites.get(formattedNumber), formattedNumber);
                    PhoneNumberRecord.Builder builder = new PhoneNumberRecord.Builder();
                    builder.withRecipientId(Recipient.externalContact(context, realNumber).getId());
                    builder.withDisplayName(CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    builder.withContactPhotoUri(CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
                    builder.withContactLabel(CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.Phone.LABEL));
                    builder.withPhoneType(CursorUtil.requireInt(cursor, ContactsContract.CommonDataKinds.Phone.TYPE));
                    builder.withContactUri(ContactsContract.Contacts.getLookupUri(CursorUtil.requireLong(cursor, ContactsContract.CommonDataKinds.Phone._ID), CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY)));
                    contactHolder.addPhoneNumberRecord(builder.build());
                } else {
                    Log.w(TAG, "Skipping phone entry with invalid number");
                }
                cursor.moveToNext();
            }
            if (!cursor.isAfterLast() && getLookupKey(cursor).equals(lookupKey)) {
                if (isStructuredNameMimeType(getMimeType(cursor))) {
                    StructuredNameRecord.Builder builder = new StructuredNameRecord.Builder();
                    builder.withGivenName(CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                    builder.withFamilyName(CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                    contactHolder.setStructuredNameRecord(builder.build());
                } else {
                    Log.i(TAG, "Skipping invalid mimeType " + mimeType);
                }
            } else {
                Log.i(TAG, "No structured name for user, rolling back cursor.");
                cursor.moveToPrevious();
            }
            contactHolder.commit(handle);
        }
    } catch (IllegalStateException e) {
        Log.w(TAG, "Hit an issue with the cursor while reading!", e);
    } finally {
        handle.finish();
    }
    if (NotificationChannels.supported()) {
        try (RecipientDatabase.RecipientReader recipients = SignalDatabase.recipients().getRecipientsWithNotificationChannels()) {
            Recipient recipient;
            while ((recipient = recipients.getNext()) != null) {
                NotificationChannels.updateContactChannelName(context, recipient);
            }
        }
    }
}
Also used : BulkOperationsHandle(org.thoughtcrime.securesms.database.RecipientDatabase.BulkOperationsHandle) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Cursor(android.database.Cursor) RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase)

Aggregations

Cursor (android.database.Cursor)2 RecipientDatabase (org.thoughtcrime.securesms.database.RecipientDatabase)2 BulkOperationsHandle (org.thoughtcrime.securesms.database.RecipientDatabase.BulkOperationsHandle)2 Recipient (org.thoughtcrime.securesms.recipients.Recipient)2