Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.test.utils;
- import java.io.File;
- import java.util.Iterator;
- import org.jivesoftware.smack.ConnectionConfiguration;
- import org.jivesoftware.smack.Roster;
- import org.jivesoftware.smack.SmackAndroid;
- import org.jivesoftware.smack.SmackConfiguration;
- import org.jivesoftware.smack.XMPPConnection;
- import org.jivesoftware.smack.packet.Presence;
- import org.jivesoftware.smack.packet.Presence.Mode;
- import org.jivesoftware.smack.provider.ProviderManager;
- import org.jivesoftware.smackx.Form;
- import org.jivesoftware.smackx.FormField;
- import org.jivesoftware.smackx.GroupChatInvitation;
- import org.jivesoftware.smackx.muc.MultiUserChat;
- import org.jivesoftware.smackx.provider.DataFormProvider;
- import org.jivesoftware.smackx.provider.DiscoverInfoProvider;
- import org.jivesoftware.smackx.provider.VCardProvider;
- import android.content.Context;
- import android.os.Build;
- public class Test {
- private static final String HOST = "test.com";
- private static final int PORT = 5222;
- private static final String SERVICE = "test.com";
- private SmackAndroid smackAndroid;
- private void login(Context context)
- {
- try {
- ConnectionConfiguration connConfig = new ConnectionConfiguration(
- HOST, PORT, SERVICE);
- if (Build.VERSION.SDK_INT >= 14) {
- connConfig.setTruststoreType("AndroidCAStore");
- connConfig.setTruststorePassword(null);
- connConfig.setTruststorePath(null);
- } else {
- connConfig.setTruststoreType("BKS");
- String path = System.getProperty("javax.net.ssl.trustStore");
- if (path == null)
- path = System.getProperty("java.home") + File.separator + "etc"
- + File.separator + "security" + File.separator
- + "cacerts.bks";
- connConfig.setTruststorePath(path);
- }
- SmackConfiguration.setPacketReplyTimeout(10000);
- XMPPConnection connection = new XMPPConnection(connConfig);
- connection.connect();
- String USERNAME = "test123" + "@" + HOST;
- connection.login(USERNAME, "123456");
- Presence presence = new Presence(Presence.Type.available);
- connection.sendPacket(presence);
- ProviderManager providerManager = ProviderManager.getInstance();
- providerManager.addExtensionProvider("x",
- "jabber:x:conference",
- new GroupChatInvitation.Provider());
- providerManager.addIQProvider("vCard", "vcard-temp",
- new VCardProvider());
- providerManager.addIQProvider("query","http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());
- providerManager.addExtensionProvider("x","jabber:x:data", new DataFormProvider());
- if (smackAndroid == null) {
- smackAndroid = SmackAndroid.init(context);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void join(XMPPConnection connection)
- {
- try
- {
- String roomId = "myroom" + "@" + "conference." + HOST ;
- MultiUserChat muc = new MultiUserChat(connection, roomId);
- boolean roomCreated = false;
- try {
- muc.create(roomId);
- roomCreated = true;
- } catch (Exception e) {
- }
- muc.join(roomId);
- if(roomCreated)
- {
- setConfig(muc, roomId);
- }
- }
- catch(Exception e)
- {
- }
- }
- private void setConfig(MultiUserChat multiUserChat, String roomId) {
- try {
- Form form = multiUserChat.getConfigurationForm();
- Form submitForm = form.createAnswerForm();
- for (Iterator<FormField> fields = form.getFields(); fields
- .hasNext();) {
- FormField field = (FormField) fields.next();
- if (!FormField.TYPE_HIDDEN.equals(field.getType())
- && field.getVariable() != null) {
- // Sets the default value as the answer
- submitForm.setDefaultAnswer(field.getVariable());
- }
- }
- submitForm.setAnswer("muc#roomconfig_roomname", roomId);
- submitForm.setAnswer("muc#roomconfig_persistentroom", true);
- submitForm.setAnswer("muc#roomconfig_publicroom", true);
- multiUserChat.sendConfigurationForm(submitForm);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void getPresence(String user,XMPPConnection xmppConnection)
- {
- try
- {
- Roster roster = xmppConnection.getRoster();
- Presence availability = roster.getPresence(user);
- Mode userMode = availability.getMode();
- retrieveState_mode(availability.getMode(),availability.isAvailable());
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static int retrieveState_mode(Mode userMode, boolean isOnline) {
- int userState = 0;
- /** 0 for offline, 1 for online, 2 for away,3 for busy*/
- if(userMode == Mode.dnd) {
- userState = 3;
- } else if (userMode == Mode.away || userMode == Mode.xa) {
- userState = 2;
- } else if (isOnline) {
- userState = 1;
- }
- return userState;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement