Search in sources :

Example 26 with NetworkException

use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.

the class ContactListActivity method showPassDialogs.

public void showPassDialogs() {
    List<XMPPAccountSettings> items = XabberAccountManager.getInstance().getXmppAccountsForCreate();
    if (items != null && items.size() > 0) {
        for (XMPPAccountSettings item : items) {
            if (XabberAccountManager.getInstance().isAccountSynchronize(item.getJid()) || SettingsManager.isSyncAllAccounts()) {
                if (!item.isDeleted() && XabberAccountManager.getInstance().getExistingAccount(item.getJid()) == null) {
                    if (item.getToken() != null && !item.getToken().isEmpty()) {
                        // create account if exist token
                        try {
                            AccountJid accountJid = AccountManager.getInstance().addAccount(item.getJid(), "", item.getToken(), false, true, true, false, false, true, false);
                            AccountManager.getInstance().setColor(accountJid, ColorManager.getInstance().convertColorNameToIndex(item.getColor()));
                            AccountManager.getInstance().setOrder(accountJid, item.getOrder());
                            AccountManager.getInstance().setTimestamp(accountJid, item.getTimestamp());
                            AccountManager.getInstance().onAccountChanged(accountJid);
                        } catch (NetworkException e) {
                            Application.getInstance().onError(e);
                        }
                    // require pass if token not exist
                    } else
                        EnterPassDialog.newInstance(item).show(getFragmentManager(), EnterPassDialog.class.getName());
                }
            }
        }
        XabberAccountManager.getInstance().clearXmppAccountsForCreate();
    }
}
Also used : XMPPAccountSettings(com.xabber.android.data.xaccount.XMPPAccountSettings) AccountJid(com.xabber.android.data.entity.AccountJid) NetworkException(com.xabber.android.data.NetworkException)

Example 27 with NetworkException

use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.

the class AccountManager method updateAccount.

/**
 * Update user`s account.
 * <p/>
 * It will reconnect to the server if changes was made.
 * <p/>
 * It will remove old account and create new one if full jid was changed.
 *
 * @param account       full source jid
 */
public void updateAccount(AccountJid account, boolean custom, String host, int port, DomainBareJid serverName, Localpart userName, boolean storePassword, String password, String token, Resourcepart resource, int priority, boolean enabled, boolean saslEnabled, TLSMode tlsMode, boolean compression, ProxyType proxyType, String proxyHost, int proxyPort, String proxyUser, String proxyPassword, boolean syncable, ArchiveMode archiveMode, int colorIndex) {
    AccountItem result;
    AccountItem accountItem = getAccount(account);
    if (accountItem == null) {
        return;
    }
    if (accountItem.getConnectionSettings().getServerName().equals(serverName) && accountItem.getConnectionSettings().getUserName().equals(userName) && accountItem.getConnectionSettings().getResource().equals(resource)) {
        result = accountItem;
        result.setColorIndex(colorIndex);
        boolean reconnect = false;
        if (accountItem.getConnectionSettings().isCustomHostAndPort() != custom || !accountItem.getConnectionSettings().getHost().equals(host) || accountItem.getConnectionSettings().getPort() != port || !accountItem.getConnectionSettings().getPassword().equals(password) || accountItem.getConnectionSettings().getTlsMode() != tlsMode || accountItem.getConnectionSettings().isSaslEnabled() != saslEnabled || accountItem.getConnectionSettings().useCompression() != compression || accountItem.getConnectionSettings().getProxyType() != proxyType || !accountItem.getConnectionSettings().getProxyHost().equals(proxyHost) || accountItem.getConnectionSettings().getProxyPort() != proxyPort || !accountItem.getConnectionSettings().getProxyUser().equals(proxyUser) || !accountItem.getConnectionSettings().getProxyPassword().equals(proxyPassword)) {
            result.updateConnectionSettings(custom, host, port, password, saslEnabled, tlsMode, compression, proxyType, proxyHost, proxyPort, proxyUser, proxyPassword);
            reconnect = true;
        }
        if (result.isSyncable() != syncable) {
            result.setSyncable(syncable);
            for (OnAccountSyncableChangedListener listener : application.getManagers(OnAccountSyncableChangedListener.class)) {
                listener.onAccountSyncableChanged(result);
            }
        }
        result.setStorePassword(storePassword);
        boolean changed = result.isEnabled() != enabled;
        result.setEnabled(enabled);
        if (result.getPriority() != priority) {
            result.setPriority(priority);
            try {
                PresenceManager.getInstance().resendPresence(account);
            } catch (NetworkException e) {
                LogManager.exception(this, e);
            }
        }
        if (result.getArchiveMode() != archiveMode) {
            result.setArchiveMode(archiveMode);
        }
        if (changed && enabled) {
            onAccountEnabled(result);
            if (result.getRawStatusMode().isOnline()) {
                onAccountOnline(result);
            }
        }
        if (changed || reconnect) {
            result.setSuccessfulConnectionHappened(false);
            result.recreateConnection();
        }
        if (changed && !enabled) {
            if (result.getRawStatusMode().isOnline()) {
                onAccountOffline(result);
            }
            onAccountDisabled(result);
        }
        requestToWriteAccount(result);
    } else {
        StatusMode statusMode = accountItem.getRawStatusMode();
        String statusText = accountItem.getStatusText();
        KeyPair keyPair = accountItem.getKeyPair();
        Date lastSync = accountItem.getLastSync();
        removeAccountWithoutCallback(account);
        result = addAccount(custom, host, port, serverName, userName, storePassword, password, token, resource, colorIndex, accountItem.getOrder(), accountItem.isSyncNotAllowed(), accountItem.getTimestamp(), priority, statusMode, statusText, enabled, saslEnabled, tlsMode, compression, proxyType, proxyHost, proxyPort, proxyUser, proxyPassword, syncable, keyPair, lastSync, archiveMode, false);
    }
    onAccountChanged(result.getAccount());
    // disable sync for account if it use not default settings
    ConnectionSettings connectionSettings = result.getConnectionSettings();
    if (connectionSettings.isCustomHostAndPort() || connectionSettings.getProxyType() != ProxyType.none || connectionSettings.getTlsMode() == TLSMode.legacy) {
        result.setSyncNotAllowed(true);
    } else
        result.setSyncNotAllowed(false);
}
Also used : OnAccountSyncableChangedListener(com.xabber.android.data.account.listeners.OnAccountSyncableChangedListener) KeyPair(java.security.KeyPair) NetworkException(com.xabber.android.data.NetworkException) Date(java.util.Date) ConnectionSettings(com.xabber.android.data.connection.ConnectionSettings)

Example 28 with NetworkException

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.
 * @return assigned account name.
 * @throws NetworkException if user or server part are invalid.
 */
public AccountJid addAccount(String user, String password, String token, boolean syncable, boolean storePassword, boolean xabberSync, boolean useOrbot, boolean registerNewAccount, boolean enabled, boolean tlsRequired) throws NetworkException {
    if (user == null) {
        throw new NetworkException(R.string.EMPTY_USER_NAME);
    }
    if (user.contains(" ")) {
        throw new NetworkException(R.string.INCORRECT_USER_NAME);
    }
    DomainBareJid serverName;
    try {
        serverName = JidCreate.domainBareFrom(user);
    } catch (XmppStringprepException e) {
        throw new NetworkException(R.string.INCORRECT_USER_NAME);
    }
    Localpart userName;
    try {
        userName = Localpart.from(XmppStringUtils.parseLocalpart(user));
    } catch (XmppStringprepException e) {
        throw new NetworkException(R.string.INCORRECT_USER_NAME);
    }
    if (isAccountExist(user))
        throw new NetworkException(R.string.ACCOUNT_EXIST);
    Resourcepart resource = null;
    String resourceString = XmppStringUtils.parseResource(user).trim();
    if (!TextUtils.isEmpty(resourceString)) {
        try {
            resource = Resourcepart.from(resourceString);
        } catch (XmppStringprepException e) {
            LogManager.exception(this, e);
        }
    }
    String host = serverName.getDomain().toString();
    int port = 5222;
    if (resource == null) {
        resource = generateResource();
    }
    AccountItem accountItem;
    boolean useCustomHost = application.getResources().getBoolean(R.bool.account_use_custom_host_default);
    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(useCustomHost, host, port, serverName, userName, storePassword, password, token, resource, getNextColorIndex(), getNextOrder(), false, XabberAccountManager.getInstance().getCurrentTime(), 0, StatusMode.available, SettingsManager.statusText(), enabled, 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();
    }
    // add xmpp account settings
    if (xabberSync)
        XabberAccountManager.getInstance().addAccountSyncState(accountItem.getAccount().getFullJid().asBareJid().toString(), true);
    else
        SettingsManager.setSyncAllAccounts(false);
    return accountItem.getAccount();
}
Also used : Localpart(org.jxmpp.jid.parts.Localpart) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NetworkException(com.xabber.android.data.NetworkException) DomainBareJid(org.jxmpp.jid.DomainBareJid) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 29 with NetworkException

use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.

the class ContactAddFragment method addContact.

@Override
public void addContact() {
    final AccountJid account = (AccountJid) accountView.getSelectedItem();
    if (account == null || getAccount() == null) {
        Toast.makeText(getActivity(), getString(R.string.EMPTY_ACCOUNT), Toast.LENGTH_LONG).show();
        return;
    }
    String contactString = userView.getText().toString();
    contactString = contactString.trim();
    if (contactString.contains(" ")) {
        userView.setError(getString(R.string.INCORRECT_USER_NAME));
        return;
    }
    if (TextUtils.isEmpty(contactString)) {
        userView.setError(getString(R.string.EMPTY_USER_NAME));
        return;
    }
    final UserJid user;
    try {
        EntityBareJid entityFullJid = JidCreate.entityBareFrom(contactString);
        user = UserJid.from(entityFullJid);
    } catch (XmppStringprepException | UserJid.UserJidCreateException e) {
        e.printStackTrace();
        userView.setError(getString(R.string.INCORRECT_USER_NAME));
        return;
    }
    if (listenerActivity != null)
        listenerActivity.showProgress(true);
    final String name = nameView.getText().toString();
    final ArrayList<String> groups = getSelected();
    Application.getInstance().runInBackgroundUserRequest(new Runnable() {

        @Override
        public void run() {
            try {
                RosterManager.getInstance().createContact(account, user, name, groups);
                PresenceManager.getInstance().requestSubscription(account, user);
            } catch (SmackException.NotLoggedInException | SmackException.NotConnectedException e) {
                Application.getInstance().onError(R.string.NOT_CONNECTED);
                stopAddContactProcess(false);
            } catch (XMPPException.XMPPErrorException e) {
                Application.getInstance().onError(R.string.XMPP_EXCEPTION);
                stopAddContactProcess(false);
            } catch (SmackException.NoResponseException e) {
                Application.getInstance().onError(R.string.CONNECTION_FAILED);
                stopAddContactProcess(false);
            } catch (NetworkException e) {
                Application.getInstance().onError(e);
                stopAddContactProcess(false);
            } catch (InterruptedException e) {
                LogManager.exception(this, e);
                stopAddContactProcess(false);
            }
            stopAddContactProcess(true);
        }
    });
}
Also used : SmackException(org.jivesoftware.smack.SmackException) UserJid(com.xabber.android.data.entity.UserJid) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) AccountJid(com.xabber.android.data.entity.AccountJid) EntityBareJid(org.jxmpp.jid.EntityBareJid) NetworkException(com.xabber.android.data.NetworkException)

Example 30 with NetworkException

use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.

the class EnterPassDialog method createAccount.

private void createAccount(boolean enabled) {
    try {
        AccountJid accountJid = AccountManager.getInstance().addAccount(jid, edtPass.getText().toString(), token, false, true, true, false, false, enabled, false);
        AccountManager.getInstance().setColor(accountJid, ColorManager.getInstance().convertColorNameToIndex(color));
        AccountManager.getInstance().setOrder(accountJid, order);
        AccountManager.getInstance().setTimestamp(accountJid, timestamp);
        AccountManager.getInstance().onAccountChanged(accountJid);
    } catch (NetworkException e) {
        Application.getInstance().onError(e);
    }
}
Also used : AccountJid(com.xabber.android.data.entity.AccountJid) NetworkException(com.xabber.android.data.NetworkException)

Aggregations

NetworkException (com.xabber.android.data.NetworkException)64 Message (org.jivesoftware.smack.packet.Message)13 AccountJid (com.xabber.android.data.entity.AccountJid)11 OtrException (net.java.otr4j.OtrException)11 AccountItem (com.xabber.android.data.account.AccountItem)10 AbstractChat (com.xabber.android.data.message.AbstractChat)10 OnResponseListener (com.xabber.android.data.connection.OnResponseListener)6 Date (java.util.Date)6 IQ (org.jivesoftware.smack.packet.IQ)6 UserJid (com.xabber.android.data.entity.UserJid)5 ArrayList (java.util.ArrayList)4 Presence (org.jivesoftware.smack.packet.Presence)4 Intent (android.content.Intent)3 Set (com.xabber.xmpp.rsm.Set)3 File (java.io.File)3 SmackException (org.jivesoftware.smack.SmackException)3 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)3 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)2 Captcha (com.xabber.android.data.extension.captcha.Captcha)2 RoomChat (com.xabber.android.data.extension.muc.RoomChat)2