use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.
the class AccountManager method addAccount.
/**
* Creates new account.
*
* @param user full or bare jid.
* @param password
* @param accountType xmpp account type can be replaced depend on server part.
* @param syncable
* @param storePassword
* @param useOrbot
* @return assigned account name.
* @throws NetworkException if user or server part are invalid.
*/
public String addAccount(String user, String password, AccountType accountType, boolean syncable, boolean storePassword, boolean useOrbot, boolean registerNewAccount) throws NetworkException {
if (accountType.getProtocol().isOAuth()) {
int index = 1;
while (true) {
user = String.valueOf(index);
boolean found = false;
for (AccountItem accountItem : accountItems.values()) {
if (accountItem.getConnectionSettings().getServerName().equals(accountType.getFirstServer()) && accountItem.getConnectionSettings().getUserName().equals(user)) {
found = true;
break;
}
}
if (!found) {
break;
}
index++;
}
}
if (user == null) {
throw new NetworkException(R.string.EMPTY_USER_NAME);
}
if (!user.contains("@")) {
if ("".equals(accountType.getFirstServer())) {
throw new NetworkException(R.string.EMPTY_SERVER_NAME);
} else {
user += "@" + accountType.getFirstServer();
}
}
String serverName = XmppStringUtils.parseDomain(user);
String userName = XmppStringUtils.parseLocalpart(user);
String resource = XmppStringUtils.parseResource(user);
String host = accountType.getHost();
int port = accountType.getPort();
boolean tlsRequired = accountType.isTLSRequired();
if (useOrbot) {
tlsRequired = true;
}
if ("".equals(serverName)) {
throw new NetworkException(R.string.EMPTY_SERVER_NAME);
} else if (!accountType.isAllowServer() && !serverName.equals(accountType.getFirstServer())) {
throw new NetworkException(R.string.INCORRECT_USER_NAME);
}
if ("".equals(userName)) {
throw new NetworkException(R.string.EMPTY_USER_NAME);
}
if ("".equals(resource)) {
resource = generateResource();
}
if (accountType.getId() == R.array.account_type_xmpp) {
host = serverName;
for (AccountType check : accountTypes) {
if (check.getServers().contains(serverName)) {
accountType = check;
host = check.getHost();
port = check.getPort();
tlsRequired = check.isTLSRequired();
break;
}
}
}
AccountItem accountItem;
while (true) {
if (getAccount(userName + '@' + serverName + '/' + resource) == null) {
break;
}
resource = generateResource();
}
boolean useCustomHost = application.getResources().getBoolean(R.bool.account_use_custom_host_default);
if (accountType.getProtocol() == AccountProtocol.gtalk) {
useCustomHost = true;
}
boolean useCompression = application.getResources().getBoolean(R.bool.account_use_compression_default);
ArchiveMode archiveMode = ArchiveMode.valueOf(application.getString(R.string.account_archive_mode_default_value));
accountItem = addAccount(accountType.getProtocol(), useCustomHost, host, port, serverName, userName, storePassword, password, resource, getNextColorIndex(), 0, StatusMode.available, SettingsManager.statusText(), true, true, tlsRequired ? TLSMode.required : TLSMode.enabled, useCompression, useOrbot ? ProxyType.orbot : ProxyType.none, "localhost", 8080, "", "", syncable, null, null, archiveMode, registerNewAccount);
if (accountItem == null) {
throw new NetworkException(R.string.ACCOUNT_REGISTER_FAILED);
}
onAccountChanged(accountItem.getAccount());
if (accountItems.size() > 1 && SettingsManager.contactsEnableShowAccounts()) {
SettingsManager.enableContactsShowAccount();
}
return accountItem.getAccount();
}
use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.
the class ConnectionManager method sendStanza.
/**
* Send stanza to authenticated connection.
*
* @param account
* @param stanza
*/
public void sendStanza(String account, Stanza stanza) throws NetworkException {
ConnectionThread connectionThread = null;
for (ConnectionThread check : managedConnections) {
if (check.getConnectionItem() instanceof AccountItem && ((AccountItem) check.getConnectionItem()).getAccount().equals(account)) {
connectionThread = check;
break;
}
}
if (connectionThread == null || !connectionThread.getConnectionItem().getState().isConnected()) {
throw new NetworkException(R.string.NOT_CONNECTED);
}
XMPPConnection xmppConnection = connectionThread.getXMPPConnection();
try {
xmppConnection.sendStanza(stanza);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
throw new NetworkException(R.string.XMPP_EXCEPTION);
}
}
use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.
the class ConnectionThread method passwordRequest.
/**
* Request to renew password using OAuth.
*/
private void passwordRequest() {
final OAuthResult oAuthResult;
try {
oAuthResult = OAuthManager.getInstance().requestAccessToken(protocol, token);
} catch (NetworkException e) {
throw new RuntimeException(e);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
onPasswordReceived(oAuthResult);
}
});
}
use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.
the class MessageArchiveManager method checkForDefaults.
private void checkForDefaults(String account, Default received) {
if (received == null || !received.isUnset() || received.getSave() != SaveMode.fls)
return;
Default defaultItem = new Default();
defaultItem.setExpire(received.getExpire());
defaultItem.setOtr(received.getOtr());
defaultItem.setSave(SaveMode.body);
Pref pref = new Pref();
pref.setDefault(defaultItem);
pref.setType(Type.set);
try {
ConnectionManager.getInstance().sendStanza(account, pref);
} catch (NetworkException e) {
}
}
use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.
the class MessageArchiveManager method requestChat.
private void requestChat(String account, CollectionHeader header, String after, boolean modification) {
Retrieve packet = new Retrieve();
packet.setType(Type.get);
Set rsm = new Set();
rsm.setMax(RSM_MAX);
rsm.setAfter(after);
packet.setRsm(rsm);
packet.setWith(header.getWith());
packet.setStartString(header.getStartString());
modificationRequests.put(account, packet.getPacketID(), modification);
try {
if (!modification) {
ConnectionManager.getInstance().sendStanza(account, packet);
return;
}
ConnectionManager.getInstance().sendRequest(account, packet, new OnResponseListener() {
@Override
public void onReceived(String account, String packetId, IQ iq) {
if (iq instanceof Chat && ((Chat) iq).isValid())
onChatReceived(account, (Chat) iq);
else
onError(account, packetId, iq);
}
@Override
public void onError(String account, String packetId, IQ iq) {
onModifiedAvailable(account);
}
@Override
public void onTimeout(String account, String packetId) {
onError(account, packetId, null);
}
@Override
public void onDisconnect(String account, String packetId) {
}
});
} catch (NetworkException e) {
}
}
Aggregations