Search in sources :

Example 1 with ContactTokenDetails

use of org.whispersystems.signalservice.api.push.ContactTokenDetails in project Signal-Android by WhisperSystems.

the class ContactsDatabase method setRegisteredUsers.

@NonNull
public synchronized List<String> setRegisteredUsers(@NonNull Account account, @NonNull String localNumber, @NonNull List<ContactTokenDetails> registeredContacts, boolean remove) throws RemoteException, OperationApplicationException {
    Map<String, ContactTokenDetails> registeredNumbers = new HashMap<>();
    List<String> addedNumbers = new LinkedList<>();
    ArrayList<ContentProviderOperation> operations = new ArrayList<>();
    Map<String, SignalContact> currentContacts = getSignalRawContacts(account, localNumber);
    for (ContactTokenDetails registeredContact : registeredContacts) {
        String registeredNumber = registeredContact.getNumber();
        registeredNumbers.put(registeredNumber, registeredContact);
        if (!currentContacts.containsKey(registeredNumber)) {
            Optional<SystemContactInfo> systemContactInfo = getSystemContactInfo(registeredNumber, localNumber);
            if (systemContactInfo.isPresent()) {
                Log.w(TAG, "Adding number: " + registeredNumber);
                addedNumbers.add(registeredNumber);
                addTextSecureRawContact(operations, account, systemContactInfo.get().number, systemContactInfo.get().name, systemContactInfo.get().id, true);
            }
        }
    }
    for (Map.Entry<String, SignalContact> currentContactEntry : currentContacts.entrySet()) {
        ContactTokenDetails tokenDetails = registeredNumbers.get(currentContactEntry.getKey());
        if (tokenDetails == null) {
            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);
    }
    return addedNumbers;
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) HashMap(java.util.HashMap) ContactTokenDetails(org.whispersystems.signalservice.api.push.ContactTokenDetails) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map) NonNull(android.support.annotation.NonNull)

Example 2 with ContactTokenDetails

use of org.whispersystems.signalservice.api.push.ContactTokenDetails in project Signal-Android by WhisperSystems.

the class PushReceivedJob method handle.

public void handle(SignalServiceEnvelope envelope, boolean sendExplicitReceipt) {
    if (!isActiveNumber(context, envelope.getSource())) {
        TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
        ContactTokenDetails contactTokenDetails = new ContactTokenDetails();
        contactTokenDetails.setNumber(envelope.getSource());
        directory.setNumber(contactTokenDetails, true);
        Recipients recipients = RecipientFactory.getRecipientsFromString(context, envelope.getSource(), false);
        ApplicationContext.getInstance(context).getJobManager().add(new DirectoryRefreshJob(context, KeyCachingService.getMasterSecret(context), recipients));
    }
    if (envelope.isReceipt()) {
        handleReceipt(envelope);
    } else if (envelope.isPreKeySignalMessage() || envelope.isSignalMessage()) {
        handleMessage(envelope, sendExplicitReceipt);
    } else {
        Log.w(TAG, "Received envelope of unknown type: " + envelope.getType());
    }
}
Also used : Recipients(org.thoughtcrime.securesms.recipients.Recipients) ContactTokenDetails(org.whispersystems.signalservice.api.push.ContactTokenDetails) TextSecureDirectory(org.thoughtcrime.securesms.database.TextSecureDirectory)

Example 3 with ContactTokenDetails

use of org.whispersystems.signalservice.api.push.ContactTokenDetails in project Signal-Android by WhisperSystems.

the class DirectoryHelper method refreshDirectory.

@NonNull
public static RefreshResult refreshDirectory(@NonNull Context context, @NonNull SignalServiceAccountManager accountManager, @NonNull String localNumber) throws IOException {
    TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
    Set<String> eligibleContactNumbers = directory.getPushEligibleContactNumbers(localNumber);
    List<ContactTokenDetails> activeTokens = accountManager.getContacts(eligibleContactNumbers);
    if (activeTokens != null) {
        for (ContactTokenDetails activeToken : activeTokens) {
            eligibleContactNumbers.remove(activeToken.getNumber());
            activeToken.setNumber(activeToken.getNumber());
        }
        directory.setNumbers(activeTokens, eligibleContactNumbers);
        return updateContactsDatabase(context, localNumber, activeTokens, true);
    }
    return new RefreshResult(new LinkedList<String>(), false);
}
Also used : ContactTokenDetails(org.whispersystems.signalservice.api.push.ContactTokenDetails) TextSecureDirectory(org.thoughtcrime.securesms.database.TextSecureDirectory) NonNull(android.support.annotation.NonNull)

Example 4 with ContactTokenDetails

use of org.whispersystems.signalservice.api.push.ContactTokenDetails in project Signal-Android by WhisperSystems.

the class DirectoryHelper method refreshDirectoryFor.

public static UserCapabilities refreshDirectoryFor(@NonNull Context context, @Nullable MasterSecret masterSecret, @NonNull Recipients recipients, @NonNull String localNumber) throws IOException {
    try {
        TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
        SignalServiceAccountManager accountManager = AccountManagerFactory.createManager(context);
        String number = Util.canonicalizeNumber(context, recipients.getPrimaryRecipient().getNumber());
        Optional<ContactTokenDetails> details = accountManager.getContact(number);
        if (details.isPresent()) {
            directory.setNumber(details.get(), true);
            RefreshResult result = updateContactsDatabase(context, localNumber, details.get());
            if (!result.getNewUsers().isEmpty() && TextSecurePreferences.isMultiDevice(context)) {
                ApplicationContext.getInstance(context).getJobManager().add(new MultiDeviceContactUpdateJob(context));
            }
            if (!result.isFresh()) {
                notifyNewUsers(context, masterSecret, result.getNewUsers());
            }
            return new UserCapabilities(Capability.SUPPORTED, details.get().isVoice() ? Capability.SUPPORTED : Capability.UNSUPPORTED, details.get().isVideo() ? Capability.SUPPORTED : Capability.UNSUPPORTED);
        } else {
            ContactTokenDetails absent = new ContactTokenDetails();
            absent.setNumber(number);
            directory.setNumber(absent, false);
            return UserCapabilities.UNSUPPORTED;
        }
    } catch (InvalidNumberException e) {
        Log.w(TAG, e);
        return UserCapabilities.UNSUPPORTED;
    }
}
Also used : MultiDeviceContactUpdateJob(org.thoughtcrime.securesms.jobs.MultiDeviceContactUpdateJob) InvalidNumberException(org.whispersystems.signalservice.api.util.InvalidNumberException) SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) ContactTokenDetails(org.whispersystems.signalservice.api.push.ContactTokenDetails) TextSecureDirectory(org.thoughtcrime.securesms.database.TextSecureDirectory)

Example 5 with ContactTokenDetails

use of org.whispersystems.signalservice.api.push.ContactTokenDetails in project Signal-Android by WhisperSystems.

the class TextSecureDirectory method setNumbers.

public void setNumbers(List<ContactTokenDetails> activeTokens, Collection<String> inactiveTokens) {
    long timestamp = System.currentTimeMillis();
    SQLiteDatabase db = databaseHelper.getWritableDatabase();
    db.beginTransaction();
    try {
        for (ContactTokenDetails token : activeTokens) {
            Log.w("Directory", "Adding active token: " + token.getNumber() + ", " + token.getToken() + ", video: " + token.isVideo());
            ContentValues values = new ContentValues();
            values.put(NUMBER, token.getNumber());
            values.put(REGISTERED, 1);
            values.put(TIMESTAMP, timestamp);
            values.put(RELAY, token.getRelay());
            values.put(VOICE, token.isVoice());
            values.put(VIDEO, token.isVideo());
            db.replace(TABLE_NAME, null, values);
        }
        for (String token : inactiveTokens) {
            ContentValues values = new ContentValues();
            values.put(NUMBER, token);
            values.put(REGISTERED, 0);
            values.put(TIMESTAMP, timestamp);
            db.replace(TABLE_NAME, null, values);
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ContactTokenDetails(org.whispersystems.signalservice.api.push.ContactTokenDetails)

Aggregations

ContactTokenDetails (org.whispersystems.signalservice.api.push.ContactTokenDetails)5 TextSecureDirectory (org.thoughtcrime.securesms.database.TextSecureDirectory)3 NonNull (android.support.annotation.NonNull)2 ContentProviderOperation (android.content.ContentProviderOperation)1 ContentValues (android.content.ContentValues)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 MultiDeviceContactUpdateJob (org.thoughtcrime.securesms.jobs.MultiDeviceContactUpdateJob)1 Recipients (org.thoughtcrime.securesms.recipients.Recipients)1 SignalServiceAccountManager (org.whispersystems.signalservice.api.SignalServiceAccountManager)1 InvalidNumberException (org.whispersystems.signalservice.api.util.InvalidNumberException)1