Search in sources :

Example 36 with Pair

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

the class SearchUtil method getStrictHighlightRanges.

static List<Pair<Integer, Integer>> getStrictHighlightRanges(@NonNull Locale locale, @NonNull String text, @NonNull String highlight) {
    if (text.length() == 0) {
        return Collections.emptyList();
    }
    String normalizedText = text.toLowerCase(locale);
    String normalizedHighlight = highlight.toLowerCase(locale);
    List<String> highlightTokens = Stream.of(normalizedHighlight.split("\\s")).filter(s -> s.trim().length() > 0).toList();
    List<Pair<Integer, Integer>> ranges = new LinkedList<>();
    int lastHighlightEndIndex = 0;
    for (String highlightToken : highlightTokens) {
        int index;
        do {
            index = normalizedText.indexOf(highlightToken, lastHighlightEndIndex);
            lastHighlightEndIndex = index + highlightToken.length();
        } while (index > 0 && !Character.isWhitespace(normalizedText.charAt(index - 1)));
        if (index >= 0) {
            ranges.add(new Pair<>(index, lastHighlightEndIndex));
        }
        if (index < 0 || lastHighlightEndIndex >= normalizedText.length()) {
            break;
        }
    }
    if (ranges.size() != highlightTokens.size()) {
        return Collections.emptyList();
    }
    return ranges;
}
Also used : Stream(com.annimon.stream.Stream) SpannableString(android.text.SpannableString) NonNull(androidx.annotation.NonNull) Spannable(android.text.Spannable) TextUtils(android.text.TextUtils) Pair(org.whispersystems.libsignal.util.Pair) List(java.util.List) Nullable(androidx.annotation.Nullable) InvalidParameterException(java.security.InvalidParameterException) Locale(java.util.Locale) CharacterStyle(android.text.style.CharacterStyle) LinkedList(java.util.LinkedList) Collections(java.util.Collections) SpannableString(android.text.SpannableString) LinkedList(java.util.LinkedList) Pair(org.whispersystems.libsignal.util.Pair)

Example 37 with Pair

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

the class SearchUtil method getHighlightRanges.

static List<Pair<Integer, Integer>> getHighlightRanges(@NonNull Locale locale, @NonNull String text, @NonNull String highlight) {
    if (text.length() == 0) {
        return Collections.emptyList();
    }
    String normalizedText = text.toLowerCase(locale);
    String normalizedHighlight = highlight.toLowerCase(locale);
    List<String> highlightTokens = Stream.of(normalizedHighlight.split("\\s")).filter(s -> s.trim().length() > 0).toList();
    List<Pair<Integer, Integer>> ranges = new LinkedList<>();
    int lastHighlightEndIndex = 0;
    for (String highlightToken : highlightTokens) {
        int index = 0;
        lastHighlightEndIndex = 0;
        while (index != -1) {
            index = normalizedText.indexOf(highlightToken, lastHighlightEndIndex);
            if (index != -1) {
                lastHighlightEndIndex = index + highlightToken.length();
                ranges.add(new Pair<>(index, lastHighlightEndIndex));
                index = lastHighlightEndIndex;
            }
        }
    }
    return ranges;
}
Also used : Stream(com.annimon.stream.Stream) SpannableString(android.text.SpannableString) NonNull(androidx.annotation.NonNull) Spannable(android.text.Spannable) TextUtils(android.text.TextUtils) Pair(org.whispersystems.libsignal.util.Pair) List(java.util.List) Nullable(androidx.annotation.Nullable) InvalidParameterException(java.security.InvalidParameterException) Locale(java.util.Locale) CharacterStyle(android.text.style.CharacterStyle) LinkedList(java.util.LinkedList) Collections(java.util.Collections) SpannableString(android.text.SpannableString) LinkedList(java.util.LinkedList) Pair(org.whispersystems.libsignal.util.Pair)

Example 38 with Pair

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

the class ChunkedDataFetcher method fetchChunks.

private void fetchChunks(@NonNull String url, long contentLength, Optional<Pair<InputStream, Long>> firstChunk, CompositeRequestController compositeController, Callback callback) {
    List<ByteRange> requestPattern;
    try {
        if (firstChunk.isPresent()) {
            requestPattern = Stream.of(getRequestPattern(contentLength - firstChunk.get().second())).map(b -> new ByteRange(b.start + firstChunk.get().second(), b.end + firstChunk.get().second(), b.ignoreFirst)).toList();
        } else {
            requestPattern = getRequestPattern(contentLength);
        }
    } catch (IOException e) {
        callback.onFailure(e);
        compositeController.cancel();
        return;
    }
    SignalExecutors.UNBOUNDED.execute(() -> {
        List<CallRequestController> controllers = Stream.of(requestPattern).map(range -> makeChunkRequest(client, url, range)).toList();
        List<InputStream> streams = new ArrayList<>(controllers.size() + (firstChunk.isPresent() ? 1 : 0));
        if (firstChunk.isPresent()) {
            streams.add(firstChunk.get().first());
        }
        Stream.of(controllers).forEach(compositeController::addController);
        for (CallRequestController controller : controllers) {
            Optional<InputStream> stream = controller.getStream();
            if (!stream.isPresent()) {
                Log.w(TAG, "Stream was canceled.");
                callback.onFailure(new IOException("Failure"));
                compositeController.cancel();
                return;
            }
            streams.add(stream.get());
        }
        try {
            callback.onSuccess(new InputStreamList(streams));
        } catch (IOException e) {
            callback.onFailure(e);
            compositeController.cancel();
        }
    });
}
Also used : Request(okhttp3.Request) Stream(com.annimon.stream.Stream) Util(org.thoughtcrime.securesms.util.Util) NonNull(androidx.annotation.NonNull) TextUtils(android.text.TextUtils) ContentLengthInputStream(com.bumptech.glide.util.ContentLengthInputStream) IOException(java.io.IOException) CacheControl(okhttp3.CacheControl) Optional(org.whispersystems.libsignal.util.guava.Optional) ArrayList(java.util.ArrayList) SecureRandom(java.security.SecureRandom) Log(org.signal.core.util.logging.Log) Pair(org.whispersystems.libsignal.util.Pair) FilterInputStream(java.io.FilterInputStream) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) Response(okhttp3.Response) Call(okhttp3.Call) LinkedList(java.util.LinkedList) SignalExecutors(org.signal.core.util.concurrent.SignalExecutors) InputStream(java.io.InputStream) ContentLengthInputStream(com.bumptech.glide.util.ContentLengthInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 39 with Pair

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

the class SmsDatabase method setTimestampRead.

@Override
@NonNull
MmsSmsDatabase.TimestampReadResult setTimestampRead(SyncMessageId messageId, long proposedExpireStarted, @NonNull Map<Long, Long> threadToLatestRead) {
    SQLiteDatabase database = databaseHelper.getSignalWritableDatabase();
    List<Pair<Long, Long>> expiring = new LinkedList<>();
    String[] projection = new String[] { ID, THREAD_ID, RECIPIENT_ID, TYPE, EXPIRES_IN, EXPIRE_STARTED };
    String query = DATE_SENT + " = ?";
    String[] args = SqlUtil.buildArgs(messageId.getTimetamp());
    List<Long> threads = new LinkedList<>();
    try (Cursor cursor = database.query(TABLE_NAME, projection, query, args, null, null, null)) {
        while (cursor.moveToNext()) {
            RecipientId theirRecipientId = messageId.getRecipientId();
            RecipientId outRecipientId = RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(RECIPIENT_ID)));
            if (outRecipientId.equals(theirRecipientId) || theirRecipientId.equals(Recipient.self().getId())) {
                long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
                long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID));
                long expiresIn = cursor.getLong(cursor.getColumnIndexOrThrow(EXPIRES_IN));
                long expireStarted = cursor.getLong(cursor.getColumnIndexOrThrow(EXPIRE_STARTED));
                expireStarted = expireStarted > 0 ? Math.min(proposedExpireStarted, expireStarted) : proposedExpireStarted;
                ContentValues contentValues = new ContentValues();
                contentValues.put(READ, 1);
                contentValues.put(REACTIONS_UNREAD, 0);
                contentValues.put(REACTIONS_LAST_SEEN, System.currentTimeMillis());
                if (expiresIn > 0) {
                    contentValues.put(EXPIRE_STARTED, expireStarted);
                    expiring.add(new Pair<>(id, expiresIn));
                }
                database.update(TABLE_NAME, contentValues, ID_WHERE, SqlUtil.buildArgs(id));
                threads.add(threadId);
                Long latest = threadToLatestRead.get(threadId);
                threadToLatestRead.put(threadId, (latest != null) ? Math.max(latest, messageId.getTimetamp()) : messageId.getTimetamp());
            }
        }
    }
    return new MmsSmsDatabase.TimestampReadResult(expiring, threads);
}
Also used : ContentValues(android.content.ContentValues) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) Cursor(android.database.Cursor) LinkedList(java.util.LinkedList) Pair(org.whispersystems.libsignal.util.Pair) NonNull(androidx.annotation.NonNull)

Example 40 with Pair

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

the class SmsDatabase method insertCallLog.

@NonNull
private Pair<Long, Long> insertCallLog(@NonNull RecipientId recipientId, long type, boolean unread, long timestamp) {
    Recipient recipient = Recipient.resolved(recipientId);
    long threadId = SignalDatabase.threads().getOrCreateThreadIdFor(recipient);
    ContentValues values = new ContentValues(6);
    values.put(RECIPIENT_ID, recipientId.serialize());
    values.put(ADDRESS_DEVICE_ID, 1);
    values.put(DATE_RECEIVED, System.currentTimeMillis());
    values.put(DATE_SENT, timestamp);
    values.put(READ, unread ? 0 : 1);
    values.put(TYPE, type);
    values.put(THREAD_ID, threadId);
    SQLiteDatabase db = databaseHelper.getSignalWritableDatabase();
    long messageId = db.insert(TABLE_NAME, null, values);
    if (unread) {
        SignalDatabase.threads().incrementUnread(threadId, 1);
    }
    SignalDatabase.threads().update(threadId, true);
    notifyConversationListeners(threadId);
    TrimThreadJob.enqueueAsync(threadId);
    return new Pair<>(messageId, threadId);
}
Also used : ContentValues(android.content.ContentValues) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Pair(org.whispersystems.libsignal.util.Pair) NonNull(androidx.annotation.NonNull)

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