From c555a9d03c25407be43b114d74edd6f2c1587300 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 8 May 2014 17:31:53 +0200 Subject: sending text message after returing from pending intent --- src/eu/siacs/conversations/crypto/PgpEngine.java | 97 ++++++++++------------ .../conversations/ui/ConversationActivity.java | 38 +++++++++ .../conversations/ui/ConversationFragment.java | 35 ++------ 3 files changed, 90 insertions(+), 80 deletions(-) (limited to 'src/eu') diff --git a/src/eu/siacs/conversations/crypto/PgpEngine.java b/src/eu/siacs/conversations/crypto/PgpEngine.java index 5a51a1e8..0e0b33d1 100644 --- a/src/eu/siacs/conversations/crypto/PgpEngine.java +++ b/src/eu/siacs/conversations/crypto/PgpEngine.java @@ -114,59 +114,18 @@ public class PgpEngine { } } - public void encrypt(Account account, final Message message, - final OnPgpEngineResult callback) { + public void encrypt(final Message message,final OnPgpEngineResult callback) { long[] keys = { message.getConversation().getContact().getPgpKeyId() }; Intent params = new Intent(); params.setAction(OpenPgpApi.ACTION_ENCRYPT); params.putExtra(OpenPgpApi.EXTRA_KEY_IDS, keys); - params.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); - params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, account.getJid()); - - 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, - OpenPgpApi.RESULT_CODE_ERROR)) { - case OpenPgpApi.RESULT_CODE_SUCCESS: - StringBuilder encryptedMessageBody = new StringBuilder(); - String[] lines = os.toString().split("\n"); - for (int i = 3; i < lines.length - 1; ++i) { - encryptedMessageBody.append(lines[i].trim()); - } - message.setEncryptedBody(encryptedMessageBody.toString()); - callback.success(); - break; - case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: - callback.userInputRequried((PendingIntent) result - .getParcelableExtra(OpenPgpApi.RESULT_INTENT)); - break; - case OpenPgpApi.RESULT_CODE_ERROR: - callback.error((OpenPgpError) result - .getParcelableExtra(OpenPgpApi.RESULT_ERROR)); - break; - } - } - }); + params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, message.getConversation().getAccount().getJid()); - } + if (message.getType() == Message.TYPE_TEXT) { + params.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); - public void encrypt(final Message message, final OnPgpEngineResult callback) { - try { - Log.d("xmppService","calling to encrypt file"); - JingleFile inputFile = this.mXmppConnectionService.getFileBackend().getJingleFile(message, true); - JingleFile outputFile = this.mXmppConnectionService.getFileBackend().getJingleFile(message, false); - outputFile.createNewFile(); - long[] keys = { message.getConversation().getContact().getPgpKeyId() }; - Intent params = new Intent(); - params.setAction(OpenPgpApi.ACTION_ENCRYPT); - params.putExtra(OpenPgpApi.EXTRA_KEY_IDS, keys); - params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, message.getConversation().getAccount().getJid()); - InputStream is = new FileInputStream(inputFile); - OutputStream os = new FileOutputStream(outputFile); + InputStream is = new ByteArrayInputStream(message.getBody().getBytes()); + final OutputStream os = new ByteArrayOutputStream(); api.executeApiAsync(params, is, os, new IOpenPgpCallback() { @Override @@ -174,6 +133,12 @@ public class PgpEngine { switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { case OpenPgpApi.RESULT_CODE_SUCCESS: + StringBuilder encryptedMessageBody = new StringBuilder(); + String[] lines = os.toString().split("\n"); + for (int i = 3; i < lines.length - 1; ++i) { + encryptedMessageBody.append(lines[i].trim()); + } + message.setEncryptedBody(encryptedMessageBody.toString()); callback.success(); break; case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: @@ -187,13 +152,41 @@ public class PgpEngine { } } }); - } catch (FileNotFoundException e) { - Log.d("xmppService","file not found: "+e.getMessage()); - } catch (IOException e) { - Log.d("xmppService","io exception during file encrypt"); + } else if (message.getType() == Message.TYPE_IMAGE) { + try { + 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(); + break; + case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: + callback.userInputRequried((PendingIntent) result + .getParcelableExtra(OpenPgpApi.RESULT_INTENT)); + break; + case OpenPgpApi.RESULT_CODE_ERROR: + callback.error((OpenPgpError) result + .getParcelableExtra(OpenPgpApi.RESULT_ERROR)); + break; + } + } + }); + } catch (FileNotFoundException e) { + Log.d("xmppService","file not found: "+e.getMessage()); + } catch (IOException e) { + Log.d("xmppService","io exception during file encrypt"); + } } } - + public long fetchKeyId(Account account, String status, String signature) { if ((signature == null) || (api == null)) { return 0; diff --git a/src/eu/siacs/conversations/ui/ConversationActivity.java b/src/eu/siacs/conversations/ui/ConversationActivity.java index a6d1be4e..646eb3c0 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -2,6 +2,7 @@ package eu.siacs.conversations.ui; import java.io.FileNotFoundException; import java.lang.ref.WeakReference; +import java.math.BigInteger; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; @@ -67,6 +68,7 @@ public class ConversationActivity extends XmppActivity { private static final int REQUEST_ATTACH_FILE_DIALOG = 0x48502; private static final int REQUEST_SEND_PGP_IMAGE = 0x53883; private static final int REQUEST_ATTACH_FILE = 0x73824; + public static final int REQUEST_ENCRYPT_MESSAGE = 0x378018; protected SlidingPaneLayout spl; @@ -652,6 +654,10 @@ public class ConversationActivity extends XmppActivity { attachFile(); } else if (requestCode == REQUEST_ANNOUNCE_PGP) { announcePgp(getSelectedConversation().getAccount(),getSelectedConversation()); + } else if (requestCode == REQUEST_ENCRYPT_MESSAGE) { + encryptTextMessage(); + } else { + Log.d(LOGTAG,"unknown result code:"+requestCode); } } } @@ -884,4 +890,36 @@ public class ConversationActivity extends XmppActivity { return bitmapWorkerTaskReference.get(); } } + + public void encryptTextMessage() { + xmppConnectionService.getPgpEngine().encrypt(this.pendingMessage, new OnPgpEngineResult() { + + @Override + public void userInputRequried( + PendingIntent pi) { + 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(); + } + } + + @Override + public void error( + OpenPgpError openPgpError) { + // TODO Auto-generated method + // stub + + } + }); + } } diff --git a/src/eu/siacs/conversations/ui/ConversationFragment.java b/src/eu/siacs/conversations/ui/ConversationFragment.java index e370deb0..e06c9eeb 100644 --- a/src/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/eu/siacs/conversations/ui/ConversationFragment.java @@ -664,10 +664,10 @@ 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(); - final Account account = message.getConversation().getAccount(); if (activity.hasPgp()) { if (contact.getPgpKeyId() != 0) { xmppService.getPgpEngine().hasKey(contact, @@ -677,37 +677,12 @@ public class ConversationFragment extends Fragment { public void userInputRequried(PendingIntent pi) { activity.runIntent( pi, - ConversationActivity.REQUEST_SEND_MESSAGE); + ConversationActivity.REQUEST_ENCRYPT_MESSAGE); } @Override public void success() { - xmppService.getPgpEngine().encrypt(account, - message, new OnPgpEngineResult() { - - @Override - public void userInputRequried( - PendingIntent pi) { - activity.runIntent( - pi, - ConversationActivity.REQUEST_SEND_MESSAGE); - } - - @Override - public void success() { - xmppService.sendMessage( - message, null); - chatMsg.setText(""); - } - - @Override - public void error( - OpenPgpError openPgpError) { - // TODO Auto-generated method - // stub - - } - }); + activity.encryptTextMessage(); } @Override @@ -809,4 +784,8 @@ public class ConversationFragment extends Fragment { public void setText(String text) { this.pastedText = text; } + + public void clearInputField() { + this.chatMsg.setText(""); + } } -- cgit v1.2.3