2012-07-21 05:23:25 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-07-21 05:23:25 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-07-21 05:23:25 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.recipients;
|
|
|
|
|
2012-10-22 00:41:44 +00:00
|
|
|
import android.content.Context;
|
2012-07-21 05:23:25 +00:00
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-10-22 00:41:44 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.KeyUtil;
|
2012-12-24 16:40:37 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient.RecipientModifiedListener;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.util.NumberUtil;
|
|
|
|
|
2012-07-21 05:23:25 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
2012-10-22 00:41:44 +00:00
|
|
|
import java.util.LinkedList;
|
2012-07-21 05:23:25 +00:00
|
|
|
import java.util.List;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
public class Recipients implements Parcelable {
|
|
|
|
|
|
|
|
public static final Parcelable.Creator<Recipients> CREATOR = new Parcelable.Creator<Recipients>() {
|
|
|
|
public Recipients createFromParcel(Parcel in) {
|
|
|
|
return new Recipients(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Recipients[] newArray(int size) {
|
|
|
|
return new Recipients[size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private List<Recipient> recipients;
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public Recipients(List<Recipient> recipients) {
|
|
|
|
this.recipients = recipients;
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2012-10-22 00:41:44 +00:00
|
|
|
public Recipients(final Recipient recipient) {
|
|
|
|
this.recipients = new LinkedList<Recipient>() {{
|
|
|
|
add(recipient);
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public Recipients(Parcel in) {
|
|
|
|
this.recipients = new ArrayList<Recipient>();
|
|
|
|
in.readTypedList(recipients, Recipient.CREATOR);
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
|
|
|
public void append(Recipients recipients) {
|
|
|
|
this.recipients.addAll(recipients.getRecipientsList());
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public Recipients truncateToSingleRecipient() {
|
|
|
|
assert(!this.recipients.isEmpty());
|
|
|
|
this.recipients = this.recipients.subList(0, 1);
|
|
|
|
return this;
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2013-01-06 23:46:26 +00:00
|
|
|
public void addListener(RecipientModifiedListener listener) {
|
2012-12-24 16:40:37 +00:00
|
|
|
for (Recipient recipient : recipients) {
|
2013-01-06 23:46:26 +00:00
|
|
|
recipient.addListener(listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeListener(RecipientModifiedListener listener) {
|
|
|
|
for (Recipient recipient : recipients) {
|
|
|
|
recipient.removeListener(listener);
|
2012-12-24 16:40:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public boolean isEmailRecipient() {
|
|
|
|
for (Recipient recipient : recipients) {
|
|
|
|
if (NumberUtil.isValidEmail(recipient.getNumber()))
|
2012-07-21 05:23:25 +00:00
|
|
|
return true;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2012-10-22 00:41:44 +00:00
|
|
|
public Recipients getSecureSessionRecipients(Context context) {
|
|
|
|
List<Recipient> secureRecipients = new LinkedList<Recipient>();
|
|
|
|
|
|
|
|
for (Recipient recipient : recipients) {
|
|
|
|
if (KeyUtil.isSessionFor(context, recipient)) {
|
|
|
|
secureRecipients.add(recipient);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Recipients(secureRecipients);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Recipients getInsecureSessionRecipients(Context context) {
|
|
|
|
List<Recipient> insecureRecipients = new LinkedList<Recipient>();
|
|
|
|
|
|
|
|
for (Recipient recipient : recipients) {
|
|
|
|
if (!KeyUtil.isSessionFor(context, recipient)) {
|
|
|
|
insecureRecipients.add(recipient);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Recipients(insecureRecipients);
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public boolean isEmpty() {
|
|
|
|
return this.recipients.isEmpty();
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public boolean isSingleRecipient() {
|
|
|
|
return this.recipients.size() == 1;
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public Recipient getPrimaryRecipient() {
|
|
|
|
if (!isEmpty())
|
|
|
|
return this.recipients.get(0);
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public List<Recipient> getRecipientsList() {
|
|
|
|
return this.recipients;
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public String[] toNumberStringArray() {
|
|
|
|
String[] recipientsArray = new String[recipients.size()];
|
|
|
|
Iterator<Recipient> iterator = recipients.iterator();
|
|
|
|
int i = 0;
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
while (iterator.hasNext())
|
|
|
|
recipientsArray[i++] = iterator.next().getNumber();
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return recipientsArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toShortString() {
|
|
|
|
String fromString = "";
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
for (int i=0;i<recipients.size();i++) {
|
|
|
|
fromString += recipients.get(i).toShortString();
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (i != recipients.size() -1 )
|
2012-07-21 05:23:25 +00:00
|
|
|
fromString += ", ";
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return fromString;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeToParcel(Parcel dest, int flags) {
|
|
|
|
dest.writeTypedList(recipients);
|
|
|
|
}
|
|
|
|
}
|