Search in sources :

Example 1 with OnResponseListener

use of com.xabber.android.data.connection.OnResponseListener 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) {
    }
}
Also used : Retrieve(com.xabber.xmpp.archive.Retrieve) Set(com.xabber.xmpp.rsm.Set) OnResponseListener(com.xabber.android.data.connection.OnResponseListener) IQ(org.jivesoftware.smack.packet.IQ) AbstractChat(com.xabber.android.data.message.AbstractChat) Chat(com.xabber.xmpp.archive.Chat) NetworkException(com.xabber.android.data.NetworkException)

Example 2 with OnResponseListener

use of com.xabber.android.data.connection.OnResponseListener 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 OnResponseListener

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

the class BlockingManager method requestBlockList.

public void requestBlockList(String account) {
    if (!isSupported(account)) {
        return;
    }
    final BlockList blockListRequest = new BlockList();
    blockListRequest.setType(IQ.Type.get);
    try {
        ConnectionManager.getInstance().sendRequest(account, blockListRequest, new OnResponseListener() {

            @Override
            public void onReceived(String account, String packetId, IQ iq) {
                if (!blockListRequest.getStanzaId().equals(packetId) || !(iq instanceof BlockList)) {
                    return;
                }
                if (iq.getType() == IQ.Type.result) {
                    blockListsForAccounts.put(account, ((BlockList) iq).getItems());
                    for (OnBlockedListChangedListener onBlockedListChangedListener : Application.getInstance().getUIListeners(OnBlockedListChangedListener.class)) {
                        onBlockedListChangedListener.onBlockedListChanged(account);
                    }
                    for (OnContactChangedListener onContactChangedListener : Application.getInstance().getUIListeners(OnContactChangedListener.class)) {
                        onContactChangedListener.onContactsChanged(new ArrayList<BaseEntity>());
                    }
                }
            }

            @Override
            public void onError(String account, String packetId, IQ iq) {
            }

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

            @Override
            public void onDisconnect(String account, String packetId) {
            }
        });
    } catch (NetworkException e) {
        e.printStackTrace();
    }
}
Also used : OnResponseListener(com.xabber.android.data.connection.OnResponseListener) BlockList(com.xabber.xmpp.blocking.BlockList) IQ(org.jivesoftware.smack.packet.IQ) ArrayList(java.util.ArrayList) OnContactChangedListener(com.xabber.android.data.roster.OnContactChangedListener) NetworkException(com.xabber.android.data.NetworkException)

Example 4 with OnResponseListener

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

the class BlockingManager method blockContact.

public void blockContact(String account, final String contactJid, final BlockContactListener listener) {
    final Block blockRequest = new Block();
    blockRequest.setType(IQ.Type.set);
    blockRequest.addItem(contactJid);
    try {
        ConnectionManager.getInstance().sendRequest(account, blockRequest, new OnResponseListener() {

            @Override
            public void onReceived(String account, String packetId, IQ iq) {
                if (!blockRequest.getStanzaId().equals(packetId)) {
                    return;
                }
                if (iq.getType() == IQ.Type.result) {
                    requestBlockList(account);
                    listener.onSuccess();
                } else {
                    listener.onError();
                }
            }

            @Override
            public void onError(String account, String packetId, IQ iq) {
                listener.onError();
            }

            @Override
            public void onTimeout(String account, String packetId) {
                listener.onError();
            }

            @Override
            public void onDisconnect(String account, String packetId) {
                listener.onError();
            }
        });
    } catch (NetworkException e) {
        e.printStackTrace();
        listener.onError();
    }
}
Also used : OnResponseListener(com.xabber.android.data.connection.OnResponseListener) IQ(org.jivesoftware.smack.packet.IQ) Block(com.xabber.xmpp.blocking.Block) NetworkException(com.xabber.android.data.NetworkException)

Example 5 with OnResponseListener

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

the class MessageArchiveManager method requestModified.

private void requestModified(String account, String before) {
    Modified packet = new Modified();
    packet.setType(Type.get);
    Set rsm = new Set();
    rsm.setMax(RSM_MAX);
    rsm.setBefore(before);
    packet.setRsm(rsm);
    packet.setStart(modificationStorages.get(account).getLastRequest());
    try {
        ConnectionManager.getInstance().sendRequest(account, packet, new OnResponseListener() {

            @Override
            public void onReceived(String account, String packetId, IQ iq) {
                if (iq instanceof Modified && ((Modified) iq).isValid())
                    onModifiedReceived(account, (Modified) 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) {
    }
}
Also used : Modified(com.xabber.xmpp.archive.Modified) Set(com.xabber.xmpp.rsm.Set) OnResponseListener(com.xabber.android.data.connection.OnResponseListener) IQ(org.jivesoftware.smack.packet.IQ) NetworkException(com.xabber.android.data.NetworkException)

Aggregations

NetworkException (com.xabber.android.data.NetworkException)6 OnResponseListener (com.xabber.android.data.connection.OnResponseListener)6 IQ (org.jivesoftware.smack.packet.IQ)6 Set (com.xabber.xmpp.rsm.Set)2 AsyncHttpClient (com.loopj.android.http.AsyncHttpClient)1 AsyncHttpResponseHandler (com.loopj.android.http.AsyncHttpResponseHandler)1 AbstractChat (com.xabber.android.data.message.AbstractChat)1 MessageItem (com.xabber.android.data.message.MessageItem)1 OnContactChangedListener (com.xabber.android.data.roster.OnContactChangedListener)1 Chat (com.xabber.xmpp.archive.Chat)1 Modified (com.xabber.xmpp.archive.Modified)1 Pref (com.xabber.xmpp.archive.Pref)1 Retrieve (com.xabber.xmpp.archive.Retrieve)1 Block (com.xabber.xmpp.blocking.Block)1 BlockList (com.xabber.xmpp.blocking.BlockList)1 Request (com.xabber.xmpp.httpfileupload.Request)1 Slot (com.xabber.xmpp.httpfileupload.Slot)1 FileEntity (cz.msebera.android.httpclient.entity.FileEntity)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1