Code example for Lock

Methods: sleeplockInterruptiblyunlock

0
     
    private void applyTransition( int transition )
    { 
        try 
        { 
            LOCK.lockInterruptibly();
            switch( transition )
            { 
                case TRANSITION_CONNECT:
                    if( connectionState == STATE_DISCONNECTED || connectionState == STATE_DISCONNECTING || connectionState == STATE_DISCONNECTED 
                        || connectionState == STATE_PAUSED || connectionState == STATE_PAUSING )
                    { 
                        connectionState = STATE_CONNECTING;
                        LOCK.unlock();
                        doConnect(); 
                    } 
                    else 
                    { 
                        LOCK.unlock();
                    } 
                    break; 
                case TRANSITION_CONNECTED:
                    if( connectionState == STATE_CONNECTING )
                    { 
                        connectionState = STATE_CONNECTED;
                        LOCK.unlock();
                        doConnected(); 
                    } 
                    else 
                    { 
                        LOCK.unlock();
                    } 
                    break; 
                case TRANSITION_PAUSE:
                    if( connectionState == STATE_CONNECTED )
                    { 
                        connectionState = STATE_PAUSING;
                        LOCK.unlock();
                        doPause(); 
                    } 
                    else 
                    { 
                        LOCK.unlock();
                    } 
                    break; 
                     
                case TRANSITION_PAUSED:
                    if( connectionState == STATE_PAUSING )
                    { 
                        connectionState = STATE_PAUSED;
                        LOCK.unlock();
                        doPaused(); 
                    } 
                    else 
                    { 
                        LOCK.unlock();
                    } 
                    break; 
                     
                     
                case TRANSITION_RECONNECT:
                    if( connectionState == STATE_DISCONNECTED || connectionState == TRANSITION_DISCONNECT )
                    { 
                        connectionState = STATE_CONNECTING;
                        LOCK.unlock();
                        doConnect(); 
                    } 
                    else if( connectionState == STATE_CONNECTING )
                    { 
                        connectionState = STATE_CONNECTING;
                        LOCK.unlock();
                        treeggerService.onConnectingFinished();
                        doReconnect(); 
                    } 
                    else if( connectionState == STATE_CONNECTED )
                    { 
                        connectionState = STATE_CONNECTING;
                        LOCK.unlock();
                        doReconnect(); 
                    } 
                    else if( connectionState == STATE_PAUSED || connectionState == STATE_PAUSING )
                    { 
                        connectionState = STATE_CONNECTING;
                        LOCK.unlock();
                        doResume();                     
                    } 
                    else 
                    { 
                        LOCK.unlock();
                    } 
                    break; 
                case TRANSITION_DISCONNECT:
                    if( connectionState == STATE_CONNECTING )
                    { 
                        connectionState = STATE_DISCONNECTING;
                        LOCK.unlock();
                        treeggerService.onConnectingFinished();
                        doDisconnect(); 
                    } 
                    else if( connectionState == STATE_PAUSED || connectionState == STATE_CONNECTED )
                    { 
                        connectionState = STATE_DISCONNECTING;
                        LOCK.unlock();
                        doDisconnect(); 
                    } 
                    else 
                    { 
                        LOCK.unlock();
                    } 
                    break; 
                case TRANSITION_DISCONNECTED:
                    if( connectionState == STATE_DISCONNECTING )
                    { 
                        connectionState = STATE_DISCONNECTED;
                        LOCK.unlock();
                        doDisconnected(); 
                    } 
                    else 
                    { 
                        LOCK.unlock();
                    } 
                    break; 
                     
                case TRANSITION_RESUME:
                    if( connectionState == STATE_PAUSED )
                    { 
                        connectionState = STATE_CONNECTING;
                        LOCK.unlock();
                        doResume();                     
                    } 
                    else 
                    { 
                        LOCK.unlock();
                    } 
                    break; 
                case TRANSITION_SIGNOUT:
                    if( !signout && ( connectionState == STATE_PAUSED || connectionState == STATE_PAUSING ) )
                    { 
                        connectionState = STATE_CONNECTING;
                        signout = true;
                        LOCK.unlock();
                        doResume();        
                    } 
                    else 
                    { 
                        connectionState = STATE_SIGNINGDOUT;
                        LOCK.unlock();
                        doSignOut();                     
                    } 
                    break; 
                default: 
                   LOCK.unlock();
            } 
        } 
        catch (Exception e)
        { 
            Log.e( TAG, e.getMessage(), e );
        } 
    } 
     
    private void doConnect() 
    { 
        lastActivity = System.currentTimeMillis();
        treeggerService.onConnecting();
        try 
        { 
           if( wsConnector != null ) wsConnector.connect( "wss", "xmpp.treegger.com", 443, "/tg-1.0", true, this );
        } 
        catch ( IOException e )
        { 
            Log.v(TAG, e.getMessage(), e );
        } 
    } 
    private void doConnected() 
    { 
        if( timer == null )
        { 
            timer = new Timer();
            timer.schedule( new PingTask(), PING_DELAY, PING_DELAY );
        } 
 
        treeggerService.onConnectingFinished();
        treeggerService.onAuthenticating();
        authenticate( account.name, account.socialnetwork, account.password );
    } 
    private void doPause() 
    { 
        try 
        { 
            if( wsConnector != null  ) wsConnector.close();
        } 
        catch ( IOException e )
        { 
            Log.v(TAG, e.getMessage(), e );
        } 
        applyTransition( TRANSITION_PAUSED );
    } 
    private void doPaused() 
    { 
        treeggerService.onPaused();
    } 
 
    private void doResume() 
    { 
        //treeggerService.handler.post( new DisplayToastRunnable( treeggerService, "Resume connection " + account.name + "@"+account.socialnetwork ) ); 
        try 
        { 
           if( wsConnector != null ) wsConnector.connect( "wss", "xmpp.treegger.com", 443, "/tg-1.0", true, this );
        } 
        catch ( IOException e )
        { 
            Log.v(TAG, "Resume failed");
        } 
    } 
    private void doReconnect() 
    { 
        doDisconnect(); 
        doConnect(); 
    } 
     
    private void doDisconnect() 
    { 
        try 
        { 
            if( timer != null ) timer.cancel();
            timer = null;
            if( wsConnector != null ) wsConnector.close();
        } 
        catch ( IOException e )
        { 
            Log.v(TAG, e.getMessage(), e );
        } 
        applyTransition( TRANSITION_DISCONNECTED );
         
    } 
    private void doDisconnected() 
    { 
        treeggerService.onDisconnected();
    } 
    private void doSignOut() 
    { 
        try 
        { 
            sendPresence( "unavailable", "", "" ); 
            if( timer != null ) timer.cancel();
            timer = null;
            Thread.sleep( 1000 ); // seems to need a little time to send data before closing & exit
            if( wsConnector != null ) wsConnector.close();
        } 
        catch ( Exception e )
        { 
            Log.v(TAG, e.getMessage(), e );
Connect your IDE to all the code out there  Get Codota for Java