Search in sources :

Example 11 with ErrorOutPut

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

the class ChatCore method sendFileMessage.

private void sendFileMessage(RequestFileMessage requestFileMessage, String uniqueId, ProgressHandler.sendFileMessage handler) {
    if (needReadStoragePermission(requestFileMessage.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;
    }
    if (!chatReady) {
        onChatNotReady(uniqueId);
        return;
    }
    if (getPodSpaceServer() == null) {
        captureError("File server is null", 0, uniqueId);
        return;
    }
    try {
        Subscription subscription = PodUploader.uploadToPodSpace(uniqueId, requestFileMessage.getFileUri(), requestFileMessage.getUserGroupHash(), context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, requestFileMessage.getImageXc(), requestFileMessage.getImageYc(), requestFileMessage.getImageHc(), requestFileMessage.getImageWc(), 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());
                sendTextMessageWithFile(requestFileMessage.getDescription(), requestFileMessage.getThreadId(), jsonMeta, requestFileMessage.getSystemMetadata(), uniqueId, typeCode, requestFileMessage.getMessageType());
            }

            @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);
                sendTextMessageWithFile(requestFileMessage.getDescription(), requestFileMessage.getThreadId(), jsonMeta, requestFileMessage.getSystemMetadata(), uniqueId, typeCode, requestFileMessage.getMessageType());
            }

            @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(requestFileMessage.getDescription(), requestFileMessage.getFileUri(), requestFileMessage.getMessageType(), requestFileMessage.getThreadId(), requestFileMessage.getUserGroupHash(), uniqueId, requestFileMessage.getSystemMetadata(), mimeType, file, length);
                showLog("UPLOAD_FILE_TO_SERVER");
                showLog(requestFileMessage.toString());
            }

            @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);
        }
    }
}
Also used : 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)

Example 12 with ErrorOutPut

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

the class ChatCore method uploadImageProgress.

public String uploadImageProgress(RequestUploadImage request, @Nullable ProgressHandler.onProgress 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, String.valueOf(request.getxC()), String.valueOf(request.getyC()), String.valueOf(request.gethC()), String.valueOf(request.getwC()), request.isPublic(), new PodUploader.IPodUploadFileToPodSpace() {

            @Override
            public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length, int actualWidth, int actualHeight, int width, int height) {
                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.onFinish(imageJson, chatResponse);
                }
                showLog("RECEIVE_UPLOAD_IMAGE", imageJson);
            }

            @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) {
                showLog("UPLOADING_FILE");
            }

            @Override
            public void onProgressUpdate(int progress, int totalBytesSent, int totalBytesToSend) {
                if (handler != null) {
                    handler.onProgressUpdate(progress);
                    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);
        }
    }
    return uniqueId;
}
Also used : 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)

Example 13 with ErrorOutPut

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

the class ChatCore method uploadImageToThread.

// new upload image function
private void uploadImageToThread(RequestUploadImage request, String userGroupHash, String uniqueId, @Nullable ProgressHandler.onProgress handler, OnWorkDone listener) {
    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);
        }
        listener.onWorkDone(null);
        return;
    }
    if (!chatReady) {
        onChatNotReady(uniqueId);
        listener.onWorkDone(null);
        return;
    }
    if (getPodSpaceServer() == null) {
        listener.onWorkDone(null);
        captureError("File server is null", 0, uniqueId);
        return;
    }
    try {
        Subscription subscription = PodUploader.uploadToPodSpace(uniqueId, request.getFileUri(), userGroupHash, context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, String.valueOf(request.getxC()), String.valueOf(request.getyC()), String.valueOf(request.gethC()), String.valueOf(request.getwC()), new PodUploader.IPodUploadFileToPodSpace() {

            @Override
            public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length, int actualWidth, int actualHeight, int width, int height) {
                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.onFinish(imageJson, chatResponse);
                }
                showLog("RECEIVE_UPLOAD_IMAGE", imageJson);
                listener.onWorkDone(response.getHashCode());
            }

            @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);
                }
                listener.onWorkDone(null);
            }

            @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.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);
        listener.onWorkDone(null);
        if (handler != null) {
            handler.onError(jsonError, error);
        }
    }
}
Also used : 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)

Example 14 with ErrorOutPut

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

the class ChatCore method replyFileMessage.

/**
 * Reply the message in the current thread and send az message and receive at the
 * <p>
 * messageContent content of the reply message
 * threadId       id of the thread
 * messageId      of the message that we want to reply
 * metaData       meta data of the message
 */
public String replyFileMessage(RequestReplyFileMessage request, ProgressHandler.sendFileMessage 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("PodSpace server is null", 0, uniqueId);
        return uniqueId;
    }
    long threadId = request.getThreadId();
    String messageContent = request.getMessageContent();
    String systemMetaData = request.getSystemMetaData();
    Uri fileUri = request.getFileUri();
    long messageId = request.getMessageId();
    int messageType = request.getMessageType();
    String methodName = ChatConstant.METHOD_REPLY_MSG;
    try {
        Subscription subscription = PodUploader.uploadToPodSpace(uniqueId, request.getFileUri(), request.getUserGroupHashCode(), context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, request.getImageXc(), request.getImageYc(), request.getImageHc(), request.getImageWc(), 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());
                showLog("SEND_REPLY_FILE_MESSAGE", jsonMeta);
                mainReplyMessage(messageContent, threadId, messageId, systemMetaData, messageType, jsonMeta, uniqueId, null);
            }

            @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);
                showLog("SEND_REPLY_FILE_MESSAGE", jsonMeta);
                mainReplyMessage(messageContent, threadId, messageId, systemMetaData, messageType, jsonMeta, uniqueId, null);
            }

            @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(messageContent, fileUri, messageType, threadId, request.getUserGroupHashCode(), 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);
        }
    }
    return uniqueId;
}
Also used : Uri(android.net.Uri) 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)

Example 15 with ErrorOutPut

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

the class ChatCore method mainMapStaticImage.

private String mainMapStaticImage(RequestLocationMessage request, Activity activity, String uniqueId, boolean isMessage, ProgressHandler.sendFileMessage handler) {
    try {
        if (Util.isNullOrEmpty(uniqueId)) {
            uniqueId = generateUniqueId();
        }
        if (needReadStoragePermission(activity))
            return uniqueId;
        if (chatReady) {
            JsonObject jsonLog = new JsonObject();
            String type = request.getType();
            int zoom = request.getZoom();
            int width = request.getWidth();
            int height = request.getHeight();
            String center = request.getCenter();
            long threadId = request.getThreadId();
            int messageType = request.getMessageType() > 0 ? request.getMessageType() : TextMessageType.Constants.POD_SPACE_PICTURE;
            String systemMetadata = request.getSystemMetadata();
            if (Util.isNullOrEmpty(type)) {
                type = "standard-night";
            }
            if (Util.isNullOrEmpty(zoom)) {
                zoom = 15;
            }
            if (Util.isNullOrEmpty(width)) {
                width = 800;
            }
            if (Util.isNullOrEmpty(height)) {
                height = 500;
            }
            RetrofitHelperMap retrofitHelperMap = new RetrofitHelperMap("https://api.neshan.org/");
            MapApi mapApi = retrofitHelperMap.getService(MapApi.class);
            Call<ResponseBody> call = mapApi.mapStaticCall(API_KEY_MAP, type, zoom, center, width, height);
            String finalUniqueId = uniqueId;
            jsonLog.addProperty("type", type);
            jsonLog.addProperty("zoom", zoom);
            jsonLog.addProperty("width", width);
            jsonLog.addProperty("height", height);
            jsonLog.addProperty("center", center);
            jsonLog.addProperty("threadId", threadId);
            jsonLog.addProperty("messageType", messageType);
            jsonLog.addProperty("systemMetadata", systemMetadata);
            jsonLog.addProperty("uniqueId", uniqueId);
            showLog("SEND_LOCATION_MESSAGE", getJsonForLog(jsonLog));
            call.enqueue(new retrofit2.Callback<ResponseBody>() {

                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    if (response.isSuccessful()) {
                        if (response.body() != null) {
                            Bitmap bitmap = BitmapFactory.decodeStream(response.body().byteStream());
                            ChatResponse<ResultStaticMapImage> chatResponse = new ChatResponse<>();
                            ResultStaticMapImage result = new ResultStaticMapImage();
                            result.setBitmap(bitmap);
                            chatResponse.setUniqueId(finalUniqueId);
                            chatResponse.setResult(result);
                            listenerManager.callOnStaticMap(chatResponse);
                            showLog("RECEIVE_MAP_STATIC");
                            if (!call.isCanceled()) {
                                call.cancel();
                            }
                            if (isMessage) {
                                File file = null;
                                try {
                                    file = FileUtils.saveBitmap(bitmap, "map");
                                } catch (Exception e) {
                                    captureError(ChatConstant.ERROR_WRITING_FILE, ChatConstant.ERROR_CODE_WRITING_FILE, finalUniqueId, e);
                                }
                                if (file == null) {
                                    captureError(ChatConstant.ERROR_WRITING_FILE, ChatConstant.ERROR_CODE_WRITING_FILE, finalUniqueId);
                                    return;
                                }
                                Uri fileUri = Uri.fromFile(file);
                                if (!chatReady) {
                                    onChatNotReady(finalUniqueId);
                                    return;
                                }
                                if (getPodSpaceServer() == null) {
                                    captureError("PodSpace server is null", 0, finalUniqueId);
                                    return;
                                }
                                removeFromUploadQueue(finalUniqueId);
                                try {
                                    Subscription subscription = PodUploader.uploadToPodSpace(finalUniqueId, fileUri, request.getUserGroupHash(), context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, new PodUploader.IPodUploadFileToPodSpace() {

                                        @Override
                                        public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length, int actualWidth, int actualHeight, int width, int height) {
                                            removeFromUploadQueue(finalUniqueId);
                                            ChatResponse<ResultImageFile> chatResponse = PodUploader.generateImageUploadResultForSendMessage(response, finalUniqueId, 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);
                                            sendTextMessageWithFile(request.getMessage(), threadId, jsonMeta, systemMetadata, finalUniqueId, typeCode, messageType);
                                        }

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

                                        @Override
                                        public void onUploadStarted(String mimeType, File file, long length) {
                                            addToUploadQueue(request.getMessage(), fileUri, messageType, threadId, request.getUserGroupHash(), finalUniqueId, systemMetadata, request.getMessageId(), mimeType, request.getCenter(), ChatConstant.METHOD_LOCATION_MSG, file, length);
                                            showLog("UPLOAD_FILE_TO_SERVER");
                                        }

                                        @Override
                                        public void onProgressUpdate(int progress, int totalBytesSent, int totalBytesToSend) {
                                            if (handler != null)
                                                handler.onProgressUpdate(finalUniqueId, progress, totalBytesSent, totalBytesToSend);
                                        }
                                    });
                                    initCancelUpload(finalUniqueId, subscription);
                                } catch (Exception e) {
                                    String jsonError = captureError(ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, finalUniqueId, e);
                                    ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, finalUniqueId);
                                    if (handler != null) {
                                        handler.onError(jsonError, error);
                                    }
                                }
                            }
                        }
                    } else {
                        captureError(ChatConstant.ERROR_CALL_NESHAN_API, ChatConstant.ERROR_CODE_CALL_NESHAN_API, finalUniqueId);
                        showErrorLog(response.message());
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    captureError(t.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, finalUniqueId, t);
                }
            });
        } else {
            captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
        }
    } catch (Throwable throwable) {
        showErrorLog(throwable.getMessage());
        onUnknownException(uniqueId, throwable);
    }
    return uniqueId;
}
Also used : ResultStaticMapImage(com.fanap.podchat.model.ResultStaticMapImage) JsonObject(com.google.gson.JsonObject) Uri(android.net.Uri) RetrofitHelperMap(com.fanap.podchat.networking.retrofithelper.RetrofitHelperMap) MapApi(com.fanap.podchat.networking.api.MapApi) ResultImageFile(com.fanap.podchat.model.ResultImageFile) Bitmap(android.graphics.Bitmap) ChatResponse(com.fanap.podchat.model.ChatResponse) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut) Subscription(rx.Subscription) 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) ProgressResponseBody(com.fanap.podchat.networking.ProgressResponseBody) ResponseBody(okhttp3.ResponseBody) UploadToPodSpaceResult(com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult) 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)

Aggregations

ErrorOutPut (com.fanap.podchat.model.ErrorOutPut)26 ResultImageFile (com.fanap.podchat.model.ResultImageFile)16 ChatResponse (com.fanap.podchat.model.ChatResponse)12 ResultFile (com.fanap.podchat.model.ResultFile)12 RequestCreateThreadWithFile (com.fanap.podchat.requestobject.RequestCreateThreadWithFile)12 RequestUploadFile (com.fanap.podchat.requestobject.RequestUploadFile)12 PodChatException (com.fanap.podchat.util.PodChatException)12 ResultDownloadFile (com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile)11 MetaDataFile (com.fanap.podchat.model.MetaDataFile)11 RequestGetFile (com.fanap.podchat.requestobject.RequestGetFile)11 RequestGetPodSpaceFile (com.fanap.podchat.requestobject.RequestGetPodSpaceFile)11 File (java.io.File)11 Subscription (rx.Subscription)11 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)10 JsonSyntaxException (com.google.gson.JsonSyntaxException)10 SentryException (io.sentry.core.protocol.SentryException)10 IOException (java.io.IOException)10 JSONException (org.json.JSONException)10 UploadToPodSpaceResult (com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult)9 ProgressHandler (com.fanap.podchat.ProgressHandler)7