Няма описание

RNViewShotModule.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package fr.greweb.reactnativeviewshot;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.os.AsyncTask;
  5. import android.util.DisplayMetrics;
  6. import android.view.View;
  7. import com.facebook.react.bridge.ReactApplicationContext;
  8. import com.facebook.react.bridge.ReactContextBaseJavaModule;
  9. import com.facebook.react.bridge.ReactMethod;
  10. import com.facebook.react.bridge.GuardedAsyncTask;
  11. import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
  12. import com.facebook.react.bridge.Promise;
  13. import com.facebook.react.bridge.ReactContext;
  14. import com.facebook.react.bridge.ReadableMap;
  15. import com.facebook.react.uimanager.UIBlock;
  16. import com.facebook.react.uimanager.UIManagerModule;
  17. import java.io.File;
  18. import java.io.FilenameFilter;
  19. import java.io.IOException;
  20. public class RNViewShotModule extends ReactContextBaseJavaModule {
  21. private final ReactApplicationContext reactContext;
  22. public RNViewShotModule(ReactApplicationContext reactContext) {
  23. super(reactContext);
  24. this.reactContext = reactContext;
  25. }
  26. @Override
  27. public String getName() {
  28. return "RNViewShot";
  29. }
  30. @Override
  31. public void onCatalystInstanceDestroy() {
  32. super.onCatalystInstanceDestroy();
  33. new CleanTask(getReactApplicationContext()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  34. }
  35. @ReactMethod
  36. public void takeSnapshot(int tag, ReadableMap options, Promise promise) {
  37. ReactApplicationContext context = getReactApplicationContext();
  38. String format = options.hasKey("format") ? options.getString("format") : "png";
  39. Bitmap.CompressFormat compressFormat =
  40. format.equals("png")
  41. ? Bitmap.CompressFormat.PNG
  42. : format.equals("jpg")||format.equals("jpeg")
  43. ? Bitmap.CompressFormat.JPEG
  44. : format.equals("webm")
  45. ? Bitmap.CompressFormat.WEBP
  46. : null;
  47. if (compressFormat == null) {
  48. promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Unsupported image format: "+format+". Try one of: png | jpg | jpeg");
  49. return;
  50. }
  51. double quality = options.hasKey("quality") ? options.getDouble("quality") : 1.0;
  52. DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
  53. Integer width = options.hasKey("width") ? (int)(displayMetrics.density * options.getDouble("width")) : null;
  54. Integer height = options.hasKey("height") ? (int)(displayMetrics.density * options.getDouble("height")) : null;
  55. String result = options.hasKey("result") ? options.getString("result") : "file";
  56. Boolean snapshotContentContainer = options.hasKey("snapshotContentContainer") ? options.getBoolean("snapshotContentContainer"):false;
  57. try {
  58. String name = options.hasKey("filename") ? options.getString("filename") : null;
  59. File tmpFile = "file".equals(result) ? createTempFile(getReactApplicationContext(), format, name) : null;
  60. UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
  61. uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, tmpFile, result,snapshotContentContainer,promise));
  62. }
  63. catch (Exception e) {
  64. promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);
  65. }
  66. }
  67. private static final String TEMP_FILE_PREFIX = "ReactNative_snapshot_image_";
  68. /**
  69. * Asynchronous task that cleans up cache dirs (internal and, if available, external) of cropped
  70. * image files. This is run when the catalyst instance is being destroyed (i.e. app is shutting
  71. * down) and when the module is instantiated, to handle the case where the app crashed.
  72. */
  73. private static class CleanTask extends GuardedAsyncTask<Void, Void> {
  74. private final Context mContext;
  75. private CleanTask(ReactContext context) {
  76. super(context);
  77. mContext = context;
  78. }
  79. @Override
  80. protected void doInBackgroundGuarded(Void... params) {
  81. cleanDirectory(mContext.getCacheDir());
  82. File externalCacheDir = mContext.getExternalCacheDir();
  83. if (externalCacheDir != null) {
  84. cleanDirectory(externalCacheDir);
  85. }
  86. }
  87. private void cleanDirectory(File directory) {
  88. File[] toDelete = directory.listFiles(
  89. new FilenameFilter() {
  90. @Override
  91. public boolean accept(File dir, String filename) {
  92. return filename.startsWith(TEMP_FILE_PREFIX);
  93. }
  94. });
  95. if (toDelete != null) {
  96. for (File file: toDelete) {
  97. file.delete();
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * Create a temporary file in the cache directory on either internal or external storage,
  104. * whichever is available and has more free space.
  105. */
  106. private File createTempFile(Context context, String ext, String name)
  107. throws IOException {
  108. File externalCacheDir = context.getExternalCacheDir();
  109. File internalCacheDir = context.getCacheDir();
  110. File cacheDir;
  111. if (externalCacheDir == null && internalCacheDir == null) {
  112. throw new IOException("No cache directory available");
  113. }
  114. if (externalCacheDir == null) {
  115. cacheDir = internalCacheDir;
  116. }
  117. else if (internalCacheDir == null) {
  118. cacheDir = externalCacheDir;
  119. } else {
  120. cacheDir = externalCacheDir.getFreeSpace() > internalCacheDir.getFreeSpace() ?
  121. externalCacheDir : internalCacheDir;
  122. }
  123. String suffix = "." + ext;
  124. File tmpFile = File.createTempFile(TEMP_FILE_PREFIX, suffix, cacheDir);
  125. if (name != null) {
  126. File renamed = new File(cacheDir, name + suffix);
  127. tmpFile.renameTo(renamed);
  128. return renamed;
  129. }
  130. return tmpFile;
  131. }
  132. }