wee bit of code cleanup

This commit is contained in:
Daniel Gultsch 2014-05-07 12:33:55 +02:00
parent 6b20a213b0
commit 955b7dbc7e
5 changed files with 104 additions and 77 deletions

View file

@ -15,6 +15,7 @@ import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpApi.IOpenPgpCallback; import org.openintents.openpgp.util.OpenPgpApi.IOpenPgpCallback;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xmpp.jingle.JingleFile; import eu.siacs.conversations.xmpp.jingle.JingleFile;
@ -230,8 +231,11 @@ public class PgpEngine {
return 0; return 0;
} }
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
Log.d("xmppService","openpgp user interaction requeried");
return 0; return 0;
case OpenPgpApi.RESULT_CODE_ERROR: case OpenPgpApi.RESULT_CODE_ERROR:
Log.d("xmppService","openpgp error: "+((OpenPgpError) result
.getParcelableExtra(OpenPgpApi.RESULT_ERROR)).getMessage());
return 0; return 0;
} }
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(); Intent params = new Intent();
params.setAction(OpenPgpApi.ACTION_GET_KEY); params.setAction(OpenPgpApi.ACTION_GET_KEY);
params.putExtra(OpenPgpApi.EXTRA_KEY_ID, keyId); params.putExtra(OpenPgpApi.EXTRA_KEY_ID, contact.getPgpKeyId());
params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, account.getJid()); params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, contact.getAccount().getJid());
InputStream is = new ByteArrayInputStream(new byte[0]); InputStream is = new ByteArrayInputStream(new byte[0]);
OutputStream os = new ByteArrayOutputStream(); OutputStream os = new ByteArrayOutputStream();
api.executeApiAsync(params, is, os, new IOpenPgpCallback() { api.executeApiAsync(params, is, os, new IOpenPgpCallback() {

View file

@ -56,7 +56,7 @@ public class Conversation extends AbstractEntity {
private transient String otrFingerprint = null; private transient String otrFingerprint = null;
public int nextMessageEncryption = Message.ENCRYPTION_NONE; private int nextMessageEncryption = Message.ENCRYPTION_NONE;
private transient MucOptions mucOptions = null; private transient MucOptions mucOptions = null;
@ -320,4 +320,21 @@ public class Conversation extends AbstractEntity {
public String getNextPresence() { public String getNextPresence() {
return this.nextPresence; 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;
}
} }

View file

@ -326,7 +326,7 @@ public class XmppConnectionService extends Service {
msg = ""; msg = "";
} }
contact.setPgpKeyId(pgp.fetchKeyId(account,msg,x.getContent())); 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, replaceContactInConversation(account,

View file

@ -320,7 +320,55 @@ public class ConversationActivity extends XmppActivity {
} }
return true; 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 @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
@ -328,25 +376,7 @@ public class ConversationActivity extends XmppActivity {
spl.openPane(); spl.openPane();
break; break;
case R.id.action_attach_file: case R.id.action_attach_file:
selectPresence(getSelectedConversation(), new OnPresenceSelected() { attachFile();
@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");
break; break;
case R.id.action_add: case R.id.action_add:
startActivity(new Intent(this, ContactsActivity.class)); startActivity(new Intent(this, ContactsActivity.class));
@ -391,19 +421,19 @@ public class ConversationActivity extends XmppActivity {
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.encryption_choice_none: case R.id.encryption_choice_none:
selConv.nextMessageEncryption = Message.ENCRYPTION_NONE; selConv.setNextEncryption(Message.ENCRYPTION_NONE);
item.setChecked(true); item.setChecked(true);
break; break;
case R.id.encryption_choice_otr: case R.id.encryption_choice_otr:
selConv.nextMessageEncryption = Message.ENCRYPTION_OTR; selConv.setNextEncryption(Message.ENCRYPTION_OTR);
item.setChecked(true); item.setChecked(true);
break; break;
case R.id.encryption_choice_pgp: case R.id.encryption_choice_pgp:
selConv.nextMessageEncryption = Message.ENCRYPTION_PGP; selConv.setNextEncryption(Message.ENCRYPTION_PGP);
item.setChecked(true); item.setChecked(true);
break; break;
default: default:
selConv.nextMessageEncryption = Message.ENCRYPTION_NONE; selConv.setNextEncryption(Message.ENCRYPTION_NONE);
break; break;
} }
fragment.updateChatMsgHint(); fragment.updateChatMsgHint();
@ -411,7 +441,7 @@ public class ConversationActivity extends XmppActivity {
} }
}); });
popup.inflate(R.menu.encryption_choices); popup.inflate(R.menu.encryption_choices);
switch (selConv.nextMessageEncryption) { switch (selConv.getNextEncryption()) {
case Message.ENCRYPTION_NONE: case Message.ENCRYPTION_NONE:
popup.getMenu().findItem(R.id.encryption_choice_none) popup.getMenu().findItem(R.id.encryption_choice_none)
.setChecked(true); .setChecked(true);
@ -591,21 +621,14 @@ public class ConversationActivity extends XmppActivity {
} else if (requestCode == ATTACH_FILE) { } else if (requestCode == ATTACH_FILE) {
final Conversation conversation = getSelectedConversation(); final Conversation conversation = getSelectedConversation();
String presence = conversation.getNextPresence(); String presence = conversation.getNextPresence();
if (conversation.nextMessageEncryption == Message.ENCRYPTION_NONE) { if (conversation.getNextEncryption() == Message.ENCRYPTION_NONE) {
xmppConnectionService.attachImageToConversation(conversation, presence, data.getData()); 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() { pendingMessage = xmppConnectionService.attachEncryptedImageToConversation(conversation, presence, data.getData(), new OnPgpEngineResult() {
@Override @Override
public void userInputRequried(PendingIntent pi) { public void userInputRequried(PendingIntent pi) {
Log.d(LOGTAG,"user input requried"); ConversationActivity.this.runIntent(pi, ConversationActivity.REQUEST_SEND_PGP_IMAGE);
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");
}
} }
@Override @Override
@ -615,6 +638,7 @@ public class ConversationActivity extends XmppActivity {
xmppConnectionService.databaseBackend.createMessage(pendingMessage); xmppConnectionService.databaseBackend.createMessage(pendingMessage);
xmppConnectionService.sendMessage(pendingMessage, null); xmppConnectionService.sendMessage(pendingMessage, null);
xmppConnectionService.updateUi(conversation, false); xmppConnectionService.updateUi(conversation, false);
pendingMessage = null;
} }
@Override @Override
@ -623,7 +647,7 @@ public class ConversationActivity extends XmppActivity {
} }
}); });
} else { } 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(); 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<Message, Void, Bitmap> { class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference; private final WeakReference<ImageView> imageViewReference;

View file

@ -79,10 +79,10 @@ public class ConversationFragment extends Fragment {
if (chatMsg.getText().length() < 1) if (chatMsg.getText().length() < 1)
return; return;
Message message = new Message(conversation, chatMsg.getText() Message message = new Message(conversation, chatMsg.getText()
.toString(), conversation.nextMessageEncryption); .toString(), conversation.getNextEncryption());
if (conversation.nextMessageEncryption == Message.ENCRYPTION_OTR) { if (conversation.getNextEncryption() == Message.ENCRYPTION_OTR) {
sendOtrMessage(message); sendOtrMessage(message);
} else if (conversation.nextMessageEncryption == Message.ENCRYPTION_PGP) { } else if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
sendPgpMessage(message); sendPgpMessage(message);
} else { } else {
sendPlainTextMessage(message); sendPlainTextMessage(message);
@ -129,7 +129,7 @@ public class ConversationFragment extends Fragment {
if (conversation.getMode() == Conversation.MODE_MULTI) { if (conversation.getMode() == Conversation.MODE_MULTI) {
chatMsg.setHint(getString(R.string.send_message_to_conference)); chatMsg.setHint(getString(R.string.send_message_to_conference));
} else { } else {
switch (conversation.nextMessageEncryption) { switch (conversation.getNextEncryption()) {
case Message.ENCRYPTION_NONE: case Message.ENCRYPTION_NONE:
chatMsg.setHint(getString(R.string.send_plain_text_message)); chatMsg.setHint(getString(R.string.send_plain_text_message));
break; break;
@ -139,9 +139,6 @@ public class ConversationFragment extends Fragment {
case Message.ENCRYPTION_PGP: case Message.ENCRYPTION_PGP:
chatMsg.setHint(getString(R.string.send_pgp_message)); chatMsg.setHint(getString(R.string.send_pgp_message));
break; break;
case Message.ENCRYPTION_DECRYPTED:
chatMsg.setHint(getString(R.string.send_pgp_message));
break;
default: default:
break; 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) { if (conversation.getMode() == Conversation.MODE_MULTI) {
activity.xmppConnectionService activity.xmppConnectionService
.setOnRenameListener(new OnRenameListener() { .setOnRenameListener(new OnRenameListener() {
@ -520,14 +511,8 @@ public class ConversationFragment extends Fragment {
this.messageListAdapter.notifyDataSetChanged(); this.messageListAdapter.notifyDataSetChanged();
if (conversation.getMode() == Conversation.MODE_SINGLE) { if (conversation.getMode() == Conversation.MODE_SINGLE) {
if (messageList.size() >= 1) { if (messageList.size() >= 1) {
int latestEncryption = this.conversation.getLatestMessage() conversation.setNextEncryption(conversation.getLatestEncryption());
.getEncryption(); makeFingerprintWarning(conversation.getLatestEncryption());
if ((latestEncryption == Message.ENCRYPTION_DECRYPTED)||(latestEncryption == Message.ENCRYPTION_DECRYPTION_FAILED)) {
conversation.nextMessageEncryption = Message.ENCRYPTION_PGP;
} else {
conversation.nextMessageEncryption = latestEncryption;
}
makeFingerprintWarning(latestEncryption);
} }
} else { } else {
if (conversation.getMucOptions().getError() != 0) { if (conversation.getMucOptions().getError() != 0) {
@ -600,17 +585,11 @@ public class ConversationFragment extends Fragment {
final Account account = message.getConversation().getAccount(); final Account account = message.getConversation().getAccount();
if (activity.hasPgp()) { if (activity.hasPgp()) {
if (contact.getPgpKeyId() != 0) { if (contact.getPgpKeyId() != 0) {
xmppService.getPgpEngine().hasKey(account,contact.getPgpKeyId(), new OnPgpEngineResult() { xmppService.getPgpEngine().hasKey(contact, new OnPgpEngineResult() {
@Override @Override
public void userInputRequried(PendingIntent pi) { public void userInputRequried(PendingIntent pi) {
try { activity.runIntent(pi, ConversationActivity.REQUEST_SEND_MESSAGE);
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");
}
} }
@Override @Override
@ -619,13 +598,7 @@ public class ConversationFragment extends Fragment {
@Override @Override
public void userInputRequried(PendingIntent pi) { public void userInputRequried(PendingIntent pi) {
try { activity.runIntent(pi, ConversationActivity.REQUEST_SEND_MESSAGE);
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");
}
} }
@Override @Override
@ -664,7 +637,7 @@ public class ConversationFragment extends Fragment {
@Override @Override
public void onClick(DialogInterface dialog, public void onClick(DialogInterface dialog,
int which) { int which) {
conversation.nextMessageEncryption = Message.ENCRYPTION_NONE; conversation.setNextEncryption(Message.ENCRYPTION_NONE);
message.setEncryption(Message.ENCRYPTION_NONE); message.setEncryption(Message.ENCRYPTION_NONE);
xmppService.sendMessage(message, null); xmppService.sendMessage(message, null);
chatMsg.setText(""); chatMsg.setText("");