Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.01 KB | None | 0 0
  1. Skip to content
  2. This repository
  3. Search
  4. Pull requests
  5. Issues
  6. Gist
  7.  @Vizhan
  8.  Sign out
  9.  Unwatch 3
  10.   Star 0
  11.   Fork 0 DanManDKo/LSDChat
  12.  Code  Issues 0  Pull requests 0  Projects 0  Wiki  Pulse  Graphs
  13. Branch: feature/conver… Find file Copy pathLSDChat/app/src/main/java/com/example/lsdchat/ui/conversation/ConversationConnection.java
  14. c8f3d41  3 hours ago
  15. @Vizhan Vizhan socket with smack/base impl 1-1 without saving history
  16. 1 contributor
  17. RawBlameHistory    
  18. 201 lines (170 sloc)  7.23 KB
  19. package com.example.lsdchat.ui.conversation;
  20.  
  21. import android.content.BroadcastReceiver;
  22. import android.content.Context;
  23. import android.content.Intent;
  24. import android.content.IntentFilter;
  25. import android.util.Log;
  26.  
  27. import org.jivesoftware.smack.Chat;
  28. import org.jivesoftware.smack.ChatManager;
  29. import org.jivesoftware.smack.ChatMessageListener;
  30. import org.jivesoftware.smack.ConnectionListener;
  31. import org.jivesoftware.smack.ReconnectionManager;
  32. import org.jivesoftware.smack.SmackException;
  33. import org.jivesoftware.smack.XMPPConnection;
  34. import org.jivesoftware.smack.XMPPException;
  35. import org.jivesoftware.smack.packet.Message;
  36. import org.jivesoftware.smack.tcp.XMPPTCPConnection;
  37. import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
  38.  
  39. import java.io.IOException;
  40.  
  41. public class ConversationConnection implements ConnectionListener, ChatMessageListener {
  42.     private static final String TAG = "ConversationConnection";
  43.  
  44.     private final Context mApplicationContext;
  45.     private final String mUsername;
  46.     private final String mPassword;
  47.     private final String mServiceName;
  48.     private XMPPTCPConnection mConnection;
  49.     private BroadcastReceiver uiThreadMessageReceiver;//Receives messages from the ui thread.
  50.  
  51.     public static enum ConnectionState {
  52.         CONNECTED, AUTHENTICATED, CONNECTING, DISCONNECTING, DISCONNECTED;
  53.     }
  54.  
  55.     public static enum LoggedInState {
  56.         LOGGED_IN, LOGGED_OUT;
  57.     }
  58.  
  59.     public ConversationConnection(Context context) {
  60.         Log.d(TAG, "RoosterConnection Constructor called.");
  61.         mApplicationContext = context.getApplicationContext();
  62.         //userId+appId
  63.         String jid = "23163511-52350@chat.quickblox.com";
  64.         mPassword = "aaaaaaaa";
  65.  
  66.         if (jid != null) {
  67.             mUsername = "23163511-52350";
  68.             mServiceName = "chat.quickblox.com";
  69.         } else {
  70.             mUsername = "";
  71.             mServiceName = "";
  72.         }
  73.     }
  74.  
  75.     public void connect() throws IOException, XMPPException, SmackException {
  76.         Log.d(TAG, "Connecting to server " + mServiceName);
  77.         XMPPTCPConnectionConfiguration.XMPPTCPConnectionConfigurationBuilder builder =
  78.                 XMPPTCPConnectionConfiguration.builder();
  79.         builder.setServiceName(mServiceName);
  80.         builder.setUsernameAndPassword(mUsername, mPassword);
  81.         builder.setRosterLoadedAtLogin(true);
  82.         builder.setResource("Rooster");
  83.  
  84.         //Set up the ui thread broadcast message receiver.
  85.         setupUiThreadBroadCastMessageReceiver();
  86.  
  87.         mConnection = new XMPPTCPConnection(builder.build());
  88.         mConnection.addConnectionListener(this);
  89.         mConnection.connect();
  90.         mConnection.login();
  91.  
  92.         ReconnectionManager reconnectionManager = ReconnectionManager.getInstanceFor(mConnection);
  93.         reconnectionManager.setEnabledPerDefault(true);
  94.         reconnectionManager.enableAutomaticReconnection();
  95.     }
  96.  
  97.     private void setupUiThreadBroadCastMessageReceiver() {
  98.         uiThreadMessageReceiver = new BroadcastReceiver() {
  99.             @Override
  100.             public void onReceive(Context context, Intent intent) {
  101.                 //Check if the Intents purpose is to send the message.
  102.                 String action = intent.getAction();
  103.                 if (action.equals(ConversationService.SEND_MESSAGE)) {
  104.                     //Send the message.
  105.                     sendMessage(intent.getStringExtra(ConversationService.BUNDLE_MESSAGE_BODY),
  106.                             intent.getStringExtra(ConversationService.BUNDLE_TO));
  107.                 }
  108.             }
  109.         };
  110.         IntentFilter filter = new IntentFilter();
  111.         filter.addAction(ConversationService.SEND_MESSAGE);
  112.         mApplicationContext.registerReceiver(uiThreadMessageReceiver, filter);
  113.     }
  114.  
  115.     private void sendMessage(String body, String toJid) {
  116.         Log.d(TAG, "Sending message to :" + toJid);
  117.         Chat chat = ChatManager.getInstanceFor(mConnection)
  118.                 .createChat(toJid, this);
  119.         try {
  120.             chat.sendMessage(body);
  121.         } catch (SmackException.NotConnectedException | XMPPException e) {
  122.             e.printStackTrace();
  123.         }
  124.     }
  125.  
  126.     @Override
  127.     public void processMessage(Chat chat, Message message) {
  128.  
  129.         Log.d(TAG, "message.getBody() :" + message.getBody());
  130.         Log.d(TAG, "message.getFrom() :" + message.getFrom());
  131. ///!!!!!!!
  132.         String from = String.valueOf(message.getFrom());
  133.         String contactJid = "";
  134.         if (from.contains("/")) {
  135.             contactJid = from.split("/")[0];
  136.             Log.d(TAG, "The real jid is :" + contactJid);
  137.         } else {
  138.             contactJid = from;
  139.         }
  140.  
  141.         //Bundle up the intent and send the broadcast.
  142.         Intent intent = new Intent(ConversationService.NEW_MESSAGE);
  143.         intent.setPackage(mApplicationContext.getPackageName());
  144.         intent.putExtra(ConversationService.BUNDLE_FROM_JID, contactJid);
  145.         intent.putExtra(ConversationService.BUNDLE_MESSAGE_BODY, message.getBody());
  146.         mApplicationContext.sendBroadcast(intent);
  147.         Log.d(TAG, "Received message from :" + contactJid + " broadcast sent.");
  148.  
  149.     }
  150.  
  151.     public void disconnect() {
  152.         Log.d(TAG, "Disconnecting from serser " + mServiceName);
  153.         if (mConnection != null) {
  154.             try {
  155.                 mConnection.disconnect();
  156.             } catch (SmackException.NotConnectedException e) {
  157.                 e.printStackTrace();
  158.             }
  159.         }
  160.         mConnection = null;
  161.         // Unregister the message broadcast receiver.
  162.         if (uiThreadMessageReceiver != null) {
  163.             mApplicationContext.unregisterReceiver(uiThreadMessageReceiver);
  164.             uiThreadMessageReceiver = null;
  165.         }
  166.  
  167.     }
  168.  
  169.     @Override
  170.     public void connected(XMPPConnection connection) {
  171.         ConversationService.sConnectionState = ConnectionState.CONNECTED;
  172.         Log.d(TAG, "Connected Successfully");
  173.     }
  174.  
  175.     @Override
  176.     public void authenticated(XMPPConnection connection) {
  177.         ConversationService.sConnectionState = ConnectionState.CONNECTED;
  178.         Log.d(TAG, "Authenticated Successfully");
  179.     }
  180.  
  181.     @Override
  182.     public void connectionClosed() {
  183.         ConversationService.sConnectionState = ConnectionState.DISCONNECTED;
  184.         Log.d(TAG, "Connectionclosed()");
  185.     }
  186.  
  187.     @Override
  188.     public void connectionClosedOnError(Exception e) {
  189.         ConversationService.sConnectionState = ConnectionState.DISCONNECTED;
  190.         Log.d(TAG, "ConnectionClosedOnError, error " + e.toString());
  191.     }
  192.  
  193.     @Override
  194.     public void reconnectionSuccessful() {
  195.         ConversationService.sConnectionState = ConnectionState.CONNECTING;
  196.         Log.d(TAG, "ReconnectingIn() ");
  197.     }
  198.  
  199.     @Override
  200.     public void reconnectingIn(int seconds) {
  201.         ConversationService.sConnectionState = ConnectionState.CONNECTED;
  202.         Log.d(TAG, "ReconnectionSuccessful()");
  203.     }
  204.  
  205.     @Override
  206.     public void reconnectionFailed(Exception e) {
  207.         ConversationService.sConnectionState = ConnectionState.DISCONNECTED;
  208.         Log.d(TAG, "ReconnectionFailed()");
  209.  
  210.     }
  211.  
  212.     private void showContactListActivityWhenAuthenticated() {
  213.         Intent i = new Intent(ConversationService.UI_AUTHENTICATED);
  214.         i.setPackage(mApplicationContext.getPackageName());
  215.         mApplicationContext.sendBroadcast(i);
  216.         Log.d(TAG, "Sent the broadcast that we are authenticated");
  217.     }
  218. }
  219. Contact GitHub API Training Shop Blog About
  220. © 2017 GitHub, Inc. Terms Privacy Security Status Help
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement