Search in sources :

Example 1 with Contacts

use of com.fanap.podchat.model.Contacts in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method addContact.

/**
 * Add one contact to the contact list
 * <p>
 * firstName and cellphoneNumber or email or username are required
 *
 * @param username        username of contact
 * @param firstName       Notice: if just put fistName without lastName its ok.
 * @param lastName        last name of the contact
 * @param cellphoneNumber Notice: If you just  put the cellPhoneNumber doesn't necessary to add email
 * @param email           email of the contact
 */
@Deprecated
public String addContact(String firstName, String lastName, String cellphoneNumber, String email, String typeCode, String username) {
    if (Util.isNullOrEmpty(firstName)) {
        firstName = "";
    }
    if (Util.isNullOrEmpty(lastName)) {
        lastName = "";
    }
    if (Util.isNullOrEmpty(email)) {
        email = "";
    }
    if (Util.isNullOrEmpty(cellphoneNumber)) {
        cellphoneNumber = "";
    }
    if (Util.isNullOrEmpty(username)) {
        username = "";
    }
    String uniqueId = generateUniqueId();
    typeCode = Util.isNullOrEmpty(typeCode) ? getTypeCode() : typeCode;
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("uniqueId", uniqueId);
    jsonObject.addProperty("tokenIssuer", 1);
    jsonObject.addProperty("firstName", firstName);
    jsonObject.addProperty("lastName", lastName);
    if (!Util.isNullOrEmpty(cellphoneNumber))
        jsonObject.addProperty("cellphoneNumber", cellphoneNumber);
    if (!Util.isNullOrEmpty(email))
        jsonObject.addProperty("email", email);
    if (!Util.isNullOrEmpty(typeCode))
        jsonObject.addProperty("typeCode", typeCode);
    if (!Util.isNullOrEmpty(username))
        jsonObject.addProperty("username", username);
    showLog("SEND_ADD_CONTACT", getJsonForLog(jsonObject));
    Observable<Response<Contacts>> addContactObservable;
    if (chatReady) {
        if (!Util.isNullOrEmpty(username)) {
            addContactObservable = contactApi.addContactWithUserName(getToken(), 1, firstName, lastName, username, uniqueId, typeCode, "", "");
        } else if (Util.isNullOrEmpty(typeCode)) {
            addContactObservable = contactApi.addContact(getToken(), 1, firstName, lastName, email, uniqueId, cellphoneNumber);
        } else {
            addContactObservable = contactApi.addContact(getToken(), 1, firstName, lastName, email, uniqueId, cellphoneNumber, typeCode);
        }
        addContactObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(addContactResponse -> {
            if (addContactResponse.isSuccessful()) {
                Contacts contacts = addContactResponse.body();
                if (contacts != null) {
                    if (!contacts.getHasError()) {
                        ChatResponse<ResultAddContact> chatResponse = Util.getReformatOutPutAddContact(contacts, uniqueId);
                        String contactsJson = gson.toJson(chatResponse);
                        listenerManager.callOnAddContact(contactsJson, chatResponse);
                        showLog("RECEIVED_ADD_CONTACT", contactsJson);
                        if (cache) {
                            // messageDatabaseHelper.saveContact(chatResponse.getResult().getContact(), getExpireAmount());
                            dataSource.saveContactResultFromServer(chatResponse.getResult().getContact());
                        }
                    } else {
                        captureError(contacts.getMessage(), contacts.getErrorCode(), uniqueId);
                    }
                }
            }
        }, (Throwable throwable) -> {
            captureError(throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId, throwable);
            Log.e(TAG, throwable.getMessage());
        });
    } else {
        captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
    }
    return uniqueId;
}
Also used : ChatResponse(com.fanap.podchat.model.ChatResponse) Response(retrofit2.Response) Contacts(com.fanap.podchat.model.Contacts) JsonObject(com.google.gson.JsonObject) ResultAddContact(com.fanap.podchat.model.ResultAddContact)

Example 2 with Contacts

use of com.fanap.podchat.model.Contacts 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 3 with Contacts

use of com.fanap.podchat.model.Contacts 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 4 with Contacts

use of com.fanap.podchat.model.Contacts in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method syncContact.

/**
 * First we get the contact from server then at the respond of that
 *
 * @param activity its for check the permission of reading the phone contact
 *                 {@link #getPhoneContact(Context, String, OnContactLoaded)}
 */
public String syncContact(Activity activity) {
    showLog(">>> Start Syncing... " + new Date());
    String uniqueId = generateUniqueId();
    if (Permission.Check_READ_CONTACTS(activity)) {
        if (chatReady) {
            getPhoneContact(getContext(), uniqueId, phoneContacts -> {
                if (phoneContacts.size() > 0) {
                    if (sentryResponseLog) {
                        showLog(">>> Synchronizing " + phoneContacts.size() + " with server at " + new Date());
                    } else {
                        showLog(">>> Synchronizing");
                    }
                    handleAddContacts(uniqueId, phoneContacts);
                } else {
                    showLog(">>> No New Contact Found. Everything is synced ");
                    ChatResponse<Contacts> chatResponse = new ChatResponse<>();
                    chatResponse.setUniqueId(uniqueId);
                    Contacts contacts = new Contacts();
                    contacts.setCount(0);
                    contacts.setResult(new ArrayList<>());
                    chatResponse.setResult(contacts);
                    listenerManager.callOnSyncContact(gson.toJson(chatResponse), chatResponse);
                    if (log)
                        showLog("SYNC_CONTACT_COMPLETED");
                }
            });
        } else {
            onChatNotReady(uniqueId);
        }
    } else {
        String jsonError = captureError(ChatConstant.ERROR_READ_CONTACT_PERMISSION, ChatConstant.ERROR_CODE_READ_CONTACT_PERMISSION, uniqueId);
        Permission.Request_READ_CONTACTS(activity, READ_CONTACTS_CODE);
        if (log)
            Log.e(TAG, jsonError);
    }
    return uniqueId;
}
Also used : Contacts(com.fanap.podchat.model.Contacts) ChatResponse(com.fanap.podchat.model.ChatResponse) Date(java.util.Date)

Example 5 with Contacts

use of com.fanap.podchat.model.Contacts in project pod-chat-android-sdk by FanapSoft.

the class ContactManager method prepareAddContactResponse.

public static ChatResponse<ResultAddContact> prepareAddContactResponse(ChatMessage chatMessage) {
    Contacts contacts = App.getGson().fromJson(chatMessage.getContent(), Contacts.class);
    ChatResponse<ResultAddContact> chatResponse = new ChatResponse<>();
    chatResponse.setUniqueId(chatMessage.getUniqueId());
    ResultAddContact resultAddContact = new ResultAddContact();
    resultAddContact.setContentCount(1);
    Contact contact = new Contact();
    contact.setCellphoneNumber(contacts.getResult().get(0).getCellphoneNumber());
    contact.setEmail(contacts.getResult().get(0).getEmail());
    contact.setFirstName(contacts.getResult().get(0).getFirstName());
    contact.setId(contacts.getResult().get(0).getId());
    contact.setLastName(contacts.getResult().get(0).getLastName());
    contact.setUniqueId(contacts.getResult().get(0).getUniqueId());
    // add linked user
    LinkedUser linkedUser = contacts.getResult().get(0).getLinkedUser();
    if (linkedUser != null)
        contact.setLinkedUser(linkedUser);
    resultAddContact.setContact(contact);
    chatResponse.setResult(resultAddContact);
    return chatResponse;
}
Also used : Contacts(com.fanap.podchat.model.Contacts) ChatResponse(com.fanap.podchat.model.ChatResponse) ResultAddContact(com.fanap.podchat.model.ResultAddContact) LinkedUser(com.fanap.podchat.mainmodel.LinkedUser) BlockedContact(com.fanap.podchat.mainmodel.BlockedContact) ResultRemoveContact(com.fanap.podchat.model.ResultRemoveContact) Contact(com.fanap.podchat.mainmodel.Contact) ResultAddContact(com.fanap.podchat.model.ResultAddContact) RequestAddContact(com.fanap.podchat.requestobject.RequestAddContact)

Aggregations

ChatResponse (com.fanap.podchat.model.ChatResponse)5 Contacts (com.fanap.podchat.model.Contacts)5 Date (java.util.Date)3 Response (retrofit2.Response)3 PhoneContact (com.fanap.podchat.cachemodel.PhoneContact)2 ResultAddContact (com.fanap.podchat.model.ResultAddContact)2 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)2 PodChatException (com.fanap.podchat.util.PodChatException)2 PodThreadManager (com.fanap.podchat.util.PodThreadManager)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 SentryException (io.sentry.core.protocol.SentryException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 JSONException (org.json.JSONException)2 RequestGetHashTagList (com.fanap.podchat.chat.hashtag.model.RequestGetHashTagList)1 RequestGetMentionList (com.fanap.podchat.chat.mention.model.RequestGetMentionList)1 BlockedContact (com.fanap.podchat.mainmodel.BlockedContact)1 Contact (com.fanap.podchat.mainmodel.Contact)1 LinkedUser (com.fanap.podchat.mainmodel.LinkedUser)1 ResultBlockList (com.fanap.podchat.model.ResultBlockList)1