use of im.actor.core.entity.PhoneBookEmail in project actor-platform by actorapp.
the class UserRouter method checkIsInPhoneBook.
@Verified
protected Promise<Void> checkIsInPhoneBook(User user) {
if (!config().isEnableOnClientPrivacy()) {
return Promise.success(null);
}
Log.d("ON_CLIENT_PRIVACY", "checking " + user.getName() + " is in phone book");
return getPhoneBook().flatMap(new Function<List<PhoneBookContact>, Promise<Void>>() {
@Override
public Promise<Void> apply(List<PhoneBookContact> phoneBookContacts) {
return new Promise<Void>(resolver -> {
List<ContactRecord> userRecords = user.getRecords();
Log.d("ON_CLIENT_PRIVACY", "phonebook have " + phoneBookContacts.size() + " records");
Log.d("ON_CLIENT_PRIVACY", "user have " + userRecords.size() + " records");
outer: for (ContactRecord record : userRecords) {
for (PhoneBookContact phoneBookContact : phoneBookContacts) {
for (PhoneBookPhone phone1 : phoneBookContact.getPhones()) {
if (record.getRecordType() == ContactRecordType.PHONE) {
if (record.getRecordData().equals(phone1.getNumber() + "")) {
context().getContactsModule().markInPhoneBook(user.getUid());
getUserVM(user.getUid()).isInPhoneBook().change(true);
Log.d("ON_CLIENT_PRIVACY", "in record book!");
break outer;
}
}
}
for (PhoneBookEmail email : phoneBookContact.getEmails()) {
if (record.getRecordType() == ContactRecordType.EMAIL) {
if (record.getRecordData().equals(email.getEmail())) {
context().getContactsModule().markInPhoneBook(user.getUid());
getUserVM(user.getUid()).isInPhoneBook().change(true);
Log.d("ON_CLIENT_PRIVACY", "in record book!");
break outer;
}
}
}
}
}
Log.d("ON_CLIENT_PRIVACY", "finish check");
resolver.result(null);
});
}
});
}
use of im.actor.core.entity.PhoneBookEmail in project actor-platform by actorapp.
the class AndroidPhoneBook method loadPhoneBook.
public static ArrayList<PhoneBookContact> loadPhoneBook(Context context, String isoCountry) {
if (DISABLE_PHONE_BOOK) {
return new ArrayList<>();
}
Log.d(TAG, "Loading phone book");
long start = SystemClock.uptimeMillis();
HashSet<Long> addedPhones = new HashSet<>();
HashSet<String> addedEmails = new HashSet<>();
ArrayList<PhoneBookContact> records = new ArrayList<>();
HashMap<Long, PhoneBookContact> recordsMap = new HashMap<>();
ContentResolver cr = context.getContentResolver();
if (cr == null) {
return new ArrayList<>();
}
//Check have permission for this
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
Log.d("Permissions", "contacts - no permission :c");
return new ArrayList<>();
}
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME }, null, null, ContactsContract.Contacts.SORT_KEY_PRIMARY);
if (cur == null) {
return new ArrayList<>();
}
int idIndex = cur.getColumnIndex(ContactsContract.Contacts._ID);
int nameIndex = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int index = 0;
while (cur.moveToNext()) {
if (index++ == READ_ITEM_DELAY_BATCH) {
if (READ_ITEM_DELAY > 0) {
try {
Thread.sleep(READ_ITEM_DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
long id = cur.getLong(idIndex);
String name = cur.getString(nameIndex);
if (name == null || name.trim().length() == 0)
continue;
PhoneBookContact record = new PhoneBookContact(id, name.trim(), new ArrayList<>(), new ArrayList<>());
records.add(record);
recordsMap.put(id, record);
}
cur.close();
cur = null;
// Loading phones
cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.NUMBER }, null, null, ContactsContract.CommonDataKinds.Phone._ID + " desc");
if (cur == null) {
return new ArrayList<>();
}
final int idContactIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
final int idPhoneIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
final int idNumberIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
while (cur.moveToNext()) {
if (index++ == READ_ITEM_DELAY_BATCH) {
if (READ_ITEM_DELAY > 0) {
try {
Thread.sleep(READ_ITEM_DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
long contactId = cur.getLong(idContactIndex);
long phoneId = cur.getLong(idPhoneIndex);
String rawPhone = cur.getString(idNumberIndex);
PhoneBookContact record = recordsMap.get(contactId);
if (record == null || rawPhone == null) {
continue;
}
if (PHONE_UTIL == null) {
synchronized (initSync) {
if (PHONE_UTIL == null) {
PHONE_UTIL = PhoneNumberUtil.getInstance();
}
}
}
try {
final Phonenumber.PhoneNumber phonenumber = PHONE_UTIL.parse(rawPhone, isoCountry);
rawPhone = phonenumber.getCountryCode() + "" + phonenumber.getNationalNumber();
} catch (final NumberParseException e) {
rawPhone = rawPhone.replaceAll("[^\\d]", "");
}
long phone = -1;
try {
phone = Long.parseLong(rawPhone);
} catch (Exception e) {
// Logger.d(TAG, "Can't parse number", e);
continue;
}
if (addedPhones.contains(phone)) {
continue;
}
addedPhones.add(phone);
record.getPhones().add(new PhoneBookPhone(phoneId, phone));
}
cur.close();
cur = null;
// Loading emails
cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Email._ID, ContactsContract.CommonDataKinds.Email.CONTACT_ID, ContactsContract.CommonDataKinds.Email.ADDRESS }, null, null, ContactsContract.CommonDataKinds.Email._ID + " desc");
if (cur == null) {
return new ArrayList<PhoneBookContact>();
}
final int idEmailContactIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID);
final int idEmailIdIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Email._ID);
final int idEmailIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS);
while (cur.moveToNext()) {
if (index++ == READ_ITEM_DELAY_BATCH) {
if (READ_ITEM_DELAY > 0) {
try {
Thread.sleep(READ_ITEM_DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
long contactId = cur.getLong(idEmailContactIndex);
long emailId = cur.getLong(idEmailIdIndex);
String rawEmail = cur.getString(idEmailIndex);
PhoneBookContact record = recordsMap.get(contactId);
if (record == null) {
continue;
}
if (rawEmail == null) {
continue;
}
rawEmail = rawEmail.toLowerCase();
if (addedEmails.contains(rawEmail)) {
continue;
}
addedEmails.add(rawEmail);
record.getEmails().add(new PhoneBookEmail(emailId, rawEmail));
}
cur.close();
// Filtering records without contacts
ArrayList<PhoneBookContact> res = new ArrayList<>();
for (PhoneBookContact rec : records) {
if (rec.getPhones().size() > 0 || rec.getEmails().size() > 0) {
res.add(rec);
}
}
Log.d(TAG, "Phone book loaded in " + (SystemClock.uptimeMillis() - start) + " ms in " + (index) + " iterations");
return res;
}
use of im.actor.core.entity.PhoneBookEmail in project actor-platform by actorapp.
the class BookImportActor method onPhoneBookLoaded.
private void onPhoneBookLoaded(List<PhoneBookContact> phoneBook) {
phoneBookReadingIsInProgress = false;
if (ENABLE_LOG) {
Log.d(TAG, "Book load completed");
}
int newPhones = 0;
int newEmails = 0;
for (PhoneBookContact record : phoneBook) {
for (PhoneBookPhone phone : record.getPhones()) {
if (storage.isImported(phone.getNumber())) {
continue;
}
if (importingPhones.contains(phone.getNumber())) {
continue;
}
importingPhones.add(phone.getNumber());
importQueue.add(new ImportPhoneQueueItem(phone.getNumber(), record.getName()));
newPhones++;
}
for (PhoneBookEmail email : record.getEmails()) {
if (storage.isImported(email.getEmail().toLowerCase())) {
continue;
}
if (importingEmails.contains(email.getEmail().toLowerCase())) {
continue;
}
importingEmails.add(email.getEmail().toLowerCase());
importQueue.add(new ImportEmailQueueItem(email.getEmail().toLowerCase(), record.getName()));
newEmails++;
}
}
if (ENABLE_LOG) {
if (newPhones == 0 && newEmails == 0) {
Log.d(TAG, "No new contacts found");
} else {
Log.d(TAG, "Founded new " + (newPhones + newEmails) + " contact records");
}
}
performImportIfRequired();
}
Aggregations