|
@@ -4,6 +4,7 @@ package fr.greweb.reactnativeviewshot;
|
4
|
4
|
import android.content.Context;
|
5
|
5
|
import android.graphics.Bitmap;
|
6
|
6
|
import android.os.AsyncTask;
|
|
7
|
+import android.os.Environment;
|
7
|
8
|
import android.util.DisplayMetrics;
|
8
|
9
|
import android.view.View;
|
9
|
10
|
|
|
@@ -22,6 +23,8 @@ import com.facebook.react.uimanager.UIManagerModule;
|
22
|
23
|
import java.io.File;
|
23
|
24
|
import java.io.FilenameFilter;
|
24
|
25
|
import java.io.IOException;
|
|
26
|
+import java.util.HashMap;
|
|
27
|
+import java.util.Map;
|
25
|
28
|
|
26
|
29
|
public class RNViewShotModule extends ReactContextBaseJavaModule {
|
27
|
30
|
|
|
@@ -37,6 +40,11 @@ public class RNViewShotModule extends ReactContextBaseJavaModule {
|
37
|
40
|
return "RNViewShot";
|
38
|
41
|
}
|
39
|
42
|
|
|
43
|
+ @Override
|
|
44
|
+ public Map<String, Object> getConstants() {
|
|
45
|
+ return getSystemFolders(this.getReactApplicationContext());
|
|
46
|
+ }
|
|
47
|
+
|
40
|
48
|
@Override
|
41
|
49
|
public void onCatalystInstanceDestroy() {
|
42
|
50
|
super.onCatalystInstanceDestroy();
|
|
@@ -66,17 +74,25 @@ public class RNViewShotModule extends ReactContextBaseJavaModule {
|
66
|
74
|
String result = options.hasKey("result") ? options.getString("result") : "file";
|
67
|
75
|
Boolean snapshotContentContainer = options.hasKey("snapshotContentContainer") ? options.getBoolean("snapshotContentContainer") : false;
|
68
|
76
|
try {
|
69
|
|
- String name = options.hasKey("filename") ? options.getString("filename") : null;
|
70
|
|
- File tmpFile = "file".equals(result) ? createTempFile(getReactApplicationContext(), format, name) : null;
|
|
77
|
+ File file = null;
|
|
78
|
+ if ("file".equals(result)) {
|
|
79
|
+ if (options.hasKey("path")) {
|
|
80
|
+ file = new File(options.getString("path"));
|
|
81
|
+ file.createNewFile();
|
|
82
|
+ }
|
|
83
|
+ else {
|
|
84
|
+ file = createTempFile(getReactApplicationContext(), format);
|
|
85
|
+ }
|
|
86
|
+ }
|
71
|
87
|
UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
|
72
|
|
- uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, tmpFile, result, snapshotContentContainer, promise));
|
|
88
|
+ uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, file, result, snapshotContentContainer, promise));
|
73
|
89
|
}
|
74
|
90
|
catch (Exception e) {
|
75
|
91
|
promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);
|
76
|
92
|
}
|
77
|
93
|
}
|
78
|
94
|
|
79
|
|
- private static final String TEMP_FILE_PREFIX = "ReactNative_snapshot_image_";
|
|
95
|
+ private static final String TEMP_FILE_PREFIX = "ReactNative-snapshot-image";
|
80
|
96
|
|
81
|
97
|
/**
|
82
|
98
|
* Asynchronous task that cleans up cache dirs (internal and, if available, external) of cropped
|
|
@@ -116,11 +132,30 @@ public class RNViewShotModule extends ReactContextBaseJavaModule {
|
116
|
132
|
}
|
117
|
133
|
}
|
118
|
134
|
|
|
135
|
+ static private Map<String, Object> getSystemFolders(ReactApplicationContext ctx) {
|
|
136
|
+ Map<String, Object> res = new HashMap<>();
|
|
137
|
+ res.put("CacheDir", ctx.getCacheDir().getAbsolutePath());
|
|
138
|
+ res.put("DCIMDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath());
|
|
139
|
+ res.put("DocumentDir", ctx.getFilesDir().getAbsolutePath());
|
|
140
|
+ res.put("DownloadDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
|
|
141
|
+ res.put("MainBundleDir", ctx.getApplicationInfo().dataDir);
|
|
142
|
+ res.put("MovieDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath());
|
|
143
|
+ res.put("MusicDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath());
|
|
144
|
+ res.put("PictureDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
|
|
145
|
+ res.put("RingtoneDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES).getAbsolutePath());
|
|
146
|
+ String state;
|
|
147
|
+ state = Environment.getExternalStorageState();
|
|
148
|
+ if (state.equals(Environment.MEDIA_MOUNTED)) {
|
|
149
|
+ res.put("SDCardDir", Environment.getExternalStorageDirectory().getAbsolutePath());
|
|
150
|
+ }
|
|
151
|
+ return res;
|
|
152
|
+ }
|
|
153
|
+
|
119
|
154
|
/**
|
120
|
155
|
* Create a temporary file in the cache directory on either internal or external storage,
|
121
|
156
|
* whichever is available and has more free space.
|
122
|
157
|
*/
|
123
|
|
- private File createTempFile(Context context, String ext, String name)
|
|
158
|
+ private File createTempFile(Context context, String ext)
|
124
|
159
|
throws IOException {
|
125
|
160
|
File externalCacheDir = context.getExternalCacheDir();
|
126
|
161
|
File internalCacheDir = context.getCacheDir();
|
|
@@ -139,12 +174,6 @@ public class RNViewShotModule extends ReactContextBaseJavaModule {
|
139
|
174
|
}
|
140
|
175
|
String suffix = "." + ext;
|
141
|
176
|
File tmpFile = File.createTempFile(TEMP_FILE_PREFIX, suffix, cacheDir);
|
142
|
|
- if (name != null) {
|
143
|
|
- File renamed = new File(cacheDir, name + suffix);
|
144
|
|
- tmpFile.renameTo(renamed);
|
145
|
|
- return renamed;
|
146
|
|
- }
|
147
|
|
-
|
148
|
177
|
return tmpFile;
|
149
|
178
|
}
|
150
|
179
|
|