|
2 | 2 |
|
3 | 3 | import android.app.Activity; |
4 | 4 | import android.app.DownloadManager; |
| 5 | +import android.content.ActivityNotFoundException; |
5 | 6 | import android.content.Intent; |
6 | | -import android.content.pm.PackageManager; |
7 | 7 | import android.net.Uri; |
8 | 8 | import android.os.Build; |
| 9 | +import android.util.SparseArray; |
9 | 10 |
|
| 11 | +import androidx.annotation.NonNull; |
| 12 | +import androidx.annotation.RequiresApi; |
10 | 13 | import androidx.core.content.FileProvider; |
11 | 14 |
|
12 | | -import android.util.SparseArray; |
13 | | -import android.content.ActivityNotFoundException; |
14 | | - |
| 15 | +import com.ReactNativeBlobUtil.Utils.FileDescription; |
15 | 16 | import com.facebook.react.bridge.ActivityEventListener; |
16 | 17 | import com.facebook.react.bridge.Callback; |
17 | 18 | import com.facebook.react.bridge.LifecycleEventListener; |
|
21 | 22 | import com.facebook.react.bridge.ReactMethod; |
22 | 23 | import com.facebook.react.bridge.ReadableArray; |
23 | 24 | import com.facebook.react.bridge.ReadableMap; |
24 | | - |
25 | | -// Cookies |
26 | 25 | import com.facebook.react.bridge.WritableMap; |
27 | | -import com.facebook.react.modules.network.ForwardingCookieHandler; |
28 | 26 | import com.facebook.react.modules.network.CookieJarContainer; |
| 27 | +import com.facebook.react.modules.network.ForwardingCookieHandler; |
29 | 28 | import com.facebook.react.modules.network.OkHttpClientProvider; |
30 | 29 |
|
31 | | -import okhttp3.JavaNetCookieJar; |
32 | | -import okhttp3.OkHttpClient; |
33 | | - |
34 | | -import javax.annotation.Nullable; |
35 | | - |
36 | 30 | import java.io.File; |
37 | 31 | import java.util.Map; |
38 | 32 | import java.util.concurrent.LinkedBlockingQueue; |
39 | 33 | import java.util.concurrent.ThreadPoolExecutor; |
40 | 34 | import java.util.concurrent.TimeUnit; |
41 | 35 |
|
| 36 | +import javax.annotation.Nullable; |
| 37 | + |
| 38 | +import okhttp3.JavaNetCookieJar; |
| 39 | +import okhttp3.OkHttpClient; |
| 40 | + |
42 | 41 | import static android.app.Activity.RESULT_OK; |
43 | 42 | import static com.ReactNativeBlobUtil.ReactNativeBlobUtilConst.GET_CONTENT_INTENT; |
44 | 43 |
|
@@ -81,6 +80,7 @@ public void onNewIntent(Intent intent) { |
81 | 80 | }); |
82 | 81 | } |
83 | 82 |
|
| 83 | + @NonNull |
84 | 84 | @Override |
85 | 85 | public String getName() { |
86 | 86 | return "ReactNativeBlobUtil"; |
@@ -114,9 +114,13 @@ public void run() { |
114 | 114 | @ReactMethod |
115 | 115 | public void actionViewIntent(String path, String mime, @Nullable String chooserTitle, final Promise promise) { |
116 | 116 | try { |
117 | | - Uri uriForFile = FileProvider.getUriForFile(this.getReactApplicationContext(), |
118 | | - this.getReactApplicationContext().getPackageName() + ".provider", new File(path)); |
119 | | - |
| 117 | + Uri uriForFile = null; |
| 118 | + if (!ReactNativeBlobUtilUtils.isContentUri(path)) { |
| 119 | + uriForFile = FileProvider.getUriForFile(this.getReactApplicationContext(), |
| 120 | + this.getReactApplicationContext().getPackageName() + ".provider", new File(path)); |
| 121 | + } else { |
| 122 | + uriForFile = Uri.parse(path); |
| 123 | + } |
120 | 124 | Intent intent = new Intent(Intent.ACTION_VIEW); |
121 | 125 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
122 | 126 | // Create the intent with data and type |
@@ -170,7 +174,7 @@ public void onHostDestroy() { |
170 | 174 |
|
171 | 175 | @ReactMethod |
172 | 176 | public void writeArrayChunk(final String streamId, final ReadableArray dataArray, final Callback callback) { |
173 | | - ReactNativeBlobUtilFS.writeArrayChunk(streamId, dataArray, callback); |
| 177 | + ReactNativeBlobUtilStream.writeArrayChunk(streamId, dataArray, callback); |
174 | 178 | } |
175 | 179 |
|
176 | 180 | @ReactMethod |
@@ -210,17 +214,17 @@ public void ls(String path, Promise promise) { |
210 | 214 |
|
211 | 215 | @ReactMethod |
212 | 216 | public void writeStream(String path, String encode, boolean append, Callback callback) { |
213 | | - new ReactNativeBlobUtilFS(this.getReactApplicationContext()).writeStream(path, encode, append, callback); |
| 217 | + new ReactNativeBlobUtilStream(this.getReactApplicationContext()).writeStream(path, encode, append, callback); |
214 | 218 | } |
215 | 219 |
|
216 | 220 | @ReactMethod |
217 | 221 | public void writeChunk(String streamId, String data, Callback callback) { |
218 | | - ReactNativeBlobUtilFS.writeChunk(streamId, data, callback); |
| 222 | + ReactNativeBlobUtilStream.writeChunk(streamId, data, callback); |
219 | 223 | } |
220 | 224 |
|
221 | 225 | @ReactMethod |
222 | 226 | public void closeStream(String streamId, Callback callback) { |
223 | | - ReactNativeBlobUtilFS.closeStream(streamId, callback); |
| 227 | + ReactNativeBlobUtilStream.closeStream(streamId, callback); |
224 | 228 | } |
225 | 229 |
|
226 | 230 | @ReactMethod |
@@ -313,7 +317,7 @@ public void readStream(final String path, final String encoding, final int buffe |
313 | 317 | fsThreadPool.execute(new Runnable() { |
314 | 318 | @Override |
315 | 319 | public void run() { |
316 | | - ReactNativeBlobUtilFS fs = new ReactNativeBlobUtilFS(ctx); |
| 320 | + ReactNativeBlobUtilStream fs = new ReactNativeBlobUtilStream(ctx); |
317 | 321 | fs.readStream(path, encoding, bufferSize, tick, streamId); |
318 | 322 | } |
319 | 323 | }); |
@@ -386,7 +390,7 @@ public void addCompleteDownload(ReadableMap config, Promise promise) { |
386 | 390 | promise.reject("EINVAL", "ReactNativeBlobUtil.addCompleteDownload config or path missing."); |
387 | 391 | return; |
388 | 392 | } |
389 | | - String path = ReactNativeBlobUtilFS.normalizePath(config.getString("path")); |
| 393 | + String path = ReactNativeBlobUtilUtils.normalizePath(config.getString("path")); |
390 | 394 | if (path == null) { |
391 | 395 | promise.reject("EINVAL", "ReactNativeBlobUtil.addCompleteDownload can not resolve URI:" + config.getString("path")); |
392 | 396 | return; |
@@ -418,4 +422,65 @@ public void getSDCardDir(Promise promise) { |
418 | 422 | public void getSDCardApplicationDir(Promise promise) { |
419 | 423 | ReactNativeBlobUtilFS.getSDCardApplicationDir(this.getReactApplicationContext(), promise); |
420 | 424 | } |
| 425 | + |
| 426 | + @ReactMethod |
| 427 | + public void createMediaFile(ReadableMap filedata, String mt, Promise promise) { |
| 428 | + if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) { |
| 429 | + promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid filedata: " + filedata.toString()); |
| 430 | + return; |
| 431 | + } |
| 432 | + if (mt == null) promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid mediatype"); |
| 433 | + |
| 434 | + FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder")); |
| 435 | + Uri res = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt)); |
| 436 | + if (res != null) promise.resolve(res.toString()); |
| 437 | + else promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created"); |
| 438 | + } |
| 439 | + |
| 440 | + @RequiresApi(api = Build.VERSION_CODES.Q) |
| 441 | + @ReactMethod |
| 442 | + public void writeToMediaFile(String fileUri, String path, Promise promise) { |
| 443 | + boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, promise); |
| 444 | + if(res) promise.resolve("Success"); |
| 445 | + } |
| 446 | + |
| 447 | + @ReactMethod |
| 448 | + public void copyToInternal(String contentUri, String destpath, Promise promise) { |
| 449 | + ReactNativeBlobUtilMediaCollection.copyToInternal(Uri.parse(contentUri), destpath, promise); |
| 450 | + } |
| 451 | + |
| 452 | + @ReactMethod |
| 453 | + public void getBlob(String contentUri, String encoding, Promise promise) { |
| 454 | + ReactNativeBlobUtilMediaCollection.getBlob(Uri.parse(contentUri), encoding, promise); |
| 455 | + } |
| 456 | + |
| 457 | + @RequiresApi(api = Build.VERSION_CODES.Q) |
| 458 | + @ReactMethod |
| 459 | + public void copyToMediaStore(ReadableMap filedata, String mt, String path, Promise promise) { |
| 460 | + if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) { |
| 461 | + promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid filedata: " + filedata.toString()); |
| 462 | + return; |
| 463 | + } |
| 464 | + if (mt == null) { |
| 465 | + promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid mediatype"); |
| 466 | + return; |
| 467 | + } |
| 468 | + if (path == null) { |
| 469 | + promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid path"); |
| 470 | + return; |
| 471 | + } |
| 472 | + |
| 473 | + FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder")); |
| 474 | + Uri fileuri = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt)); |
| 475 | + |
| 476 | + if (fileuri == null) { |
| 477 | + promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created"); |
| 478 | + return; |
| 479 | + } |
| 480 | + |
| 481 | + boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, promise); |
| 482 | + |
| 483 | + if(res) promise.resolve(fileuri.toString()); |
| 484 | + } |
| 485 | + |
421 | 486 | } |
0 commit comments