Java Code Examples for org.whispersystems.libsignal.util.guava.Optional#or()

The following examples show how to use org.whispersystems.libsignal.util.guava.Optional#or() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PushProcessMessageJob.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static Optional<List<LinkPreview>> getLinkPreviews(Optional<List<Preview>> previews, @NonNull String message) {
  if (!previews.isPresent()) return Optional.absent();

  List<LinkPreview> linkPreviews = new ArrayList<>(previews.get().size());

  for (Preview preview : previews.get()) {
    Optional<Attachment> thumbnail     = PointerAttachment.forPointer(preview.getImage());
    Optional<String>     url           = Optional.fromNullable(preview.getUrl());
    Optional<String>     title         = Optional.fromNullable(preview.getTitle());
    boolean              hasContent    = !TextUtils.isEmpty(title.or("")) || thumbnail.isPresent();
    boolean              presentInBody = url.isPresent() && Stream.of(LinkPreviewUtil.findWhitelistedUrls(message)).map(Link::getUrl).collect(Collectors.toSet()).contains(url.get());
    boolean              validDomain   = url.isPresent() && LinkPreviewUtil.isWhitelistedLinkUrl(url.get());

    if (hasContent && presentInBody && validDomain) {
      LinkPreview linkPreview = new LinkPreview(url.get(), title.or(""), thumbnail);
      linkPreviews.add(linkPreview);
    } else {
      Log.w(TAG, String.format("Discarding an invalid link preview. hasContent: %b presentInBody: %b validDomain: %b", hasContent, presentInBody, validDomain));
    }
  }

  return Optional.of(linkPreviews);
}
 
Example 2
Source File: ImageSlide.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
protected static Attachment doneAttachmentFromUri(@NonNull Context context,
                                                  Uri uri,
                                                  @NonNull String defaultMime,
                                                  long size,
                                                  boolean hasThumbnail,
                                                  @Nullable String fileName,
                                                  boolean voiceNote,
                                                  int status) {
    try {
        Optional<String> resolvedType = Optional.fromNullable(MediaUtil.getMimeType(context, uri));
        String fastPreflightId = String.valueOf(SecureRandom.getInstance("SHA1PRNG").nextLong());
        UriAttachment attachment;
        if (status == DONE) {
            attachment = new UriAttachment(uri, hasThumbnail ? uri : null, resolvedType.or(defaultMime), AttachmentDbModel.TransferState.DONE.getState(), size, fileName, fastPreflightId, voiceNote);
        } else if (status == PENDING) {
            attachment = new UriAttachment(uri, hasThumbnail ? uri : null, resolvedType.or(defaultMime), AttachmentDbModel.TransferState.PENDING.getState(), size, fileName, fastPreflightId, voiceNote);
        } else {
            attachment = (UriAttachment) constructAttachmentFromUri(context, uri, resolvedType.or(defaultMime), size, true, fileName, voiceNote);
        }
        return attachment;
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
}
 
Example 3
Source File: Slide.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
protected static Attachment constructAttachmentFromUri(@NonNull Context context,
                                                       Uri uri,
                                                       @NonNull String defaultMime,
                                                       long size,
                                                       boolean hasThumbnail,
                                                       @Nullable String fileName,
                                                       boolean voiceNote) {
    try {
        Optional<String> resolvedType = Optional.fromNullable(MediaUtil.getMimeType(context, uri));
        String fastPreflightId = String.valueOf(SecureRandom.getInstance("SHA1PRNG").nextLong());
        return new UriAttachment(uri, hasThumbnail ? uri : null, resolvedType.or(defaultMime), AttachmentDbModel.TransferState.STARTED.getState(), size, fileName, fastPreflightId, voiceNote);
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
}
 
Example 4
Source File: DocumentSlide.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
protected static Attachment doneAttachmentFromUri(@NonNull Context context,
                                                  Uri uri,
                                                  @NonNull String defaultMime,
                                                  long size,
                                                  boolean hasThumbnail,
                                                  @Nullable String fileName,
                                                  boolean voiceNote,
                                                  int status) {
    try {
        Optional<String> resolvedType = Optional.fromNullable(MediaUtil.getMimeType(context, uri));
        if (!resolvedType.isPresent() && fileName != null && !fileName.isEmpty()) {
            int index = fileName.lastIndexOf(".");
            if (index >= 0) {
                String suffix = fileName.substring(++index);
                resolvedType = Optional.fromNullable(MimeTypeMap.getSingleton().getMimeTypeFromExtension(suffix));
            }
        }
        String fastPreflightId = String.valueOf(SecureRandom.getInstance("SHA1PRNG").nextLong());
        UriAttachment attachment;
        if (status == STATED) {
            attachment = new UriAttachment(uri, hasThumbnail ? uri : null, resolvedType.or(defaultMime), AttachmentDbModel.TransferState.STARTED.getState(), size, fileName, fastPreflightId, voiceNote);
        } else if (status == PENDING) {
            attachment = new UriAttachment(uri, hasThumbnail ? uri : null, resolvedType.or(defaultMime), AttachmentDbModel.TransferState.PENDING.getState(), size, fileName, fastPreflightId, voiceNote);
        } else {
            attachment = new UriAttachment(uri, hasThumbnail ? uri : null, resolvedType.or(defaultMime), AttachmentDbModel.TransferState.DONE.getState(), size, fileName, fastPreflightId, voiceNote);
        }
        return attachment;
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
}
 
Example 5
Source File: Slide.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
protected static Attachment constructAttachmentFromUri(@NonNull Context context,
                                                       @NonNull Uri     uri,
                                                       @NonNull String  defaultMime,
                                                                long     size,
                                                                boolean  hasThumbnail)
{
  Optional<String> resolvedType = Optional.fromNullable(MediaUtil.getMimeType(context, uri));
  return new UriAttachment(uri, hasThumbnail ? uri : null, resolvedType.or(defaultMime), AttachmentDatabase.TRANSFER_PROGRESS_STARTED, size);
}
 
Example 6
Source File: PushProcessMessageJob.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private long handleSynchronizeSentMediaMessage(@NonNull SentTranscriptMessage message)
    throws MmsException, BadGroupIdException
{
  MmsDatabase                 database        = DatabaseFactory.getMmsDatabase(context);
  Recipient                   recipients      = getSyncMessageDestination(message);
  Optional<QuoteModel>        quote           = getValidatedQuote(message.getMessage().getQuote());
  Optional<Attachment>        sticker         = getStickerAttachment(message.getMessage().getSticker());
  Optional<List<Contact>>     sharedContacts  = getContacts(message.getMessage().getSharedContacts());
  Optional<List<LinkPreview>> previews        = getLinkPreviews(message.getMessage().getPreviews(), message.getMessage().getBody().or(""));
  boolean                     viewOnce        = message.getMessage().isViewOnce();
  List<Attachment>            syncAttachments = viewOnce ? Collections.singletonList(new TombstoneAttachment(MediaUtil.VIEW_ONCE, false))
                                                         : PointerAttachment.forPointers(message.getMessage().getAttachments());

  if (sticker.isPresent()) {
    syncAttachments.add(sticker.get());
  }

  OutgoingMediaMessage mediaMessage = new OutgoingMediaMessage(recipients, message.getMessage().getBody().orNull(),
                                                               syncAttachments,
                                                               message.getTimestamp(), -1,
                                                               message.getMessage().getExpiresInSeconds() * 1000,
                                                               viewOnce,
                                                               ThreadDatabase.DistributionTypes.DEFAULT, quote.orNull(),
                                                               sharedContacts.or(Collections.emptyList()),
                                                               previews.or(Collections.emptyList()),
                                                               Collections.emptyList(), Collections.emptyList());

  mediaMessage = new OutgoingSecureMediaMessage(mediaMessage);

  if (recipients.getExpireMessages() != message.getMessage().getExpiresInSeconds()) {
    handleSynchronizeSentExpirationUpdate(message);
  }

  long threadId  = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);

  database.beginTransaction();

  try {
    long messageId = database.insertMessageOutbox(mediaMessage, threadId, false, GroupReceiptDatabase.STATUS_UNKNOWN, null);

    if (recipients.isGroup()) {
      updateGroupReceiptStatus(message, messageId, recipients.requireGroupId());
    } else {
      database.markUnidentified(messageId, isUnidentified(message, recipients));
    }

    database.markAsSent(messageId, true);

    List<DatabaseAttachment> allAttachments     = DatabaseFactory.getAttachmentDatabase(context).getAttachmentsForMessage(messageId);
    List<DatabaseAttachment> stickerAttachments = Stream.of(allAttachments).filter(Attachment::isSticker).toList();
    List<DatabaseAttachment> attachments        = Stream.of(allAttachments).filterNot(Attachment::isSticker).toList();

    forceStickerDownloadIfNecessary(stickerAttachments);

    for (DatabaseAttachment attachment : attachments) {
      ApplicationDependencies.getJobManager().add(new AttachmentDownloadJob(messageId, attachment.getAttachmentId(), false));
    }

    if (message.getMessage().getExpiresInSeconds() > 0) {
      database.markExpireStarted(messageId, message.getExpirationStartTimestamp());
      ApplicationContext.getInstance(context)
                        .getExpiringMessageManager()
                        .scheduleDeletion(messageId, true,
                                          message.getExpirationStartTimestamp(),
                                          message.getMessage().getExpiresInSeconds() * 1000L);
    }

    if (recipients.isLocalNumber()) {
      SyncMessageId id = new SyncMessageId(recipients.getId(), message.getTimestamp());
      DatabaseFactory.getMmsSmsDatabase(context).incrementDeliveryReceiptCount(id, System.currentTimeMillis());
      DatabaseFactory.getMmsSmsDatabase(context).incrementReadReceiptCount(id, System.currentTimeMillis());
    }

    database.setTransactionSuccessful();
  } finally {
    database.endTransaction();
  }
  return threadId;
}