app/src/main/java/com/yahala/xmpp/XMPPManager.java
/**
     * Tries to fully establish the given XMPPConnection
     * Calls maybeStartReconnect() or stop() in an error case
     *
     * @param connection
     * @return true if we are connected and authenticated, false otherwise
     */
Tries to fully establish the given XMPPConnection
Calls maybeStartReconnect() or stop() in an error case

@param connection
@return true if we are connected and authenticated, false otherwise
Tries to fully establish the given XMPPConnection Calls maybeStartReconnect () or stop () in an error case @param connection @return true if we are connected and authenticated false otherwise
private boolean connectAndAuth(XMPPTCPConnection connection) {
        if (UserConfig.currentUser == null)
            UserConfig.loadConfig();
        try {
            FileLog.d("XMPPManager", "Connecting to " + HOST + ":" + SPORT);
            connection.connect();
            registerRoasterListener(connection);
        } catch (Exception e) {
            FileLog.e("XMPPManager", "XMPP connection failed", e);
            if (e instanceof XMPPException) {
                XMPPException xmppEx = (XMPPException) e;
            }
            connectionState = ConnectionState.DISCONNECTED;
            maybeStartReconnect();
            Utilities.RunOnUIThread(new Runnable() {
                @Override
                public void run() {
                    NotificationCenter.getInstance().postNotificationName(connectionStateDidChanged);
                }
            });
            return false;
        }
        if (connection.isAuthenticated()) {
            return true;
        }
        FileLog.e("XMPPManager", "Service discovery");
        mPingManager = PingManager.getInstanceFor(connection);
        mPingManager.setDefaultPingInterval(30);
        mPingManager.setPingInterval(30);
        mPingManager.registerPingFailedListener(new PingFailedListener() {
            @Override
            public void pingFailed() {
                long now = new Date().getTime();
                if (now - lastPing > 30000) {
                    FileLog.e("XMPPManager", "getInstanceFor reported failed ping, calling maybeStartReconnect()");
                    mCurrentRetryCount = 0;
                    connectionState = ConnectionState.DISCONNECTED;
                    Utilities.RunOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            NotificationCenter.getInstance().postNotificationName(connectionStateDidChanged);
                        }
                    });
                    maybeStartReconnect();
                    lastPing = now;
                } else {
                    FileLog.e("XMPPManager", "Ping failure reported too early. Skipping this occurrence.");
                }
                FileLog.e("XMPPManager", "PingManager reported failed ping");
            }
        });
        try {
            XHTMLManager.setServiceEnabled(connection, false);
        } catch (Exception e) {
            FileLog.e("XMPPManager", "Failed to set ServiceEnabled flag for XHTMLManager", e);
            if (e.getMessage() == null) {
                restartConnection();
            }
        }
        try {
            FileLog.e("XMPPManager", "Login with " + UserConfig.currentUser.phone + "@yahala\\" + RESOURCE);
            connection.login();
            DeliveryReceiptManager.getInstanceFor(connection).setAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always); 
            DeliveryReceiptManager.getInstanceFor(connection).addReceiptReceivedListener(new ReceiptReceivedListener() {
                @Override
                public void onReceiptReceived(final Jid fromJid, Jid toJid, final String receiptId, Stanza receipt) {
                    XmppNotifications.updateMessageSendState(receiptId, fromJid.toString(), "", MESSAGE_SEND_STATE_AKN);
                    Utilities.RunOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            NotificationCenter.getInstance().postNotificationName(XMPPManager.messageReceivedByAck, fromJid.toString(), receiptId.toString());
                        }
                    });
                }
            });
            sendPresenceRequest(connection);
        } catch (Exception e) {
            FileLog.e("XMPPManager login failed", "", e);
            if (e instanceof SmackException.AlreadyLoggedInException) {
                FileLog.e("XMPPManager", "Already logged in");
                return true;
            } else if (e instanceof XMPPException.StreamErrorException) {
                FileLog.e("XMPPManager", "XMPP connection failed because of stream error: " + e.getMessage());
                maybeStartReconnect();
            } else {
                stop();
            }
            return false;
        }
        return true;
    }
private boolean connectAndAuth ( XMPPTCPConnection connection ) { if ( UserConfig . currentUser == null ) UserConfig . loadConfig ( ) ; try { FileLog . d ( "XMPPManager" , "Connecting to " + HOST + ":" + SPORT ) ; connection . connect ( ) ; registerRoasterListener ( connection ) ; } catch ( Exception e ) { FileLog . e ( "XMPPManager" , "XMPP connection failed" , e ) ; if ( e instanceof XMPPException ) { XMPPException xmppEx = ( XMPPException ) e ; } connectionState = ConnectionState . DISCONNECTED ; maybeStartReconnect ( ) ; Utilities . RunOnUIThread ( new Runnable ( ) { @ Override public void run ( ) { NotificationCenter . getInstance ( ) . postNotificationName ( connectionStateDidChanged ) ; } } ) ; return false ; } if ( connection . isAuthenticated ( ) ) { return true ; } FileLog . e ( "XMPPManager" , "Service discovery" ) ; mPingManager = PingManager . getInstanceFor ( connection ) ; mPingManager . setDefaultPingInterval ( 30 ) ; mPingManager . setPingInterval ( 30 ) ; mPingManager . registerPingFailedListener ( new PingFailedListener ( ) { @ Override public void pingFailed ( ) { long now = new Date ( ) . getTime ( ) ; if ( now - lastPing > 30000 ) { FileLog . e ( "XMPPManager" , "getInstanceFor reported failed ping, calling maybeStartReconnect()" ) ; mCurrentRetryCount = 0 ; connectionState = ConnectionState . DISCONNECTED ; Utilities . RunOnUIThread ( new Runnable ( ) { @ Override public void run ( ) { NotificationCenter . getInstance ( ) . postNotificationName ( connectionStateDidChanged ) ; } } ) ; maybeStartReconnect ( ) ; lastPing = now ; } else { FileLog . e ( "XMPPManager" , "Ping failure reported too early. Skipping this occurrence." ) ; } FileLog . e ( "XMPPManager" , "PingManager reported failed ping" ) ; } } ) ; try { XHTMLManager . setServiceEnabled ( connection , false ) ; } catch ( Exception e ) { FileLog . e ( "XMPPManager" , "Failed to set ServiceEnabled flag for XHTMLManager" , e ) ; if ( e . getMessage ( ) == null ) { restartConnection ( ) ; } } try { FileLog . e ( "XMPPManager" , "Login with " + UserConfig . currentUser . phone + "@yahala\\" + RESOURCE ) ; connection . login ( ) ; DeliveryReceiptManager . getInstanceFor ( connection ) . setAutoReceiptMode ( DeliveryReceiptManager . AutoReceiptMode . always ) ; DeliveryReceiptManager . getInstanceFor ( connection ) . addReceiptReceivedListener ( new ReceiptReceivedListener ( ) { @ Override public void onReceiptReceived ( final Jid fromJid , Jid toJid , final String receiptId , Stanza receipt ) { XmppNotifications . updateMessageSendState ( receiptId , fromJid . toString ( ) , "" , MESSAGE_SEND_STATE_AKN ) ; Utilities . RunOnUIThread ( new Runnable ( ) { @ Override public void run ( ) { NotificationCenter . getInstance ( ) . postNotificationName ( XMPPManager . messageReceivedByAck , fromJid . toString ( ) , receiptId . toString ( ) ) ; } } ) ; } } ) ; sendPresenceRequest ( connection ) ; } catch ( Exception e ) { FileLog . e ( "XMPPManager login failed" , "" , e ) ; if ( e instanceof SmackException . AlreadyLoggedInException ) { FileLog . e ( "XMPPManager" , "Already logged in" ) ; return true ; } else if ( e instanceof XMPPException . StreamErrorException ) { FileLog . e ( "XMPPManager" , "XMPP connection failed because of stream error: " + e . getMessage ( ) ) ; maybeStartReconnect ( ) ; } else { stop ( ) ; } return false ; } return true ; }
Tries to fully establish the given XMPPConnection
 Calls maybeStartReconnect() or stop() in an error case
Tries to fully establish the given XMPPConnection Calls maybeStartReconnect () or stop () in an error case
// StreamError error = xmppEx.getStreamError(); // // Make sure the error is not null //if (error != null) { //    FileLog.e("XMPPManager","XMPP connection failed because of stream error: " + error.toString()); // } // if we reuse the connection and the auth was done with the connect() //add roster listener - must added befor login. // registerRoasterListener(connection); // org.jivesoftware.smackx.muc. // Managing an issue with ServiceDiscoveryManager //UserConfig.loadConfig(); // connection.login(UserConfig.currentUser.phone, "admin", RESOURCE); //get offline messages //enableAutoReceipts(); // FileLog.e("XMPPManager messageReceivedByAck ", fromJid + " " + receiptId); //send presence request after login //change presence to online // sadly, smack throws the same generic XMPPException for network // related messages (eg "no response from the server") as for // authoritative login errors (ie, bad password).  The only // differentiation is the message itself which starts with this // hard-coded string. //  if (e instanceof org.apache.harmony.javax.security.sasl.SaslException) { //     // doesn't look like a bad username/password, so retry //      maybeStartReconnect(); //  } else