2015-05-04 11:36:18 -07:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2015-06-29 15:33:36 -07:00
|
|
|
import android.content.res.TypedArray;
|
2015-05-04 11:36:18 -07:00
|
|
|
import android.provider.ContactsContract;
|
2015-05-14 08:53:35 -07:00
|
|
|
import android.support.annotation.Nullable;
|
2015-05-04 11:36:18 -07:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
2015-06-29 15:33:36 -07:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-06-30 09:16:05 -07:00
|
|
|
import org.thoughtcrime.securesms.color.MaterialColor;
|
|
|
|
import org.thoughtcrime.securesms.color.ThemeType;
|
2015-06-23 15:10:50 -07:00
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
|
2015-06-23 10:15:33 -07:00
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
2015-05-04 11:36:18 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2015-06-14 11:20:19 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2015-05-04 11:36:18 -07:00
|
|
|
|
|
|
|
public class AvatarImageView extends ImageView {
|
|
|
|
|
2015-06-29 15:33:36 -07:00
|
|
|
private boolean inverted;
|
|
|
|
|
2015-05-04 11:36:18 -07:00
|
|
|
public AvatarImageView(Context context) {
|
|
|
|
super(context);
|
2015-05-15 17:14:20 -07:00
|
|
|
setScaleType(ScaleType.CENTER_CROP);
|
2015-05-04 11:36:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public AvatarImageView(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
2015-05-15 17:14:20 -07:00
|
|
|
setScaleType(ScaleType.CENTER_CROP);
|
2015-06-29 15:33:36 -07:00
|
|
|
|
|
|
|
if (attrs != null) {
|
|
|
|
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AvatarImageView, 0, 0);
|
|
|
|
inverted = typedArray.getBoolean(0, false);
|
|
|
|
typedArray.recycle();
|
|
|
|
}
|
2015-05-04 11:36:18 -07:00
|
|
|
}
|
|
|
|
|
2015-06-14 11:20:19 -07:00
|
|
|
public void setAvatar(@Nullable Recipients recipients, boolean quickContactEnabled) {
|
2015-06-30 09:16:05 -07:00
|
|
|
ThemeType themeType = ThemeType.getCurrent(getContext());
|
|
|
|
|
2015-06-14 11:20:19 -07:00
|
|
|
if (recipients != null) {
|
2015-06-30 09:16:05 -07:00
|
|
|
MaterialColor backgroundColor = recipients.getColor(getContext());
|
|
|
|
setImageDrawable(recipients.getContactPhoto().asDrawable(getContext(), backgroundColor.toConversationColor(themeType), inverted));
|
2015-06-14 11:20:19 -07:00
|
|
|
setAvatarClickHandler(recipients, quickContactEnabled);
|
2015-05-14 08:53:35 -07:00
|
|
|
} else {
|
2015-06-30 09:16:05 -07:00
|
|
|
setImageDrawable(ContactPhotoFactory.getDefaultContactPhoto(null).asDrawable(getContext(), ContactColors.UNKNOWN_COLOR.toConversationColor(themeType), inverted));
|
2015-05-14 08:53:35 -07:00
|
|
|
setOnClickListener(null);
|
|
|
|
}
|
2015-05-04 11:36:18 -07:00
|
|
|
}
|
|
|
|
|
2015-06-14 11:20:19 -07:00
|
|
|
public void setAvatar(@Nullable Recipient recipient, boolean quickContactEnabled) {
|
|
|
|
setAvatar(RecipientFactory.getRecipientsFor(getContext(), recipient, true), quickContactEnabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setAvatarClickHandler(final Recipients recipients, boolean quickContactEnabled) {
|
|
|
|
if (!recipients.isGroupRecipient() && quickContactEnabled) {
|
2015-05-04 11:36:18 -07:00
|
|
|
setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2015-06-14 11:20:19 -07:00
|
|
|
Recipient recipient = recipients.getPrimaryRecipient();
|
|
|
|
|
|
|
|
if (recipient != null && recipient.getContactUri() != null) {
|
2015-05-04 11:36:18 -07:00
|
|
|
ContactsContract.QuickContact.showQuickContact(getContext(), AvatarImageView.this, recipient.getContactUri(), ContactsContract.QuickContact.MODE_LARGE, null);
|
2015-06-14 11:20:19 -07:00
|
|
|
} else if (recipient != null) {
|
2015-05-04 11:36:18 -07:00
|
|
|
final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
|
|
|
|
intent.putExtra(ContactsContract.Intents.Insert.PHONE, recipient.getNumber());
|
|
|
|
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
|
|
|
|
getContext().startActivity(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setOnClickListener(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|