public void presenceChanged(Presence p) {
    String from = p.getFrom();
    if (from != null) {
      p = connection.getRoster().getPresence(p.getFrom());
      from = StringUtils.parseBareAddress(from);
      final Friend friend = api.getFriendById(from);
      if (friend != null) {
        for (final FriendListener l : api.getFriendListeners()) {
          final Presence.Type previousType = typeUsers.get(from);
          if (p.getType() == Presence.Type.available
              && (previousType == null || previousType != Presence.Type.available)) {
            l.onFriendJoin(friend);
          } else if (p.getType() == Presence.Type.unavailable
              && (previousType == null || previousType != Presence.Type.unavailable)) {
            l.onFriendLeave(friend);
          }

          final Presence.Mode previousMode = modeUsers.get(from);
          if (p.getMode() == Presence.Mode.chat
              && (previousMode == null || previousMode != Presence.Mode.chat)) {
            l.onFriendAvailable(friend);
          } else if (p.getMode() == Presence.Mode.away
              && (previousMode == null || previousMode != Presence.Mode.away)) {
            l.onFriendAway(friend);
          } else if (p.getMode() == Presence.Mode.dnd
              && (previousMode == null || previousMode != Presence.Mode.dnd)) {
            l.onFriendBusy(friend);
          }

          if (p.getStatus() != null) {
            try {
              final LolStatus previousStatus = statusUsers.get(from);
              final LolStatus newStatus = new LolStatus(p.getStatus());
              if (previousStatus != null && !newStatus.equals(previousStatus)) {
                l.onFriendStatusChange(friend);
              }
            } catch (JDOMException | IOException e) {
            }
          }
        }

        typeUsers.put(from, p.getType());
        modeUsers.put(from, p.getMode());
        if (p.getStatus() != null) {
          try {
            statusUsers.put(from, new LolStatus(p.getStatus()));
          } catch (JDOMException | IOException e) {
          }
        }
      }
    }
  }
 public void updateUser(Presence presence) {
   boolean newUser = true;
   Contact removeC = null;
   for (Contact c : contactList) {
     if (c.getRosterEntry().getUser().equals(StringUtils.parseBareAddress(presence.getFrom()))) {
       newUser = false;
       if (!(presence.isAvailable() || presence.isAway())) {
         removeC = c;
       }
       break;
     }
   }
   if (removeC != null) {
     contactList.remove(removeC);
   }
   if (newUser) {
     RosterEntry re = roster.getEntry(StringUtils.parseBareAddress(presence.getFrom()));
     Contact c = new Contact();
     if (re.getName() == null || re.getName().trim().equals("")) c.setName(re.getUser());
     else c.setName(re.getName());
     c.setStatus(presence.getStatus());
     c.setRosterEntry(re);
     contactList.add(c);
   }
 }
Esempio n. 3
0
  /**
   * 根据RosterEntry创建一个User
   *
   * @param entry
   * @return
   */
  public User transEntryToUser(RosterEntry entry, Roster roster) {
    User user = new User();
    user.setJID(entry.getUser());
    Presence presence = roster.getPresence(entry.getUser());
    user.setFrom(presence.getFrom());
    user.setStatus(presence.getStatus());
    user.setAvailable(presence.isAvailable());
    user.setType(entry.getType());

    // user.setvCard(userManager.getUserVCard(entry.getUser()));
    if (entry.getName() == null) {
      //			if(user.getvCard()==null)
      //			user.setName(StringUtil.getUserNameByJid(entry.getUser()));
      //			else {
      //				user.setName(user.getvCard().getNickName());
      //			}
      user.setName(StringUtil.getUserNameByJid(entry.getUser()));
    } else {
      user.setName(entry.getName());
    }
    String avatar = UserManager.saveAvatar(entry.getUser(), context);
    user.setAvatar_path(avatar);

    return user;
  }
Esempio n. 4
0
  /**
   * Updates user's changed presence
   *
   * @param presence
   */
  public void presenceChanged(Presence presence) {

    String from = presence.getFrom();

    // Log.i(tag, presence.getMode().toString());
    new BuddiesController(context)
        .notifyPresenceChanged(from, presence.getMode(), presence.getStatus());
    return;
  }
Esempio n. 5
0
  private void handleIncomingSubscribe(Presence request) {
    final ContentValues values = new ContentValues();

    values.put(RosterConstants.JID, request.getFrom());
    values.put(RosterConstants.ALIAS, request.getFrom());
    values.put(RosterConstants.GROUP, "");

    values.put(RosterConstants.STATUS_MODE, getStatusInt(request));
    values.put(RosterConstants.STATUS_MESSAGE, request.getStatus());

    Uri uri = contentResolver.insert(RosterProvider.CONTENT_URI, values);
    Log.d(TAG, "handleIncomingSubscribe: faked " + uri);
  }
Esempio n. 6
0
  private ContentValues getContentValuesForRosterEntry(final RosterEntry entry) {
    final ContentValues values = new ContentValues();

    values.put(RosterConstants.JID, entry.getUser());
    values.put(RosterConstants.ALIAS, getName(entry));

    Presence presence = mRoster.getPresence(entry.getUser());
    values.put(RosterConstants.STATUS_MODE, getStatusInt(presence));
    values.put(RosterConstants.STATUS_MESSAGE, presence.getStatus());
    values.put(RosterConstants.GROUP, getGroup(entry.getGroups()));

    return values;
  }
Esempio n. 7
0
  private ContentValues getContentValuesForRosterEntry(final RosterEntry entry) {
    final ContentValues values = new ContentValues();

    values.put(RosterConstants.JID, entry.getUser());
    values.put(RosterConstants.ALIAS, getName(entry));

    Presence presence = roster.getPresence(entry.getUser());
    values.put(RosterConstants.STATUS_MODE, getStatusInt(presence));
    if (presence.getType() == Presence.Type.error) {
      values.put(RosterConstants.STATUS_MESSAGE, presence.getError().toString());
    } else values.put(RosterConstants.STATUS_MESSAGE, presence.getStatus());
    values.put(RosterConstants.GROUP, getGroup(entry.getGroups()));

    return values;
  }
Esempio n. 8
0
 @Override
 public String toString() {
   if (toS == null) {
     StringBuilder sb = new StringBuilder();
     sb.append(entry.getName()).append("(").append(entry.getUser()).append(")\n");
     if (presence.getType() == Presence.Type.available) {
       sb.append("(online)");
     } else {
       sb.append("(offline)");
     }
     sb.append(presence.getStatus());
     toS = sb.toString();
   }
   return toS;
 }
Esempio n. 9
0
  // carefull, this method does also get called by the SmackListener Thread
  @Override
  public void presenceChanged(Presence presence) {
    String bareUserId = StringUtils.parseBareAddress(presence.getFrom());

    Intent intent = new Intent(MainService.ACTION_XMPP_PRESENCE_CHANGED);
    intent.putExtra("userid", bareUserId);
    intent.putExtra("fullid", presence.getFrom());
    intent.putExtra("state", retrieveState(presence.getMode(), presence.isAvailable()));
    intent.putExtra("status", presence.getStatus());
    sContext.sendBroadcast(intent);

    // TODO Make this a general intent action.NOTIFICATION_ADDRESS_AVAILABLE
    // and handle it for example within XmppPresenceStatus
    // if the notification address is/has become available, update the resource status string
    if (sSettings.containsNotifiedAddress(bareUserId) && presence.isAvailable()) {
      intent = new Intent(MainService.ACTION_COMMAND);
      intent.setClass(sContext, MainService.class);
      intent.putExtra("cmd", "batt");
      intent.putExtra("args", "silent");
      MainService.sendToServiceHandler(intent);
    }
  }
Esempio n. 10
0
  /** Saves the VCard. */
  private void saveVCard() {
    final VCard vcard = new VCard();

    // Save personal info
    vcard.setFirstName(personalPanel.getFirstName());
    vcard.setLastName(personalPanel.getLastName());
    vcard.setMiddleName(personalPanel.getMiddleName());
    vcard.setEmailHome(personalPanel.getEmailAddress());
    vcard.setNickName(personalPanel.getNickname());

    // Save business info
    vcard.setOrganization(businessPanel.getCompany());
    vcard.setAddressFieldWork("STREET", businessPanel.getStreetAddress());
    vcard.setAddressFieldWork("LOCALITY", businessPanel.getCity());
    vcard.setAddressFieldWork("REGION", businessPanel.getState());
    vcard.setAddressFieldWork("PCODE", businessPanel.getZipCode());
    vcard.setAddressFieldWork("CTRY", businessPanel.getCountry());
    vcard.setField("TITLE", businessPanel.getJobTitle());
    vcard.setOrganizationUnit(businessPanel.getDepartment());
    vcard.setPhoneWork("VOICE", businessPanel.getPhone());
    vcard.setPhoneWork("FAX", businessPanel.getFax());
    vcard.setPhoneWork("PAGER", businessPanel.getPager());
    vcard.setPhoneWork("CELL", businessPanel.getMobile());
    vcard.setField("URL", businessPanel.getWebPage());

    // Save Home Info
    vcard.setAddressFieldHome("STREET", homePanel.getStreetAddress());
    vcard.setAddressFieldHome("LOCALITY", homePanel.getCity());
    vcard.setAddressFieldHome("REGION", homePanel.getState());
    vcard.setAddressFieldHome("PCODE", homePanel.getZipCode());
    vcard.setAddressFieldHome("CTRY", homePanel.getCountry());
    vcard.setPhoneHome("VOICE", homePanel.getPhone());
    vcard.setPhoneHome("FAX", homePanel.getFax());
    vcard.setPhoneHome("PAGER", homePanel.getPager());
    vcard.setPhoneHome("CELL", homePanel.getMobile());

    // Save Avatar
    final File avatarFile = avatarPanel.getAvatarFile();
    byte[] avatarBytes = avatarPanel.getAvatarBytes();

    if (avatarFile != null) {
      try {
        // Make it 48x48
        ImageIcon icon = new ImageIcon(avatarFile.toURI().toURL());
        Image image = icon.getImage();
        image = image.getScaledInstance(-1, 48, Image.SCALE_SMOOTH);
        avatarBytes = GraphicUtils.getBytesFromImage(image);
      } catch (MalformedURLException e) {
        Log.error("Unable to set avatar.", e);
      }
    }

    // If avatar bytes, persist as vcard.
    if (avatarBytes != null) {
      vcard.setAvatar(avatarBytes);
    }

    try {
      final VCardManager vcardManager = SparkManager.getVCardManager();
      vcardManager.setPersonalVCard(vcard);

      vcard.save(SparkManager.getConnection());

      // Notify users.
      if (avatarFile != null || avatarBytes != null) {
        Presence presence = SparkManager.getWorkspace().getStatusBar().getPresence();
        Presence newPresence =
            new Presence(
                presence.getType(),
                presence.getStatus(),
                presence.getPriority(),
                presence.getMode());

        // Change my own presence
        SparkManager.getSessionManager().changePresence(newPresence);

        // Chnage avatar in status bar.
        StatusBar statusBar = SparkManager.getWorkspace().getStatusBar();
        statusBar.setAvatar(new ImageIcon(vcard.getAvatar()));
      } else {
        String firstName = vcard.getFirstName();
        String lastName = vcard.getLastName();
        StatusBar statusBar = SparkManager.getWorkspace().getStatusBar();
        if (ModelUtil.hasLength(firstName) && ModelUtil.hasLength(lastName)) {
          statusBar.setNickname(firstName + " " + lastName);
        } else if (ModelUtil.hasLength(firstName)) {
          statusBar.setNickname(firstName);
        }

        statusBar.setAvatar(null);
      }

      // Notify listenres
      SparkManager.getVCardManager().notifyVCardListeners();
    } catch (XMPPException e) {
      Log.error(e);
      JOptionPane.showMessageDialog(
          SparkManager.getMainWindow(),
          Res.getString("message.vcard.not.supported"),
          Res.getString("title.error"),
          JOptionPane.ERROR_MESSAGE);
    }
  }
Esempio n. 11
0
  /**
   * Updates the icon of the user based on their presence.
   *
   * @param presence the users presence.
   */
  public void updatePresenceIcon(Presence presence) {
    ChatManager chatManager = SparkManager.getChatManager();
    boolean handled = chatManager.fireContactItemPresenceChanged(this, presence);
    if (handled) {
      return;
    }

    String status = presence.getStatus();
    Icon statusIcon = SparkRes.getImageIcon(SparkRes.GREEN_BALL);
    boolean isAvailable = false;
    if (status == null && presence.isAvailable()) {
      Presence.Mode mode = presence.getMode();
      if (mode == Presence.Mode.available) {
        status = Res.getString("status.online");
        isAvailable = true;
      } else if (mode == Presence.Mode.away) {
        status = Res.getString("status.away");
        statusIcon = SparkRes.getImageIcon(SparkRes.IM_AWAY);
      } else if (mode == Presence.Mode.chat) {
        status = Res.getString("status.free.to.chat");
      } else if (mode == Presence.Mode.dnd) {
        status = Res.getString("status.do.not.disturb");
        statusIcon = SparkRes.getImageIcon(SparkRes.IM_AWAY);
      } else if (mode == Presence.Mode.xa) {
        status = Res.getString("status.extended.away");
        statusIcon = SparkRes.getImageIcon(SparkRes.IM_AWAY);
      }
    }

    if (presence.isAvailable()
        && (presence.getMode() == Presence.Mode.dnd
            || presence.getMode() == Presence.Mode.away
            || presence.getMode() == Presence.Mode.xa)) {
      statusIcon = SparkRes.getImageIcon(SparkRes.IM_AWAY);
    } else if (presence.isAvailable()) {
      isAvailable = true;
    } else if (!presence.isAvailable()) {
      getNicknameLabel().setFont(new Font("Dialog", Font.PLAIN, fontSize));
      getNicknameLabel().setForeground((Color) UIManager.get("ContactItemOffline.color"));

      RosterEntry entry = SparkManager.getConnection().getRoster().getEntry(getJID());
      if (entry != null
          && (entry.getType() == RosterPacket.ItemType.none
              || entry.getType() == RosterPacket.ItemType.from)
          && RosterPacket.ItemStatus.SUBSCRIPTION_PENDING == entry.getStatus()) {
        // Do not move out of group.
        setIcon(SparkRes.getImageIcon(SparkRes.SMALL_QUESTION));
        getNicknameLabel().setFont(new Font("Dialog", Font.PLAIN, fontSize));
        setStatusText(Res.getString("status.pending"));
      } else {
        // We should keep the offline bullet (not available) instead of putting icon null.
        setIcon(SparkRes.getImageIcon(SparkRes.CLEAR_BALL_ICON));
        setFont(new Font("Dialog", Font.PLAIN, fontSize));
        getNicknameLabel().setFont(new Font("Dialog", Font.PLAIN, fontSize));
        setAvailable(false);
        if (ModelUtil.hasLength(status)) {
          setStatusText(status);
        } else {
          setStatusText("");
        }
      }

      sideIcon.setIcon(null);
      setAvailable(false);
      return;
    }

    Icon sIcon = PresenceManager.getIconFromPresence(presence);
    if (sIcon != null) {
      setIcon(sIcon);
    } else {
      setIcon(statusIcon);
    }
    if (status != null) {
      setStatus(status);
    }

    if (PresenceManager.isOnPhone(presence)) {
      statusIcon = SparkRes.getImageIcon(SparkRes.ON_PHONE_IMAGE);
      setIcon(statusIcon);
    }

    // Always change nickname label to black.
    getNicknameLabel().setForeground((Color) UIManager.get("ContactItemNickname.foreground"));

    if (isAvailable) {
      getNicknameLabel().setFont(new Font("Dialog", Font.PLAIN, fontSize));
      if (Res.getString("status.online").equals(status)
          || Res.getString("available").equalsIgnoreCase(status)) {
        setStatusText("");
      } else {
        setStatusText(status);
      }
    } else if (presence.isAvailable()) {
      LocalPreferences pref = SettingsManager.getLocalPreferences();
      if (pref.isGrayingOutEnabled()) {
        getNicknameLabel().setFont(new Font("Dialog", Font.ITALIC, fontSize));
        getNicknameLabel().setForeground(Color.gray);
      } else {
        getNicknameLabel().setFont(new Font("Dialog", Font.PLAIN, fontSize));
        getNicknameLabel().setForeground(Color.black);
      }
      if (status != null) {
        setStatusText(status);
      }
    }

    setAvailable(true);
  }