Skip to content

Commit 3e64606

Browse files
authored
fix(android): run downloadFile asynchronously
1 parent 8c24903 commit 3e64606

File tree

2 files changed

+66
-21
lines changed

2 files changed

+66
-21
lines changed

filesystem/android/src/main/java/com/capacitorjs/plugins/filesystem/Filesystem.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import android.content.Context;
44
import android.net.Uri;
55
import android.os.Environment;
6+
import android.os.Handler;
7+
import android.os.Looper;
68
import android.util.Base64;
79
import com.capacitorjs.plugins.filesystem.exceptions.CopyFailedException;
810
import com.capacitorjs.plugins.filesystem.exceptions.DirectoryExistsException;
@@ -27,6 +29,8 @@
2729
import java.nio.charset.Charset;
2830
import java.nio.charset.StandardCharsets;
2931
import java.util.Locale;
32+
import java.util.concurrent.ExecutorService;
33+
import java.util.concurrent.Executors;
3034
import org.json.JSONException;
3135

3236
public class Filesystem {
@@ -303,9 +307,32 @@ public void copyRecursively(File src, File dst) throws IOException {
303307
}
304308
}
305309

306-
public JSObject downloadFile(PluginCall call, Bridge bridge, HttpRequestHandler.ProgressEmitter emitter)
307-
throws IOException, URISyntaxException, JSONException {
310+
public void downloadFile(
311+
PluginCall call,
312+
Bridge bridge,
313+
HttpRequestHandler.ProgressEmitter emitter,
314+
FilesystemDownloadCallback callback
315+
) {
308316
String urlString = call.getString("url", "");
317+
ExecutorService executor = Executors.newSingleThreadExecutor();
318+
Handler handler = new Handler(Looper.getMainLooper());
319+
320+
executor.execute(
321+
() -> {
322+
try {
323+
JSObject result = doDownloadInBackground(urlString, call, bridge, emitter);
324+
handler.post(() -> callback.onSuccess(result));
325+
} catch (Exception error) {
326+
handler.post(() -> callback.onError(error));
327+
} finally {
328+
executor.shutdown();
329+
}
330+
}
331+
);
332+
}
333+
334+
private JSObject doDownloadInBackground(String urlString, PluginCall call, Bridge bridge, HttpRequestHandler.ProgressEmitter emitter)
335+
throws IOException, URISyntaxException, JSONException {
309336
JSObject headers = call.getObject("headers", new JSObject());
310337
JSObject params = call.getObject("params", new JSObject());
311338
Integer connectTimeout = call.getInt("connectTimeout");
@@ -374,10 +401,14 @@ public JSObject downloadFile(PluginCall call, Bridge bridge, HttpRequestHandler.
374401
connectionInputStream.close();
375402
fileOutputStream.close();
376403

377-
return new JSObject() {
378-
{
379-
put("path", file.getAbsolutePath());
380-
}
381-
};
404+
JSObject ret = new JSObject();
405+
ret.put("path", file.getAbsolutePath());
406+
return ret;
407+
}
408+
409+
public interface FilesystemDownloadCallback {
410+
void onSuccess(JSObject result);
411+
412+
void onError(Exception error);
382413
}
383414
}

filesystem/android/src/main/java/com/capacitorjs/plugins/filesystem/FilesystemPlugin.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -388,23 +388,37 @@ public void downloadFile(PluginCall call) {
388388

389389
if (isPublicDirectory(directory) && !isStoragePermissionGranted()) {
390390
requestAllPermissions(call, "permissionCallback");
391-
} else {
392-
HttpRequestHandler.ProgressEmitter emitter = (bytes, contentLength) -> {
393-
JSObject ret = new JSObject();
394-
ret.put("url", call.getString("url"));
395-
ret.put("bytes", bytes);
396-
ret.put("contentLength", contentLength);
391+
return;
392+
}
397393

398-
notifyListeners("progress", ret);
399-
};
394+
HttpRequestHandler.ProgressEmitter emitter = (bytes, contentLength) -> {
395+
JSObject ret = new JSObject();
396+
ret.put("url", call.getString("url"));
397+
ret.put("bytes", bytes);
398+
ret.put("contentLength", contentLength);
399+
notifyListeners("progress", ret);
400+
};
401+
402+
implementation.downloadFile(
403+
call,
404+
bridge,
405+
emitter,
406+
new Filesystem.FilesystemDownloadCallback() {
407+
@Override
408+
public void onSuccess(JSObject response) {
409+
// update mediaStore index only if file was written to external storage
410+
if (isPublicDirectory(directory)) {
411+
MediaScannerConnection.scanFile(getContext(), new String[] { response.getString("path") }, null, null);
412+
}
413+
call.resolve(response);
414+
}
400415

401-
JSObject response = implementation.downloadFile(call, bridge, emitter);
402-
// update mediaStore index only if file was written to external storage
403-
if (isPublicDirectory(directory)) {
404-
MediaScannerConnection.scanFile(getContext(), new String[] { response.getString("path") }, null, null);
416+
@Override
417+
public void onError(Exception error) {
418+
call.reject("Error downloading file: " + error.getLocalizedMessage(), error);
419+
}
405420
}
406-
call.resolve(response);
407-
}
421+
);
408422
} catch (Exception ex) {
409423
call.reject("Error downloading file: " + ex.getLocalizedMessage(), ex);
410424
}

0 commit comments

Comments
 (0)