Java Code Examples for org.jivesoftware.smack.packet.Presence#setStatus()

The following examples show how to use org.jivesoftware.smack.packet.Presence#setStatus() . 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: SendPresence.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 {
    Presence.Type typeVal = Presence.Type.valueOf(sampler.getPropertyAsString(TYPE, Presence.Type.available.toString()));
    Presence.Mode modeVal = Presence.Mode.valueOf(sampler.getPropertyAsString(MODE, Presence.Mode.available.toString()));

    Presence presence = new Presence(typeVal);
    presence.setMode(modeVal);

    String to = sampler.getPropertyAsString(RECIPIENT);
    if (!to.isEmpty()) {
        presence.setTo(to);
    }

    String text = sampler.getPropertyAsString(STATUS_TEXT);
    if (!text.isEmpty()) {
        presence.setStatus(text);
    }

    sampler.getXMPPConnection().sendPacket(presence);
    res.setSamplerData(presence.toXML().toString());
    return res;
}
 
Example 2
Source File: XMPP.java    From XMPPSample_Studio with Apache License 2.0 5 votes vote down vote up
public void login(String user, String pass, StatusItem status, String username)
            throws XMPPException, SmackException, IOException, InterruptedException {
        Log.i(TAG, "inside XMPP getlogin Method");
        long l = System.currentTimeMillis();
        XMPPTCPConnection connect = connect();
        if (connect.isAuthenticated()) {
            Log.i(TAG, "User already logged in");
            return;
        }

        Log.i(TAG, "Time taken to connect: " + (System.currentTimeMillis() - l));

        l = System.currentTimeMillis();
        connect.login(user, pass);
        Log.i(TAG, "Time taken to login: " + (System.currentTimeMillis() - l));

        Log.i(TAG, "login step passed");

        Presence p = new Presence(Presence.Type.available);
        p.setMode(Presence.Mode.available);
        p.setPriority(24);
        p.setFrom(connect.getUser());
        if (status != null) {
            p.setStatus(status.toJSON());
        } else {
            p.setStatus(new StatusItem().toJSON());
        }
//        p.setTo("");
        VCard ownVCard = new VCard();
        ownVCard.load(connect);
        ownVCard.setNickName(username);
        ownVCard.save(connect);

        PingManager pingManager = PingManager.getInstanceFor(connect);
        pingManager.setPingInterval(150000);
        connect.sendPacket(p);


    }
 
Example 3
Source File: Client.java    From desktopclient-java with GNU General Public License v3.0 5 votes vote down vote up
public void sendUserPresence(String statusText) {
    Presence presence = new Presence(Presence.Type.available);
    if (!statusText.isEmpty())
        presence.setStatus(statusText);

    // note: not setting priority, according to anti-dicrimination rules;)

    // for testing
    //presence.addExtension(new PresenceSignature(""));

    this.sendPacket(presence);
}
 
Example 4
Source File: XmppConnection.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 修改心情
 * 
 * @param connection
 * @param status
 */
public void changeStateMessage(String status) {
	if (getConnection() == null)
		return;
	Presence presence = new Presence(Presence.Type.available);
	presence.setStatus(status);
	getConnection().sendPacket(presence);
}
 
Example 5
Source File: PresenceTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * User1 logs in, then sets offline presence information (presence with status text). User2
 * logs in and checks to see if offline presence is returned.
 *
 * @throws Exception if an exception occurs.
 */
public void testOfflineStatusPresence() throws Exception {
    // Add a new roster entry for other user.
    Roster roster = getConnection(0).getRoster();
    roster.createEntry(getBareJID(1), "gato1", null);

    // Wait up to 2 seconds
    long initial = System.currentTimeMillis();
    while (System.currentTimeMillis() - initial < 2000 && (
            roster.getPresence(getBareJID(1)).getType().equals(Presence.Type.unavailable))) {
        Thread.sleep(100);
    }

    // Sign out of conn1 with status
    Presence offlinePresence = new Presence(Presence.Type.unavailable);
    offlinePresence.setStatus("Offline test");
    getConnection(1).disconnect(offlinePresence);

    // Wait 500 ms
    Thread.sleep(500);
    Presence presence = getConnection(0).getRoster().getPresence(getBareJID(1));
    assertEquals("Offline presence status not received.", "Offline test", presence.getStatus());

    // Sign out of conn0.
    getConnection(0).disconnect();

    // See if conneciton 0 can get offline status.
    XMPPTCPConnection con0 = getConnection(0);
    con0.connect();
    con0.login(getUsername(0), getUsername(0));

    // Wait 500 ms
    Thread.sleep(500);
    presence = con0.getRoster().getPresence(getBareJID(1));
    assertTrue("Offline presence status not received after logout.",
            "Offline test".equals(presence.getStatus()));
}
 
Example 6
Source File: ApplePlugin.java    From Spark with Apache License 2.0 5 votes vote down vote up
private void sparkIsIdle() {
LocalPreferences localPref = SettingsManager.getLocalPreferences();
if (!localPref.isIdleOn()) {
    return;
}

try {
    // Handle if spark is not connected to the server.
    if (SparkManager.getConnection() == null || !SparkManager.getConnection().isConnected()) {
	return;
    }

    // Change Status
    Workspace workspace = SparkManager.getWorkspace();
    if (workspace != null) {
	Presence presence = workspace.getStatusBar().getPresence();
	long diff = System.currentTimeMillis() - lastActive;
	boolean idle = diff > 60000 * 60;
	if (presence.getMode() == Presence.Mode.available && idle) {
	    unavailable = true;
	    StatusItem away = workspace.getStatusBar().getStatusItem("Away");
	    Presence p = away.getPresence();
	    p.setStatus(Res.getString("message.away.idle"));

	    previousPriority = presence.getPriority();

	    p.setPriority(0);

	    SparkManager.getSessionManager().changePresence(p);
	}
    }
} catch (Exception e) {
    Log.error("Error with IDLE status.", e);
}
   }
 
Example 7
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * change jabber status. Example: sendPresencePacket(Presence.Type.AVAILABLE, "at meeting...", 1, Presence.Mode.AWAY);
 * 
 * @param type
 * @param status
 * @param priority
 * @param mode
 */
public void sendPresence(final Presence.Type type, String status, final int priority, final Presence.Mode mode) {
    // get rid of "&" because they break xml packages!
    if (status != null) {
        status = status.replaceAll("&", "&amp;");
    }
    if (connection == null || !connection.isConnected()) {
        return;
    }
    if (collaborationDisabled) {
        return;
    }

    setStatus(mode);
    final Presence presence = new Presence(type);
    if (status == null) {
        if (mode == Presence.Mode.available) {
            status = InstantMessagingConstants.PRESENCE_MODE_AVAILABLE;
        } else if (mode == Presence.Mode.away) {
            status = InstantMessagingConstants.PRESENCE_MODE_AWAY;
        } else if (mode == Presence.Mode.chat) {
            status = InstantMessagingConstants.PRESENCE_MODE_CHAT;
        } else if (mode == Presence.Mode.dnd) {
            status = InstantMessagingConstants.PRESENCE_MODE_DND;
        } else if (mode == Presence.Mode.xa) {
            status = InstantMessagingConstants.PRESENCE_MODE_XAWAY;
        }
        presence.setStatus(status);
    } else {
        presence.setStatus(status);
    }
    setStatusMsg(presence.getStatus());
    // setting prio when type == unavailable causes error on IM server
    if (presence.getType() == Presence.Type.available) {
        presence.setPriority(priority);
    }
    if (mode != null) {
        presence.setMode(mode);
    }
    try {
        connection.sendPacket(presence);
    } catch (final RuntimeException ex) {
        log.warn("Error while trying to send Instant Messaging packet for user: " + userInfo.getUsername() + " .Errormessage: ", ex);
    }
}
 
Example 8
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * change jabber status. Example: sendPresencePacket(Presence.Type.AVAILABLE, "at meeting...", 1, Presence.Mode.AWAY);
 * 
 * @param type
 * @param status
 * @param priority
 * @param mode
 */
public void sendPresence(final Presence.Type type, String status, final int priority, final Presence.Mode mode) {
    // get rid of "&" because they break xml packages!
    if (status != null) {
        status = status.replaceAll("&", "&amp;");
    }
    if (connection == null || !connection.isConnected()) {
        return;
    }
    if (collaborationDisabled) {
        return;
    }

    setStatus(mode);
    final Presence presence = new Presence(type);
    if (status == null) {
        if (mode == Presence.Mode.available) {
            status = InstantMessagingConstants.PRESENCE_MODE_AVAILABLE;
        } else if (mode == Presence.Mode.away) {
            status = InstantMessagingConstants.PRESENCE_MODE_AWAY;
        } else if (mode == Presence.Mode.chat) {
            status = InstantMessagingConstants.PRESENCE_MODE_CHAT;
        } else if (mode == Presence.Mode.dnd) {
            status = InstantMessagingConstants.PRESENCE_MODE_DND;
        } else if (mode == Presence.Mode.xa) {
            status = InstantMessagingConstants.PRESENCE_MODE_XAWAY;
        }
        presence.setStatus(status);
    } else {
        presence.setStatus(status);
    }
    setStatusMsg(presence.getStatus());
    // setting prio when type == unavailable causes error on IM server
    if (presence.getType() == Presence.Type.available) {
        presence.setPriority(priority);
    }
    if (mode != null) {
        presence.setMode(mode);
    }
    try {
        connection.sendPacket(presence);
    } catch (final RuntimeException ex) {
        log.warn("Error while trying to send Instant Messaging packet for user: " + userInfo.getUsername() + " .Errormessage: ", ex);
    }
}
 
Example 9
Source File: StatusBar.java    From Spark with Apache License 2.0 4 votes vote down vote up
private String changePresence(Presence presence) {
	// SPARK-1521. Other clients can see "Invisible" status while we are disappearing.
	// So we send "Offline" instead of "Invisible" for them.
	boolean isNewPresenceInvisible = PresenceManager
			.isInvisible(presence);
	if (isNewPresenceInvisible && !PrivacyManager.getInstance().isPrivacyActive()) {
		JOptionPane.showMessageDialog(null, Res.getString("dialog.invisible.privacy.lists.not.supported"));
	}

	Presence copyPresence = copyPresence(presence);
	if (isNewPresenceInvisible) {
		copyPresence.setStatus(null);
	}
	if (PresenceManager.areEqual(getCurrentPresence(), copyPresence)) {
		return presence.getStatus();
	}

	// ask user to confirm that all group chat rooms will be closed if
	// he/she goes to invisible.
	if (isNewPresenceInvisible
			&& SparkManager.getChatManager().getChatContainer()
			.hasGroupChatRooms()) {
		int reply = JOptionPane
				.showConfirmDialog(
						null,
						Res.getString("dialog.confirm.close.all.conferences.if.invisible.msg"),
						Res.getString("dialog.confirm.to.reveal.visibility.title"),
						JOptionPane.YES_NO_OPTION);
		if (reply == JOptionPane.NO_OPTION) {
			return getCurrentPresence().getStatus();
		}
	}

	// If we go visible then we should send "Available" first.
	if (!isNewPresenceInvisible
			&& PresenceManager.isInvisible(getCurrentPresence()))
		PrivacyManager.getInstance().goToVisible();

	// Then set the current status.
	SparkManager.getSessionManager().changePresence(copyPresence);

	// If we go invisible we should activate the "globally invisible list"
	// and send "Available" after "Unavailable" presence.
	if (isNewPresenceInvisible) {
		SparkManager.getChatManager().getChatContainer()
		.closeAllGroupChatRooms();
		PrivacyManager.getInstance().goToInvisible();
	}

	return presence.getStatus();
}