use of org.jivesoftware.smackx.iqlast.packet.LastActivity in project Zom-Android by zom.
the class XmppConnection method getLastSeen.
private void getLastSeen(Contact contact) {
if (getState() == ImConnection.LOGGED_IN) {
try {
LastActivity activity = mLastActivityManager.getLastActivity(JidCreate.bareFrom(contact.getAddress().getBareAddress()));
if (activity != null) {
Presence presence = new Presence();
Date now = new Date();
presence.setLastSeen(new Date(now.getTime() - (activity.getIdleTime() * 1000)));
contact.setPresence(presence);
}
} catch (Exception e) {
debug("LastActivity", "error getting last activity for: " + contact.getAddress().getAddress());
}
}
}
use of org.jivesoftware.smackx.iqlast.packet.LastActivity in project xabber-android by redsolution.
the class LastActivityInteractor method onStanza.
@Override
public void onStanza(ConnectionItem connection, Stanza packet) {
if (packet instanceof LastActivity) {
try {
Jid jid = packet.getFrom();
long result = ((LastActivity) packet).lastActivity;
if (result > 0) {
result = System.currentTimeMillis() / 1000 - result;
setLastActivity(connection.getAccount(), UserJid.from(jid), result);
}
} catch (UserJid.UserJidCreateException e) {
e.printStackTrace();
}
}
}
use of org.jivesoftware.smackx.iqlast.packet.LastActivity in project Smack by igniterealtime.
the class LastActivityTest method checkProvider.
@Test
public void checkProvider() throws Exception {
XMLBuilder xml = XMLBuilder.create("iq");
xml.a("from", "romeo@montague.net/orchard").a("id", "last2").a("to", "juliet@capulet.com/balcony").a("type", "get").e("query").namespace(LastActivity.NAMESPACE);
DummyConnection c = new DummyConnection();
c.connect();
IQ lastRequest = PacketParserUtils.parseStanza(xml.asString());
assertTrue(lastRequest instanceof LastActivity);
c.processStanza(lastRequest);
Stanza reply = c.getSentPacket();
assertTrue(reply instanceof LastActivity);
LastActivity l = (LastActivity) reply;
assertEquals("last2", l.getStanzaId());
assertEquals(IQ.Type.result, l.getType());
}
use of org.jivesoftware.smackx.iqlast.packet.LastActivity in project Spark by igniterealtime.
the class ContactList method showPopup.
/**
* Shows popup for right-clicking of ContactItem.
*
* @param e the MouseEvent
* @param item the ContactItem
* @param component the owning component
*/
public void showPopup(Component component, MouseEvent e, final ContactItem item) {
if (item.getJID() == null) {
return;
}
activeItem = item;
final JPopupMenu popup = new JPopupMenu();
// Add Start Chat Menu
popup.add(chatMenu);
// Add Send File Action
Action sendAction = new AbstractAction() {
private static final long serialVersionUID = -7519717310558205566L;
public void actionPerformed(ActionEvent actionEvent) {
SparkManager.getTransferManager().sendFileTo(item);
}
};
// See if we should disable the option to transfer files and images
if (!Default.getBoolean("DISABLE_FILE_XFER") && Enterprise.containsFeature(Enterprise.FILE_TRANSFER_FEATURE)) {
sendAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.DOCUMENT_16x16));
sendAction.putValue(Action.NAME, Res.getString("menuitem.send.a.file"));
if (item.getPresence() != null)
popup.add(sendAction);
}
popup.addSeparator();
String groupName = item.getGroupName();
ContactGroup contactGroup = getContactGroup(groupName);
// Only show "Remove Contact From Group" if the user belongs to more than one group.
if (!contactGroup.isSharedGroup() && !contactGroup.isOfflineGroup() && contactGroup != getUnfiledGroup()) {
Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
RosterEntry entry = roster.getEntry(item.getJID());
if (entry != null) {
int groupCount = entry.getGroups().size();
// which would put them into the unfiled group.
if (groupCount > 1) {
popup.add(removeContactFromGroupMenu);
}
}
}
// Define remove entry action
Action removeAction = new AbstractAction() {
private static final long serialVersionUID = -2565914214685979320L;
public void actionPerformed(ActionEvent e) {
removeContactFromRoster(item);
}
};
removeAction.putValue(Action.NAME, Res.getString("menuitem.remove.from.roster"));
removeAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_CIRCLE_DELETE));
// Check if user is in shared group.
boolean isInSharedGroup = false;
for (ContactGroup cGroup : new ArrayList<>(getContactGroups())) {
if (cGroup.isSharedGroup()) {
ContactItem it = cGroup.getContactItemByJID(item.getJID());
if (it != null) {
isInSharedGroup = true;
}
}
}
// See if we should disable the option to remove a contact
if (!Default.getBoolean("DISABLE_REMOVALS") && Enterprise.containsFeature(Enterprise.REMOVALS_FEATURE)) {
if (!contactGroup.isSharedGroup() && !isInSharedGroup)
popup.add(removeAction);
}
// See if we should disable the option to rename a contact
if (!Default.getBoolean("DISABLE_RENAMES") && Enterprise.containsFeature(Enterprise.RENAMES_FEATURE))
popup.add(renameMenu);
Action viewProfile = new AbstractAction() {
private static final long serialVersionUID = -2562731455090634805L;
public void actionPerformed(ActionEvent e) {
VCardManager vcardSupport = SparkManager.getVCardManager();
String jid = item.getJID();
vcardSupport.viewProfile(jid, SparkManager.getWorkspace());
}
};
viewProfile.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.PROFILE_IMAGE_16x16));
viewProfile.putValue(Action.NAME, Res.getString("menuitem.view.profile"));
popup.add(viewProfile);
popup.addSeparator();
Action lastActivityAction = new AbstractAction() {
private static final long serialVersionUID = -4884230635430933060L;
public void actionPerformed(ActionEvent actionEvent) {
try {
String client = "";
if (item.getPresence().getType() != Presence.Type.unavailable) {
client = item.getPresence().getFrom();
if ((client != null) && (client.lastIndexOf("/") != -1)) {
client = client.substring(client.lastIndexOf("/"));
} else
client = "/";
}
LastActivity activity = LastActivityManager.getInstanceFor(SparkManager.getConnection()).getLastActivity(item.getJID() + client);
long idleTime = (activity.getIdleTime() * 1000);
String time = ModelUtil.getTimeFromLong(idleTime);
UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
JOptionPane.showMessageDialog(getGUI(), Res.getString("message.idle.for", time), Res.getString("title.last.activity"), JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e1) {
UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
JOptionPane.showMessageDialog(getGUI(), Res.getString("message.unable.to.retrieve.last.activity", item.getJID()), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
}
}
};
lastActivityAction.putValue(Action.NAME, Res.getString("menuitem.view.last.activity"));
lastActivityAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_USER1_STOPWATCH));
if (contactGroup == offlineGroup || item.getPresence().isAway() || (item.getPresence().getType() == Presence.Type.unavailable) || (item.getPresence().getType() == null)) {
popup.add(lastActivityAction);
}
Action subscribeAction = new AbstractAction() {
private static final long serialVersionUID = -7754905015338902300L;
public void actionPerformed(ActionEvent e) {
String jid = item.getJID();
Presence response = new Presence(Presence.Type.subscribe);
response.setTo(jid);
try {
SparkManager.getConnection().sendStanza(response);
} catch (SmackException.NotConnectedException e1) {
Log.warning("Unable to send subscribe to " + jid, e1);
}
}
};
subscribeAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_USER1_INFORMATION));
subscribeAction.putValue(Action.NAME, Res.getString("menuitem.subscribe.to"));
Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
RosterEntry entry = roster.getEntry(item.getJID());
if (entry != null && entry.getType() == RosterPacket.ItemType.from) {
popup.add(subscribeAction);
} else if (entry != null && entry.getType() != RosterPacket.ItemType.both && entry.getStatus() == RosterPacket.ItemStatus.SUBSCRIPTION_PENDING) {
popup.add(subscribeAction);
}
// Fire Context Menu Listener
fireContextMenuListenerPopup(popup, item);
ContactGroup group = getContactGroup(item.getGroupName());
if (component == null) {
popup.show(group.getList(), e.getX(), e.getY());
} else {
popup.show(component, e.getX(), e.getY());
popup.requestFocus();
}
}
use of org.jivesoftware.smackx.iqlast.packet.LastActivity in project Spark by igniterealtime.
the class ContactInfoWindow method customizeUI.
public void customizeUI(ContactItem contactItem) {
if (contactItem == null) {
return;
}
nicknameLabel.setText(contactItem.getDisplayName());
String status = contactItem.getStatus();
if (!ModelUtil.hasLength(status)) {
if (contactItem.getPresence() == null || contactItem.getPresence().getType() == Presence.Type.unavailable) {
status = Res.getString("offline");
} else {
status = Res.getString("online");
}
}
if (status.equals(Res.getString("offline")) || contactItem.getPresence().isAway()) {
try {
String client = "";
if (!status.equals(Res.getString("offline"))) {
// If user is away (not offline), last activity request is sent to client
client = contactItem.getPresence().getFrom();
if ((client != null) && (client.lastIndexOf("/") != -1)) {
client = client.substring(client.lastIndexOf("/"));
} else
client = "/";
}
LastActivity activity = LastActivityManager.getInstanceFor(SparkManager.getConnection()).getLastActivity(contactItem.getJID() + client);
long idleTime = (activity.getIdleTime() * 1000);
if (idleTime > 0) {
if (status.equals(Res.getString("offline"))) {
SimpleDateFormat format = new SimpleDateFormat("M/d/yy");
Date l = new Date();
String curDay = format.format(l);
l.setTime(l.getTime() - idleTime);
// If idleTime is within today show the time, otherwise, show the day, date, and time
if (curDay.equals(format.format(l))) {
format = new SimpleDateFormat("h:mm a");
} else {
format = new SimpleDateFormat("EEE M/d/yy h:mm a");
}
status += (" since " + format.format(l));
} else if (contactItem.getPresence().isAway()) {
status += "\n";
String time = ModelUtil.getTimeFromLong(idleTime);
status += Res.getString("message.idle.for", time);
}
}
} catch (Exception e1) {
Log.warning("Unable to get Last Activity from: " + contactItem.toString(), e1);
}
}
statusLabel.setText(status);
Transport transport = TransportUtils.getTransport(XmppStringUtils.parseDomain(contactItem.getJID()));
if (transport != null) {
fullJIDLabel.setIcon(transport.getIcon());
String name = XmppStringUtils.parseLocalpart(contactItem.getJID());
name = XmppStringUtils.unescapeLocalpart(name);
fullJIDLabel.setText(transport.getName() + " - " + name);
} else {
String name = XmppStringUtils.unescapeLocalpart(contactItem.getJID());
fullJIDLabel.setText(name);
fullJIDLabel.setIcon(null);
}
avatarLabel.setBorder(null);
try {
URL avatarURL = contactItem.getAvatarURL();
ImageIcon icon = null;
if (avatarURL != null) {
icon = new ImageIcon(avatarURL);
}
if (icon != null && icon.getIconHeight() > 1) {
icon = GraphicUtils.scaleImageIcon(icon, 96, 96);
avatarLabel.setIcon(icon);
} else {
icon = SparkRes.getImageIcon(SparkRes.DEFAULT_AVATAR_64x64_IMAGE);
avatarLabel.setIcon(icon);
}
avatarLabel.setBorder(BorderFactory.createBevelBorder(0, Color.white, Color.lightGray));
} catch (MalformedURLException e) {
Log.error(e);
}
// Get VCard from memory (if available)
String title = "";
String phone = "";
VCard vcard = SparkManager.getVCardManager().getVCardFromMemory(XmppStringUtils.parseBareJid(contactItem.getJID()));
if (vcard != null) {
title = vcard.getField("TITLE");
phone = vcard.getPhoneWork("VOICE");
if (!ModelUtil.hasLength(title)) {
title = "";
}
if (!ModelUtil.hasLength(phone)) {
phone = "";
}
}
titleLabel.setText(title);
phoneLabel.setText(phone);
}
Aggregations