Skip to content

Instantly share code, notes, and snippets.

@arthtilva
Created July 16, 2016 06:15
Show Gist options
  • Save arthtilva/e961740d53cd5b6d8aab883f0d0a5414 to your computer and use it in GitHub Desktop.
Save arthtilva/e961740d53cd5b6d8aab883f0d0a5414 to your computer and use it in GitHub Desktop.
public void createNewAccount() {
isLoginComplete = false;
if (checkInternetAvailable() == false) {
isConnected = false;
return;
}
new AsyncTask<Void, Void, Boolean>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Void... params) {
try {
XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration
.builder()
.setServiceName(Constants.IP)
.setHost(Constants.IP)
.setPort(Constants.PORT)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.build();
connection = new XMPPTCPConnection(conf);
// connection.setPacketReplyTimeout(10000);
connection.connect();
// Every 10*60s = 10min.
ExceptionHandler.appendLog("My Application connect successful", "cosmos");
StoreUserData storeUserData = new StoreUserData(getApplicationContext());
storeUserData.getBoolean(Constants.IS_ACCOUNT_CREATED);
Log.d("JJ", storeUserData.getBoolean(Constants.IS_ACCOUNT_CREATED) + " ");
if (!storeUserData.getBoolean(Constants.IS_ACCOUNT_CREATED)) {
try {
AccountManager accountManager = AccountManager.getInstance(connection);
accountManager.sensitiveOperationOverInsecureConnection(true);
HashMap<String, String> attributes = new HashMap<>();
attributes.put("username", storeUserData.getString(Constants.USER_IMEI));
attributes.put("password", storeUserData.getString(Constants.USER_IMEI));
attributes.put("email", storeUserData.getString(Constants.USER_EMAIL));
attributes.put("phone", storeUserData.getString(Constants.USER_MOBILE_NUMBER));
attributes.put("misc", storeUserData.getString(Constants.USER_COMPANY));
attributes.put("company", storeUserData.getString(Constants.USER_COMPANY));
attributes.put("last", storeUserData.getString(Constants.USER_LAST_NAME));
attributes.put("first", storeUserData.getString(Constants.USER_FIRST_NAME));
accountManager.createAccount(storeUserData.getString(Constants.USER_IMEI), storeUserData.getString(Constants.USER_IMEI), attributes);
storeUserData.setBoolean(Constants.IS_ACCOUNT_CREATED, true);
ExceptionHandler.appendLog("My Application account created successful", "cosmos");
isFail = false;
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
ExceptionHandler.appendLog(e);
isFail = true;
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
storeUserData.setBoolean(Constants.IS_ACCOUNT_CREATED, true);
isFail = true;
ExceptionHandler.appendLog(e);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
ExceptionHandler.appendLog(e);
isFail = true;
} catch (Exception e) {
e.printStackTrace();
ExceptionHandler.appendLog(e);
isFail = true;
}
}
SASLMechanism mechanism = new SASLDigestMD5Mechanism();
SASLAuthentication.registerSASLMechanism(mechanism);
//SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
//SASLAuthentication.blacklistSASLMechanism("9798-M-DSA-SHA1");
SASLAuthentication.blacklistSASLMechanism("PLAIN");
//SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
//SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
ExceptionHandler.appendLog("My Application before login " + storeUserData.getString(Constants.USER_IMEI), "cosmos");
connection.login(storeUserData.getString(Constants.USER_IMEI), storeUserData.getString(Constants.USER_IMEI));
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
PingManager.setDefaultPingInterval(1);
PingManager pingManager = PingManager.getInstanceFor(connection);
pingManager.pingMyServer();
pingManager.registerPingFailedListener(new PingFailedListener() {
@Override
public void pingFailed() {
Log.d("appendLog", "pingFailed");
}
});
ReconnectionManager reconnectionManager = ReconnectionManager.getInstanceFor(connection);
reconnectionManager.enableAutomaticReconnection();
reconnectionManager.setFixedDelay(1500); // 15 * 1000
// Roster.getInstanceFor(connection).addRosterListener(new RosterListener() {
// @Override
// public void entriesAdded(Collection<String> addresses) {
// Log.d("Roster", "added");
// }
//
// @Override
// public void entriesUpdated(Collection<String> addresses) {
// Log.d("Roster", "updated");
// }
//
// @Override
// public void entriesDeleted(Collection<String> addresses) {
// Log.d("Roster", "deleted");
// }
//
// @Override
// public void presenceChanged(Presence presence) {
//
// }
// });
connection.addConnectionListener(new ConnectionListener() {
@Override
public void connected(XMPPConnection connection) {
ArrayList<ChatMessageHelper> chatMessageHelpers = new TableHelper().getUnSentMessages(getApplicationContext());
for (int i = chatMessageHelpers.size() - 1; i > -1; i--) {
ChatMessageHelper chatMessageHelper = chatMessageHelpers.get(i);
Log.i("UNSENT_MESSAGE_TO", chatMessageHelper.to);
Log.i("UNSENT_MESSAGE_FROM", chatMessageHelper.from);
Log.i("UNSENT_MESSAGE_MESS", chatMessageHelper.msg);
sendChatMessage(chatMessageHelper, true);
// chatMessageHelper.msgStatus = TableHelper.MsgStatus.SENT;
// new TableHelper().updateChat(getApplicationContext(), chatMessageHelper);
}
}
@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
}
@Override
public void connectionClosed() {
isConnected = false;
}
@Override
public void connectionClosedOnError(Exception e) {
isConnected = false;
ExceptionHandler.appendLog(e);
ExceptionHandler.appendLog("My Application connection error" + e, "cosmos");
}
@Override
public void reconnectionSuccessful() {
}
@Override
public void reconnectingIn(int seconds) {
}
@Override
public void reconnectionFailed(Exception e) {
isConnected = false;
ExceptionHandler.appendLog(e);
ExceptionHandler.appendLog("My Application connection error" + e, "cosmos");
}
});
ExceptionHandler.appendLog("My Application complete login ", "cosmos");
isFail = false;
isLoginComplete = true;
return true;
} catch (Exception ex) {
//Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
ex.printStackTrace();
Log.e("XMPPClient", ex.toString());
ExceptionHandler.appendLog(ex);
isFail = true;
}
return false;
}
@Override
protected void onPostExecute(Boolean aVoid) {
super.onPostExecute(aVoid);
Log.d("CREATE ACC", aVoid.booleanValue() + "");
if (isFail) {
if (loginListener != null)
loginListener.onError();
return;
}
if (!connection.isAuthenticated() && isOnline(getApplicationContext())) {
if (loginListener != null)
loginListener.onError();
return;
}
if (!aVoid.booleanValue()) {
if (loginListener != null)
loginListener.onError();
} else {
isLoginComplete = true;
if (loginListener != null)
loginListener.onSuccess();
setXMPPConnection(connection);
join_groups();
try {
//handleOfflineMessages(connection, getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
new StoreUserData(getApplicationContext()).setBoolean(Constants.IS_APP_OPEN, true);
sendPendingMsg();
}
}
}.execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment