code clean up

This commit is contained in:
Daniel Gultsch 2014-05-14 11:49:43 +02:00
parent 65e7294802
commit 1f600be4dd
1 changed files with 51 additions and 104 deletions

View File

@ -67,9 +67,11 @@ public class ConversationActivity extends XmppActivity {
private static final int REQUEST_ATTACH_FILE_DIALOG = 0x48502; private static final int REQUEST_ATTACH_FILE_DIALOG = 0x48502;
private static final int REQUEST_IMAGE_CAPTURE = 0x33788; private static final int REQUEST_IMAGE_CAPTURE = 0x33788;
private static final int REQUEST_SEND_PGP_IMAGE = 0x53883; 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; public static final int REQUEST_ENCRYPT_MESSAGE = 0x378018;
private static final int ATTACHMENT_CHOICE_CHOOSE_IMAGE = 0x92734;
private static final int ATTACHMENT_CHOICE_TAKE_PHOTO = 0x84123;
protected SlidingPaneLayout spl; protected SlidingPaneLayout spl;
private List<Conversation> conversationList = new ArrayList<Conversation>(); private List<Conversation> conversationList = new ArrayList<Conversation>();
@ -332,37 +334,24 @@ public class ConversationActivity extends XmppActivity {
return true; return true;
} }
private void attachFileDialog() { private void selectPresenceToAttachFile(final int attachmentChoice) {
selectPresence(getSelectedConversation(), new OnPresenceSelected() { selectPresence(getSelectedConversation(), new OnPresenceSelected() {
@Override @Override
public void onPresenceSelected(boolean success, String presence) { public void onPresenceSelected(boolean success, String presence) {
if (success) { if (success) {
Intent attachFileIntent = new Intent(); if (attachmentChoice==ATTACHMENT_CHOICE_TAKE_PHOTO) {
attachFileIntent.setType("image/*"); Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
attachFileIntent.setAction(Intent.ACTION_GET_CONTENT); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, ImageProvider.getIncomingContentUri());
Intent chooser = Intent.createChooser(attachFileIntent, getString(R.string.attach_file)); if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(chooser, REQUEST_ATTACH_FILE_DIALOG); startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
} }
} } else if (attachmentChoice==ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
Intent attachFileIntent = new Intent();
@Override attachFileIntent.setType("image/*");
public void onSendPlainTextInstead() { attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent chooser = Intent.createChooser(attachFileIntent, getString(R.string.attach_file));
} startActivityForResult(chooser, REQUEST_ATTACH_FILE_DIALOG);
},"file");
}
private void takePicture() {
selectPresence(getSelectedConversation(), new OnPresenceSelected() {
@Override
public void onPresenceSelected(boolean success, String presence) {
if (success) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, ImageProvider.getIncomingContentUri());
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
} }
} }
} }
@ -373,10 +362,9 @@ public class ConversationActivity extends XmppActivity {
} }
},"file"); },"file");
} }
private void attachFile() { private void attachFile(final int attachmentChoice) {
final Conversation conversation = getSelectedConversation(); final Conversation conversation = getSelectedConversation();
if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
if (hasPgp()) { if (hasPgp()) {
@ -385,12 +373,12 @@ public class ConversationActivity extends XmppActivity {
@Override @Override
public void userInputRequried(PendingIntent pi) { public void userInputRequried(PendingIntent pi) {
ConversationActivity.this.runIntent(pi, REQUEST_ATTACH_FILE); ConversationActivity.this.runIntent(pi, attachmentChoice);
} }
@Override @Override
public void success() { public void success() {
attachFileDialog(); selectPresenceToAttachFile(attachmentChoice);
} }
@Override @Override
@ -408,14 +396,14 @@ public class ConversationActivity extends XmppActivity {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
conversation.setNextEncryption(Message.ENCRYPTION_NONE); conversation.setNextEncryption(Message.ENCRYPTION_NONE);
attachFileDialog(); selectPresenceToAttachFile(attachmentChoice);
} }
}); });
} }
} }
} }
} else if (getSelectedConversation().getNextEncryption() == Message.ENCRYPTION_NONE) { } else if (getSelectedConversation().getNextEncryption() == Message.ENCRYPTION_NONE) {
attachFileDialog(); selectPresenceToAttachFile(attachmentChoice);
} else { } else {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.otr_file_transfer)); builder.setTitle(getString(R.string.otr_file_transfer));
@ -427,7 +415,7 @@ public class ConversationActivity extends XmppActivity {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
conversation.setNextEncryption(Message.ENCRYPTION_NONE); conversation.setNextEncryption(Message.ENCRYPTION_NONE);
attachFile(); attachFile(attachmentChoice);
} }
}); });
} else { } else {
@ -436,7 +424,7 @@ public class ConversationActivity extends XmppActivity {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
conversation.setNextEncryption(Message.ENCRYPTION_PGP); conversation.setNextEncryption(Message.ENCRYPTION_PGP);
attachFile(); attachFile(attachmentChoice);
} }
}); });
} }
@ -460,10 +448,10 @@ public class ConversationActivity extends XmppActivity {
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.attach_choose_picture: case R.id.attach_choose_picture:
attachFile(); attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
break; break;
case R.id.attach_take_picture: case R.id.attach_take_picture:
takePicture(); attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
break; break;
} }
return false; return false;
@ -714,87 +702,46 @@ public class ConversationActivity extends XmppActivity {
selectedFragment.hidePgpPassphraseBox(); selectedFragment.hidePgpPassphraseBox();
} }
} else if (requestCode == REQUEST_ATTACH_FILE_DIALOG) { } else if (requestCode == REQUEST_ATTACH_FILE_DIALOG) {
prepareImageToast = Toast.makeText(getApplicationContext(), getText(R.string.preparing_image), Toast.LENGTH_LONG); attachImageToConversation(getSelectedConversation(),data.getData());
final Conversation conversation = getSelectedConversation();
if (conversation.getNextEncryption() == Message.ENCRYPTION_NONE) {
prepareImageToast.show();
this.pendingMessage = xmppConnectionService.attachImageToConversation(conversation, data.getData(),new UiCallback() {
@Override
public void userInputRequried(PendingIntent pi) {
}
@Override
public void success() {
sendPendingImageMessage();
}
@Override
public void error(int error) {
pendingMessage = null;
displayErrorDialog(error);
}
});
} else if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
prepareImageToast.show();
attachPgpFile(conversation,data.getData());
} else {
Log.d(LOGTAG,"unknown next message encryption: "+conversation.getNextEncryption());
}
} else if (requestCode == REQUEST_SEND_PGP_IMAGE) { } else if (requestCode == REQUEST_SEND_PGP_IMAGE) {
} else if (requestCode == REQUEST_ATTACH_FILE) { } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
attachFile(); attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
} else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
} else if (requestCode == REQUEST_ANNOUNCE_PGP) { } else if (requestCode == REQUEST_ANNOUNCE_PGP) {
announcePgp(getSelectedConversation().getAccount(),getSelectedConversation()); announcePgp(getSelectedConversation().getAccount(),getSelectedConversation());
} else if (requestCode == REQUEST_ENCRYPT_MESSAGE) { } else if (requestCode == REQUEST_ENCRYPT_MESSAGE) {
encryptTextMessage(); encryptTextMessage();
} else if (requestCode == REQUEST_IMAGE_CAPTURE) { } else if (requestCode == REQUEST_IMAGE_CAPTURE) {
this.pendingMessage = xmppConnectionService.attachImageToConversation(getSelectedConversation(), null, new UiCallback() { attachImageToConversation(getSelectedConversation(), null);
@Override
public void userInputRequried(PendingIntent pi) {
// TODO Auto-generated method stub
}
@Override
public void success() {
sendPendingImageMessage();
}
@Override
public void error(int errorCode) {
// TODO Auto-generated method stub
}
});
} else { } else {
Log.d(LOGTAG,"unknown result code:"+requestCode); Log.d(LOGTAG,"unknown result code:"+requestCode);
} }
} }
} }
private void attachPgpFile(Conversation conversation, Uri uri) { private void attachImageToConversation(Conversation conversation, Uri uri) {
pendingMessage = xmppConnectionService.attachImageToConversation(conversation, uri, new UiCallback() { prepareImageToast = Toast.makeText(getApplicationContext(), getText(R.string.preparing_image), Toast.LENGTH_LONG);
prepareImageToast.show();
pendingMessage = xmppConnectionService.attachImageToConversation(conversation, uri, new UiCallback() {
@Override @Override
public void userInputRequried(PendingIntent pi) { public void userInputRequried(PendingIntent pi) {
ConversationActivity.this.runIntent(pi, ConversationActivity.REQUEST_SEND_PGP_IMAGE); ConversationActivity.this.runIntent(pi, ConversationActivity.REQUEST_SEND_PGP_IMAGE);
} }
@Override @Override
public void success() { public void success() {
sendPendingImageMessage(); sendPendingImageMessage();
} }
@Override @Override
public void error(int error) { public void error(int error) {
pendingMessage = null; pendingMessage = null;
displayErrorDialog(error); displayErrorDialog(error);
} }
}); });
} }
private void sendPendingImageMessage() { private void sendPendingImageMessage() {