if (ChannelStateEnum.UP.equals(channel.getState()) && callSession != null) {
if (!ChannelStateEnum.UP.equals(channel.getState()) && callSession != null) {
        public void run() {            while (shouldRun) {                if (lastRun + PERIOD > System.currentTimeMillis()) {                    Map channels = asteriskManager.getChannels();                    for (Object o : channels.entrySet()) {                        Map.Entry entry = (Map.Entry) o;                        String uniqueID = (String) entry.getKey();                        Channel channel = (Channel) entry.getValue();                        CallSession callSession = CallSessionFactory.getCallSessionFactory().getCallSession(uniqueID);                        if (ChannelStateEnum.UP.equals(channel.getState()) && callSession != null) {                            Log.debug("AsteriskPhoneManger.ChannelStatusRunnable: User " + callSession.getUsername() +                                    " has no more call sessions, but his presence is " +                                    "still ON_PHONE. Changing to AVAILABLE");                            CallSessionFactory.getCallSessionFactory().destroyPhoneSession(uniqueID);                            if (!restoreUserPresence(callSession.getUsername())) {                                // TODO Remove this code when the "always on-the-phone problem is fixed"                                // Check if the user is available and his presence is still                                // on-the-phone (and no there are no more calls)                                SessionManager sessionManager = XMPPServer.getInstance().getSessionManager();                                Collection<ClientSession> sessions = sessionManager.getSessions(callSession.getUsername());                                for (ClientSession session : sessions) {                                    Presence presence = session.getPresence();                                    Element phoneStatusElement = presence.getElement().element("phone-status");                                    // If the phone-status attribute exists check to see if the status is avaialbable                                    if (phoneStatusElement != null &&                                            PhoneStatus.Status.ON_PHONE.name().equals(phoneStatusElement.attributeValue("status")))                                    {                                        Log.debug("Asterisk-IM HangupTask: User " + callSession.getUsername() +                                                " has no more call sessions, but his presence is " +                                                "still ON_PHONE. Changing to AVAILABLE");                                        // Change presence to available since there are no more active calls                                        phoneStatusElement.addAttribute("status", PhoneStatus.Status.AVAILABLE.name());                                        getInstance().getPresenceRouter().route(presence);                                    }                                }                            }                        }                    }                }                try {                    Thread.sleep(5 * JiveConstants.SECOND);                }                catch (InterruptedException e) {                    // Not really a big deal if this is interrupted                }            }        }