コード例 #1
0
  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;
    }
  }
コード例 #2
0
  private void init(ChatGroup group, boolean isNewSession) {

    mIsGroupChat = true;

    mContactId = insertOrUpdateGroupContactInDb(group);
    group.addMemberListener(mListenerAdapter);

    try {
      mChatSessionManager
          .getChatGroupManager()
          .joinChatGroupAsync(group.getAddress(), group.getName());

      mMessageURI = Imps.Messages.getContentUriByThreadId(mContactId);

      mChatURI = ContentUris.withAppendedId(Imps.Chats.CONTENT_URI, mContactId);

      if (isNewSession) insertOrUpdateChat("");

      for (Contact c : group.getMembers()) {
        mContactStatusMap.put(c.getName(), c.getPresence().getStatus());
      }

    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
コード例 #3
0
 public void onGroupCreated(ChatGroup group) {
   if (mGroupName.equalsIgnoreCase(group.getName())) {
     mGroupMgr.removeGroupListener(this);
     group.addMemberListener(this);
     mGroupMgr.inviteUserAsync(group, (Contact) mChatSession.getParticipant());
   }
 }
コード例 #4
0
  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()};
    }
  }
コード例 #5
0
  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();
    }
  }
コード例 #6
0
  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;
  }
コード例 #7
0
  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);
    }
  }