streamlined onpresenceselected listener

This commit is contained in:
iNPUTmice 2014-06-16 14:21:22 +02:00
parent 30a0be2998
commit 7e8c68b698
5 changed files with 106 additions and 124 deletions

View file

@ -6,11 +6,9 @@ import java.util.ArrayList;
import java.util.List;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.services.ImageProvider;
import eu.siacs.conversations.utils.ExceptionHelper;
import eu.siacs.conversations.utils.UIHelper;
@ -345,39 +343,29 @@ public class ConversationActivity extends XmppActivity {
selectPresence(getSelectedConversation(), new OnPresenceSelected() {
@Override
public void onPresenceSelected(boolean success, String presence) {
if (success) {
if (attachmentChoice == ATTACHMENT_CHOICE_TAKE_PHOTO) {
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);
}
} else if (attachmentChoice == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
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,
REQUEST_ATTACH_FILE_DIALOG);
} else if (attachmentChoice == ATTACHMENT_CHOICE_RECORD_VOICE) {
Intent intent = new Intent(
MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, REQUEST_RECORD_AUDIO);
public void onPresenceSelected() {
if (attachmentChoice == ATTACHMENT_CHOICE_TAKE_PHOTO) {
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);
}
} else if (attachmentChoice == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
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, REQUEST_ATTACH_FILE_DIALOG);
} else if (attachmentChoice == ATTACHMENT_CHOICE_RECORD_VOICE) {
Intent intent = new Intent(
MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, REQUEST_RECORD_AUDIO);
}
}
@Override
public void onSendPlainTextInstead() {
// TODO Auto-generated method stub
}
});
}
@ -851,59 +839,6 @@ public class ConversationActivity extends XmppActivity {
listView.invalidateViews();
}
public void selectPresence(final Conversation conversation,
final OnPresenceSelected listener) {
Contact contact = conversation.getContact();
if (contact == null) {
showAddToRosterDialog(conversation);
listener.onPresenceSelected(false, null);
} else {
Presences presences = contact.getPresences();
if (presences.size() == 0) {
conversation.setNextPresence(null);
listener.onPresenceSelected(true, null);
} else if (presences.size() == 1) {
String presence = (String) presences.asStringArray()[0];
conversation.setNextPresence(presence);
listener.onPresenceSelected(true, presence);
} else {
final StringBuilder presence = new StringBuilder();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.choose_presence));
final String[] presencesArray = presences.asStringArray();
int preselectedPresence = 0;
for (int i = 0; i < presencesArray.length; ++i) {
if (presencesArray[i].equals(contact.lastseen.presence)) {
preselectedPresence = i;
break;
}
}
presence.append(presencesArray[preselectedPresence]);
builder.setSingleChoiceItems(presencesArray,
preselectedPresence,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
presence.delete(0, presence.length());
presence.append(presencesArray[which]);
}
});
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
conversation.setNextPresence(presence.toString());
listener.onPresenceSelected(true, presence.toString());
}
});
builder.create().show();
}
}
}
public boolean showLastseen() {
if (getSelectedConversation() == null) {
return false;
@ -913,27 +848,6 @@ public class ConversationActivity extends XmppActivity {
}
}
private void showAddToRosterDialog(final Conversation conversation) {
String jid = conversation.getContactJid();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(jid);
builder.setMessage(getString(R.string.not_in_roster));
builder.setNegativeButton(getString(R.string.cancel), null);
builder.setPositiveButton(getString(R.string.add_contact),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String jid = conversation.getContactJid();
Account account = getSelectedConversation()
.getAccount();
Contact contact = account.getRoster().getContact(jid);
xmppConnectionService.createContact(contact);
}
});
builder.create().show();
}
public void runIntent(PendingIntent pi, int requestCode) {
try {
this.startIntentSenderForResult(pi.getIntentSender(), requestCode,

View file

@ -925,18 +925,8 @@ public class ConversationFragment extends Fragment {
new OnPresenceSelected() {
@Override
public void onPresenceSelected(boolean success,
String presence) {
if (success) {
message.setPresence(presence);
xmppService.sendMessage(message);
messageSent();
}
}
@Override
public void onSendPlainTextInstead() {
message.setEncryption(Message.ENCRYPTION_NONE);
public void onPresenceSelected() {
message.setPresence(conversation.getNextPresence());
xmppService.sendMessage(message);
messageSent();
}

View file

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

View file

@ -155,12 +155,17 @@ public class ShareWithActivity extends XmppActivity {
}
}
private void share(Conversation conversation) {
private void share(final Conversation conversation) {
String sharedText = null;
if (isImage) {
Uri uri = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
Log.d(LOGTAG,uri.toString());
ShareWithActivity.this.xmppConnectionService.attachImageToConversation(conversation, uri,attachImageCallback);
final Uri uri = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
selectPresence(conversation, new OnPresenceSelected() {
@Override
public void onPresenceSelected() {
ShareWithActivity.this.xmppConnectionService.attachImageToConversation(conversation, uri,attachImageCallback);
}
});
} else {
sharedText = getIntent().getStringExtra(
Intent.EXTRA_TEXT);

View file

@ -2,8 +2,10 @@ package eu.siacs.conversations.ui;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder;
import eu.siacs.conversations.utils.ExceptionHelper;
@ -219,4 +221,76 @@ public abstract class XmppActivity extends Activity {
});
}
protected void showAddToRosterDialog(final Conversation conversation) {
String jid = conversation.getContactJid();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(jid);
builder.setMessage(getString(R.string.not_in_roster));
builder.setNegativeButton(getString(R.string.cancel), null);
builder.setPositiveButton(getString(R.string.add_contact),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String jid = conversation.getContactJid();
Account account = conversation.getAccount();
Contact contact = account.getRoster().getContact(jid);
xmppConnectionService.createContact(contact);
}
});
builder.create().show();
}
public void selectPresence(final Conversation conversation,
final OnPresenceSelected listener) {
Contact contact = conversation.getContact();
if (contact == null) {
showAddToRosterDialog(conversation);
} else {
Presences presences = contact.getPresences();
if (presences.size() == 0) {
conversation.setNextPresence(null);
listener.onPresenceSelected();
} else if (presences.size() == 1) {
String presence = (String) presences.asStringArray()[0];
conversation.setNextPresence(presence);
listener.onPresenceSelected();
} else {
final StringBuilder presence = new StringBuilder();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.choose_presence));
final String[] presencesArray = presences.asStringArray();
int preselectedPresence = 0;
for (int i = 0; i < presencesArray.length; ++i) {
if (presencesArray[i].equals(contact.lastseen.presence)) {
preselectedPresence = i;
break;
}
}
presence.append(presencesArray[preselectedPresence]);
builder.setSingleChoiceItems(presencesArray,
preselectedPresence,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
presence.delete(0, presence.length());
presence.append(presencesArray[which]);
}
});
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
conversation.setNextPresence(presence.toString());
listener.onPresenceSelected();
}
});
builder.create().show();
}
}
}
}