Search in sources :

Example 1 with Address

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

the class ContactsDatabase method getSystemContactInfo.

private Optional<SystemContactInfo> getSystemContactInfo(@NonNull Address address) {
    if (!address.isPhone())
        return Optional.absent();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address.toPhoneString()));
    String[] projection = { ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME };
    Cursor numberCursor = null;
    Cursor idCursor = null;
    try {
        numberCursor = context.getContentResolver().query(uri, projection, null, null, null);
        while (numberCursor != null && numberCursor.moveToNext()) {
            String systemNumber = numberCursor.getString(0);
            Address systemAddress = Address.fromExternal(context, systemNumber);
            if (systemAddress.equals(address)) {
                idCursor = context.getContentResolver().query(RawContacts.CONTENT_URI, new String[] { RawContacts._ID }, RawContacts.CONTACT_ID + " = ? ", new String[] { String.valueOf(numberCursor.getLong(1)) }, null);
                if (idCursor != null && idCursor.moveToNext()) {
                    return Optional.of(new SystemContactInfo(numberCursor.getString(2), numberCursor.getString(0), idCursor.getLong(0)));
                }
            }
        }
    } finally {
        if (numberCursor != null)
            numberCursor.close();
        if (idCursor != null)
            idCursor.close();
    }
    return Optional.absent();
}
Also used : Address(org.thoughtcrime.securesms.database.Address) Cursor(android.database.Cursor) Uri(android.net.Uri)

Example 2 with Address

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

the class ContactsDatabase method setRegisteredUsers.

public synchronized void setRegisteredUsers(@NonNull Account account, @NonNull List<Address> registeredAddressList, boolean remove) throws RemoteException, OperationApplicationException {
    Set<Address> registeredAddressSet = new HashSet<>();
    ArrayList<ContentProviderOperation> operations = new ArrayList<>();
    Map<Address, SignalContact> currentContacts = getSignalRawContacts(account);
    for (Address registeredAddress : registeredAddressList) {
        registeredAddressSet.add(registeredAddress);
        if (!currentContacts.containsKey(registeredAddress)) {
            Optional<SystemContactInfo> systemContactInfo = getSystemContactInfo(registeredAddress);
            if (systemContactInfo.isPresent()) {
                Log.w(TAG, "Adding number: " + registeredAddress);
                addTextSecureRawContact(operations, account, systemContactInfo.get().number, systemContactInfo.get().name, systemContactInfo.get().id, true);
            }
        }
    }
    for (Map.Entry<Address, SignalContact> currentContactEntry : currentContacts.entrySet()) {
        if (!registeredAddressSet.contains(currentContactEntry.getKey())) {
            if (remove) {
                Log.w(TAG, "Removing number: " + currentContactEntry.getKey());
                removeTextSecureRawContact(operations, account, currentContactEntry.getValue().getId());
            }
        } else if (!currentContactEntry.getValue().isVoiceSupported()) {
            Log.w(TAG, "Adding voice support: " + currentContactEntry.getKey());
            addContactVoiceSupport(operations, currentContactEntry.getKey(), currentContactEntry.getValue().getId());
        } else if (!Util.isStringEquals(currentContactEntry.getValue().getRawDisplayName(), currentContactEntry.getValue().getAggregateDisplayName())) {
            Log.w(TAG, "Updating display name: " + currentContactEntry.getKey());
            updateDisplayName(operations, currentContactEntry.getValue().getAggregateDisplayName(), currentContactEntry.getValue().getId(), currentContactEntry.getValue().getDisplayNameSource());
        }
    }
    if (!operations.isEmpty()) {
        context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operations);
    }
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) Address(org.thoughtcrime.securesms.database.Address) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 3 with Address

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

the class ContactsDatabase method getSignalRawContacts.

@NonNull
private Map<Address, SignalContact> getSignalRawContacts(@NonNull Account account) {
    Uri currentContactsUri = RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name).appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type).build();
    Map<Address, SignalContact> signalContacts = new HashMap<>();
    Cursor cursor = null;
    try {
        String[] projection;
        if (Build.VERSION.SDK_INT >= 11) {
            projection = new String[] { BaseColumns._ID, RawContacts.SYNC1, RawContacts.SYNC4, RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY, RawContacts.DISPLAY_NAME_SOURCE };
        } else {
            projection = new String[] { BaseColumns._ID, RawContacts.SYNC1, RawContacts.SYNC4, RawContacts.CONTACT_ID };
        }
        cursor = context.getContentResolver().query(currentContactsUri, projection, null, null, null);
        while (cursor != null && cursor.moveToNext()) {
            Address currentAddress = Address.fromExternal(context, cursor.getString(1));
            long rawContactId = cursor.getLong(0);
            long contactId = cursor.getLong(3);
            String supportsVoice = cursor.getString(2);
            String rawContactDisplayName = null;
            String aggregateDisplayName = null;
            int rawContactDisplayNameSource = 0;
            if (Build.VERSION.SDK_INT >= 11) {
                rawContactDisplayName = cursor.getString(4);
                rawContactDisplayNameSource = cursor.getInt(5);
                aggregateDisplayName = getDisplayName(contactId);
            }
            signalContacts.put(currentAddress, new SignalContact(rawContactId, supportsVoice, rawContactDisplayName, aggregateDisplayName, rawContactDisplayNameSource));
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return signalContacts;
}
Also used : Address(org.thoughtcrime.securesms.database.Address) HashMap(java.util.HashMap) Cursor(android.database.Cursor) Uri(android.net.Uri) NonNull(android.support.annotation.NonNull)

Example 4 with Address

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

the class TextSecureIdentityKeyStore method saveIdentity.

public boolean saveIdentity(SignalProtocolAddress address, IdentityKey identityKey, boolean nonBlockingApproval) {
    synchronized (LOCK) {
        IdentityDatabase identityDatabase = DatabaseFactory.getIdentityDatabase(context);
        Address signalAddress = Address.fromExternal(context, address.getName());
        Optional<IdentityRecord> identityRecord = identityDatabase.getIdentity(signalAddress);
        if (!identityRecord.isPresent()) {
            Log.w(TAG, "Saving new identity...");
            identityDatabase.saveIdentity(signalAddress, identityKey, VerifiedStatus.DEFAULT, true, System.currentTimeMillis(), nonBlockingApproval);
            return false;
        }
        if (!identityRecord.get().getIdentityKey().equals(identityKey)) {
            Log.w(TAG, "Replacing existing identity...");
            VerifiedStatus verifiedStatus;
            if (identityRecord.get().getVerifiedStatus() == VerifiedStatus.VERIFIED || identityRecord.get().getVerifiedStatus() == VerifiedStatus.UNVERIFIED) {
                verifiedStatus = VerifiedStatus.UNVERIFIED;
            } else {
                verifiedStatus = VerifiedStatus.DEFAULT;
            }
            identityDatabase.saveIdentity(signalAddress, identityKey, verifiedStatus, false, System.currentTimeMillis(), nonBlockingApproval);
            IdentityUtil.markIdentityUpdate(context, Recipient.from(context, signalAddress, true));
            SessionUtil.archiveSiblingSessions(context, address);
            return true;
        }
        if (isNonBlockingApprovalRequired(identityRecord.get())) {
            Log.w(TAG, "Setting approval status...");
            identityDatabase.setApproval(signalAddress, nonBlockingApproval);
            return false;
        }
        return false;
    }
}
Also used : IdentityDatabase(org.thoughtcrime.securesms.database.IdentityDatabase) VerifiedStatus(org.thoughtcrime.securesms.database.IdentityDatabase.VerifiedStatus) Address(org.thoughtcrime.securesms.database.Address) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) IdentityRecord(org.thoughtcrime.securesms.database.IdentityDatabase.IdentityRecord)

Example 5 with Address

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

the class TextSecureIdentityKeyStore method isTrustedIdentity.

@Override
public boolean isTrustedIdentity(SignalProtocolAddress address, IdentityKey identityKey, Direction direction) {
    synchronized (LOCK) {
        IdentityDatabase identityDatabase = DatabaseFactory.getIdentityDatabase(context);
        String ourNumber = TextSecurePreferences.getLocalNumber(context);
        Address theirAddress = Address.fromExternal(context, address.getName());
        if (ourNumber.equals(address.getName()) || Address.fromSerialized(ourNumber).equals(theirAddress)) {
            return identityKey.equals(IdentityKeyUtil.getIdentityKey(context));
        }
        switch(direction) {
            case SENDING:
                return isTrustedForSending(identityKey, identityDatabase.getIdentity(theirAddress));
            case RECEIVING:
                return true;
            default:
                throw new AssertionError("Unknown direction: " + direction);
        }
    }
}
Also used : IdentityDatabase(org.thoughtcrime.securesms.database.IdentityDatabase) Address(org.thoughtcrime.securesms.database.Address) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Aggregations

Address (org.thoughtcrime.securesms.database.Address)40 Recipient (org.thoughtcrime.securesms.recipients.Recipient)13 LinkedList (java.util.LinkedList)10 Uri (android.net.Uri)9 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)7 Cursor (android.database.Cursor)6 ByteString (com.google.protobuf.ByteString)6 IOException (java.io.IOException)6 File (java.io.File)5 GroupContext (org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContext)5 NonNull (android.support.annotation.NonNull)4 Attachment (org.thoughtcrime.securesms.attachments.Attachment)4 InsertResult (org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult)4 SignalServiceAttachment (org.whispersystems.signalservice.api.messages.SignalServiceAttachment)4 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)4 SuppressLint (android.annotation.SuppressLint)3 Nullable (android.support.annotation.Nullable)3 HashSet (java.util.HashSet)3 List (java.util.List)3 ContentValues (android.content.ContentValues)2