Search in sources :

Example 66 with ChatResponse

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

the class ChatCore method uploadFileToThread.

// private String uploadFileProgress(Activity activity, Uri uri, ProgressHandler.onProgressFile handler) {
// String uniqueId = generateUniqueId();
// try {
// if (chatReady) {
// 
// if (Permission.Check_READ_STORAGE(activity)) {
// 
// if (getFileServer() != null) {
// 
// String mimeType = getMimType(uri);
// //                    File file = new File(getRealPathFromURI(context, uri));
// String path = FilePick.getSmartFilePath(getContext(), uri);
// File file = new File(path);
// 
// 
// JsonObject jLog = new JsonObject();
// 
// jLog.addProperty("name", file.getName());
// jLog.addProperty("token", getToken());
// jLog.addProperty("tokenIssuer", TOKEN_ISSUER);
// jLog.addProperty("uniqueId", uniqueId);
// 
// showLog("UPLOADING_FILE", getJsonForLog(jLog));
// 
// RetrofitHelperFileServer retrofitHelperFileServer = new RetrofitHelperFileServer(getFileServer());
// FileApi fileApi = retrofitHelperFileServer.getService(FileApi.class);
// RequestBody name = RequestBody.create(MediaType.parse("text/plain"), file.getName());
// ProgressRequestBody requestFile = new ProgressRequestBody(file, mimeType, uniqueId, new ProgressRequestBody.UploadCallbacks() {
// 
// @Override
// public void onProgress(String uniqueId, int progress, int totalBytesSent, int totalBytesToSend) {
// handler.onProgress(uniqueId, progress, totalBytesSent, totalBytesToSend);
// handler.onProgressUpdate(progress);
// }
// 
// });
// 
// MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
// Observable<Response<FileUpload>> uploadObservable = fileApi.sendFile(body, getToken(), TOKEN_ISSUER, name);
// uploadObservable.subscribeOn(Schedulers.io())
// .observeOn(AndroidSchedulers.mainThread())
// .subscribe(fileUploadResponse -> {
// if (fileUploadResponse.isSuccessful()) {
// boolean hasError = fileUploadResponse.body().isHasError();
// if (hasError) {
// String errorMessage = fileUploadResponse.body().getMessage();
// int errorCode = fileUploadResponse.body().getErrorCode();
// String jsonError = getErrorOutPut(errorMessage, errorCode, uniqueId);
// ErrorOutPut error = new ErrorOutPut(true, errorMessage, errorCode, uniqueId);
// handler.onError(jsonError, error);
// } else {
// 
// FileUpload result = fileUploadResponse.body();
// ResultFile resultFile = result.getResult();
// resultFile.setUrl(getFile(resultFile.getId(), resultFile.getHashCode(), true));
// 
// 
// ChatResponse<ResultFile> chatResponse = new ChatResponse<>();
// chatResponse.setResult(resultFile);
// chatResponse.setUniqueId(uniqueId);
// String json = gson.toJson(chatResponse);
// showLog("FINISH_UPLOAD_FILE", json);
// listenerManager.callOnUploadFile(json, chatResponse);
// 
// handler.onFinish(json, result);
// }
// }
// }, throwable -> {
// ErrorOutPut error = new ErrorOutPut(true, throwable.getMessage(), 0, uniqueId);
// String json = gson.toJson(error);
// getErrorOutPut(ChatConstant.ERROR_UPLOAD_FILE + " " + throwable.getMessage(), ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
// handler.onError(json, error);
// 
// });
// } else {
// 
// if (log) Log.e(TAG, "FileServer url Is null");
// 
// getErrorOutPut("File Server url Is null", ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
// 
// }
// 
// } else {
// String jsonError = getErrorOutPut(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);
// handler.onError(jsonError, error);
// Permission.Request_WRITE_TORAGE(activity, WRITE_EXTERNAL_STORAGE_CODE);
// }
// } else {
// getErrorOutPut(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
// }
// 
// } catch (Throwable throwable) {
// if (log) Log.e(TAG, throwable.getMessage());
// getErrorOutPut(throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId);
// 
// }
// return uniqueId;
// }
// new upload file function
private void uploadFileToThread(RequestUploadFile request, String userGroupHash, String uniqueId, @Nullable ProgressHandler.onProgressFile 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);
        }
        return;
    }
    if (!chatReady) {
        onChatNotReady(uniqueId);
        return;
    }
    if (getPodSpaceServer() == null) {
        captureError("File server is null", 0, uniqueId);
        return;
    }
    try {
        Subscription subscription = PodUploader.uploadToPodSpace(uniqueId, request.getFileUri(), request.getUserGroupHashCode(), context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, 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(getFile(resultFile.getId(), resultFile.getHashCode(), true));
                showLog("FINISH_UPLOAD_FILE", gson.toJson(resultFile));
                MetaDataFile metaDataFile = new MetaDataFile();
                FileMetaDataContent metaDataContent = new FileMetaDataContent();
                metaDataContent.setHashCode(resultFile.getHashCode());
                metaDataContent.setId(resultFile.getId());
                metaDataContent.setName(resultFile.getName());
                metaDataFile.setFile(metaDataContent);
                chatResponse.setResult(resultFile);
                chatResponse.setUniqueId(uniqueId);
                JsonObject metadata = (JsonObject) gson.toJsonTree(metaDataFile);
                metadata.addProperty("name", result.getResult().getName());
                metadata.addProperty("id", result.getResult().getId());
                if (handler != null) {
                    handler.onFinish(gson.toJson(chatResponse), result);
                }
                listener.onWorkDone(metadata.toString());
            }

            @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);
        }
    }
}
Also used : FileMetaDataContent(com.fanap.podchat.model.FileMetaDataContent) JsonObject(com.google.gson.JsonObject) 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) MetaDataFile(com.fanap.podchat.model.MetaDataFile)

Example 67 with ChatResponse

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

the class ChatCore method handleThreadInfoUpdated.

private void handleThreadInfoUpdated(Thread thread, String uniqueId) {
    ResultThread resultThread = new ResultThread();
    resultThread.setThread(thread);
    ChatResponse<ResultThread> chatResponse = new ChatResponse<>();
    chatResponse.setResult(resultThread);
    chatResponse.setUniqueId(uniqueId);
    listenerManager.callOnThreadInfoUpdated(chatResponse.getJson(), chatResponse);
    if (sentryResponseLog) {
        showLog("THREAD_INFO_UPDATED", chatResponse.getJson());
    } else {
        showLog("THREAD_INFO_UPDATED");
    }
    if (cache) {
        // messageDatabaseHelper.saveNewThread(resultThread.getThread());
        dataSource.saveThreadResultFromServer(thread);
    }
}
Also used : ChatResponse(com.fanap.podchat.model.ChatResponse) ResultThread(com.fanap.podchat.model.ResultThread)

Example 68 with ChatResponse

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

the class ChatCore method handleThreadInfoUpdated.

private void handleThreadInfoUpdated(ChatMessage chatMessage) {
    ResultThread resultThread = new ResultThread();
    Thread thread = gson.fromJson(chatMessage.getContent(), Thread.class);
    resultThread.setThread(thread);
    ChatResponse<ResultThread> chatResponse = new ChatResponse<>();
    chatResponse.setResult(resultThread);
    chatResponse.setUniqueId(chatMessage.getUniqueId());
    if (sentryResponseLog) {
        showLog("THREAD_INFO_UPDATED", chatMessage.getContent());
    } else {
        showLog("THREAD_INFO_UPDATED");
    }
    if (cache) {
        dataSource.saveThreadResultFromServer(resultThread.getThread());
    }
    listenerManager.callOnThreadInfoUpdated(chatMessage.getContent(), 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 69 with ChatResponse

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

the class ChatCore method handleSetRole.

private void handleSetRole(ChatMessage chatMessage) {
    ChatResponse<ResultSetAdmin> chatResponse = new ChatResponse<>();
    ResultSetAdmin resultSetAdmin = new ResultSetAdmin();
    ArrayList<Admin> admins = gson.fromJson(chatMessage.getContent(), new TypeToken<ArrayList<Admin>>() {
    }.getType());
    resultSetAdmin.setAdmins(admins);
    chatResponse.setResult(resultSetAdmin);
    chatResponse.setUniqueId(chatMessage.getUniqueId());
    chatResponse.setSubjectId(chatMessage.getSubjectId());
    String responseJson = gson.toJson(chatResponse);
    OutputSetRoleToUser output = new OutputSetRoleToUser();
    output.setResultSetAdmin(resultSetAdmin);
    long threadId = chatMessage.getSubjectId();
    if (ThreadManager.hasSetAdminSubscriber(chatResponse)) {
        if (sentryResponseLog) {
            showLog("RECEIVE_SET_ROLE_FOR_SAFE_LEAVE", responseJson);
        } else {
            showLog("RECEIVE_SET_ROLE_FOR_SAFE_LEAVE");
        }
        return;
    }
    if (cache) {
        dataSource.updateParticipantRoles(admins, threadId);
    }
    showLog("RECEIVE_SET_ROLE", responseJson);
    listenerManager.callOnSetRoleToUser(responseJson, chatResponse);
}
Also used : ResultSetAdmin(com.fanap.podchat.model.ResultSetAdmin) TypeToken(com.google.gson.reflect.TypeToken) ChatResponse(com.fanap.podchat.model.ChatResponse) OutputSetRoleToUser(com.fanap.podchat.model.OutputSetRoleToUser) Admin(com.fanap.podchat.model.Admin) RequestSetAdmin(com.fanap.podchat.requestobject.RequestSetAdmin) RequestGetAdmin(com.fanap.podchat.requestobject.RequestGetAdmin) ResultSetAdmin(com.fanap.podchat.model.ResultSetAdmin)

Example 70 with ChatResponse

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

the class ChatCore method mainMapStaticImage.

/**
 * [Required] center
 * if these params don't set they have default values :
 * [default value] messageType = "standard-night"
 * [default value] zoom = 15
 * [default value] width = 800
 * [default value] height = 500
 */
private String mainMapStaticImage(RequestLocationMessage request, Activity activity, String uniqueId, boolean isMessage) {
    try {
        if (Util.isNullOrEmpty(uniqueId)) {
            uniqueId = generateUniqueId();
        }
        if (needReadStoragePermission(activity))
            return uniqueId;
        if (chatReady) {
            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();
            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;
            }
            JsonObject jsonObject = (JsonObject) gson.toJsonTree(request);
            jsonObject.addProperty("uniqueId", uniqueId);
            showLog("SEND_GET_MAP_STATIC", getJsonForLog(jsonObject));
            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;
            String finalUniqueId1 = uniqueId;
            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, finalUniqueId1);
                                }
                                if (file == null) {
                                    captureError(ChatConstant.ERROR_WRITING_FILE, ChatConstant.ERROR_CODE_WRITING_FILE, finalUniqueId1);
                                    return;
                                }
                                Uri fileUri = Uri.fromFile(file);
                                // String newPath = FilePick.getSmartFilePath(getContext(), fileUri);
                                String mimType = handleMimType(fileUri, file);
                                LFileUpload lFileUpload = new LFileUpload();
                                lFileUpload.setFileUri(fileUri);
                                if (activity != null) {
                                    lFileUpload.setActivity(activity);
                                }
                                lFileUpload.setThreadId(threadId);
                                lFileUpload.setUniqueId(finalUniqueId);
                                lFileUpload.setMessageType(messageType);
                                lFileUpload.setMimeType(mimType);
                                lFileUpload.setMethodName(ChatConstant.METHOD_LOCATION_MSG);
                                lFileUpload.setSystemMetaData(systemMetadata);
                                lFileUpload.setCenter(center);
                                uploadImageFileMessage(lFileUpload);
                            }
                        }
                    } 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) {
        captureError(throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId, throwable);
    }
    return uniqueId;
}
Also used : ResultStaticMapImage(com.fanap.podchat.model.ResultStaticMapImage) LFileUpload(com.fanap.podchat.localmodel.LFileUpload) JsonObject(com.google.gson.JsonObject) Uri(android.net.Uri) 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) RetrofitHelperMap(com.fanap.podchat.networking.retrofithelper.RetrofitHelperMap) MapApi(com.fanap.podchat.networking.api.MapApi) Bitmap(android.graphics.Bitmap) ChatResponse(com.fanap.podchat.model.ChatResponse) 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

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