@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; }
/** * 删除好友 * @param xmppConnection * @param userName * @return */ public static boolean removeUser(XMPPConnection xmppConnection,String userName) { try { RosterEntry entry = null; if (userName.contains("@")){ entry = xmppConnection.getRoster().getEntry( userName + "@" + xmppConnection.getServiceName()); }else{ entry = xmppConnection.getRoster().getEntry( userName + "@" + xmppConnection.getServiceName()); } if (entry == null){ entry = xmppConnection.getRoster().getEntry(userName); } xmppConnection.getRoster().removeEntry(entry); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
/** * 删除好友 * @param xmppConnection * @param userName * @return */ public static boolean removeUser(XMPPConnection xmppConnection, String userName) { try { RosterEntry entry = null; if (userName.contains("@")) { entry = xmppConnection.getRoster().getEntry( userName + "@" + xmppConnection.getServiceName()); } else { entry = xmppConnection.getRoster().getEntry( userName + "@" + xmppConnection.getServiceName()); } if (entry == null) { entry = xmppConnection.getRoster().getEntry(userName); } xmppConnection.getRoster().removeEntry(entry); return true; } catch (Exception e) { Log.e("removeUser", e.getMessage()); e.printStackTrace(); return false; } }
/** * 添加到分组 * * @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)); } } }
/** * 获取某一个分组的成员 * * @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; }
/** * 判断是否是好友 * * @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; }
public void findFriends() { try { listGroup=new ArrayList<Group>(); XMPPConnection conn = QQApplication.xmppConnection; Roster roster = conn.getRoster(); Collection<RosterGroup> groups = roster.getGroups(); for (RosterGroup group : groups) { Group mygroup=new Group(); mygroup.setGroupName(group.getName()); Collection<RosterEntry> entries = group.getEntries(); List<Child> childList=new ArrayList<Child>(); for (RosterEntry entry : entries) { if(entry.getType().name().equals("both")){ Child child=new Child(); child.setUsername(entry.getUser().split("@")[0]); childList.add(child); } } mygroup.setChildList(childList); listGroup.add(mygroup); } } catch (Exception e) { e.printStackTrace(); } }
/** * 把一个好友添加到一个组中 * @param userJid * @param groupName */ public static void addUserToGroup(final String userJid, final String groupName, final XMPPConnection connection) { RosterGroup group = connection.getRoster().getGroup(groupName); // 这个组已经存在就添加到这个组,不存在创建一个组 RosterEntry entry = connection.getRoster().getEntry(userJid); try { if (group != null) { if (entry != null) group.addEntry(entry); } else { RosterGroup newGroup = connection.getRoster().createGroup("我的好友"); if (entry != null) newGroup.addEntry(entry); } } catch (Exception e) { e.printStackTrace(); } }
public RosterEntry getEntry(String userOrBuddyId) { RosterEntry entry = null; String id = null; int pos = userOrBuddyId.indexOf("/"); if (pos > 0) { id = userOrBuddyId.substring(0, pos); } else { id = userOrBuddyId; } entry = roster.getEntry(id); if (entry != null) { return entry; } if (idToEntry.containsKey(id)) { return idToEntry.get(id); } return null; }
/** * Notify current presence. * * @param connection * the connection */ private void notifyCurrentPresence(XMPPConnection connection) { if (multiplicityGroup == null) { log.log(Level.WARNING, "Could not find the multiplicity roster group."); return; } log.log(Level.FINE, "Loading roster entries."); for (RosterEntry re : multiplicityGroup.getEntries()) { log.finer("Roster entry check availability on " + re.getUser()); Presence presence = roster.getPresence(re.getUser()); if (presence.isAvailable()) { log.finer(" is available!"); notifyDeviceAvailable(re.getName()); } } }
/** * Names to ji ds. * * @param devices * the devices * @return the string[] */ public String[] namesToJIDs(String... devices) { String[] jids = new String[devices.length]; for (RosterEntry re : xmpp.getRoster().getEntries()) { for (int i = 0; i < devices.length; i++) { if (re.getName().equals(devices[i])) { jids[i] = re.getUser(); } } } return jids; }
/** * Adds a roster entry to the packet. * * @param rosterEntry a roster entry to add. */ public void addRosterEntry(RosterEntry rosterEntry) { // Obtain a String[] from the roster entry groups name List<String> groupNamesList = new ArrayList<String>(); String[] groupNames; for (RosterGroup group : rosterEntry.getGroups()) { groupNamesList.add(group.getName()); } groupNames = groupNamesList.toArray(new String[groupNamesList.size()]); // Create a new Entry based on the rosterEntry and add it to the packet RemoteRosterEntry remoteRosterEntry = new RemoteRosterEntry(rosterEntry.getUser(), rosterEntry.getName(), groupNames); addRosterEntry(remoteRosterEntry); }
/** * ������з�����ϵ�� * * @return */ public static List<MRosterGroup> getGroups(Roster roster) { if (contacters == null) throw new RuntimeException("contacters is null"); List<MRosterGroup> groups = new ArrayList<ContacterManager.MRosterGroup>(); groups.add(new MRosterGroup(Constant.ALL_FRIEND, getContacterList())); for (RosterGroup group : roster.getGroups()) { List<User> groupUsers = new ArrayList<User>(); for (RosterEntry entry : group.getEntries()) { groupUsers.add(contacters.get(entry.getUser())); } groups.add(new MRosterGroup(group.getName(), groupUsers)); } groups.add(new MRosterGroup(Constant.NO_GROUP_FRIEND, getNoGroupUserList(roster))); return groups; }
/** * ����RosterEntry����һ��User * * @param entry * @return */ public static User transEntryToUser(RosterEntry entry, Roster roster) { User user = new User(); if (entry.getName() == null) { user.setName(StringUtil.getUserNameByJid(entry.getUser())); } else { user.setName(entry.getName()); } user.setJID(entry.getUser()); System.out.println(entry.getUser()); Presence presence = roster.getPresence(entry.getUser()); user.setFrom(presence.getFrom()); user.setStatus(presence.getStatus()); user.setSize(entry.getGroups().size()); user.setAvailable(presence.isAvailable()); user.setType(entry.getType()); return user; }
/** * ��һ�����Ѵ�����ɾ�� * * @param user * @param groupName */ public static void removeUserFromGroup(final User user, final String groupName, final XMPPConnection connection) { if (groupName == null || user == null) return; new Thread() { public void run() { RosterGroup group = connection.getRoster().getGroup(groupName); if (group != null) { try { System.out.println(user.getJID() + "----------------"); RosterEntry entry = connection.getRoster().getEntry( user.getJID()); if (entry != null) group.removeEntry(entry); } catch (XMPPException e) { e.printStackTrace(); } } } }.start(); }
/** * �����û�jid�õ��û� * * @param userJid * @param nickname */ public static User getByUserJid(String userJId, XMPPConnection connection) { Roster roster = connection.getRoster(); RosterEntry entry = connection.getRoster().getEntry(userJId); if (null == entry) { return null; } User user = new User(); if (entry.getName() == null) { user.setName(StringUtil.getUserNameByJid(entry.getUser())); } else { user.setName(entry.getName()); } user.setJID(entry.getUser()); System.out.println(entry.getUser()); Presence presence = roster.getPresence(entry.getUser()); user.setFrom(presence.getFrom()); user.setStatus(presence.getStatus()); user.setSize(entry.getGroups().size()); user.setAvailable(presence.isAvailable()); user.setType(entry.getType()); return user; }
@Override public void presenceChanged(Presence presence) { Intent intent = new Intent(); intent.setAction(Constant.ROSTER_PRESENCE_CHANGED); String subscriber = presence.getFrom().substring(0, presence.getFrom().indexOf("/")); RosterEntry entry = roster.getEntry(subscriber); if (ContacterManager.contacters.containsKey(subscriber)) { // ��״̬�ı�֮ǰ��user�㲥��ȥ intent.putExtra(User.userKey, ContacterManager.contacters.get(subscriber)); ContacterManager.contacters.remove(subscriber); ContacterManager.contacters.put(subscriber, ContacterManager.transEntryToUser(entry, roster)); } sendBroadcast(intent); }
@Override public void entriesUpdated(Collection<String> addresses) { for (String address : addresses) { Intent intent = new Intent(); intent.setAction(Constant.ROSTER_UPDATED); // ���״̬�ı��entry RosterEntry userEntry = roster.getEntry(address); User user = ContacterManager .transEntryToUser(userEntry, roster); if (ContacterManager.contacters.get(address) != null) { // ��������Ǹ���ǰ��user intent.putExtra(User.userKey, ContacterManager.contacters.get(address)); // �������ı���û����µ�userManager ContacterManager.contacters.remove(address); ContacterManager.contacters.put(address, user); } sendBroadcast(intent); // �û����£�getEntries����� // roster.getUnfiledEntries�е�entry������� } }
public void invite(View v){ final List<RosterEntry> friends = XmppConnectionManager.getInstance().getAllEntrys(con.getRoster()); String[] names = new String[friends.size()]; for(int i=0;i<friends.size();i++){ RosterEntry entry = friends.get(i); names[i] = entry.getName(); } AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("ѡ�����"); builder.setItems(names, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int pos) { // TODO Auto-generated method stub dialog.cancel(); // if(XmppTool.getInstance().isUserSupportMUC(friends.get(pos).getUser())) muc.invite(friends.get(pos).getUser(), "�����ҵĻ����Ұ�"); // else // Toast.makeText(MUCRoom.this, "�ú��������˲����������", Toast.LENGTH_SHORT).show(); } }); builder.create().show(); }
public void displayfrndslist() { Roster rster = conn.getRoster(); Collection<RosterEntry> records = rster.getEntries(); System.out.println("\nTotal friends: "+records.size()); int i = 1; String status = null; for(RosterEntry r:records) { Presence presence = rster.getPresence(r.getUser()); if ((presence != null)){ if(rster.getPresence(r.getUser()).toString().contains("away")) status = "idle"; else if (rster.getPresence(r.getUser()).toString().contains("unavailable")) status = "offline"; else if (rster.getPresence(r.getUser()).toString().contains("dnd")) status = "busy"; else status = "available"; System.out.println("(#" + i + ")"+r.getUser()+"---"+status); i++; } } }
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); }
private void tryToMoveRosterEntryToGroup(String userName, String groupName) throws XXException { mRoster = mXMPPConnection.getRoster(); RosterGroup rosterGroup = getRosterGroup(groupName); RosterEntry rosterEntry = mRoster.getEntry(userName); removeRosterEntryFromGroups(rosterEntry); if (groupName.length() == 0) return; else { try { rosterGroup.addEntry(rosterEntry); } catch (XMPPException e) { throw new XXException(e.getLocalizedMessage()); } } }
/** * 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(); }
/** * 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(); }
@Override public void presenceChanged(Presence presence) { Intent intent = new Intent(); intent.setAction(IMConstant.ACTION_ROSTER_PRESENCE_CHANGED); String subscriber = presence.getFrom().substring(0, presence.getFrom().indexOf("/")); RosterEntry entry = roster.getEntry(subscriber); if (IMUtil.contacters.containsKey(subscriber)) { // 将状态改变之前的user广播出去 intent.putExtra(IMUser.USERKEY, IMUtil.contacters.get(subscriber)); IMUtil.contacters.remove(subscriber); IMUtil.contacters.put(subscriber, IMUtil.transEntryToUser(entry, roster)); } sendBroadcast(intent); }
@Override public void entriesUpdated(Collection<String> addresses) { for (String address : addresses) { Intent intent = new Intent(); intent.setAction(IMConstant.ACTION_ROSTER_UPDATED); // 获得状态改变的entry RosterEntry userEntry = roster.getEntry(address); IMUser user = IMUtil .transEntryToUser(userEntry, roster); if (IMUtil.contacters.get(address) != null) { // 这里发布的是更新前的user intent.putExtra(IMUser.USERKEY, IMUtil.contacters.get(address)); // 将发生改变的用户更新到userManager IMUtil.contacters.remove(address); IMUtil.contacters.put(address, user); } sendBroadcast(intent); // 用户更新,getEntries会更新 // roster.getUnfiledEntries中的entry不会更新 } }
/** * 获得花名册包含的分组列表 */ public static List<IMRosterGroup> getRosterGroups(Roster roster) { List<IMRosterGroup> groups = new ArrayList<IMRosterGroup>(); groups.add(new IMRosterGroup(IMConstant.ALL_FRIEND, getContacterList())); for (RosterGroup group : roster.getGroups()) { List<IMUser> users = new ArrayList<IMUser>(); for (RosterEntry entry : group.getEntries()) { users.add(contacters.get(entry.getUser())); } groups.add(new IMRosterGroup(group.getName(), users)); } groups.add(new IMRosterGroup(IMConstant.NO_GROUP_FRIEND, getNoGroupUserList(roster))); return groups; }
/** * 获得所有的联系人列表 */ public static List<IMUser> getContacterList() { if (contacters == null){ contacters = new HashMap<String, IMUser>(); for (RosterEntry entry : getRoster().getEntries()) { contacters.put(entry.getUser(), transEntryToUser(entry,getRoster())); } } List<IMUser> userList = new ArrayList<IMUser>(); for (String key : contacters.keySet()) userList.add(contacters.get(key)); return userList; }
/** * Adds a roster entry to the packet. * * @param rosterEntry * a roster entry to add. */ public void addRosterEntry(RosterEntry rosterEntry) { // Obtain a String[] from the roster entry groups name List<String> groupNamesList = new ArrayList<String>(); String[] groupNames; for (RosterGroup group : rosterEntry.getGroups()) { groupNamesList.add(group.getName()); } groupNames = groupNamesList.toArray(new String[groupNamesList.size()]); // Create a new Entry based on the rosterEntry and add it to the packet RemoteRosterEntry remoteRosterEntry = new RemoteRosterEntry( rosterEntry.getUser(), rosterEntry.getName(), groupNames); addRosterEntry(remoteRosterEntry); }
public void entriesAdded(Collection<String> addresses) { for (String addr : addresses) { RosterEntry entry = getSession().conn.getRoster().getEntry(addr); getSession().getBuddyManager().storeBuddy(new XMPPBuddy(getSession().getBuddyManager(), entry.getUser(), entry.getName(), entry.getGroups(), entry)); // Facebook does not support presence probes in their XMPP implementation. See http://developers.facebook.com/docs/chat#features if (!TransportType.facebook.equals(getSession().getTransport().getType())) { //ProbePacket probe = new ProbePacket(getSession().getJID()+"/"+getSession().xmppResource, entry.getUser()); ProbePacket probe = new ProbePacket(null, entry.getUser()); Log.debug("XMPP: Sending the following probe packet: "+probe.toXML()); try { getSession().conn.sendPacket(probe); } catch (IllegalStateException e) { Log.debug("XMPP: Not connected while trying to send probe."); } } } }
public void updateUserList(Activity act) { if (!ThreadHelper.xmppConnection.isAuthenticated()) { if (D) Log.d(TAG, "XMPP connection is not authenticated!"); return; } ArrayList<User> list = new ArrayList<User>(); Roster roster = ThreadHelper.xmppConnection.getRoster(); Iterator<RosterEntry> cit = roster.getEntries().iterator(); while(cit.hasNext()) { RosterEntry entry = cit.next(); VCard vCard = new VCard(); try { vCard.load(ThreadHelper.xmppConnection, entry.getUser()); } catch (XMPPException e) { Log.e(TAG, e.getMessage(), e); } list.add(new User(entry.getUser(), vCard.getNickName(), roster.getPresence(entry.getUser()).isAvailable())); } updateUserList(act, list); }
private void tryToMoveRosterEntryToGroup(String userName, String groupName) throws AppException { mRoster = mXMPPConnection.getRoster(); RosterGroup rosterGroup = getRosterGroup(groupName); RosterEntry rosterEntry = mRoster.getEntry(userName); removeRosterEntryFromGroups(rosterEntry); if (groupName.length() == 0) return; else { try { rosterGroup.addEntry(rosterEntry); } catch (XMPPException e) { throw new AppException(e.getLocalizedMessage()); } } }
public ParcelableRoster(RosterEntry entry, Presence presence) { if(entry != null) { // entry data 파싱 this.userID = entry.getUser(); this.userName = entry.getName(); for(RosterGroup rosterGroup : entry.getGroups()) { this.group = rosterGroup.getName(); } } if(presence != null) { // 접속상태 확인 this.availablity = presence.getType().toString(); if(availablity.equals("available")) parseStatusString( presence.getStatus() ); if(presence.getMode() != null) this.mode = presence.getMode().toString(); } }
List<BuddyGroup> getContactList() { Roster roster = getInternalService().getConnection().getRoster(); Collection<RosterEntry> entries = roster.getEntries(); for (RosterEntry entry : entries) { if (entry.getName() == null) { try { VCard vc = new VCard(); vc.load(getInternalService().getConnection(), entry.getUser()); String nick = getNicknameFromVCard(vc); if (nick != null) { entry.setName(nick); } } catch (XMPPException e) { Logger.log(e); } } } List<BuddyGroup> groups = getInternalService().getService().getEntityAdapter().rosterGroupCollection2BuddyGroupList(roster.getGroups(), entries, getInternalService().getOnlineInfo().getProtocolUid(), getInternalService().getService().getContext(), getInternalService().getService().getServiceId()); return groups; }