public boolean createSessionsIfNeeded(final Conversation conversation) {
    Log.i(
        Config.LOGTAG,
        AxolotlService.getLogprefix(account) + "Creating axolotl sessions if needed...");
    boolean newSessions = false;
    Set<AxolotlAddress> addresses = findDevicesWithoutSession(conversation);
    for (AxolotlAddress address : addresses) {
      Log.d(
          Config.LOGTAG,
          AxolotlService.getLogprefix(account) + "Processing device: " + address.toString());
      FetchStatus status = fetchStatusMap.get(address);
      if (status == null || status == FetchStatus.ERROR) {
        fetchStatusMap.put(address, FetchStatus.PENDING);
        this.buildSessionFromPEP(address);
        newSessions = true;
      } else if (status == FetchStatus.PENDING) {
        newSessions = true;
      } else {
        Log.d(
            Config.LOGTAG,
            AxolotlService.getLogprefix(account)
                + "Already fetching bundle for "
                + address.toString());
      }
    }

    return newSessions;
  }
 private void putDevicesForJid(
     String bareJid, List<Integer> deviceIds, SQLiteAxolotlStore store) {
   for (Integer deviceId : deviceIds) {
     AxolotlAddress axolotlAddress = new AxolotlAddress(bareJid, deviceId);
     Log.d(
         Config.LOGTAG,
         AxolotlService.getLogprefix(account)
             + "Building session for remote address: "
             + axolotlAddress.toString());
     String fingerprint =
         store
             .loadSession(axolotlAddress)
             .getSessionState()
             .getRemoteIdentityKey()
             .getFingerprint()
             .replaceAll("\\s", "");
     this.put(
         axolotlAddress, new XmppAxolotlSession(account, store, axolotlAddress, fingerprint));
   }
 }
  public Set<AxolotlAddress> findDevicesWithoutSession(final Conversation conversation) {
    Log.d(
        Config.LOGTAG,
        AxolotlService.getLogprefix(account)
            + "Finding devices without session for "
            + conversation.getContact().getJid().toBareJid());
    Jid contactJid = conversation.getContact().getJid().toBareJid();
    Set<AxolotlAddress> addresses = new HashSet<>();
    if (deviceIds.get(contactJid) != null) {
      for (Integer foreignId : this.deviceIds.get(contactJid)) {
        AxolotlAddress address = new AxolotlAddress(contactJid.toString(), foreignId);
        if (sessions.get(address) == null) {
          IdentityKey identityKey =
              axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
          if (identityKey != null) {
            Log.d(
                Config.LOGTAG,
                AxolotlService.getLogprefix(account)
                    + "Already have session for "
                    + address.toString()
                    + ", adding to cache...");
            XmppAxolotlSession session =
                new XmppAxolotlSession(
                    account,
                    axolotlStore,
                    address,
                    identityKey.getFingerprint().replaceAll("\\s", ""));
            sessions.put(address, session);
          } else {
            Log.d(
                Config.LOGTAG,
                AxolotlService.getLogprefix(account)
                    + "Found device "
                    + account.getJid().toBareJid()
                    + ":"
                    + foreignId);
            addresses.add(new AxolotlAddress(contactJid.toString(), foreignId));
          }
        }
      }
    } else {
      Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
    }
    if (deviceIds.get(account.getJid().toBareJid()) != null) {
      for (Integer ownId : this.deviceIds.get(account.getJid().toBareJid())) {
        AxolotlAddress address = new AxolotlAddress(account.getJid().toBareJid().toString(), ownId);
        if (sessions.get(address) == null) {
          IdentityKey identityKey =
              axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
          if (identityKey != null) {
            Log.d(
                Config.LOGTAG,
                AxolotlService.getLogprefix(account)
                    + "Already have session for "
                    + address.toString()
                    + ", adding to cache...");
            XmppAxolotlSession session =
                new XmppAxolotlSession(
                    account,
                    axolotlStore,
                    address,
                    identityKey.getFingerprint().replaceAll("\\s", ""));
            sessions.put(address, session);
          } else {
            Log.d(
                Config.LOGTAG,
                AxolotlService.getLogprefix(account)
                    + "Found device "
                    + account.getJid().toBareJid()
                    + ":"
                    + ownId);
            addresses.add(new AxolotlAddress(account.getJid().toBareJid().toString(), ownId));
          }
        }
      }
    }

    return addresses;
  }