Search in sources :

Example 1 with PhoneBookPhone

use of im.actor.core.entity.PhoneBookPhone 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);
            });
        }
    });
}
Also used : ModuleContext(im.actor.core.modules.ModuleContext) ContactRecord(im.actor.core.entity.ContactRecord) PhoneBookContact(im.actor.core.entity.PhoneBookContact) ApiUser(im.actor.core.api.ApiUser) UpdateUserContactsChanged(im.actor.core.api.updates.UpdateUserContactsChanged) ApiMapValue(im.actor.core.api.ApiMapValue) ApiBotCommand(im.actor.core.api.ApiBotCommand) PhoneBookEmail(im.actor.core.entity.PhoneBookEmail) PromisesArray(im.actor.runtime.promise.PromisesArray) RouterLoadFullUser(im.actor.core.modules.users.router.entity.RouterLoadFullUser) UpdateContactRegistered(im.actor.core.api.updates.UpdateContactRegistered) UpdateUserNameChanged(im.actor.core.api.updates.UpdateUserNameChanged) ContactRecordType(im.actor.core.entity.ContactRecordType) Tuple2(im.actor.runtime.function.Tuple2) UpdateUserBlocked(im.actor.core.api.updates.UpdateUserBlocked) Void(im.actor.runtime.actors.messages.Void) Verified(im.actor.runtime.annotations.Verified) UserPhone(im.actor.core.viewmodel.UserPhone) ResponseLoadFullUsers(im.actor.core.api.rpc.ResponseLoadFullUsers) UpdateUserAvatarChanged(im.actor.core.api.updates.UpdateUserAvatarChanged) List(java.util.List) UpdateUserAboutChanged(im.actor.core.api.updates.UpdateUserAboutChanged) ModuleActor(im.actor.core.modules.ModuleActor) ArrayListUserPhone(im.actor.core.viewmodel.generics.ArrayListUserPhone) UpdateUserFullExtChanged(im.actor.core.api.updates.UpdateUserFullExtChanged) UserEmail(im.actor.core.viewmodel.UserEmail) Peer(im.actor.core.entity.Peer) UpdateUserExtChanged(im.actor.core.api.updates.UpdateUserExtChanged) MessageState(im.actor.core.entity.MessageState) Message(im.actor.core.entity.Message) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) ApiAvatar(im.actor.core.api.ApiAvatar) ApiContactRecord(im.actor.core.api.ApiContactRecord) UpdateUserTimeZoneChanged(im.actor.core.api.updates.UpdateUserTimeZoneChanged) Function(im.actor.runtime.function.Function) ServiceUserRegistered(im.actor.core.entity.content.ServiceUserRegistered) PhoneBookProvider(im.actor.core.providers.PhoneBookProvider) Promise(im.actor.runtime.promise.Promise) UpdateUserBotCommandsChanged(im.actor.core.api.updates.UpdateUserBotCommandsChanged) UpdateUserNickChanged(im.actor.core.api.updates.UpdateUserNickChanged) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) User(im.actor.core.entity.User) UpdateUserLocalNameChanged(im.actor.core.api.updates.UpdateUserLocalNameChanged) ArrayListUserEmail(im.actor.core.viewmodel.generics.ArrayListUserEmail) JavaUtil.equalsE(im.actor.core.util.JavaUtil.equalsE) UpdateUserUnblocked(im.actor.core.api.updates.UpdateUserUnblocked) RequestLoadFullUsers(im.actor.core.api.rpc.RequestLoadFullUsers) ContactsSyncActor(im.actor.core.modules.contacts.ContactsSyncActor) UserVM(im.actor.core.viewmodel.UserVM) RouterFetchMissingUsers(im.actor.core.modules.users.router.entity.RouterFetchMissingUsers) BookImportStorage(im.actor.core.modules.contacts.entity.BookImportStorage) RouterApplyUsers(im.actor.core.modules.users.router.entity.RouterApplyUsers) UpdateUserPreferredLanguagesChanged(im.actor.core.api.updates.UpdateUserPreferredLanguagesChanged) Log(im.actor.runtime.Log) PhoneBookPhone(im.actor.core.entity.PhoneBookPhone) Update(im.actor.core.network.parser.Update) RouterUserUpdate(im.actor.core.modules.users.router.entity.RouterUserUpdate) Promise(im.actor.runtime.promise.Promise) PhoneBookContact(im.actor.core.entity.PhoneBookContact) List(java.util.List) ArrayList(java.util.ArrayList) PhoneBookEmail(im.actor.core.entity.PhoneBookEmail) Void(im.actor.runtime.actors.messages.Void) ContactRecord(im.actor.core.entity.ContactRecord) ApiContactRecord(im.actor.core.api.ApiContactRecord) PhoneBookPhone(im.actor.core.entity.PhoneBookPhone) Verified(im.actor.runtime.annotations.Verified)

Example 2 with PhoneBookPhone

use of im.actor.core.entity.PhoneBookPhone 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();
}
Also used : PhoneBookContact(im.actor.core.entity.PhoneBookContact) PhoneBookEmail(im.actor.core.entity.PhoneBookEmail) PhoneBookPhone(im.actor.core.entity.PhoneBookPhone)

Example 3 with PhoneBookPhone

use of im.actor.core.entity.PhoneBookPhone 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;
}
Also used : PhoneBookContact(im.actor.core.entity.PhoneBookContact) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) NumberParseException(com.google.i18n.phonenumbers.NumberParseException) ContentResolver(android.content.ContentResolver) PhoneBookPhone(im.actor.core.entity.PhoneBookPhone) NumberParseException(com.google.i18n.phonenumbers.NumberParseException) PhoneBookEmail(im.actor.core.entity.PhoneBookEmail) Phonenumber(com.google.i18n.phonenumbers.Phonenumber) HashSet(java.util.HashSet)

Aggregations

PhoneBookContact (im.actor.core.entity.PhoneBookContact)3 PhoneBookEmail (im.actor.core.entity.PhoneBookEmail)3 PhoneBookPhone (im.actor.core.entity.PhoneBookPhone)3 ContentResolver (android.content.ContentResolver)1 Cursor (android.database.Cursor)1 NumberParseException (com.google.i18n.phonenumbers.NumberParseException)1 Phonenumber (com.google.i18n.phonenumbers.Phonenumber)1 ApiAvatar (im.actor.core.api.ApiAvatar)1 ApiBotCommand (im.actor.core.api.ApiBotCommand)1 ApiContactRecord (im.actor.core.api.ApiContactRecord)1 ApiMapValue (im.actor.core.api.ApiMapValue)1 ApiUser (im.actor.core.api.ApiUser)1 ApiUserOutPeer (im.actor.core.api.ApiUserOutPeer)1 RequestLoadFullUsers (im.actor.core.api.rpc.RequestLoadFullUsers)1 ResponseLoadFullUsers (im.actor.core.api.rpc.ResponseLoadFullUsers)1 UpdateContactRegistered (im.actor.core.api.updates.UpdateContactRegistered)1 UpdateUserAboutChanged (im.actor.core.api.updates.UpdateUserAboutChanged)1 UpdateUserAvatarChanged (im.actor.core.api.updates.UpdateUserAvatarChanged)1 UpdateUserBlocked (im.actor.core.api.updates.UpdateUserBlocked)1 UpdateUserBotCommandsChanged (im.actor.core.api.updates.UpdateUserBotCommandsChanged)1