Search in sources :

Example 46 with AccountItem

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

the class AccountListActivity method updateAccountOrder.

public void updateAccountOrder() {
    if (accountListAdapter != null) {
        int order = 1;
        for (AccountItem account : accountListAdapter.getItems()) AccountManager.getInstance().setOrder(account.getAccount(), order++);
        XabberAccountManager.getInstance().setLastOrderChangeTimestampIsNow();
        if (XabberAccountManager.getInstance().getAccount() != null)
            XabberAccountManager.getInstance().updateAccountSettings();
    }
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem)

Example 47 with AccountItem

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

the class AccountListActivity method update.

private void update() {
    List<AccountItem> accountItems = new ArrayList<>();
    for (AccountItem accountItem : AccountManager.getInstance().getAllAccountItems()) {
        accountItems.add(accountItem);
    }
    accountListAdapter.setAccountItems(accountItems);
    barPainter.setDefaultColor();
    // set margin for textView
    int height = 52 * (accountItems.size() + 1) + 16;
    final float scale = getResources().getDisplayMetrics().density;
    int pixels = (int) (height * scale + 0.5f);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(16, pixels, 16, 0);
    tvSummary.setLayoutParams(params);
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) ArrayList(java.util.ArrayList) RelativeLayout(android.widget.RelativeLayout)

Example 48 with AccountItem

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

the class ConnectionThread method connectAndLogin.

@SuppressWarnings("WeakerAccess")
void connectAndLogin() {
    AndroidLoggingHandler.reset(new AndroidLoggingHandler());
    java.util.logging.Logger.getLogger(XMPPTCPConnection.class.getName()).setLevel(Level.FINEST);
    java.util.logging.Logger.getLogger(AbstractDNSClient.class.getName()).setLevel(Level.FINEST);
    java.util.logging.Logger.getLogger(AbstractXMPPConnection.class.getName()).setLevel(Level.FINEST);
    java.util.logging.Logger.getLogger(DNSUtil.class.getName()).setLevel(Level.FINEST);
    if (connection.getConfiguration().getPassword().isEmpty()) {
        AccountErrorEvent accountErrorEvent = new AccountErrorEvent(connectionItem.getAccount(), AccountErrorEvent.Type.PASS_REQUIRED, "");
        // com.xabber.android.data.account.AccountManager.getInstance().addAccountError(accountErrorEvent);
        com.xabber.android.data.account.AccountManager.getInstance().setEnabled(connectionItem.getAccount(), false);
        EventBus.getDefault().postSticky(accountErrorEvent);
        return;
    }
    // switch (SettingsManager.connectionDnsResolver()) {
    // case dnsJavaResolver:
    // LogManager.i(this, "Use DNS Java resolver");
    // ExtDNSJavaResolver.setup();
    // break;
    // case miniDnsResolver:
    // LogManager.i(this, "Use Mini DNS resolver");
    // MiniDnsResolver.setup();
    // break;
    // }
    LogManager.i(this, "Use DNS Java resolver");
    ExtDNSJavaResolver.setup();
    ProviderManager.addExtensionProvider(DataForm.ELEMENT, DataForm.NAMESPACE, new CustomDataProvider());
    ProviderManager.addExtensionProvider(ForwardComment.ELEMENT, ForwardComment.NAMESPACE, new ForwardCommentProvider());
    ProviderManager.addExtensionProvider(ReferenceElement.ELEMENT, ReferenceElement.NAMESPACE, new ReferencesProvider());
    ProviderManager.addIQProvider(XTokenIQ.ELEMENT, XTokenIQ.NAMESPACE, new XTokenProvider());
    ProviderManager.addIQProvider(SessionsIQ.ELEMENT, SessionsIQ.NAMESPACE, new SessionsProvider());
    try {
        LogManager.i(this, "Trying to connect and login...");
        if (!connection.isConnected()) {
            connectionItem.updateState(ConnectionState.connecting);
            connection.connect();
        } else {
            LogManager.i(this, "Already connected");
        }
        if (!connection.isAuthenticated()) {
            ProviderManager.addIQProvider(HttpConfirmIq.ELEMENT, HttpConfirmIq.NAMESPACE, new HttpConfirmIqProvider());
            connection.login();
        } else {
            LogManager.i(this, "Already authenticated");
        }
    } catch (SASLErrorException e) {
        LogManager.exception(this, e);
        if (e.getMechanism().equals(SASLXTOKENMechanism.NAME)) {
            LogManager.d(this, "Authorization error with x-token: " + e.toString());
            AccountManager.getInstance().removeXToken(connectionItem.getAccount());
        }
        AccountErrorEvent accountErrorEvent = new AccountErrorEvent(connectionItem.getAccount(), AccountErrorEvent.Type.AUTHORIZATION, e.getMessage());
        // com.xabber.android.data.account.AccountManager.getInstance().addAccountError(accountErrorEvent);
        com.xabber.android.data.account.AccountManager.getInstance().setEnabled(connectionItem.getAccount(), false);
        EventBus.getDefault().postSticky(accountErrorEvent);
    // catching RuntimeExceptions seems to be strange, but we got a lot of error coming from
    // Smack or mini DSN client inside of Smack.
    } catch (XMPPException | SmackException | IOException | RuntimeException e) {
        LogManager.exception(this, e);
        if (!((AccountItem) connectionItem).isSuccessfulConnectionHappened()) {
            LogManager.i(this, "There was no successful connection, disabling account");
            AccountErrorEvent accountErrorEvent = new AccountErrorEvent(connectionItem.getAccount(), AccountErrorEvent.Type.CONNECTION, Log.getStackTraceString(e));
            com.xabber.android.data.account.AccountManager.getInstance().addAccountError(accountErrorEvent);
            com.xabber.android.data.account.AccountManager.getInstance().setEnabled(connectionItem.getAccount(), false);
            EventBus.getDefault().postSticky(accountErrorEvent);
        }
    } catch (InterruptedException e) {
        LogManager.exception(this, e);
    }
    LogManager.i(this, "Connection thread finished");
}
Also used : SessionsProvider(com.xabber.android.data.extension.xtoken.SessionsProvider) AndroidLoggingHandler(com.xabber.android.data.log.AndroidLoggingHandler) AccountItem(com.xabber.android.data.account.AccountItem) ReferencesProvider(com.xabber.android.data.extension.references.ReferencesProvider) SmackException(org.jivesoftware.smack.SmackException) HttpConfirmIqProvider(com.xabber.android.data.xaccount.HttpConfirmIqProvider) XTokenProvider(com.xabber.android.data.extension.xtoken.XTokenProvider) IOException(java.io.IOException) AccountErrorEvent(com.xabber.android.data.account.AccountErrorEvent) CustomDataProvider(com.xabber.android.data.extension.httpfileupload.CustomDataProvider) ForwardCommentProvider(com.xabber.android.data.extension.forward.ForwardCommentProvider) SASLErrorException(org.jivesoftware.smack.sasl.SASLErrorException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 49 with AccountItem

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

the class RecentChatFragment method updateChats.

public void updateChats() {
    Collection<AbstractChat> chats = MessageManager.getInstance().getChats();
    List<AbstractChat> recentChats = new ArrayList<>();
    for (AbstractChat abstractChat : chats) {
        MessageItem lastMessage = abstractChat.getLastMessage();
        if (lastMessage != null) {
            AccountItem accountItem = AccountManager.getInstance().getAccount(abstractChat.getAccount());
            if (accountItem != null && accountItem.isEnabled()) {
                recentChats.add(abstractChat);
            }
        }
    }
    // crowdfunding chat
    int unreadCount = CrowdfundingManager.getInstance().getUnreadMessageCount();
    CrowdfundingMessage message = CrowdfundingManager.getInstance().getLastNotDelayedMessageFromRealm();
    if (message != null)
        recentChats.add(CrowdfundingChat.createCrowdfundingChat(unreadCount, message));
    Collections.sort(recentChats, ChatComparator.CHAT_COMPARATOR);
    final List<AbstractContact> newContacts = new ArrayList<>();
    for (AbstractChat chat : recentChats) {
        if (chat instanceof CrowdfundingChat)
            newContacts.add(new CrowdfundingContact((CrowdfundingChat) chat));
        else if (!chat.isArchived() || ((ChatActivity) getActivity()).isShowArchived())
            newContacts.add(RosterManager.getInstance().getBestContact(chat.getAccount(), chat.getUser()));
    }
    updateItems(newContacts);
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) CrowdfundingContact(com.xabber.android.data.roster.CrowdfundingContact) ChatActivity(com.xabber.android.ui.activity.ChatActivity) AccountItem(com.xabber.android.data.account.AccountItem) AbstractChat(com.xabber.android.data.message.AbstractChat) ArrayList(java.util.ArrayList) AbstractContact(com.xabber.android.data.roster.AbstractContact) CrowdfundingChat(com.xabber.android.data.message.CrowdfundingChat) CrowdfundingMessage(com.xabber.android.data.database.realm.CrowdfundingMessage)

Example 50 with AccountItem

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

the class UploadService method startWork.

private void startWork(AccountJid account, UserJid user, List<String> filePaths, CharSequence uploadServerUrl, String existMessageId) {
    // get account item
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem == null) {
        publishError(null, "Account not found");
        return;
    }
    // get upload jid
    Jid uploadJid;
    try {
        uploadJid = JidCreate.bareFrom(uploadServerUrl);
    } catch (XmppStringprepException e) {
        publishError(null, "Wrong upload jid");
        return;
    }
    final String fileMessageId;
    if (existMessageId == null) {
        // create fileMessage with files
        List<File> files = new ArrayList<>();
        for (String filePath : filePaths) {
            files.add(new File(filePath));
        }
        fileMessageId = MessageManager.getInstance().createFileMessage(account, user, files);
    } else
        // use existing fileMessage
        fileMessageId = existMessageId;
    HashMap<String, String> uploadedFilesUrls = new HashMap<>();
    List<String> notUploadedFilesPaths = new ArrayList<>();
    List<File> notUploadedFiles = new ArrayList<>();
    List<String> errors = new ArrayList<>();
    for (String filePath : filePaths) {
        if (needStop) {
            stopWork(fileMessageId);
            return;
        }
        try {
            File uncompressedFile = new File(filePath);
            final File file;
            // compress file if image
            if (FileManager.fileIsImage(uncompressedFile) && SettingsManager.connectionCompressImage()) {
                file = ImageCompressor.compressImage(uncompressedFile, getCompressedDirPath());
                if (file == null)
                    throw new Exception("Compress image failed");
            } else
                file = uncompressedFile;
            // request slot
            Stanza slot = requestSlot(accountItem, file, uploadJid);
            if (!(slot instanceof Slot))
                throw new Exception("Could not request upload slot");
            // upload file
            Response response = uploadFileToSlot(account, (Slot) slot, file);
            if (response.isSuccessful())
                uploadedFilesUrls.put(filePath, ((Slot) slot).getGetUrl());
            else
                throw new Exception("Upload failed: " + response.message());
        } catch (Exception e) {
            notUploadedFilesPaths.add(filePath);
            notUploadedFiles.add(new File(filePath));
            errors.add(e.toString());
        }
        publishProgress(fileMessageId, uploadedFilesUrls.size(), filePaths.size());
    }
    removeTempDirectory();
    // check that files are uploaded
    if (uploadedFilesUrls.size() == 0) {
        setErrorForMessage(fileMessageId, generateErrorDescriptionForFiles(notUploadedFilesPaths, errors));
        publishError(fileMessageId, "Could not upload any files");
        return;
    }
    // save results to Realm and send message
    MessageManager.getInstance().updateFileMessage(account, user, fileMessageId, uploadedFilesUrls, notUploadedFilesPaths);
    publishCompleted(fileMessageId);
    // if some files have errors move its to separate message
    if (notUploadedFilesPaths.size() > 0) {
        String messageId = MessageManager.getInstance().createFileMessage(account, user, notUploadedFiles);
        setErrorForMessage(messageId, generateErrorDescriptionForFiles(notUploadedFilesPaths, errors));
    }
}
Also used : UserJid(com.xabber.android.data.entity.UserJid) AccountJid(com.xabber.android.data.entity.AccountJid) Jid(org.jxmpp.jid.Jid) AccountItem(com.xabber.android.data.account.AccountItem) HashMap(java.util.HashMap) Stanza(org.jivesoftware.smack.packet.Stanza) ArrayList(java.util.ArrayList) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException) Response(okhttp3.Response) Slot(com.xabber.xmpp.httpfileupload.Slot) File(java.io.File)

Aggregations

AccountItem (com.xabber.android.data.account.AccountItem)96 AccountJid (com.xabber.android.data.entity.AccountJid)24 UserJid (com.xabber.android.data.entity.UserJid)14 View (android.view.View)12 NetworkException (com.xabber.android.data.NetworkException)11 ArrayList (java.util.ArrayList)11 SmackException (org.jivesoftware.smack.SmackException)11 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)11 Message (org.jivesoftware.smack.packet.Message)11 Presence (org.jivesoftware.smack.packet.Presence)10 TextView (android.widget.TextView)9 XMPPException (org.jivesoftware.smack.XMPPException)9 Jid (org.jxmpp.jid.Jid)9 ImageView (android.widget.ImageView)8 StatusMode (com.xabber.android.data.account.StatusMode)6 ConnectionState (com.xabber.android.data.connection.ConnectionState)5 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)5 Realm (io.realm.Realm)5 ColorDrawable (android.graphics.drawable.ColorDrawable)4 AccountManager (com.xabber.android.data.account.AccountManager)4