Java Code Examples for org.jivesoftware.smack.Connection#getRoster()

The following examples show how to use org.jivesoftware.smack.Connection#getRoster() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XMPPContactsService.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
private void connectionChanged(Connection connection, ConnectionState state) {
  if (state == ConnectionState.CONNECTING) {
    roster = connection.getRoster();
    if (roster == null) {
      log.fatal("A new connection without a roster should not happen!");
      return;
    }

    roster.addRosterListener(rosterListener);
    contacts.clear();
  } else if (state == ConnectionState.CONNECTED) {
    /* The roster provided list is probably empty after connection start, and we get contacts
    later via {@link RosterListener#entriesAdded(Collection)}, but just to be sure. */
    contacts.init(roster);
    notifyListeners(null, UpdateType.CONNECTED);
  } else if (state == ConnectionState.DISCONNECTING || state == ConnectionState.ERROR) {
    if (roster != null) roster.removeRosterListener(rosterListener);
    roster = null;

    contacts.getAll().forEach(XMPPContact::removeResources);
    notifyListeners(null, UpdateType.NOT_CONNECTED);
  }
}
 
Example 2
Source File: XmppVcard.java    From yiim_v2 with GNU General Public License v2.0 4 votes vote down vote up
public static XmppVcard loadByVcard(Context context, Connection connection,
		String user, XmppVcard xmppVcard) {
	XmppVcard ret = null;
	if (xmppVcard != null) {
		ret = xmppVcard;
	} else {
		ret = new XmppVcard(context);
	}
	try {
		VCard vCard = new VCard();
		vCard.load(connection, user);

		Roster roster = connection.getRoster();

		ret.setNickName(vCard.getNickName());

		// 在线状态获取
		Presence presence = roster.getPresence(user);
		if (presence.getType() != Type.unavailable) {
			ret.setPresence("online");
		} else {
			ret.setPresence("unavailable");
		}

		// 加载用户头像
		byte[] avatar = vCard.getAvatar();
		if (avatar != null) {
			YiStoreCache.cacheRawData(user, avatar);
		}

		ret.setGender(vCard.getField(Const.SEX));
		ret.setSign(vCard.getField(Const.SIGN));
		ret.setCountry(vCard.getField(Const.COUNTRY));
		ret.setProvince(vCard.getField(Const.PROVINCE));
		ret.setAddress(vCard.getField(Const.ADDRESS));
		ret.setBirthday(Long.valueOf(vCard.getField(Const.BIRTHDAY)));
		ret.setSecondBirthday(Long.valueOf(vCard
				.getField(Const.SECOND_BIRTHDAY)));
		ret.setOnlineTime(Long.valueOf(vCard.getField(Const.ONLINETIME)));
		ret.setRealName(vCard.getField(Const.REALNAME));
		ret.setBloodGroup(vCard.getField(Const.BLOOD_GROUP));
		ret.setPhone(vCard.getField(Const.PHONE));
		ret.setOccupation(vCard.getField(Const.OCCUPATION));
		ret.setEmail(vCard.getField(Const.EMAIL));

		//通知重新加载好友列表
		Intent intent = new Intent(Const.NOTIFY_RELOAD_ROSTER_ENTRIES);
		context.sendBroadcast(intent);
	} catch (Exception e) {
	}

	return ret;
}
 
Example 3
Source File: XMPPUtils.java    From saros with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param connectionService network component that should be used to resolve the nickname or
 *     <code>null</code> to use the default one
 * @param jid the JID to resolve the nickname for
 * @param alternative nickname to return if no nickname is available, can be <code>null</code>
 * @return The nickname associated with the given JID in the current roster or the
 *     <tt>alternative</tt> representation if the current roster is not available or the nickname
 *     has not been set.
 */
public static String getNickname(
    XMPPConnectionService connectionService, final JID jid, final String alternative) {

  if (connectionService == null) connectionService = defaultConnectionService;

  if (connectionService == null) return alternative;

  Connection connection = connectionService.getConnection();

  if (connection == null) return alternative;

  Roster roster = connection.getRoster();

  if (roster == null) return alternative;

  RosterEntry entry = roster.getEntry(jid.getBase());

  if (entry == null) return alternative;

  String nickName = entry.getName();

  if (nickName != null && nickName.trim().length() > 0) return nickName;

  return alternative;
}