Java 类org.jivesoftware.smack.chat.ChatManager 实例源码

项目:BizareChat    文件:QuickbloxPrivateXmppConnection.java   
public void sendDisplayedReceipt(String receiverJid, String stanzaId, String dialog_id) {
    Chat chat;
    if ((chat = privateChats.get(receiverJid)) == null) {
        chat = ChatManager.getInstanceFor(privateChatConnection).createChat(receiverJid, this);
        privateChats.put(receiverJid, chat);
    }

    Message message = new Message(receiverJid);
    Displayed read = new Displayed(stanzaId);
    QuickbloxChatExtension extension = new QuickbloxChatExtension();
    extension.setProperty("dialog_id", dialog_id);

    message.setStanzaId(StanzaIdUtil.newStanzaId());
    message.setType(Message.Type.chat);
    message.addExtension(read);
    message.addExtension(extension);

    try {
        chat.sendMessage(message);
    } catch (SmackException.NotConnectedException ex) {
        Logger.logExceptionToFabric(ex);
        offlineMessages.add(message);
    }
}
项目:BizareChat    文件:QuickbloxPrivateXmppConnection.java   
public void sendReceivedReceipt(String receiverJid, String stanzaId, String dialog_id) {
    Chat chat;
    if ((chat = privateChats.get(receiverJid)) == null) {
        chat = ChatManager.getInstanceFor(privateChatConnection).createChat(receiverJid, this);
        privateChats.put(receiverJid, chat);
    }

    Message message = new Message(receiverJid);
    Received delivered = new Received(stanzaId);
    QuickbloxChatExtension extension = new QuickbloxChatExtension();
    extension.setProperty("dialog_id", dialog_id);

    message.setStanzaId(StanzaIdUtil.newStanzaId());
    message.setType(Message.Type.chat);
    message.addExtension(delivered);
    message.addExtension(extension);

    try {
        chat.sendMessage(message);
    } catch (SmackException.NotConnectedException ex) {
        offlineMessages.add(message);
    }
}
项目:BizareChat    文件:QuickbloxPrivateXmppConnection.java   
public void sendPrivateMessage(String body, String receiverJid, long timestamp, String stanzaId) {
    Log.d(TAG, "Sending message to : " + receiverJid);

    Chat chat;
    if ((chat = privateChats.get(receiverJid)) == null) {
        chat = ChatManager.getInstanceFor(privateChatConnection).createChat(receiverJid, this);
        privateChats.put(receiverJid, chat);
    }

    QuickbloxChatExtension extension = new QuickbloxChatExtension();
    extension.setProperty("date_sent", timestamp + "");
    extension.setProperty("save_to_history", "1");

    Message message = new Message(receiverJid);
    message.setStanzaId(stanzaId);
    message.setBody(body);
    message.setType(Message.Type.chat);
    message.addExtension(new Markable());
    message.addExtension(extension);

    try {
        chat.sendMessage(message);
    } catch (SmackException.NotConnectedException ex) {
        offlineMessages.add(message);
    }
}
项目:XMPPSample_Studio    文件:ActivityChatScreen.java   
@SuppressLint("NewApi")
    @Override
    protected void onDestroy() {

//        XMPP.getInstance().getConnection(acitiviy).getRoster()
//                .removeRosterListener(rosterListener);
        Roster.getInstanceFor(XMPP.getInstance().getConnection(acitiviy)).removeRosterListener(rosterListener);
        ChatManager.getInstanceFor(XMPP.getInstance().getConnection(acitiviy))
                .removeChatListener(chatListener);
        if (chat != null && messageListener != null) {
            chat.removeMessageListener(messageListener);
        }

        if (popupWindow != null) {
            popupWindow.dismiss();
        }
        if (emoticonsCover != null) {
            emoticonsCover.setVisibility(View.GONE);
        }

        // actionBar.setIcon(R.drawable.ic_launcher);

        super.onDestroy();
    }
项目:myrobotlab    文件:Xmpp.java   
public void connect(String hostname, int port, String username, String password) throws Exception {

    purgeTask("reconnect");

    this.hostname = hostname;
    this.serviceName = hostname;
    this.port = port;
    this.username = username;
    this.password = password;

    Builder builder = XMPPTCPConnectionConfiguration.builder();
    builder.setUsernameAndPassword(username, password);
    builder.setServiceName(serviceName);
    builder.setServiceName(hostname);
    builder.setPort(port);
    builder.setSecurityMode(SecurityMode.disabled);
    builder.setDebuggerEnabled(true);

    XMPPTCPConnectionConfiguration config = builder.build();

    connection = new XMPPTCPConnection(config);
    connection.connect().login();

    roster = Roster.getInstanceFor(connection);
    chatManager = ChatManager.getInstanceFor(connection);

    roster.addRosterListener(this);

    isConnected = true;

    // not worth it - always empty right after connect
    // getContactList();

    broadcastState();
  }
项目:ChatApp-Android    文件:XmppManager.java   
/**
 * send the raw string as a message without wrapping it with xml attributes
 * @param message the message to be sent
 * @param buddyJID the buddy to send the message to
 */
public void sendRaw(String message, String buddyJID){
  ChatManager chatManager = ChatManager.getInstanceFor(connection);
  if (connection != null && connection.isConnected() && chatManager != null){
    try{
      Chat chat = chatManager.createChat(buddyJID);
      chat.sendMessage(message);
    }catch (Exception e){
      e.printStackTrace();
    }
  }
}
项目:ChatApp-Android    文件:XmppManager.java   
/**
 * sends a text message
 *
 * @param message  the message text to send
 * @param buddyJID the Buddy to receive the message
 * @return true if sending was successful
 */
public boolean sendTextMessage(String message, String buddyJID, long id){
  ChatManager chatManager = ChatManager.getInstanceFor(connection);
  if (connection != null && connection.isConnected() && chatManager != null){
    if (buddyJID.indexOf('@') == -1)
      buddyJID += "@" + service;
    Chat chat = chatManager.createChat(buddyJID);
    try{
      // wrap the message with all necessary xml attributes
      Document doc = DocumentBuilderFactory.newInstance()
              .newDocumentBuilder().newDocument();
      Element msg = doc.createElement("message");
      doc.appendChild(msg);
      msg.setAttribute("type", MessageHistory.TYPE_TEXT);
      msg.setAttribute("id", String.valueOf(id));
      Element file = doc.createElement("content");
      msg.appendChild(file);
      file.setTextContent(message);

      // transform everything to a string
      Transformer t = TransformerFactory.newInstance().newTransformer();
      StringWriter writer = new StringWriter();
      StreamResult r = new StreamResult(writer);
      t.transform(new DOMSource(doc), r);
      message = writer.toString();

      // send the message
      chat.sendMessage(message);
      Log.d("DEBUG", "Success: Sent message");
      return true;
    }catch (Exception e){
      Log.e("ERROR", "Couldn't send message.");
      Log.e("ERROR", e.toString());
      return false;
    }
  }
  Log.e("ERROR", "Sending failed: No connection.");
  return false;
}
项目:ChatApp-Android    文件:XmppManager.java   
/**
 * send an acknowledgement
 * @param buddyId the buddyId to receive the acknowledgement
 * @param othersId the id the buddy has sent the message with
 * @param type the type of acknowledgement to send
 * @return true if sending was successful
 */
public boolean sendAcknowledgement(String buddyId, long othersId, String type){
  ChatManager chatManager = ChatManager.getInstanceFor(connection);
  if (connection != null && connection.isConnected() && chatManager != null){
    if (buddyId.indexOf('@') == -1)
      buddyId += "@" + service;
    Chat chat = chatManager.createChat(buddyId);
    try{
      // create the message structure
      Document doc = DocumentBuilderFactory.newInstance()
              .newDocumentBuilder().newDocument();
      Element ack = doc.createElement("acknowledgement");
      doc.appendChild(ack);
      ack.setAttribute("id", String.valueOf(othersId));
      ack.setAttribute("type", type);

      // create the string representation of the message
      Transformer t = TransformerFactory.newInstance().newTransformer();
      StringWriter writer = new StringWriter();
      StreamResult r = new StreamResult(writer);
      t.transform(new DOMSource(doc), r);
      String message = writer.toString();

      // send the message
      chat.sendMessage(message);
      Log.d("DEBUG", "Success: Sent message");
      return true;
    }catch (Exception e){
      Log.e("ERROR", "Couldn't send message.");
      Log.e("ERROR", e.toString());
      return false;
    }
  }
  Log.e("ERROR", "Sending failed: No connection.");
  return false;
}
项目:ChatApp-Android    文件:MessageService.java   
private void initialize(){
    try{
      //initialize xmpp:
      Log.d("SERVICE_DEBUG", "MessageService initializing");
      if (xmppManager == null)
        xmppManager = XmppManager.getInstance(getApplicationContext());
      if (!xmppManager.isConnected()){
        xmppManager.init();
        xmppManager.performLogin(getUserName(), getPassword());
//        OfflineMessageManager offlineMessageManager = new
//                OfflineMessageManager(xmppManager.getConnection());
//        if (offlineMessageManager.supportsFlexibleRetrieval())
//          processMessages(offlineMessageManager.getMessages().toArray(new
//                  Message[offlineMessageManager.getMessageCount()]));
        new Thread(reloadRoster).start();
      }

      ChatManager chatManager = ChatManager.getInstanceFor(xmppManager
              .getConnection());
      chatManager.addChatListener(new MyChatManagerListener());
      xmppManager.setStatus(true, String.valueOf(getSharedPreferences(Constants
              .PREFERENCES, 0).getLong(Constants.LAST_PRESENCE_SENT, 0)));
    }catch (Exception e){
      Log.e("SERVICE_ERROR", "An error while initializing the MessageService " +
              "occurred.");
      e.printStackTrace();
    }
  }
项目:xmpp-service    文件:XmppServiceConnection.java   
public void connect() throws IOException, XMPPException, SmackException {
    if (mConnection == null) {
        createConnection();
    }

    if (!mConnection.isConnected()) {
        Logger.info(TAG, "Connecting to " + mAccount.getHost() + ":" + mAccount.getPort());
        mConnection.connect();

        Roster roster = Roster.getInstanceFor(mConnection);
        roster.removeRosterListener(this);
        roster.addRosterListener(this);
        roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
        roster.setRosterLoadedAtLogin(true);
    }

    if (!mConnection.isAuthenticated()) {
        Logger.info(TAG, "Authenticating " + mAccount.getXmppJid());
        mConnection.login();

        PingManager.setDefaultPingInterval(XmppService.DEFAULT_PING_INTERVAL);
        PingManager pingManager = PingManager.getInstanceFor(mConnection);
        pingManager.registerPingFailedListener(this);

        ChatManager chatManager = ChatManager.getInstanceFor(mConnection);
        chatManager.removeChatListener(this);
        chatManager.addChatListener(this);

        DeliveryReceiptManager receipts = DeliveryReceiptManager.getInstanceFor(mConnection);
        receipts.setAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);
        receipts.autoAddDeliveryReceiptRequests();
    }

    mOwnAvatar = getAvatarFor("");
}
项目:Smack    文件:ChatStateManager.java   
private ChatStateManager(XMPPConnection connection) {
    super(connection);
    chatManager = ChatManager.getInstanceFor(connection);
    chatManager.addOutgoingMessageInterceptor(outgoingInterceptor, filter);
    chatManager.addChatListener(incomingInterceptor);

    ServiceDiscoveryManager.getInstanceFor(connection).addFeature(NAMESPACE);
    INSTANCES.put(connection, this);
}
项目:jabbot    文件:XmppBinding.java   
@Override
public void sendMessage(TxMessage message) {
    if(message.getDestination().getType().equals(Resource.Type.ROOM)){
        Room room = getRoom(message.getDestination().getAddress());
        if(room != null) {
            room.sendMessage(message);
        }
    }else{
        XmppRxMessage origin = (XmppRxMessage)message.getRequest();
        Chat chat = ChatManager.getInstanceFor(connection).getThreadChat(origin.getThread());
        if(chat == null){
            logger.trace("chat was null, creating a new chat instance");

            chat = ChatManager.getInstanceFor(connection).createChat(message.getDestination().getAddress(),origin.getThread(),null);
        }else{
            logger.trace("an existing chat was found for thread id {}", (origin.getThread()));
        }
        try {
            Message msg = MessageHelper.createXmppMessage(message);
            msg.addExtension(new DeliveryReceipt(origin.getId()));
            chat.sendMessage(msg);
        } catch (SmackException.NotConnectedException e) {
            logger.warn("Trying to send a message on XMPP binding while connection is closed",e);
        }

    }
}
项目:BizareChat    文件:QuickbloxPrivateXmppConnection.java   
@Override
public void authenticated(XMPPConnection xmppConnection, boolean b) {
    Log.d(TAG, "Authenticated Successfully");
    connected = true;
    ChatManager.getInstanceFor(privateChatConnection).addChatListener(this);
}
项目:mangosta-android    文件:RoomsListManager.java   
public ChatManager getChatManager() {
    return ChatManager.getInstanceFor(XMPPSession.getInstance().getXMPPConnection());
}
项目:ChatApp-Android    文件:XmppManager.java   
/**
 * sends an image message
 *
 * @param serverFile  the file on the server
 * @param description the description of the sent image
 * @param buddyJID    the Buddy to receive the message
 * @return true if sending was successful
 */
public boolean sendImageMessage(String serverFile, String description, String
        buddyJID, long id){
  if (buddyJID.indexOf('@') == -1)
    buddyJID += "@" + service;
  ChatManager chatManager = ChatManager.getInstanceFor(connection);
  if (connection != null && connection.isConnected() && chatManager != null){
    Chat chat = chatManager.createChat(buddyJID);
    try{
      //generate the message in order to set the type to image
      Document doc = DocumentBuilderFactory.newInstance()
              .newDocumentBuilder().newDocument();
      Element msg = doc.createElement("message");
      doc.appendChild(msg);
      msg.setAttribute("type", MessageHistory.TYPE_IMAGE);
      msg.setAttribute("id", String.valueOf(id));
      Element file = doc.createElement("file");
      msg.appendChild(file);
      file.setTextContent(serverFile);
      Element desc = doc.createElement("description");
      msg.appendChild(desc);
      desc.setTextContent(description);

      // create the string
      Transformer t = TransformerFactory.newInstance().newTransformer();
      StringWriter writer = new StringWriter();
      StreamResult r = new StreamResult(writer);
      t.transform(new DOMSource(doc), r);
      String message = writer.toString();

      // send the message
      chat.sendMessage(message);
      Log.d("DEBUG", "Success: Sent message");
      return true;
    }catch (Exception e){
      Log.e("ERROR", "Couldn't send message.");
      Log.e("ERROR", e.toString());
      return false;
    }
  }
  Log.e("ERROR", "Sending failed: No connection.");
  return false;
}
项目:xmpp-service    文件:XmppServiceConnection.java   
private void sendMessage(String destinationJID, String message) throws SmackException.NotConnectedException {
    Logger.debug(TAG, "Sending message to " + destinationJID);
    Chat chat = ChatManager.getInstanceFor(mConnection).createChat(destinationJID, this);
    chat.sendMessage(message);
}
项目:openyu-commons    文件:XmppSessionImpl.java   
public XmppSessionImpl(XmppSessionFactoryImpl xmppSessionFactoryImpl, XMPPConnection xmppConnection) {
    this.xmppSessionFactoryImpl = xmppSessionFactoryImpl;
    this.xmppConnection = xmppConnection;
    //
    this.delegate = ChatManager.getInstanceFor(xmppConnection);
}