use of com.fanap.podchat.util.PodChatException in project pod-chat-android-sdk by FanapSoft.
the class PodUploader method uploadImageToChatServer.
public static Subscription uploadImageToChatServer(String uniqueId, @NonNull Uri fileUri, Context context, String fileServer, String token, int tokenIssuer, IPodUploadImage listener) throws Exception {
if (fileUri.getPath() == null)
throw new NullPointerException("Invalid file uri!");
String mimeType = FileUtils.getMimeType(fileUri, context);
String path = FilePick.getSmartFilePath(context, fileUri);
if (path == null)
throw new NullPointerException("Invalid path!");
File file = new File(path);
if (!file.exists() || !file.isFile())
throw new FileNotFoundException("Invalid file!");
long fileSize = 0;
try {
fileSize = file.length();
} catch (Exception x) {
Log.e(TAG, "File length exception: " + x.getMessage());
}
listener.onUploadStarted(mimeType, file, fileSize);
RetrofitHelperFileServer retrofitHelperFileServer = new RetrofitHelperFileServer(fileServer);
FileApi fileApi = retrofitHelperFileServer.getService(FileApi.class);
RequestBody namePart = RequestBody.create(MediaType.parse("multipart/form-data"), file.getName());
ProgressRequestBody requestFile = new ProgressRequestBody(file, mimeType, uniqueId, new ProgressRequestBody.UploadCallbacks() {
@Override
public void onProgress(String uniqueId, int progress, int totalBytesSent, int totalBytesToSend) {
listener.onProgressUpdate(progress, totalBytesSent, totalBytesToSend);
}
});
MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
Observable<Response<FileImageUpload>> uploadObservable = fileApi.sendImageFile(filePart, token, tokenIssuer, namePart);
long finalFileSize = fileSize;
return uploadObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).doOnError(error -> listener.onFailure(uniqueId + " - " + error.getMessage(), error)).subscribe(response -> {
if (response.isSuccessful() && response.body() != null) {
if (response.body().isHasError()) {
listener.onFailure(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), new PodChatException(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), uniqueId, token));
} else {
listener.onSuccess(response.body(), file, mimeType, finalFileSize);
}
} else {
if (response.body() != null) {
listener.onFailure(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), new PodChatException(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), uniqueId, token));
} else {
listener.onFailure(uniqueId + " - " + response.message(), new PodChatException(uniqueId + " - " + response.message(), uniqueId, token));
}
}
});
}
use of com.fanap.podchat.util.PodChatException in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method handleSent.
private void handleSent(ChatMessage chatMessage, String messageUniqueId, long threadId) {
if (cache) {
dataSource.deleteWaitQueueWithUniqueId(messageUniqueId);
} else {
waitQList.remove(messageUniqueId);
}
boolean found = false;
try {
if (threadCallbacks.containsKey(threadId)) {
ArrayList<Callback> callbacks = threadCallbacks.get(threadId);
if (callbacks != null) {
for (Callback callback : callbacks) {
if (messageUniqueId.equals(callback.getUniqueId())) {
if (callback.isSent()) {
found = true;
ChatResponse<ResultMessage> chatResponse = new ChatResponse<>();
ResultMessage resultMessage = new ResultMessage();
chatResponse.setErrorCode(0);
chatResponse.setErrorMessage("");
chatResponse.setHasError(false);
chatResponse.setUniqueId(callback.getUniqueId());
chatResponse.setSubjectId(chatMessage.getSubjectId());
resultMessage.setConversationId(chatMessage.getSubjectId());
try {
resultMessage.setMessageId(Long.parseLong(chatMessage.getContent()));
} catch (NumberFormatException e) {
captureError(new PodChatException(e.getMessage(), messageUniqueId, getToken()));
resultMessage.setMessageId(0);
}
chatResponse.setResult(resultMessage);
String json = gson.toJson(chatResponse);
listenerManager.callOnSentMessage(json, chatResponse);
runOnUIThread(() -> {
if (handlerSend.get(callback.getUniqueId()) != null) {
ChatHandler handler = handlerSend.get(callback.getUniqueId());
if (handler != null) {
handler.onSentResult(chatMessage.getContent());
}
}
});
Callback callbackUpdateSent = new Callback();
callbackUpdateSent.setSent(false);
callbackUpdateSent.setDelivery(callback.isDelivery());
callbackUpdateSent.setSeen(callback.isSeen());
callbackUpdateSent.setUniqueId(callback.getUniqueId());
callbacks.set(callbacks.indexOf(callback), callbackUpdateSent);
threadCallbacks.put(threadId, callbacks);
if (sentryResponseLog) {
showLog("RECEIVED_SENT_MESSAGE", json);
} else {
showLog("RECEIVED_SENT_MESSAGE");
}
}
break;
}
}
}
}
if (!found) {
ChatResponse<ResultMessage> chatResponse = new ChatResponse<>();
ResultMessage resultMessage = new ResultMessage();
chatResponse.setErrorCode(0);
chatResponse.setErrorMessage("");
chatResponse.setHasError(false);
chatResponse.setUniqueId(messageUniqueId);
chatResponse.setSubjectId(chatMessage.getSubjectId());
resultMessage.setConversationId(chatMessage.getSubjectId());
resultMessage.setMessageId(Long.parseLong(chatMessage.getContent()));
chatResponse.setResult(resultMessage);
String json = gson.toJson(chatResponse);
listenerManager.callOnSentMessage(json, chatResponse);
// Log.d("MTAG", "threadid isn't in callbacks!");
runOnUIThread(() -> {
if (handlerSend.get(messageUniqueId) != null) {
handlerSend.get(messageUniqueId).onSentResult(chatMessage.getContent());
}
});
Callback callbackUpdateSent = new Callback();
callbackUpdateSent.setSent(false);
callbackUpdateSent.setUniqueId(messageUniqueId);
if (sentryResponseLog) {
showLog("RECEIVED_SENT_MESSAGE", json);
} else {
showLog("RECEIVED_SENT_MESSAGE");
}
}
} catch (Throwable e) {
showErrorLog(e.getMessage());
onUnknownException(chatMessage.getUniqueId(), e);
}
}
use of com.fanap.podchat.util.PodChatException in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method getDownloaderErrorInterface.
private PodDownloader.IDownloaderError getDownloaderErrorInterface(ProgressHandler.IDownloadFile progressHandler, String uniqueId, String url) {
return new PodDownloader.IDownloaderError() {
@Override
public void errorOnWritingToFile() {
String error = captureError(ChatConstant.ERROR_WRITING_FILE, ChatConstant.ERROR_CODE_WRITING_FILE, uniqueId);
progressHandler.onError(uniqueId, error, url);
}
@Override
public void errorOnDownloadingFile(int errorCode) {
String error = captureError(ChatConstant.ERROR_DOWNLOAD_FILE, ChatConstant.ERROR_CODE_DOWNLOAD_FILE, uniqueId);
progressHandler.onError(uniqueId, error, url);
}
@Override
public void errorUnknownException(String cause) {
String error = captureError(ChatConstant.ERROR_DOWNLOAD_FILE, ChatConstant.ERROR_CODE_DOWNLOAD_FILE, uniqueId, new PodChatException(cause, uniqueId, token));
progressHandler.onError(uniqueId, error, url);
}
};
}
use of com.fanap.podchat.util.PodChatException in project pod-chat-android-sdk by FanapSoft.
the class PodUploader method uploadFileToChatServer.
public static Subscription uploadFileToChatServer(String uniqueId, @NonNull Uri fileUri, Context context, String fileServer, String token, int tokenIssuer, IPodUploadFile listener) throws Exception {
if (fileUri.getPath() == null)
throw new NullPointerException("Invalid file uri!");
String mimeType = FileUtils.getMimeType(fileUri, context);
String path = FilePick.getSmartFilePath(context, fileUri);
if (path == null)
throw new NullPointerException("Invalid path!");
File file = new File(path);
if (!file.exists() || !file.isFile())
throw new FileNotFoundException("Invalid file!");
long fileSize = 0;
try {
fileSize = file.length();
} catch (Exception x) {
Log.e(TAG, "File length exception: " + x.getMessage());
}
listener.onUploadStarted(mimeType, file, fileSize);
RetrofitHelperFileServer retrofitHelperFileServer = new RetrofitHelperFileServer(fileServer);
FileApi fileApi = retrofitHelperFileServer.getService(FileApi.class);
RequestBody namePart = RequestBody.create(MediaType.parse("multipart/form-data"), file.getName());
ProgressRequestBody requestFile = new ProgressRequestBody(file, mimeType, uniqueId, new ProgressRequestBody.UploadCallbacks() {
@Override
public void onProgress(String uniqueId, int progress, int totalBytesSent, int totalBytesToSend) {
listener.onProgressUpdate(progress, totalBytesSent, totalBytesToSend);
}
});
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
Observable<Response<FileUpload>> uploadObservable = fileApi.sendFile(filePart, token, tokenIssuer, namePart);
long finalFileSize = fileSize;
return uploadObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).doOnError(error -> listener.onFailure(uniqueId + " - " + error.getMessage(), error)).subscribe(response -> {
if (response.isSuccessful() && response.body() != null) {
if (response.body().isHasError()) {
listener.onFailure(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), new PodChatException(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), uniqueId, token));
} else {
listener.onSuccess(response.body(), file, mimeType, finalFileSize);
}
} else {
if (response.body() != null) {
listener.onFailure(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), new PodChatException(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), uniqueId, token));
} else {
listener.onFailure(uniqueId + " - " + response.message(), new PodChatException(uniqueId + " - " + response.message(), uniqueId, token));
}
}
});
}
use of com.fanap.podchat.util.PodChatException in project pod-chat-android-sdk by FanapSoft.
the class PodUploader method uploadFileToPodSpace.
private static Subscription uploadFileToPodSpace(String uniqueId, String threadHashCode, String fileServer, String token, int tokenIssuer, IPodUploadFileToPodSpace listener, String mimeType, File file, long fileSize) {
listener.onUploadStarted(mimeType, file, fileSize);
RetrofitHelperFileServer retrofitHelperFileServer = new RetrofitHelperFileServer(fileServer);
FileApi fileApi = retrofitHelperFileServer.getService(FileApi.class);
RequestBody namePart = RequestBody.create(MediaType.parse("multipart/form-data"), file.getName());
RequestBody hashGroupPart = RequestBody.create(MediaType.parse("multipart/form-data"), threadHashCode);
ProgressRequestBody requestFile = new ProgressRequestBody(file, mimeType, uniqueId, new ProgressRequestBody.UploadCallbacks() {
@Override
public void onProgress(String uniqueId, int progress, int totalBytesSent, int totalBytesToSend) {
if (progress < 95)
listener.onProgressUpdate(progress, totalBytesSent, totalBytesToSend);
}
});
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
Observable<Response<UploadToPodSpaceResponse>> uploadObservable = fileApi.uploadToPodSpace(filePart, token, tokenIssuer, namePart, hashGroupPart);
long finalFileSize = fileSize;
return uploadObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).doOnError(error -> listener.onFailure(error.getMessage(), error)).subscribe(response -> {
if (response.isSuccessful() && response.body() != null) {
if (response.body().isHasError()) {
listener.onFailure(response.body().getMessage(), new PodChatException(response.body().getMessage(), uniqueId, token));
return;
}
listener.onProgressUpdate(100, (int) finalFileSize, 0);
listener.onSuccess(response.body().getUploadToPodSpaceResult(), file, mimeType, finalFileSize);
} else {
try {
if (response.errorBody() != null) {
listener.onFailure(response.errorBody().string(), new PodChatException(response.errorBody().string(), uniqueId, token));
} else {
listener.onFailure(response.message(), new PodChatException(response.message(), uniqueId, token));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}, error -> listener.onFailure(error.getMessage(), error));
}
Aggregations