Search in sources :

Example 1 with RequestGetFile

use of com.fanap.podchat.requestobject.RequestGetFile in project pod-chat-android-sdk by FanapSoft.

the class ChatActivity method downloadFile.

private void downloadFile() {
    String url = "https://core.pod.ir/nzh/image?imageId=222808&hashCode=16c3cd2b93f-0.527719303638482";
    // image
    // long imageId = 404143;
    long imageId = 68349;
    long fileId = 68684;
    // String imageHashCode = "17024463974-0.5588549950419339";
    String imageHashCode = "16feadc7bf1-0.5248624596970302";
    String fileHashCode = "17077360d4b-0.2366487166443898";
    RequestGetImage requestGetImage = new RequestGetImage.Builder(imageId, imageHashCode, true).build();
    RequestGetFile requestGetFile = new RequestGetFile.Builder(fileId, fileHashCode, true).build();
    RequestGetPodSpaceFile rePod = new RequestGetPodSpaceFile.Builder("GAUPE1ZRK76VOXBS").build();
    RequestGetPodSpaceImage rePodImage = new RequestGetPodSpaceImage.Builder("613Q7WCCEXZ1DGY5").setQuality(0.45f).build();
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_WRITE_EXTERNAL_STORAGE);
        return;
    }
    downloadingId = presenter.downloadFile(rePodImage, new ProgressHandler.IDownloadFile() {

        @Override
        public void onProgressUpdate(String uniqueId, long bytesDownloaded, long totalBytesToDownload) {
            Log.e("DOWNLOAD", "IN ACTIVITY: " + "Downloaded: " + bytesDownloaded + " Left: " + totalBytesToDownload);
        }

        @Override
        public void onProgressUpdate(String uniqueId, int progress) {
            Log.e("DOWNLOAD", "IN ACTIVITY: " + "Progress: " + progress);
        }

        @Override
        public void onError(String uniqueId, String error, String url) {
            Log.e("DOWNLOAD", "IN ACTIVITY: ERROR :(((");
        }

        @Override
        public void onLowFreeSpace(String uniqueId, String url) {
            Log.e("DOWNLOAD", "Low Space...");
        }

        @Override
        public void onFileReady(ChatResponse<ResultDownloadFile> response) {
            Log.e("DOWNLOAD", "IN ACTIVITY: Finish File!!!!");
            Log.e("DOWNLOAD", "File name: " + response.getResult().getFile().getName());
            Log.e("DOWNLOAD", "Uri " + response.getResult().getUri());
            Log.e("DOWNLOAD", "File Exist " + response.getResult().getFile().exists());
            if (response.getResult().getFile().exists()) {
                try {
                    Bitmap v = BitmapFactory.decodeFile(response.getResult().getFile().getAbsolutePath());
                    runOnUiThread(() -> {
                    // imageMap.setImageBitmap(v)
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("DOWNLOAD", "Not Image");
                }
            }
        }
    });
// downloadingId = presenter.downloadFile(requestGetImage, new ProgressHandler.IDownloadFile() {
// 
// 
// @Override
// public void onProgressUpdate(String uniqueId, int bytesDownloaded, int totalBytesToDownload) {
// Log.e("DOWNLOAD", "IN ACTIVITY: " + "Downloaded: " + bytesDownloaded + " Left: " + totalBytesToDownload);
// 
// }
// 
// @Override
// public void onProgressUpdate(String uniqueId, int progress) {
// 
// }
// 
// @Override
// public void onError(String uniqueId, String error, String url) {
// Log.e("DOWNLOAD", "IN ACTIVITY: ERROR :(((");
// 
// }
// 
// @Override
// public void onLowFreeSpace(String uniqueId, String url) {
// Log.e("DOWNLOAD", "Low Space...");
// 
// }
// 
// @Override
// public void onFileReady(ChatResponse<ResultDownloadFile> response) {
// Log.e("DOWNLOAD", "IN ACTIVITY: Finish File!!!!");
// Log.e("DOWNLOAD", "File name: " + response.getResult().getFile().getName());
// Log.e("DOWNLOAD", "Uri " + response.getResult().getUri());
// Log.e("DOWNLOAD", "File Exist " + response.getResult().getFile().exists());
// 
// 
// if (response.getResult().getFile().exists()) {
// 
// 
// try {
// Bitmap v = BitmapFactory.decodeFile(response.getResult().getFile().getAbsolutePath());
// runOnUiThread(() -> imageMap.setImageBitmap(v));
// } catch (Exception e) {
// e.printStackTrace();
// }
// 
// }
// 
// 
// }
// 
// });
}
Also used : RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) GsonBuilder(com.google.gson.GsonBuilder) RequestGetImage(com.fanap.podchat.requestobject.RequestGetImage) RequestGetPodSpaceImage(com.fanap.podchat.requestobject.RequestGetPodSpaceImage) Bitmap(android.graphics.Bitmap) ChatResponse(com.fanap.podchat.model.ChatResponse) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile)

Example 2 with RequestGetFile

use of com.fanap.podchat.requestobject.RequestGetFile 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)

Aggregations

RequestGetFile (com.fanap.podchat.requestobject.RequestGetFile)2 RequestGetPodSpaceFile (com.fanap.podchat.requestobject.RequestGetPodSpaceFile)2 Bitmap (android.graphics.Bitmap)1 ProgressHandler (com.fanap.podchat.ProgressHandler)1 PodDownloader (com.fanap.podchat.chat.file_manager.download_file.PodDownloader)1 ResultDownloadFile (com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile)1 ChatResponse (com.fanap.podchat.model.ChatResponse)1 MetaDataFile (com.fanap.podchat.model.MetaDataFile)1 ResultFile (com.fanap.podchat.model.ResultFile)1 ResultImageFile (com.fanap.podchat.model.ResultImageFile)1 RequestCreateThreadWithFile (com.fanap.podchat.requestobject.RequestCreateThreadWithFile)1 RequestGetImage (com.fanap.podchat.requestobject.RequestGetImage)1 RequestGetPodSpaceImage (com.fanap.podchat.requestobject.RequestGetPodSpaceImage)1 RequestUploadFile (com.fanap.podchat.requestobject.RequestUploadFile)1 GsonBuilder (com.google.gson.GsonBuilder)1 File (java.io.File)1 Call (retrofit2.Call)1