Created
April 27, 2015 07:24
-
-
Save fpopic/0ad1677f0d0a664630c1 to your computer and use it in GitHub Desktop.
smack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.fp.xmppproject; | |
| import android.app.Service; | |
| import android.content.Intent; | |
| import android.os.IBinder; | |
| import android.util.Log; | |
| import org.jivesoftware.smack.ConnectionConfiguration; | |
| import org.jivesoftware.smack.ConnectionListener; | |
| import org.jivesoftware.smack.PresenceListener; | |
| import org.jivesoftware.smack.ReconnectionManager; | |
| import org.jivesoftware.smack.SmackException; | |
| import org.jivesoftware.smack.XMPPConnection; | |
| import org.jivesoftware.smack.XMPPException; | |
| import org.jivesoftware.smack.chat.Chat; | |
| import org.jivesoftware.smack.chat.ChatManagerListener; | |
| import org.jivesoftware.smack.chat.ChatMessageListener; | |
| import org.jivesoftware.smack.packet.Message; | |
| import org.jivesoftware.smack.packet.Presence; | |
| import org.jivesoftware.smack.roster.Roster; | |
| import org.jivesoftware.smack.roster.RosterListener; | |
| import org.jivesoftware.smack.roster.RosterLoadedListener; | |
| import org.jivesoftware.smack.tcp.XMPPTCPConnection; | |
| import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; | |
| import org.jivesoftware.smackx.ping.PingFailedListener; | |
| import java.io.IOException; | |
| import java.util.Collection; | |
| public class SmackConnection extends Service implements | |
| ConnectionListener, PingFailedListener, | |
| ChatManagerListener, ChatMessageListener, | |
| RosterLoadedListener, RosterListener, | |
| PresenceListener{ | |
| public SmackConnection() { | |
| } | |
| // Custom methods. | |
| private void connect() { | |
| XMPPTCPConnection mConnection = new XMPPTCPConnection( | |
| XMPPTCPConnectionConfiguration.builder() | |
| .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) | |
| .setUsernameAndPassword("mate", "mate") | |
| .setResource("AndroidEmulator") | |
| .setHost("10.0.2.2") //beacuse of emulator | |
| .setServiceName("127.0.0.1") //openfire domain | |
| .setPort(5222) | |
| .setDebuggerEnabled(true) | |
| .build() | |
| ); | |
| mConnection.setPacketReplyTimeout(10000); | |
| try { | |
| mConnection.connect().login(); | |
| } catch (SmackException | IOException | XMPPException e) { | |
| Log.d("SMACK_ERROR", "Connect and Login: " + e.toString()); | |
| } | |
| } | |
| // Android Service. | |
| @Override | |
| public IBinder onBind(Intent intent) { | |
| return null; | |
| } | |
| // Connection listener. | |
| /** | |
| * Notification that the connection has been successfully connected to the remote endpoint (e.g. the XMPP server). | |
| * <p> | |
| * Note that the connection is likely not yet authenticated and therefore only limited operations like registering | |
| * an account may be possible. | |
| * </p> | |
| * | |
| * @param connection the XMPPConnection which successfully connected to its endpoint. | |
| */ | |
| @Override | |
| public void connected(XMPPConnection connection) { | |
| } | |
| /** | |
| * Notification that the connection has been authenticated. | |
| * | |
| * @param connection the XMPPConnection which successfully authenticated. | |
| * @param resumed true if a previous XMPP session's stream was resumed. | |
| */ | |
| @Override | |
| public void authenticated(XMPPConnection connection, boolean resumed) { | |
| } | |
| /** | |
| * Notification that the connection was closed normally. | |
| */ | |
| @Override | |
| public void connectionClosed() { | |
| } | |
| /** | |
| * Notification that the connection was closed due to an exception. When | |
| * abruptly disconnected it is possible for the connection to try reconnecting | |
| * to the server. | |
| * | |
| * @param e the exception. | |
| */ | |
| @Override | |
| public void connectionClosedOnError(Exception e) { | |
| } | |
| /** | |
| * The connection has reconnected successfully to the server. Connections will | |
| * reconnect to the server when the previous socket connection was abruptly closed. | |
| */ | |
| @Override | |
| public void reconnectionSuccessful() { | |
| } | |
| /** | |
| * The connection will retry to reconnect in the specified number of seconds. | |
| * <p> | |
| * Note: This method is only called if {@link ReconnectionManager#isAutomaticReconnectEnabled()} returns true, i.e. | |
| * only when the reconnection manager is enabled for the connection. | |
| * </p> | |
| * | |
| * @param seconds remaining seconds before attempting a reconnection. | |
| */ | |
| @Override | |
| public void reconnectingIn(int seconds) { | |
| } | |
| /** | |
| * An attempt to connect to the server has failed. The connection will keep trying reconnecting to the server in a | |
| * moment. | |
| * <p> | |
| * Note: This method is only called if {@link ReconnectionManager#isAutomaticReconnectEnabled()} returns true, i.e. | |
| * only when the reconnection manager is enabled for the connection. | |
| * </p> | |
| * | |
| * @param e the exception that caused the reconnection to fail. | |
| */ | |
| @Override | |
| public void reconnectionFailed(Exception e) { | |
| } | |
| // ChatManager listener. | |
| /** | |
| * Event fired when a new chat is created. | |
| * | |
| * @param chat the chat that was created. | |
| * @param createdLocally true if the chat was created by the local user and false if it wasn't. | |
| */ | |
| @Override | |
| public void chatCreated(Chat chat, boolean createdLocally) { | |
| } | |
| // ChatMessage listener. | |
| @Override | |
| public void processMessage(Chat chat, Message message) { | |
| } | |
| // RosterLoaded listener. | |
| /** | |
| * Called when the Roster was loaded successfully. | |
| * | |
| * @param roster the Roster that was loaded successfully. | |
| */ | |
| @Override | |
| public void onRosterLoaded(Roster roster) { | |
| } | |
| // Roster listener. | |
| /** | |
| * Called when roster entries are added. | |
| * | |
| * @param addresses the XMPP addresses of the contacts that have been added to the roster. | |
| */ | |
| @Override | |
| public void entriesAdded(Collection<String> addresses) { | |
| } | |
| /** | |
| * Called when a roster entries are updated. | |
| * | |
| * @param addresses the XMPP addresses of the contacts whose entries have been updated. | |
| */ | |
| @Override | |
| public void entriesUpdated(Collection<String> addresses) { | |
| } | |
| /** | |
| * Called when a roster entries are removed. | |
| * | |
| * @param addresses the XMPP addresses of the contacts that have been removed from the roster. | |
| */ | |
| @Override | |
| public void entriesDeleted(Collection<String> addresses) { | |
| } | |
| /** | |
| * Called when the presence of a roster entry is changed. Care should be taken | |
| * when using the presence data delivered as part of this event. Specifically, | |
| * when a user account is online with multiple resources, the UI should account | |
| * for that. For example, say a user is online with their desktop computer and | |
| * mobile phone. If the user logs out of the IM client on their mobile phone, the | |
| * user should not be shown in the roster (contact list) as offline since they're | |
| * still available as another resource.<p> | |
| * <p/> | |
| * To get the current "best presence" for a user after the presence update, query the roster: | |
| * <pre> | |
| * String user = presence.getFrom(); | |
| * Presence bestPresence = roster.getPresence(user); | |
| * </pre> | |
| * <p/> | |
| * That will return the presence value for the user with the highest priority and | |
| * availability. | |
| * <p/> | |
| * Note that this listener is triggered for presence (mode) changes only | |
| * (e.g presence of types available and unavailable. Subscription-related | |
| * presence packets will not cause this method to be called. | |
| * | |
| * @param presence the presence that changed. | |
| * @see org.jivesoftware.smack.roster.Roster#getPresence(String) | |
| */ | |
| @Override | |
| public void presenceChanged(Presence presence) { | |
| } | |
| // Ping Failed listener. | |
| /** | |
| * Called when the server ping fails. | |
| */ | |
| @Override | |
| public void pingFailed() { | |
| } | |
| // Presence listener. | |
| @Override | |
| public void processPresence(Presence presence) { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment