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

The following examples show how to use org.whispersystems.libsignal.util.guava.Optional#absent() . 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: MultiDeviceContactUpdateJob.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private Optional<SignalServiceAttachmentStream> getProfileAvatar(@NonNull RecipientId recipientId) {
  if (AvatarHelper.hasAvatar(context, recipientId)) {
    try {
      long length = AvatarHelper.getAvatarLength(context, recipientId);
      return Optional.of(SignalServiceAttachmentStream.newStreamBuilder()
                                                      .withStream(AvatarHelper.getAvatar(context, recipientId))
                                                      .withContentType("image/*")
                                                      .withLength(length)
                                                      .build());
    } catch (IOException e) {
      Log.w(TAG, "Failed to read profile avatar!", e);
      return Optional.absent();
    }
  }

  return Optional.absent();
}
 
Example 2
Source File: SignalServiceCipher.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
private SignalServiceTypingMessage createTypingMessage(Metadata metadata, TypingMessage content) throws ProtocolInvalidMessageException {
  SignalServiceTypingMessage.Action action;

  if      (content.getAction() == TypingMessage.Action.STARTED) action = SignalServiceTypingMessage.Action.STARTED;
  else if (content.getAction() == TypingMessage.Action.STOPPED) action = SignalServiceTypingMessage.Action.STOPPED;
  else                                                          action = SignalServiceTypingMessage.Action.UNKNOWN;

  if (content.hasTimestamp() && content.getTimestamp() != metadata.getTimestamp()) {
    throw new ProtocolInvalidMessageException(new InvalidMessageException("Timestamps don't match: " + content.getTimestamp() + " vs " + metadata.getTimestamp()),
                                              metadata.getSender().getIdentifier(),
                                              metadata.getSenderDevice());
  }

  return new SignalServiceTypingMessage(action, content.getTimestamp(),
                                        content.hasGroupId() ? Optional.of(content.getGroupId().toByteArray()) :
                                                               Optional.<byte[]>absent());
}
 
Example 3
Source File: SignalServiceSyncMessage.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static SignalServiceSyncMessage empty() {
  return new SignalServiceSyncMessage(Optional.<SentTranscriptMessage>absent(),
      Optional.<ContactsMessage>absent(),
      Optional.<SignalServiceAttachment>absent(),
      Optional.<BlockedListMessage>absent(),
      Optional.<RequestMessage>absent(),
                                      Optional.<List<ReadMessage>>absent(),
                                      Optional.<ViewOnceOpenMessage>absent(),
                                      Optional.<VerifiedMessage>absent(),
                                      Optional.<ConfigurationMessage>absent(),
                                      Optional.<List<StickerPackOperationMessage>>absent(),
                                      Optional.<FetchType>absent(),
                                      Optional.<KeysMessage>absent(),
                                      Optional.<MessageRequestResponseMessage>absent());
}
 
Example 4
Source File: SignalServiceSyncMessage.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static SignalServiceSyncMessage forVerified(VerifiedMessage verifiedMessage) {
  return new SignalServiceSyncMessage(Optional.<SentTranscriptMessage>absent(),
                                      Optional.<ContactsMessage>absent(),
                                      Optional.<SignalServiceAttachment>absent(),
                                      Optional.<BlockedListMessage>absent(),
                                      Optional.<RequestMessage>absent(),
                                      Optional.<List<ReadMessage>>absent(),
                                      Optional.<ViewOnceOpenMessage>absent(),
                                      Optional.of(verifiedMessage),
                                      Optional.<ConfigurationMessage>absent(),
                                      Optional.<List<StickerPackOperationMessage>>absent(),
                                      Optional.<FetchType>absent(),
                                      Optional.<KeysMessage>absent(),
                                      Optional.<MessageRequestResponseMessage>absent());
}
 
Example 5
Source File: OptionalUtil.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static Optional<byte[]> absentIfEmpty(ByteString value) {
  if (value == null || value.isEmpty()) {
    return Optional.absent();
  } else {
    return Optional.of(value.toByteArray());
  }
}
 
Example 6
Source File: ChunkedDataFetcher.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private Optional<Long> parseLengthFromContentRange(@NonNull String contentRange) {
  int totalStartPos = contentRange.indexOf('/');

  if (totalStartPos >= 0 && contentRange.length() > totalStartPos + 1) {
    String totalString = contentRange.substring(totalStartPos + 1);

    try {
      return Optional.of(Long.parseLong(totalString));
    } catch (NumberFormatException e) {
      return Optional.absent();
    }
  }

  return Optional.absent();
}
 
Example 7
Source File: SignalServiceSyncMessage.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static SignalServiceSyncMessage forRead(List<ReadMessage> reads) {
  return new SignalServiceSyncMessage(Optional.<SentTranscriptMessage>absent(),
                                      Optional.<ContactsMessage>absent(),
                                      Optional.<SignalServiceAttachment>absent(),
                                      Optional.<BlockedListMessage>absent(),
                                      Optional.<RequestMessage>absent(),
                                      Optional.of(reads),
                                      Optional.<ViewOnceOpenMessage>absent(),
                                      Optional.<VerifiedMessage>absent(),
                                      Optional.<ConfigurationMessage>absent(),
                                      Optional.<List<StickerPackOperationMessage>>absent(),
                                      Optional.<FetchType>absent(),
                                      Optional.<KeysMessage>absent(),
                                      Optional.<MessageRequestResponseMessage>absent());
}
 
Example 8
Source File: PreKeySignalMessage.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
public PreKeySignalMessage(byte[] serialized)
    throws InvalidMessageException, InvalidVersionException
{
  try {
    this.version = ByteUtil.highBitsToInt(serialized[0]);

    if (this.version > CiphertextMessage.CURRENT_VERSION) {
      throw new InvalidVersionException("Unknown version: " + this.version);
    }

    if (this.version < CiphertextMessage.CURRENT_VERSION) {
      throw new LegacyMessageException("Legacy version: " + this.version);
    }

    SignalProtos.PreKeySignalMessage preKeyWhisperMessage
        = SignalProtos.PreKeySignalMessage.parseFrom(ByteString.copyFrom(serialized, 1,
                                                                         serialized.length-1));

    if (!preKeyWhisperMessage.hasSignedPreKeyId()  ||
        !preKeyWhisperMessage.hasBaseKey()         ||
        !preKeyWhisperMessage.hasIdentityKey()     ||
        !preKeyWhisperMessage.hasMessage())
    {
      throw new InvalidMessageException("Incomplete message.");
    }

    this.serialized     = serialized;
    this.registrationId = preKeyWhisperMessage.getRegistrationId();
    this.preKeyId       = preKeyWhisperMessage.hasPreKeyId() ? Optional.of(preKeyWhisperMessage.getPreKeyId()) : Optional.<Integer>absent();
    this.signedPreKeyId = preKeyWhisperMessage.hasSignedPreKeyId() ? preKeyWhisperMessage.getSignedPreKeyId() : -1;
    this.baseKey        = Curve.decodePoint(preKeyWhisperMessage.getBaseKey().toByteArray(), 0);
    this.identityKey    = new IdentityKey(Curve.decodePoint(preKeyWhisperMessage.getIdentityKey().toByteArray(), 0));
    this.message        = new SignalMessage(preKeyWhisperMessage.getMessage().toByteArray());
  } catch (InvalidProtocolBufferException | InvalidKeyException | LegacyMessageException e) {
    throw new InvalidMessageException(e);
  }
}
 
Example 9
Source File: SharedContact.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public SharedContact build() {
  return new SharedContact(name, Optional.fromNullable(avatar),
                           phone.isEmpty()   ? Optional.<List<Phone>>absent() : Optional.of(phone),
                           email.isEmpty()   ? Optional.<List<Email>>absent() : Optional.of(email),
                           address.isEmpty() ? Optional.<List<PostalAddress>>absent() : Optional.of(address),
                           Optional.fromNullable(organization));
}
 
Example 10
Source File: SignalServiceSyncMessage.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public static SignalServiceSyncMessage forVerified(VerifiedMessage verifiedMessage) {
  return new SignalServiceSyncMessage(Optional.<SentTranscriptMessage>absent(),
                                      Optional.<ContactsMessage>absent(),
                                      Optional.<SignalServiceAttachment>absent(),
                                      Optional.<BlockedListMessage>absent(),
                                      Optional.<RequestMessage>absent(),
                                      Optional.<List<ReadMessage>>absent(),
                                      Optional.<ViewOnceOpenMessage>absent(),
                                      Optional.of(verifiedMessage),
                                      Optional.<ConfigurationMessage>absent(),
                                      Optional.<List<StickerPackOperationMessage>>absent(),
                                      Optional.<FetchType>absent());
}
 
Example 11
Source File: SignalServiceCallMessage.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public static SignalServiceCallMessage forIceUpdate(final IceUpdateMessage iceUpdateMessage) {
  List<IceUpdateMessage> iceUpdateMessages = new LinkedList<>();
  iceUpdateMessages.add(iceUpdateMessage);

  return new SignalServiceCallMessage(Optional.<OfferMessage>absent(),
                                      Optional.<AnswerMessage>absent(),
                                      Optional.of(iceUpdateMessages),
                                      Optional.<HangupMessage>absent(),
                                      Optional.<BusyMessage>absent());
}
 
Example 12
Source File: LinkPreviewRepository.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private @NonNull Optional<String> getProperty(@NonNull String searchText, @NonNull String property) {
  Pattern pattern = Pattern.compile("<\\s*meta\\s+property\\s*=\\s*\"\\s*og:" + property + "\\s*\"\\s+[^>]*content\\s*=\\s*\"(.*?)\"[^>]*/?\\s*>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
  Matcher matcher = pattern.matcher(searchText);

  if (matcher.find()) {
    String text = Html.fromHtml(matcher.group(1)).toString();
    return TextUtils.isEmpty(text) ? Optional.absent() : Optional.of(text);
  }

  return Optional.absent();
}
 
Example 13
Source File: ProfileUtil.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private static Optional<UnidentifiedAccess> getUnidentifiedAccess(@NonNull Context context, @NonNull Recipient recipient) {
  Optional<UnidentifiedAccessPair> unidentifiedAccess = UnidentifiedAccessUtil.getAccessFor(context, recipient);

  if (unidentifiedAccess.isPresent()) {
    return unidentifiedAccess.get().getTargetUnidentifiedAccess();
  }

  return Optional.absent();
}
 
Example 14
Source File: SignalServiceSyncMessage.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public static SignalServiceSyncMessage forRead(List<ReadMessage> reads) {
  return new SignalServiceSyncMessage(Optional.<SentTranscriptMessage>absent(),
                                      Optional.<ContactsMessage>absent(),
                                      Optional.<SignalServiceAttachment>absent(),
                                      Optional.<BlockedListMessage>absent(),
                                      Optional.<RequestMessage>absent(),
                                      Optional.of(reads),
                                      Optional.<ViewOnceOpenMessage>absent(),
                                      Optional.<VerifiedMessage>absent(),
                                      Optional.<ConfigurationMessage>absent(),
                                      Optional.<List<StickerPackOperationMessage>>absent(),
                                      Optional.<FetchType>absent());
}
 
Example 15
Source File: SignalServiceContent.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private SignalServiceContent(SignalServiceDataMessage message, SignalServiceAddress sender, int senderDevice, long timestamp, long serverTimestamp, boolean needsReceipt, SignalServiceContentProto serializedState) {
  this.sender          = sender;
  this.senderDevice    = senderDevice;
  this.timestamp       = timestamp;
  this.serverTimestamp = serverTimestamp;
  this.needsReceipt    = needsReceipt;
  this.serializedState = serializedState;

  this.message            = Optional.fromNullable(message);
  this.synchronizeMessage = Optional.absent();
  this.callMessage        = Optional.absent();
  this.readMessage        = Optional.absent();
  this.typingMessage      = Optional.absent();
}
 
Example 16
Source File: GroupDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public Optional<GroupRecord> getGroup(@NonNull GroupId groupId) {
  try (Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, null, GROUP_ID + " = ?",
                                                                  new String[] {groupId.toString()},
                                                                  null, null, null))
  {
    if (cursor != null && cursor.moveToNext()) {
      return getGroup(cursor);
    }

    return Optional.absent();
  }
}
 
Example 17
Source File: SignalServiceAccountManager.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public Optional<SignalStorageManifest> getStorageManifestIfDifferentVersion(StorageKey storageKey, long manifestVersion) throws IOException, InvalidKeyException {
  try {
    String          authToken       = this.pushServiceSocket.getStorageAuth();
    StorageManifest storageManifest = this.pushServiceSocket.getStorageManifestIfDifferentVersion(authToken, manifestVersion);

    if (storageManifest.getValue().isEmpty()) {
      Log.w(TAG, "Got an empty storage manifest!");
      return Optional.absent();
    }

    return Optional.of(SignalStorageModels.remoteToLocalStorageManifest(storageManifest, storageKey));
  } catch (NoContentException e) {
    return Optional.absent();
  }
}
 
Example 18
Source File: DeviceContactsInputStream.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
public DeviceContact read() throws IOException {
  long   detailsLength     = readRawVarint32();
  byte[] detailsSerialized = new byte[(int)detailsLength];
  Util.readFully(in, detailsSerialized);

  SignalServiceProtos.ContactDetails      details    = SignalServiceProtos.ContactDetails.parseFrom(detailsSerialized);
  String                                  number     = details.getNumber();
  Optional<String>                        name       = Optional.fromNullable(details.getName());
  Optional<SignalServiceAttachmentStream> avatar     = Optional.absent();
  Optional<String>                        color      = details.hasColor() ? Optional.of(details.getColor()) : Optional.<String>absent();
  Optional<VerifiedMessage>               verified   = Optional.absent();
  Optional<byte[]>                        profileKey = Optional.absent();

  if (details.hasAvatar()) {
    long        avatarLength      = details.getAvatar().getLength();
    InputStream avatarStream      = new LimitedInputStream(in, avatarLength);
    String      avatarContentType = details.getAvatar().getContentType();

    avatar = Optional.of(new SignalServiceAttachmentStream(avatarStream, avatarContentType, avatarLength, Optional.<String>absent(), false, details.getNumber(),null));
  }

  if (details.hasVerified()) {
    try {
      String      destination = details.getVerified().getDestination();
      IdentityKey identityKey = new IdentityKey(details.getVerified().getIdentityKey().toByteArray(), 0);

      VerifiedMessage.VerifiedState state;

      switch (details.getVerified().getState()) {
        case VERIFIED:  state = VerifiedMessage.VerifiedState.VERIFIED;   break;
        case UNVERIFIED:state = VerifiedMessage.VerifiedState.UNVERIFIED; break;
        case DEFAULT:   state = VerifiedMessage.VerifiedState.DEFAULT;    break;
        default:        throw new InvalidMessageException("Unknown state: " + details.getVerified().getState());
      }

      verified = Optional.of(new VerifiedMessage(destination, identityKey, state, System.currentTimeMillis()));
    } catch (InvalidKeyException | InvalidMessageException e) {
      Log.w(TAG, e);
      verified = Optional.absent();
    }
  }

  if (details.hasProfileKey()) {
    profileKey = Optional.fromNullable(details.getProfileKey().toByteArray());
  }

  return new DeviceContact(number, name, avatar, color, verified, profileKey);
}
 
Example 19
Source File: ShareData.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
static ShareData forIntentData(@NonNull Uri uri, @NonNull String mimeType, boolean external) {
  return new ShareData(Optional.of(uri), Optional.of(mimeType), Optional.absent(), external);
}
 
Example 20
Source File: SignalServiceAddress.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
public SignalServiceAddress(String e164number) {
  this(e164number, Optional.<String>absent());
}