Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QuickContact in GroupMembersDialog #3033

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 78 additions & 20 deletions src/org/thoughtcrime/securesms/GroupMembersDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.util.Log;

import com.afollestad.materialdialogs.AlertDialogWrapper;
Expand All @@ -21,8 +25,6 @@

public class GroupMembersDialog extends AsyncTask<Void, Void, Recipients> {

private static final String TAG = GroupMembersDialog.class.getSimpleName();

private final Recipients recipients;
private final Context context;

Expand Down Expand Up @@ -56,20 +58,12 @@ public void onPostExecute(Recipients members) {
progress.dismiss();
}

List<String> recipientStrings = new LinkedList<>();
recipientStrings.add(context.getString(R.string.GroupMembersDialog_me));

for (Recipient recipient : members.getRecipientsList()) {
if (!isLocalNumber(recipient)) {
recipientStrings.add(recipient.toShortString());
}
}

GroupMembers groupMembers = new GroupMembers(members);
AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(context);
builder.setTitle(R.string.ConversationActivity_group_members);
builder.setIconAttribute(R.attr.group_members_dialog_icon);
builder.setCancelable(true);
builder.setItems(recipientStrings.toArray(new String[]{}), null);
builder.setItems(groupMembers.getRecipientStrings(), new GroupMembersOnClickListener(context, groupMembers));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make the listener an inner class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

builder.setPositiveButton(android.R.string.ok, null);
builder.show();
}
Expand All @@ -79,15 +73,79 @@ public void display() {
else onPostExecute(recipients);
}

private boolean isLocalNumber(Recipient recipient) {
try {
String localNumber = TextSecurePreferences.getLocalNumber(context);
String e164Number = Util.canonicalizeNumber(context, recipient.getNumber());
private class GroupMembersOnClickListener implements DialogInterface.OnClickListener {
private final Rect SPAWN_TARGET = new Rect(0, 0, 0, 0);
private final GroupMembers groupMembers;
private final Context context;

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, SPAWN_TARGET, 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);
}
}
}

return e164Number != null && e164Number.equals(localNumber);
} catch (InvalidNumberException e) {
Log.w(TAG, e);
return false;
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;
}
}
}
}