diff options
7 files changed, 147 insertions, 121 deletions
diff --git a/src/eu/siacs/conversations/crypto/PgpEngine.java b/src/eu/siacs/conversations/crypto/PgpEngine.java index 3b78db5e..75c47768 100644 --- a/src/eu/siacs/conversations/crypto/PgpEngine.java +++ b/src/eu/siacs/conversations/crypto/PgpEngine.java @@ -36,17 +36,19 @@ public class PgpEngine { this.mXmppConnectionService = service; } - public void decrypt(final Message message, final UiCallback callback) { - Log.d("xmppService","decrypting message "+message.getUuid()); + public void decrypt(final Message message, + final UiCallback<Message> callback) { + Log.d("xmppService", "decrypting message " + message.getUuid()); Intent params = new Intent(); params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, message .getConversation().getAccount().getJid()); if (message.getType() == Message.TYPE_TEXT) { - InputStream is = new ByteArrayInputStream(message.getBody().getBytes()); + InputStream is = new ByteArrayInputStream(message.getBody() + .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, @@ -54,14 +56,15 @@ public class PgpEngine { case OpenPgpApi.RESULT_CODE_SUCCESS: message.setBody(os.toString()); message.setEncryption(Message.ENCRYPTION_DECRYPTED); - callback.success(); + callback.success(message); return; case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: callback.userInputRequried((PendingIntent) result - .getParcelableExtra(OpenPgpApi.RESULT_INTENT)); + .getParcelableExtra(OpenPgpApi.RESULT_INTENT), + message); return; case OpenPgpApi.RESULT_CODE_ERROR: - callback.error(R.string.openpgp_error); + callback.error(R.string.openpgp_error, message); return; default: return; @@ -70,13 +73,15 @@ public class PgpEngine { }); } else if (message.getType() == Message.TYPE_IMAGE) { try { - final JingleFile inputFile = this.mXmppConnectionService.getFileBackend().getJingleFile(message, false); - final JingleFile outputFile = this.mXmppConnectionService.getFileBackend().getJingleFile(message,true); + final JingleFile inputFile = this.mXmppConnectionService + .getFileBackend().getJingleFile(message, false); + final JingleFile outputFile = this.mXmppConnectionService + .getFileBackend().getJingleFile(message, true); outputFile.createNewFile(); InputStream is = new FileInputStream(inputFile); OutputStream os = new FileOutputStream(outputFile); api.executeApiAsync(params, is, os, new IOpenPgpCallback() { - + @Override public void onReturn(Intent result) { switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, @@ -84,21 +89,27 @@ public class PgpEngine { case OpenPgpApi.RESULT_CODE_SUCCESS: BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; - BitmapFactory.decodeFile(outputFile.getAbsolutePath(),options); + BitmapFactory.decodeFile( + outputFile.getAbsolutePath(), options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; - message.setBody(""+outputFile.getSize()+","+imageWidth+","+imageHeight); + message.setBody("" + outputFile.getSize() + "," + + imageWidth + "," + imageHeight); message.setEncryption(Message.ENCRYPTION_DECRYPTED); - PgpEngine.this.mXmppConnectionService.updateMessage(message); - PgpEngine.this.mXmppConnectionService.updateUi(message.getConversation(), false); - callback.success(); + PgpEngine.this.mXmppConnectionService + .updateMessage(message); + PgpEngine.this.mXmppConnectionService.updateUi( + message.getConversation(), false); + callback.success(message); return; case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: - callback.userInputRequried((PendingIntent) result - .getParcelableExtra(OpenPgpApi.RESULT_INTENT)); + callback.userInputRequried( + (PendingIntent) result + .getParcelableExtra(OpenPgpApi.RESULT_INTENT), + message); return; case OpenPgpApi.RESULT_CODE_ERROR: - callback.error(R.string.openpgp_error); + callback.error(R.string.openpgp_error, message); return; default: return; @@ -106,33 +117,38 @@ public class PgpEngine { } }); } catch (FileNotFoundException e) { - callback.error(R.string.error_decrypting_file); + callback.error(R.string.error_decrypting_file, message); } catch (IOException e) { - callback.error(R.string.error_decrypting_file); + callback.error(R.string.error_decrypting_file, message); } - + } } - public void encrypt(final Message message,final UiCallback callback) { - + public void encrypt(final Message message, + final UiCallback<Message> callback) { + Intent params = new Intent(); params.setAction(OpenPgpApi.ACTION_ENCRYPT); if (message.getConversation().getMode() == Conversation.MODE_SINGLE) { - long[] keys = { message.getConversation().getContact().getPgpKeyId() }; + long[] keys = { message.getConversation().getContact() + .getPgpKeyId() }; params.putExtra(OpenPgpApi.EXTRA_KEY_IDS, keys); } else { - params.putExtra(OpenPgpApi.EXTRA_KEY_IDS, message.getConversation().getMucOptions().getPgpKeyIds()); + params.putExtra(OpenPgpApi.EXTRA_KEY_IDS, message.getConversation() + .getMucOptions().getPgpKeyIds()); } - params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, message.getConversation().getAccount().getJid()); - + params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, message + .getConversation().getAccount().getJid()); + if (message.getType() == Message.TYPE_TEXT) { params.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); - - InputStream is = new ByteArrayInputStream(message.getBody().getBytes()); + + InputStream is = new ByteArrayInputStream(message.getBody() + .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, @@ -143,53 +159,61 @@ public class PgpEngine { for (int i = 3; i < lines.length - 1; ++i) { encryptedMessageBody.append(lines[i].trim()); } - message.setEncryptedBody(encryptedMessageBody.toString()); - callback.success(); + message.setEncryptedBody(encryptedMessageBody + .toString()); + message.ready = true; + callback.success(message); break; case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: callback.userInputRequried((PendingIntent) result - .getParcelableExtra(OpenPgpApi.RESULT_INTENT)); + .getParcelableExtra(OpenPgpApi.RESULT_INTENT), + message); break; case OpenPgpApi.RESULT_CODE_ERROR: - callback.error(R.string.openpgp_error); + callback.error(R.string.openpgp_error, message); break; } } }); } else if (message.getType() == Message.TYPE_IMAGE) { try { - JingleFile inputFile = this.mXmppConnectionService.getFileBackend().getJingleFile(message, true); - JingleFile outputFile = this.mXmppConnectionService.getFileBackend().getJingleFile(message, false); + JingleFile inputFile = this.mXmppConnectionService + .getFileBackend().getJingleFile(message, true); + JingleFile outputFile = this.mXmppConnectionService + .getFileBackend().getJingleFile(message, false); outputFile.createNewFile(); InputStream is = new FileInputStream(inputFile); 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)) { case OpenPgpApi.RESULT_CODE_SUCCESS: - callback.success(); + message.ready = true; + callback.success(message); break; case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: - callback.userInputRequried((PendingIntent) result - .getParcelableExtra(OpenPgpApi.RESULT_INTENT)); + callback.userInputRequried( + (PendingIntent) result + .getParcelableExtra(OpenPgpApi.RESULT_INTENT), + message); break; case OpenPgpApi.RESULT_CODE_ERROR: - callback.error(R.string.openpgp_error); + callback.error(R.string.openpgp_error, message); break; } } }); } catch (FileNotFoundException e) { - Log.d("xmppService","file not found: "+e.getMessage()); + Log.d("xmppService", "file not found: " + e.getMessage()); } catch (IOException e) { - Log.d("xmppService","io exception during file encrypt"); + Log.d("xmppService", "io exception during file encrypt"); } } } - + public long fetchKeyId(Account account, String status, String signature) { if ((signature == null) || (api == null)) { return 0; @@ -229,15 +253,18 @@ public class PgpEngine { case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: return 0; case OpenPgpApi.RESULT_CODE_ERROR: - Log.d("xmppService","openpgp error: "+((OpenPgpError) result - .getParcelableExtra(OpenPgpApi.RESULT_ERROR)).getMessage()); + Log.d("xmppService", + "openpgp error: " + + ((OpenPgpError) result + .getParcelableExtra(OpenPgpApi.RESULT_ERROR)) + .getMessage()); return 0; } return 0; } public void generateSignature(final Account account, String status, - final UiCallback callback) { + final UiCallback<Account> callback) { Intent params = new Intent(); params.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); params.setAction(OpenPgpApi.ACTION_SIGN); @@ -256,52 +283,57 @@ public class PgpEngine { signatureBuilder.append(lines[i].trim()); } account.setKey("pgp_signature", signatureBuilder.toString()); - callback.success(); + callback.success(account); return; case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: callback.userInputRequried((PendingIntent) result - .getParcelableExtra(OpenPgpApi.RESULT_INTENT)); + .getParcelableExtra(OpenPgpApi.RESULT_INTENT), + account); return; case OpenPgpApi.RESULT_CODE_ERROR: - callback.error(R.string.openpgp_error); + callback.error(R.string.openpgp_error, account); return; } } }); } - - public void hasKey(Contact contact, final UiCallback callback) { + + public void hasKey(final Contact contact, final UiCallback<Contact> callback) { Intent params = new Intent(); params.setAction(OpenPgpApi.ACTION_GET_KEY); params.putExtra(OpenPgpApi.EXTRA_KEY_ID, contact.getPgpKeyId()); - params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, contact.getAccount().getJid()); + params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, contact.getAccount() + .getJid()); api.executeApiAsync(params, null, null, new IOpenPgpCallback() { - + @Override public void onReturn(Intent result) { switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) { case OpenPgpApi.RESULT_CODE_SUCCESS: - callback.success(); + callback.success(contact); return; case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: callback.userInputRequried((PendingIntent) result - .getParcelableExtra(OpenPgpApi.RESULT_INTENT)); + .getParcelableExtra(OpenPgpApi.RESULT_INTENT), + contact); return; case OpenPgpApi.RESULT_CODE_ERROR: - callback.error(R.string.openpgp_error); + callback.error(R.string.openpgp_error, contact); return; } } }); } - + public PendingIntent getIntentForKey(Contact contact) { Intent params = new Intent(); params.setAction(OpenPgpApi.ACTION_GET_KEY); params.putExtra(OpenPgpApi.EXTRA_KEY_ID, contact.getPgpKeyId()); - params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, contact.getAccount().getJid()); + params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, contact.getAccount() + .getJid()); Intent result = api.executeApi(params, null, null); - return (PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); + return (PendingIntent) result + .getParcelableExtra(OpenPgpApi.RESULT_INTENT); } public PendingIntent getIntentForKey(Account account, long pgpKeyId) { @@ -310,6 +342,7 @@ public class PgpEngine { params.putExtra(OpenPgpApi.EXTRA_KEY_ID, pgpKeyId); params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, account.getJid()); Intent result = api.executeApi(params, null, null); - return (PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); + return (PendingIntent) result + .getParcelableExtra(OpenPgpApi.RESULT_INTENT); } } diff --git a/src/eu/siacs/conversations/entities/Message.java b/src/eu/siacs/conversations/entities/Message.java index c298a652..0f50f026 100644 --- a/src/eu/siacs/conversations/entities/Message.java +++ b/src/eu/siacs/conversations/entities/Message.java @@ -55,6 +55,8 @@ public class Message extends AbstractEntity { protected transient Conversation conversation = null; protected transient JingleConnection jingleConnection = null; + + public boolean ready = true; private Message() { diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index 81ac77c2..1d59692c 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -356,7 +356,7 @@ public class XmppConnectionService extends Service { } public Message attachImageToConversation(final Conversation conversation, - final Uri uri, final UiCallback callback) { + final Uri uri, final UiCallback<Message> callback) { final Message message; if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { message = new Message(conversation, "", @@ -376,10 +376,11 @@ public class XmppConnectionService extends Service { if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { getPgpEngine().encrypt(message, callback); } else { - callback.success(); + message.ready = true; + callback.success(message); } } catch (FileBackend.ImageCopyException e) { - callback.error(e.getResId()); + callback.error(e.getResId(),message); } } }).start(); diff --git a/src/eu/siacs/conversations/ui/ConversationActivity.java b/src/eu/siacs/conversations/ui/ConversationActivity.java index 55c538d6..0c9c154f 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -85,8 +85,6 @@ public class ConversationActivity extends XmppActivity { private boolean showLastseen = false; private ArrayAdapter<Conversation> listAdapter; - public Message pendingMessage = null; - private OnConversationListChangedListener onConvChanged = new OnConversationListChangedListener() { @Override @@ -399,21 +397,23 @@ public class ConversationActivity extends XmppActivity { if (hasPgp()) { if (conversation.getContact().getPgpKeyId() != 0) { xmppConnectionService.getPgpEngine().hasKey( - conversation.getContact(), new UiCallback() { + conversation.getContact(), + new UiCallback<Contact>() { @Override - public void userInputRequried(PendingIntent pi) { + public void userInputRequried(PendingIntent pi, + Contact contact) { ConversationActivity.this.runIntent(pi, attachmentChoice); } @Override - public void success() { + public void success(Contact contact) { selectPresenceToAttachFile(attachmentChoice); } @Override - public void error(int error) { + public void error(int error, Contact contact) { displayErrorDialog(error); } }); @@ -794,7 +794,7 @@ public class ConversationActivity extends XmppActivity { announcePgp(getSelectedConversation().getAccount(), getSelectedConversation()); } else if (requestCode == REQUEST_ENCRYPT_MESSAGE) { - encryptTextMessage(); + // encryptTextMessage(); } else if (requestCode == REQUEST_IMAGE_CAPTURE) { attachImageToConversation(getSelectedConversation(), null); } else if (requestCode == REQUEST_RECORD_AUDIO) { @@ -815,26 +815,30 @@ public class ConversationActivity extends XmppActivity { prepareImageToast = Toast.makeText(getApplicationContext(), getText(R.string.preparing_image), Toast.LENGTH_LONG); prepareImageToast.show(); - pendingMessage = xmppConnectionService.attachImageToConversation( - conversation, uri, new UiCallback() { + xmppConnectionService.attachImageToConversation(conversation, uri, + new UiCallback<Message>() { @Override - public void userInputRequried(PendingIntent pi) { + public void userInputRequried(PendingIntent pi, + Message object) { hidePrepareImageToast(); ConversationActivity.this.runIntent(pi, ConversationActivity.REQUEST_SEND_PGP_IMAGE); } @Override - public void success() { - sendPendingImageMessage(); - hidePrepareImageToast(); + public void success(Message message) { + message.getConversation().getMessages().add(message); + xmppConnectionService.databaseBackend + .createMessage(message); + xmppConnectionService.sendMessage(message, null); + xmppConnectionService.updateUi( + message.getConversation(), false); } @Override - public void error(int error) { + public void error(int error, Message message) { hidePrepareImageToast(); - pendingMessage = null; displayErrorDialog(error); } }); @@ -852,14 +856,6 @@ public class ConversationActivity extends XmppActivity { } } - private void sendPendingImageMessage() { - pendingMessage.getConversation().getMessages().add(pendingMessage); - xmppConnectionService.databaseBackend.createMessage(pendingMessage); - xmppConnectionService.sendMessage(pendingMessage, null); - xmppConnectionService.updateUi(pendingMessage.getConversation(), false); - pendingMessage = null; - } - public void updateConversationList() { conversationList.clear(); conversationList.addAll(xmppConnectionService.getConversations()); @@ -1084,29 +1080,24 @@ public class ConversationActivity extends XmppActivity { } } - public void encryptTextMessage() { - xmppConnectionService.getPgpEngine().encrypt(this.pendingMessage, - new UiCallback() { + public void encryptTextMessage(Message message) { + xmppConnectionService.getPgpEngine().encrypt(message, + new UiCallback<Message>() { @Override - public void userInputRequried(PendingIntent pi) { + public void userInputRequried(PendingIntent pi, + Message message) { activity.runIntent(pi, ConversationActivity.REQUEST_SEND_MESSAGE); } @Override - public void success() { - xmppConnectionService.sendMessage(pendingMessage, null); - pendingMessage = null; - ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager() - .findFragmentByTag("conversation"); - if (selectedFragment != null) { - selectedFragment.clearInputField(); - } + public void success(Message message) { + xmppConnectionService.sendMessage(message, null); } @Override - public void error(int error) { + public void error(int error, Message message) { } }); diff --git a/src/eu/siacs/conversations/ui/ConversationFragment.java b/src/eu/siacs/conversations/ui/ConversationFragment.java index 29033891..e976d799 100644 --- a/src/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/eu/siacs/conversations/ui/ConversationFragment.java @@ -621,26 +621,26 @@ public class ConversationFragment extends Fragment { } } - private void decryptMessage(final Message message) { + private void decryptMessage(Message message) { PgpEngine engine = activity.xmppConnectionService.getPgpEngine(); if (engine != null) { - engine.decrypt(message, new UiCallback() { + engine.decrypt(message, new UiCallback<Message>() { @Override - public void userInputRequried(PendingIntent pi) { + public void userInputRequried(PendingIntent pi, Message message) { askForPassphraseIntent = pi.getIntentSender(); pgpInfo.setVisibility(View.VISIBLE); } @Override - public void success() { + public void success(Message message) { activity.xmppConnectionService.databaseBackend .updateMessage(message); updateMessages(); } @Override - public void error(int error) { + public void error(int error, Message message) { message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED); // updateMessages(); } @@ -752,7 +752,6 @@ public class ConversationFragment extends Fragment { } protected void sendPgpMessage(final Message message) { - activity.pendingMessage = message; final ConversationActivity activity = (ConversationActivity) getActivity(); final XmppConnectionService xmppService = activity.xmppConnectionService; final Contact contact = message.getConversation().getContact(); @@ -760,22 +759,22 @@ public class ConversationFragment extends Fragment { if (conversation.getMode() == Conversation.MODE_SINGLE) { if (contact.getPgpKeyId() != 0) { xmppService.getPgpEngine().hasKey(contact, - new UiCallback() { + new UiCallback<Contact>() { @Override - public void userInputRequried(PendingIntent pi) { + public void userInputRequried(PendingIntent pi,Contact contact) { activity.runIntent( pi, ConversationActivity.REQUEST_ENCRYPT_MESSAGE); } @Override - public void success() { - activity.encryptTextMessage(); + public void success(Contact contact) { + activity.encryptTextMessage(message); } @Override - public void error(int error) { + public void error(int error, Contact contact) { } }); @@ -805,7 +804,7 @@ public class ConversationFragment extends Fragment { warning.setGravity(Gravity.CENTER_VERTICAL, 0, 0); warning.show(); } - activity.encryptTextMessage(); + activity.encryptTextMessage(message); } else { showNoPGPKeyDialog(true, new DialogInterface.OnClickListener() { diff --git a/src/eu/siacs/conversations/ui/UiCallback.java b/src/eu/siacs/conversations/ui/UiCallback.java index f9273b96..05a869f7 100644 --- a/src/eu/siacs/conversations/ui/UiCallback.java +++ b/src/eu/siacs/conversations/ui/UiCallback.java @@ -2,8 +2,8 @@ package eu.siacs.conversations.ui; import android.app.PendingIntent; -public interface UiCallback { - public void success(); - public void error(int errorCode); - public void userInputRequried(PendingIntent pi); +public interface UiCallback<T> { + public void success(T object); + public void error(int errorCode, T object); + public void userInputRequried(PendingIntent pi, T object); } diff --git a/src/eu/siacs/conversations/ui/XmppActivity.java b/src/eu/siacs/conversations/ui/XmppActivity.java index 81092b22..ca9ee490 100644 --- a/src/eu/siacs/conversations/ui/XmppActivity.java +++ b/src/eu/siacs/conversations/ui/XmppActivity.java @@ -166,11 +166,11 @@ public abstract class XmppActivity extends Activity { startActivity(viewConversationIntent); } - protected void announcePgp(final Account account, final Conversation conversation) { - xmppConnectionService.getPgpEngine().generateSignature(account, "online", new UiCallback() { + protected void announcePgp(Account account, final Conversation conversation) { + xmppConnectionService.getPgpEngine().generateSignature(account, "online", new UiCallback<Account>() { @Override - public void userInputRequried(PendingIntent pi) { + public void userInputRequried(PendingIntent pi, Account account) { try { startIntentSenderForResult(pi.getIntentSender(), REQUEST_ANNOUNCE_PGP, null, 0, 0, 0); } catch (SendIntentException e) { @@ -179,7 +179,7 @@ public abstract class XmppActivity extends Activity { } @Override - public void success() { + public void success(Account account) { xmppConnectionService.databaseBackend.updateAccount(account); xmppConnectionService.sendPresence(account); if (conversation!=null) { @@ -188,7 +188,7 @@ public abstract class XmppActivity extends Activity { } @Override - public void error(int error) { + public void error(int error, Account account) { displayErrorDialog(error); } }); |