Search in sources :

Example 56 with ChatResponse

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

the class ChatCore method handleRemoveFromThread.

private void handleRemoveFromThread(ChatMessage chatMessage) {
    ChatResponse<ResultThread> chatResponse = new ChatResponse<>();
    ResultThread resultThread = new ResultThread();
    Thread thread = new Thread();
    thread.setId(chatMessage.getSubjectId());
    resultThread.setThread(thread);
    String content = gson.toJson(chatResponse);
    if (cache) {
        messageDatabaseHelper.deleteThread(chatMessage.getSubjectId());
    }
    if (sentryResponseLog) {
        showLog("RECEIVED_REMOVED_FROM_THREAD", content);
    } else {
        showLog("RECEIVED_REMOVED_FROM_THREAD");
    }
    listenerManager.callOnRemovedFromThread(content, chatResponse);
}
Also used : ChatResponse(com.fanap.podchat.model.ChatResponse) ResultThread(com.fanap.podchat.model.ResultThread) RequestCreateThread(com.fanap.podchat.requestobject.RequestCreateThread) ResultPinThread(com.fanap.podchat.chat.pin.pin_thread.model.ResultPinThread) Thread(com.fanap.podchat.mainmodel.Thread) RequestLeaveThread(com.fanap.podchat.requestobject.RequestLeaveThread) HandlerThread(android.os.HandlerThread) PinThread(com.fanap.podchat.chat.pin.pin_thread.PinThread) ResultLeaveThread(com.fanap.podchat.model.ResultLeaveThread) RequestMuteThread(com.fanap.podchat.requestobject.RequestMuteThread) ResultJoinPublicThread(com.fanap.podchat.chat.thread.public_thread.ResultJoinPublicThread) RequestJoinPublicThread(com.fanap.podchat.chat.thread.public_thread.RequestJoinPublicThread) PublicThread(com.fanap.podchat.chat.thread.public_thread.PublicThread) RequestThread(com.fanap.podchat.requestobject.RequestThread) ResultThread(com.fanap.podchat.model.ResultThread) RequestPinThread(com.fanap.podchat.chat.pin.pin_thread.model.RequestPinThread)

Example 57 with ChatResponse

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

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

the class ChatCore method handleOutPutBlock.

private void handleOutPutBlock(ChatMessage chatMessage, String messageUniqueId) {
    BlockedContact contact = gson.fromJson(chatMessage.getContent(), BlockedContact.class);
    ChatResponse<ResultBlock> chatResponse = new ChatResponse<>();
    ResultBlock resultBlock = new ResultBlock();
    resultBlock.setContact(contact);
    chatResponse.setResult(resultBlock);
    chatResponse.setErrorCode(0);
    chatResponse.setHasError(false);
    chatResponse.setUniqueId(chatMessage.getUniqueId());
    String jsonBlock = gson.toJson(chatResponse);
    if (cache) {
        dataSource.saveBlockedContactResultFromServer(contact);
    }
    if (sentryResponseLog) {
        showLog("RECEIVE_BLOCK", jsonBlock);
    } else {
        showLog("RECEIVE_BLOCK");
    }
    messageCallbacks.remove(messageUniqueId);
    listenerManager.callOnBlock(jsonBlock, chatResponse);
}
Also used : BlockedContact(com.fanap.podchat.mainmodel.BlockedContact) ChatResponse(com.fanap.podchat.model.ChatResponse) ResultBlock(com.fanap.podchat.model.ResultBlock)

Example 59 with ChatResponse

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

the class ChatCore method uploadFileProgress.

/**
 * It uploads file and it shows progress of the file downloading
 */
public String uploadFileProgress(RequestUploadFile request, @Nullable ProgressHandler.onProgressFile handler) {
    String uniqueId = generateUniqueId();
    if (needReadStoragePermission(request.getActivity())) {
        String jsonError = captureError(ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
        ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
        if (handler != null) {
            handler.onError(jsonError, error);
        }
        return uniqueId;
    }
    if (!chatReady) {
        onChatNotReady(uniqueId);
        return uniqueId;
    }
    if (getPodSpaceServer() == null) {
        captureError("File server is null", 0, uniqueId);
        return uniqueId;
    }
    try {
        Subscription subscription = PodUploader.uploadPublicToPodSpace(uniqueId, request.getFileUri(), context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, request.isPublic(), new PodUploader.IPodUploadFileToPodSpace() {

            @Override
            public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length) {
                ResultFile resultFile = PodUploader.generateFileUploadResult(response);
                FileUpload result = new FileUpload();
                result.setResult(resultFile);
                ChatResponse<ResultFile> chatResponse = new ChatResponse<>();
                resultFile.setUrl(getPodSpaceFileUrl(resultFile.getHashCode()));
                showLog("FINISH_UPLOAD_FILE", gson.toJson(resultFile));
                chatResponse.setResult(resultFile);
                chatResponse.setUniqueId(uniqueId);
                if (handler != null) {
                    handler.onFinish(gson.toJson(chatResponse), result);
                }
                listenerManager.callOnUploadFile(gson.toJson(resultFile), chatResponse);
            }

            @Override
            public void onFailure(String cause, Throwable t) {
                String jsonError = captureError(cause, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId, t);
                ErrorOutPut error = new ErrorOutPut(true, cause, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
                if (handler != null) {
                    handler.onError(jsonError, error);
                }
            }

            @Override
            public void onUploadStarted(String mimeType, File file, long length) {
                showLog("UPLOADING_FILE");
            }

            @Override
            public void onProgressUpdate(int progress, int totalBytesSent, int totalBytesToSend) {
                if (handler != null) {
                    handler.onProgressUpdate(progress);
                    handler.onProgress(uniqueId, progress, totalBytesSent, totalBytesToSend);
                }
            }
        });
        initCancelUpload(uniqueId, subscription);
    } catch (Exception e) {
        String jsonError = captureError(ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId, e);
        ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId);
        if (handler != null) {
            handler.onError(jsonError, error);
        }
    }
    return uniqueId;
}
Also used : ResultFile(com.fanap.podchat.model.ResultFile) PodUploader(com.fanap.podchat.chat.file_manager.upload_file.PodUploader) 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) UploadToPodSpaceResult(com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult) ChatResponse(com.fanap.podchat.model.ChatResponse) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut) Subscription(rx.Subscription) MetaDataFile(com.fanap.podchat.model.MetaDataFile) RequestUploadFile(com.fanap.podchat.requestobject.RequestUploadFile) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) ResultFile(com.fanap.podchat.model.ResultFile) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile) RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) RequestCreateThreadWithFile(com.fanap.podchat.requestobject.RequestCreateThreadWithFile) File(java.io.File) ResultImageFile(com.fanap.podchat.model.ResultImageFile) FileUpload(com.fanap.podchat.mainmodel.FileUpload) LFileUpload(com.fanap.podchat.localmodel.LFileUpload)

Example 60 with ChatResponse

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

the class ChatCore method publishThreadsList.

private String publishThreadsList(String uniqueId, Long finalOffset, ThreadManager.ThreadResponse cacheThreadResponse) {
    ChatResponse<ResultThreads> chatResponse = new ChatResponse<>();
    List<Thread> threadList = cacheThreadResponse.getThreadList();
    chatResponse.setCache(true);
    long contentCount = cacheThreadResponse.getContentCount();
    ResultThreads resultThreads = new ResultThreads();
    resultThreads.setThreads(threadList);
    resultThreads.setContentCount(contentCount);
    chatResponse.setCache(true);
    resultThreads.setHasNext(threadList.size() + finalOffset < contentCount);
    resultThreads.setNextOffset(finalOffset + threadList.size());
    chatResponse.setResult(resultThreads);
    chatResponse.setUniqueId(uniqueId);
    String result = gson.toJson(chatResponse);
    listenerManager.callOnGetThread(result, chatResponse);
    return result;
}
Also used : ResultThreads(com.fanap.podchat.model.ResultThreads) ChatResponse(com.fanap.podchat.model.ChatResponse) RequestCreateThread(com.fanap.podchat.requestobject.RequestCreateThread) ResultPinThread(com.fanap.podchat.chat.pin.pin_thread.model.ResultPinThread) Thread(com.fanap.podchat.mainmodel.Thread) RequestLeaveThread(com.fanap.podchat.requestobject.RequestLeaveThread) HandlerThread(android.os.HandlerThread) PinThread(com.fanap.podchat.chat.pin.pin_thread.PinThread) ResultLeaveThread(com.fanap.podchat.model.ResultLeaveThread) RequestMuteThread(com.fanap.podchat.requestobject.RequestMuteThread) ResultJoinPublicThread(com.fanap.podchat.chat.thread.public_thread.ResultJoinPublicThread) RequestJoinPublicThread(com.fanap.podchat.chat.thread.public_thread.RequestJoinPublicThread) PublicThread(com.fanap.podchat.chat.thread.public_thread.PublicThread) RequestThread(com.fanap.podchat.requestobject.RequestThread) ResultThread(com.fanap.podchat.model.ResultThread) RequestPinThread(com.fanap.podchat.chat.pin.pin_thread.model.RequestPinThread)

Aggregations

ChatResponse (com.fanap.podchat.model.ChatResponse)162 ChatListener (com.fanap.podchat.chat.ChatListener)38 Test (org.junit.Test)38 ArrayList (java.util.ArrayList)35 Thread (com.fanap.podchat.mainmodel.Thread)29 JsonSyntaxException (com.google.gson.JsonSyntaxException)29 ResultHistory (com.fanap.podchat.model.ResultHistory)28 LargeTest (android.support.test.filters.LargeTest)27 RequestThread (com.fanap.podchat.requestobject.RequestThread)27 PodChatException (com.fanap.podchat.util.PodChatException)26 MessageVO (com.fanap.podchat.mainmodel.MessageVO)25 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)23 IOException (java.io.IOException)23 JSONException (org.json.JSONException)22 ResultThreads (com.fanap.podchat.model.ResultThreads)21 RequestGetHistory (com.fanap.podchat.requestobject.RequestGetHistory)21 SentryException (io.sentry.core.protocol.SentryException)21 FlakyTest (android.support.test.filters.FlakyTest)19 MediumTest (android.support.test.filters.MediumTest)19 Activity (android.app.Activity)17