Search in sources :

Example 76 with WorkerThread

use of androidx.annotation.WorkerThread in project Signal-Android by WhisperSystems.

the class VoiceNoteMediaItemFactory method buildMediaItem.

/**
 * Build out a MediaItem for a given voice note. Expects to be run
 * on a background thread.
 *
 * @param context       Context.
 * @param messageRecord The MessageRecord of the given voice note.
 * @return A MediaItem with all the details the service expects.
 */
@WorkerThread
@Nullable
static MediaItem buildMediaItem(@NonNull Context context, @NonNull MessageRecord messageRecord) {
    int startingPosition = SignalDatabase.mmsSms().getMessagePositionInConversation(messageRecord.getThreadId(), messageRecord.getDateReceived());
    Recipient threadRecipient = Objects.requireNonNull(SignalDatabase.threads().getRecipientForThreadId(messageRecord.getThreadId()));
    Recipient sender = messageRecord.isOutgoing() ? Recipient.self() : messageRecord.getIndividualRecipient();
    Recipient avatarRecipient = threadRecipient.isGroup() ? threadRecipient : sender;
    AudioSlide audioSlide = ((MmsMessageRecord) messageRecord).getSlideDeck().getAudioSlide();
    if (audioSlide == null) {
        Log.w(TAG, "Message does not have an audio slide. Can't play this voice note.");
        return null;
    }
    Uri uri = audioSlide.getUri();
    if (uri == null) {
        Log.w(TAG, "Audio slide does not have a URI. Can't play this voice note.");
        return null;
    }
    return buildMediaItem(context, threadRecipient, avatarRecipient, sender, startingPosition, messageRecord.getThreadId(), messageRecord.getId(), messageRecord.getDateReceived(), uri);
}
Also used : AudioSlide(org.thoughtcrime.securesms.mms.AudioSlide) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Uri(android.net.Uri) WorkerThread(androidx.annotation.WorkerThread) Nullable(androidx.annotation.Nullable)

Example 77 with WorkerThread

use of androidx.annotation.WorkerThread in project Signal-Android by WhisperSystems.

the class GroupsV2CapabilityChecker method allHaveUuidAndSupportGroupsV2.

@WorkerThread
static boolean allHaveUuidAndSupportGroupsV2(@NonNull Collection<RecipientId> recipientIds) throws IOException {
    Set<RecipientId> recipientIdsSet = new HashSet<>(recipientIds);
    refreshCapabilitiesIfNecessary(Recipient.resolvedList(recipientIdsSet));
    boolean noSelfGV2Support = false;
    int noGv2Count = 0;
    int noUuidCount = 0;
    for (RecipientId id : recipientIds) {
        Recipient member = Recipient.resolved(id);
        Recipient.Capability gv2Capability = member.getGroupsV2Capability();
        if (gv2Capability != Recipient.Capability.SUPPORTED) {
            Log.w(TAG, "At least one recipient does not support GV2, capability was " + gv2Capability);
            noGv2Count++;
            if (member.isSelf()) {
                noSelfGV2Support = true;
            }
        }
        if (!member.hasServiceId()) {
            noUuidCount++;
        }
    }
    if (noGv2Count + noUuidCount > 0) {
        if (noUuidCount > 0) {
            Log.w(TAG, noUuidCount + " recipient(s) did not have a UUID known to us");
        }
        if (noGv2Count > 0) {
            Log.w(TAG, noGv2Count + " recipient(s) do not support GV2");
            if (noSelfGV2Support) {
                Log.w(TAG, "Self does not support GV2");
            }
        }
        return false;
    }
    return true;
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) Recipient(org.thoughtcrime.securesms.recipients.Recipient) HashSet(java.util.HashSet) WorkerThread(androidx.annotation.WorkerThread)

Example 78 with WorkerThread

use of androidx.annotation.WorkerThread in project Signal-Android by WhisperSystems.

the class GroupsV2CapabilityChecker method refreshCapabilitiesIfNecessary.

/**
 * @param resolved A collection of resolved recipients.
 * @return True if a recipient needed to be refreshed, otherwise false.
 */
@WorkerThread
public static boolean refreshCapabilitiesIfNecessary(@NonNull Collection<Recipient> resolved) throws IOException {
    Set<RecipientId> needsRefresh = Stream.of(resolved).filter(r -> r.getGroupsV2Capability() != Recipient.Capability.SUPPORTED).map(Recipient::getId).collect(Collectors.toSet());
    if (needsRefresh.size() > 0) {
        Log.d(TAG, "[refreshCapabilitiesIfNecessary] Need to refresh " + needsRefresh.size() + " recipients.");
        List<Job> jobs = RetrieveProfileJob.forRecipients(needsRefresh);
        JobManager jobManager = ApplicationDependencies.getJobManager();
        for (Job job : jobs) {
            if (!jobManager.runSynchronously(job, TimeUnit.SECONDS.toMillis(10)).isPresent()) {
                throw new IOException("Recipient capability was not retrieved in time");
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) JobManager(org.thoughtcrime.securesms.jobmanager.JobManager) IOException(java.io.IOException) RetrieveProfileJob(org.thoughtcrime.securesms.jobs.RetrieveProfileJob) Job(org.thoughtcrime.securesms.jobmanager.Job) WorkerThread(androidx.annotation.WorkerThread)

Example 79 with WorkerThread

use of androidx.annotation.WorkerThread in project Signal-Android by WhisperSystems.

the class CameraContactsRepository method getContacts.

@WorkerThread
@NonNull
private List<Recipient> getContacts(@NonNull String query) {
    List<Recipient> recipients = new ArrayList<>();
    try (Cursor cursor = contactRepository.querySignalContacts(query)) {
        while (cursor.moveToNext()) {
            RecipientId id = RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ContactRepository.ID_COLUMN)));
            Recipient recipient = Recipient.resolved(id);
            recipients.add(recipient);
        }
    }
    return recipients;
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) ArrayList(java.util.ArrayList) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Cursor(android.database.Cursor) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Example 80 with WorkerThread

use of androidx.annotation.WorkerThread in project Signal-Android by WhisperSystems.

the class ImageEditorModelRenderMediaTransform method transform.

@WorkerThread
@Override
@NonNull
public Media transform(@NonNull Context context, @NonNull Media media) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Bitmap bitmap = modelToRender.render(context, size);
    try {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
        Uri uri = BlobProvider.getInstance().forData(outputStream.toByteArray()).withMimeType(MediaUtil.IMAGE_JPEG).createForSingleSessionOnDisk(context);
        return new Media(uri, MediaUtil.IMAGE_JPEG, media.getDate(), bitmap.getWidth(), bitmap.getHeight(), outputStream.size(), 0, false, false, media.getBucketId(), media.getCaption(), Optional.absent());
    } catch (IOException e) {
        Log.w(TAG, "Failed to render image. Using base image.");
        return media;
    } finally {
        bitmap.recycle();
        StreamUtil.close(outputStream);
    }
}
Also used : Bitmap(android.graphics.Bitmap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Uri(android.net.Uri) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Aggregations

WorkerThread (androidx.annotation.WorkerThread)365 NonNull (androidx.annotation.NonNull)151 IOException (java.io.IOException)83 Recipient (org.thoughtcrime.securesms.recipients.Recipient)82 Nullable (androidx.annotation.Nullable)51 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)46 Cursor (android.database.Cursor)41 ArrayList (java.util.ArrayList)41 Context (android.content.Context)39 Uri (android.net.Uri)32 LinkedList (java.util.LinkedList)30 List (java.util.List)30 RecipientDatabase (org.thoughtcrime.securesms.database.RecipientDatabase)28 Stream (com.annimon.stream.Stream)26 HashMap (java.util.HashMap)26 Log (org.signal.core.util.logging.Log)26 HashSet (java.util.HashSet)24 Map (java.util.Map)21 Collections (java.util.Collections)20 ApplicationDependencies (org.thoughtcrime.securesms.dependencies.ApplicationDependencies)20