1. Switch from 300 to 500 colors. 2. Colorize incoming conversation bubbles. 3. Colorize recipeint preference activity toolbar. 4. Support inverted colors in avatars. 5. Make status bar icons tint according to secondary color. // FREEBIE
177 lines
5.3 KiB
Java
177 lines
5.3 KiB
Java
/**
|
|
* Copyright (C) 2011 Whisper Systems
|
|
*
|
|
* 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.
|
|
*
|
|
* 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;
|
|
import android.support.annotation.Nullable;
|
|
import android.util.Log;
|
|
|
|
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.recipients.RecipientProvider.RecipientDetails;
|
|
import org.thoughtcrime.securesms.util.FutureTaskListener;
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
|
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
|
|
|
import java.util.Collections;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
import java.util.WeakHashMap;
|
|
|
|
public class Recipient {
|
|
|
|
private final static String TAG = Recipient.class.getSimpleName();
|
|
|
|
private final Set<RecipientModifiedListener> listeners = Collections.newSetFromMap(new WeakHashMap<RecipientModifiedListener, Boolean>());
|
|
|
|
private final long recipientId;
|
|
|
|
private String number;
|
|
private String name;
|
|
|
|
private ContactPhoto contactPhoto;
|
|
private Uri contactUri;
|
|
private Optional<Integer> color;
|
|
|
|
Recipient(long recipientId, String number, ListenableFutureTask<RecipientDetails> future)
|
|
{
|
|
this.recipientId = recipientId;
|
|
this.number = number;
|
|
this.contactPhoto = ContactPhotoFactory.getLoadingPhoto();
|
|
this.color = Optional.absent();
|
|
|
|
future.addListener(new FutureTaskListener<RecipientDetails>() {
|
|
@Override
|
|
public void onSuccess(RecipientDetails result) {
|
|
if (result != null) {
|
|
synchronized (Recipient.this) {
|
|
Recipient.this.name = result.name;
|
|
Recipient.this.number = result.number;
|
|
Recipient.this.contactUri = result.contactUri;
|
|
Recipient.this.contactPhoto = result.avatar;
|
|
Recipient.this.color = result.color;
|
|
}
|
|
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(Throwable error) {
|
|
Log.w("Recipient", error);
|
|
}
|
|
});
|
|
}
|
|
|
|
Recipient(long recipientId, RecipientDetails details) {
|
|
this.recipientId = recipientId;
|
|
this.number = details.number;
|
|
this.contactUri = details.contactUri;
|
|
this.name = details.name;
|
|
this.contactPhoto = details.avatar;
|
|
this.color = details.color;
|
|
}
|
|
|
|
public synchronized Uri getContactUri() {
|
|
return this.contactUri;
|
|
}
|
|
|
|
public synchronized @Nullable String getName() {
|
|
return this.name;
|
|
}
|
|
|
|
public synchronized @NonNull Optional<Integer> getColor() {
|
|
if (color.isPresent()) return color;
|
|
else if (name != null) return Optional.of(ContactColors.generateFor(name));
|
|
else return Optional.of(ContactColors.UNKNOWN_COLOR);
|
|
}
|
|
|
|
public void setColor(Optional<Integer> color) {
|
|
synchronized (this) {
|
|
this.color = color;
|
|
}
|
|
|
|
notifyListeners();
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
public synchronized @NonNull ContactPhoto getContactPhoto() {
|
|
return contactPhoto;
|
|
}
|
|
|
|
public static Recipient getUnknownRecipient() {
|
|
return new Recipient(-1, new RecipientDetails("Unknown", "Unknown", null,
|
|
ContactPhotoFactory.getDefaultContactPhoto("Unknown"),
|
|
Optional.<Integer>absent()));
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
if (this == o) return true;
|
|
if (o == null || !(o instanceof Recipient)) return false;
|
|
|
|
Recipient that = (Recipient) o;
|
|
|
|
return this.recipientId == that.recipientId;
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return 31 + (int)this.recipientId;
|
|
}
|
|
|
|
private void notifyListeners() {
|
|
Set<RecipientModifiedListener> localListeners;
|
|
|
|
synchronized (this) {
|
|
localListeners = new HashSet<>(listeners);
|
|
}
|
|
|
|
for (RecipientModifiedListener listener : localListeners)
|
|
listener.onModified(Recipient.this);
|
|
}
|
|
|
|
public interface RecipientModifiedListener {
|
|
public void onModified(Recipient recipient);
|
|
}
|
|
}
|