use of org.telegram.messenger.ContactsController in project Telegram-FOSS by Telegram-FOSS-Team.
the class LaunchActivity method findContacts.
private List<TLRPC.TL_contact> findContacts(String userName, String userPhone, boolean allowSelf) {
final MessagesController messagesController = MessagesController.getInstance(currentAccount);
final ContactsController contactsController = ContactsController.getInstance(currentAccount);
final List<TLRPC.TL_contact> contacts = new ArrayList<>(contactsController.contacts);
final List<TLRPC.TL_contact> foundContacts = new ArrayList<>();
if (userPhone != null) {
userPhone = PhoneFormat.stripExceptNumbers(userPhone);
TLRPC.TL_contact contact = contactsController.contactsByPhone.get(userPhone);
if (contact == null) {
String shortUserPhone = userPhone.substring(Math.max(0, userPhone.length() - 7));
contact = contactsController.contactsByShortPhone.get(shortUserPhone);
}
if (contact != null) {
final TLRPC.User user = messagesController.getUser(contact.user_id);
if (user != null && (!user.self || allowSelf)) {
foundContacts.add(contact);
} else {
// disable search by name
userName = null;
}
}
}
if (foundContacts.isEmpty() && userName != null) {
final String query1 = userName.trim().toLowerCase();
if (!TextUtils.isEmpty(query1)) {
String query2 = LocaleController.getInstance().getTranslitString(query1);
if (query1.equals(query2) || query2.length() == 0) {
query2 = null;
}
final String[] queries = new String[] { query1, query2 };
for (int i = 0, size = contacts.size(); i < size; i++) {
final TLRPC.TL_contact contact = contacts.get(i);
if (contact != null) {
final TLRPC.User user = messagesController.getUser(contact.user_id);
if (user != null) {
if (user.self && !allowSelf) {
continue;
}
final String[] names = new String[3];
names[0] = ContactsController.formatName(user.first_name, user.last_name).toLowerCase();
names[1] = LocaleController.getInstance().getTranslitString(names[0]);
if (names[0].equals(names[1])) {
names[1] = null;
}
if (UserObject.isReplyUser(user)) {
names[2] = LocaleController.getString("RepliesTitle", R.string.RepliesTitle).toLowerCase();
} else if (user.self) {
names[2] = LocaleController.getString("SavedMessages", R.string.SavedMessages).toLowerCase();
}
boolean found = false;
for (String q : queries) {
if (q == null) {
continue;
}
for (int j = 0; j < names.length; j++) {
final String name = names[j];
if (name != null && (name.startsWith(q) || name.contains(" " + q))) {
found = true;
break;
}
}
if (!found && user.username != null && user.username.startsWith(q)) {
found = true;
}
if (found) {
foundContacts.add(contact);
break;
}
}
}
}
}
}
}
return foundContacts;
}
Aggregations