2015-05-19 14:00:54 -07:00
|
|
|
package org.thoughtcrime.securesms.contacts;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2017-10-16 13:11:42 -07:00
|
|
|
import android.support.annotation.NonNull;
|
2015-07-21 13:30:29 -07:00
|
|
|
import android.text.TextUtils;
|
2015-05-19 14:00:54 -07:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.CheckBox;
|
2015-11-02 17:40:41 -08:00
|
|
|
import android.widget.LinearLayout;
|
2015-05-19 14:00:54 -07:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-06-23 15:10:50 -07:00
|
|
|
import org.thoughtcrime.securesms.components.AvatarImageView;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2017-10-16 13:11:42 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideRequests;
|
2017-08-01 08:56:00 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientModifiedListener;
|
2017-11-19 16:46:12 -08:00
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
2017-10-04 14:47:29 -07:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2015-11-26 19:04:27 +01:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
2015-05-19 14:00:54 -07:00
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
public class ContactSelectionListItem extends LinearLayout implements RecipientModifiedListener {
|
2015-05-19 14:00:54 -07:00
|
|
|
|
2017-11-16 15:21:46 -08:00
|
|
|
@SuppressWarnings("unused")
|
2017-10-04 14:47:29 -07:00
|
|
|
private static final String TAG = ContactSelectionListItem.class.getSimpleName();
|
|
|
|
|
2015-06-23 15:10:50 -07:00
|
|
|
private AvatarImageView contactPhotoImage;
|
|
|
|
private TextView numberView;
|
|
|
|
private TextView nameView;
|
|
|
|
private TextView labelView;
|
|
|
|
private CheckBox checkBox;
|
2015-05-19 14:00:54 -07:00
|
|
|
|
2017-10-16 13:11:42 -07:00
|
|
|
private String number;
|
|
|
|
private Recipient recipient;
|
|
|
|
private GlideRequests glideRequests;
|
2015-05-19 14:00:54 -07:00
|
|
|
|
|
|
|
public ContactSelectionListItem(Context context) {
|
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ContactSelectionListItem(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onFinishInflate() {
|
2015-06-23 15:10:50 -07:00
|
|
|
super.onFinishInflate();
|
2017-10-16 13:11:42 -07:00
|
|
|
this.contactPhotoImage = findViewById(R.id.contact_photo_image);
|
|
|
|
this.numberView = findViewById(R.id.number);
|
|
|
|
this.labelView = findViewById(R.id.label);
|
|
|
|
this.nameView = findViewById(R.id.name);
|
|
|
|
this.checkBox = findViewById(R.id.check_box);
|
2015-11-26 19:04:27 +01:00
|
|
|
|
|
|
|
ViewUtil.setTextViewGravityStart(this.nameView, getContext());
|
2015-05-19 14:00:54 -07:00
|
|
|
}
|
|
|
|
|
2017-11-16 15:21:46 -08:00
|
|
|
public void set(@NonNull GlideRequests glideRequests, int type, String name, String number, String label, int color, boolean multiSelect) {
|
2017-10-16 13:11:42 -07:00
|
|
|
this.glideRequests = glideRequests;
|
|
|
|
this.number = number;
|
2015-05-19 14:00:54 -07:00
|
|
|
|
2015-07-14 14:31:03 -07:00
|
|
|
if (type == ContactsDatabase.NEW_TYPE) {
|
2017-08-01 08:56:00 -07:00
|
|
|
this.recipient = null;
|
2017-10-16 13:11:42 -07:00
|
|
|
this.contactPhotoImage.setAvatar(glideRequests, Recipient.from(getContext(), Address.UNKNOWN, true), false);
|
2015-07-21 13:30:29 -07:00
|
|
|
} else if (!TextUtils.isEmpty(number)) {
|
2017-07-26 09:59:15 -07:00
|
|
|
Address address = Address.fromExternal(getContext(), number);
|
2017-08-21 18:32:38 -07:00
|
|
|
this.recipient = Recipient.from(getContext(), address, true);
|
2017-10-04 14:47:29 -07:00
|
|
|
this.recipient.addListener(this);
|
2015-07-21 13:30:29 -07:00
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
if (this.recipient.getName() != null) {
|
|
|
|
name = this.recipient.getName();
|
2015-07-21 13:30:29 -07:00
|
|
|
}
|
2015-05-19 14:00:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this.nameView.setTextColor(color);
|
|
|
|
this.numberView.setTextColor(color);
|
2017-10-16 13:11:42 -07:00
|
|
|
this.contactPhotoImage.setAvatar(glideRequests, recipient, false);
|
2015-05-19 14:00:54 -07:00
|
|
|
|
|
|
|
setText(type, name, number, label);
|
|
|
|
|
|
|
|
if (multiSelect) this.checkBox.setVisibility(View.VISIBLE);
|
|
|
|
else this.checkBox.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setChecked(boolean selected) {
|
|
|
|
this.checkBox.setChecked(selected);
|
|
|
|
}
|
|
|
|
|
2017-10-16 13:11:42 -07:00
|
|
|
public void unbind(GlideRequests glideRequests) {
|
2017-08-01 08:56:00 -07:00
|
|
|
if (recipient != null) {
|
|
|
|
recipient.removeListener(this);
|
|
|
|
recipient = null;
|
2015-05-19 14:00:54 -07:00
|
|
|
}
|
2017-10-16 13:11:42 -07:00
|
|
|
|
|
|
|
contactPhotoImage.clear(glideRequests);
|
2015-05-19 14:00:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setText(int type, String name, String number, String label) {
|
2017-11-19 16:46:12 -08:00
|
|
|
if (number == null || number.isEmpty() || GroupUtil.isEncodedGroup(number)) {
|
2015-05-19 14:00:54 -07:00
|
|
|
this.nameView.setEnabled(false);
|
|
|
|
this.numberView.setText("");
|
|
|
|
this.labelView.setVisibility(View.GONE);
|
|
|
|
} else if (type == ContactsDatabase.PUSH_TYPE) {
|
|
|
|
this.numberView.setText(number);
|
|
|
|
this.nameView.setEnabled(true);
|
|
|
|
this.labelView.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
this.numberView.setText(number);
|
|
|
|
this.nameView.setEnabled(true);
|
|
|
|
this.labelView.setText(label);
|
|
|
|
this.labelView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.nameView.setText(name);
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:10:50 -07:00
|
|
|
public String getNumber() {
|
|
|
|
return number;
|
2015-05-19 14:00:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-01 08:56:00 -07:00
|
|
|
public void onModified(final Recipient recipient) {
|
|
|
|
if (this.recipient == recipient) {
|
2017-10-04 14:47:29 -07:00
|
|
|
Util.runOnMain(() -> {
|
2017-10-16 13:11:42 -07:00
|
|
|
contactPhotoImage.setAvatar(glideRequests, recipient, false);
|
2017-10-04 14:47:29 -07:00
|
|
|
nameView.setText(recipient.toShortString());
|
2015-05-19 14:00:54 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|