less hacky way of converting ContactData to Recipients

This commit is contained in:
Jake McGinty 2014-02-17 16:19:38 -08:00
parent 04327e9ed7
commit c19ac8ec1e
3 changed files with 26 additions and 15 deletions

View File

@ -631,9 +631,9 @@ public class ConversationActivity extends PassphraseRequiredSherlockFragmentActi
} }
private void initializeRecipientsInput() { private void initializeRecipientsInput() {
if (recipients == null || recipients.isEmpty()) if (recipients == null || recipients.isEmpty()) {
recipientsPanel.setVisibility(View.VISIBLE); recipientsPanel.setVisibility(View.VISIBLE);
else if (recipients != null) { } else if (recipients != null) {
recipientsPanel.addRecipients(this.recipients); recipientsPanel.addRecipients(this.recipients);
} else { } else {
InputMethodManager input = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager input = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

View File

@ -25,15 +25,20 @@ import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
import org.thoughtcrime.securesms.components.SingleRecipientPanel; import org.thoughtcrime.securesms.components.SingleRecipientPanel;
import org.thoughtcrime.securesms.contacts.ContactAccessor;
import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.ThreadDatabase; import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.RecipientFormattingException; import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
import org.thoughtcrime.securesms.recipients.Recipients; import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.ActionBarUtil; import org.thoughtcrime.securesms.util.ActionBarUtil;
import org.thoughtcrime.securesms.util.DynamicTheme; import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.NumberUtil;
import org.whispersystems.textsecure.crypto.MasterSecret; import org.whispersystems.textsecure.crypto.MasterSecret;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import static org.thoughtcrime.securesms.contacts.ContactAccessor.ContactData; import static org.thoughtcrime.securesms.contacts.ContactAccessor.ContactData;
@ -72,29 +77,35 @@ public class SingleContactSelectionActivity extends PassphraseRequiredSherlockFr
listFragment.setOnContactSelectedListener(new SingleContactSelectionListFragment.OnContactSelectedListener() { listFragment.setOnContactSelectedListener(new SingleContactSelectionListFragment.OnContactSelectedListener() {
@Override @Override
public void onContactSelected(ContactData contactData) { public void onContactSelected(ContactData contactData) {
ArrayList<ContactData> contactList = new ArrayList<ContactData>(); Log.i(TAG, "Choosing contact from list.");
contactList.add(contactData); Recipients recipients = contactDataToRecipients(contactData);
openNewConversation(recipients);
recipientsPanel.setVisibility(View.INVISIBLE);
recipientsPanel.addContacts(contactList);
try {
openNewConversation(recipientsPanel.getRecipients());
} catch (RecipientFormattingException rfe) {
recipientsPanel.clear();
recipientsPanel.setVisibility(View.VISIBLE);
}
} }
}); });
recipientsPanel.setPanelChangeListener(new SingleRecipientPanel.RecipientsPanelChangedListener() { recipientsPanel.setPanelChangeListener(new SingleRecipientPanel.RecipientsPanelChangedListener() {
@Override @Override
public void onRecipientsPanelUpdate(Recipients recipients) { public void onRecipientsPanelUpdate(Recipients recipients) {
Log.i(TAG, "onRecipientsPanelUpdate received."); Log.i(TAG, "Choosing contact from autocompletion.");
openNewConversation(recipients); openNewConversation(recipients);
} }
}); });
} }
private Recipients contactDataToRecipients(ContactData contactData) {
if (contactData == null || contactData.numbers == null) return null;
List<Recipient> recipients = new ArrayList<Recipient>();
for (ContactAccessor.NumberData numberData : contactData.numbers) {
if (NumberUtil.isValidSmsOrEmailOrGroup(numberData.number)) {
Recipient recipient = RecipientFactory.getRecipientForNumber(SingleContactSelectionActivity.this,
numberData.number,
false);
recipients.add(recipient);
}
}
return new Recipients(recipients);
}
private void openNewConversation(Recipients recipients) { private void openNewConversation(Recipients recipients) {
if (recipients != null) { if (recipients != null) {
Intent intent = new Intent(SingleContactSelectionActivity.this, ConversationActivity.class); Intent intent = new Intent(SingleContactSelectionActivity.this, ConversationActivity.class);

View File

@ -51,7 +51,7 @@ public class RecipientFactory {
return new Recipients(results); return new Recipients(results);
} }
private static Recipient getRecipientForNumber(Context context, String number, boolean asynchronous) { public static Recipient getRecipientForNumber(Context context, String number, boolean asynchronous) {
long recipientId = CanonicalAddressDatabase.getInstance(context).getCanonicalAddress(number); long recipientId = CanonicalAddressDatabase.getInstance(context).getCanonicalAddress(number);
return provider.getRecipient(context, recipientId, asynchronous); return provider.getRecipient(context, recipientId, asynchronous);
} }