use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.
the class XmppConnectionService method onOtrSessionEstablished.
public void onOtrSessionEstablished(Conversation conversation) {
final Account account = conversation.getAccount();
final Session otrSession = conversation.getOtrSession();
Log.d(Config.LOGTAG, account.getJid().toBareJid() + " otr session established with " + conversation.getJid() + "/" + otrSession.getSessionID().getUserID());
conversation.findUnsentMessagesWithEncryption(Message.ENCRYPTION_OTR, new Conversation.OnMessageFound() {
@Override
public void onMessageFound(Message message) {
SessionID id = otrSession.getSessionID();
try {
message.setCounterpart(Jid.fromString(id.getAccountID() + "/" + id.getUserID()));
} catch (InvalidJidException e) {
return;
}
if (message.needsUploading()) {
mJingleConnectionManager.createNewConnection(message);
} else {
MessagePacket outPacket = mMessageGenerator.generateOtrChat(message);
if (outPacket != null) {
mMessageGenerator.addDelay(outPacket, message.getTimeSent());
message.setStatus(Message.STATUS_SEND);
databaseBackend.updateMessage(message);
sendMessagePacket(account, outPacket);
}
}
updateConversationUi();
}
});
}
use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.
the class XmppConnectionService method restoreFromDatabase.
private void restoreFromDatabase() {
synchronized (this.conversations) {
final Map<String, Account> accountLookupTable = new Hashtable<>();
for (Account account : this.accounts) {
accountLookupTable.put(account.getUuid(), account);
}
Log.d(Config.LOGTAG, "restoring conversations...");
final long startTimeConversationsRestore = SystemClock.elapsedRealtime();
this.conversations.addAll(databaseBackend.getConversations(Conversation.STATUS_AVAILABLE));
for (Iterator<Conversation> iterator = conversations.listIterator(); iterator.hasNext(); ) {
Conversation conversation = iterator.next();
Account account = accountLookupTable.get(conversation.getAccountUuid());
if (account != null) {
conversation.setAccount(account);
} else {
Log.e(Config.LOGTAG, "unable to restore Conversations with " + conversation.getJid());
iterator.remove();
}
}
long diffConversationsRestore = SystemClock.elapsedRealtime() - startTimeConversationsRestore;
Log.d(Config.LOGTAG, "finished restoring conversations in " + diffConversationsRestore + "ms");
Runnable runnable = new Runnable() {
@Override
public void run() {
long deletionDate = getAutomaticMessageDeletionDate();
mLastExpiryRun.set(SystemClock.elapsedRealtime());
if (deletionDate > 0) {
Log.d(Config.LOGTAG, "deleting messages that are older than " + AbstractGenerator.getTimestamp(deletionDate));
databaseBackend.expireOldMessages(deletionDate);
}
Log.d(Config.LOGTAG, "restoring roster...");
for (Account account : accounts) {
databaseBackend.readRoster(account.getRoster());
// roster needs to be loaded at this stage
account.initAccountServices(XmppConnectionService.this);
}
getBitmapCache().evictAll();
loadPhoneContacts();
Log.d(Config.LOGTAG, "restoring messages...");
final long startMessageRestore = SystemClock.elapsedRealtime();
for (Conversation conversation : conversations) {
conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE));
checkDeletedFiles(conversation);
conversation.findUnsentTextMessages(new Conversation.OnMessageFound() {
@Override
public void onMessageFound(Message message) {
markMessage(message, Message.STATUS_WAITING);
}
});
conversation.findUnreadMessages(new Conversation.OnMessageFound() {
@Override
public void onMessageFound(Message message) {
mNotificationService.pushFromBacklog(message);
}
});
}
mNotificationService.finishBacklog(false);
restoredFromDatabaseLatch.countDown();
final long diffMessageRestore = SystemClock.elapsedRealtime() - startMessageRestore;
Log.d(Config.LOGTAG, "finished restoring messages in " + diffMessageRestore + "ms");
updateConversationUi();
}
};
// will contain one write command (expiry) but that's fine
mDatabaseReaderExecutor.execute(runnable);
}
}
use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.
the class MessageParser method parseOtrChat.
private Message parseOtrChat(String body, Jid from, String id, Conversation conversation) {
String presence;
if (from.isBareJid()) {
presence = "";
} else {
presence = from.getResourcepart();
}
if (body.matches("^\\?OTRv\\d{1,2}\\?.*")) {
conversation.endOtrIfNeeded();
}
if (!conversation.hasValidOtrSession()) {
conversation.startOtrSession(presence, false);
} else {
String foreignPresence = conversation.getOtrSession().getSessionID().getUserID();
if (!foreignPresence.equals(presence)) {
conversation.endOtrIfNeeded();
conversation.startOtrSession(presence, false);
}
}
try {
conversation.setLastReceivedOtrMessageId(id);
Session otrSession = conversation.getOtrSession();
body = otrSession.transformReceiving(body);
SessionStatus status = otrSession.getSessionStatus();
if (body == null && status == SessionStatus.ENCRYPTED) {
mXmppConnectionService.onOtrSessionEstablished(conversation);
return null;
} else if (body == null && status == SessionStatus.FINISHED) {
conversation.resetOtrSession();
mXmppConnectionService.updateConversationUi();
return null;
} else if (body == null || (body.isEmpty())) {
return null;
}
if (body.startsWith(CryptoHelper.FILETRANSFER)) {
String key = body.substring(CryptoHelper.FILETRANSFER.length());
conversation.setSymmetricKey(CryptoHelper.hexToBytes(key));
return null;
}
if (clientMightSendHtml(conversation.getAccount(), from)) {
Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": received OTR message from bad behaving client. escaping HTML…");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
body = Html.fromHtml(body, Html.FROM_HTML_MODE_LEGACY).toString();
} else {
body = Html.fromHtml(body).toString();
}
}
final OtrService otrService = conversation.getAccount().getOtrService();
Message finishedMessage = new Message(conversation, body, Message.ENCRYPTION_OTR, Message.STATUS_RECEIVED);
finishedMessage.setFingerprint(otrService.getFingerprint(otrSession.getRemotePublicKey()));
conversation.setLastReceivedOtrMessageId(null);
return finishedMessage;
} catch (Exception e) {
conversation.resetOtrSession();
return null;
}
}
use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.
the class AudioPlayer method onProgressChanged.
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
synchronized (AudioPlayer.LOCK) {
final RelativeLayout audioPlayer = (RelativeLayout) seekBar.getParent();
final Message message = (Message) audioPlayer.getTag();
if (fromUser && message == AudioPlayer.currentlyPlayingMessage) {
float percent = progress / 100f;
int duration = AudioPlayer.player.getDuration();
int seekTo = Math.round(duration * percent);
AudioPlayer.player.seekTo(seekTo);
}
}
}
use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.
the class ShareWithActivity method share.
private void share(final Conversation conversation) {
if (share.uris.size() != 0 && !hasStoragePermission(REQUEST_STORAGE_PERMISSION)) {
mPendingConversation = conversation;
return;
}
final Account account = conversation.getAccount();
final XmppConnection connection = account.getXmppConnection();
final long max = connection == null ? -1 : connection.getFeatures().getMaxHttpUploadSize();
mListView.setEnabled(false);
if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP && !hasPgp()) {
if (share.uuid == null) {
showInstallPgpDialog();
} else {
Toast.makeText(this, R.string.openkeychain_not_installed, Toast.LENGTH_SHORT).show();
finish();
}
return;
}
if (share.uris.size() != 0) {
PresenceSelector.OnPresenceSelected callback = () -> {
attachmentCounter.set(share.uris.size());
if (share.image) {
Log.d(Config.LOGTAG, "ShareWithActivity share() image " + share.uris.size() + " uri(s) " + share.uris.toString());
share.multiple = share.uris.size() > 1;
replaceToast(getString(share.multiple ? R.string.preparing_images : R.string.preparing_image));
for (Iterator<Uri> i = share.uris.iterator(); i.hasNext(); i.remove()) {
final Uri uri = i.next();
delegateUriPermissionsToService(uri);
xmppConnectionService.attachImageToConversation(conversation, uri, attachFileCallback);
}
} else {
Log.d(Config.LOGTAG, "ShareWithActivity share() file " + share.uris.size() + " uri(s) " + share.uris.toString());
replaceToast(getString(R.string.preparing_file));
final Uri uri = share.uris.get(0);
delegateUriPermissionsToService(uri);
xmppConnectionService.attachFileToConversation(conversation, uri, share.type, attachFileCallback);
finish();
}
};
if (account.httpUploadAvailable() && ((share.image && !neverCompressPictures()) || conversation.getMode() == Conversation.MODE_MULTI || FileBackend.allFilesUnderSize(this, share.uris, max)) && conversation.getNextEncryption() != Message.ENCRYPTION_OTR) {
callback.onPresenceSelected();
} else {
selectPresence(conversation, callback);
}
} else {
if (mReturnToPrevious && this.share.text != null && !this.share.text.isEmpty()) {
final PresenceSelector.OnPresenceSelected callback = new PresenceSelector.OnPresenceSelected() {
private void finishAndSend(Message message) {
replaceToast(getString(R.string.shared_text_with_x, conversation.getName()));
finish();
}
private UiCallback<Message> messageEncryptionCallback = new UiCallback<Message>() {
@Override
public void success(final Message message) {
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
runOnUiThread(() -> finishAndSend(message));
}
@Override
public void error(final int errorCode, Message object) {
runOnUiThread(() -> {
replaceToast(getString(errorCode));
finish();
});
}
@Override
public void userInputRequried(PendingIntent pi, Message object) {
finish();
}
};
@Override
public void onPresenceSelected() {
final int encryption = conversation.getNextEncryption();
Message message = new Message(conversation, share.text, encryption);
Log.d(Config.LOGTAG, "on presence selected encrpytion=" + encryption);
if (encryption == Message.ENCRYPTION_PGP) {
replaceToast(getString(R.string.encrypting_message));
xmppConnectionService.getPgpEngine().encrypt(message, messageEncryptionCallback);
return;
}
if (encryption == Message.ENCRYPTION_OTR) {
message.setCounterpart(conversation.getNextCounterpart());
}
xmppConnectionService.sendMessage(message);
finishAndSend(message);
}
};
if (conversation.getNextEncryption() == Message.ENCRYPTION_OTR) {
selectPresence(conversation, callback);
} else {
callback.onPresenceSelected();
}
} else {
final PresenceSelector.OnPresenceSelected callback = () -> {
Message message = new Message(conversation, share.text, conversation.getNextEncryption());
if (conversation.getNextEncryption() == Message.ENCRYPTION_OTR) {
message.setCounterpart(conversation.getNextCounterpart());
}
xmppConnectionService.sendMessage(message);
replaceToast(getString(R.string.shared_text_with_x, conversation.getName()));
switchToConversation(message.getConversation());
};
if (conversation.getNextEncryption() == Message.ENCRYPTION_OTR) {
selectPresence(conversation, callback);
} else {
callback.onPresenceSelected();
}
}
}
}
Aggregations