Search in sources :

Example 1 with UploadingQueueCache

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

the class ChatCore method retryUpload.

/**
 * It retry upload that they didn't send
 */
// public void retryUpload(RetryUpload retry, ProgressHandler.sendFileMessage handler) {
// 
// Runnable retryTask = () -> {
// String uniqueId = retry.getUniqueId();
// Activity activity = retry.getActivity();
// 
// UploadingQueueCache uploadingQ;
// if (cache) {
// uploadingQ = messageDatabaseHelper.getUploadingQ(uniqueId);
// } else {
// uploadingQ = uploadingQList.get(uniqueId);
// }
// 
// if (uploadingQ != null) {
// long messageId = uploadingQ.getId();
// int messageType = uploadingQ.getMessageType();
// long threadId = uploadingQ.getThreadId();
// String message = uploadingQ.getMessage();
// String systemMetadata = uploadingQ.getSystemMetadata();
// MetaDataFile metaDataFile = gson.fromJson(systemMetadata, MetaDataFile.class);
// String link = metaDataFile.getFile().getLink();
// String mimeType = metaDataFile.getFile().getMimeType();
// 
// LFileUpload lFileUpload = new LFileUpload();
// lFileUpload.setActivity(activity);
// lFileUpload.setDescription(message);
// lFileUpload.setFileUri(Uri.parse(link));
// lFileUpload.setHandler(handler);
// lFileUpload.setMessageType(messageType);
// lFileUpload.setThreadId(threadId);
// lFileUpload.setUniqueId(uniqueId);
// lFileUpload.setSystemMetaData(systemMetadata);
// lFileUpload.setHandler(handler);
// lFileUpload.setMimeType(mimeType);
// 
// if (!Util.isNullOrEmpty(messageId)) {
// String methodName = ChatConstant.METHOD_REPLY_MSG;
// lFileUpload.setMethodName(methodName);
// }
// 
// removeFromUploadQueue(uniqueId);
// 
// if (FileUtils.isImage(mimeType) && !FileUtils.isGif(mimeType)) {
// uploadImageFileMessage(lFileUpload);
// } else {
// uploadFileMessage(lFileUpload);
// }
// 
// }
// };
// 
// new PodThreadManager()
// .doThisAndGo(retryTask);
// 
// }
public void retryUpload(RetryUpload retry, ProgressHandler.sendFileMessage handler) {
    Runnable retryTask = () -> {
        String uniqueId = retry.getUniqueId();
        Activity activity = retry.getActivity();
        UploadingQueueCache uploadingQ;
        if (cache) {
            uploadingQ = dataSource.getUploadingQ(uniqueId);
        // uploadingQ = messageDatabaseHelper.getUploadingQ(uniqueId);
        } else {
            uploadingQ = uploadingQList.get(uniqueId);
        }
        if (uploadingQ != null) {
            long messageId = uploadingQ.getId();
            int messageType = uploadingQ.getMessageType();
            long threadId = uploadingQ.getThreadId();
            String userGroupHash = uploadingQ.getUserGroupHash();
            String methodName = !Util.isNullOrEmpty(messageId) ? ChatConstant.METHOD_REPLY_MSG : null;
            String message = uploadingQ.getMessage();
            String systemMetadata = uploadingQ.getSystemMetadata();
            String metadata = uploadingQ.getMetadata();
            MetaDataFile metaDataFile = gson.fromJson(metadata, MetaDataFile.class);
            String link = null;
            try {
                link = metaDataFile.getFile().getLink();
            } catch (Exception e) {
                Log.e(TAG, "Couldn't retrieve link");
            }
            if (needReadStoragePermission(activity)) {
                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;
            }
            if (!chatReady) {
                onChatNotReady(uniqueId);
                return;
            }
            if (getPodSpaceServer() == null) {
                captureError("File server is null", 0, uniqueId);
                return;
            }
            removeFromUploadQueue(uniqueId);
            try {
                String finalLink = link;
                Subscription subscription = PodUploader.uploadToPodSpace(uniqueId, Util.isNullOrEmpty(link) ? null : Uri.parse(link), userGroupHash, context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, new PodUploader.IPodUploadFileToPodSpace() {

                    @Override
                    public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length) {
                        removeFromUploadQueue(uniqueId);
                        ChatResponse<ResultFile> chatResponse = PodUploader.generateImageUploadResultForSendMessage(response, uniqueId);
                        String json = gson.toJson(chatResponse);
                        showLog("FILE_UPLOADED_TO_SERVER", json);
                        listenerManager.callOnUploadFile(json, chatResponse);
                        if (handler != null) {
                            handler.onFinishFile(json, chatResponse);
                        }
                        String jsonMeta = createFileMetadata(file, response.getHashCode(), 0, mimeType, length, response.getParentHash());
                        if (isReplyMessage(methodName)) {
                            showLog("SEND_REPLY_FILE_MESSAGE", jsonMeta);
                            mainReplyMessage(message, threadId, messageId, systemMetadata, messageType, jsonMeta, uniqueId, null);
                        } else {
                            sendTextMessageWithFile(message, threadId, jsonMeta, systemMetadata, uniqueId, typeCode, messageType);
                        }
                    }

                    @Override
                    public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length, int actualWidth, int actualHeight, int width, int height) {
                        removeFromUploadQueue(uniqueId);
                        ChatResponse<ResultImageFile> chatResponse = PodUploader.generateImageUploadResultForSendMessage(response, uniqueId, actualWidth, actualHeight, width, height, getPodSpaceImageUrl(response.getHashCode()));
                        String imageJson = gson.toJson(chatResponse);
                        listenerManager.callOnUploadImageFile(imageJson, chatResponse);
                        if (handler != null) {
                            handler.onFinishImage(imageJson, chatResponse);
                        }
                        showLog("RECEIVE_UPLOAD_IMAGE", imageJson);
                        String jsonMeta = createImageMetadata(file, response.getHashCode(), 0, height, width, mimeType, length, response.getParentHash(), false, null);
                        if (isReplyMessage(methodName)) {
                            showLog("SEND_REPLY_FILE_MESSAGE", jsonMeta);
                            mainReplyMessage(message, threadId, messageId, systemMetadata, messageType, jsonMeta, uniqueId, null);
                        } else {
                            sendTextMessageWithFile(message, threadId, jsonMeta, systemMetadata, uniqueId, typeCode, messageType);
                        }
                    }

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

                    @Override
                    public void onUploadStarted(String mimeType, File file, long length) {
                        addToUploadQueue(message, Util.isNullOrEmpty(finalLink) ? null : Uri.parse(finalLink), messageType, threadId, userGroupHash, uniqueId, systemMetadata, messageId, mimeType, null, methodName, file, length);
                        showLog("UPLOAD_FILE_TO_SERVER");
                    }

                    @Override
                    public void onProgressUpdate(int progress, int totalBytesSent, int totalBytesToSend) {
                        if (handler != null)
                            handler.onProgressUpdate(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);
                }
            }
        }
    };
    new PodThreadManager().doThisAndGo(retryTask);
}
Also used : PodThreadManager(com.fanap.podchat.util.PodThreadManager) UploadingQueueCache(com.fanap.podchat.cachemodel.queue.UploadingQueueCache) Activity(android.app.Activity) ResultFile(com.fanap.podchat.model.ResultFile) 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) ResultImageFile(com.fanap.podchat.model.ResultImageFile) UploadToPodSpaceResult(com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult) 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) MetaDataFile(com.fanap.podchat.model.MetaDataFile)

Example 2 with UploadingQueueCache

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

the class ChatCore method addToUploadQueue.

private void addToUploadQueue(String description, Uri fileUri, Integer messageType, long threadId, String userGroupHash, String uniqueId, String systemMetadata, String mimeType, File file, long file_size) {
    UploadingQueueCache uploadingQueue = new UploadingQueueCache();
    uploadingQueue.setMessage(description);
    uploadingQueue.setMessageType(messageType);
    uploadingQueue.setSystemMetadata(systemMetadata);
    uploadingQueue.setUniqueId(uniqueId);
    uploadingQueue.setThreadId(threadId);
    uploadingQueue.setUserGroupHash(userGroupHash);
    String metaData = createFileMetadata(file, null, 0, mimeType, file_size, fileUri.toString());
    uploadingQueue.setMetadata(metaData);
    if (cache) {
        dataSource.insertUploadingQueue(uploadingQueue);
    // messageDatabaseHelper.insertUploadingQueue(uploadingQueue);
    } else {
        uploadingQList.put(uniqueId, uploadingQueue);
    }
}
Also used : UploadingQueueCache(com.fanap.podchat.cachemodel.queue.UploadingQueueCache)

Example 3 with UploadingQueueCache

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

the class ChatCore method addToUploadQueue.

private void addToUploadQueue(Integer chatMessageType, String message, String uniqueId) {
    UploadingQueueCache uploadingQueue = new UploadingQueueCache();
    uploadingQueue.setMessageType(chatMessageType);
    uploadingQueue.setUniqueId(uniqueId);
    uploadingQueue.setMessage(message);
    if (cache) {
        dataSource.insertUploadingQueue(uploadingQueue);
    // messageDatabaseHelper.insertUploadingQueue(uploadingQueue);
    } else {
        uploadingQList.put(uniqueId, uploadingQueue);
    }
}
Also used : UploadingQueueCache(com.fanap.podchat.cachemodel.queue.UploadingQueueCache)

Example 4 with UploadingQueueCache

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

the class MessageManager method getUploadingFromUploadCache.

public static List<Uploading> getUploadingFromUploadCache(List<UploadingQueueCache> uploadingQueueCaches) {
    List<Uploading> uploadingQueues = new ArrayList<>();
    for (UploadingQueueCache queueCache : uploadingQueueCaches) {
        Uploading uploadingQueue = new Uploading();
        MessageVO messageVO = new MessageVO();
        messageVO.setId(queueCache.getId());
        messageVO.setMessage(queueCache.getMessage());
        messageVO.setMessageType(queueCache.getMessageType());
        messageVO.setMetadata(queueCache.getMetadata());
        messageVO.setSystemMetadata(queueCache.getSystemMetadata());
        uploadingQueue.setMessageVo(messageVO);
        uploadingQueue.setThreadId(queueCache.getThreadId());
        uploadingQueue.setUniqueId(queueCache.getUniqueId());
        uploadingQueues.add(uploadingQueue);
    }
    return uploadingQueues;
}
Also used : UploadingQueueCache(com.fanap.podchat.cachemodel.queue.UploadingQueueCache) ArrayList(java.util.ArrayList) Uploading(com.fanap.podchat.cachemodel.queue.Uploading) MessageVO(com.fanap.podchat.mainmodel.MessageVO)

Aggregations

UploadingQueueCache (com.fanap.podchat.cachemodel.queue.UploadingQueueCache)4 Activity (android.app.Activity)1 Uploading (com.fanap.podchat.cachemodel.queue.Uploading)1 ResultDownloadFile (com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile)1 UploadToPodSpaceResult (com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult)1 MessageVO (com.fanap.podchat.mainmodel.MessageVO)1 ErrorOutPut (com.fanap.podchat.model.ErrorOutPut)1 MetaDataFile (com.fanap.podchat.model.MetaDataFile)1 ResultFile (com.fanap.podchat.model.ResultFile)1 ResultImageFile (com.fanap.podchat.model.ResultImageFile)1 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)1 RequestCreateThreadWithFile (com.fanap.podchat.requestobject.RequestCreateThreadWithFile)1 RequestGetFile (com.fanap.podchat.requestobject.RequestGetFile)1 RequestGetPodSpaceFile (com.fanap.podchat.requestobject.RequestGetPodSpaceFile)1 RequestUploadFile (com.fanap.podchat.requestobject.RequestUploadFile)1 PodChatException (com.fanap.podchat.util.PodChatException)1 PodThreadManager (com.fanap.podchat.util.PodThreadManager)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 SentryException (io.sentry.core.protocol.SentryException)1 File (java.io.File)1