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);
}
}
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();
}
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> (");
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;
}
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);
}
}
}
}
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);
}
}
Aggregations