don't send messages in callback

This commit is contained in:
Daniel Gultsch 2018-02-27 11:44:23 +01:00
parent 521a711fbc
commit 6b55f05e27
6 changed files with 90 additions and 145 deletions

View File

@ -37,6 +37,14 @@ public class PgpEngine {
this.mXmppConnectionService = service; this.mXmppConnectionService = service;
} }
private static void logError(Account account, OpenPgpError error) {
if (error != null) {
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": OpenKeychain error '" + error.getMessage() + "' code=" + error.getErrorId());
} else {
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": OpenKeychain error with no message");
}
}
public void encrypt(final Message message, final UiCallback<Message> callback) { public void encrypt(final Message message, final UiCallback<Message> callback) {
Intent params = new Intent(); Intent params = new Intent();
params.setAction(OpenPgpApi.ACTION_ENCRYPT); params.setAction(OpenPgpApi.ACTION_ENCRYPT);
@ -61,12 +69,8 @@ public class PgpEngine {
} }
InputStream is = new ByteArrayInputStream(body.getBytes()); InputStream is = new ByteArrayInputStream(body.getBytes());
final OutputStream os = new ByteArrayOutputStream(); final OutputStream os = new ByteArrayOutputStream();
api.executeApiAsync(params, is, os, new IOpenPgpCallback() { api.executeApiAsync(params, is, os, result -> {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
@Override
public void onReturn(Intent result) {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_SUCCESS:
try { try {
os.flush(); os.flush();
@ -77,8 +81,9 @@ public class PgpEngine {
encryptedMessageBody.append(lines[i].trim()); encryptedMessageBody.append(lines[i].trim());
} }
} }
message.setEncryptedBody(encryptedMessageBody message.setEncryptedBody(encryptedMessageBody.toString());
.toString()); message.setEncryption(Message.ENCRYPTION_DECRYPTED);
mXmppConnectionService.sendMessage(message);
callback.success(message); callback.success(message);
} catch (IOException e) { } catch (IOException e) {
callback.error(R.string.openpgp_error, message); callback.error(R.string.openpgp_error, message);
@ -86,16 +91,13 @@ public class PgpEngine {
break; break;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried((PendingIntent) result callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), message);
.getParcelableExtra(OpenPgpApi.RESULT_INTENT),
message);
break; break;
case OpenPgpApi.RESULT_CODE_ERROR: case OpenPgpApi.RESULT_CODE_ERROR:
logError(conversation.getAccount(), (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR)); logError(conversation.getAccount(), result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
callback.error(R.string.openpgp_error, message); callback.error(R.string.openpgp_error, message);
break; break;
} }
}
}); });
} else { } else {
try { try {
@ -107,12 +109,8 @@ public class PgpEngine {
outputFile.createNewFile(); outputFile.createNewFile();
final InputStream is = new FileInputStream(inputFile); final InputStream is = new FileInputStream(inputFile);
final OutputStream os = new FileOutputStream(outputFile); final OutputStream os = new FileOutputStream(outputFile);
api.executeApiAsync(params, is, os, new IOpenPgpCallback() { api.executeApiAsync(params, is, os, result -> {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
@Override
public void onReturn(Intent result) {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_SUCCESS:
try { try {
os.flush(); os.flush();
@ -120,20 +118,17 @@ public class PgpEngine {
//ignored //ignored
} }
FileBackend.close(os); FileBackend.close(os);
mXmppConnectionService.sendMessage(message);
callback.success(message); callback.success(message);
break; break;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried( callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), message);
(PendingIntent) result
.getParcelableExtra(OpenPgpApi.RESULT_INTENT),
message);
break; break;
case OpenPgpApi.RESULT_CODE_ERROR: case OpenPgpApi.RESULT_CODE_ERROR:
logError(conversation.getAccount(), (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR)); logError(conversation.getAccount(), result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
callback.error(R.string.openpgp_error, message); callback.error(R.string.openpgp_error, message);
break; break;
} }
}
}); });
} catch (final IOException e) { } catch (final IOException e) {
callback.error(R.string.openpgp_error, message); callback.error(R.string.openpgp_error, message);
@ -188,24 +183,18 @@ public class PgpEngine {
public void chooseKey(final Account account, final UiCallback<Account> callback) { public void chooseKey(final Account account, final UiCallback<Account> callback) {
Intent p = new Intent(); Intent p = new Intent();
p.setAction(OpenPgpApi.ACTION_GET_SIGN_KEY_ID); p.setAction(OpenPgpApi.ACTION_GET_SIGN_KEY_ID);
api.executeApiAsync(p, null, null, new IOpenPgpCallback() { api.executeApiAsync(p, null, null, result -> {
@Override
public void onReturn(Intent result) {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) { switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_SUCCESS:
callback.success(account); callback.success(account);
return; return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried((PendingIntent) result callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), account);
.getParcelableExtra(OpenPgpApi.RESULT_INTENT),
account);
return; return;
case OpenPgpApi.RESULT_CODE_ERROR: case OpenPgpApi.RESULT_CODE_ERROR:
logError(account, (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR)); logError(account, result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
callback.error(R.string.openpgp_error, account); callback.error(R.string.openpgp_error, account);
} }
}
}); });
} }
@ -220,10 +209,7 @@ public class PgpEngine {
InputStream is = new ByteArrayInputStream(status.getBytes()); InputStream is = new ByteArrayInputStream(status.getBytes());
final OutputStream os = new ByteArrayOutputStream(); final OutputStream os = new ByteArrayOutputStream();
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": signing status message \"" + status + "\""); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": signing status message \"" + status + "\"");
api.executeApiAsync(params, is, os, new IOpenPgpCallback() { api.executeApiAsync(params, is, os, result -> {
@Override
public void onReturn(Intent result) {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) { switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_SUCCESS:
StringBuilder signatureBuilder = new StringBuilder(); StringBuilder signatureBuilder = new StringBuilder();
@ -253,9 +239,7 @@ public class PgpEngine {
callback.success(account); callback.success(account);
return; return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried((PendingIntent) result callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), account);
.getParcelableExtra(OpenPgpApi.RESULT_INTENT),
account);
return; return;
case OpenPgpApi.RESULT_CODE_ERROR: case OpenPgpApi.RESULT_CODE_ERROR:
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR); OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
@ -266,7 +250,6 @@ public class PgpEngine {
callback.error(R.string.unable_to_connect_to_keychain, null); callback.error(R.string.unable_to_connect_to_keychain, null);
} }
} }
}
}); });
} }
@ -283,27 +266,16 @@ public class PgpEngine {
callback.success(contact); callback.success(contact);
return; return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried((PendingIntent) result callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), contact);
.getParcelableExtra(OpenPgpApi.RESULT_INTENT),
contact);
return; return;
case OpenPgpApi.RESULT_CODE_ERROR: case OpenPgpApi.RESULT_CODE_ERROR:
logError(contact.getAccount(), (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR)); logError(contact.getAccount(), result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
callback.error(R.string.openpgp_error, contact); callback.error(R.string.openpgp_error, contact);
} }
} }
}); });
} }
private static void logError(Account account, OpenPgpError error) {
if (error != null) {
Log.d(Config.LOGTAG,account.getJid().toBareJid().toString()+": OpenKeychain error '"+error.getMessage()+"' code="+error.getErrorId());
} else {
Log.d(Config.LOGTAG,account.getJid().toBareJid().toString()+": OpenKeychain error with no message");
}
}
public PendingIntent getIntentForKey(long pgpKeyId) { public PendingIntent getIntentForKey(long pgpKeyId) {
Intent params = new Intent(); Intent params = new Intent();
params.setAction(OpenPgpApi.ACTION_GET_KEY); params.setAction(OpenPgpApi.ACTION_GET_KEY);

View File

@ -60,6 +60,7 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
mXmppConnectionService.getPgpEngine().encrypt(message, callback); mXmppConnectionService.getPgpEngine().encrypt(message, callback);
} else { } else {
mXmppConnectionService.sendMessage(message);
callback.success(message); callback.success(message);
} }
} else { } else {
@ -74,6 +75,7 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod
callback.error(R.string.unable_to_connect_to_keychain, null); callback.error(R.string.unable_to_connect_to_keychain, null);
} }
} else { } else {
mXmppConnectionService.sendMessage(message);
callback.success(message); callback.success(message);
} }
} catch (FileBackend.FileCopyException e) { } catch (FileBackend.FileCopyException e) {

View File

@ -447,9 +447,7 @@ public class XmppConnectionService extends Service {
return this.mAvatarService; return this.mAvatarService;
} }
public void attachLocationToConversation(final Conversation conversation, public void attachLocationToConversation(final Conversation conversation,final Uri uri, final UiCallback<Message> callback) {
final Uri uri,
final UiCallback<Message> callback) {
int encryption = conversation.getNextEncryption(); int encryption = conversation.getNextEncryption();
if (encryption == Message.ENCRYPTION_PGP) { if (encryption == Message.ENCRYPTION_PGP) {
encryption = Message.ENCRYPTION_DECRYPTED; encryption = Message.ENCRYPTION_DECRYPTED;
@ -461,6 +459,7 @@ public class XmppConnectionService extends Service {
if (encryption == Message.ENCRYPTION_DECRYPTED) { if (encryption == Message.ENCRYPTION_DECRYPTED) {
getPgpEngine().encrypt(message, callback); getPgpEngine().encrypt(message, callback);
} else { } else {
sendMessage(message);
callback.success(message); callback.success(message);
} }
} }
@ -528,6 +527,7 @@ public class XmppConnectionService extends Service {
callback.error(R.string.unable_to_connect_to_keychain, null); callback.error(R.string.unable_to_connect_to_keychain, null);
} }
} else { } else {
sendMessage(message);
callback.success(message); callback.success(message);
} }
} catch (final FileBackend.FileCopyException e) { } catch (final FileBackend.FileCopyException e) {

View File

@ -493,12 +493,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
@Override @Override
public void success(Message message) { public void success(Message message) {
activity.xmppConnectionService.sendMessage(message);
} }
@Override @Override
public void error(int errorCode, Message object) { public void error(int errorCode, Message object) {
//TODO show possible pgp error
} }
@Override @Override
@ -526,7 +526,6 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
public void success(Message message) { public void success(Message message) {
runOnUiThread(() -> activity.hideToast()); runOnUiThread(() -> activity.hideToast());
hidePrepareFileToast(prepareFileToast); hidePrepareFileToast(prepareFileToast);
activity.xmppConnectionService.sendMessage(message);
} }
@Override @Override
@ -565,7 +564,6 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
@Override @Override
public void success(Message message) { public void success(Message message) {
hidePrepareFileToast(prepareFileToast); hidePrepareFileToast(prepareFileToast);
activity.xmppConnectionService.sendMessage(message);
} }
@Override @Override
@ -2198,8 +2196,6 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
@Override @Override
public void success(Message message) { public void success(Message message) {
//TODO the following two call can be made before the callback //TODO the following two call can be made before the callback
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
activity.xmppConnectionService.sendMessage(message);
getActivity().runOnUiThread(() -> messageSent()); getActivity().runOnUiThread(() -> messageSent());
} }

View File

@ -68,12 +68,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
@Override @Override
public void inform(final String text) { public void inform(final String text) {
runOnUiThread(new Runnable() { runOnUiThread(() -> replaceToast(text));
@Override
public void run() {
replaceToast(text);
}
});
} }
@Override @Override
@ -84,10 +79,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
@Override @Override
public void success(final Message message) { public void success(final Message message) {
xmppConnectionService.sendMessage(message); runOnUiThread(() -> {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (attachmentCounter.decrementAndGet() <=0 ) { if (attachmentCounter.decrementAndGet() <=0 ) {
int resId; int resId;
if (share.image && share.multiple) { if (share.image && share.multiple) {
@ -104,20 +96,16 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
switchToConversation(message.getConversation()); switchToConversation(message.getConversation());
} }
} }
}
}); });
} }
@Override @Override
public void error(final int errorCode, Message object) { public void error(final int errorCode, Message object) {
runOnUiThread(new Runnable() { runOnUiThread(() -> {
@Override
public void run() {
replaceToast(getString(errorCode)); replaceToast(getString(errorCode));
if (attachmentCounter.decrementAndGet() <=0 ) { if (attachmentCounter.decrementAndGet() <=0 ) {
finish(); finish();
} }
}
}); });
} }
}; };
@ -343,7 +331,6 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
final PresenceSelector.OnPresenceSelected callback = new PresenceSelector.OnPresenceSelected() { final PresenceSelector.OnPresenceSelected callback = new PresenceSelector.OnPresenceSelected() {
private void finishAndSend(Message message) { private void finishAndSend(Message message) {
xmppConnectionService.sendMessage(message);
replaceToast(getString(R.string.shared_text_with_x, conversation.getName())); replaceToast(getString(R.string.shared_text_with_x, conversation.getName()));
finish(); finish();
} }
@ -351,23 +338,14 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
private UiCallback<Message> messageEncryptionCallback = new UiCallback<Message>() { private UiCallback<Message> messageEncryptionCallback = new UiCallback<Message>() {
@Override @Override
public void success(final Message message) { public void success(final Message message) {
message.setEncryption(Message.ENCRYPTION_DECRYPTED); runOnUiThread(() -> finishAndSend(message));
runOnUiThread(new Runnable() {
@Override
public void run() {
finishAndSend(message);
}
});
} }
@Override @Override
public void error(final int errorCode, Message object) { public void error(final int errorCode, Message object) {
runOnUiThread(new Runnable() { runOnUiThread(() -> {
@Override
public void run() {
replaceToast(getString(errorCode)); replaceToast(getString(errorCode));
finish(); finish();
}
}); });
} }
@ -391,10 +369,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
xmppConnectionService.getPgpEngine().encrypt(message,messageEncryptionCallback); xmppConnectionService.getPgpEngine().encrypt(message,messageEncryptionCallback);
return; return;
} }
xmppConnectionService.sendMessage(message);
if (encryption == Message.ENCRYPTION_OTR) {
message.setCounterpart(conversation.getNextCounterpart());
}
finishAndSend(message); finishAndSend(message);
} }
}; };