65 lines
2.4 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.components;
import android.content.Context;
import android.content.Intent;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.Recipients;
public class AvatarImageView extends ImageView {
public AvatarImageView(Context context) {
super(context);
setScaleType(ScaleType.CENTER_CROP);
}
public AvatarImageView(Context context, AttributeSet attrs) {
super(context, attrs);
setScaleType(ScaleType.CENTER_CROP);
}
public void setAvatar(@Nullable Recipients recipients, boolean quickContactEnabled) {
if (recipients != null) {
setImageDrawable(recipients.getContactPhoto().asDrawable(getContext()));
setAvatarClickHandler(recipients, quickContactEnabled);
} else {
setImageDrawable(ContactPhotoFactory.getDefaultContactPhoto(null).asDrawable(getContext()));
setOnClickListener(null);
}
}
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) {
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Recipient recipient = recipients.getPrimaryRecipient();
if (recipient != null && recipient.getContactUri() != null) {
ContactsContract.QuickContact.showQuickContact(getContext(), AvatarImageView.this, recipient.getContactUri(), ContactsContract.QuickContact.MODE_LARGE, null);
} else if (recipient != null) {
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);
}
}
}