use of com.fanap.podchat.ProgressHandler in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method getFile.
/**
* @param request fileId and hashCode of desired file
* @param progressHandler callbacks for downloading progress.
* @return uniqueId of request.
*/
public String getFile(RequestGetFile request, ProgressHandler.IDownloadFile progressHandler) {
String uniqueId = generateUniqueId();
String url = getFile(request.getFileId(), request.getHashCode(), true);
PodDownloader.IDownloaderError downloaderErrorInterface = getDownloaderErrorInterface(progressHandler, uniqueId, url);
File destinationFolder;
if (cache && request.canUseCache()) {
destinationFolder = FileUtils.getDownloadDirectory() != null ? FileUtils.getOrCreateDownloadDirectory(FileUtils.FILES) : FileUtils.getOrCreateDirectory(FileUtils.FILES);
} else {
destinationFolder = FileUtils.getDownloadDirectory() != null ? FileUtils.getOrCreateDownloadDirectory(FileUtils.FILES) : FileUtils.getPublicFilesDirectory();
}
String fileName = "file_" + request.getFileId() + "_" + request.getHashCode();
if (destinationFolder == null) {
progressHandler.onError(uniqueId, ChatConstant.ERROR_WRITING_FILE, url);
return uniqueId;
}
File cachedFile = FileUtils.findFileInFolder(destinationFolder, fileName);
if (cachedFile != null && cachedFile.isFile() && request.canUseCache()) {
// file exists
ChatResponse<ResultDownloadFile> response = PodDownloader.generateDownloadResult(request.getHashCode(), request.getFileId(), cachedFile);
progressHandler.onFileReady(response);
return uniqueId;
}
// only url should return in callback
if (!hasFreeSpace) {
progressHandler.onLowFreeSpace(uniqueId, url);
return uniqueId;
}
if (chatReady) {
Call call = PodDownloader.download(new ProgressHandler.IDownloadFile() {
@Override
public void onError(String mUniqueId, String error, String mUrl) {
progressHandler.onError(uniqueId, error, url);
}
@Override
public void onProgressUpdate(String mUniqueId, long bytesDownloaded, long totalBytesToDownload) {
progressHandler.onProgressUpdate(uniqueId, bytesDownloaded, totalBytesToDownload);
if (totalBytesToDownload > checkFreeSpace()) {
progressHandler.onLowFreeSpace(uniqueId, url);
}
}
@Override
public void onProgressUpdate(String mUniqueId, int progress) {
progressHandler.onProgressUpdate(uniqueId, progress);
}
@Override
public void onFileReady(ChatResponse<ResultDownloadFile> response) {
progressHandler.onFileReady(response);
}
}, getFileServer(), url, fileName, destinationFolder, downloaderErrorInterface, request.getHashCode(), request.getFileId());
downloadCallList.put(uniqueId, call);
} else
onChatNotReady(uniqueId);
return uniqueId;
}
use of com.fanap.podchat.ProgressHandler in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method getImage.
/**
* @param request imageId and hashCode of desired image
* @param progressHandler callbacks for downloading progress.
* @return uniqueId of request.
*/
public String getImage(RequestGetImage request, ProgressHandler.IDownloadFile progressHandler) {
String uniqueId = generateUniqueId();
String url = getImage(request.getImageId(), request.getHashCode(), true);
PodDownloader.IDownloaderError downloaderErrorInterface = getDownloaderErrorInterface(progressHandler, uniqueId, url);
File destinationFolder;
if (cache && request.canUseCache()) {
destinationFolder = FileUtils.getDownloadDirectory() != null ? FileUtils.getOrCreateDownloadDirectory(FileUtils.PICTURES) : FileUtils.getOrCreateDirectory(FileUtils.PICTURES);
} else {
destinationFolder = FileUtils.getDownloadDirectory() != null ? FileUtils.getOrCreateDownloadDirectory(FileUtils.PICTURES) : FileUtils.getPublicFilesDirectory();
}
String fileName = "image_" + request.getImageId() + "_" + request.getHashCode();
if (destinationFolder == null) {
progressHandler.onError(uniqueId, ChatConstant.ERROR_WRITING_FILE, url);
return uniqueId;
}
File cachedFile = FileUtils.findFileInFolder(destinationFolder, fileName);
if (cachedFile != null && cachedFile.isFile() && request.canUseCache()) {
// file exists
ChatResponse<ResultDownloadFile> response = PodDownloader.generateDownloadResult(request.getHashCode(), request.getImageId(), cachedFile);
progressHandler.onFileReady(response);
return uniqueId;
}
// only url should return in callback
if (!hasFreeSpace) {
progressHandler.onLowFreeSpace(uniqueId, url);
return uniqueId;
}
if (chatReady) {
Call call = PodDownloader.download(new ProgressHandler.IDownloadFile() {
@Override
public void onError(String mUniqueId, String error, String mUrl) {
progressHandler.onError(uniqueId, error, url);
}
@Override
public void onProgressUpdate(String mUniqueId, long bytesDownloaded, long totalBytesToDownload) {
progressHandler.onProgressUpdate(uniqueId, bytesDownloaded, totalBytesToDownload);
if (totalBytesToDownload > checkFreeSpace()) {
progressHandler.onLowFreeSpace(uniqueId, url);
}
}
@Override
public void onProgressUpdate(String mUniqueId, int progress) {
progressHandler.onProgressUpdate(uniqueId, progress);
}
@Override
public void onFileReady(ChatResponse<ResultDownloadFile> response) {
progressHandler.onFileReady(response);
}
}, getFileServer(), url, fileName, destinationFolder, downloaderErrorInterface, request.getHashCode(), request.getImageId());
downloadCallList.put(uniqueId, call);
} else
onChatNotReady(uniqueId);
return uniqueId;
}
use of com.fanap.podchat.ProgressHandler in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method getFile.
/**
* @param request desired file hashCode
* @param progressHandler callbacks for downloading progress.
* @return uniqueId of request.
*/
public String getFile(RequestGetPodSpaceFile request, ProgressHandler.IDownloadFile progressHandler) {
String uniqueId = generateUniqueId();
String url = getPodSpaceFileUrl(request.getHashCode());
showLog("DOWNLOAD FILE: " + url);
PodDownloader.IDownloaderError downloaderErrorInterface = getDownloaderErrorInterface(progressHandler, uniqueId, url);
File destinationFolder;
if (cache && request.canUseCache()) {
destinationFolder = FileUtils.getDownloadDirectory() != null ? FileUtils.getOrCreateDownloadDirectory(FileUtils.FILES) : FileUtils.getOrCreateDirectory(FileUtils.FILES);
} else {
destinationFolder = FileUtils.getDownloadDirectory() != null ? FileUtils.getOrCreateDownloadDirectory(FileUtils.FILES) : FileUtils.getPublicFilesDirectory();
}
showLog("Save in folder: " + destinationFolder);
String fileName = "file_" + request.getHashCode();
if (destinationFolder == null) {
showErrorLog("Error Creating destination folder");
progressHandler.onError(uniqueId, ChatConstant.ERROR_WRITING_FILE, url);
return uniqueId;
}
File cachedFile = FileUtils.findFileInFolder(destinationFolder, fileName);
if (cachedFile != null && cachedFile.isFile() && request.canUseCache()) {
showLog("File Exist in cache: " + cachedFile);
// file exists
ChatResponse<ResultDownloadFile> response = PodDownloader.generatePodSpaceDownloadResult(request.getHashCode(), cachedFile);
progressHandler.onFileReady(response);
return uniqueId;
}
// only url should return in callback
if (!hasFreeSpace) {
showErrorLog("Download couldn't start. cause: LOW FREE SPACE");
progressHandler.onLowFreeSpace(uniqueId, url);
return uniqueId;
}
if (chatReady) {
showLog("Download Started", request.toString());
Call call = PodDownloader.downloadFileFromPodSpace(new ProgressHandler.IDownloadFile() {
@Override
public void onError(String mUniqueId, String error, String mUrl) {
progressHandler.onError(uniqueId, error, url);
showErrorLog("Download Error. cause: " + error);
}
@Override
public void onProgressUpdate(String mUniqueId, long bytesDownloaded, long totalBytesToDownload) {
progressHandler.onProgressUpdate(uniqueId, bytesDownloaded, totalBytesToDownload);
if (totalBytesToDownload > checkFreeSpace()) {
showErrorLog("Total file space is more than free space");
progressHandler.onLowFreeSpace(uniqueId, url);
}
}
@Override
public void onProgressUpdate(String mUniqueId, int progress) {
progressHandler.onProgressUpdate(uniqueId, progress);
}
@Override
public void onFileReady(ChatResponse<ResultDownloadFile> response) {
progressHandler.onFileReady(response);
showLog("Download is complete!");
}
}, getToken(), TOKEN_ISSUER, request.getHashCode(), getPodSpaceServer(), fileName, destinationFolder, downloaderErrorInterface);
downloadCallList.put(uniqueId, call);
} else
onChatNotReady(uniqueId);
return uniqueId;
}
Aggregations