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,15 +91,12 @@ 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 {
@ -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,19 +118,16 @@ 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) {
@ -168,19 +163,19 @@ public class PgpEngine {
Intent result = api.executeApi(params, is, os); Intent result = api.executeApi(params, is, os);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
OpenPgpApi.RESULT_CODE_ERROR)) { OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_SUCCESS:
OpenPgpSignatureResult sigResult = result OpenPgpSignatureResult sigResult = result
.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE); .getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);
if (sigResult != null) { if (sigResult != null) {
return sigResult.getKeyId(); return sigResult.getKeyId();
} else { } else {
return 0;
}
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
return 0;
case OpenPgpApi.RESULT_CODE_ERROR:
logError(account, (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
return 0; return 0;
}
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
return 0;
case OpenPgpApi.RESULT_CODE_ERROR:
logError(account, (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
return 0;
} }
return 0; return 0;
} }
@ -188,23 +183,17 @@ 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 -> {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
@Override case OpenPgpApi.RESULT_CODE_SUCCESS:
public void onReturn(Intent result) { callback.success(account);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) { return;
case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.success(account); callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), account);
return; return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: case OpenPgpApi.RESULT_CODE_ERROR:
callback.userInputRequried((PendingIntent) result logError(account, result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
.getParcelableExtra(OpenPgpApi.RESULT_INTENT), callback.error(R.string.openpgp_error, account);
account);
return;
case OpenPgpApi.RESULT_CODE_ERROR:
logError(account, (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
callback.error(R.string.openpgp_error, account);
}
} }
}); });
} }
@ -219,12 +208,9 @@ public class PgpEngine {
params.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, account.getPgpId()); params.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, account.getPgpId());
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 -> {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
@Override
public void onReturn(Intent result) {
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();
try { try {
@ -253,19 +239,16 @@ 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);
if (error != null && "signing subkey not found!".equals(error.getMessage())) { if (error != null && "signing subkey not found!".equals(error.getMessage())) {
callback.error(0,account); callback.error(0, account);
} else { } else {
logError(account, error); logError(account, error);
callback.error(R.string.unable_to_connect_to_keychain, null); callback.error(R.string.unable_to_connect_to_keychain, null);
} }
}
} }
}); });
} }
@ -279,31 +262,20 @@ public class PgpEngine {
@Override @Override
public void onReturn(Intent result) { 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(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), return;
contact); case OpenPgpApi.RESULT_CODE_ERROR:
return; logError(contact.getAccount(), result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
case OpenPgpApi.RESULT_CODE_ERROR: callback.error(R.string.openpgp_error, contact);
logError(contact.getAccount(), (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR)); }
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,25 +79,21 @@ 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() { if (attachmentCounter.decrementAndGet() <=0 ) {
@Override int resId;
public void run() { if (share.image && share.multiple) {
if (attachmentCounter.decrementAndGet() <=0 ) { resId = R.string.shared_images_with_x;
int resId; } else if (share.image) {
if (share.image && share.multiple) { resId = R.string.shared_image_with_x;
resId = R.string.shared_images_with_x; } else {
} else if (share.image) { resId = R.string.shared_file_with_x;
resId = R.string.shared_image_with_x; }
} else { replaceToast(getString(resId, message.getConversation().getName()));
resId = R.string.shared_file_with_x; if (mReturnToPrevious) {
} finish();
replaceToast(getString(resId, message.getConversation().getName())); } else {
if (mReturnToPrevious) { switchToConversation(message.getConversation());
finish();
} else {
switchToConversation(message.getConversation());
}
} }
} }
}); });
@ -110,13 +101,10 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
@Override @Override
public void error(final int errorCode, Message object) { public void error(final int errorCode, Message object) {
runOnUiThread(new Runnable() { runOnUiThread(() -> {
@Override replaceToast(getString(errorCode));
public void run() { if (attachmentCounter.decrementAndGet() <=0 ) {
replaceToast(getString(errorCode)); finish();
if (attachmentCounter.decrementAndGet() <=0 ) {
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 replaceToast(getString(errorCode));
public void run() { finish();
replaceToast(getString(errorCode));
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);
} }
}; };