From 9014c4cac0d1caccea94b02d5776bef476272456 Mon Sep 17 00:00:00 2001 From: Michael Diarmid Date: Thu, 10 Jan 2019 10:38:05 +0000 Subject: [PATCH] fix(android): replace deprecated `WebView.PictureListener`->`onContentSizeChange` impl (#164) * [android] replace deprecated `WebView.PictureListener` onSizeChanged implementation * [android] use pre-provided w/h values --- .../webview/RNCWebViewManager.java | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java index 818751f..1668ea3 100644 --- a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java +++ b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java @@ -23,7 +23,6 @@ import java.util.Map; import android.content.ActivityNotFoundException; import android.content.Intent; import android.graphics.Bitmap; -import android.graphics.Picture; import android.net.Uri; import android.os.Build; import android.os.Environment; @@ -126,7 +125,6 @@ public class RNCWebViewManager extends SimpleViewManager { protected static final String BLANK_URL = "about:blank"; protected WebViewConfig mWebViewConfig; - protected @Nullable WebView.PictureListener mPictureListener; protected static class RNCWebViewClient extends WebViewClient { @@ -227,6 +225,11 @@ public class RNCWebViewManager extends SimpleViewManager { protected @Nullable String injectedJS; protected boolean messagingEnabled = false; protected @Nullable RNCWebViewClient mRNCWebViewClient; + protected boolean sendContentSizeChangeEvents = false; + public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) { + this.sendContentSizeChangeEvents = sendContentSizeChangeEvents; + } + protected class RNCWebViewBridge { RNCWebView mContext; @@ -267,6 +270,20 @@ public class RNCWebViewManager extends SimpleViewManager { cleanupCallbacksAndDestroy(); } + @Override + protected void onSizeChanged(int w, int h, int ow, int oh) { + if (sendContentSizeChangeEvents) { + dispatchEvent( + this, + new ContentSizeChangeEvent( + this.getId(), + w, + h + ) + ); + } + } + @Override public void setWebViewClient(WebViewClient client) { super.setWebViewClient(client); @@ -641,11 +658,7 @@ public class RNCWebViewManager extends SimpleViewManager { @ReactProp(name = "onContentSizeChange") public void setOnContentSizeChange(WebView view, boolean sendContentSizeChangeEvents) { - if (sendContentSizeChangeEvents) { - view.setPictureListener(getPictureListener()); - } else { - view.setPictureListener(null); - } + ((RNCWebView) view).setSendContentSizeChangeEvents(sendContentSizeChangeEvents); } @ReactProp(name = "mixedContentMode") @@ -770,23 +783,6 @@ public class RNCWebViewManager extends SimpleViewManager { ((RNCWebView) webView).cleanupCallbacksAndDestroy(); } - protected WebView.PictureListener getPictureListener() { - if (mPictureListener == null) { - mPictureListener = new WebView.PictureListener() { - @Override - public void onNewPicture(WebView webView, Picture picture) { - dispatchEvent( - webView, - new ContentSizeChangeEvent( - webView.getId(), - webView.getWidth(), - webView.getContentHeight())); - } - }; - } - return mPictureListener; - } - protected static void dispatchEvent(WebView webView, Event event) { ReactContext reactContext = (ReactContext) webView.getContext(); EventDispatcher eventDispatcher =