Browse Source

Update Android functionality to provide the "captureScreenshot". This will capture the entire screen and all the views currently displayed. This performs a native screen capture so a ref tag is not required.

foxmicha 7 years ago
parent
commit
8df4c24ee3

+ 6
- 1
android/src/main/java/fr/greweb/reactnativeviewshot/RNViewShotModule.java View File

85
               file = createTempFile(getReactApplicationContext(), format);
85
               file = createTempFile(getReactApplicationContext(), format);
86
             }
86
             }
87
             UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
87
             UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
88
-            uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, file, result, snapshotContentContainer,reactContext, promise));
88
+            uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, file, result, snapshotContentContainer,reactContext, getCurrentActivity(), promise));
89
         }
89
         }
90
         catch (Exception e) {
90
         catch (Exception e) {
91
             promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);
91
             promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);
92
         }
92
         }
93
     }
93
     }
94
 
94
 
95
+    @ReactMethod
96
+    public void captureScreenshot(ReadableMap options, Promise promise) {
97
+        captureRef(-1, options, promise);
98
+    }
99
+
95
     private static final String TEMP_FILE_PREFIX = "ReactNative-snapshot-image";
100
     private static final String TEMP_FILE_PREFIX = "ReactNative-snapshot-image";
96
 
101
 
97
     /**
102
     /**

+ 11
- 1
android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java View File

44
     private Promise promise;
44
     private Promise promise;
45
     private Boolean snapshotContentContainer;
45
     private Boolean snapshotContentContainer;
46
     private  ReactApplicationContext reactContext;
46
     private  ReactApplicationContext reactContext;
47
+    private Activity currentActivity;
47
 
48
 
48
     public ViewShot(
49
     public ViewShot(
49
             int tag,
50
             int tag,
56
             String result,
57
             String result,
57
             Boolean snapshotContentContainer,
58
             Boolean snapshotContentContainer,
58
             ReactApplicationContext reactContext,
59
             ReactApplicationContext reactContext,
60
+            Activity currentActivity,
59
             Promise promise) {
61
             Promise promise) {
60
         this.tag = tag;
62
         this.tag = tag;
61
         this.extension = extension;
63
         this.extension = extension;
67
         this.result = result;
69
         this.result = result;
68
         this.snapshotContentContainer = snapshotContentContainer;
70
         this.snapshotContentContainer = snapshotContentContainer;
69
         this.reactContext = reactContext;
71
         this.reactContext = reactContext;
72
+        this.currentActivity = currentActivity;
70
         this.promise = promise;
73
         this.promise = promise;
71
     }
74
     }
72
 
75
 
73
     @Override
76
     @Override
74
     public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
77
     public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
75
         OutputStream os = null;
78
         OutputStream os = null;
76
-        View view = nativeViewHierarchyManager.resolveView(tag);
79
+        View view = null;
80
+
81
+        if (tag == -1) {
82
+            view = currentActivity.getWindow().getDecorView().findViewById(android.R.id.content);
83
+        } else {
84
+            view = nativeViewHierarchyManager.resolveView(tag);
85
+        }
86
+
77
         if (view == null) {
87
         if (view == null) {
78
             promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "No view found with reactTag: "+tag);
88
             promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "No view found with reactTag: "+tag);
79
             return;
89
             return;

+ 13
- 0
src/index.js View File

114
   }
114
   }
115
 }
115
 }
116
 
116
 
117
+export function captureScreenshot(
118
+  optionsObject?: Options
119
+): Promise<string> {
120
+  const { options, errors } = validateOptions(optionsObject);
121
+  if (__DEV__ && errors.length > 0) {
122
+    console.warn(
123
+      "react-native-view-shot: bad options:\n" +
124
+        errors.map(e => `- ${e}`).join("\n")
125
+    );
126
+  }
127
+  return RNViewShot.captureScreenshot(options);
128
+}
129
+
117
 type Props = {
130
 type Props = {
118
   options?: Object,
131
   options?: Object,
119
   captureMode?: "mount" | "continuous" | "update",
132
   captureMode?: "mount" | "continuous" | "update",