public boolean offerData(String offerId, String url, String type) {
    if (mConnection.getState() == ImConnection.SUSPENDED) {
      // TODO send later
      return false;
    }

    HashMap<String, String> headers = null;
    if (type != null) {
      headers = new HashMap<>();
      headers.put("Mime-Type", type);
    }

    try {
      Address localUser = mConnection.getLoginUser().getAddress();

      if (mChatSession.getParticipant() instanceof Contact) {

        Address remoteUser = new XmppAddress(getDefaultOtrChatSession().getRemoteUserId());
        mDataHandler.offerData(offerId, localUser, remoteUser, url, headers);
      } else if (mChatSession.getParticipant() instanceof ChatGroup) {
        ChatGroup group = (ChatGroup) mChatSession.getParticipant();

        for (Contact member : group.getMembers()) {
          mDataHandler.offerData(offerId, localUser, member.getAddress(), url, headers);
        }
      }

      return true;
    } catch (Exception ioe) {
      Log.w(ImApp.LOG_TAG, "unable to offer data", ioe);
      return false;
    }
  }
  String getNickName(String username) {
    ImEntity participant = mChatSession.getParticipant();
    if (mIsGroupChat) {

      ChatGroup group = (ChatGroup) participant;
      List<Contact> members = group.getMembers();
      for (Contact c : members) {
        if (username.equals(c.getAddress().getAddress())) {

          return c.getAddress().getResource();
        }
      }

      // not found, impossible
      String[] parts = username.split("/");
      return parts[parts.length - 1];
    } else {
      return ((Contact) participant).getName();
    }
  }
  void deleteGroupMemberInDb(Contact member) {
    String where = Imps.GroupMembers.USERNAME + "=?";
    String[] selectionArgs = {member.getAddress().getAddress()};

    if (mChatURI != null) {
      long groupId = ContentUris.parseId(mChatURI);
      Uri uri = ContentUris.withAppendedId(Imps.GroupMembers.CONTENT_URI, groupId);
      mContentResolver.delete(uri, where, selectionArgs);
    }
    //  insertMessageInDb(member.getName(), null, System.currentTimeMillis(),
    //    Imps.MessageType.PRESENCE_UNAVAILABLE);
  }
  void insertGroupMemberInDb(Contact member) {

    if (mChatURI != null) {
      ContentValues values1 = new ContentValues(2);
      values1.put(Imps.GroupMembers.USERNAME, member.getAddress().getAddress());
      values1.put(Imps.GroupMembers.NICKNAME, member.getName());
      ContentValues values = values1;

      long groupId = ContentUris.parseId(mChatURI);
      Uri uri = ContentUris.withAppendedId(Imps.GroupMembers.CONTENT_URI, groupId);
      mContentResolver.insert(uri, values);

      //  insertMessageInDb(member.getName(), null, System.currentTimeMillis(),
      //      Imps.MessageType.PRESENCE_AVAILABLE);
    }
  }
  public String[] getParticipants() {
    if (mIsGroupChat) {
      Contact self = mConnection.getLoginUser();
      ChatGroup group = (ChatGroup) mChatSession.getParticipant();
      List<Contact> members = group.getMembers();
      String[] result = new String[members.size() - 1];
      int index = 0;
      for (Contact c : members) {
        if (!c.equals(self)) {
          result[index++] = c.getAddress().getAddress();
        }
      }
      return result;
    } else {

      return new String[] {mChatSession.getParticipant().getAddress().getAddress()};
    }
  }
  private long insertOrUpdateGroupContactInDb(ChatGroup group) {
    // Insert a record in contacts table
    ContentValues values = new ContentValues(4);
    values.put(Imps.Contacts.USERNAME, group.getAddress().getAddress());
    values.put(Imps.Contacts.NICKNAME, group.getName());
    values.put(Imps.Contacts.CONTACTLIST, ContactListManagerAdapter.LOCAL_GROUP_LIST_ID);
    values.put(Imps.Contacts.TYPE, Imps.Contacts.TYPE_GROUP);

    Uri contactUri =
        ContentUris.withAppendedId(
            ContentUris.withAppendedId(Imps.Contacts.CONTENT_URI, mConnection.mProviderId),
            mConnection.mAccountId);

    ContactListManagerAdapter listManager =
        (ContactListManagerAdapter) mConnection.getContactListManager();

    long id = listManager.queryGroup(group);

    if (id == -1) {
      id = ContentUris.parseId(mContentResolver.insert(contactUri, values));

      ArrayList<ContentValues> memberValues = new ArrayList<ContentValues>();
      Contact self = mConnection.getLoginUser();
      for (Contact member : group.getMembers()) {
        if (!member.equals(self)) { // avoid to insert the user himself
          ContentValues memberValue = new ContentValues(2);
          memberValue.put(Imps.GroupMembers.USERNAME, member.getAddress().getAddress());
          memberValue.put(Imps.GroupMembers.NICKNAME, member.getName());
          memberValues.add(memberValue);
        }
      }
      if (!memberValues.isEmpty()) {
        ContentValues[] result = new ContentValues[memberValues.size()];
        memberValues.toArray(result);
        Uri memberUri = ContentUris.withAppendedId(Imps.GroupMembers.CONTENT_URI, id);
        mContentResolver.bulkInsert(memberUri, result);
      }
    }

    return id;
  }
  private void initOtrChatSession(ImEntity participant) {
    try {
      if (mConnection != null) {
        mDataHandler = new OtrDataHandler(mChatSession);
        mDataHandlerListener = new DataHandlerListenerImpl();
        mDataHandler.setDataListener(mDataHandlerListener);

        OtrChatManager cm = service.getOtrChatManager();
        cm.addOtrEngineListener(mListenerAdapter);
        mChatSession.setMessageListener(new OtrChatListener(cm, mListenerAdapter));

        if (participant instanceof Contact) {
          String key = participant.getAddress().getAddress();
          if (!mOtrChatSessions.containsKey(key)) {
            OtrChatSessionAdapter adapter =
                new OtrChatSessionAdapter(
                    mConnection.getLoginUser().getAddress().getAddress(), participant, cm);
            mOtrChatSessions.put(key, adapter);
          }
        } else if (participant instanceof ChatGroup) {
          ChatGroup group = (ChatGroup) mChatSession.getParticipant();

          for (Contact contact : group.getMembers()) {
            String key = contact.getAddress().getAddress();
            if (!mOtrChatSessions.containsKey(key)) {
              OtrChatSessionAdapter adapter =
                  new OtrChatSessionAdapter(
                      mConnection.getLoginUser().getAddress().getAddress(), contact, cm);
              mOtrChatSessions.put(key, adapter);
            }
          }
        }

        mDataHandler.setChatId(getId());
      }
    } catch (NullPointerException npe) {
      Log.e(ImApp.LOG_TAG, "error init OTR session", npe);
    }
  }