Browse Source

feat(android): Add androidLayerType as prop (#1588)

* Add androidLayerType as a prop

* Deprecate "androidHardwareAccelerationDisabled" prop

* Update reference

Co-authored-by: Olivia Caraiman <olcaraim@microsoft.com>
oliviacaraiman 4 years ago
parent
commit
9ffca8f9db
No account linked to committer's email address

+ 15
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

@@ -299,6 +299,21 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
299 299
     }
300 300
   }
301 301
 
302
+  @ReactProp(name = "androidLayerType")
303
+  public void setLayerType(WebView view, String layerTypeString) {
304
+    int layerType = View.LAYER_TYPE_NONE;
305
+    switch (layerTypeString) {
306
+        case "hardware":
307
+          layerType = View.LAYER_TYPE_HARDWARE;
308
+          break;
309
+        case "software":
310
+          layerType = View.LAYER_TYPE_SOFTWARE;
311
+          break;
312
+    }
313
+    view.setLayerType(layerType, null);
314
+  }
315
+
316
+
302 317
   @ReactProp(name = "overScrollMode")
303 318
   public void setOverScrollMode(WebView view, String overScrollModeString) {
304 319
     Integer overScrollMode;

+ 20
- 1
docs/Reference.md View File

@@ -35,6 +35,7 @@ This document lays out the current public properties and methods for the React N
35 35
 - [`javaScriptEnabled`](Reference.md#javascriptenabled)
36 36
 - [`javaScriptCanOpenWindowsAutomatically`](Reference.md#javascriptcanopenwindowsautomatically)
37 37
 - [`androidHardwareAccelerationDisabled`](Reference.md#androidHardwareAccelerationDisabled)
38
+- [`androidLayerType`](Reference.md#androidLayerType)
38 39
 - [`mixedContentMode`](Reference.md#mixedcontentmode)
39 40
 - [`thirdPartyCookiesEnabled`](Reference.md#thirdpartycookiesenabled)
40 41
 - [`userAgent`](Reference.md#useragent)
@@ -781,7 +782,7 @@ A Boolean value indicating whether JavaScript can open windows without user inte
781 782
 
782 783
 ### `androidHardwareAccelerationDisabled`[⬆](#props-index)<!-- Link generated with jump2header -->
783 784
 
784
-Boolean value to disable Hardware Acceleration in the `WebView`. Used on Android only as Hardware Acceleration is a feature only for Android. The default value is `false`.
785
+**Deprecated.** Use the `androidLayerType` prop instead. 
785 786
 
786 787
 | Type | Required | Platform |
787 788
 | ---- | -------- | -------- |
@@ -789,6 +790,24 @@ Boolean value to disable Hardware Acceleration in the `WebView`. Used on Android
789 790
 
790 791
 ---
791 792
 
793
+### `androidLayerType`[⬆](#props-index)<!-- Link generated with jump2header -->
794
+
795
+Specifies the layer type. 
796
+
797
+Possible values for `androidLayerType` are:
798
+
799
+- `none` (default) - The view does not have a layer.
800
+- `software` - The view has a software layer. A software layer is backed by a bitmap and causes the view to be rendered using Android's software rendering pipeline, even if hardware acceleration is enabled.
801
+- `hardware` - The view has a hardware layer. A hardware layer is backed by a hardware specific texture and causes the view to be rendered using Android's hardware rendering pipeline, but only if hardware acceleration is turned on for the view hierarchy.
802
+
803
+Avoid setting both `androidLayerType` and `androidHardwareAccelerationDisabled` props at the same time, as this may cause undefined behaviour.
804
+
805
+| Type   | Required | Platform |
806
+| ------ | -------- | -------- |
807
+| string | No       | Android  |
808
+
809
+---
810
+
792 811
 ### `mixedContentMode`[⬆](#props-index)<!-- Link generated with jump2header -->
793 812
 
794 813
 Specifies the mixed content mode. i.e WebView will allow a secure origin to load content from any other origin.

+ 1
- 0
src/WebView.android.tsx View File

@@ -61,6 +61,7 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
61 61
     saveFormDataDisabled: false,
62 62
     cacheEnabled: true,
63 63
     androidHardwareAccelerationDisabled: false,
64
+    androidLayerType: 'none',
64 65
     originWhitelist: defaultOriginWhitelist,
65 66
   };
66 67
 

+ 15
- 0
src/WebViewTypes.ts View File

@@ -182,6 +182,8 @@ export type OverScrollModeType = 'always' | 'content' | 'never';
182 182
 
183 183
 export type CacheMode = 'LOAD_DEFAULT' | 'LOAD_CACHE_ONLY' | 'LOAD_CACHE_ELSE_NETWORK' | 'LOAD_NO_CACHE';
184 184
 
185
+export type AndroidLayerType = 'none' | 'software' | 'hardware';
186
+
185 187
 export interface WebViewSourceUri {
186 188
   /**
187 189
    * The URI to load in the `WebView`. Can be a local or remote file.
@@ -284,6 +286,7 @@ export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
284 286
   allowFileAccessFromFileURLs?: boolean;
285 287
   allowUniversalAccessFromFileURLs?: boolean;
286 288
   androidHardwareAccelerationDisabled?: boolean;
289
+  androidLayerType?: AndroidLayerType;
287 290
   domStorageEnabled?: boolean;
288 291
   geolocationEnabled?: boolean;
289 292
   javaScriptEnabled?: boolean;
@@ -809,6 +812,18 @@ export interface AndroidWebViewProps extends WebViewSharedProps {
809 812
    */
810 813
   androidHardwareAccelerationDisabled?: boolean;
811 814
 
815
+    /**
816
+   * https://developer.android.com/reference/android/webkit/WebView#setLayerType(int,%20android.graphics.Paint)
817
+   * Sets the layerType. Possible values are:
818
+   *
819
+   * - `'none'` (default)
820
+   * - `'software'`
821
+   * - `'hardware'`
822
+   *
823
+   * @platform android
824
+   */
825
+  androidLayerType?: AndroidLayerType;
826
+
812 827
   /**
813 828
    * Boolean value to enable third party cookies in the `WebView`. Used on
814 829
    * Android Lollipop and above only as third party cookies are enabled by