Search in sources :

Example 1 with ProgressHandler

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;
}
Also used : PodDownloader(com.fanap.podchat.chat.file_manager.download_file.PodDownloader) Call(retrofit2.Call) ProgressHandler(com.fanap.podchat.ProgressHandler) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) 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 2 with ProgressHandler

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;
}
Also used : PodDownloader(com.fanap.podchat.chat.file_manager.download_file.PodDownloader) Call(retrofit2.Call) ProgressHandler(com.fanap.podchat.ProgressHandler) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) 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 3 with ProgressHandler

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;
}
Also used : PodDownloader(com.fanap.podchat.chat.file_manager.download_file.PodDownloader) Call(retrofit2.Call) ProgressHandler(com.fanap.podchat.ProgressHandler) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) 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

ProgressHandler (com.fanap.podchat.ProgressHandler)3 PodDownloader (com.fanap.podchat.chat.file_manager.download_file.PodDownloader)3 ResultDownloadFile (com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile)3 MetaDataFile (com.fanap.podchat.model.MetaDataFile)3 ResultFile (com.fanap.podchat.model.ResultFile)3 ResultImageFile (com.fanap.podchat.model.ResultImageFile)3 RequestCreateThreadWithFile (com.fanap.podchat.requestobject.RequestCreateThreadWithFile)3 RequestGetFile (com.fanap.podchat.requestobject.RequestGetFile)3 RequestGetPodSpaceFile (com.fanap.podchat.requestobject.RequestGetPodSpaceFile)3 RequestUploadFile (com.fanap.podchat.requestobject.RequestUploadFile)3 File (java.io.File)3 Call (retrofit2.Call)3