Guest User

Untitled

a guest
Oct 21st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.78 KB | None | 0 0
  1. package example.com.db.nimbuzz_banbot_android_by_db;
  2.  
  3. import android.app.Service;
  4. import android.content.Intent;
  5. import android.os.AsyncTask;
  6. import android.os.Handler;
  7. import android.os.IBinder;
  8. import android.os.Looper;
  9. import android.util.Log;
  10. import android.widget.Toast;
  11.  
  12. import org.jivesoftware.smack.ConnectionConfiguration;
  13. import org.jivesoftware.smack.ConnectionListener;
  14. import org.jivesoftware.smack.SmackException;
  15. import org.jivesoftware.smack.XMPPConnection;
  16. import org.jivesoftware.smack.XMPPException;
  17. import org.jivesoftware.smack.packet.Presence;
  18. import org.jivesoftware.smack.tcp.XMPPTCPConnection;
  19. import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
  20. import org.jivesoftware.smackx.ping.PingFailedListener;
  21. import org.jivesoftware.smackx.ping.PingManager;
  22.  
  23. import java.io.IOException;
  24.  
  25. public class dbXMPPConn extends Service {
  26.  
  27. private PingManager pingManager;
  28.  
  29. private static final String TAG = "ConnectXmpp";
  30. private static final String DOMAIN = "nimbuzz.com";
  31. private static final String HOST = "o.nimbuzz.com";
  32. private static final int PORT = 5222;
  33.  
  34. private String userName ="";
  35. private String passWord = "";
  36.  
  37. XMPPConnectionListener connectionListener = new XMPPConnectionListener();
  38.  
  39. public static boolean connected = false;
  40. public static boolean isToasted = true;
  41. public static boolean chat_created = true;
  42.  
  43. public dbXMPPConn() {
  44. }
  45.  
  46.  
  47. @Override
  48. public int onStartCommand(Intent intent, int flags, int startId)
  49. {
  50. if(intent != null){
  51. userName = intent.getStringExtra("user");
  52. passWord = intent.getStringExtra("pwd");
  53. dbcon.init(userName, passWord);
  54. dbcon.connectConnection();
  55. }
  56.  
  57. return 0;
  58. }
  59.  
  60. @Override
  61. public void onCreate() {
  62. super.onCreate();
  63.  
  64. }
  65.  
  66. @Override
  67. public void onDestroy() {
  68. dbcon.disconnectConnection();
  69. super.onDestroy();
  70. }
  71.  
  72. @Override
  73. public IBinder onBind(Intent intent) {
  74. // TODO: Return the communication channel to the service.
  75. throw new UnsupportedOperationException("Not yet implemented");
  76. }
  77.  
  78.  
  79. private dbXMPP dbcon = new dbXMPP();
  80.  
  81. public class dbXMPP{
  82.  
  83. public void init(final String userName, final String passWord) {
  84.  
  85. Log.i("dbXMPP", "Initializing!");
  86.  
  87. XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
  88. configBuilder.setUsernameAndPassword(userName, passWord);
  89. configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
  90. configBuilder.setResource("dbanbot-4droid-coded-by-db~");
  91. configBuilder.setServiceName(DOMAIN);
  92. configBuilder.setHost(HOST);
  93. configBuilder.setPort(PORT);
  94.  
  95. XMPPLogic.connection = new XMPPTCPConnection(configBuilder.build());
  96. XMPPLogic.connection.addConnectionListener(connectionListener);
  97. }
  98.  
  99. public void connectConnection() {
  100. AsyncTask<Void, Void, Boolean> connectionThread = new AsyncTask<Void, Void, Boolean>() {
  101.  
  102. @Override
  103. protected Boolean doInBackground(Void... arg0) {
  104.  
  105. // Create a connection
  106. try {
  107. XMPPLogic.connection.connect();
  108. login();
  109. connected = true;
  110.  
  111. } catch (IOException e) {
  112. } catch (SmackException e) {
  113.  
  114. } catch (XMPPException e) {
  115. }
  116.  
  117. return null;
  118. }
  119. };
  120. connectionThread.execute();
  121.  
  122. }
  123.  
  124. public void disconnectConnection() {
  125. new Thread(new Runnable() {
  126. @Override
  127. public void run() {
  128. XMPPLogic.connection.disconnect();
  129. }
  130. }).start();
  131. }
  132. }
  133.  
  134.  
  135.  
  136. private void sendping() throws InterruptedException {
  137. pingManager = PingManager.getInstanceFor(XMPPLogic.connection);
  138. pingManager.setPingInterval(10);
  139.  
  140. pingManager.registerPingFailedListener(new PingFailedListener() {
  141. @Override
  142. public void pingFailed() {
  143. pingManager.setPingInterval(10);
  144. }
  145. });
  146.  
  147. }
  148.  
  149.  
  150. private boolean loggedin = true;
  151. public void login() {
  152.  
  153. try {
  154.  
  155. XMPPLogic.connection.login(userName, passWord);
  156. //connection.login(userName, passWord);
  157.  
  158.  
  159. } catch (XMPPException | SmackException | IOException e) {
  160. e.printStackTrace();
  161. } catch (Exception e) {
  162. loggedin = false;
  163. chat_created = false;
  164. }
  165.  
  166. if (!loggedin)
  167. {
  168. Log.e(TAG, "Unable to login");
  169.  
  170. disconnect();
  171. loggedin = true;
  172. }
  173.  
  174. else {
  175.  
  176.  
  177. Log.e(TAG, "Logged in");
  178. }
  179.  
  180. }
  181.  
  182.  
  183. private void disconnect() {
  184. if(XMPPLogic.connection != null && XMPPLogic.connection.isConnected()) {
  185. new AsyncTask<Void, Void, Void>() {
  186. @Override
  187. protected Void doInBackground(Void... params) {
  188. XMPPLogic.connection.disconnect();
  189. Log.e(TAG, "Connection disconnected");
  190. return null;
  191. }
  192. }.execute();
  193. }
  194. }
  195.  
  196.  
  197. public void setPresence() throws SmackException.NotConnectedException {
  198. Presence dbpres = new Presence(Presence.Type.available);
  199. dbpres.setMode(Presence.Mode.dnd);
  200. dbpres.setStatus("Nimbuzz Login SMACK XMPP Android Coded By DB~@NC");
  201. XMPPLogic.connection.sendStanza(dbpres);
  202.  
  203. }
  204.  
  205.  
  206. // Connection Listener to check connection state
  207. public class XMPPConnectionListener implements ConnectionListener {
  208. @Override
  209. public void connected(final XMPPConnection connection) {
  210.  
  211. Log.d("dbXMPP", "Connected!");
  212. connected = true;
  213.  
  214.  
  215. if (!connection.isAuthenticated()) {
  216. login();
  217. }
  218.  
  219.  
  220. }
  221.  
  222.  
  223. @Override
  224. public void connectionClosed() {
  225. if (isToasted)
  226.  
  227. new Handler(Looper.getMainLooper()).post(new Runnable() {
  228.  
  229. @Override
  230. public void run() {
  231. // TODO Auto-generated method stub
  232. Toast.makeText(getApplicationContext(), "Connection Closed!" , Toast.LENGTH_SHORT).show();
  233.  
  234. }
  235. });
  236. Log.d("dbXMPP", "Connection Closed!");
  237. connected = false;
  238. chat_created = false;
  239. loggedin = false;
  240. }
  241.  
  242. @Override
  243. public void connectionClosedOnError(Exception arg0) {
  244. if (isToasted)
  245.  
  246. new Handler(Looper.getMainLooper()).post(new Runnable() {
  247.  
  248. @Override
  249. public void run() {
  250.  
  251. Toast.makeText(getApplicationContext(), "Connection Closed On Error", Toast.LENGTH_SHORT).show();
  252. }
  253. });
  254. Log.d("dbXMPP", "ConnectionClosedOn Error!");
  255. connected = false;
  256.  
  257. chat_created = false;
  258. loggedin = false;
  259. }
  260.  
  261. @Override
  262. public void reconnectingIn(int arg0) {
  263.  
  264. Log.d("dbXMPP", "Reconnecting " + arg0);
  265.  
  266. loggedin = false;
  267. }
  268.  
  269. @Override
  270. public void reconnectionFailed(Exception arg0) {
  271. if (isToasted)
  272.  
  273. new Handler(Looper.getMainLooper()).post(new Runnable() {
  274.  
  275. @Override
  276. public void run() {
  277. Toast.makeText(getApplicationContext(), "Re-Connection Failed", Toast.LENGTH_SHORT).show();
  278.  
  279. }
  280. });
  281. Log.d("dbXMPP", "ReconnectionFailed!");
  282. connected = false;
  283.  
  284. chat_created = false;
  285. loggedin = false;
  286. }
  287.  
  288. @Override
  289. public void reconnectionSuccessful() {
  290. if (isToasted)
  291.  
  292. new Handler(Looper.getMainLooper()).post(new Runnable() {
  293.  
  294. @Override
  295. public void run() {
  296. // TODO Auto-generated method stub
  297.  
  298. Toast.makeText(getApplicationContext(), "Re-Connected!", Toast.LENGTH_SHORT).show();
  299.  
  300. }
  301. });
  302. Log.d("dbXMPP", "ReconnectionSuccessful");
  303. connected = true;
  304.  
  305. chat_created = false;
  306. loggedin = false;
  307. }
  308.  
  309. @Override
  310. public void authenticated(XMPPConnection arg0, boolean arg1) {
  311. Log.d("dbXMPP", "Authenticated!");
  312. loggedin = true;
  313.  
  314. //xmppClient.dbhere();
  315. chat_created = true;
  316.  
  317. if (isToasted)
  318.  
  319. new Handler(Looper.getMainLooper()).post(new Runnable() {
  320.  
  321. @Override
  322. public void run() {
  323. // TODO Auto-generated method stub
  324. Toast.makeText(getApplicationContext(), "Logged In Successfully", Toast.LENGTH_SHORT).show();
  325.  
  326. try {
  327. setPresence();
  328. } catch (SmackException.NotConnectedException e) {
  329. e.printStackTrace();
  330. }
  331. // conference();
  332.  
  333. Intent dbint = new Intent(dbXMPPConn.this, BotMainTabActivity.class);
  334. dbint.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  335. startActivity(dbint);
  336. try {
  337. sendping();
  338. } catch (InterruptedException e) {
  339. e.printStackTrace();
  340. }
  341.  
  342.  
  343. }
  344.  
  345.  
  346.  
  347. });
  348.  
  349.  
  350. }
  351. }
  352.  
  353.  
  354. }
Add Comment
Please, Sign In to add comment