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;
}
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) {
Intent params = new Intent();
params.setAction(OpenPgpApi.ACTION_ENCRYPT);
@ -61,12 +69,8 @@ public class PgpEngine {
}
InputStream is = new ByteArrayInputStream(body.getBytes());
final OutputStream os = new ByteArrayOutputStream();
api.executeApiAsync(params, is, os, new IOpenPgpCallback() {
@Override
public void onReturn(Intent result) {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
OpenPgpApi.RESULT_CODE_ERROR)) {
api.executeApiAsync(params, is, os, result -> {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
try {
os.flush();
@ -77,8 +81,9 @@ public class PgpEngine {
encryptedMessageBody.append(lines[i].trim());
}
}
message.setEncryptedBody(encryptedMessageBody
.toString());
message.setEncryptedBody(encryptedMessageBody.toString());
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
mXmppConnectionService.sendMessage(message);
callback.success(message);
} catch (IOException e) {
callback.error(R.string.openpgp_error, message);
@ -86,15 +91,12 @@ public class PgpEngine {
break;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried((PendingIntent) result
.getParcelableExtra(OpenPgpApi.RESULT_INTENT),
message);
callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), message);
break;
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);
break;
}
}
});
} else {
@ -107,12 +109,8 @@ public class PgpEngine {
outputFile.createNewFile();
final InputStream is = new FileInputStream(inputFile);
final OutputStream os = new FileOutputStream(outputFile);
api.executeApiAsync(params, is, os, new IOpenPgpCallback() {
@Override
public void onReturn(Intent result) {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
OpenPgpApi.RESULT_CODE_ERROR)) {
api.executeApiAsync(params, is, os, result -> {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
try {
os.flush();
@ -120,19 +118,16 @@ public class PgpEngine {
//ignored
}
FileBackend.close(os);
mXmppConnectionService.sendMessage(message);
callback.success(message);
break;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried(
(PendingIntent) result
.getParcelableExtra(OpenPgpApi.RESULT_INTENT),
message);
callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), message);
break;
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);
break;
}
}
});
} catch (final IOException e) {
@ -168,19 +163,19 @@ public class PgpEngine {
Intent result = api.executeApi(params, is, os);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
OpenPgpSignatureResult sigResult = result
.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);
if (sigResult != null) {
return sigResult.getKeyId();
} else {
case OpenPgpApi.RESULT_CODE_SUCCESS:
OpenPgpSignatureResult sigResult = result
.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);
if (sigResult != null) {
return sigResult.getKeyId();
} 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;
}
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;
}
@ -188,23 +183,17 @@ public class PgpEngine {
public void chooseKey(final Account account, final UiCallback<Account> callback) {
Intent p = new Intent();
p.setAction(OpenPgpApi.ACTION_GET_SIGN_KEY_ID);
api.executeApiAsync(p, null, null, new IOpenPgpCallback() {
@Override
public void onReturn(Intent result) {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
callback.success(account);
return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried((PendingIntent) result
.getParcelableExtra(OpenPgpApi.RESULT_INTENT),
account);
return;
case OpenPgpApi.RESULT_CODE_ERROR:
logError(account, (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
callback.error(R.string.openpgp_error, account);
}
api.executeApiAsync(p, null, null, result -> {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
callback.success(account);
return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), account);
return;
case OpenPgpApi.RESULT_CODE_ERROR:
logError(account, 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());
InputStream is = new ByteArrayInputStream(status.getBytes());
final OutputStream os = new ByteArrayOutputStream();
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": signing status message \""+status+"\"");
api.executeApiAsync(params, is, os, new IOpenPgpCallback() {
@Override
public void onReturn(Intent result) {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": signing status message \"" + status + "\"");
api.executeApiAsync(params, is, os, result -> {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
StringBuilder signatureBuilder = new StringBuilder();
try {
@ -253,19 +239,16 @@ public class PgpEngine {
callback.success(account);
return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried((PendingIntent) result
.getParcelableExtra(OpenPgpApi.RESULT_INTENT),
account);
callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), account);
return;
case OpenPgpApi.RESULT_CODE_ERROR:
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
if (error != null && "signing subkey not found!".equals(error.getMessage())) {
callback.error(0,account);
callback.error(0, account);
} else {
logError(account, error);
callback.error(R.string.unable_to_connect_to_keychain, null);
}
}
}
});
}
@ -279,31 +262,20 @@ public class PgpEngine {
@Override
public void onReturn(Intent result) {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
callback.success(contact);
return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried((PendingIntent) result
.getParcelableExtra(OpenPgpApi.RESULT_INTENT),
contact);
return;
case OpenPgpApi.RESULT_CODE_ERROR:
logError(contact.getAccount(), (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
callback.error(R.string.openpgp_error, contact);
}
case OpenPgpApi.RESULT_CODE_SUCCESS:
callback.success(contact);
return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), contact);
return;
case OpenPgpApi.RESULT_CODE_ERROR:
logError(contact.getAccount(), 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) {
Intent params = new Intent();
params.setAction(OpenPgpApi.ACTION_GET_KEY);

View file

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

View file

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

View file

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

View file

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