Search in sources :

Example 61 with RecipientId

use of org.thoughtcrime.securesms.recipients.RecipientId in project Signal-Android by WhisperSystems.

the class QuoteId method deserialize.

@Nullable
public static QuoteId deserialize(@NonNull Context context, @NonNull String serialized) {
    try {
        JSONObject json = new JSONObject(serialized);
        RecipientId id = json.has(AUTHOR) ? RecipientId.from(json.getString(AUTHOR)) : Recipient.external(context, json.getString(AUTHOR_DEPRECATED)).getId();
        return new QuoteId(json.getLong(ID), id);
    } catch (JSONException e) {
        Log.e(TAG, "Failed to deserialize from json", e);
        return null;
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) Nullable(androidx.annotation.Nullable)

Example 62 with RecipientId

use of org.thoughtcrime.securesms.recipients.RecipientId in project Signal-Android by WhisperSystems.

the class ConfirmPaymentRepository method confirmPayment.

@AnyThread
void confirmPayment(@NonNull ConfirmPaymentState state, @NonNull Consumer<ConfirmPaymentResult> consumer) {
    Log.i(TAG, "confirmPayment");
    SignalExecutors.BOUNDED.execute(() -> {
        Balance balance = wallet.getCachedBalance();
        if (state.getTotal().requireMobileCoin().greaterThan(balance.getFullAmount().requireMobileCoin())) {
            Log.w(TAG, "The total was greater than the wallet's balance");
            consumer.accept(new ConfirmPaymentResult.Error());
            return;
        }
        Payee payee = state.getPayee();
        RecipientId recipientId;
        MobileCoinPublicAddress mobileCoinPublicAddress;
        if (payee.hasRecipientId()) {
            recipientId = payee.requireRecipientId();
            try {
                mobileCoinPublicAddress = ProfileUtil.getAddressForRecipient(Recipient.resolved(recipientId));
            } catch (IOException e) {
                Log.w(TAG, "Failed to get address for recipient " + recipientId);
                consumer.accept(new ConfirmPaymentResult.Error());
                return;
            } catch (PaymentsAddressException e) {
                Log.w(TAG, "Failed to get address for recipient " + recipientId);
                consumer.accept(new ConfirmPaymentResult.Error(e.getCode()));
                return;
            }
        } else if (payee.hasPublicAddress()) {
            recipientId = null;
            mobileCoinPublicAddress = payee.requirePublicAddress();
        } else
            throw new AssertionError();
        UUID paymentUuid = PaymentSendJob.enqueuePayment(recipientId, mobileCoinPublicAddress, Util.emptyIfNull(state.getNote()), state.getAmount().requireMobileCoin(), state.getFee().requireMobileCoin());
        Log.i(TAG, "confirmPayment: PaymentSendJob enqueued");
        consumer.accept(new ConfirmPaymentResult.Success(paymentUuid));
    });
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) IOException(java.io.IOException) PaymentsAddressException(org.thoughtcrime.securesms.payments.PaymentsAddressException) UUID(java.util.UUID) Balance(org.thoughtcrime.securesms.payments.Balance) Payee(org.thoughtcrime.securesms.payments.Payee) MobileCoinPublicAddress(org.thoughtcrime.securesms.payments.MobileCoinPublicAddress) AnyThread(androidx.annotation.AnyThread)

Example 63 with RecipientId

use of org.thoughtcrime.securesms.recipients.RecipientId in project Signal-Android by WhisperSystems.

the class NotificationChannels method ensureCustomChannelConsistency.

@TargetApi(26)
@WorkerThread
public static synchronized void ensureCustomChannelConsistency(@NonNull Context context) {
    if (!supported()) {
        return;
    }
    Log.d(TAG, "ensureCustomChannelConsistency()");
    NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);
    RecipientDatabase db = SignalDatabase.recipients();
    List<Recipient> customRecipients = new ArrayList<>();
    Set<String> customChannelIds = new HashSet<>();
    try (RecipientDatabase.RecipientReader reader = db.getRecipientsWithNotificationChannels()) {
        Recipient recipient;
        while ((recipient = reader.getNext()) != null) {
            customRecipients.add(recipient);
            customChannelIds.add(recipient.getNotificationChannel());
        }
    }
    Set<String> existingChannelIds = Stream.of(notificationManager.getNotificationChannels()).map(NotificationChannel::getId).collect(Collectors.toSet());
    for (NotificationChannel existingChannel : notificationManager.getNotificationChannels()) {
        if ((existingChannel.getId().startsWith(CONTACT_PREFIX) || existingChannel.getId().startsWith(MESSAGES_PREFIX)) && Build.VERSION.SDK_INT >= CONVERSATION_SUPPORT_VERSION && existingChannel.getConversationId() != null) {
            if (customChannelIds.contains(existingChannel.getId())) {
                continue;
            }
            RecipientId id = ConversationUtil.getRecipientId(existingChannel.getConversationId());
            if (id != null) {
                Log.i(TAG, "Consistency: Conversation channel created outside of app, update " + id + " to use '" + existingChannel.getId() + "'");
                db.setNotificationChannel(id, existingChannel.getId());
            } else {
                Log.i(TAG, "Consistency: Conversation channel created outside of app with no matching recipient, deleting channel '" + existingChannel.getId() + "'");
                notificationManager.deleteNotificationChannel(existingChannel.getId());
            }
        } else if (existingChannel.getId().startsWith(CONTACT_PREFIX) && !customChannelIds.contains(existingChannel.getId())) {
            Log.i(TAG, "Consistency: Deleting channel '" + existingChannel.getId() + "' because the DB has no record of it.");
            notificationManager.deleteNotificationChannel(existingChannel.getId());
        } else if (existingChannel.getId().startsWith(MESSAGES_PREFIX) && !existingChannel.getId().equals(getMessagesChannel(context))) {
            Log.i(TAG, "Consistency: Deleting channel '" + existingChannel.getId() + "' because it's out of date.");
            notificationManager.deleteNotificationChannel(existingChannel.getId());
        }
    }
    for (Recipient customRecipient : customRecipients) {
        if (!existingChannelIds.contains(customRecipient.getNotificationChannel())) {
            Log.i(TAG, "Consistency: Removing custom channel '" + customRecipient.getNotificationChannel() + "' because the system doesn't have it.");
            db.setNotificationChannel(customRecipient.getId(), null);
        }
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) NotificationManager(android.app.NotificationManager) ArrayList(java.util.ArrayList) Recipient(org.thoughtcrime.securesms.recipients.Recipient) HashSet(java.util.HashSet) WorkerThread(androidx.annotation.WorkerThread) TargetApi(android.annotation.TargetApi)

Example 64 with RecipientId

use of org.thoughtcrime.securesms.recipients.RecipientId in project Signal-Android by WhisperSystems.

the class EditSelfProfileRepository method getCurrentAvatar.

@Override
public void getCurrentAvatar(@NonNull Consumer<byte[]> avatarConsumer) {
    RecipientId selfId = Recipient.self().getId();
    if (AvatarHelper.hasAvatar(context, selfId)) {
        SimpleTask.run(() -> {
            try {
                return StreamUtil.readFully(AvatarHelper.getAvatar(context, selfId));
            } catch (IOException e) {
                Log.w(TAG, e);
                return null;
            }
        }, avatarConsumer::accept);
    } else if (!excludeSystem) {
        SystemProfileUtil.getSystemProfileAvatar(context, new ProfileMediaConstraints()).addListener(new ListenableFuture.Listener<byte[]>() {

            @Override
            public void onSuccess(byte[] result) {
                avatarConsumer.accept(result);
            }

            @Override
            public void onFailure(ExecutionException e) {
                Log.w(TAG, e);
                avatarConsumer.accept(null);
            }
        });
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) ProfileMediaConstraints(org.thoughtcrime.securesms.profiles.ProfileMediaConstraints) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 65 with RecipientId

use of org.thoughtcrime.securesms.recipients.RecipientId in project Signal-Android by WhisperSystems.

the class ReactionsConversationView method buildSortedReactionsList.

@NonNull
private static List<Reaction> buildSortedReactionsList(@NonNull List<ReactionRecord> records) {
    Map<String, Reaction> counters = new LinkedHashMap<>();
    RecipientId selfId = Recipient.self().getId();
    for (ReactionRecord record : records) {
        String baseEmoji = EmojiUtil.getCanonicalRepresentation(record.getEmoji());
        Reaction info = counters.get(baseEmoji);
        if (info == null) {
            info = new Reaction(baseEmoji, record.getEmoji(), 1, record.getDateReceived(), selfId.equals(record.getAuthor()));
        } else {
            info.update(record.getEmoji(), record.getDateReceived(), selfId.equals(record.getAuthor()));
        }
        counters.put(baseEmoji, info);
    }
    List<Reaction> reactions = new ArrayList<>(counters.values());
    Collections.sort(reactions, Collections.reverseOrder());
    if (reactions.size() > 3) {
        List<Reaction> shortened = new ArrayList<>(3);
        shortened.add(reactions.get(0));
        shortened.add(reactions.get(1));
        shortened.add(Stream.of(reactions).skip(2).reduce(new Reaction(null, null, 0, 0, false), Reaction::merge));
        return shortened;
    } else {
        return reactions;
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) ArrayList(java.util.ArrayList) ReactionRecord(org.thoughtcrime.securesms.database.model.ReactionRecord) LinkedHashMap(java.util.LinkedHashMap) NonNull(androidx.annotation.NonNull)

Aggregations

RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)154 NonNull (androidx.annotation.NonNull)70 Recipient (org.thoughtcrime.securesms.recipients.Recipient)70 List (java.util.List)34 ArrayList (java.util.ArrayList)33 Nullable (androidx.annotation.Nullable)32 Context (android.content.Context)31 Log (org.signal.core.util.logging.Log)31 IOException (java.io.IOException)30 ApplicationDependencies (org.thoughtcrime.securesms.dependencies.ApplicationDependencies)29 WorkerThread (androidx.annotation.WorkerThread)28 Stream (com.annimon.stream.Stream)27 LinkedList (java.util.LinkedList)25 ContentValues (android.content.ContentValues)24 Cursor (android.database.Cursor)24 Collections (java.util.Collections)24 SignalDatabase (org.thoughtcrime.securesms.database.SignalDatabase)24 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)23 Optional (org.whispersystems.libsignal.util.guava.Optional)22 HashSet (java.util.HashSet)21