Search in sources :

Example 1 with NetworkException

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

the class PresenceManager method onPacket.

@Override
public void onPacket(ConnectionItem connection, String bareAddress, Stanza stanza) {
    if (!(connection instanceof AccountItem)) {
        return;
    }
    if (!(stanza instanceof Presence)) {
        return;
    }
    Presence presence = (Presence) stanza;
    if (presence.getType() == Presence.Type.subscribe) {
        String account = ((AccountItem) connection).getAccount();
        // Subscription request
        HashSet<String> set = requestedSubscriptions.get(account);
        if (set != null && set.contains(bareAddress)) {
            try {
                acceptSubscription(account, bareAddress);
            } catch (NetworkException e) {
            }
            subscriptionRequestProvider.remove(account, bareAddress);
        } else {
            subscriptionRequestProvider.add(new SubscriptionRequest(account, bareAddress), null);
        }
    }
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) Presence(org.jivesoftware.smack.packet.Presence) NetworkException(com.xabber.android.data.NetworkException)

Example 2 with NetworkException

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

the class HttpFileUploadManager method uploadFile.

public void uploadFile(final String account, final String user, final String filePath) {
    final String uploadServerUrl = uploadServers.get(account);
    if (uploadServerUrl == null) {
        return;
    }
    final File file = new File(filePath);
    final Request httpFileUpload = new Request();
    httpFileUpload.setFilename(file.getName());
    httpFileUpload.setSize(String.valueOf(file.length()));
    httpFileUpload.setTo(uploadServerUrl);
    try {
        ConnectionManager.getInstance().sendRequest(account, httpFileUpload, new OnResponseListener() {

            @Override
            public void onReceived(final String account, String packetId, IQ iq) {
                if (!httpFileUpload.getStanzaId().equals(packetId) || !(iq instanceof Slot)) {
                    return;
                }
                uploadFileToSlot(account, (Slot) iq);
            }

            private void uploadFileToSlot(final String account, final Slot slot) {
                AsyncHttpClient client = new AsyncHttpClient();
                client.setLoggingEnabled(SettingsManager.debugLog());
                client.setResponseTimeout(60 * 1000);
                FileEntity fileEntity = new FileEntity(file, ContentType.DEFAULT_BINARY);
                LogManager.i(this, "fileEntity.getContentLength() " + fileEntity.getContentLength());
                client.put(Application.getInstance(), slot.getPutUrl(), fileEntity, CONTENT_TYPE, new AsyncHttpResponseHandler() {

                    MessageItem fileMessage;

                    @Override
                    public void onStart() {
                        super.onStart();
                        LogManager.i(this, "uploadFileToSlot onStart");
                        fileMessage = MessageManager.getInstance().createFileMessage(account, user, file);
                    }

                    @Override
                    public void onSuccess(int i, Header[] headers, byte[] bytes) {
                        LogManager.i(this, "uploadFileToSlot onSuccess " + i);
                        MessageManager.getInstance().replaceMessage(account, user, fileMessage, slot.getGetUrl());
                        if (FileManager.fileIsImage(file)) {
                            saveImageToCache(slot.getGetUrl(), file);
                        }
                    }

                    @Override
                    public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                        LogManager.i(this, "uploadFileToSlot onFailure " + i);
                        MessageManager.getInstance().updateMessageWithError(account, user, fileMessage, file.getName());
                    }

                    @Override
                    public void onRetry(int retryNo) {
                        super.onRetry(retryNo);
                        LogManager.i(this, "uploadFileToSlot onRetry " + retryNo);
                    }

                    @Override
                    public void onCancel() {
                        super.onCancel();
                        LogManager.i(this, "uploadFileToSlot onCancel");
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        LogManager.i(this, "uploadFileToSlot onFinish");
                    }
                });
            }

            @Override
            public void onError(String account, String packetId, IQ iq) {
                LogManager.i(this, "On HTTP file upload slot error");
                Application.getInstance().onError(R.string.http_file_upload_slot_error);
            }

            @Override
            public void onTimeout(String account, String packetId) {
            }

            @Override
            public void onDisconnect(String account, String packetId) {
            }
        });
    } catch (NetworkException e) {
        e.printStackTrace();
    }
}
Also used : MessageItem(com.xabber.android.data.message.MessageItem) FileEntity(cz.msebera.android.httpclient.entity.FileEntity) Request(com.xabber.xmpp.httpfileupload.Request) IQ(org.jivesoftware.smack.packet.IQ) AsyncHttpResponseHandler(com.loopj.android.http.AsyncHttpResponseHandler) OnResponseListener(com.xabber.android.data.connection.OnResponseListener) Slot(com.xabber.xmpp.httpfileupload.Slot) File(java.io.File) NetworkException(com.xabber.android.data.NetworkException) AsyncHttpClient(com.loopj.android.http.AsyncHttpClient)

Example 3 with NetworkException

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

the class ServerInfoManager method onAuthorized.

@Override
public void onAuthorized(ConnectionItem connection) {
    if (connection instanceof AccountItem) {
        String account = ((AccountItem) connection).getAccount();
        if (protocols.get(account) == null) {
            DiscoverInfo packet = new DiscoverInfo();
            packet.setTo(Jid.getServer(account));
            packet.setType(Type.get);
            try {
                ConnectionManager.getInstance().sendRequest(account, packet, this);
            } catch (NetworkException e) {
            }
            return;
        }
    }
    onAvailable(connection);
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) AccountItem(com.xabber.android.data.account.AccountItem) NetworkException(com.xabber.android.data.NetworkException)

Example 4 with NetworkException

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

the class ChatStateManager method updateChatState.

/**
     * Update chat state information and send message if necessary.
     *
     * @param account
     * @param user
     * @param chatState
     */
private void updateChatState(String account, String user, ChatState chatState) {
    if (!SettingsManager.chatsStateNotification() || sent.get(account, user) == chatState)
        return;
    AbstractChat chat = MessageManager.getInstance().getChat(account, user);
    if (chat == null || !isSupported(chat, false))
        return;
    sent.put(chat.getAccount(), chat.getUser(), chatState);
    Message message = new Message();
    message.setType(chat.getType());
    message.setTo(chat.getTo());
    message.addExtension(new ChatStateExtension(chatState));
    try {
        ConnectionManager.getInstance().sendStanza(account, message);
    } catch (NetworkException e) {
    // Just ignore it.
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) ChatStateExtension(org.jivesoftware.smackx.chatstates.packet.ChatStateExtension) AbstractChat(com.xabber.android.data.message.AbstractChat) NetworkException(com.xabber.android.data.NetworkException)

Example 5 with NetworkException

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

the class AbstractChat method sendQueue.

/**
     * Requests to send messages from queue.
     *
     * @param intent can be <code>null</code>.
     */
protected void sendQueue(MessageItem intent) {
    if (!canSendMessage())
        return;
    final ArrayList<MessageItem> sentMessages = new ArrayList<MessageItem>();
    final ArrayList<MessageItem> removeMessages = new ArrayList<MessageItem>();
    for (final MessageItem messageItem : sendQuery) {
        String text = prepareText(messageItem.getText());
        if (text == null) {
            messageItem.markAsError();
            Application.getInstance().runInBackground(new Runnable() {

                @Override
                public void run() {
                    if (messageItem.getId() != null)
                        MessageTable.getInstance().markAsError(messageItem.getId());
                }
            });
        } else {
            Message message = createMessagePacket(text);
            messageItem.setPacketID(message.getPacketID());
            ChatStateManager.getInstance().updateOutgoingMessage(this, message);
            ReceiptManager.getInstance().updateOutgoingMessage(this, message, messageItem);
            CarbonManager.getInstance().updateOutgoingMessage(this, message, messageItem);
            if (messageItem != intent)
                message.addExtension(new DelayInformation(messageItem.getTimestamp()));
            try {
                ConnectionManager.getInstance().sendStanza(account, message);
            } catch (NetworkException e) {
                break;
            }
        }
        if (MessageArchiveManager.getInstance().getSaveMode(account, user, threadId) == SaveMode.fls)
            messageItem.setTag(NO_RECORD_TAG);
        if (messageItem != intent) {
            messageItem.setSentTimeStamp(new Date());
            Collections.sort(messages);
        }
        messageItem.markAsSent();
        if (AccountManager.getInstance().getArchiveMode(messageItem.getChat().getAccount()).saveLocally())
            sentMessages.add(messageItem);
        else
            removeMessages.add(messageItem);
    }
    sendQuery.removeAll(sentMessages);
    sendQuery.removeAll(removeMessages);
    MessageManager.getInstance().onChatChanged(account, user, false);
    Application.getInstance().runInBackground(new Runnable() {

        @Override
        public void run() {
            Collection<Long> sentIds = MessageManager.getMessageIds(sentMessages, false);
            Collection<Long> removeIds = MessageManager.getMessageIds(removeMessages, true);
            MessageTable.getInstance().markAsSent(sentIds);
            MessageTable.getInstance().removeMessages(removeIds);
        }
    });
}
Also used : Message(org.jivesoftware.smack.packet.Message) DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) ArrayList(java.util.ArrayList) Collection(java.util.Collection) NetworkException(com.xabber.android.data.NetworkException) Date(java.util.Date)

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