Search in sources :

Example 1 with MessageItem

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

the class ChatMessageAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    final int viewType = getItemViewType(position);
    MessageItem messageItem = getMessageItem(position);
    switch(viewType) {
        case VIEW_TYPE_HINT:
            ((BasicMessage) holder).messageText.setText(hint);
            break;
        case VIEW_TYPE_ACTION_MESSAGE:
            ChatAction action = messageItem.getAction();
            String time = StringUtils.getSmartTimeText(context, messageItem.getTimestamp());
            String name;
            if (isMUC) {
                name = messageItem.getResource();
            } else {
                name = RosterManager.getInstance().getBestContact(account, messageItem.getChat().getUser()).getName();
            }
            ((BasicMessage) holder).messageText.setText(time + ": " + action.getText(context, name, messageItem.getSpannable().toString()));
            break;
        case VIEW_TYPE_INCOMING_MESSAGE:
            setUpIncomingMessage((IncomingMessage) holder, messageItem);
            break;
        case VIEW_TYPE_OUTGOING_MESSAGE:
            setUpOutgoingMessage((Message) holder, messageItem);
            break;
    }
}
Also used : MessageItem(com.xabber.android.data.message.MessageItem) ChatAction(com.xabber.android.data.message.ChatAction)

Example 2 with MessageItem

use of com.xabber.android.data.message.MessageItem 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 MessageItem

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

the class RoomChat method onPacket.

@Override
protected boolean onPacket(String bareAddress, Stanza packet) {
    if (!super.onPacket(bareAddress, packet)) {
        return false;
    }
    MUCUser mucUserExtension = MUC.getMUCUserExtension(packet);
    if (mucUserExtension != null && mucUserExtension.getInvite() != null) {
        return false;
    }
    final String from = packet.getFrom();
    final String resource = XmppStringUtils.parseResource(from);
    if (packet instanceof Message) {
        final Message message = (Message) packet;
        if (message.getType() == Message.Type.error) {
            String invite = invites.remove(message.getPacketID());
            if (invite != null) {
                newAction(nickname, invite, ChatAction.invite_error);
            }
            return true;
        }
        MUCUser mucUser = MUC.getMUCUserExtension(packet);
        if (mucUser != null && mucUser.getDecline() != null) {
            onInvitationDeclined(mucUser.getDecline().getFrom(), mucUser.getDecline().getReason());
            return true;
        }
        if (mucUser != null && mucUser.getStatus() != null && mucUser.getStatus().contains(MUCUser.Status.create("100")) && ChatManager.getInstance().isSuppress100(account, user)) {
            // 'This room is not anonymous'
            return true;
        }
        final String text = message.getBody();
        final String subject = message.getSubject();
        if (text == null && subject == null) {
            return true;
        }
        if (subject != null) {
            if (this.subject.equals(subject)) {
                return true;
            }
            this.subject = subject;
            RosterManager.getInstance().onContactChanged(account, bareAddress);
            newAction(resource, subject, ChatAction.subject);
        } else {
            boolean notify = true;
            String packetID = message.getPacketID();
            Date delay = Delay.getDelay(message);
            if (delay != null) {
                notify = false;
            }
            for (MessageItem messageItem : messages) {
                // Search for duplicates
                if (packetID != null && packetID.equals(messageItem.getPacketID())) {
                    // Server send our own message back
                    messageItem.markAsDelivered();
                    RosterManager.getInstance().onContactChanged(account, user);
                    return true;
                }
                if (delay != null) {
                    if (delay.equals(messageItem.getDelayTimestamp()) && resource.equals(messageItem.getResource()) && text.equals(messageItem.getText())) {
                        return true;
                    }
                }
            }
            if (isSelf(resource)) {
                // Own message from other client
                notify = false;
            }
            updateThreadId(message.getThread());
            MessageItem messageItem = newMessage(resource, text, null, delay, true, notify, false, false, true);
            messageItem.setPacketID(packetID);
        }
    } else if (packet instanceof Presence) {
        String stringPrep = Jid.getStringPrep(resource);
        Presence presence = (Presence) packet;
        if (presence.getType() == Presence.Type.available) {
            Occupant oldOccupant = occupants.get(stringPrep);
            Occupant newOccupant = createOccupant(resource, presence);
            occupants.put(stringPrep, newOccupant);
            if (oldOccupant == null) {
                onAvailable(resource);
                RosterManager.getInstance().onContactChanged(account, user);
            } else {
                boolean changed = false;
                if (oldOccupant.getAffiliation() != newOccupant.getAffiliation()) {
                    changed = true;
                    onAffiliationChanged(resource, newOccupant.getAffiliation());
                }
                if (oldOccupant.getRole() != newOccupant.getRole()) {
                    changed = true;
                    onRoleChanged(resource, newOccupant.getRole());
                }
                if (oldOccupant.getStatusMode() != newOccupant.getStatusMode() || !oldOccupant.getStatusText().equals(newOccupant.getStatusText())) {
                    changed = true;
                    onStatusChanged(resource, newOccupant.getStatusMode(), newOccupant.getStatusText());
                }
                if (changed) {
                    RosterManager.getInstance().onContactChanged(account, user);
                }
            }
        } else if (presence.getType() == Presence.Type.unavailable && state == RoomState.available) {
            occupants.remove(stringPrep);
            MUCUser mucUser = MUC.getMUCUserExtension(presence);
            if (mucUser != null && mucUser.getStatus() != null) {
                if (mucUser.getStatus().contains(MUCUser.Status.KICKED_307)) {
                    onKick(resource, mucUser.getItem().getActor());
                } else if (mucUser.getStatus().contains(MUCUser.Status.BANNED_301)) {
                    onBan(resource, mucUser.getItem().getActor());
                } else if (mucUser.getStatus().contains(MUCUser.Status.NEW_NICKNAME_303)) {
                    String newNick = mucUser.getItem().getNick();
                    if (newNick == null) {
                        return true;
                    }
                    onRename(resource, newNick);
                    Occupant occupant = createOccupant(newNick, presence);
                    occupants.put(Jid.getStringPrep(newNick), occupant);
                } else if (mucUser.getStatus().contains(MUCUser.Status.REMOVED_AFFIL_CHANGE_321)) {
                    onRevoke(resource, mucUser.getItem().getActor());
                }
            } else {
                onLeave(resource);
            }
            RosterManager.getInstance().onContactChanged(account, user);
        }
    }
    return true;
}
Also used : MessageItem(com.xabber.android.data.message.MessageItem) MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence) Date(java.util.Date)

Example 4 with MessageItem

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

the class ChatStorage method addItem.

public void addItem(AbstractChat abstractChat, Chat chat, AbstractMessage message, long offset) {
    boolean incoming = message instanceof From;
    if (message.getUtc() == null)
        timestamp = new Date(timestamp.getTime() + message.getSecs() * 1000);
    else
        timestamp = message.getUtc();
    String body = message.getBody();
    net.java.otr4j.io.messages.AbstractMessage otrMessage;
    try {
        otrMessage = SerializationUtils.toMessage(body);
    } catch (IOException e) {
        return;
    }
    if (otrMessage != null) {
        if (otrMessage.messageType != net.java.otr4j.io.messages.AbstractMessage.MESSAGE_PLAINTEXT)
            return;
        body = ((PlainTextMessage) otrMessage).cleanText;
    }
    MessageItem messageItem = new MessageItem(abstractChat, chat.getStartString(), Jid.getResource(chat.getWith()), body, null, new Date(timestamp.getTime() - offset), null, incoming, true, true, false, true, false, false);
    items.add(messageItem);
}
Also used : MessageItem(com.xabber.android.data.message.MessageItem) From(com.xabber.xmpp.archive.From) IOException(java.io.IOException) Date(java.util.Date)

Example 5 with MessageItem

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

the class MessageArchiveManager method apply.

/**
     * Apply received messages.
     *
     * @param account
     * @param bareAddress
     * @param tag
     * @param chatStorage
     * @param historyStorage
     * @return Whether enough messages have been received.
     */
private boolean apply(String account, String bareAddress, String tag, ChatStorage chatStorage, HistoryStorage historyStorage) {
    AbstractChat abstractChat = MessageManager.getInstance().getChat(account, bareAddress);
    int newCount = abstractChat.onMessageDownloaded(tag, chatStorage.getItems(), false);
    int incomingCount = 0;
    for (MessageItem messageItem : chatStorage.getItems()) if (messageItem.isIncoming())
        incomingCount += 1;
    chatStorage.onApplied();
    if (historyStorage.enoughMessages(newCount, incomingCount)) {
        historyStorage.onSuccess();
        return true;
    }
    return false;
}
Also used : MessageItem(com.xabber.android.data.message.MessageItem) AbstractChat(com.xabber.android.data.message.AbstractChat)

Aggregations

MessageItem (com.xabber.android.data.message.MessageItem)5 Date (java.util.Date)2 AsyncHttpClient (com.loopj.android.http.AsyncHttpClient)1 AsyncHttpResponseHandler (com.loopj.android.http.AsyncHttpResponseHandler)1 NetworkException (com.xabber.android.data.NetworkException)1 OnResponseListener (com.xabber.android.data.connection.OnResponseListener)1 AbstractChat (com.xabber.android.data.message.AbstractChat)1 ChatAction (com.xabber.android.data.message.ChatAction)1 From (com.xabber.xmpp.archive.From)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 IOException (java.io.IOException)1 IQ (org.jivesoftware.smack.packet.IQ)1 Message (org.jivesoftware.smack.packet.Message)1 Presence (org.jivesoftware.smack.packet.Presence)1 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)1