152 lines
4.5 KiB
Java
Raw Normal View History

/**
2011-12-20 10:20:44 -08:00
* Copyright (C) 2011 Whisper Systems
*
2011-12-20 10:20:44 -08: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 10:20:44 -08: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.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
2011-12-20 10:20:44 -08:00
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
import org.thoughtcrime.securesms.recipients.RecipientProvider.RecipientDetails;
import org.thoughtcrime.securesms.util.FutureTaskListener;
import org.thoughtcrime.securesms.util.GroupUtil;
import org.thoughtcrime.securesms.util.ListenableFutureTask;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.WeakHashMap;
public class Recipient {
2011-12-20 10:20:44 -08:00
private final static String TAG = Recipient.class.getSimpleName();
2014-01-18 18:17:08 -08:00
private final Set<RecipientModifiedListener> listeners = Collections.newSetFromMap(new WeakHashMap<RecipientModifiedListener, Boolean>());
private final long recipientId;
private String number;
private String name;
private Drawable contactPhoto;
private Uri contactUri;
Recipient(String number, Drawable contactPhoto,
long recipientId, ListenableFutureTask<RecipientDetails> future)
{
this.number = number;
this.contactPhoto = contactPhoto;
this.recipientId = recipientId;
future.addListener(new FutureTaskListener<RecipientDetails>() {
@Override
public void onSuccess(RecipientDetails result) {
if (result != null) {
Set<RecipientModifiedListener> localListeners;
synchronized (Recipient.this) {
Recipient.this.name = result.name;
Recipient.this.number = result.number;
Recipient.this.contactUri = result.contactUri;
Recipient.this.contactPhoto = result.avatar;
localListeners = new HashSet<>(listeners);
listeners.clear();
}
for (RecipientModifiedListener listener : localListeners)
listener.onModified(Recipient.this);
}
}
@Override
public void onFailure(Throwable error) {
Log.w("Recipient", error);
}
});
}
Recipient(String name, String number, long recipientId, Uri contactUri, Drawable contactPhoto) {
this.number = number;
this.recipientId = recipientId;
this.contactUri = contactUri;
this.name = name;
this.contactPhoto = contactPhoto;
2011-12-20 10:20:44 -08:00
}
public synchronized Uri getContactUri() {
return this.contactUri;
2011-12-20 10:20:44 -08:00
}
public synchronized String getName() {
return this.name;
2011-12-20 10:20:44 -08:00
}
public String getNumber() {
return number;
}
public long getRecipientId() {
return recipientId;
}
public boolean isGroupRecipient() {
return GroupUtil.isEncodedGroup(number);
}
public synchronized void addListener(RecipientModifiedListener listener) {
listeners.add(listener);
}
public synchronized void removeListener(RecipientModifiedListener listener) {
listeners.remove(listener);
}
public synchronized String toShortString() {
return (name == null ? number : name);
2011-12-20 10:20:44 -08:00
}
public synchronized Drawable getContactPhoto() {
return contactPhoto;
2011-12-20 10:20:44 -08:00
}
public static Recipient getUnknownRecipient(Context context) {
return new Recipient("Unknown", "Unknown", -1, null,
ContactPhotoFactory.getDefaultContactPhoto(context, null));
}
2014-01-18 18:17:08 -08:00
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || !(o instanceof Recipient)) return false;
2014-01-18 18:17:08 -08:00
Recipient that = (Recipient) o;
2014-01-18 18:17:08 -08:00
return this.recipientId == that.recipientId;
2014-01-18 18:17:08 -08:00
}
@Override
public int hashCode() {
return 31 + (int)this.recipientId;
2014-01-18 18:17:08 -08:00
}
public interface RecipientModifiedListener {
public void onModified(Recipient recipient);
}
2011-12-20 10:20:44 -08:00
}