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;
|
|
|
|
|
2013-08-17 18:37:18 -07:00
|
|
|
import android.content.Context;
|
2011-12-20 10:20:44 -08:00
|
|
|
import android.graphics.Bitmap;
|
2012-09-11 18:23:19 -07:00
|
|
|
import android.net.Uri;
|
2011-12-20 10:20:44 -08:00
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
2012-12-27 12:00:14 -08:00
|
|
|
import android.util.Log;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
|
2012-12-24 08:40:37 -08:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientProvider.RecipientDetails;
|
2014-02-02 19:38:06 -08:00
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
|
|
|
import org.whispersystems.textsecure.storage.CanonicalRecipient;
|
2013-09-11 15:32:18 -07:00
|
|
|
import org.whispersystems.textsecure.util.FutureTaskListener;
|
|
|
|
import org.whispersystems.textsecure.util.ListenableFutureTask;
|
2012-12-24 08:40:37 -08:00
|
|
|
|
2013-01-06 15:46:26 -08:00
|
|
|
import java.util.HashSet;
|
2012-12-24 08:40:37 -08:00
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
public class Recipient implements Parcelable, CanonicalRecipient {
|
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
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public static final Parcelable.Creator<Recipient> CREATOR = new Parcelable.Creator<Recipient>() {
|
|
|
|
public Recipient createFromParcel(Parcel in) {
|
|
|
|
return new Recipient(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Recipient[] newArray(int size) {
|
|
|
|
return new Recipient[size];
|
|
|
|
}
|
|
|
|
};
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2013-01-06 15:46:26 -08:00
|
|
|
private final HashSet<RecipientModifiedListener> listeners = new HashSet<RecipientModifiedListener>();
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
private final String number;
|
|
|
|
private final long recipientId;
|
|
|
|
|
2013-01-06 15:46:26 -08:00
|
|
|
private String name;
|
|
|
|
private Bitmap contactPhoto;
|
|
|
|
private Uri contactUri;
|
2012-12-24 08:40:37 -08:00
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
Recipient(String number, Bitmap contactPhoto, long recipientId,
|
|
|
|
ListenableFutureTask<RecipientDetails> future)
|
2012-12-27 12:00:14 -08:00
|
|
|
{
|
2013-01-06 15:46:26 -08:00
|
|
|
this.number = number;
|
|
|
|
this.contactPhoto = contactPhoto;
|
2014-02-02 19:38:06 -08:00
|
|
|
this.recipientId = recipientId;
|
2012-12-24 08:40:37 -08:00
|
|
|
|
2012-12-27 12:00:14 -08:00
|
|
|
future.setListener(new FutureTaskListener<RecipientDetails>() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(RecipientDetails result) {
|
|
|
|
if (result != null) {
|
2013-01-06 15:46:26 -08:00
|
|
|
HashSet<RecipientModifiedListener> localListeners;
|
|
|
|
|
|
|
|
synchronized (Recipient.this) {
|
|
|
|
Recipient.this.name = result.name;
|
|
|
|
Recipient.this.contactUri = result.contactUri;
|
|
|
|
Recipient.this.contactPhoto = result.avatar;
|
|
|
|
localListeners = (HashSet<RecipientModifiedListener>)listeners.clone();
|
|
|
|
listeners.clear();
|
2012-12-27 12:00:14 -08:00
|
|
|
}
|
2013-01-06 15:46:26 -08:00
|
|
|
|
|
|
|
for (RecipientModifiedListener listener : localListeners)
|
|
|
|
listener.onModified(Recipient.this);
|
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
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
Recipient(String name, String number, long recipientId, Uri contactUri, Bitmap contactPhoto) {
|
2013-01-06 15:46:26 -08:00
|
|
|
this.number = number;
|
2014-02-02 19:38:06 -08:00
|
|
|
this.recipientId = recipientId;
|
2013-01-06 15:46:26 -08:00
|
|
|
this.contactUri = contactUri;
|
|
|
|
this.name = name;
|
|
|
|
this.contactPhoto = contactPhoto;
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public Recipient(Parcel in) {
|
2013-01-06 15:46:26 -08:00
|
|
|
this.number = in.readString();
|
|
|
|
this.name = in.readString();
|
2014-02-02 19:38:06 -08:00
|
|
|
this.recipientId = in.readLong();
|
2014-03-17 23:25:09 -07:00
|
|
|
this.contactUri = in.readParcelable(null);
|
|
|
|
this.contactPhoto = in.readParcelable(null);
|
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
|
|
|
}
|
|
|
|
|
2014-02-14 15:59:57 -08:00
|
|
|
public synchronized void setContactPhoto(Bitmap bitmap) {
|
|
|
|
this.contactPhoto = bitmap;
|
2014-02-23 21:18:08 -08:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void setName(String name) {
|
|
|
|
this.name = name;
|
|
|
|
notifyListeners();
|
2014-02-14 15:59:57 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 15:46:26 -08:00
|
|
|
public synchronized String getName() {
|
|
|
|
return this.name;
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getNumber() {
|
|
|
|
return number;
|
|
|
|
}
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-14 15:59:57 -08:00
|
|
|
public void notifyListeners() {
|
|
|
|
HashSet<RecipientModifiedListener> localListeners;
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
localListeners = (HashSet<RecipientModifiedListener>)listeners.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (RecipientModifiedListener listener : localListeners) {
|
|
|
|
listener.onModified(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-06 15:46:26 -08:00
|
|
|
public synchronized void writeToParcel(Parcel dest, int flags) {
|
2011-12-20 10:20:44 -08:00
|
|
|
dest.writeString(number);
|
2013-01-06 15:46:26 -08:00
|
|
|
dest.writeString(name);
|
2014-02-02 19:38:06 -08:00
|
|
|
dest.writeLong(recipientId);
|
2013-01-06 15:46:26 -08:00
|
|
|
dest.writeParcelable(contactUri, 0);
|
|
|
|
dest.writeParcelable(contactPhoto, 0);
|
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 String toShortString() {
|
|
|
|
return (name == null ? number : name);
|
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 Bitmap getContactPhoto() {
|
|
|
|
return contactPhoto;
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2014-02-02 19:38:06 -08:00
|
|
|
public static Recipient getUnknownRecipient(Context context) {
|
|
|
|
return new Recipient("Unknown", "Unknown", -1, null, ContactPhotoFactory.getDefaultContactPhoto(context));
|
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
|
|
|
}
|
|
|
|
|
2012-12-24 08:40:37 -08:00
|
|
|
public static interface RecipientModifiedListener {
|
|
|
|
public void onModified(Recipient recipient);
|
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|