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;
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2020-01-15 05:13:28 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.loki.redesign.views.ProfilePictureView;
|
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;
|
2020-01-15 05:13:28 +00:00
|
|
|
import org.whispersystems.signalservice.loki.api.LokiAPI;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import network.loki.messenger.R;
|
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();
|
|
|
|
|
2020-01-15 05:13:28 +00:00
|
|
|
private ProfilePictureView profilePictureView;
|
2015-06-23 22:10:50 +00:00
|
|
|
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;
|
2020-01-15 05:13:28 +00:00
|
|
|
private long threadID;
|
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();
|
2020-01-15 05:13:28 +00:00
|
|
|
this.profilePictureView = findViewById(R.id.profilePictureView);
|
2017-10-16 20:11:42 +00:00
|
|
|
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;
|
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
|
|
|
}
|
|
|
|
|
2020-01-15 05:13:28 +00:00
|
|
|
threadID = DatabaseFactory.getThreadDatabase(getContext()).getThreadIdFor(recipient);
|
|
|
|
|
2015-05-19 21:00:54 +00:00
|
|
|
this.numberView.setTextColor(color);
|
2020-01-15 05:13:28 +00:00
|
|
|
updateProfilePicture(glideRequests, name, threadID);
|
2015-05-19 21:00:54 +00:00
|
|
|
|
2019-01-14 07:30:54 +00:00
|
|
|
if (!multiSelect && recipient != null && recipient.isLocalNumber()) {
|
|
|
|
name = getContext().getString(R.string.note_to_self);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(() -> {
|
2020-01-15 05:13:28 +00:00
|
|
|
threadID = DatabaseFactory.getThreadDatabase(getContext()).getThreadIdFor(recipient);
|
|
|
|
updateProfilePicture(glideRequests, recipient.getName(), threadID);
|
2017-10-04 21:47:29 +00:00
|
|
|
nameView.setText(recipient.toShortString());
|
2015-05-19 21:00:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-01-15 05:13:28 +00:00
|
|
|
|
|
|
|
private void updateProfilePicture(GlideRequests glide, String name, long threadID) {
|
|
|
|
if (this.recipient.isGroupRecipient()) {
|
|
|
|
Set<String> usersAsSet = LokiAPI.Companion.getUserHexEncodedPublicKeyCache().get(threadID);
|
|
|
|
if (usersAsSet == null) {
|
|
|
|
usersAsSet = new HashSet<>();
|
|
|
|
}
|
|
|
|
ArrayList<String> users = new ArrayList<>(usersAsSet);
|
|
|
|
Collections.sort(users); // Sort to provide a level of stability
|
|
|
|
profilePictureView.setHexEncodedPublicKey(users.size() > 0 ? users.get(0) : "");
|
|
|
|
profilePictureView.setAdditionalHexEncodedPublicKey(users.size() > 1 ? users.get(1) : "");
|
2020-02-04 02:07:54 +00:00
|
|
|
profilePictureView.setRSSFeed(name.equals("Loki News") || name.equals("Session Updates"));
|
2020-01-15 05:13:28 +00:00
|
|
|
} else {
|
|
|
|
profilePictureView.setHexEncodedPublicKey(this.number);
|
|
|
|
profilePictureView.setAdditionalHexEncodedPublicKey(null);
|
|
|
|
profilePictureView.setRSSFeed(false);
|
|
|
|
}
|
|
|
|
profilePictureView.glide = glide;
|
|
|
|
profilePictureView.update();
|
|
|
|
}
|
2015-05-19 21:00:54 +00:00
|
|
|
}
|