Search in sources :

Example 1 with PhoneContact

use of com.fanap.podchat.cachemodel.PhoneContact in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method addGroupContacts.

private PublishSubject<List<PhoneContact>> addGroupContacts(List<PhoneContact> phoneContacts, String uniqueId) {
    PublishSubject<List<PhoneContact>> subject = PublishSubject.create();
    ArrayList<String> firstNames = new ArrayList<>();
    ArrayList<String> cellphoneNumbers = new ArrayList<>();
    ArrayList<String> lastNames = new ArrayList<>();
    ArrayList<String> typeCodes = new ArrayList<>();
    ArrayList<String> uniqueIds = new ArrayList<>();
    ArrayList<String> emails = new ArrayList<>();
    for (PhoneContact contact : phoneContacts) {
        firstNames.add(contact.getName());
        lastNames.add(contact.getLastName());
        uniqueIds.add(generateUniqueId());
        String phoneNum = String.valueOf(contact.getPhoneNumber());
        if (phoneNum.startsWith("9")) {
            phoneNum = phoneNum.replaceFirst("9", "09");
        }
        cellphoneNumbers.add(phoneNum);
        emails.add("");
        typeCodes.add(getTypeCode());
    }
    Observable<Response<Contacts>> addContactsObservable;
    if (getPlatformHost() != null) {
        if (!Util.isNullOrEmpty(getTypeCode())) {
            addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers, typeCodes);
        } else {
            addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers);
        }
        showLog("Call add contacts " + new Date());
        addContactsObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(contactsResponse -> {
            showLog(">>> Server Respond " + new Date());
            boolean error = false;
            if (contactsResponse.body() != null) {
                error = contactsResponse.body().getHasError();
                showLog(">>>Response: " + contactsResponse.code());
                showLog(">>>ReferenceNumber: " + contactsResponse.body().getReferenceNumber());
                showLog(">>>Ott: " + contactsResponse.body().getOtt());
            }
            if (contactsResponse.isSuccessful()) {
                if (error) {
                    captureError(contactsResponse.body().getMessage(), contactsResponse.body().getErrorCode(), uniqueId);
                    showLog("Error add Contacts: " + contactsResponse.body().getMessage());
                    subject.onError(new Throwable(contactsResponse.body().getMessage()));
                } else {
                    // successful response
                    Contacts contacts = contactsResponse.body();
                    ChatResponse<Contacts> chatResponse = new ChatResponse<>();
                    chatResponse.setResult(contacts);
                    chatResponse.setUniqueId(uniqueId);
                    Runnable updatePhoneContactsDBTask = () -> {
                        try {
                            boolean result = phoneContactDbHelper.addPhoneContacts(phoneContacts);
                            if (!result) {
                                disableCache(() -> phoneContactDbHelper.addPhoneContacts(phoneContacts));
                            }
                        } catch (Exception e) {
                            showErrorLog("Updating Contacts cache failed: " + e.getMessage());
                            onUnknownException(uniqueId, e);
                        }
                    };
                    Runnable updateCachedContactsTask = () -> {
                        if (cache) {
                            try {
                                // messageDatabaseHelper.saveContacts(chatResponse.getResult().getResult(), getExpireAmount());
                                dataSource.saveContactsResultFromServer(chatResponse.getResult().getResult());
                            } catch (Exception e) {
                                showErrorLog("Saving Contacts Failed: " + e.getMessage());
                                onUnknownException(uniqueId, e);
                            }
                        }
                    };
                    new PodThreadManager().addNewTask(updatePhoneContactsDBTask).addNewTask(updateCachedContactsTask).addNewTask(() -> subject.onNext(phoneContacts)).runTasksSynced();
                }
            } else {
                captureError(contactsResponse.message(), contactsResponse.code(), uniqueId);
                showLog("Error add Contacts: " + contactsResponse.raw());
                subject.onError(new Throwable(contactsResponse.message()));
            }
        }, throwable -> {
            captureError(throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId);
            subject.onError(throwable);
        });
    }
    return subject;
}
Also used : PhoneContact(com.fanap.podchat.cachemodel.PhoneContact) PodThreadManager(com.fanap.podchat.util.PodThreadManager) ArrayList(java.util.ArrayList) Date(java.util.Date) JSONException(org.json.JSONException) SentryException(io.sentry.core.protocol.SentryException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PodChatException(com.fanap.podchat.util.PodChatException) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException) ChatResponse(com.fanap.podchat.model.ChatResponse) Response(retrofit2.Response) Contacts(com.fanap.podchat.model.Contacts) ChatResponse(com.fanap.podchat.model.ChatResponse) ResultBlockList(com.fanap.podchat.model.ResultBlockList) RequestGetMentionList(com.fanap.podchat.chat.mention.model.RequestGetMentionList) RequestDeliveredMessageList(com.fanap.podchat.requestobject.RequestDeliveredMessageList) ArrayList(java.util.ArrayList) RequestBlockList(com.fanap.podchat.requestobject.RequestBlockList) RequestGetHashTagList(com.fanap.podchat.chat.hashtag.model.RequestGetHashTagList) RequestSeenMessageList(com.fanap.podchat.requestobject.RequestSeenMessageList) List(java.util.List)

Example 2 with PhoneContact

use of com.fanap.podchat.cachemodel.PhoneContact in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method addContacts.

/**
 * Add list of contacts with their mobile numbers and their cellphoneNumbers
 */
private void addContacts(List<PhoneContact> phoneContacts, String uniqueId) {
    ArrayList<String> firstNames = new ArrayList<>();
    ArrayList<String> cellphoneNumbers = new ArrayList<>();
    ArrayList<String> lastNames = new ArrayList<>();
    ArrayList<String> typeCodes = new ArrayList<>();
    ArrayList<String> uniqueIds = new ArrayList<>();
    ArrayList<String> emails = new ArrayList<>();
    for (PhoneContact contact : phoneContacts) {
        firstNames.add(contact.getName());
        lastNames.add(contact.getLastName());
        uniqueIds.add(generateUniqueId());
        String phoneNum = String.valueOf(contact.getPhoneNumber());
        if (phoneNum.startsWith("9")) {
            phoneNum = phoneNum.replaceFirst("9", "09");
        }
        cellphoneNumbers.add(phoneNum);
        emails.add("");
        typeCodes.add(getTypeCode());
    }
    Observable<Response<Contacts>> addContactsObservable;
    if (getPlatformHost() != null) {
        if (!Util.isNullOrEmpty(getTypeCode())) {
            addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers, typeCodes);
        } else {
            addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers);
        }
        showLog("Call add contacts " + new Date());
        addContactsObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(contactsResponse -> {
            showLog(">>> Server Respond " + new Date());
            boolean error = false;
            if (contactsResponse.body() != null) {
                error = contactsResponse.body().getHasError();
                showLog(">>>Response: " + contactsResponse.code());
                showLog(">>>ReferenceNumber: " + contactsResponse.body().getReferenceNumber());
                showLog(">>>Ott: " + contactsResponse.body().getOtt());
            }
            if (contactsResponse.isSuccessful()) {
                if (error) {
                    captureError(contactsResponse.body().getMessage(), contactsResponse.body().getErrorCode(), uniqueId);
                    showLog("Error add Contacts: " + contactsResponse.body().getMessage());
                } else {
                    // successful response
                    Contacts contacts = contactsResponse.body();
                    ChatResponse<Contacts> chatResponse = new ChatResponse<>();
                    chatResponse.setResult(contacts);
                    chatResponse.setUniqueId(uniqueId);
                    Runnable updatePhoneContactsDBTask = () -> {
                        try {
                            boolean result = phoneContactDbHelper.addPhoneContacts(phoneContacts);
                            if (!result) {
                                disableCache(() -> phoneContactDbHelper.addPhoneContacts(phoneContacts));
                            }
                        } catch (Exception e) {
                            showErrorLog("Updating Contacts cache failed: " + e.getMessage());
                            onUnknownException(uniqueId, e);
                        }
                    };
                    Runnable updateCachedContactsTask = () -> {
                        if (cache) {
                            try {
                                // messageDatabaseHelper.saveContacts(chatResponse.getResult().getResult(), getExpireAmount());
                                dataSource.saveContactsResultFromServer(chatResponse.getResult().getResult());
                            } catch (Exception e) {
                                showErrorLog("Saving Contacts Failed: " + e.getMessage());
                                onUnknownException(uniqueId, e);
                            }
                        }
                    };
                    new PodThreadManager().addNewTask(updatePhoneContactsDBTask).addNewTask(updateCachedContactsTask).runTasksSynced();
                    String contactsJson = gson.toJson(chatResponse);
                    listenerManager.callOnSyncContact(contactsJson, chatResponse);
                    showLog("SYNC_CONTACT_COMPLETED", contactsJson);
                }
            } else {
                captureError(contactsResponse.message(), contactsResponse.code(), uniqueId);
                showLog("Error add Contacts: " + contactsResponse.raw());
            }
        }, throwable -> captureError(throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId));
    }
}
Also used : PhoneContact(com.fanap.podchat.cachemodel.PhoneContact) PodThreadManager(com.fanap.podchat.util.PodThreadManager) ArrayList(java.util.ArrayList) Date(java.util.Date) JSONException(org.json.JSONException) SentryException(io.sentry.core.protocol.SentryException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PodChatException(com.fanap.podchat.util.PodChatException) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException) ChatResponse(com.fanap.podchat.model.ChatResponse) Response(retrofit2.Response) Contacts(com.fanap.podchat.model.Contacts) ChatResponse(com.fanap.podchat.model.ChatResponse)

Example 3 with PhoneContact

use of com.fanap.podchat.cachemodel.PhoneContact in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method getPhoneContact.

/**
 * Get the list of the Device Contact
 */
private void getPhoneContact(Context context, String uniqueId, OnContactLoaded listener) {
    try {
        showLog(">>> Getting phone contacts ");
        List<PhoneContact> cachePhoneContacts = new ArrayList<>();
        PhoneContactAsyncTask task = new PhoneContactAsyncTask(phoneContactDbHelper, contacts -> {
            String firstName;
            String phoneNumber;
            String lastName;
            String empty = "";
            int version;
            ArrayList<PhoneContact> newPhoneContact = new ArrayList<>();
            HashMap<String, PhoneContact> newContactsMap = new HashMap<>();
            showLog("#" + contacts.size() + " Contacts Loaded From Cache");
            cachePhoneContacts.addAll(contacts);
            HashMap<String, PhoneContact> mapCacheContactKeeper = new HashMap<>();
            if (cachePhoneContacts.size() > 0) {
                for (PhoneContact contact : cachePhoneContacts) {
                    mapCacheContactKeeper.put(contact.getPhoneNumber(), contact);
                }
            }
            Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            if (cursor == null) {
                showLog("Contacts loader cursor is null");
                listener.onLoad(newPhoneContact);
                return;
            }
            while (cursor.moveToNext()) {
                firstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                lastName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                version = cursor.getInt(cursor.getColumnIndex(ContactsContract.RawContacts.VERSION));
                phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                PhoneContact phoneContact = new PhoneContact();
                if (!Util.isNullOrEmpty(phoneNumber)) {
                    phoneContact.setPhoneNumber(phoneNumber);
                    if (!Util.isNullOrEmpty(firstName)) {
                        phoneContact.setName(firstName);
                    } else {
                        phoneContact.setName(empty);
                    }
                    if (!Util.isNullOrEmpty(lastName)) {
                        phoneContact.setLastName(lastName);
                    } else {
                        phoneContact.setLastName(empty);
                    }
                    if (!Util.isNullOrEmpty(version)) {
                        phoneContact.setVersion(version);
                    }
                    if (cachePhoneContacts.size() > 0) {
                        // if its not in PhoneContactCache and its a contact that added recently
                        if (mapCacheContactKeeper.get(phoneNumber) != null) {
                            if (version != mapCacheContactKeeper.get(phoneNumber).getVersion()) {
                                newContactsMap.put(phoneNumber, phoneContact);
                            }
                        } else {
                            newContactsMap.put(phoneNumber, phoneContact);
                        }
                    } else {
                        newContactsMap.put(phoneNumber, phoneContact);
                    }
                }
            }
            cursor.close();
            // retrieve unique contacts
            for (String key : newContactsMap.keySet()) {
                if (!mapCacheContactKeeper.containsKey(key))
                    newPhoneContact.add(newContactsMap.get(key));
            }
            showLog("#" + newPhoneContact.size() + " New Contact Found");
            listener.onLoad(newPhoneContact);
        });
        task.execute();
    } catch (Exception e) {
        showErrorLog(e.getMessage());
        onUnknownException(uniqueId, e);
    }
}
Also used : PhoneContact(com.fanap.podchat.cachemodel.PhoneContact) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) JSONException(org.json.JSONException) SentryException(io.sentry.core.protocol.SentryException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PodChatException(com.fanap.podchat.util.PodChatException) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException)

Example 4 with PhoneContact

use of com.fanap.podchat.cachemodel.PhoneContact in project pod-chat-android-sdk by FanapSoft.

the class ExampleUnitTest method contactsEquality.

@Test
public void contactsEquality() {
    PhoneContact a = new PhoneContact();
    a.setName("a contact");
    a.setLastName("a lastname");
    a.setPhoneNumber("+98 915 777 0684");
    a.setVersion(150);
    PhoneContact b = new PhoneContact();
    b.setName("b contact");
    b.setLastName("b lastname");
    b.setPhoneNumber("+98 915 777 0684");
    b.setVersion(750);
    boolean e = a.equals(b);
    System.out.println(e);
    assertEquals(a, b);
}
Also used : PhoneContact(com.fanap.podchat.cachemodel.PhoneContact) Test(org.junit.Test)

Example 5 with PhoneContact

use of com.fanap.podchat.cachemodel.PhoneContact in project pod-chat-android-sdk by FanapSoft.

the class ExampleUnitTest method contactsEqualityInList.

@Test
public void contactsEqualityInList() {
    HashMap<String, PhoneContact> phoneContacts = new HashMap<>();
    List<PhoneContact> list = new ArrayList<>();
    PhoneContact a = new PhoneContact();
    a.setName("a contact");
    a.setLastName("a lastname");
    a.setPhoneNumber("+98 915 777 0684");
    a.setVersion(150);
    PhoneContact b = new PhoneContact();
    b.setName("b contact");
    b.setLastName("b lastname");
    b.setPhoneNumber("+98 915 777 0684");
    b.setVersion(750);
    phoneContacts.put(a.getPhoneNumber(), a);
    list.add(b);
    phoneContacts.put(b.getPhoneNumber(), b);
    list.add(a);
    System.out.println(phoneContacts.size());
    assertEquals(phoneContacts.size(), 1);
    assertEquals(phoneContacts.get("+98 915 777 0684").getName(), "b contact");
    System.out.println("list + " + list);
    Collections.sort(list, (o1, o2) -> Long.compare(o1.getVersion(), o2.getVersion()));
    System.out.println("list + " + list);
}
Also used : PhoneContact(com.fanap.podchat.cachemodel.PhoneContact) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

PhoneContact (com.fanap.podchat.cachemodel.PhoneContact)5 ArrayList (java.util.ArrayList)4 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)3 PodChatException (com.fanap.podchat.util.PodChatException)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 SentryException (io.sentry.core.protocol.SentryException)3 IOException (java.io.IOException)3 JSONException (org.json.JSONException)3 ChatResponse (com.fanap.podchat.model.ChatResponse)2 Contacts (com.fanap.podchat.model.Contacts)2 PodThreadManager (com.fanap.podchat.util.PodThreadManager)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Response (retrofit2.Response)2 Cursor (android.database.Cursor)1 RequestGetHashTagList (com.fanap.podchat.chat.hashtag.model.RequestGetHashTagList)1 RequestGetMentionList (com.fanap.podchat.chat.mention.model.RequestGetMentionList)1 ResultBlockList (com.fanap.podchat.model.ResultBlockList)1 RequestBlockList (com.fanap.podchat.requestobject.RequestBlockList)1