2014-02-15 20:28:07 +01:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-04-21 11:54:46 +02:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.Rect;
|
2014-02-15 20:28:07 +01:00
|
|
|
import android.os.AsyncTask;
|
2015-04-21 11:54:46 +02:00
|
|
|
import android.provider.ContactsContract;
|
2014-02-15 20:28:07 +01:00
|
|
|
import android.util.Log;
|
|
|
|
|
2015-03-24 13:44:22 +01:00
|
|
|
import com.afollestad.materialdialogs.AlertDialogWrapper;
|
|
|
|
|
2014-02-15 20:28:07 +01:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2015-06-09 16:37:20 +02:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
2014-02-15 20:28:07 +01:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
2014-11-28 19:46:50 +01:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
2014-02-15 20:28:07 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class GroupMembersDialog extends AsyncTask<Void, Void, Recipients> {
|
|
|
|
|
2014-11-28 19:46:50 +01:00
|
|
|
private static final String TAG = GroupMembersDialog.class.getSimpleName();
|
|
|
|
|
2014-02-15 20:28:07 +01:00
|
|
|
private final Recipients recipients;
|
|
|
|
private final Context context;
|
|
|
|
|
|
|
|
public GroupMembersDialog(Context context, Recipients recipients) {
|
|
|
|
this.recipients = recipients;
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-05-19 00:48:43 +02:00
|
|
|
public void onPreExecute() {}
|
2014-02-15 20:28:07 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Recipients doInBackground(Void... params) {
|
|
|
|
try {
|
|
|
|
String groupId = recipients.getPrimaryRecipient().getNumber();
|
|
|
|
return DatabaseFactory.getGroupDatabase(context)
|
2014-02-24 09:19:54 +01:00
|
|
|
.getGroupMembers(GroupUtil.getDecodedId(groupId), true);
|
2014-02-15 20:28:07 +01:00
|
|
|
} catch (IOException e) {
|
2015-04-21 11:54:46 +02:00
|
|
|
Log.w(TAG, e);
|
2015-06-09 16:37:20 +02:00
|
|
|
return RecipientFactory.getRecipientsFor(context, new LinkedList<Recipient>(), true);
|
2014-02-15 20:28:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPostExecute(Recipients members) {
|
2015-04-21 11:54:46 +02:00
|
|
|
GroupMembers groupMembers = new GroupMembers(members);
|
2015-03-24 13:44:22 +01:00
|
|
|
AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(context);
|
|
|
|
builder.setTitle(R.string.ConversationActivity_group_members);
|
|
|
|
builder.setIconAttribute(R.attr.group_members_dialog_icon);
|
2014-02-15 20:28:07 +01:00
|
|
|
builder.setCancelable(true);
|
2015-04-21 11:54:46 +02:00
|
|
|
builder.setItems(groupMembers.getRecipientStrings(), new GroupMembersOnClickListener(context, groupMembers));
|
2014-02-15 20:28:07 +01:00
|
|
|
builder.setPositiveButton(android.R.string.ok, null);
|
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void display() {
|
|
|
|
if (recipients.isGroupRecipient()) execute();
|
|
|
|
else onPostExecute(recipients);
|
|
|
|
}
|
2014-11-28 19:46:50 +01:00
|
|
|
|
2015-04-21 11:54:46 +02:00
|
|
|
private class GroupMembersOnClickListener implements DialogInterface.OnClickListener {
|
|
|
|
private final GroupMembers groupMembers;
|
|
|
|
private final Context context;
|
2014-11-28 19:46:50 +01:00
|
|
|
|
2015-04-21 11:54:46 +02: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);
|
|
|
|
intent.putExtra(ContactsContract.Intents.Insert.PHONE, recipient.getNumber());
|
|
|
|
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<>();
|
|
|
|
|
|
|
|
public GroupMembers(Recipients recipients) {
|
|
|
|
for (Recipient recipient : recipients.getRecipientsList()) {
|
|
|
|
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 {
|
|
|
|
recipientStrings.add(recipient.toShortString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return recipientStrings.toArray(new String[members.size()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Recipient get(int index) {
|
|
|
|
return members.get(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isLocalNumber(Recipient recipient) {
|
|
|
|
try {
|
|
|
|
String localNumber = TextSecurePreferences.getLocalNumber(context);
|
|
|
|
String e164Number = Util.canonicalizeNumber(context, recipient.getNumber());
|
|
|
|
|
|
|
|
return e164Number != null && e164Number.equals(localNumber);
|
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-28 19:46:50 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-15 20:28:07 +01:00
|
|
|
}
|