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;
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-12-24 08:40:37 -08:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientProvider.RecipientDetails;
|
2012-12-27 12:00:14 -08:00
|
|
|
import org.thoughtcrime.securesms.util.FutureTaskListener;
|
|
|
|
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
2012-12-24 08:40:37 -08:00
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public class Recipient implements Parcelable {
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-12-24 08:40:37 -08:00
|
|
|
private final AtomicReference<String> name = new AtomicReference<String>(null);
|
|
|
|
private final AtomicReference<Bitmap> contactPhoto = new AtomicReference<Bitmap>(null);
|
|
|
|
private final AtomicReference<Uri> contactUri = new AtomicReference<Uri>(null);
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
private final String number;
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2012-12-24 08:40:37 -08:00
|
|
|
private RecipientModifiedListener listener;
|
|
|
|
private boolean asynchronousUpdateComplete = false;
|
|
|
|
|
2012-12-27 12:00:14 -08:00
|
|
|
public Recipient(String number, Bitmap contactPhoto,
|
|
|
|
ListenableFutureTask<RecipientDetails> future)
|
|
|
|
{
|
|
|
|
this.number = number;
|
|
|
|
this.contactPhoto.set(contactPhoto);
|
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) {
|
|
|
|
Recipient.this.name.set(result.name);
|
|
|
|
Recipient.this.contactUri.set(result.contactUri);
|
|
|
|
Recipient.this.contactPhoto.set(result.avatar);
|
|
|
|
|
|
|
|
synchronized(this) {
|
|
|
|
if (listener == null) asynchronousUpdateComplete = true;
|
|
|
|
else listener.onModified(Recipient.this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable error) {
|
|
|
|
Log.w("Recipient", error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-09-11 18:23:19 -07:00
|
|
|
|
2012-12-24 08:40:37 -08:00
|
|
|
public Recipient(String name, String number, Uri contactUri, Bitmap contactPhoto) {
|
|
|
|
this.number = number;
|
|
|
|
this.contactUri.set(contactUri);
|
|
|
|
this.name.set(name);
|
|
|
|
this.contactPhoto.set(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) {
|
2012-12-24 08:40:37 -08:00
|
|
|
this.number = in.readString();
|
|
|
|
|
|
|
|
this.name.set(in.readString());
|
|
|
|
this.contactUri.set((Uri)in.readParcelable(null));
|
|
|
|
this.contactPhoto.set((Bitmap)in.readParcelable(null));
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2012-09-11 18:23:19 -07:00
|
|
|
|
|
|
|
public Uri getContactUri() {
|
2012-12-24 08:40:37 -08:00
|
|
|
return this.contactUri.get();
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
2012-12-24 08:40:37 -08:00
|
|
|
return name.get();
|
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;
|
|
|
|
}
|
|
|
|
|
2012-12-27 12:00:14 -08:00
|
|
|
// public void updateAsynchronousContent(RecipientDetails result) {
|
|
|
|
// if (result != null) {
|
|
|
|
// Recipient.this.name.set(result.name);
|
|
|
|
// Recipient.this.contactUri.set(result.contactUri);
|
|
|
|
// Recipient.this.contactPhoto.set(result.avatar);
|
|
|
|
//
|
|
|
|
// synchronized(this) {
|
|
|
|
// if (listener == null) asynchronousUpdateComplete = true;
|
|
|
|
// else listener.onModified(Recipient.this);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2012-12-24 08:40:37 -08:00
|
|
|
|
|
|
|
public synchronized void setListener(RecipientModifiedListener listener) {
|
|
|
|
this.listener = listener;
|
|
|
|
if (asynchronousUpdateComplete) {
|
|
|
|
if (listener != null)
|
|
|
|
listener.onModified(this);
|
|
|
|
asynchronousUpdateComplete = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public void writeToParcel(Parcel dest, int flags) {
|
|
|
|
dest.writeString(number);
|
2012-12-24 08:40:37 -08:00
|
|
|
dest.writeString(name.get());
|
|
|
|
dest.writeParcelable(contactUri.get(), 0);
|
|
|
|
dest.writeParcelable(contactPhoto.get(), 0);
|
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 String toShortString() {
|
2012-12-24 08:40:37 -08:00
|
|
|
return (name.get() == null ? number : name.get());
|
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 Bitmap getContactPhoto() {
|
2012-12-24 08:40:37 -08:00
|
|
|
return contactPhoto.get();
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2012-09-11 18:23:19 -07: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
|
|
|
}
|