Search in sources :

Example 11 with Pair

use of org.whispersystems.libsignal.util.Pair in project libsignal-service-java by signalapp.

the class WebSocketConnection method onClosed.

@Override
public synchronized void onClosed(WebSocket webSocket, int code, String reason) {
    Log.w(TAG, "onClose()...");
    this.connected = false;
    Iterator<Map.Entry<Long, SettableFuture<Pair<Integer, String>>>> iterator = outgoingRequests.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<Long, SettableFuture<Pair<Integer, String>>> entry = iterator.next();
        entry.getValue().setException(new IOException("Closed: " + code + ", " + reason));
        iterator.remove();
    }
    if (keepAliveSender != null) {
        keepAliveSender.shutdown();
        keepAliveSender = null;
    }
    if (listener != null) {
        listener.onDisconnected();
    }
    Util.wait(this, Math.min(++attempts * 200, TimeUnit.SECONDS.toMillis(15)));
    if (client != null) {
        client.close(1000, "OK");
        client = null;
        connected = false;
        connect();
    }
    notifyAll();
}
Also used : SettableFuture(org.whispersystems.signalservice.internal.util.concurrent.SettableFuture) ByteString(okio.ByteString) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) Pair(org.whispersystems.libsignal.util.Pair)

Example 12 with Pair

use of org.whispersystems.libsignal.util.Pair in project libsignal-service-java by signalapp.

the class WebSocketConnection method createTlsSocketFactory.

private Pair<SSLSocketFactory, X509TrustManager> createTlsSocketFactory(TrustStore trustStore) {
    try {
        SSLContext context = SSLContext.getInstance("TLS");
        TrustManager[] trustManagers = BlacklistingTrustManager.createFor(trustStore);
        context.init(null, trustManagers, null);
        return new Pair<>(context.getSocketFactory(), (X509TrustManager) trustManagers[0]);
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new AssertionError(e);
    }
}
Also used : SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) BlacklistingTrustManager(org.whispersystems.signalservice.internal.util.BlacklistingTrustManager) Pair(org.whispersystems.libsignal.util.Pair)

Example 13 with Pair

use of org.whispersystems.libsignal.util.Pair in project Signal-Android by signalapp.

the class ConversationParentFragment method initializeDraftFromDatabase.

private ListenableFuture<Boolean> initializeDraftFromDatabase() {
    SettableFuture<Boolean> future = new SettableFuture<>();
    final Context context = requireContext().getApplicationContext();
    new AsyncTask<Void, Void, Pair<Drafts, CharSequence>>() {

        @Override
        protected Pair<Drafts, CharSequence> doInBackground(Void... params) {
            DraftDatabase draftDatabase = SignalDatabase.drafts();
            Drafts results = draftDatabase.getDrafts(threadId);
            Draft mentionsDraft = results.getDraftOfType(Draft.MENTION);
            Spannable updatedText = null;
            if (mentionsDraft != null) {
                String text = results.getDraftOfType(Draft.TEXT).getValue();
                List<Mention> mentions = MentionUtil.bodyRangeListToMentions(context, Base64.decodeOrThrow(mentionsDraft.getValue()));
                UpdatedBodyAndMentions updated = MentionUtil.updateBodyAndMentionsWithDisplayNames(context, text, mentions);
                updatedText = new SpannableString(updated.getBody());
                MentionAnnotation.setMentionAnnotations(updatedText, updated.getMentions());
            }
            draftDatabase.clearDrafts(threadId);
            return new Pair<>(results, updatedText);
        }

        @Override
        protected void onPostExecute(Pair<Drafts, CharSequence> draftsWithUpdatedMentions) {
            Drafts drafts = Objects.requireNonNull(draftsWithUpdatedMentions.first());
            CharSequence updatedText = draftsWithUpdatedMentions.second();
            if (drafts.isEmpty()) {
                future.set(false);
                updateToggleButtonState();
                return;
            }
            AtomicInteger draftsRemaining = new AtomicInteger(drafts.size());
            AtomicBoolean success = new AtomicBoolean(false);
            ListenableFuture.Listener<Boolean> listener = new AssertedSuccessListener<Boolean>() {

                @Override
                public void onSuccess(Boolean result) {
                    success.compareAndSet(false, result);
                    if (draftsRemaining.decrementAndGet() <= 0) {
                        future.set(success.get());
                    }
                }
            };
            for (Draft draft : drafts) {
                try {
                    switch(draft.getType()) {
                        case Draft.TEXT:
                            composeText.setText(updatedText == null ? draft.getValue() : updatedText);
                            listener.onSuccess(true);
                            break;
                        case Draft.LOCATION:
                            attachmentManager.setLocation(SignalPlace.deserialize(draft.getValue()), getCurrentMediaConstraints()).addListener(listener);
                            break;
                        case Draft.IMAGE:
                            setMedia(Uri.parse(draft.getValue()), MediaType.IMAGE).addListener(listener);
                            break;
                        case Draft.AUDIO:
                            setMedia(Uri.parse(draft.getValue()), MediaType.AUDIO).addListener(listener);
                            break;
                        case Draft.VIDEO:
                            setMedia(Uri.parse(draft.getValue()), MediaType.VIDEO).addListener(listener);
                            break;
                        case Draft.QUOTE:
                            SettableFuture<Boolean> quoteResult = new SettableFuture<>();
                            new QuoteRestorationTask(draft.getValue(), quoteResult).execute();
                            quoteResult.addListener(listener);
                            break;
                        case Draft.VOICE_NOTE:
                            draftViewModel.setVoiceNoteDraft(recipient.getId(), draft);
                            break;
                    }
                } catch (IOException e) {
                    Log.w(TAG, e);
                }
            }
            updateToggleButtonState();
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    return future;
}
Also used : SettableFuture(org.thoughtcrime.securesms.util.concurrent.SettableFuture) EmojiEventListener(org.thoughtcrime.securesms.components.emoji.EmojiEventListener) OnFocusChangeListener(android.view.View.OnFocusChangeListener) OnKeyListener(android.view.View.OnKeyListener) StickerEventListener(org.thoughtcrime.securesms.stickers.StickerEventListener) AssertedSuccessListener(org.thoughtcrime.securesms.util.concurrent.AssertedSuccessListener) OnKeyboardShownListener(org.thoughtcrime.securesms.components.KeyboardAwareLinearLayout.OnKeyboardShownListener) OnClickListener(android.view.View.OnClickListener) DraftDatabase(org.thoughtcrime.securesms.database.DraftDatabase) SpannableString(android.text.SpannableString) ArrayList(java.util.ArrayList) IdentityRecordList(org.thoughtcrime.securesms.database.identity.IdentityRecordList) List(java.util.List) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Pair(org.whispersystems.libsignal.util.Pair) Context(android.content.Context) UpdatedBodyAndMentions(org.thoughtcrime.securesms.database.MentionUtil.UpdatedBodyAndMentions) IOException(java.io.IOException) Drafts(org.thoughtcrime.securesms.database.DraftDatabase.Drafts) SpannableString(android.text.SpannableString) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VoiceNoteDraft(org.thoughtcrime.securesms.components.voice.VoiceNoteDraft) Draft(org.thoughtcrime.securesms.database.DraftDatabase.Draft) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Spannable(android.text.Spannable)

Example 14 with Pair

use of org.whispersystems.libsignal.util.Pair in project Signal-Android by signalapp.

the class MmsDatabase method insertMessageInbox.

public Pair<Long, Long> insertMessageInbox(@NonNull NotificationInd notification, int subscriptionId) {
    SQLiteDatabase db = databaseHelper.getSignalWritableDatabase();
    long threadId = getThreadIdFor(notification);
    ContentValues contentValues = new ContentValues();
    ContentValuesBuilder contentBuilder = new ContentValuesBuilder(contentValues);
    Log.i(TAG, "Message received type: " + notification.getMessageType());
    contentBuilder.add(CONTENT_LOCATION, notification.getContentLocation());
    contentBuilder.add(DATE_SENT, System.currentTimeMillis());
    contentBuilder.add(EXPIRY, notification.getExpiry());
    contentBuilder.add(MESSAGE_SIZE, notification.getMessageSize());
    contentBuilder.add(TRANSACTION_ID, notification.getTransactionId());
    contentBuilder.add(MESSAGE_TYPE, notification.getMessageType());
    if (notification.getFrom() != null) {
        Recipient recipient = Recipient.external(context, Util.toIsoString(notification.getFrom().getTextString()));
        contentValues.put(RECIPIENT_ID, recipient.getId().serialize());
    } else {
        contentValues.put(RECIPIENT_ID, RecipientId.UNKNOWN.serialize());
    }
    contentValues.put(MESSAGE_BOX, Types.BASE_INBOX_TYPE);
    contentValues.put(THREAD_ID, threadId);
    contentValues.put(STATUS, Status.DOWNLOAD_INITIALIZED);
    contentValues.put(DATE_RECEIVED, generatePduCompatTimestamp(System.currentTimeMillis()));
    contentValues.put(READ, Util.isDefaultSmsProvider(context) ? 0 : 1);
    contentValues.put(SUBSCRIPTION_ID, subscriptionId);
    if (!contentValues.containsKey(DATE_SENT))
        contentValues.put(DATE_SENT, contentValues.getAsLong(DATE_RECEIVED));
    long messageId = db.insert(TABLE_NAME, null, contentValues);
    return new Pair<>(messageId, threadId);
}
Also used : ContentValues(android.content.ContentValues) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Pair(org.whispersystems.libsignal.util.Pair)

Example 15 with Pair

use of org.whispersystems.libsignal.util.Pair in project Signal-Android by signalapp.

the class MmsDatabase method getOldestUnreadMentionDetails.

@Override
@Nullable
public Pair<RecipientId, Long> getOldestUnreadMentionDetails(long threadId) {
    SQLiteDatabase database = databaseHelper.getSignalReadableDatabase();
    String[] projection = new String[] { RECIPIENT_ID, DATE_RECEIVED };
    String selection = THREAD_ID + " = ? AND " + READ + " = 0 AND " + MENTIONS_SELF + " = 1";
    String[] args = SqlUtil.buildArgs(threadId);
    try (Cursor cursor = database.query(TABLE_NAME, projection, selection, args, null, null, DATE_RECEIVED + " ASC", "1")) {
        if (cursor != null && cursor.moveToFirst()) {
            return new Pair<>(RecipientId.from(CursorUtil.requireString(cursor, RECIPIENT_ID)), CursorUtil.requireLong(cursor, DATE_RECEIVED));
        }
    }
    return null;
}
Also used : Cursor(android.database.Cursor) Pair(org.whispersystems.libsignal.util.Pair) Nullable(androidx.annotation.Nullable)

Aggregations

Pair (org.whispersystems.libsignal.util.Pair)49 NonNull (androidx.annotation.NonNull)26 List (java.util.List)18 LinkedList (java.util.LinkedList)15 Context (android.content.Context)14 Stream (com.annimon.stream.Stream)14 IOException (java.io.IOException)14 Recipient (org.thoughtcrime.securesms.recipients.Recipient)14 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)14 Nullable (androidx.annotation.Nullable)12 SpannableString (android.text.SpannableString)10 TextUtils (android.text.TextUtils)10 ArrayList (java.util.ArrayList)10 Collections (java.util.Collections)10 Cursor (android.database.Cursor)9 ContentValues (android.content.ContentValues)8 HashSet (java.util.HashSet)8 Locale (java.util.Locale)8 Set (java.util.Set)8 Log (org.signal.core.util.logging.Log)8