Advertisement
Guest User

Untitled

a guest
Oct 14th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. public class Authenticate extends Application {
  2.  
  3. private static final String DOMAIN = StaticVariables.chatServer;
  4. private static final String HOST = StaticVariables.chatServer;
  5. private static final int PORT = 5222;
  6. static AbstractXMPPConnection connection ;
  7. String username,password;
  8. private boolean connected;
  9.  
  10. @Override
  11. public void onCreate() {
  12. super.onCreate();
  13.  
  14. }
  15.  
  16. public AbstractXMPPConnection initializeXMPPTCPConnection(String username,String password) {
  17. Log.e("APPLICATION", "username: "+username);
  18. Log.e("APPLICATION", "password: "+password);
  19. Log.i("APPLCATION", "initializeXMPPTCPConnection calle:");
  20. this.username=username;
  21. this.password=password;
  22. XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
  23. configBuilder.setUsernameAndPassword(username, password);
  24. configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
  25. configBuilder.setResource("Android");
  26. configBuilder.setServiceName(DOMAIN);
  27. configBuilder.setHost(HOST);
  28. configBuilder.setPort(PORT);
  29. configBuilder.setDebuggerEnabled(true);
  30. connection = new XMPPTCPConnection(configBuilder.build());
  31. connection=connectConnection();
  32. Log.e("APPLICATION", "initializeXMPPTCPConnection: "+connection.isConnected());
  33. return connection;
  34. }
  35.  
  36. public AbstractXMPPConnection getConnection(){
  37. return connection;
  38. }
  39.  
  40. public AbstractXMPPConnection connectConnection()
  41. {
  42. AsyncTask<Void, Void, AbstractXMPPConnection> connectionThread = new AsyncTask<Void, Void, AbstractXMPPConnection>() {
  43.  
  44. @Override
  45. protected AbstractXMPPConnection doInBackground(Void... arg0) {
  46.  
  47. // Create a connection
  48. try {
  49. connection.connect().login();
  50. Log.e("Application", "doInBackground: "+connection.isConnected());
  51. //login();
  52. connected = true;
  53. //sendMsg();
  54.  
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. } catch (SmackException e) {
  58. e.printStackTrace();
  59. } catch (XMPPException e) {
  60. e.printStackTrace();
  61. }
  62.  
  63. return connection;
  64. }
  65.  
  66. @Override
  67. protected void onPostExecute(AbstractXMPPConnection connection2) {
  68. super.onPostExecute(connection2);
  69. // sendMsg(message.getText().toString());
  70. Log.e("APPLICATION", "onPostExecute: "+connection2.isConnected());
  71. connection=connection2;
  72. }
  73. };
  74. try {
  75. connection=connectionThread.execute().get();
  76. } catch (InterruptedException e) {
  77. e.printStackTrace();
  78. } catch (ExecutionException e) {
  79. e.printStackTrace();
  80. }
  81. Log.e("Application", "connectConnection: "+connection.isConnected());
  82. return connection;
  83. }
  84.  
  85. public void login() {
  86.  
  87. try {
  88. connection.login(username, password);
  89. Log.e("APPLICATIPN", "Yey! We're connected to the Xmpp server!");
  90.  
  91. } catch (XMPPException | SmackException | IOException e) {
  92. e.printStackTrace();
  93. } catch (Exception e) {
  94. }
  95.  
  96. }
  97.  
  98.  
  99.  
  100. }
  101.  
  102. connection=((Authenticate)getApplication()).getConnection();
  103.  
  104. public void listenChat(String name){
  105. ChatManager manager = ChatManager.getInstanceFor(connection);
  106. manager.addChatListener(new ChatManagerListener() {
  107.  
  108. @Override
  109. public void chatCreated(final Chat chat, boolean createdLocally) {
  110. System.out.println("Created chat");
  111. chat.addMessageListener(new ChatMessageListener() {
  112.  
  113. @Override
  114. public void processMessage(final Chat chat, final org.jivesoftware.smack.packet.Message message) {
  115. //This is called twice
  116.  
  117. }
  118. }
  119. });
  120.  
  121. }
  122. });
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement