2014-02-15 19:28:07 +00:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-04-21 09:54:46 +00:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.Rect;
|
2014-02-15 19:28:07 +00:00
|
|
|
import android.os.AsyncTask;
|
2015-04-21 09:54:46 +00:00
|
|
|
import android.provider.ContactsContract;
|
2015-05-20 21:36:30 +00:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2014-02-15 19:28:07 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2014-11-28 18:46:50 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2014-02-15 19:28:07 +00:00
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public class GroupMembersDialog extends AsyncTask<Void, Void, List<Recipient>> {
|
2014-02-15 19:28:07 +00:00
|
|
|
|
2014-11-28 18:46:50 +00:00
|
|
|
private static final String TAG = GroupMembersDialog.class.getSimpleName();
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
private final Recipient recipient;
|
2014-02-15 19:28:07 +00:00
|
|
|
private final Context context;
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public GroupMembersDialog(Context context, Recipient recipient) {
|
|
|
|
this.recipient = recipient;
|
|
|
|
this.context = context;
|
2014-02-15 19:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-05-18 22:48:43 +00:00
|
|
|
public void onPreExecute() {}
|
2014-02-15 19:28:07 +00:00
|
|
|
|
|
|
|
@Override
|
2017-08-01 15:56:00 +00:00
|
|
|
protected List<Recipient> doInBackground(Void... params) {
|
|
|
|
return DatabaseFactory.getGroupDatabase(context).getGroupMembers(recipient.getAddress().toGroupString(), true);
|
2014-02-15 19:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-01 15:56:00 +00:00
|
|
|
public void onPostExecute(List<Recipient> members) {
|
2015-04-21 09:54:46 +00:00
|
|
|
GroupMembers groupMembers = new GroupMembers(members);
|
2015-05-20 21:36:30 +00:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
2015-03-24 12:44:22 +00:00
|
|
|
builder.setTitle(R.string.ConversationActivity_group_members);
|
|
|
|
builder.setIconAttribute(R.attr.group_members_dialog_icon);
|
2014-02-15 19:28:07 +00:00
|
|
|
builder.setCancelable(true);
|
2015-04-21 09:54:46 +00:00
|
|
|
builder.setItems(groupMembers.getRecipientStrings(), new GroupMembersOnClickListener(context, groupMembers));
|
2014-02-15 19:28:07 +00:00
|
|
|
builder.setPositiveButton(android.R.string.ok, null);
|
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void display() {
|
2017-08-01 15:56:00 +00:00
|
|
|
execute();
|
2014-02-15 19:28:07 +00:00
|
|
|
}
|
2014-11-28 18:46:50 +00:00
|
|
|
|
2015-08-12 23:22:08 +00:00
|
|
|
private static class GroupMembersOnClickListener implements DialogInterface.OnClickListener {
|
2015-04-21 09:54:46 +00:00
|
|
|
private final GroupMembers groupMembers;
|
|
|
|
private final Context context;
|
2014-11-28 18:46:50 +00:00
|
|
|
|
2015-04-21 09:54:46 +00:00
|
|
|
public GroupMembersOnClickListener(Context context, GroupMembers members) {
|
|
|
|
this.context = context;
|
|
|
|
this.groupMembers = members;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialogInterface, int item) {
|
|
|
|
Recipient recipient = groupMembers.get(item);
|
|
|
|
|
|
|
|
if (recipient.getContactUri() != null) {
|
|
|
|
ContactsContract.QuickContact.showQuickContact(context, new Rect(0,0,0,0),
|
|
|
|
recipient.getContactUri(),
|
|
|
|
ContactsContract.QuickContact.MODE_LARGE, null);
|
|
|
|
} else {
|
|
|
|
final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
|
2017-07-26 16:59:15 +00:00
|
|
|
if (recipient.getAddress().isEmail()) {
|
|
|
|
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, recipient.getAddress().toEmailString());
|
|
|
|
} else {
|
|
|
|
intent.putExtra(ContactsContract.Intents.Insert.PHONE, recipient.getAddress().toPhoneString());
|
|
|
|
}
|
2015-04-21 09:54:46 +00:00
|
|
|
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
|
|
|
|
context.startActivity(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps a List of Recipient (just like @class Recipients),
|
|
|
|
* but with focus on the order of the Recipients.
|
|
|
|
* So that the order of the RecipientStrings[] matches
|
|
|
|
* the internal order.
|
|
|
|
*
|
|
|
|
* @author Christoph Haefner
|
|
|
|
*/
|
|
|
|
private class GroupMembers {
|
|
|
|
private final String TAG = GroupMembers.class.getSimpleName();
|
|
|
|
|
|
|
|
private final LinkedList<Recipient> members = new LinkedList<>();
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public GroupMembers(List<Recipient> recipients) {
|
|
|
|
for (Recipient recipient : recipients) {
|
2015-04-21 09:54:46 +00:00
|
|
|
if (isLocalNumber(recipient)) {
|
|
|
|
members.push(recipient);
|
|
|
|
} else {
|
|
|
|
members.add(recipient);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String[] getRecipientStrings() {
|
|
|
|
List<String> recipientStrings = new LinkedList<>();
|
|
|
|
|
|
|
|
for (Recipient recipient : members) {
|
|
|
|
if (isLocalNumber(recipient)) {
|
|
|
|
recipientStrings.add(context.getString(R.string.GroupMembersDialog_me));
|
|
|
|
} else {
|
2017-08-16 02:23:42 +00:00
|
|
|
String name = recipient.toShortString();
|
|
|
|
|
|
|
|
if (recipient.getName() == null && recipient.getProfileName() != null) {
|
|
|
|
name += " ~" + recipient.getProfileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
recipientStrings.add(name);
|
2015-04-21 09:54:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return recipientStrings.toArray(new String[members.size()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Recipient get(int index) {
|
|
|
|
return members.get(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isLocalNumber(Recipient recipient) {
|
2017-07-26 16:59:15 +00:00
|
|
|
return Util.isOwnNumber(context, recipient.getAddress());
|
2014-11-28 18:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-15 19:28:07 +00:00
|
|
|
}
|