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;
}
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;
}
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();
}
});
}
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);
}
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);
}
Aggregations