Chintan Soni
Chintan Soni

Reputation: 25267

android: create global connection to open fire using Smack API 4.1.3

Currently, I am writing the below code to connect to openfire server, in all the activities (or say fragments):

XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                .setServiceName(HOST)
                .setHost(HOST)
                .setPort(PORT)
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setDebuggerEnabled(true).build();
conn = new XMPPTCPConnection(config);
conn.connect();

And then, I am fulfilling my tasks like user authorization and authentication (login), getting rosters, etc.

Is there any other way to create somewhere global connection, so that I don't have to create connection to every time.

I want implementation like, after that every time app starts, it should create connection to Openfire server just once, and after that, I should be able to use the same connection on any of the Activity (or fragment).

Upvotes: 0

Views: 519

Answers (1)

Ghost Oracle
Ghost Oracle

Reputation: 11

Use the following singleton class:

public class XMPPConnectionManager {

    String serviceName = "SERVICENAME";
    String host = "HOST";
    int port = 5222;
    XMPPTCPConnectionConfiguration.Builder configBuilder;
    AbstractXMPPConnection connection;
    boolean setSecurityModeOn  = false;
    boolean shouldLogin = true;

    private static XMPPConnectionManager instance = null;

    private XMPPConnectionManager(){}

    public static XMPPConnectionManager getInstance() {
        if(instance == null) {
            instance = new XMPPConnectionManager();
        }
        return instance;
    }

    public void connect(String username, String password){

        XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
        configBuilder.setUsernameAndPassword(username, password);
        configBuilder.setServiceName(serviceName);
        configBuilder.setHost(host);
        configBuilder.setPort(port);
        if(!setSecurityModeOn) {
            configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        }

        connection = new XMPPTCPConnection(configBuilder.build());
        try {
            connection.connect();
            if(shouldLogin) {
                connection.login();
            }

        } catch (SmackException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } catch (XMPPException e) {

            e.printStackTrace();
        }
    }
    public void logUserIn(){
        try {
            connection.login();
        } catch (XMPPException e) {
            e.printStackTrace();
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void disconnect(){
        if(connection != null) {
            connection.disconnect();
        }
    }
    public AbstractXMPPConnection getConnection() {
        return connection;
    }

    public XMPPTCPConnectionConfiguration.Builder getConfigBuilder() {
        return configBuilder;
    }

    public void setConfigBuilder(XMPPTCPConnectionConfiguration.Builder configBuilder) {
        this.configBuilder = configBuilder;
    }

    public void setConnection(AbstractXMPPConnection connection) {
        this.connection = connection;
    }

    public void setHost(String host) {
        this.host = host;
    }



    public void setPort(int port) {
        this.port = port;
    }

    public void setSetSecurityModeOn(boolean setSecurityModeOn) {
        this.setSecurityModeOn = setSecurityModeOn;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public void setShouldLogin(boolean shouldLogin) {
        this.shouldLogin = shouldLogin;
    }
}

To use:

 XMPPConnectionManager.getInstance().connect("username","password");

To get the connection in any other class/activity, simply get the singleton class of the manager again like this:

AbstractXMPPConnection connection = XMPPConnectionManager.getInstance().getConnection();

If anything is wrong with this, please let me know.

Upvotes: 1

Related Questions