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