use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.
the class PresenceParser method parseContactPresence.
public void parseContactPresence(final PresencePacket packet, final Account account) {
final PresenceGenerator mPresenceGenerator = mXmppConnectionService.getPresenceGenerator();
final Jid from = packet.getFrom();
if (from == null || from.equals(account.getJid())) {
return;
}
final String type = packet.getAttribute("type");
final Contact contact = account.getRoster().getContact(from);
if (type == null) {
final String resource = from.isBareJid() ? "" : from.getResourcepart();
if (contact.setPresenceName(packet.findChildContent("nick", Namespace.NICK))) {
mXmppConnectionService.getAvatarService().clear(contact);
}
Avatar avatar = Avatar.parsePresence(packet.findChild("x", "vcard-temp:x:update"));
if (avatar != null && (!contact.isSelf() || account.getAvatar() == null)) {
avatar.owner = from.toBareJid();
if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
if (avatar.owner.equals(account.getJid().toBareJid())) {
account.setAvatar(avatar.getFilename());
mXmppConnectionService.databaseBackend.updateAccount(account);
mXmppConnectionService.getAvatarService().clear(account);
mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateAccountUi();
} else if (contact.setAvatar(avatar)) {
mXmppConnectionService.getAvatarService().clear(contact);
mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateRosterUi();
}
} else if (mXmppConnectionService.isDataSaverDisabled()) {
mXmppConnectionService.fetchAvatar(account, avatar);
}
}
int sizeBefore = contact.getPresences().size();
final String show = packet.findChildContent("show");
final Element caps = packet.findChild("c", "http://jabber.org/protocol/caps");
final String message = packet.findChildContent("status");
final Presence presence = Presence.parse(show, caps, message);
contact.updatePresence(resource, presence);
if (presence.hasCaps()) {
mXmppConnectionService.fetchCaps(account, from, presence);
}
final Element idle = packet.findChild("idle", Namespace.IDLE);
if (idle != null) {
try {
final String since = idle.getAttribute("since");
contact.setLastseen(AbstractParser.parseTimestamp(since));
contact.flagInactive();
} catch (Throwable throwable) {
if (contact.setLastseen(AbstractParser.parseTimestamp(packet))) {
contact.flagActive();
}
}
} else {
if (contact.setLastseen(AbstractParser.parseTimestamp(packet))) {
contact.flagActive();
}
}
PgpEngine pgp = mXmppConnectionService.getPgpEngine();
Element x = packet.findChild("x", "jabber:x:signed");
if (pgp != null && x != null) {
Element status = packet.findChild("status");
String msg = status != null ? status.getContent() : "";
contact.setPgpKeyId(pgp.fetchKeyId(account, msg, x.getContent()));
}
boolean online = sizeBefore < contact.getPresences().size();
mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, online);
} else if (type.equals("unavailable")) {
if (contact.setLastseen(AbstractParser.parseTimestamp(packet, 0L, true))) {
contact.flagInactive();
}
if (from.isBareJid()) {
contact.clearPresences();
} else {
contact.removePresence(from.getResourcepart());
}
mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, false);
} else if (type.equals("subscribe")) {
if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
mXmppConnectionService.sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
} else {
contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, contact.getJid().toBareJid(), false, false);
final String statusMessage = packet.findChildContent("status");
if (statusMessage != null && !statusMessage.isEmpty() && conversation.countMessages() == 0) {
conversation.add(new Message(conversation, statusMessage, Message.ENCRYPTION_NONE, Message.STATUS_RECEIVED));
}
}
}
mXmppConnectionService.updateRosterUi();
}
use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.
the class JingleConnection method init.
public void init(Account account, JinglePacket packet) {
this.mJingleStatus = JINGLE_STATUS_INITIATED;
Conversation conversation = this.mXmppConnectionService.findOrCreateConversation(account, packet.getFrom().toBareJid(), false, true);
this.message = new Message(conversation, "", Message.ENCRYPTION_NONE);
this.message.setStatus(Message.STATUS_RECEIVED);
this.mStatus = Transferable.STATUS_OFFER;
this.message.setTransferable(this);
final Jid from = packet.getFrom();
this.message.setCounterpart(from);
this.account = account;
this.initiator = packet.getFrom();
this.responder = this.account.getJid();
this.sessionId = packet.getSessionId();
Content content = packet.getJingleContent();
this.contentCreator = content.getAttribute("creator");
this.contentName = content.getAttribute("name");
this.transportId = content.getTransportId();
this.mergeCandidates(JingleCandidate.parse(content.socks5transport().getChildren()));
this.ftVersion = content.getVersion();
if (ftVersion == null) {
this.sendCancel();
this.fail();
return;
}
this.fileOffer = content.getFileOffer(this.ftVersion);
mXmppConnectionService.sendIqPacket(account, packet.generateResponse(IqPacket.TYPE.RESULT), null);
if (fileOffer != null) {
Element encrypted = fileOffer.findChild("encrypted", AxolotlService.PEP_PREFIX);
if (encrypted != null) {
this.mXmppAxolotlMessage = XmppAxolotlMessage.fromElement(encrypted, packet.getFrom().toBareJid());
}
Element fileSize = fileOffer.findChild("size");
Element fileNameElement = fileOffer.findChild("name");
if (fileNameElement != null) {
String[] filename = fileNameElement.getContent().toLowerCase(Locale.US).toLowerCase().split("\\.");
String filename_new = fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4);
String extension = filename[filename.length - 1];
if (VALID_IMAGE_EXTENSIONS.contains(extension)) {
message.setType(Message.TYPE_IMAGE);
message.setRelativeFilePath(filename_new + "." + extension);
} else if (VALID_CRYPTO_EXTENSIONS.contains(filename[filename.length - 1])) {
if (filename.length == 3) {
extension = filename[filename.length - 2];
if (VALID_IMAGE_EXTENSIONS.contains(extension)) {
message.setType(Message.TYPE_IMAGE);
message.setRelativeFilePath(filename_new + "." + extension);
} else {
message.setType(Message.TYPE_FILE);
}
if (filename[filename.length - 1].equals("otr")) {
message.setEncryption(Message.ENCRYPTION_OTR);
} else {
message.setEncryption(Message.ENCRYPTION_PGP);
}
}
} else {
message.setType(Message.TYPE_FILE);
}
if (message.getType() == Message.TYPE_FILE) {
String suffix = "";
if (!fileNameElement.getContent().isEmpty()) {
String[] parts = fileNameElement.getContent().split("/");
suffix = parts[parts.length - 1];
if (message.getEncryption() == Message.ENCRYPTION_OTR && suffix.endsWith(".otr")) {
suffix = suffix.substring(0, suffix.length() - 4);
} else if (message.getEncryption() == Message.ENCRYPTION_PGP && (suffix.endsWith(".pgp") || suffix.endsWith(".gpg"))) {
suffix = suffix.substring(0, suffix.length() - 4);
}
}
message.setRelativeFilePath(filename_new + "_" + suffix);
}
long size = Long.parseLong(fileSize.getContent());
message.setBody(Long.toString(size));
conversation.add(message);
mJingleConnectionManager.updateConversationUi(true);
if (mJingleConnectionManager.hasStoragePermission() && size < this.mJingleConnectionManager.getAutoAcceptFileSize() && mXmppConnectionService.isDataSaverDisabled()) {
Log.d(Config.LOGTAG, "auto accepting file from " + packet.getFrom());
this.acceptedAutomatically = true;
this.sendAccept();
} else {
message.markUnread();
Log.d(Config.LOGTAG, "not auto accepting new file offer with size: " + size + " allowed size:" + this.mJingleConnectionManager.getAutoAcceptFileSize());
this.mXmppConnectionService.getNotificationService().push(message);
}
this.file = this.mXmppConnectionService.getFileBackend().getFile(message, false);
if (mXmppAxolotlMessage != null) {
XmppAxolotlMessage.XmppAxolotlKeyTransportMessage transportMessage = account.getAxolotlService().processReceivingKeyTransportMessage(mXmppAxolotlMessage, false);
if (transportMessage != null) {
message.setEncryption(Message.ENCRYPTION_AXOLOTL);
this.file.setKey(transportMessage.getKey());
this.file.setIv(transportMessage.getIv());
message.setFingerprint(transportMessage.getFingerprint());
} else {
Log.d(Config.LOGTAG, "could not process KeyTransportMessage");
}
} else if (message.getEncryption() == Message.ENCRYPTION_OTR) {
byte[] key = conversation.getSymmetricKey();
if (key == null) {
this.sendCancel();
this.fail();
return;
} else {
this.file.setKeyAndIv(key);
}
}
this.file.setExpectedSize(size);
message.resetFileParams();
Log.d(Config.LOGTAG, "receiving file: expecting size of " + this.file.getExpectedSize());
} else {
this.sendCancel();
this.fail();
}
} else {
this.sendCancel();
this.fail();
}
}
use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.
the class NotificationService method modifyForTextOnly.
private void modifyForTextOnly(final Builder builder, final UnreadConversation.Builder uBuilder, final ArrayList<Message> messages) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(mXmppConnectionService.getString(R.string.me));
Conversation conversation = messages.get(0).getConversation();
if (conversation.getMode() == Conversation.MODE_MULTI) {
messagingStyle.setConversationTitle(conversation.getName());
}
for (Message message : messages) {
String sender = message.getStatus() == Message.STATUS_RECEIVED ? UIHelper.getMessageDisplayName(message) : null;
messagingStyle.addMessage(UIHelper.getMessagePreview(mXmppConnectionService, message).first, message.getTimeSent(), sender);
}
builder.setStyle(messagingStyle);
} else {
if (messages.get(0).getConversation().getMode() == Conversation.MODE_SINGLE) {
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(getMergedBodies(messages)));
builder.setContentText(UIHelper.getMessagePreview(mXmppConnectionService, messages.get(0)).first);
} else {
final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
SpannableString styledString;
for (Message message : messages) {
final String name = UIHelper.getMessageDisplayName(message);
styledString = new SpannableString(name + ": " + message.getBody());
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
style.addLine(styledString);
}
builder.setStyle(style);
int count = messages.size();
if (count == 1) {
final String name = UIHelper.getMessageDisplayName(messages.get(0));
styledString = new SpannableString(name + ": " + messages.get(0).getBody());
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
builder.setContentText(styledString);
} else {
builder.setContentText(mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count));
}
}
}
/**
* message preview for Android Auto *
*/
for (Message message : messages) {
Pair<String, Boolean> preview = UIHelper.getMessagePreview(mXmppConnectionService, message);
// only show user written text
if (!preview.second) {
uBuilder.addMessage(preview.first);
uBuilder.setLatestTimestamp(message.getTimeSent());
}
}
}
use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.
the class NotificationService method buildMultipleConversation.
private Builder buildMultipleConversation() {
final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
style.setBigContentTitle(notifications.size() + " " + mXmppConnectionService.getString(R.string.unread_conversations));
final StringBuilder names = new StringBuilder();
Conversation conversation = null;
for (final ArrayList<Message> messages : notifications.values()) {
if (messages.size() > 0) {
conversation = messages.get(0).getConversation();
final String name = conversation.getName();
SpannableString styledString;
if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) {
int count = messages.size();
styledString = new SpannableString(name + ": " + mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count));
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
style.addLine(styledString);
} else {
styledString = new SpannableString(name + ": " + UIHelper.getMessagePreview(mXmppConnectionService, messages.get(0)).first);
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
style.addLine(styledString);
}
names.append(name);
names.append(", ");
}
}
if (names.length() >= 2) {
names.delete(names.length() - 2, names.length());
}
mBuilder.setContentTitle(notifications.size() + " " + mXmppConnectionService.getString(R.string.unread_conversations));
mBuilder.setContentText(names.toString());
mBuilder.setStyle(style);
if (conversation != null) {
mBuilder.setContentIntent(createContentIntent(conversation));
}
mBuilder.setGroupSummary(true);
mBuilder.setGroup(CONVERSATIONS_GROUP);
mBuilder.setDeleteIntent(createDeleteIntent(null));
mBuilder.setSmallIcon(R.drawable.ic_notification);
return mBuilder;
}
use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.
the class XmppConnectionService method sendMessage.
private void sendMessage(final Message message, final boolean resend, final boolean delay) {
final Account account = message.getConversation().getAccount();
if (account.setShowErrorNotification(true)) {
databaseBackend.updateAccount(account);
mNotificationService.updateErrorNotification();
}
final Conversation conversation = message.getConversation();
account.deactivateGracePeriod();
MessagePacket packet = null;
final boolean addToConversation = (conversation.getMode() != Conversation.MODE_MULTI || !Patches.BAD_MUC_REFLECTION.contains(account.getServerIdentity())) && !message.edited();
boolean saveInDb = addToConversation;
message.setStatus(Message.STATUS_WAITING);
if (!resend && message.getEncryption() != Message.ENCRYPTION_OTR) {
message.getConversation().endOtrIfNeeded();
message.getConversation().findUnsentMessagesWithEncryption(Message.ENCRYPTION_OTR, new Conversation.OnMessageFound() {
@Override
public void onMessageFound(Message message) {
markMessage(message, Message.STATUS_SEND_FAILED);
}
});
}
if (account.isOnlineAndConnected()) {
switch(message.getEncryption()) {
case Message.ENCRYPTION_NONE:
if (message.needsUploading()) {
if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize()) || conversation.getMode() == Conversation.MODE_MULTI || message.fixCounterpart()) {
this.sendFileMessage(message, delay);
} else {
break;
}
} else {
packet = mMessageGenerator.generateChat(message);
}
break;
case Message.ENCRYPTION_PGP:
case Message.ENCRYPTION_DECRYPTED:
if (message.needsUploading()) {
if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize()) || conversation.getMode() == Conversation.MODE_MULTI || message.fixCounterpart()) {
this.sendFileMessage(message, delay);
} else {
break;
}
} else {
packet = mMessageGenerator.generatePgpChat(message);
}
break;
case Message.ENCRYPTION_OTR:
SessionImpl otrSession = conversation.getOtrSession();
if (otrSession != null && otrSession.getSessionStatus() == SessionStatus.ENCRYPTED) {
try {
message.setCounterpart(Jid.fromSessionID(otrSession.getSessionID()));
} catch (InvalidJidException e) {
break;
}
if (message.needsUploading()) {
mJingleConnectionManager.createNewConnection(message);
} else {
packet = mMessageGenerator.generateOtrChat(message);
}
} else if (otrSession == null) {
if (message.fixCounterpart()) {
conversation.startOtrSession(message.getCounterpart().getResourcepart(), true);
} else {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not fix counterpart for OTR message to contact " + message.getCounterpart());
break;
}
} else {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + " OTR session with " + message.getContact() + " is in wrong state: " + otrSession.getSessionStatus().toString());
}
break;
case Message.ENCRYPTION_AXOLOTL:
message.setFingerprint(account.getAxolotlService().getOwnFingerprint());
if (message.needsUploading()) {
if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize()) || conversation.getMode() == Conversation.MODE_MULTI || message.fixCounterpart()) {
this.sendFileMessage(message, delay);
} else {
break;
}
} else {
XmppAxolotlMessage axolotlMessage = account.getAxolotlService().fetchAxolotlMessageFromCache(message);
if (axolotlMessage == null) {
account.getAxolotlService().preparePayloadMessage(message, delay);
} else {
packet = mMessageGenerator.generateAxolotlChat(message, axolotlMessage);
}
}
break;
}
if (packet != null) {
if (account.getXmppConnection().getFeatures().sm() || (conversation.getMode() == Conversation.MODE_MULTI && message.getCounterpart().isBareJid())) {
message.setStatus(Message.STATUS_UNSEND);
} else {
message.setStatus(Message.STATUS_SEND);
}
}
} else {
switch(message.getEncryption()) {
case Message.ENCRYPTION_DECRYPTED:
if (!message.needsUploading()) {
String pgpBody = message.getEncryptedBody();
String decryptedBody = message.getBody();
// TODO might throw NPE
message.setBody(pgpBody);
message.setEncryption(Message.ENCRYPTION_PGP);
if (message.edited()) {
message.setBody(decryptedBody);
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
databaseBackend.updateMessage(message, message.getEditedId());
updateConversationUi();
return;
} else {
databaseBackend.createMessage(message);
saveInDb = false;
message.setBody(decryptedBody);
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
}
}
break;
case Message.ENCRYPTION_OTR:
if (!conversation.hasValidOtrSession() && message.getCounterpart() != null) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": create otr session without starting for " + message.getContact().getJid());
conversation.startOtrSession(message.getCounterpart().getResourcepart(), false);
}
break;
case Message.ENCRYPTION_AXOLOTL:
message.setFingerprint(account.getAxolotlService().getOwnFingerprint());
break;
}
}
boolean mucMessage = conversation.getMode() == Conversation.MODE_MULTI && message.getType() != Message.TYPE_PRIVATE;
if (mucMessage) {
message.setCounterpart(conversation.getMucOptions().getSelf().getFullJid());
}
if (resend) {
if (packet != null && addToConversation) {
if (account.getXmppConnection().getFeatures().sm() || mucMessage) {
markMessage(message, Message.STATUS_UNSEND);
} else {
markMessage(message, Message.STATUS_SEND);
}
}
} else {
if (addToConversation) {
conversation.add(message);
}
if (saveInDb) {
databaseBackend.createMessage(message);
} else if (message.edited()) {
databaseBackend.updateMessage(message, message.getEditedId());
}
updateConversationUi();
}
if (packet != null) {
if (delay) {
mMessageGenerator.addDelay(packet, message.getTimeSent());
}
if (conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
if (this.sendChatStates()) {
packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
}
}
sendMessagePacket(account, packet);
}
}
Aggregations