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
 import android.content.ActivityNotFoundException;
23
 import android.content.ActivityNotFoundException;
24
 import android.content.Intent;
24
 import android.content.Intent;
25
 import android.graphics.Bitmap;
25
 import android.graphics.Bitmap;
26
-import android.graphics.Picture;
27
 import android.net.Uri;
26
 import android.net.Uri;
28
 import android.os.Build;
27
 import android.os.Build;
29
 import android.os.Environment;
28
 import android.os.Environment;
126
   protected static final String BLANK_URL = "about:blank";
125
   protected static final String BLANK_URL = "about:blank";
127
 
126
 
128
   protected WebViewConfig mWebViewConfig;
127
   protected WebViewConfig mWebViewConfig;
129
-  protected @Nullable WebView.PictureListener mPictureListener;
130
 
128
 
131
   protected static class RNCWebViewClient extends WebViewClient {
129
   protected static class RNCWebViewClient extends WebViewClient {
132
 
130
 
227
     protected @Nullable String injectedJS;
225
     protected @Nullable String injectedJS;
228
     protected boolean messagingEnabled = false;
226
     protected boolean messagingEnabled = false;
229
     protected @Nullable RNCWebViewClient mRNCWebViewClient;
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
     protected class RNCWebViewBridge {
234
     protected class RNCWebViewBridge {
232
       RNCWebView mContext;
235
       RNCWebView mContext;
267
       cleanupCallbacksAndDestroy();
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
     @Override
287
     @Override
271
     public void setWebViewClient(WebViewClient client) {
288
     public void setWebViewClient(WebViewClient client) {
272
       super.setWebViewClient(client);
289
       super.setWebViewClient(client);
641
 
658
 
642
   @ReactProp(name = "onContentSizeChange")
659
   @ReactProp(name = "onContentSizeChange")
643
   public void setOnContentSizeChange(WebView view, boolean sendContentSizeChangeEvents) {
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
   @ReactProp(name = "mixedContentMode")
664
   @ReactProp(name = "mixedContentMode")
770
     ((RNCWebView) webView).cleanupCallbacksAndDestroy();
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
   protected static void dispatchEvent(WebView webView, Event event) {
786
   protected static void dispatchEvent(WebView webView, Event event) {
791
     ReactContext reactContext = (ReactContext) webView.getContext();
787
     ReactContext reactContext = (ReactContext) webView.getContext();
792
     EventDispatcher eventDispatcher =
788
     EventDispatcher eventDispatcher =