Browse Source

fix(android): replace deprecated `WebView.PictureListener`->`onContentSizeChange` impl (#164)

* [android] replace deprecated `WebView.PictureListener` onSizeChanged implementation

* [android] use pre-provided w/h values
Michael Diarmid 5 years ago
parent
commit
9014c4cac0

+ 20
- 24
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

@@ -23,7 +23,6 @@ import java.util.Map;
23 23
 import android.content.ActivityNotFoundException;
24 24
 import android.content.Intent;
25 25
 import android.graphics.Bitmap;
26
-import android.graphics.Picture;
27 26
 import android.net.Uri;
28 27
 import android.os.Build;
29 28
 import android.os.Environment;
@@ -126,7 +125,6 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
126 125
   protected static final String BLANK_URL = "about:blank";
127 126
 
128 127
   protected WebViewConfig mWebViewConfig;
129
-  protected @Nullable WebView.PictureListener mPictureListener;
130 128
 
131 129
   protected static class RNCWebViewClient extends WebViewClient {
132 130
 
@@ -227,6 +225,11 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
227 225
     protected @Nullable String injectedJS;
228 226
     protected boolean messagingEnabled = false;
229 227
     protected @Nullable RNCWebViewClient mRNCWebViewClient;
228
+    protected boolean sendContentSizeChangeEvents = false;
229
+    public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
230
+      this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
231
+    }
232
+
230 233
 
231 234
     protected class RNCWebViewBridge {
232 235
       RNCWebView mContext;
@@ -267,6 +270,20 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
267 270
       cleanupCallbacksAndDestroy();
268 271
     }
269 272
 
273
+    @Override
274
+    protected void onSizeChanged(int w, int h, int ow, int oh) {
275
+      if (sendContentSizeChangeEvents) {
276
+        dispatchEvent(
277
+          this,
278
+          new ContentSizeChangeEvent(
279
+            this.getId(),
280
+            w,
281
+            h
282
+          )
283
+        );
284
+      }
285
+    }
286
+
270 287
     @Override
271 288
     public void setWebViewClient(WebViewClient client) {
272 289
       super.setWebViewClient(client);
@@ -641,11 +658,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
641 658
 
642 659
   @ReactProp(name = "onContentSizeChange")
643 660
   public void setOnContentSizeChange(WebView view, boolean sendContentSizeChangeEvents) {
644
-    if (sendContentSizeChangeEvents) {
645
-      view.setPictureListener(getPictureListener());
646
-    } else {
647
-      view.setPictureListener(null);
648
-    }
661
+    ((RNCWebView) view).setSendContentSizeChangeEvents(sendContentSizeChangeEvents);
649 662
   }
650 663
 
651 664
   @ReactProp(name = "mixedContentMode")
@@ -770,23 +783,6 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
770 783
     ((RNCWebView) webView).cleanupCallbacksAndDestroy();
771 784
   }
772 785
 
773
-  protected WebView.PictureListener getPictureListener() {
774
-    if (mPictureListener == null) {
775
-      mPictureListener = new WebView.PictureListener() {
776
-        @Override
777
-        public void onNewPicture(WebView webView, Picture picture) {
778
-          dispatchEvent(
779
-            webView,
780
-            new ContentSizeChangeEvent(
781
-              webView.getId(),
782
-              webView.getWidth(),
783
-              webView.getContentHeight()));
784
-        }
785
-      };
786
-    }
787
-    return mPictureListener;
788
-  }
789
-
790 786
   protected static void dispatchEvent(WebView webView, Event event) {
791 787
     ReactContext reactContext = (ReactContext) webView.getContext();
792 788
     EventDispatcher eventDispatcher =