org.jivesoftware.smack.RosterEntry Java Examples

The following examples show how to use org.jivesoftware.smack.RosterEntry. 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: InstantMessagingClient.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * Used by Velocity renderer
 * 
 * @param groupname
 * @return a String representing the online buddies out of the number of total buddies for a single group like (3/5)
 */
protected String buddyCountOnlineForGroup(final String groupname) {
    final RosterGroup rosterGroup = connection.getRoster().getGroup(groupname);
    int buddyEntries = rosterGroup.getEntryCount();
    final int allBuddies = buddyEntries;
    for (final Iterator I = rosterGroup.getEntries().iterator(); I.hasNext();) {
        final RosterEntry entry = (RosterEntry) I.next();
        final Presence presence = connection.getRoster().getPresence(entry.getUser());
        if (presence.getType() == Presence.Type.unavailable) {
            buddyEntries--;
        }
    }
    // final string looks like e.g. "(3/5)"
    final StringBuilder sb = new StringBuilder(10);
    sb.append("(");
    sb.append(buddyEntries);
    sb.append("/");
    sb.append(allBuddies);
    sb.append(")");
    return sb.toString();
}
 
Example #2
Source File: XmppManager.java    From weixin with Apache License 2.0 6 votes vote down vote up
/**
	 * 获取所有好友信息
	 * 
	 * @param roster
	 * @return
	 */
	public List<User> getAllUser(Roster roster) {
		List<User> mList_user = new ArrayList<User>();
		Collection<RosterEntry> entries = roster.getEntries();
		Iterator<RosterEntry> iterator = entries.iterator();
		while (iterator.hasNext()) {
			RosterEntry entry = iterator.next();
			User user = new User();
			user.setUserAccount(entry.getName());
			user.setUserPhote(String.valueOf(R.drawable.user_picture));
//			user.setUserImage(getUserImage(entry.getName()));
			mList_user.add(user);
			L.i(LOGTAG, "------获取好友-getAllUser----------------------");
			L.i(LOGTAG, "getAllUser-getName-->" + entry.getName());
			L.i(LOGTAG, "getAllUser-getUser-->" + entry.getUser());
			L.i(LOGTAG, "getAllUser-getStatus-->" + entry.getStatus());
		}
		return mList_user;
	}
 
Example #3
Source File: XmppManager.java    From weixin with Apache License 2.0 6 votes vote down vote up
/**
 * 删除好友
 * 
 * @param roster
 * @param userName
 * @return
 */
public boolean removeUser(Roster roster, String userName) {
	try {
		if (userName.contains("@")) {
			userName = userName.split("@")[0];
		}
		RosterEntry entry = roster.getEntry(userName);
		L.i(LOGTAG, "删除好友--->" + userName);
		L.d(LOGTAG, "User." + (roster.getEntry(userName) == null));
		roster.removeEntry(entry);
		return true;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return false;
}
 
Example #4
Source File: XmppConnection.java    From weixin with Apache License 2.0 6 votes vote down vote up
/**
 * 删除好友
 * 
 * @param userName
 * @return
 */
public boolean removeUser(String userName) {
	if (getConnection() == null)
		return false;
	try {
		RosterEntry entry = null;
		if (userName.contains("@"))
			entry = getConnection().getRoster().getEntry(userName);
		else
			entry = getConnection().getRoster().getEntry(userName + "@" + getConnection().getServiceName());
		if (entry == null)
			entry = getConnection().getRoster().getEntry(userName);
		getConnection().getRoster().removeEntry(entry);

		return true;
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	}
}
 
Example #5
Source File: XMPPContactsService.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Remove a contact completely from roster.
 *
 * <p>This method runs asynchronous and can fail silently (logging errors). If the removal was
 * successful, the roster sends an update and this will change the contact list leading to a
 * notification to all {@link IContactsUpdate} listener.
 *
 * <p>Known errors:
 *
 * <ul>
 *   <li>Smack possibly includes 'ask' attribute in roster items when sending requests
 *       (https://issues.igniterealtime.org/browse/SMACK-766) - catching exception, deletion still
 *       works sometimes, should be fixed by Smack update 4.2.1
 * </ul>
 *
 * @param contact XMPPContact to remove from roster
 */
public void removeContact(XMPPContact contact) {
  contactsExecutor.execute(
      () -> {
        if (roster == null) return;

        RosterEntry entry = roster.getEntry(contact.getBareJid().getRAW());
        if (entry == null) {
          log.error("Remove of " + contact + " was not possible, RosterEntry not found!");
          return;
        }

        try {
          roster.removeEntry(entry);
        } catch (XMPPException e) {
          log.error("Remove of " + contact + " was not possible", e);
        }
      });
}
 
Example #6
Source File: XMPPContactsService.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Rename a contact on roster.
 *
 * <p>This method runs asynchronous and can fail silently. If the rename was successful, the
 * roster sends an update and this will change the contact leading to a notification to all {@link
 * IContactsUpdate} listener.
 *
 * <p>Known errors:
 *
 * <ul>
 *   <li>Contact rename only works correct after subscription is done. If the subscription is
 *       pending, Smack will provide a listener update and a new getEntry will provide the changed
 *       nickname, but this action is only local and not visible after reconnect.
 * </ul>
 *
 * @param contact XMPPContact to rename on roster
 */
public void renameContact(XMPPContact contact, String newName) {
  contactsExecutor.execute(
      () -> {
        if (roster == null) return;

        RosterEntry entry = roster.getEntry(contact.getBareJid().getRAW());
        if (entry == null) {
          log.error("Rename of " + contact + " was not possible, RosterEntry not found!");
          return;
        }

        String changeTo = newName;
        if (changeTo != null) {
          changeTo = changeTo.trim();
          if ("".equals(changeTo)) changeTo = null;
        }
        if (!Objects.equals(entry.getName(), changeTo)) entry.setName(changeTo);
      });
}
 
Example #7
Source File: SmackConnection.java    From SmackAndroidDemo with Apache License 2.0 6 votes vote down vote up
private void rebuildRoster() {
    mRoster = new ArrayList<>();
    String status;
    for (RosterEntry entry : mConnection.getRoster().getEntries()) {
        if(mConnection.getRoster().getPresence(entry.getUser()).isAvailable()){
            status = "Online";
        } else {
            status = "Offline";
        }
        mRoster.add(entry.getUser()+ ": " + status);
    }

    Intent intent = new Intent(SmackService.NEW_ROSTER);
    intent.setPackage(mApplicationContext.getPackageName());
    intent.putStringArrayListExtra(SmackService.BUNDLE_ROSTER, mRoster);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    }
    mApplicationContext.sendBroadcast(intent);
}
 
Example #8
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * Used by Velocity renderer
 * 
 * @return a String representing the online buddies out of the number of total buddies
 */
public String buddyCountOnline() {
    int onlineBuddyEntries = connection.getRoster().getEntryCount();
    final int allBuddies = onlineBuddyEntries;
    for (final Iterator l = connection.getRoster().getEntries().iterator(); l.hasNext();) {
        final RosterEntry entry = (RosterEntry) l.next();
        final Presence presence = connection.getRoster().getPresence(entry.getUser());
        if (presence.getType() == Presence.Type.unavailable) {
            onlineBuddyEntries--;
        }
    }
    // final string looks like e.g. "(3/5)"
    final StringBuilder sb = new StringBuilder(10);
    sb.append("(");
    sb.append(onlineBuddyEntries);
    sb.append("/");
    sb.append(allBuddies);
    sb.append(")");
    return sb.toString();
}
 
Example #9
Source File: XMPPContactsService.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
private void contactsUpdated(Collection<String> addresses) {
  if (roster == null) return;

  for (String address : addresses) {
    XMPPContact contact = contacts.get(address);
    if (contact == null) {
      log.error("Should not happen: Contact for " + address + " not found!");
      continue;
    }

    RosterEntry entry = roster.getEntry(address);
    if (entry == null) {
      log.error("Should not happen: Contact found, but RosterEntry is missing for: " + address);
      continue;
    }

    if (handlePendingSubscription(contact, entry)) notifyListeners(contact, UpdateType.STATUS);

    if (contacts.setContactGroups(contact, entry.getGroups()))
      notifyListeners(contact, UpdateType.GROUP_MAPPING);

    if (contact.setNickname(entry.getName()))
      notifyListeners(contact, UpdateType.NICKNAME_CHANGED);
  }
}
 
Example #10
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * Used by Velocity renderer
 * 
 * @param groupname
 * @return a String representing the online buddies out of the number of total buddies for a single group like (3/5)
 */
protected String buddyCountOnlineForGroup(final String groupname) {
    final RosterGroup rosterGroup = connection.getRoster().getGroup(groupname);
    int buddyEntries = rosterGroup.getEntryCount();
    final int allBuddies = buddyEntries;
    for (final Iterator I = rosterGroup.getEntries().iterator(); I.hasNext();) {
        final RosterEntry entry = (RosterEntry) I.next();
        final Presence presence = connection.getRoster().getPresence(entry.getUser());
        if (presence.getType() == Presence.Type.unavailable) {
            buddyEntries--;
        }
    }
    // final string looks like e.g. "(3/5)"
    final StringBuilder sb = new StringBuilder(10);
    sb.append("(");
    sb.append(buddyEntries);
    sb.append("/");
    sb.append(allBuddies);
    sb.append(")");
    return sb.toString();
}
 
Example #11
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * Used by Velocity renderer
 * 
 * @return a String representing the online buddies out of the number of total buddies
 */
public String buddyCountOnline() {
    int onlineBuddyEntries = connection.getRoster().getEntryCount();
    final int allBuddies = onlineBuddyEntries;
    for (final Iterator l = connection.getRoster().getEntries().iterator(); l.hasNext();) {
        final RosterEntry entry = (RosterEntry) l.next();
        final Presence presence = connection.getRoster().getPresence(entry.getUser());
        if (presence.getType() == Presence.Type.unavailable) {
            onlineBuddyEntries--;
        }
    }
    // final string looks like e.g. "(3/5)"
    final StringBuilder sb = new StringBuilder(10);
    sb.append("(");
    sb.append(onlineBuddyEntries);
    sb.append("/");
    sb.append(allBuddies);
    sb.append(")");
    return sb.toString();
}
 
Example #12
Source File: XmppTool.java    From xmpp with Apache License 2.0 6 votes vote down vote up
/**
 * 判断是否是好友
 *
 * @param
 * @param user
 * @return
 */
public boolean isFriendly(String user) {


    Roster roster = getCon().getRoster();
    List<RosterEntry> list = new ArrayList<RosterEntry>();
    list.addAll(roster.getEntries());
    for (int i = 0; i < list.size(); i++) {
        Log.i("xmppttttttttt", list.get(i).getUser().toUpperCase() + "\t" + user);
        if (list.get(i).getUser().contains(user.toLowerCase())) {
            if (list.get(i).getType().toString().equals("both")) {
                return true;
            } else {
                return false;
            }
        }
    }
    return false;

}
 
Example #13
Source File: XmppTool.java    From xmpp with Apache License 2.0 6 votes vote down vote up
/**
 * 获取某一个分组的成员
 *
 * @param
 * @param groupName
 * @return
 */
public List<RosterEntry> getEntrysByGroup(String groupName) {

    Roster roster = getCon().getRoster();
    List<RosterEntry> list = new ArrayList<RosterEntry>();
    RosterGroup group = roster.getGroup(groupName);
    Collection<RosterEntry> rosterEntiry = group.getEntries();
    Iterator<RosterEntry> iter = rosterEntiry.iterator();
    while (iter.hasNext()) {
        RosterEntry entry = iter.next();
        SLog.i("xmpptool", entry.getUser() + "\t" + entry.getName() + entry.getType().toString());
        if (entry.getType().toString().equals("both")) {
            list.add(entry);
        }

    }
    return list;

}
 
Example #14
Source File: XmppTool.java    From xmpp with Apache License 2.0 6 votes vote down vote up
/**
 * 添加到分组
 *
 * @param
 * @param userName
 * @param groupName
 */
public void addUserToGroup(String userName, String groupName) {
    Roster roster = con.getRoster();
    RosterGroup group = roster.getGroup(groupName);
    if (null == group) {
        group = roster.createGroup(groupName);
    }
    RosterEntry entry = roster.getEntry(userName);
    if (entry != null) {
        try {
            group.addEntry(entry);
        } catch (XMPPException e) {
            SLog.e(tag, Log.getStackTraceString(e));
        }
    }

}
 
Example #15
Source File: RosterAction.java    From jmeter-bzm-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public SampleResult perform(JMeterXMPPSampler sampler, SampleResult res) throws Exception {
    Action action = Action.valueOf(sampler.getPropertyAsString(ACTION, Action.get_roster.toString()));
    Roster roster = sampler.getXMPPConnection().getRoster();
    String entry = sampler.getPropertyAsString(ENTRY);
    res.setSamplerData(action.toString() + ": " + entry);
    if (action == Action.get_roster) {
        res.setResponseData(rosterToString(roster).getBytes());
    } else if (action == Action.add_item) {
        roster.createEntry(entry, entry, new String[0]);
    } else if (action == Action.delete_item) {
        RosterEntry rosterEntry = roster.getEntry(entry);
        if (rosterEntry != null) {
            roster.removeEntry(rosterEntry);
        }
    }

    return res;
}
 
Example #16
Source File: XMPPContact.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new XMPPContact from RosterEntry.
 *
 * @param entry RosterEntry
 * @return new XMPPContact
 */
static XMPPContact from(RosterEntry entry) {
  ContactStatus baseStatus;
  if (entry.getStatus() == RosterPacket.ItemStatus.SUBSCRIPTION_PENDING) {
    baseStatus = ContactStatus.TYPE_SUBSCRIPTION_PENDING;
  } else if (entry.getType() == ItemType.none || entry.getType() == ItemType.from) {
    /* see http://xmpp.org/rfcs/rfc3921.html chapter 8.2.1, 8.3.1 and 8.6 */
    baseStatus = ContactStatus.TYPE_SUBSCRIPTION_CANCELED;
  } else {
    baseStatus = ContactStatus.TYPE_OFFLINE;
  }

  return new XMPPContact(new JID(entry.getUser()), baseStatus, entry.getName());
}
 
Example #17
Source File: RosterAction.java    From jmeter-bzm-plugins with Apache License 2.0 5 votes vote down vote up
private String rosterToString(Roster roster) {
    StringBuilder res = new StringBuilder();
    for (RosterEntry entry : roster.getEntries()) {
        res.append(entry.toString());
        res.append(':');
        res.append(roster.getPresence(entry.getUser()).toString());
        res.append('\n');
    }
    return res.toString();
}
 
Example #18
Source File: XMPPContactsService.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
private void contactsAdded(Collection<String> addresses) {
  if (roster == null) return;

  for (String address : addresses) {
    RosterEntry entry = roster.getEntry(address);
    if (entry == null) {
      log.error("Should not happen: Contact added, but RosterEntry is missing for: " + address);
      continue;
    }

    XMPPContact contact = contacts.getOrCreateXMPPUser(entry);
    contacts.setContactGroups(contact, entry.getGroups());
    notifyListeners(contact, UpdateType.ADDED);
  }
}
 
Example #19
Source File: MessengerService.java    From KlyphMessenger with MIT License 5 votes vote down vote up
private ArrayList<PRosterEntry> getRosterEntries()
{
	Log.d(TAG, "getRosterEntries");
	if (connection != null && connection.isConnected() == true)
	{
		Roster roster = connection.getRoster();
		Collection<RosterEntry> entries = roster.getEntries();

		ArrayList<PRosterEntry> data = new ArrayList<PRosterEntry>();

		for (RosterEntry rosterEntry : entries)
		{
			PRosterEntry re = new PRosterEntry();
			Presence p = roster.getPresence(rosterEntry.getUser());

			re.name = rosterEntry.getName();
			re.user = rosterEntry.getUser();
			re.presence = p.getType().name();

			data.add(re);
		}
		Log.d(TAG, "getRosterEntries " + data.size());
		return data;
	}
	else
	{
		return new ArrayList<PRosterEntry>();
	}
}
 
Example #20
Source File: FriendGroup.java    From League-of-Legends-XMPP-Chat-Library with MIT License 5 votes vote down vote up
/**
 * Gets a list of all Friends in this FriendGroup.
 * 
 * @return list of all Friends in this group
 */
public List<Friend> getFriends() {
	final List<Friend> friends = new ArrayList<>();
	for (final RosterEntry e : get().getEntries()) {
		friends.add(new Friend(api, con, e));
	}
	return friends;
}
 
Example #21
Source File: LolChat.java    From League-of-Legends-XMPP-Chat-Library with MIT License 5 votes vote down vote up
/**
 * Gets a list of your friends based on a given filter.
 * 
 * @param filter
 *            The filter defines conditions that your Friends must meet.
 * @return A List of your Friends that meet the condition of your Filter
 */
public List<Friend> getFriends(Filter<Friend> filter) {
	final ArrayList<Friend> friends = new ArrayList<>();
	for (final RosterEntry e : connection.getRoster().getEntries()) {
		final Friend f = new Friend(this, connection, e);
		if (filter.accept(f)) {
			friends.add(f);
		}
	}
	return friends;
}
 
Example #22
Source File: LolChat.java    From League-of-Legends-XMPP-Chat-Library with MIT License 5 votes vote down vote up
/**
 * Gets a friend based on his XMPPAddress.
 * 
 * @param xmppAddress
 *            For example [email protected]
 * @return The corresponding Friend or null if user is not found or he is
 *         not a friend of you.
 */
public Friend getFriendById(String xmppAddress) {
	final RosterEntry entry = connection.getRoster().getEntry(
			StringUtils.parseBareAddress(xmppAddress));
	if (entry != null) {
		return new Friend(this, connection, entry);
	}
	return null;
}
 
Example #23
Source File: XMPPUtils.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
public static String getDisplayableName(RosterEntry entry) {
  String nickName = entry.getName();
  if (nickName != null && nickName.trim().length() > 0) {
    return nickName.trim();
  }
  return entry.getUser();
}
 
Example #24
Source File: LolChat.java    From League-of-Legends-XMPP-Chat-Library with MIT License 5 votes vote down vote up
/**
 * Gets a friend based on a given filter.
 * 
 * @param filter
 *            The filter defines conditions that your Friend must meet.
 * @return The first Friend that meets the conditions or null if not found.
 */
public Friend getFriend(Filter<Friend> filter) {
	for (final RosterEntry e : connection.getRoster().getEntries()) {
		final Friend f = new Friend(this, connection, e);
		if (filter.accept(f)) {
			return f;
		}
	}
	return null;
}
 
Example #25
Source File: XMPPUtils.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes given contact from the {@link Roster}.
 *
 * @blocking
 * @param rosterEntry the contact that is to be removed
 * @throws XMPPException is thrown if no connection is established.
 */
public static void removeFromRoster(Connection connection, RosterEntry rosterEntry)
    throws XMPPException {
  if (!connection.isConnected()) {
    throw new XMPPException("Not connected");
  }
  connection.getRoster().removeEntry(rosterEntry);
}
 
Example #26
Source File: XmppManager.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 获得某个组里面的所有好友
 * 
 * @param roster
 * @param groupName 组名
 * @return
 */
public List<RosterEntry> getEntriesByGroup(Roster roster, String groupName) {
	List<RosterEntry> entriesList = new ArrayList<RosterEntry>();
	RosterGroup rosterGroup = roster.getGroup(groupName);
	Collection<RosterEntry> rosterEntries = rosterGroup.getEntries();
	Iterator<RosterEntry> i = rosterEntries.iterator();
	while (i.hasNext()) {
		entriesList.add(i.next());
	}
	return entriesList;
}
 
Example #27
Source File: XmppConnection.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 获取所有好友信息
 * 
 * @return
 */
public List<RosterEntry> getAllEntries() {
	if (getConnection() == null)
		return null;
	List<RosterEntry> Entrieslist = new ArrayList<RosterEntry>();
	Collection<RosterEntry> rosterEntry = getConnection().getRoster().getEntries();
	Iterator<RosterEntry> i = rosterEntry.iterator();
	while (i.hasNext()) {
		Entrieslist.add(i.next());
	}
	return Entrieslist;
}
 
Example #28
Source File: XmppConnection.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 获取某个组里面的所有好友
 * 
 * @param roster
 * @param groupName 组名
 * @return
 */
public List<RosterEntry> getEntriesByGroup(String groupName) {
	if (getConnection() == null)
		return null;
	List<RosterEntry> Entrieslist = new ArrayList<RosterEntry>();
	RosterGroup rosterGroup = getConnection().getRoster().getGroup(groupName);
	Collection<RosterEntry> rosterEntry = rosterGroup.getEntries();
	Iterator<RosterEntry> i = rosterEntry.iterator();
	while (i.hasNext()) {
		Entrieslist.add(i.next());
	}
	return Entrieslist;
}
 
Example #29
Source File: XmppTool.java    From xmpp with Apache License 2.0 5 votes vote down vote up
/**
 * 删除好友
 *
 * @param userName
 * @return
 */
public boolean removeUser(String userName) {
    Roster roster = con.getRoster();
    try {
        RosterEntry entry = roster.getEntry(userName);
        if (null != entry) {
            roster.removeEntry(entry);
        }
        return true;
    } catch (XMPPException e) {
        SLog.e(tag, Log.getStackTraceString(e));
    }
    return false;
}
 
Example #30
Source File: XmppLoadRosterRunnable.java    From yiim_v2 with GNU General Public License v2.0 5 votes vote down vote up
private void loadRosterEntry(long groupId, Collection<RosterEntry> entries,
		Roster roster, String owner) throws Exception {
	try {
		for (RosterEntry rosterEntry : entries) {
			String userId = StringUtils.escapeUserResource(rosterEntry
					.getUser());

			XmppVcard vCard = new XmppVcard(mContext);
			vCard.load(XmppConnectionUtils.getInstance().getConnection(),
					userId, true);

			ContentValues enValues = new ContentValues();
			enValues.put(RosterColumns.GROUP_ID, groupId);
			enValues.put(RosterColumns.USERID, userId);
			enValues.put(RosterColumns.MEMO_NAME, rosterEntry.getName());

			enValues.put(RosterColumns.ROSTER_TYPE, rosterEntry.getType()
					.toString());

			enValues.put(RosterColumns.OWNER, owner);

			mContext.getContentResolver().insert(RosterColumns.CONTENT_URI,
					enValues);
		}
	} catch (Exception e) {
		throw e;
	}
}