use of com.fanap.podchat.requestobject.RequestGetPodSpaceFile 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();
// }
//
// }
//
//
// }
//
// });
}
use of com.fanap.podchat.requestobject.RequestGetPodSpaceFile 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;
}
use of com.fanap.podchat.requestobject.RequestGetPodSpaceFile in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method isAvailableInCache.
/**
* @param request contains desired file hashcode
* @return true if file exist in cache
*/
public boolean isAvailableInCache(RequestGetPodSpaceFile request) {
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");
return false;
}
File cachedFile = FileUtils.findFileInFolder(destinationFolder, fileName);
if (cachedFile != null && cachedFile.isFile() && request.canUseCache()) {
showLog("File Exist in cache: " + cachedFile);
return true;
}
return false;
}
Aggregations