use of com.xabber.android.data.extension.references.ReferenceElement in project xabber-android by redsolution.
the class AbstractChat method sendMessage.
@SuppressWarnings("WeakerAccess")
boolean sendMessage(MessageItem messageItem) {
String text = prepareText(messageItem.getText());
messageItem.setEncrypted(OTRManager.getInstance().isEncrypted(text));
Long timestamp = messageItem.getTimestamp();
Date currentTime = new Date(System.currentTimeMillis());
Date delayTimestamp = null;
if (timestamp != null) {
if (currentTime.getTime() - timestamp > 60000) {
delayTimestamp = currentTime;
}
}
Message message = null;
if (messageItem.haveAttachments()) {
message = createFileMessagePacket(messageItem.getStanzaId(), messageItem.getAttachments(), text);
} else if (messageItem.haveForwardedMessages()) {
Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
RealmResults<MessageItem> items = realm.where(MessageItem.class).in(MessageItem.Fields.UNIQUE_ID, messageItem.getForwardedIdsAsArray()).findAll();
List<ReferenceElement> references = new ArrayList<>();
StringBuilder builder = new StringBuilder();
if (items != null && !items.isEmpty()) {
for (MessageItem item : items) {
String forward = ClipManager.createMessageTree(realm, item.getUniqueId()) + "\n";
int begin = getSizeOfEncodedChars(builder.toString());
builder.append(forward);
ReferenceElement reference = ReferencesManager.createForwardReference(item, begin, getSizeOfEncodedChars(builder.toString()) - 1);
references.add(reference);
}
}
builder.append(text);
text = builder.toString();
message = createMessagePacket(text, messageItem.getStanzaId());
for (ReferenceElement element : references) {
message.addExtension(element);
}
} else if (text != null) {
message = createMessagePacket(text, messageItem.getStanzaId());
}
if (message != null) {
ChatStateManager.getInstance().updateOutgoingMessage(AbstractChat.this, message);
CarbonManager.getInstance().updateOutgoingMessage(AbstractChat.this, message);
message.addExtension(new OriginIdElement(messageItem.getStanzaId()));
if (delayTimestamp != null) {
message.addExtension(new DelayInformation(delayTimestamp));
}
final String messageId = messageItem.getUniqueId();
try {
StanzaSender.sendStanza(account, message, new StanzaListener() {
@Override
public void processStanza(Stanza packet) throws SmackException.NotConnectedException {
Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
MessageItem acknowledgedMessage = realm.where(MessageItem.class).equalTo(MessageItem.Fields.UNIQUE_ID, messageId).findFirst();
if (acknowledgedMessage != null) {
acknowledgedMessage.setAcknowledged(true);
}
}
});
realm.close();
}
});
} catch (NetworkException e) {
return false;
}
}
if (message == null) {
messageItem.setError(true);
messageItem.setErrorDescription("Internal error: message is null");
} else {
message.setFrom(account.getFullJid());
messageItem.setOriginalStanza(message.toXML().toString());
}
if (delayTimestamp != null) {
messageItem.setDelayTimestamp(delayTimestamp.getTime());
}
if (messageItem.getTimestamp() == null) {
messageItem.setTimestamp(currentTime.getTime());
}
messageItem.setSent(true);
return true;
}
use of com.xabber.android.data.extension.references.ReferenceElement in project xabber-android by redsolution.
the class AbstractChat method createFileMessagePacket.
/**
* Send stanza with data-references.
*/
public Message createFileMessagePacket(String stanzaId, RealmList<Attachment> attachments, String body) {
Message message = new Message();
message.setTo(getTo());
message.setType(getType());
message.setThread(threadId);
if (stanzaId != null)
message.setStanzaId(stanzaId);
StringBuilder builder = new StringBuilder(body);
for (Attachment attachment : attachments) {
StringBuilder rowBuilder = new StringBuilder();
if (builder.length() > 0)
rowBuilder.append("\n");
rowBuilder.append(attachment.getFileUrl());
int begin = getSizeOfEncodedChars(builder.toString());
builder.append(rowBuilder);
ReferenceElement reference = ReferencesManager.createMediaReferences(attachment, begin, getSizeOfEncodedChars(builder.toString()) - 1);
message.addExtension(reference);
}
message.setBody(builder);
return message;
}
Aggregations