Java Code Examples for org.whispersystems.signalservice.api.util.UuidUtil#isUuid()

The following examples show how to use org.whispersystems.signalservice.api.util.UuidUtil#isUuid() . 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: Recipient.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * A safety wrapper around {@link #external(Context, String)} for when you know you're using an
 * identifier for a system contact, and therefore always want to prevent interpreting it as a
 * UUID. This will crash if given a UUID.
 *
 * (This may seem strange, but apparently some devices are returning valid UUIDs for contacts)
 */
@WorkerThread
public static @NonNull Recipient externalContact(@NonNull Context context, @NonNull String identifier) {
  RecipientDatabase db = DatabaseFactory.getRecipientDatabase(context);
  RecipientId       id = null;

  if (UuidUtil.isUuid(identifier)) {
    throw new UuidRecipientError();
  } else if (NumberUtil.isValidEmail(identifier)) {
    id = db.getOrInsertFromEmail(identifier);
  } else {
    id = db.getOrInsertFromE164(identifier);
  }

  return Recipient.resolved(id);
}
 
Example 2
Source File: Recipient.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a fully-populated {@link Recipient} based off of a string identifier, creating one in
 * the database if necessary. The identifier may be a uuid, phone number, email,
 * or serialized groupId.
 *
 * If the identifier is a UUID of a Signal user, prefer using
 * {@link #externalPush(Context, UUID, String)} or its overload, as this will let us associate
 * the phone number with the recipient.
 */
@WorkerThread
public static @NonNull Recipient external(@NonNull Context context, @NonNull String identifier) {
  Preconditions.checkNotNull(identifier, "Identifier cannot be null!");

  RecipientDatabase db = DatabaseFactory.getRecipientDatabase(context);
  RecipientId       id = null;

  if (UuidUtil.isUuid(identifier)) {
    UUID uuid = UuidUtil.parseOrThrow(identifier);

    if (FeatureFlags.uuids()) {
      id = db.getOrInsertFromUuid(uuid);
    } else {
      Optional<RecipientId> possibleId = db.getByUuid(uuid);

      if (possibleId.isPresent()) {
        id = possibleId.get();
      } else {
        if (!FeatureFlags.uuids() && FeatureFlags.groupsV2()) {
          throw new RuntimeException(new UuidRecipientError());
        } else {
          throw new UuidRecipientError();
        }
      }
    }
  } else if (GroupId.isEncodedGroup(identifier)) {
    id = db.getOrInsertFromGroupId(GroupId.parseOrThrow(identifier));
  } else if (NumberUtil.isValidEmail(identifier)) {
    id = db.getOrInsertFromEmail(identifier);
  } else {
    String e164 = PhoneNumberFormatter.get(context).format(identifier);
    id = db.getOrInsertFromE164(e164);
  }

  return Recipient.resolved(id);
}
 
Example 3
Source File: Util.java    From signal-cli with GNU General Public License v3.0 5 votes vote down vote up
public static SignalServiceAddress getSignalServiceAddressFromIdentifier(final String identifier) {
    if (UuidUtil.isUuid(identifier)) {
        return new SignalServiceAddress(UuidUtil.parseOrNull(identifier), null);
    } else {
        return new SignalServiceAddress(null, identifier);
    }
}
 
Example 4
Source File: JsonSessionStore.java    From signal-cli with GNU General Public License v3.0 5 votes vote down vote up
@Override
public JsonSessionStore deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);

    JsonSessionStore sessionStore = new JsonSessionStore();

    if (node.isArray()) {
        for (JsonNode session : node) {
            String sessionName = session.has("name")
                    ? session.get("name").asText()
                    : null;
            if (UuidUtil.isUuid(sessionName)) {
                // Ignore sessions that were incorrectly created with UUIDs as name
                continue;
            }

            UUID uuid = session.hasNonNull("uuid")
                    ? UuidUtil.parseOrNull(session.get("uuid").asText())
                    : null;
            final SignalServiceAddress serviceAddress = uuid == null
                    ? Util.getSignalServiceAddressFromIdentifier(sessionName)
                    : new SignalServiceAddress(uuid, sessionName);
            final int deviceId = session.get("deviceId").asInt();
            final String record = session.get("record").asText();
            try {
                SessionInfo sessionInfo = new SessionInfo(serviceAddress, deviceId, Base64.decode(record));
                sessionStore.sessions.add(sessionInfo);
            } catch (IOException e) {
                System.out.println(String.format("Error while decoding session for: %s", sessionName));
            }
        }
    }

    return sessionStore;
}
 
Example 5
Source File: DirectoryHelperV1.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private static boolean isValidContactNumber(@Nullable String number) {
  return !TextUtils.isEmpty(number) && !UuidUtil.isUuid(number);
}
 
Example 6
Source File: Manager.java    From signal-cli with GNU General Public License v3.0 4 votes vote down vote up
public SignalServiceAddress canonicalizeAndResolveSignalServiceAddress(String identifier) throws InvalidNumberException {
    String canonicalizedNumber = UuidUtil.isUuid(identifier) ? identifier : Util.canonicalizeNumber(identifier, account.getUsername());
    return resolveSignalServiceAddress(canonicalizedNumber);
}