Advertisement
Guest User

Untitled

a guest
Nov 11th, 2013
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 KB | None | 0 0
  1. package com.test.utils;
  2.  
  3. import java.io.File;
  4. import java.util.Iterator;
  5.  
  6. import org.jivesoftware.smack.ConnectionConfiguration;
  7. import org.jivesoftware.smack.Roster;
  8. import org.jivesoftware.smack.SmackAndroid;
  9. import org.jivesoftware.smack.SmackConfiguration;
  10. import org.jivesoftware.smack.XMPPConnection;
  11. import org.jivesoftware.smack.packet.Presence;
  12. import org.jivesoftware.smack.packet.Presence.Mode;
  13. import org.jivesoftware.smack.provider.ProviderManager;
  14. import org.jivesoftware.smackx.Form;
  15. import org.jivesoftware.smackx.FormField;
  16. import org.jivesoftware.smackx.GroupChatInvitation;
  17. import org.jivesoftware.smackx.muc.MultiUserChat;
  18. import org.jivesoftware.smackx.provider.DataFormProvider;
  19. import org.jivesoftware.smackx.provider.DiscoverInfoProvider;
  20. import org.jivesoftware.smackx.provider.VCardProvider;
  21.  
  22. import android.content.Context;
  23. import android.os.Build;
  24.  
  25. public class Test {
  26.  
  27.     private static final String HOST = "test.com";
  28.     private static final int PORT = 5222;
  29.     private static final String SERVICE = "test.com";
  30.  
  31.     private SmackAndroid smackAndroid;
  32.    
  33.     private void login(Context context)
  34.     {
  35.        
  36.         try {
  37.             ConnectionConfiguration connConfig = new ConnectionConfiguration(
  38.                     HOST, PORT, SERVICE);
  39.  
  40.             if (Build.VERSION.SDK_INT >= 14) {
  41.                 connConfig.setTruststoreType("AndroidCAStore");
  42.                 connConfig.setTruststorePassword(null);
  43.                 connConfig.setTruststorePath(null);
  44.             } else {
  45.                 connConfig.setTruststoreType("BKS");
  46.                 String path = System.getProperty("javax.net.ssl.trustStore");
  47.                 if (path == null)
  48.                     path = System.getProperty("java.home") + File.separator + "etc"
  49.                         + File.separator + "security" + File.separator
  50.                         + "cacerts.bks";
  51.                 connConfig.setTruststorePath(path);
  52.             }
  53.            
  54.             SmackConfiguration.setPacketReplyTimeout(10000);
  55.            
  56.             XMPPConnection connection = new XMPPConnection(connConfig);
  57.  
  58.             connection.connect();
  59.  
  60.             String USERNAME = "test123" + "@" + HOST;
  61.             connection.login(USERNAME, "123456");
  62.  
  63.             Presence presence = new Presence(Presence.Type.available);
  64.             connection.sendPacket(presence);
  65.  
  66.             ProviderManager providerManager = ProviderManager.getInstance();
  67.  
  68.             providerManager.addExtensionProvider("x",
  69.                     "jabber:x:conference",
  70.                     new GroupChatInvitation.Provider());
  71.             providerManager.addIQProvider("vCard", "vcard-temp",
  72.                     new VCardProvider());
  73.  
  74.             providerManager.addIQProvider("query","http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());
  75.             providerManager.addExtensionProvider("x","jabber:x:data", new DataFormProvider());
  76.            
  77.              if (smackAndroid == null) {
  78.              smackAndroid = SmackAndroid.init(context);
  79.              }
  80.         } catch (Exception e) {
  81.             e.printStackTrace();
  82.         }
  83.     }
  84.    
  85.    
  86.     private void join(XMPPConnection connection)
  87.     {
  88.         try
  89.         {
  90.             String roomId = "myroom" + "@" + "conference." + HOST ;
  91.  
  92.             MultiUserChat muc = new MultiUserChat(connection, roomId);
  93.            
  94.             boolean roomCreated = false;
  95.            
  96.             try {
  97.                 muc.create(roomId);
  98.                 roomCreated = true;
  99.             } catch (Exception e) {
  100.             }
  101.  
  102.                 muc.join(roomId);
  103.  
  104.             if(roomCreated)
  105.             {
  106.                setConfig(muc, roomId);
  107.             }
  108.         }
  109.         catch(Exception e)
  110.         {
  111.            
  112.         }
  113.     }
  114.    
  115.     private void setConfig(MultiUserChat multiUserChat, String roomId) {
  116.        
  117.         try {
  118.  
  119.             Form form = multiUserChat.getConfigurationForm();
  120.            
  121.             Form submitForm = form.createAnswerForm();
  122.  
  123.             for (Iterator<FormField> fields = form.getFields(); fields
  124.                     .hasNext();) {
  125.                 FormField field = (FormField) fields.next();
  126.                 if (!FormField.TYPE_HIDDEN.equals(field.getType())
  127.                         && field.getVariable() != null) {
  128.                     // Sets the default value as the answer
  129.                     submitForm.setDefaultAnswer(field.getVariable());
  130.                 }
  131.             }
  132.  
  133.             submitForm.setAnswer("muc#roomconfig_roomname", roomId);
  134.             submitForm.setAnswer("muc#roomconfig_persistentroom", true);
  135.             submitForm.setAnswer("muc#roomconfig_publicroom", true);
  136.            
  137.             multiUserChat.sendConfigurationForm(submitForm);
  138.  
  139.         } catch (Exception e) {
  140.             e.printStackTrace();
  141.         }
  142.     }
  143.  
  144.     private void getPresence(String user,XMPPConnection xmppConnection)
  145.     {
  146.         try
  147.         {
  148.             Roster roster = xmppConnection.getRoster();
  149.             Presence availability = roster.getPresence(user);
  150.             Mode userMode = availability.getMode();
  151.  
  152.             retrieveState_mode(availability.getMode(),availability.isAvailable());
  153.         } catch (Exception e) {
  154.             // TODO Auto-generated catch block
  155.             e.printStackTrace();
  156.         }
  157.        
  158.     }
  159.    
  160.     public static int retrieveState_mode(Mode userMode, boolean isOnline) {
  161.         int userState = 0;
  162.         /** 0 for offline, 1 for online, 2 for away,3 for busy*/
  163.         if(userMode == Mode.dnd) {
  164.             userState = 3;
  165.         } else if (userMode == Mode.away || userMode == Mode.xa) {  
  166.             userState = 2;
  167.         } else if (isOnline) {
  168.             userState = 1;
  169.         }
  170.         return userState;
  171. }
  172.    
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement