Search in sources :

Example 36 with NetworkException

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

the class VCardManager method onVCardReceived.

private void onVCardReceived(final String account, final String bareAddress, final VCard vCard) {
    final StructuredName name;
    if (vCard.getType() == Type.error) {
        onVCardFailed(account, bareAddress);
        if (names.containsKey(bareAddress)) {
            return;
        }
        name = EMPTY_STRUCTURED_NAME;
    } else {
        for (OnVCardListener listener : Application.getInstance().getUIListeners(OnVCardListener.class)) {
            listener.onVCardReceived(account, bareAddress, vCard);
        }
        String hash = vCard.getAvatarHash();
        AvatarManager.getInstance().onAvatarReceived(bareAddress, hash, vCard.getAvatar());
        name = new StructuredName(vCard.getNickName(), vCard.getField(VCardProperty.FN.name()), vCard.getFirstName(), vCard.getMiddleName(), vCard.getLastName());
        Application.getInstance().runInBackground(new Runnable() {

            @Override
            public void run() {
                try {
                    if (Jid.getBareAddress(account).equals(bareAddress)) {
                        PresenceManager.getInstance().resendPresence(account);
                    }
                } catch (NetworkException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    names.put(bareAddress, name);
    for (RosterContact rosterContact : RosterManager.getInstance().getContacts()) {
        if (rosterContact.getUser().equals(bareAddress)) {
            for (OnRosterChangedListener listener : Application.getInstance().getManagers(OnRosterChangedListener.class)) {
                listener.onContactStructuredInfoChanged(rosterContact, name);
            }
        }
    }
    Application.getInstance().runInBackground(new Runnable() {

        @Override
        public void run() {
            VCardTable.getInstance().write(bareAddress, name);
        }
    });
    if (vCard.getFrom() == null) {
        // account it self
        AccountManager.getInstance().onAccountChanged(account);
    } else {
        RosterManager.getInstance().onContactChanged(account, bareAddress);
    }
}
Also used : OnRosterChangedListener(com.xabber.android.data.roster.OnRosterChangedListener) RosterContact(com.xabber.android.data.roster.RosterContact) StructuredName(com.xabber.android.data.roster.StructuredName) NetworkException(com.xabber.android.data.NetworkException)

Example 37 with NetworkException

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

the class VCardManager method saveVCard.

public void saveVCard(final String account, final VCard vCard) {
    ConnectionThread connectionThread = AccountManager.getInstance().getAccount(account).getConnectionThread();
    if (connectionThread == null) {
        onVCardSaveFailed(account);
        return;
    }
    final AbstractXMPPConnection xmppConnection = connectionThread.getXMPPConnection();
    final org.jivesoftware.smackx.vcardtemp.VCardManager vCardManager = org.jivesoftware.smackx.vcardtemp.VCardManager.getInstanceFor(xmppConnection);
    final Thread thread = new Thread("Save vCard for account " + account) {

        @Override
        public void run() {
            boolean isSuccess = true;
            xmppConnection.setPacketReplyTimeout(120000);
            vCardSaveRequests.add(account);
            try {
                vCardManager.saveVCard(vCard);
                String avatarHash = vCard.getAvatarHash();
                if (avatarHash == null) {
                    avatarHash = AvatarManager.EMPTY_HASH;
                }
                PresenceManager.getInstance().sendVCardUpdatePresence(account, avatarHash);
            } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | NetworkException e) {
                LogManager.w(this, "Error saving vCard: " + e.getMessage());
                isSuccess = false;
            }
            vCardSaveRequests.remove(account);
            xmppConnection.setPacketReplyTimeout(ConnectionManager.PACKET_REPLY_TIMEOUT);
            final boolean finalIsSuccess = isSuccess;
            Application.getInstance().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (finalIsSuccess) {
                        onVCardSaveSuccess(account);
                    } else {
                        onVCardSaveFailed(account);
                    }
                }
            });
        }
    };
    thread.start();
}
Also used : ConnectionThread(com.xabber.android.data.connection.ConnectionThread) ConnectionThread(com.xabber.android.data.connection.ConnectionThread) NetworkException(com.xabber.android.data.NetworkException) AbstractXMPPConnection(org.jivesoftware.smack.AbstractXMPPConnection)

Example 38 with NetworkException

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

the class MessageManager method exportChat.

/**
     * Export chat to file with specified name.
     *
     * @param account
     * @param user
     * @param fileName
     * @throws NetworkException
     */
public File exportChat(String account, String user, String fileName) throws NetworkException {
    final File file = new File(Environment.getExternalStorageDirectory(), fileName);
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        final String titleName = RosterManager.getInstance().getName(account, user) + " (" + user + ")";
        out.write("<html><head><title>");
        out.write(StringUtils.escapeHtml(titleName));
        out.write("</title></head><body>");
        final AbstractChat abstractChat = getChat(account, user);
        if (abstractChat != null) {
            final boolean isMUC = abstractChat instanceof RoomChat;
            final String accountName = AccountManager.getInstance().getNickName(account);
            final String userName = RosterManager.getInstance().getName(account, user);
            for (MessageItem messageItem : abstractChat.getMessages()) {
                if (messageItem.getAction() != null) {
                    continue;
                }
                final String name;
                if (isMUC) {
                    name = messageItem.getResource();
                } else {
                    if (messageItem.isIncoming()) {
                        name = userName;
                    } else {
                        name = accountName;
                    }
                }
                out.write("<b>");
                out.write(StringUtils.escapeHtml(name));
                out.write("</b>&nbsp;(");
                out.write(StringUtils.getDateTimeText(messageItem.getTimestamp()));
                out.write(")<br />\n<p>");
                out.write(StringUtils.escapeHtml(messageItem.getText()));
                out.write("</p><hr />\n");
            }
        }
        out.write("</body></html>");
        out.close();
    } catch (IOException e) {
        throw new NetworkException(R.string.FILE_NOT_FOUND);
    }
    return file;
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) RoomChat(com.xabber.android.data.extension.muc.RoomChat) NetworkException(com.xabber.android.data.NetworkException) BufferedWriter(java.io.BufferedWriter)

Example 39 with NetworkException

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

the class ReceiptManager method onPacket.

@Override
public void onPacket(ConnectionItem connection, String bareAddress, Stanza packet) {
    if (!(connection instanceof AccountItem))
        return;
    String account = ((AccountItem) connection).getAccount();
    final String user = packet.getFrom();
    if (user == null)
        return;
    if (!(packet instanceof Message))
        return;
    final Message message = (Message) packet;
    if (message.getType() == Message.Type.error) {
        final MessageItem messageItem = sent.remove(account, message.getPacketID());
        if (messageItem != null && !messageItem.isError()) {
            messageItem.markAsError();
            Application.getInstance().runInBackground(new Runnable() {

                @Override
                public void run() {
                    if (messageItem.getId() != null)
                        MessageTable.getInstance().markAsError(messageItem.getId());
                }
            });
            MessageManager.getInstance().onChatChanged(messageItem.getChat().getAccount(), messageItem.getChat().getUser(), false);
        }
    } else {
        // TODO setDefaultAutoReceiptMode should be used
        for (ExtensionElement packetExtension : message.getExtensions()) if (packetExtension instanceof DeliveryReceiptRequest) {
            String id = message.getPacketID();
            if (id == null)
                continue;
            Message receipt = new Message(user);
            receipt.addExtension(new DeliveryReceipt(id));
            // the key problem is Thread - smack does not keep it in auto reply
            receipt.setThread(message.getThread());
            try {
                ConnectionManager.getInstance().sendStanza(account, receipt);
            } catch (NetworkException e) {
                LogManager.exception(this, e);
            }
        }
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) AccountItem(com.xabber.android.data.account.AccountItem) DeliveryReceiptRequest(org.jivesoftware.smackx.receipts.DeliveryReceiptRequest) DeliveryReceipt(org.jivesoftware.smackx.receipts.DeliveryReceipt) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) NetworkException(com.xabber.android.data.NetworkException)

Example 40 with NetworkException

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

the class OTRManager method initSmp.

/**
     * Initiate request using SM protocol.
     */
public void initSmp(String account, String user, String question, String secret) throws NetworkException {
    LogManager.i(this, "initializing smp... " + user);
    removeSMRequest(account, user);
    addSMProgress(account, user);
    try {
        getOrCreateSession(account, user).initSmp(question, secret);
    } catch (OtrException e) {
        throw new NetworkException(R.string.OTR_ERROR, e);
    }
}
Also used : OtrException(net.java.otr4j.OtrException) NetworkException(com.xabber.android.data.NetworkException)

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