Search in sources :

Example 41 with AccountJid

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

the class AttentionManager method onStanza.

@Override
public void onStanza(ConnectionItem connection, Stanza stanza) {
    if (!(stanza instanceof Message)) {
        return;
    }
    if (!SettingsManager.chatsAttention()) {
        return;
    }
    final AccountJid account = connection.getAccount();
    UserJid from;
    try {
        from = UserJid.from(stanza.getFrom());
    } catch (UserJid.UserJidCreateException e) {
        e.printStackTrace();
        return;
    }
    for (ExtensionElement packetExtension : stanza.getExtensions()) {
        if (packetExtension instanceof AttentionExtension) {
            MessageManager.getInstance().openChat(account, from);
            MessageManager.getInstance().getOrCreateChat(account, from).newAction(null, null, ChatAction.attention_requested);
            attentionRequestProvider.add(new AttentionRequest(account, from.getBareUserJid()), true);
        }
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) AccountJid(com.xabber.android.data.entity.AccountJid) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) UserJid(com.xabber.android.data.entity.UserJid) AttentionExtension(org.jivesoftware.smackx.attention.packet.AttentionExtension)

Example 42 with AccountJid

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

the class BlockingManager method getBlockedContacts.

public List<UserJid> getBlockedContacts(AccountJid account) {
    List<UserJid> blockedContacts = new ArrayList<>();
    Boolean supported = isSupported(account);
    BlockingCommandManager blockingCommandManager = getBlockingCommandManager(account);
    if (blockingCommandManager != null && supported != null && supported) {
        try {
            List<Jid> blockedJids = blockingCommandManager.getBlockList();
            for (Jid jid : blockedJids) {
                blockedContacts.add(UserJid.from(jid));
            }
        } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException | SmackException.NotConnectedException | UserJid.UserJidCreateException e) {
            LogManager.exception(LOG_TAG, e);
        }
    }
    updateCachedBlockedContacts(account, blockedContacts);
    return blockedContacts;
}
Also used : UserJid(com.xabber.android.data.entity.UserJid) Jid(org.jxmpp.jid.Jid) AccountJid(com.xabber.android.data.entity.AccountJid) ArrayList(java.util.ArrayList) UserJid(com.xabber.android.data.entity.UserJid) BlockingCommandManager(org.jivesoftware.smackx.blocking.BlockingCommandManager)

Example 43 with AccountJid

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

the class BlockingManager method onAuthorized.

public void onAuthorized(final ConnectionItem connection) {
    final AccountJid account = connection.getAccount();
    BlockingCommandManager blockingCommandManager = BlockingCommandManager.getInstanceFor(connection.getConnection());
    try {
        boolean supportedByServer = blockingCommandManager.isSupportedByServer();
        if (supportedByServer) {
            // cache block list inside
            List<UserJid> blockedContacts = new ArrayList<>();
            try {
                List<Jid> blockedJids = blockingCommandManager.getBlockList();
                for (Jid jid : blockedJids) {
                    blockedContacts.add(UserJid.from(jid));
                }
            } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException | SmackException.NotConnectedException | UserJid.UserJidCreateException e) {
                LogManager.exception(LOG_TAG, e);
            }
            // Cache block inside manager
            // For contact list building used only this list of blocked contacts
            updateCachedBlockedContacts(account, blockedContacts);
        }
        addBlockedListener(blockingCommandManager, account);
        addUnblockedListener(blockingCommandManager, account);
        addUnblockedAllListener(blockingCommandManager, account);
        // block list already cached successfully
        supportForAccounts.put(account, supportedByServer);
        BlockingManager.notify(account);
    } catch (SmackException.NotConnectedException | XMPPException.XMPPErrorException | SmackException.NoResponseException | InterruptedException e) {
        LogManager.exception(this, e);
    }
}
Also used : UserJid(com.xabber.android.data.entity.UserJid) Jid(org.jxmpp.jid.Jid) AccountJid(com.xabber.android.data.entity.AccountJid) ArrayList(java.util.ArrayList) UserJid(com.xabber.android.data.entity.UserJid) BlockingCommandManager(org.jivesoftware.smackx.blocking.BlockingCommandManager) AccountJid(com.xabber.android.data.entity.AccountJid)

Example 44 with AccountJid

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

the class ClientStateManager method sendClientState.

protected static void sendClientState(Nonza nonza) {
    AccountManager accountManager = AccountManager.getInstance();
    for (AccountJid accountName : accountManager.getEnabledAccounts()) {
        AccountItem accountItem = accountManager.getAccount(accountName);
        if (accountItem == null) {
            continue;
        }
        AbstractXMPPConnection xmppConnection = accountItem.getConnection();
        if (!xmppConnection.isAuthenticated()) {
            continue;
        }
        if (xmppConnection.hasFeature("csi", ClientStateIndication.NAMESPACE))
            try {
                xmppConnection.sendNonza(nonza);
            } catch (SmackException.NotConnectedException | InterruptedException e) {
                LogManager.exception(LOG_TAG, e);
            }
    }
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) SmackException(org.jivesoftware.smack.SmackException) AccountManager(com.xabber.android.data.account.AccountManager) AbstractXMPPConnection(org.jivesoftware.smack.AbstractXMPPConnection)

Example 45 with AccountJid

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

the class HttpFileUploadManager method uploadFile.

public void uploadFile(final AccountJid account, final UserJid user, final String filePath) {
    final Jid uploadServerUrl = uploadServers.get(account);
    if (uploadServerUrl == null) {
        return;
    }
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem == null) {
        return;
    }
    final File file = new File(filePath);
    final com.xabber.xmpp.httpfileupload.Request httpFileUpload = new com.xabber.xmpp.httpfileupload.Request();
    httpFileUpload.setFilename(file.getName());
    httpFileUpload.setSize(String.valueOf(file.length()));
    httpFileUpload.setTo(uploadServerUrl);
    try {
        accountItem.getConnection().sendIqWithResponseCallback(httpFileUpload, new StanzaListener() {

            @Override
            public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
                if (!(packet instanceof Slot)) {
                    return;
                }
                uploadFileToSlot(account, (Slot) packet);
            }

            private void uploadFileToSlot(final AccountJid account, final Slot slot) {
                SSLSocketFactory sslSocketFactory = null;
                MemorizingTrustManager mtm = CertificateManager.getInstance().getNewFileUploadManager(account);
                final SSLContext sslContext;
                try {
                    sslContext = SSLContext.getInstance("SSL");
                    sslContext.init(null, new X509TrustManager[] { mtm }, new java.security.SecureRandom());
                    sslSocketFactory = sslContext.getSocketFactory();
                } catch (NoSuchAlgorithmException | KeyManagementException e) {
                    return;
                }
                OkHttpClient client = new OkHttpClient().newBuilder().sslSocketFactory(sslSocketFactory).hostnameVerifier(mtm.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier())).writeTimeout(5, TimeUnit.MINUTES).connectTimeout(5, TimeUnit.MINUTES).readTimeout(5, TimeUnit.MINUTES).build();
                Request request = new Request.Builder().url(slot.getPutUrl()).put(RequestBody.create(CONTENT_TYPE, file)).build();
                final String fileMessageId;
                fileMessageId = MessageManager.getInstance().createFileMessage(account, user, file);
                LogManager.i(HttpFileUploadManager.this, "starting upload file to " + slot.getPutUrl() + " size " + file.length());
                client.newCall(request).enqueue(new Callback() {

                    @Override
                    public void onFailure(Call call, IOException e) {
                        LogManager.i(HttpFileUploadManager.this, "onFailure " + e.getMessage());
                        MessageManager.getInstance().updateMessageWithError(fileMessageId, e.toString());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        LogManager.i(HttpFileUploadManager.this, "onResponse " + response.isSuccessful() + " " + response.body().string());
                        if (response.isSuccessful()) {
                            MessageManager.getInstance().updateFileMessage(account, user, fileMessageId, slot.getGetUrl());
                        } else {
                            MessageManager.getInstance().updateMessageWithError(fileMessageId, response.message());
                        }
                    }
                });
            }
        }, new ExceptionCallback() {

            @Override
            public void processException(Exception exception) {
                LogManager.i(this, "On HTTP file upload slot error");
                LogManager.exception(this, exception);
                Application.getInstance().onError(R.string.http_file_upload_slot_error);
            }
        });
    } catch (SmackException.NotConnectedException | InterruptedException e) {
        LogManager.exception(this, e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) AccountItem(com.xabber.android.data.account.AccountItem) StanzaListener(org.jivesoftware.smack.StanzaListener) AccountJid(com.xabber.android.data.entity.AccountJid) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Call(okhttp3.Call) UserJid(com.xabber.android.data.entity.UserJid) AccountJid(com.xabber.android.data.entity.AccountJid) DomainBareJid(org.jxmpp.jid.DomainBareJid) Jid(org.jxmpp.jid.Jid) Stanza(org.jivesoftware.smack.packet.Stanza) Request(okhttp3.Request) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) ExceptionCallback(org.jivesoftware.smack.ExceptionCallback) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException) MemorizingTrustManager(de.duenndns.ssl.MemorizingTrustManager) Response(okhttp3.Response) Callback(okhttp3.Callback) ExceptionCallback(org.jivesoftware.smack.ExceptionCallback) X509TrustManager(javax.net.ssl.X509TrustManager) Slot(com.xabber.xmpp.httpfileupload.Slot) File(java.io.File)

Aggregations

AccountJid (com.xabber.android.data.entity.AccountJid)74 UserJid (com.xabber.android.data.entity.UserJid)38 AccountItem (com.xabber.android.data.account.AccountItem)21 Jid (org.jxmpp.jid.Jid)13 Intent (android.content.Intent)11 ArrayList (java.util.ArrayList)11 View (android.view.View)10 NetworkException (com.xabber.android.data.NetworkException)9 AbstractChat (com.xabber.android.data.message.AbstractChat)7 Message (org.jivesoftware.smack.packet.Message)7 Presence (org.jivesoftware.smack.packet.Presence)7 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)6 TextView (android.widget.TextView)5 StatusMode (com.xabber.android.data.account.StatusMode)5 Resourcepart (org.jxmpp.jid.parts.Resourcepart)5 ImageView (android.widget.ImageView)4 AccountManager (com.xabber.android.data.account.AccountManager)4 RosterContact (com.xabber.android.data.roster.RosterContact)4 ContactVO (com.xabber.android.presentation.ui.contactlist.viewobjects.ContactVO)4 BarPainter (com.xabber.android.ui.color.BarPainter)4