From 955b7dbc7e56e2bf631a5ccf10ad306938511af4 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 7 May 2014 12:33:55 +0200 Subject: [PATCH] wee bit of code cleanup --- .../siacs/conversations/crypto/PgpEngine.java | 10 +- .../conversations/entities/Conversation.java | 19 +++- .../services/XmppConnectionService.java | 2 +- .../ui/ConversationActivity.java | 103 ++++++++++++------ .../ui/ConversationFragment.java | 47 ++------ 5 files changed, 104 insertions(+), 77 deletions(-) diff --git a/src/eu/siacs/conversations/crypto/PgpEngine.java b/src/eu/siacs/conversations/crypto/PgpEngine.java index 35247ef2..5a51a1e8 100644 --- a/src/eu/siacs/conversations/crypto/PgpEngine.java +++ b/src/eu/siacs/conversations/crypto/PgpEngine.java @@ -15,6 +15,7 @@ import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpApi.IOpenPgpCallback; import eu.siacs.conversations.entities.Account; +import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.xmpp.jingle.JingleFile; @@ -230,8 +231,11 @@ public class PgpEngine { return 0; } case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: + Log.d("xmppService","openpgp user interaction requeried"); return 0; case OpenPgpApi.RESULT_CODE_ERROR: + Log.d("xmppService","openpgp error: "+((OpenPgpError) result + .getParcelableExtra(OpenPgpApi.RESULT_ERROR)).getMessage()); return 0; } return 0; @@ -272,11 +276,11 @@ public class PgpEngine { }); } - public void hasKey(Account account, long keyId, final OnPgpEngineResult callback) { + public void hasKey(Contact contact, final OnPgpEngineResult callback) { Intent params = new Intent(); params.setAction(OpenPgpApi.ACTION_GET_KEY); - params.putExtra(OpenPgpApi.EXTRA_KEY_ID, keyId); - params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, account.getJid()); + params.putExtra(OpenPgpApi.EXTRA_KEY_ID, contact.getPgpKeyId()); + params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, contact.getAccount().getJid()); InputStream is = new ByteArrayInputStream(new byte[0]); OutputStream os = new ByteArrayOutputStream(); api.executeApiAsync(params, is, os, new IOpenPgpCallback() { diff --git a/src/eu/siacs/conversations/entities/Conversation.java b/src/eu/siacs/conversations/entities/Conversation.java index 0f4d24e5..c02ae9e7 100644 --- a/src/eu/siacs/conversations/entities/Conversation.java +++ b/src/eu/siacs/conversations/entities/Conversation.java @@ -56,7 +56,7 @@ public class Conversation extends AbstractEntity { private transient String otrFingerprint = null; - public int nextMessageEncryption = Message.ENCRYPTION_NONE; + private int nextMessageEncryption = Message.ENCRYPTION_NONE; private transient MucOptions mucOptions = null; @@ -320,4 +320,21 @@ public class Conversation extends AbstractEntity { public String getNextPresence() { return this.nextPresence; } + + public int getLatestEncryption() { + int latestEncryption = this.getLatestMessage().getEncryption(); + if ((latestEncryption == Message.ENCRYPTION_DECRYPTED) || (latestEncryption == Message.ENCRYPTION_DECRYPTION_FAILED)) { + return Message.ENCRYPTION_PGP; + } else { + return latestEncryption; + } + } + + public int getNextEncryption() { + return this.nextMessageEncryption; + } + + public void setNextEncryption(int encryption) { + this.nextMessageEncryption = encryption; + } } diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index a4e8f744..6b67c419 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -326,7 +326,7 @@ public class XmppConnectionService extends Service { msg = ""; } contact.setPgpKeyId(pgp.fetchKeyId(account,msg,x.getContent())); - Log.d("xmppService","fetched key id for "+contact.getDisplayName()+" was:"+contact.getPgpKeyId()); + Log.d("xmppService",account.getJid()+": fetched key id for "+contact.getJid()+" was:"+contact.getPgpKeyId()); } } replaceContactInConversation(account, diff --git a/src/eu/siacs/conversations/ui/ConversationActivity.java b/src/eu/siacs/conversations/ui/ConversationActivity.java index 85025108..fbe2752a 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -320,7 +320,55 @@ public class ConversationActivity extends XmppActivity { } return true; } + + private void attachFileDialog() { + selectPresence(getSelectedConversation(), new OnPresenceSelected() { + + @Override + public void onPresenceSelected(boolean success, String presence) { + if (success) { + Intent attachFileIntent = new Intent(); + attachFileIntent.setType("image/*"); + attachFileIntent.setAction(Intent.ACTION_GET_CONTENT); + Intent chooser = Intent.createChooser(attachFileIntent, getString(R.string.attach_file)); + startActivityForResult(chooser, ATTACH_FILE); + } + } + @Override + public void onSendPlainTextInstead() { + + } + },"file"); + } + + private void attachFile() { + if (getSelectedConversation().getNextEncryption() == Message.ENCRYPTION_PGP) { + if (hasPgp()) { + xmppConnectionService.getPgpEngine().hasKey(getSelectedConversation().getContact(), new OnPgpEngineResult() { + + @Override + public void userInputRequried(PendingIntent pi) { + ConversationActivity.this.runIntent(pi, REQUEST_SEND_PGP_IMAGE); + } + + @Override + public void success() { + attachFileDialog(); + } + + @Override + public void error(OpenPgpError openPgpError) { + // TODO Auto-generated method stub + + } + }); + } + } else if (getSelectedConversation().getNextEncryption() == Message.ENCRYPTION_NONE) { + attachFileDialog(); + } + } + @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { @@ -328,25 +376,7 @@ public class ConversationActivity extends XmppActivity { spl.openPane(); break; case R.id.action_attach_file: - selectPresence(getSelectedConversation(), new OnPresenceSelected() { - - @Override - public void onPresenceSelected(boolean success, String presence) { - if (success) { - Intent attachFileIntent = new Intent(); - attachFileIntent.setType("image/*"); - attachFileIntent.setAction(Intent.ACTION_GET_CONTENT); - Intent chooser = Intent.createChooser(attachFileIntent, getString(R.string.attach_file)); - startActivityForResult(chooser, ATTACH_FILE); - } - } - - @Override - public void onSendPlainTextInstead() { - // TODO Auto-generated method stub - - } - },"file"); + attachFile(); break; case R.id.action_add: startActivity(new Intent(this, ContactsActivity.class)); @@ -391,19 +421,19 @@ public class ConversationActivity extends XmppActivity { public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.encryption_choice_none: - selConv.nextMessageEncryption = Message.ENCRYPTION_NONE; + selConv.setNextEncryption(Message.ENCRYPTION_NONE); item.setChecked(true); break; case R.id.encryption_choice_otr: - selConv.nextMessageEncryption = Message.ENCRYPTION_OTR; + selConv.setNextEncryption(Message.ENCRYPTION_OTR); item.setChecked(true); break; case R.id.encryption_choice_pgp: - selConv.nextMessageEncryption = Message.ENCRYPTION_PGP; + selConv.setNextEncryption(Message.ENCRYPTION_PGP); item.setChecked(true); break; default: - selConv.nextMessageEncryption = Message.ENCRYPTION_NONE; + selConv.setNextEncryption(Message.ENCRYPTION_NONE); break; } fragment.updateChatMsgHint(); @@ -411,7 +441,7 @@ public class ConversationActivity extends XmppActivity { } }); popup.inflate(R.menu.encryption_choices); - switch (selConv.nextMessageEncryption) { + switch (selConv.getNextEncryption()) { case Message.ENCRYPTION_NONE: popup.getMenu().findItem(R.id.encryption_choice_none) .setChecked(true); @@ -591,21 +621,14 @@ public class ConversationActivity extends XmppActivity { } else if (requestCode == ATTACH_FILE) { final Conversation conversation = getSelectedConversation(); String presence = conversation.getNextPresence(); - if (conversation.nextMessageEncryption == Message.ENCRYPTION_NONE) { + if (conversation.getNextEncryption() == Message.ENCRYPTION_NONE) { xmppConnectionService.attachImageToConversation(conversation, presence, data.getData()); - } else if (conversation.nextMessageEncryption == Message.ENCRYPTION_PGP) { + } else if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { pendingMessage = xmppConnectionService.attachEncryptedImageToConversation(conversation, presence, data.getData(), new OnPgpEngineResult() { @Override public void userInputRequried(PendingIntent pi) { - Log.d(LOGTAG,"user input requried"); - try { - startIntentSenderForResult(pi.getIntentSender(), - ConversationActivity.REQUEST_SEND_PGP_IMAGE, null, 0, - 0, 0); - } catch (SendIntentException e1) { - Log.d("xmppService","failed to start intent to send message"); - } + ConversationActivity.this.runIntent(pi, ConversationActivity.REQUEST_SEND_PGP_IMAGE); } @Override @@ -615,6 +638,7 @@ public class ConversationActivity extends XmppActivity { xmppConnectionService.databaseBackend.createMessage(pendingMessage); xmppConnectionService.sendMessage(pendingMessage, null); xmppConnectionService.updateUi(conversation, false); + pendingMessage = null; } @Override @@ -623,7 +647,7 @@ public class ConversationActivity extends XmppActivity { } }); } else { - Log.d(LOGTAG,"unknown next message encryption: "+conversation.nextMessageEncryption); + Log.d(LOGTAG,"unknown next message encryption: "+conversation.getNextEncryption()); } } } @@ -730,6 +754,15 @@ public class ConversationActivity extends XmppActivity { builder.create().show(); } + public void runIntent(PendingIntent pi, int requestCode) { + try { + this.startIntentSenderForResult(pi.getIntentSender(),requestCode, null, 0, + 0, 0); + } catch (SendIntentException e1) { + Log.d("xmppService","failed to start intent to send message"); + } + } + class BitmapWorkerTask extends AsyncTask { private final WeakReference imageViewReference; diff --git a/src/eu/siacs/conversations/ui/ConversationFragment.java b/src/eu/siacs/conversations/ui/ConversationFragment.java index dab03552..af722edc 100644 --- a/src/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/eu/siacs/conversations/ui/ConversationFragment.java @@ -79,10 +79,10 @@ public class ConversationFragment extends Fragment { if (chatMsg.getText().length() < 1) return; Message message = new Message(conversation, chatMsg.getText() - .toString(), conversation.nextMessageEncryption); - if (conversation.nextMessageEncryption == Message.ENCRYPTION_OTR) { + .toString(), conversation.getNextEncryption()); + if (conversation.getNextEncryption() == Message.ENCRYPTION_OTR) { sendOtrMessage(message); - } else if (conversation.nextMessageEncryption == Message.ENCRYPTION_PGP) { + } else if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { sendPgpMessage(message); } else { sendPlainTextMessage(message); @@ -129,7 +129,7 @@ public class ConversationFragment extends Fragment { if (conversation.getMode() == Conversation.MODE_MULTI) { chatMsg.setHint(getString(R.string.send_message_to_conference)); } else { - switch (conversation.nextMessageEncryption) { + switch (conversation.getNextEncryption()) { case Message.ENCRYPTION_NONE: chatMsg.setHint(getString(R.string.send_plain_text_message)); break; @@ -139,9 +139,6 @@ public class ConversationFragment extends Fragment { case Message.ENCRYPTION_PGP: chatMsg.setHint(getString(R.string.send_pgp_message)); break; - case Message.ENCRYPTION_DECRYPTED: - chatMsg.setHint(getString(R.string.send_pgp_message)); - break; default: break; } @@ -441,12 +438,6 @@ public class ConversationFragment extends Fragment { } } - if (queuedPqpMessage != null) { - this.conversation.nextMessageEncryption = Message.ENCRYPTION_PGP; - Message message = new Message(conversation, queuedPqpMessage, - Message.ENCRYPTION_PGP); - sendPgpMessage(message); - } if (conversation.getMode() == Conversation.MODE_MULTI) { activity.xmppConnectionService .setOnRenameListener(new OnRenameListener() { @@ -520,14 +511,8 @@ public class ConversationFragment extends Fragment { this.messageListAdapter.notifyDataSetChanged(); if (conversation.getMode() == Conversation.MODE_SINGLE) { if (messageList.size() >= 1) { - int latestEncryption = this.conversation.getLatestMessage() - .getEncryption(); - if ((latestEncryption == Message.ENCRYPTION_DECRYPTED)||(latestEncryption == Message.ENCRYPTION_DECRYPTION_FAILED)) { - conversation.nextMessageEncryption = Message.ENCRYPTION_PGP; - } else { - conversation.nextMessageEncryption = latestEncryption; - } - makeFingerprintWarning(latestEncryption); + conversation.setNextEncryption(conversation.getLatestEncryption()); + makeFingerprintWarning(conversation.getLatestEncryption()); } } else { if (conversation.getMucOptions().getError() != 0) { @@ -600,17 +585,11 @@ public class ConversationFragment extends Fragment { final Account account = message.getConversation().getAccount(); if (activity.hasPgp()) { if (contact.getPgpKeyId() != 0) { - xmppService.getPgpEngine().hasKey(account,contact.getPgpKeyId(), new OnPgpEngineResult() { + xmppService.getPgpEngine().hasKey(contact, new OnPgpEngineResult() { @Override public void userInputRequried(PendingIntent pi) { - try { - getActivity().startIntentSenderForResult(pi.getIntentSender(), - ConversationActivity.REQUEST_SEND_MESSAGE, null, 0, - 0, 0); - } catch (SendIntentException e1) { - Log.d("xmppService","failed to start intent to send message"); - } + activity.runIntent(pi, ConversationActivity.REQUEST_SEND_MESSAGE); } @Override @@ -619,13 +598,7 @@ public class ConversationFragment extends Fragment { @Override public void userInputRequried(PendingIntent pi) { - try { - activity.startIntentSenderForResult(pi.getIntentSender(), - ConversationActivity.REQUEST_SEND_MESSAGE, null, 0, - 0, 0); - } catch (SendIntentException e1) { - Log.d("xmppService","failed to start intent to send message"); - } + activity.runIntent(pi, ConversationActivity.REQUEST_SEND_MESSAGE); } @Override @@ -664,7 +637,7 @@ public class ConversationFragment extends Fragment { @Override public void onClick(DialogInterface dialog, int which) { - conversation.nextMessageEncryption = Message.ENCRYPTION_NONE; + conversation.setNextEncryption(Message.ENCRYPTION_NONE); message.setEncryption(Message.ENCRYPTION_NONE); xmppService.sendMessage(message, null); chatMsg.setText("");