Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.20 KB | None | 0 0
  1. package com.example.lsdchat.ui.conversation;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.util.Log;
  8.  
  9. import org.jivesoftware.smack.ConnectionConfiguration;
  10. import org.jivesoftware.smack.ConnectionListener;
  11. import org.jivesoftware.smack.ReconnectionManager;
  12. import org.jivesoftware.smack.SmackException;
  13. import org.jivesoftware.smack.XMPPConnection;
  14. import org.jivesoftware.smack.XMPPException;
  15. import org.jivesoftware.smack.packet.DefaultExtensionElement;
  16. import org.jivesoftware.smack.packet.Message;
  17. import org.jivesoftware.smack.tcp.XMPPTCPConnection;
  18. import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
  19. import org.jivesoftware.smackx.muc.MultiUserChat;
  20. import org.jivesoftware.smackx.muc.MultiUserChatManager;
  21.  
  22. import java.io.IOException;
  23. import java.util.Random;
  24.  
  25. public class ConversationConnection implements ConnectionListener {
  26.     private static final String TAG = "ConversationConnection";
  27.  
  28.     private final Context mApplicationContext;
  29.     private final String mUsername;
  30.     private final String mPassword;
  31.     private final String mServiceName;
  32.     private XMPPTCPConnection mConnection;
  33.     private BroadcastReceiver uiThreadMessageReceiver;//Receives messages from the ui thread.
  34.  
  35.     public static enum ConnectionState {
  36.         CONNECTED, AUTHENTICATED, CONNECTING, DISCONNECTING, DISCONNECTED;
  37.     }
  38.  
  39.     public static enum LoggedInState {
  40.         LOGGED_IN, LOGGED_OUT;
  41.     }
  42.  
  43.     //test
  44.     MultiUserChat muc;
  45.     MultiUserChatManager manager;
  46.     public static final String mucChat = "52350_589f6bfda0eb47ea8400026a@muc.chat.quickblox.com";
  47. //---
  48.  
  49.     public ConversationConnection(Context context) {
  50.         Log.d(TAG, "RoosterConnection Constructor called.");
  51.         mApplicationContext = context.getApplicationContext();
  52.         //userId+appId
  53.         String jid = "23163511-52350@chat.quickblox.com";
  54.         mPassword = "aaaaaaaa";
  55.  
  56.         if (jid != null) {
  57.             mUsername = "23163511-52350";
  58.             mServiceName = "chat.quickblox.com";
  59.         } else {
  60.             mUsername = "";
  61.             mServiceName = "";
  62.         }
  63.     }
  64.  
  65.     public void connect() throws IOException, XMPPException, SmackException {
  66.         Log.d(TAG, "Connecting to server " + mServiceName);
  67.         XMPPTCPConnectionConfiguration builder2 = XMPPTCPConnectionConfiguration.builder()
  68.                 .setServiceName(mServiceName)
  69.                 .setUsernameAndPassword(mUsername, mPassword)
  70.                 .setPort(5222)
  71.                 .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
  72.                 .build();
  73.  
  74.         //Set up the ui thread broadcast message receiver.
  75.         setupUiThreadBroadCastMessageReceiver();
  76.  
  77.         mConnection = new XMPPTCPConnection(builder2);
  78.         mConnection.addConnectionListener(this);
  79.         mConnection.connect();
  80.         mConnection.login();
  81.        
  82.         manager = MultiUserChatManager.getInstanceFor(mConnection);
  83.         muc = manager.getMultiUserChat(mucChat);
  84.         muc.join(mConnection.getUser());
  85.        
  86.         ReconnectionManager reconnectionManager = ReconnectionManager.getInstanceFor(mConnection);
  87.         reconnectionManager.setEnabledPerDefault(true);
  88.         reconnectionManager.enableAutomaticReconnection();
  89.  
  90.     }
  91.  
  92.     private void setupUiThreadBroadCastMessageReceiver() {
  93.         uiThreadMessageReceiver = new BroadcastReceiver() {
  94.             @Override
  95.             public void onReceive(Context context, Intent intent) {
  96.                 //Check if the Intents purpose is to send the message.
  97.                 String action = intent.getAction();
  98.                 if (action.equals(ConversationService.SEND_MESSAGE)) {
  99.                     //Send the message.
  100.                     sendMessage(intent.getStringExtra(ConversationService.BUNDLE_MESSAGE_BODY),
  101.                             intent.getStringExtra(ConversationService.BUNDLE_TO));
  102.                 }
  103.             }
  104.         };
  105.         IntentFilter filter = new IntentFilter();
  106.         filter.addAction(ConversationService.SEND_MESSAGE);
  107.         mApplicationContext.registerReceiver(uiThreadMessageReceiver, filter);
  108.     }
  109.  
  110.     private void sendMessage(String body, String toJid) {
  111.         Message msg = new Message();
  112.         DefaultExtensionElement extensionElement = new DefaultExtensionElement("extraParams", "jabber:client");
  113.         extensionElement.setValue("save_to_history", "1");
  114.  
  115.         Log.d("stas", msg.getBody() + " = getBody");
  116.         msg.setBody(body);
  117.         Log.d("stas", body + " - body");
  118.         msg.setStanzaId(String.valueOf(new Random(1000).nextInt()));
  119.         msg.setType(Message.Type.groupchat);
  120.         msg.setTo(toJid);
  121.         msg.addExtension(extensionElement);
  122.         try {
  123.             muc.sendMessage(msg);
  124.         } catch (SmackException.NotConnectedException e) {
  125.             e.printStackTrace();
  126.         }
  127. //        Bundle up the intent and send the broadcast.
  128.         Intent intent = new Intent(ConversationService.NEW_MESSAGE);
  129.         intent.setPackage(mApplicationContext.getPackageName());
  130.         intent.putExtra(ConversationService.BUNDLE_FROM_JID, "23163511-52350@chat.quickblox.com");
  131.         intent.putExtra(ConversationService.BUNDLE_MESSAGE_BODY, msg.getBody());
  132.         mApplicationContext.sendBroadcast(intent);
  133.     }
  134.  
  135.     public void disconnect() {
  136.         Log.d(TAG, "Disconnecting from serser " + mServiceName);
  137.         if (mConnection != null) {
  138.             mConnection.disconnect();
  139.         }
  140.         mConnection = null;
  141.         // Unregister the message broadcast receiver.
  142.         if (uiThreadMessageReceiver != null) {
  143.             mApplicationContext.unregisterReceiver(uiThreadMessageReceiver);
  144.             uiThreadMessageReceiver = null;
  145.         }
  146.     }
  147.  
  148.     @Override
  149.     public void connected(XMPPConnection connection) {
  150.         ConversationService.sConnectionState = ConnectionState.CONNECTED;
  151.         Log.d(TAG, "Connected Successfully");
  152.     }
  153.  
  154.     @Override
  155.     public void authenticated(XMPPConnection connection, boolean resumed) {
  156.         ConversationService.sConnectionState = ConnectionState.CONNECTED;
  157.         Log.d(TAG, "Authenticated Successfully");
  158.     }
  159.  
  160.     @Override
  161.     public void connectionClosed() {
  162.         ConversationService.sConnectionState = ConnectionState.DISCONNECTED;
  163.         Log.d(TAG, "Connectionclosed()");
  164.     }
  165.  
  166.     @Override
  167.     public void connectionClosedOnError(Exception e) {
  168.         ConversationService.sConnectionState = ConnectionState.DISCONNECTED;
  169.         Log.d(TAG, "ConnectionClosedOnError, error " + e.toString());
  170.     }
  171.  
  172.     @Override
  173.     public void reconnectionSuccessful() {
  174.         ConversationService.sConnectionState = ConnectionState.CONNECTING;
  175.         Log.d(TAG, "ReconnectingIn() ");
  176.     }
  177.  
  178.     @Override
  179.     public void reconnectingIn(int seconds) {
  180.         ConversationService.sConnectionState = ConnectionState.CONNECTED;
  181.         Log.d(TAG, "ReconnectionSuccessful()");
  182.     }
  183.  
  184.     @Override
  185.     public void reconnectionFailed(Exception e) {
  186.         ConversationService.sConnectionState = ConnectionState.DISCONNECTED;
  187.         Log.d(TAG, "ReconnectionFailed()");
  188.  
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement