unified presence selection for otr and file transfer

This commit is contained in:
Daniel Gultsch 2014-04-26 12:40:21 +02:00
parent a55ddb889e
commit f36f218469
4 changed files with 101 additions and 75 deletions

View File

@ -68,4 +68,14 @@
<string name="download_image">Download Image</string> <string name="download_image">Download Image</string>
<string name="error_loading_image">Error loading image (File not found)</string> <string name="error_loading_image">Error loading image (File not found)</string>
<string name="image_offered_for_download"><i>Image file offered for download</i></string> <string name="image_offered_for_download"><i>Image file offered for download</i></string>
<string name="not_connected">Not Connected</string>
<string name="you_are_offline">You have to be online to send %s but your account assoziated with this Conversation is currently offline.</string>
<string name="you_are_offline_blank">You can not perform this action while being offline</string>
<string name="files">files</string>
<string name="otr_messages">OTR encrypted messages</string>
<string name="manage_account">Manage account</string>
<string name="contact_offline">Your Contact is Offline</string>
<string name="contact_offline_otr">Sending OTR encrypted messages to an offline contact is unfortunately not supported.\nWould you like to send the message in plain text?</string>
<string name="contact_offline_file">Sending files to an offline contact is unfortunately not supported.</string>
<string name="send_unencrypted">Send unencrypted</string>
</resources> </resources>

View File

@ -332,7 +332,13 @@ public class ConversationActivity extends XmppActivity {
startActivityForResult(chooser, 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));
@ -589,36 +595,77 @@ public class ConversationActivity extends XmppActivity {
listView.invalidateViews(); listView.invalidateViews();
} }
public void selectPresence(final Conversation conversation, final OnPresenceSelected listener) { public void selectPresence(final Conversation conversation, final OnPresenceSelected listener, String reason) {
Contact contact = conversation.getContact(); Account account = conversation.getAccount();
if (contact==null) { if (account.getStatus() != Account.STATUS_ONLINE) {
showAddToRosterDialog(conversation); AlertDialog.Builder builder = new AlertDialog.Builder(this);
listener.onPresenceSelected(false,null); builder.setTitle(getString(R.string.not_connected));
} else { builder.setIconAttribute(android.R.attr.alertDialogIcon);
Hashtable<String, Integer> presences = contact.getPresences(); if ("otr".equals(reason)) {
if (presences.size() == 0) { builder.setMessage(getString(R.string.you_are_offline,getString(R.string.otr_messages)));
listener.onPresenceSelected(false, null); } else if ("file".equals(reason)) {
} else if (presences.size() == 1) { builder.setMessage(getString(R.string.you_are_offline,getString(R.string.files)));
String presence = (String) presences.keySet().toArray()[0];
conversation.setNextPresence(presence);
listener.onPresenceSelected(true, presence);
} else { } else {
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(getString(R.string.you_are_offline_blank));
builder.setTitle(getString(R.string.choose_presence)); }
final String[] presencesArray = new String[presences.size()]; builder.setNegativeButton(getString(R.string.cancel), null);
presences.keySet().toArray(presencesArray); builder.setPositiveButton(getString(R.string.manage_account), new OnClickListener() {
builder.setItems(presencesArray,
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(activity, ManageAccountActivity.class));
}
});
builder.create().show();
listener.onPresenceSelected(false, null);
} else {
Contact contact = conversation.getContact();
if (contact==null) {
showAddToRosterDialog(conversation);
listener.onPresenceSelected(false,null);
} else {
Hashtable<String, Integer> presences = contact.getPresences();
if (presences.size() == 0) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.contact_offline));
if ("otr".equals(reason)) {
builder.setMessage(getString(R.string.contact_offline_otr));
builder.setPositiveButton(getString(R.string.send_unencrypted), new OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, public void onClick(DialogInterface dialog, int which) {
int which) { listener.onSendPlainTextInstead();
String presence = presencesArray[which];
conversation.setNextPresence(presence);
listener.onPresenceSelected(true,presence);
} }
}); });
builder.create().show(); } else if ("file".equals(reason)) {
builder.setMessage(getString(R.string.contact_offline_file));
}
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setNegativeButton(getString(R.string.cancel), null);
builder.create().show();
listener.onPresenceSelected(false, null);
} else if (presences.size() == 1) {
String presence = (String) presences.keySet().toArray()[0];
conversation.setNextPresence(presence);
listener.onPresenceSelected(true, presence);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.choose_presence));
final String[] presencesArray = new String[presences.size()];
presences.keySet().toArray(presencesArray);
builder.setItems(presencesArray,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
String presence = presencesArray[which];
conversation.setNextPresence(presence);
listener.onPresenceSelected(true,presence);
}
});
builder.create().show();
}
} }
} }
} }

View File

@ -616,55 +616,23 @@ public class ConversationFragment extends Fragment {
activity.xmppConnectionService.sendMessage(message, null); activity.xmppConnectionService.sendMessage(message, null);
chatMsg.setText(""); chatMsg.setText("");
} else { } else {
Hashtable<String, Integer> presences; activity.selectPresence(message.getConversation(), new OnPresenceSelected() {
if (conversation.getContact() != null) {
presences = conversation.getContact().getPresences();
} else {
presences = null;
}
if ((presences == null) || (presences.size() == 0)) {
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setTitle("Contact is offline");
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setMessage("Sending OTR encrypted messages to an offline contact is impossible.");
builder.setPositiveButton("Send plain text",
new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, public void onPresenceSelected(boolean success, String presence) {
int which) { if (success) {
conversation.nextMessageEncryption = Message.ENCRYPTION_NONE; xmppService.sendMessage(message,presence);
message.setEncryption(Message.ENCRYPTION_NONE); chatMsg.setText("");
xmppService.sendMessage(message, null); }
chatMsg.setText(""); }
}
});
builder.setNegativeButton("Cancel", null);
builder.create().show();
} else if (presences.size() == 1) {
xmppService.sendMessage(message, (String) presences.keySet()
.toArray()[0]);
chatMsg.setText("");
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setTitle("Choose Presence");
final String[] presencesArray = new String[presences.size()];
presences.keySet().toArray(presencesArray);
builder.setItems(presencesArray,
new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, public void onSendPlainTextInstead() {
int which) { message.setEncryption(Message.ENCRYPTION_NONE);
xmppService.sendMessage(message, xmppService.sendMessage(message,null);
presencesArray[which]); chatMsg.setText("");
chatMsg.setText(""); }
} },"otr");
});
builder.create().show();
}
} }
} }

View File

@ -2,4 +2,5 @@ package eu.siacs.conversations.ui;
public interface OnPresenceSelected { public interface OnPresenceSelected {
public void onPresenceSelected(boolean success, String presence); public void onPresenceSelected(boolean success, String presence);
public void onSendPlainTextInstead();
} }