2012-09-11 18:23:19 -07:00
|
|
|
/**
|
2011-12-20 10:20:44 -08:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-09-11 18:23:19 -07:00
|
|
|
*
|
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.
|
2012-09-11 18:23:19 -07:00
|
|
|
*
|
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;
|
|
|
|
|
2012-09-11 18:23:19 -07:00
|
|
|
import android.net.Uri;
|
2015-06-23 10:15:33 -07:00
|
|
|
import android.support.annotation.NonNull;
|
2015-06-23 15:10:50 -07:00
|
|
|
import android.support.annotation.Nullable;
|
2012-12-27 12:00:14 -08:00
|
|
|
import android.util.Log;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2015-06-30 09:16:05 -07:00
|
|
|
import org.thoughtcrime.securesms.color.MaterialColor;
|
2015-06-29 15:33:36 -07:00
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
|
2015-06-23 10:15:33 -07:00
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
2012-12-24 08:40:37 -08:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientProvider.RecipientDetails;
|
2014-11-12 09:20:57 -08:00
|
|
|
import org.thoughtcrime.securesms.util.FutureTaskListener;
|
2015-05-04 11:36:18 -07:00
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
2014-11-12 09:20:57 -08:00
|
|
|
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
2012-12-24 08:40:37 -08:00
|
|
|
|
2015-06-12 13:54:47 -07:00
|
|
|
import java.util.Collections;
|
2013-01-06 15:46:26 -08:00
|
|
|
import java.util.HashSet;
|
2015-06-12 13:54:47 -07:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.WeakHashMap;
|
2012-12-24 08:40:37 -08:00
|
|
|
|
2014-12-17 18:14:19 -08:00
|
|
|
public class Recipient {
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2014-02-18 16:28:54 -08:00
|
|
|
private final static String TAG = Recipient.class.getSimpleName();
|
2014-01-18 18:17:08 -08:00
|
|
|
|
2015-06-12 13:54:47 -07:00
|
|
|
private final Set<RecipientModifiedListener> listeners = Collections.newSetFromMap(new WeakHashMap<RecipientModifiedListener, Boolean>());
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2014-04-24 16:40:54 -07:00
|
|
|
private final long recipientId;
|
2014-02-02 19:38:06 -08:00
|
|
|
|
2014-04-24 16:40:54 -07:00
|
|
|
private String number;
|
2013-01-06 15:46:26 -08:00
|
|
|
private String name;
|
2014-03-07 00:58:26 +01:00
|
|
|
|
2015-06-30 09:16:05 -07:00
|
|
|
private ContactPhoto contactPhoto;
|
|
|
|
private Uri contactUri;
|
|
|
|
|
|
|
|
@Nullable private MaterialColor color;
|
2012-12-24 08:40:37 -08:00
|
|
|
|
2015-06-23 10:15:33 -07:00
|
|
|
Recipient(long recipientId, String number, ListenableFutureTask<RecipientDetails> future)
|
2012-12-27 12:00:14 -08:00
|
|
|
{
|
2015-06-23 10:15:33 -07:00
|
|
|
this.recipientId = recipientId;
|
|
|
|
this.number = number;
|
|
|
|
this.contactPhoto = ContactPhotoFactory.getLoadingPhoto();
|
2015-06-30 09:16:05 -07:00
|
|
|
this.color = null;
|
2012-12-24 08:40:37 -08:00
|
|
|
|
2014-10-28 02:23:00 -05:00
|
|
|
future.addListener(new FutureTaskListener<RecipientDetails>() {
|
2012-12-27 12:00:14 -08:00
|
|
|
@Override
|
|
|
|
public void onSuccess(RecipientDetails result) {
|
|
|
|
if (result != null) {
|
2013-01-06 15:46:26 -08:00
|
|
|
synchronized (Recipient.this) {
|
2015-06-23 10:15:33 -07:00
|
|
|
Recipient.this.name = result.name;
|
|
|
|
Recipient.this.number = result.number;
|
|
|
|
Recipient.this.contactUri = result.contactUri;
|
|
|
|
Recipient.this.contactPhoto = result.avatar;
|
2015-06-29 15:33:36 -07:00
|
|
|
Recipient.this.color = result.color;
|
2012-12-27 12:00:14 -08:00
|
|
|
}
|
2013-01-06 15:46:26 -08:00
|
|
|
|
2015-06-29 15:33:36 -07:00
|
|
|
notifyListeners();
|
2012-12-27 12:00:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable error) {
|
|
|
|
Log.w("Recipient", error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2015-06-23 10:15:33 -07:00
|
|
|
Recipient(long recipientId, RecipientDetails details) {
|
|
|
|
this.recipientId = recipientId;
|
|
|
|
this.number = details.number;
|
|
|
|
this.contactUri = details.contactUri;
|
|
|
|
this.name = details.name;
|
|
|
|
this.contactPhoto = details.avatar;
|
2015-06-29 15:33:36 -07:00
|
|
|
this.color = details.color;
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2013-01-06 15:46:26 -08:00
|
|
|
public synchronized Uri getContactUri() {
|
|
|
|
return this.contactUri;
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:10:50 -07:00
|
|
|
public synchronized @Nullable String getName() {
|
2013-01-06 15:46:26 -08:00
|
|
|
return this.name;
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
|
2015-06-30 09:16:05 -07:00
|
|
|
public synchronized @NonNull MaterialColor getColor() {
|
|
|
|
if (color != null) return color;
|
|
|
|
else if (name != null) return ContactColors.generateFor(name);
|
|
|
|
else return ContactColors.UNKNOWN_COLOR;
|
2015-06-29 15:33:36 -07:00
|
|
|
}
|
|
|
|
|
2015-06-30 09:16:05 -07:00
|
|
|
public void setColor(@NonNull MaterialColor color) {
|
2015-06-29 15:33:36 -07:00
|
|
|
synchronized (this) {
|
|
|
|
this.color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public String getNumber() {
|
|
|
|
return number;
|
|
|
|
}
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
public long getRecipientId() {
|
|
|
|
return recipientId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isGroupRecipient() {
|
|
|
|
return GroupUtil.isEncodedGroup(number);
|
|
|
|
}
|
|
|
|
|
2013-01-06 15:46:26 -08:00
|
|
|
public synchronized void addListener(RecipientModifiedListener listener) {
|
|
|
|
listeners.add(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void removeListener(RecipientModifiedListener listener) {
|
|
|
|
listeners.remove(listener);
|
2012-12-24 08:40:37 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 15:46:26 -08:00
|
|
|
public synchronized String toShortString() {
|
|
|
|
return (name == null ? number : name);
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2015-06-23 10:15:33 -07:00
|
|
|
public synchronized @NonNull ContactPhoto getContactPhoto() {
|
2013-01-06 15:46:26 -08:00
|
|
|
return contactPhoto;
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2015-06-23 10:15:33 -07:00
|
|
|
public static Recipient getUnknownRecipient() {
|
2015-06-29 15:33:36 -07:00
|
|
|
return new Recipient(-1, new RecipientDetails("Unknown", "Unknown", null,
|
2015-07-14 14:31:03 -07:00
|
|
|
ContactPhotoFactory.getDefaultContactPhoto(null), null));
|
2013-08-17 18:37:18 -07:00
|
|
|
}
|
|
|
|
|
2014-01-18 18:17:08 -08:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
2014-02-03 11:52:27 -08:00
|
|
|
if (o == null || !(o instanceof Recipient)) return false;
|
2014-01-18 18:17:08 -08:00
|
|
|
|
2014-02-03 11:52:27 -08:00
|
|
|
Recipient that = (Recipient) o;
|
2014-01-18 18:17:08 -08:00
|
|
|
|
2014-02-03 11:52:27 -08:00
|
|
|
return this.recipientId == that.recipientId;
|
2014-01-18 18:17:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2014-02-03 11:52:27 -08:00
|
|
|
return 31 + (int)this.recipientId;
|
2014-01-18 18:17:08 -08:00
|
|
|
}
|
|
|
|
|
2015-06-29 15:33:36 -07:00
|
|
|
private void notifyListeners() {
|
|
|
|
Set<RecipientModifiedListener> localListeners;
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
localListeners = new HashSet<>(listeners);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (RecipientModifiedListener listener : localListeners)
|
|
|
|
listener.onModified(Recipient.this);
|
|
|
|
}
|
|
|
|
|
2015-05-04 11:36:18 -07:00
|
|
|
public interface RecipientModifiedListener {
|
2012-12-24 08:40:37 -08:00
|
|
|
public void onModified(Recipient recipient);
|
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|