2012-07-21 05:23:25 +00:00
|
|
|
/**
|
2015-06-09 14:37:20 +00:00
|
|
|
* Copyright (C) 2015 Open 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;
|
|
|
|
|
2015-06-14 18:20:19 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2015-06-09 14:37:20 +00:00
|
|
|
import android.net.Uri;
|
2015-05-14 15:53:35 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2015-06-09 14:37:20 +00:00
|
|
|
import android.util.Log;
|
2013-03-14 22:59:28 +00:00
|
|
|
import android.util.Patterns;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2015-06-14 18:20:19 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
|
2015-06-09 14:37:20 +00:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientsPreferences;
|
|
|
|
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.VibrateState;
|
2012-12-24 16:40:37 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient.RecipientModifiedListener;
|
2015-06-09 14:37:20 +00:00
|
|
|
import org.thoughtcrime.securesms.util.FutureTaskListener;
|
2014-02-03 03:38:06 +00:00
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
2015-06-09 14:37:20 +00:00
|
|
|
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.util.NumberUtil;
|
2014-11-12 19:15:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
2012-07-21 05:23:25 +00:00
|
|
|
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;
|
2015-06-09 14:37:20 +00:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.WeakHashMap;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
public class Recipients implements Iterable<Recipient>, RecipientModifiedListener {
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
private static final String TAG = Recipients.class.getSimpleName();
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
private final Set<RecipientsModifiedListener> listeners = Collections.newSetFromMap(new WeakHashMap<RecipientsModifiedListener, Boolean>());
|
|
|
|
private final List<Recipient> recipients;
|
|
|
|
|
|
|
|
private Uri ringtone = null;
|
|
|
|
private long mutedUntil = 0;
|
|
|
|
private boolean blocked = false;
|
|
|
|
private VibrateState vibrate = VibrateState.DEFAULT;
|
|
|
|
|
|
|
|
Recipients() {
|
|
|
|
this(new LinkedList<Recipient>(), (RecipientsPreferences)null);
|
|
|
|
}
|
|
|
|
|
|
|
|
Recipients(List<Recipient> recipients, @Nullable RecipientsPreferences preferences) {
|
2011-12-20 18:20:44 +00:00
|
|
|
this.recipients = recipients;
|
2015-06-09 14:37:20 +00:00
|
|
|
|
|
|
|
if (preferences != null) {
|
|
|
|
ringtone = preferences.getRingtone();
|
|
|
|
mutedUntil = preferences.getMuteUntil();
|
|
|
|
vibrate = preferences.getVibrateState();
|
|
|
|
blocked = preferences.isBlocked();
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
Recipients(List<Recipient> recipients, ListenableFutureTask<RecipientsPreferences> preferences) {
|
|
|
|
this.recipients = recipients;
|
|
|
|
|
|
|
|
preferences.addListener(new FutureTaskListener<RecipientsPreferences>() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(RecipientsPreferences result) {
|
|
|
|
if (result != null) {
|
|
|
|
|
|
|
|
Set<RecipientsModifiedListener> localListeners;
|
|
|
|
|
|
|
|
synchronized (Recipients.this) {
|
|
|
|
ringtone = result.getRingtone();
|
|
|
|
mutedUntil = result.getMuteUntil();
|
|
|
|
vibrate = result.getVibrateState();
|
|
|
|
blocked = result.isBlocked();
|
|
|
|
|
|
|
|
localListeners = new HashSet<>(listeners);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (RecipientsModifiedListener listener : localListeners) {
|
|
|
|
listener.onModified(Recipients.this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable error) {
|
|
|
|
Log.w(TAG, error);
|
|
|
|
}
|
|
|
|
});
|
2012-10-22 00:41:44 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
public synchronized @Nullable Uri getRingtone() {
|
|
|
|
return ringtone;
|
2012-07-21 05:23:25 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
public void setRingtone(Uri ringtone) {
|
|
|
|
synchronized (this) {
|
|
|
|
this.ringtone = ringtone;
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized boolean isMuted() {
|
|
|
|
return System.currentTimeMillis() <= mutedUntil;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMuted(long mutedUntil) {
|
|
|
|
synchronized (this) {
|
|
|
|
this.mutedUntil = mutedUntil;
|
2013-01-06 23:46:26 +00:00
|
|
|
}
|
2015-06-09 14:37:20 +00:00
|
|
|
|
|
|
|
notifyListeners();
|
2013-01-06 23:46:26 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
public synchronized boolean isBlocked() {
|
|
|
|
return blocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBlocked(boolean blocked) {
|
|
|
|
synchronized (this) {
|
|
|
|
this.blocked = blocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized VibrateState getVibrate() {
|
|
|
|
return vibrate;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setVibrate(VibrateState vibrate) {
|
|
|
|
synchronized (this) {
|
|
|
|
this.vibrate = vibrate;
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2015-06-14 18:20:19 +00:00
|
|
|
public Drawable getContactPhoto(Context context) {
|
|
|
|
if (recipients.size() == 1) return recipients.get(0).getContactPhoto();
|
|
|
|
else return ContactPhotoFactory.getDefaultGroupPhoto(context);
|
|
|
|
}
|
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
public synchronized void addListener(RecipientsModifiedListener listener) {
|
|
|
|
if (listeners.isEmpty()) {
|
|
|
|
for (Recipient recipient : recipients) {
|
|
|
|
recipient.addListener(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
listeners.add(listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void removeListener(RecipientsModifiedListener listener) {
|
|
|
|
listeners.remove(listener);
|
|
|
|
|
|
|
|
if (listeners.isEmpty()) {
|
|
|
|
for (Recipient recipient : recipients) {
|
|
|
|
recipient.removeListener(this);
|
|
|
|
}
|
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
|
|
|
|
2014-02-03 03:38:06 +00:00
|
|
|
public boolean isGroupRecipient() {
|
|
|
|
return isSingleRecipient() && GroupUtil.isEncodedGroup(recipients.get(0).getNumber());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-05-14 15:53:35 +00:00
|
|
|
public @Nullable Recipient getPrimaryRecipient() {
|
2011-12-20 18:20:44 +00:00
|
|
|
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
|
|
|
|
2014-12-18 02:14:19 +00:00
|
|
|
public long[] getIds() {
|
|
|
|
long[] ids = new long[recipients.size()];
|
|
|
|
for (int i=0; i<recipients.size(); i++) {
|
|
|
|
ids[i] = recipients.get(i).getRecipientId();
|
2014-02-24 08:19:54 +00:00
|
|
|
}
|
2014-12-18 02:14:19 +00:00
|
|
|
return ids;
|
2014-02-24 08:19:54 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
public String getSortedIdsString() {
|
|
|
|
Set<Long> recipientSet = new HashSet<>();
|
|
|
|
|
|
|
|
for (Recipient recipient : this.recipients) {
|
|
|
|
recipientSet.add(recipient.getRecipientId());
|
|
|
|
}
|
|
|
|
|
|
|
|
long[] recipientArray = new long[recipientSet.size()];
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (Long recipientId : recipientSet) {
|
|
|
|
recipientArray[i++] = recipientId;
|
|
|
|
}
|
|
|
|
|
|
|
|
Arrays.sort(recipientArray);
|
|
|
|
|
|
|
|
return Util.join(recipientArray, " ");
|
|
|
|
}
|
|
|
|
|
2013-03-14 22:59:28 +00:00
|
|
|
public String[] toNumberStringArray(boolean scrub) {
|
2011-12-20 18:20:44 +00:00
|
|
|
String[] recipientsArray = new String[recipients.size()];
|
|
|
|
Iterator<Recipient> iterator = recipients.iterator();
|
|
|
|
int i = 0;
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2013-03-14 22:59:28 +00:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
String number = iterator.next().getNumber();
|
|
|
|
|
2014-02-14 23:59:57 +00:00
|
|
|
if (scrub && number != null &&
|
|
|
|
!Patterns.EMAIL_ADDRESS.matcher(number).matches() &&
|
|
|
|
!GroupUtil.isEncodedGroup(number))
|
|
|
|
{
|
2013-03-14 22:59:28 +00:00
|
|
|
number = number.replaceAll("[^0-9+]", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
recipientsArray[i++] = number;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
@Override
|
|
|
|
public Iterator<Recipient> iterator() {
|
|
|
|
return recipients.iterator();
|
|
|
|
}
|
2015-06-09 14:37:20 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onModified(Recipient recipient) {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notifyListeners() {
|
|
|
|
Set<RecipientsModifiedListener> localListeners;
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
localListeners = new HashSet<>(listeners);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (RecipientsModifiedListener listener : localListeners) {
|
|
|
|
listener.onModified(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public interface RecipientsModifiedListener {
|
|
|
|
public void onModified(Recipients recipient);
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|