|
3 | 3 | import android.content.Context; |
4 | 4 | import android.net.Uri; |
5 | 5 | import android.os.Environment; |
| 6 | +import android.os.Handler; |
| 7 | +import android.os.Looper; |
6 | 8 | import android.util.Base64; |
7 | 9 | import com.capacitorjs.plugins.filesystem.exceptions.CopyFailedException; |
8 | 10 | import com.capacitorjs.plugins.filesystem.exceptions.DirectoryExistsException; |
|
27 | 29 | import java.nio.charset.Charset; |
28 | 30 | import java.nio.charset.StandardCharsets; |
29 | 31 | import java.util.Locale; |
| 32 | +import java.util.concurrent.ExecutorService; |
| 33 | +import java.util.concurrent.Executors; |
30 | 34 | import org.json.JSONException; |
31 | 35 |
|
32 | 36 | public class Filesystem { |
@@ -303,9 +307,32 @@ public void copyRecursively(File src, File dst) throws IOException { |
303 | 307 | } |
304 | 308 | } |
305 | 309 |
|
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 | + ) { |
308 | 316 | 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 { |
309 | 336 | JSObject headers = call.getObject("headers", new JSObject()); |
310 | 337 | JSObject params = call.getObject("params", new JSObject()); |
311 | 338 | Integer connectTimeout = call.getInt("connectTimeout"); |
@@ -374,10 +401,14 @@ public JSObject downloadFile(PluginCall call, Bridge bridge, HttpRequestHandler. |
374 | 401 | connectionInputStream.close(); |
375 | 402 | fileOutputStream.close(); |
376 | 403 |
|
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); |
382 | 413 | } |
383 | 414 | } |
0 commit comments