From 7f92f84087e8d7b114052c5e00031225de7c45a7 Mon Sep 17 00:00:00 2001 From: Kesha Antonov Date: Wed, 24 Jan 2024 13:06:10 +0300 Subject: [PATCH] android: fix mv file on api level < 26 --- .../com/eko/RNBackgroundDownloaderModule.java | 79 ++++++++++--------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/android/src/main/java/com/eko/RNBackgroundDownloaderModule.java b/android/src/main/java/com/eko/RNBackgroundDownloaderModule.java index 78de06b..c018722 100644 --- a/android/src/main/java/com/eko/RNBackgroundDownloaderModule.java +++ b/android/src/main/java/com/eko/RNBackgroundDownloaderModule.java @@ -36,8 +36,6 @@ import javax.annotation.Nullable; import java.util.Set; import java.net.URL; import java.net.URLConnection; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; import android.content.BroadcastReceiver; import android.content.Context; @@ -103,7 +101,6 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule { WritableMap downloadStatus = downloader.checkDownloadStatus(downloadId); int status = downloadStatus.getInt("status"); - stopTrackingProgress(config.id); synchronized (sharedLock) { @@ -113,7 +110,12 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule { String localUri = downloadStatus.getString("localUri"); File file = new File(localUri); File dest = new File(config.destination); - Files.move(file.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING); + + if (dest.exists()) { + dest.delete(); + } + + file.renameTo(dest); WritableMap params = Arguments.createMap(); params.putString("id", config.id); @@ -157,7 +159,7 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule { compatRegisterReceiver(reactContext, downloadReceiver, filter, true); // iterate over downloadIdToConfig - for(Map.Entry entry : downloadIdToConfig.entrySet()) { + for (Map.Entry entry : downloadIdToConfig.entrySet()) { Long downloadId = entry.getKey(); RNBGDTaskConfig config = entry.getValue(); @@ -292,7 +294,7 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule { ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file)); Map config = (HashMap) inputStream.readObject(); // iterate over config - for(Map.Entry entry : config.entrySet()) { + for (Map.Entry entry : config.entrySet()) { String key = entry.getKey(); Object valueObj = entry.getValue(); @@ -357,7 +359,8 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule { request.setDestinationInExternalFilesDir(this.getReactApplicationContext(), null, fileName); // TOREMOVE - // request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS.toString(), fileName); + // request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS.toString(), + // fileName); // request.setDestinationUri(Uri.parse(destination)); if (headers != null) { @@ -419,40 +422,37 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule { onBeginTh.join(); OnProgress onProgressTh = new OnProgress( - config, - downloadId, - downloader, - new ProgressCallback() { - @Override - public void onProgress(String configId, int bytesDownloaded, int bytesTotal) { - double prevPercent = configIdToPercent.get(configId); - double percent = (double) bytesDownloaded / bytesTotal; - if (percent - prevPercent > 0.01) { - WritableMap params = Arguments.createMap(); - params.putString("id", configId); - params.putInt("bytesDownloaded", bytesDownloaded); - params.putInt("bytesTotal", bytesTotal); + config, + downloadId, + downloader, + new ProgressCallback() { + @Override + public void onProgress(String configId, int bytesDownloaded, int bytesTotal) { + double prevPercent = configIdToPercent.get(configId); + double percent = (double) bytesDownloaded / bytesTotal; + if (percent - prevPercent > 0.01) { + WritableMap params = Arguments.createMap(); + params.putString("id", configId); + params.putInt("bytesDownloaded", bytesDownloaded); + params.putInt("bytesTotal", bytesTotal); - progressReports.put(configId, params); - configIdToPercent.put(configId, percent); - } - - Date now = new Date(); - if ( - now.getTime() - lastProgressReportedAt.getTime() > progressInterval && - progressReports.size() > 0 - ) { - WritableArray reportsArray = Arguments.createArray(); - for (Object report : progressReports.values()) { - reportsArray.pushMap((WritableMap) report); + progressReports.put(configId, params); + configIdToPercent.put(configId, percent); + } + + Date now = new Date(); + if (now.getTime() - lastProgressReportedAt.getTime() > progressInterval && + progressReports.size() > 0) { + WritableArray reportsArray = Arguments.createArray(); + for (Object report : progressReports.values()) { + reportsArray.pushMap((WritableMap) report); + } + ee.emit("downloadProgress", reportsArray); + lastProgressReportedAt = now; + progressReports.clear(); } - ee.emit("downloadProgress", reportsArray); - lastProgressReportedAt = now; - progressReports.clear(); } - } - } - ); + }); onProgressThreads.put(config.id, onProgressTh); onProgressTh.start(); } catch (Exception e) { @@ -506,7 +506,8 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule { Long downloadId = configIdToDownloadId.get(configId); if (downloadId != null) { removeFromMaps(downloadId); - // REMOVES DOWNLOAD FROM DownloadManager SO IT WOULD NOT BE RETURNED IN checkForExistingDownloads + // REMOVES DOWNLOAD FROM DownloadManager SO IT WOULD NOT BE RETURNED IN + // checkForExistingDownloads downloader.cancelDownload(downloadId); } }