session-android/src/org/thoughtcrime/securesms/recipients/Recipient.java

324 lines
9.1 KiB
Java
Raw Normal View History

/**
2011-12-20 19:20:44 +01:00
* Copyright (C) 2011 Whisper Systems
*
2011-12-20 19:20:44 +01: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.
*
2011-12-20 19:20:44 +01: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;
import android.net.Uri;
import android.support.annotation.NonNull;
2015-06-24 00:10:50 +02:00
import android.support.annotation.Nullable;
import android.util.Log;
2011-12-20 19:20:44 +01:00
import org.thoughtcrime.securesms.color.MaterialColor;
import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.VibrateState;
import org.thoughtcrime.securesms.recipients.RecipientProvider.RecipientDetails;
import org.thoughtcrime.securesms.util.FutureTaskListener;
import org.thoughtcrime.securesms.util.ListenableFutureTask;
import org.thoughtcrime.securesms.util.Util;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutionException;
public class Recipient implements RecipientModifiedListener {
2011-12-20 19:20:44 +01:00
private final static String TAG = Recipient.class.getSimpleName();
2014-01-19 03:17:08 +01:00
private final Set<RecipientModifiedListener> listeners = Collections.newSetFromMap(new WeakHashMap<RecipientModifiedListener, Boolean>());
private final @NonNull Address address;
private final @NonNull List<Recipient> participants = new LinkedList<>();
private @Nullable String name;
private @Nullable String customLabel;
private boolean stale;
private boolean resolving;
private ContactPhoto contactPhoto;
private Uri contactUri;
private Uri ringtone = null;
private long mutedUntil = 0;
private boolean blocked = false;
private VibrateState vibrate = VibrateState.DEFAULT;
private int expireMessages = 0;
@Nullable private MaterialColor color;
Recipient(@NonNull Address address,
@Nullable Recipient stale,
@NonNull ListenableFutureTask<RecipientDetails> future)
{
this.address = address;
this.contactPhoto = ContactPhotoFactory.getLoadingPhoto();
this.color = null;
this.resolving = true;
if (stale != null) {
this.name = stale.name;
this.contactUri = stale.contactUri;
this.contactPhoto = stale.contactPhoto;
this.color = stale.color;
this.customLabel = stale.customLabel;
this.ringtone = stale.ringtone;
this.mutedUntil = stale.mutedUntil;
this.blocked = stale.blocked;
this.vibrate = stale.vibrate;
this.expireMessages = stale.expireMessages;
}
future.addListener(new FutureTaskListener<RecipientDetails>() {
@Override
public void onSuccess(RecipientDetails result) {
if (result != null) {
synchronized (Recipient.this) {
Recipient.this.name = result.name;
Recipient.this.contactUri = result.contactUri;
Recipient.this.contactPhoto = result.avatar;
Recipient.this.color = result.color;
Recipient.this.customLabel = result.customLabel;
Recipient.this.ringtone = result.ringtone;
Recipient.this.mutedUntil = result.mutedUntil;
Recipient.this.blocked = result.blocked;
Recipient.this.vibrate = result.vibrateState;
Recipient.this.expireMessages = result.expireMessages;
Recipient.this.participants.addAll(result.participants);
Recipient.this.resolving = false;
if (!listeners.isEmpty()) {
for (Recipient recipient : participants) recipient.addListener(Recipient.this);
}
}
notifyListeners();
}
}
@Override
public void onFailure(ExecutionException error) {
Log.w(TAG, error);
}
});
}
Recipient(@NonNull Address address, @NonNull RecipientDetails details) {
this.address = address;
this.contactUri = details.contactUri;
this.name = details.name;
this.contactPhoto = details.avatar;
this.color = details.color;
this.customLabel = details.customLabel;
this.ringtone = details.ringtone;
this.mutedUntil = details.mutedUntil;
this.blocked = details.blocked;
this.vibrate = details.vibrateState;
this.expireMessages = details.expireMessages;
this.participants.addAll(details.participants);
this.resolving = false;
2011-12-20 19:20:44 +01:00
}
public synchronized @Nullable Uri getContactUri() {
return this.contactUri;
2011-12-20 19:20:44 +01:00
}
2015-06-24 00:10:50 +02:00
public synchronized @Nullable String getName() {
if (this.name == null && isMmsGroupRecipient()) {
List<String> names = new LinkedList<>();
for (Recipient recipient : participants) {
names.add(recipient.toShortString());
}
return Util.join(names, ", ");
}
return this.name;
2011-12-20 19:20:44 +01:00
}
public synchronized @NonNull MaterialColor getColor() {
if (isGroupRecipient()) return MaterialColor.GROUP;
else if (color != null) return color;
else if (name != null) return ContactColors.generateFor(name);
else return ContactColors.UNKNOWN_COLOR;
}
public void setColor(@NonNull MaterialColor color) {
synchronized (this) {
this.color = color;
}
notifyListeners();
}
public @NonNull Address getAddress() {
return address;
2011-12-20 19:20:44 +01:00
}
public @Nullable String getCustomLabel() {
return customLabel;
}
public boolean isGroupRecipient() {
return address.isGroup();
}
public boolean isMmsGroupRecipient() {
return address.isMmsGroup();
}
public boolean isPushGroupRecipient() {
return address.isGroup() && !address.isMmsGroup();
}
public List<Recipient> getParticipants() {
return participants;
}
public synchronized void addListener(RecipientModifiedListener listener) {
if (listeners.isEmpty()) {
for (Recipient recipient : participants) recipient.addListener(this);
}
listeners.add(listener);
}
public synchronized void removeListener(RecipientModifiedListener listener) {
listeners.remove(listener);
if (listeners.isEmpty()) {
for (Recipient recipient : participants) recipient.removeListener(this);
}
}
public synchronized String toShortString() {
return (getName() == null ? address.serialize() : getName());
2011-12-20 19:20:44 +01:00
}
public synchronized @NonNull ContactPhoto getContactPhoto() {
return contactPhoto;
2011-12-20 19:20:44 +01:00
}
public synchronized @Nullable Uri getRingtone() {
return ringtone;
}
public void setRingtone(Uri ringtone) {
synchronized (this) {
this.ringtone = ringtone;
}
notifyListeners();
}
public synchronized boolean isMuted() {
return System.currentTimeMillis() <= mutedUntil;
}
public void setMuted(long mutedUntil) {
synchronized (this) {
this.mutedUntil = mutedUntil;
}
notifyListeners();
}
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();
}
public synchronized int getExpireMessages() {
return expireMessages;
}
public void setExpireMessages(int expireMessages) {
synchronized (this) {
this.expireMessages = expireMessages;
}
notifyListeners();
}
2014-01-19 03:17:08 +01:00
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || !(o instanceof Recipient)) return false;
2014-01-19 03:17:08 +01:00
Recipient that = (Recipient) o;
2014-01-19 03:17:08 +01:00
return this.address.equals(that.address);
2014-01-19 03:17:08 +01:00
}
@Override
public int hashCode() {
return this.address.hashCode();
2014-01-19 03:17:08 +01:00
}
private void notifyListeners() {
Set<RecipientModifiedListener> localListeners;
synchronized (this) {
localListeners = new HashSet<>(listeners);
}
for (RecipientModifiedListener listener : localListeners)
listener.onModified(this);
}
@Override
public void onModified(Recipient recipient) {
notifyListeners();
}
boolean isStale() {
return stale;
}
void setStale() {
this.stale = true;
}
synchronized boolean isResolving() {
return resolving;
}
2011-12-20 19:20:44 +01:00
}