@Override
 public void presenceChanged(final org.jivesoftware.smack.packet.Presence presence) {
   String localJID;
   try {
     localJID = getLocalJid();
   } catch (FriendException e) {
     localJID = null;
   }
   if (!presence.getFrom().equals(localJID)) {
     XMPPFriendImpl friend = getFriend(presence);
     if (friend != null) {
       LOG.debugf("presence from {0} changed to {1}", presence.getFrom(), presence.getType());
       // synchronize to avoid updates or presences from being lost
       // better to replace that with a lock object private to this
       // connection class
       synchronized (friend) {
         if (presence.getType().equals(org.jivesoftware.smack.packet.Presence.Type.available)) {
           if (!friend.getPresences().containsKey(presence.getFrom())) {
             addNewPresence(friend, presence);
           } else {
             updatePresence(friend, presence);
           }
         } else if (presence
             .getType()
             .equals(org.jivesoftware.smack.packet.Presence.Type.unavailable)) {
           PresenceImpl p = (PresenceImpl) friend.getPresence(presence.getFrom());
           if (p != null) {
             p.update(presence);
             friend.removePresense(p);
           }
         }
       }
     } else {
       LOG.debugf("no friend for presence {0}", presence.getFrom());
     }
   }
 }
 private void addNewPresence(
     final XMPPFriendImpl friend, final org.jivesoftware.smack.packet.Presence presence) {
   final PresenceImpl presenceImpl = new PresenceImpl(presence, friend, featureSupport);
   presenceImpl.addTransport(AddressFeature.class, addressIQListener);
   presenceImpl.addTransport(AuthTokenFeature.class, authTokenIQListener);
   presenceImpl.addTransport(ConnectBackRequestFeature.class, connectRequestIQListener);
   presenceImpl.addTransport(LibraryChangedNotifierFeature.class, libraryChangedIQListener);
   presenceImpl.addTransport(FileOfferFeature.class, fileTransferIQListener);
   friend.addPresense(presenceImpl);
 }
 private void updatePresence(
     XMPPFriendImpl friend, org.jivesoftware.smack.packet.Presence presence) {
   PresenceImpl currentPresence = (PresenceImpl) friend.getPresences().get(presence.getFrom());
   currentPresence.update(presence);
   friend.updatePresence(currentPresence);
 }