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

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

* [android] use pre-provided w/h values
This commit is contained in:
Michael Diarmid 2019-01-10 10:38:05 +00:00 committed by Thibault Malbranche
parent 028e3f8512
commit 9014c4cac0

View File

@ -23,7 +23,6 @@ import java.util.Map;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Picture;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Environment; import android.os.Environment;
@ -126,7 +125,6 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
protected static final String BLANK_URL = "about:blank"; protected static final String BLANK_URL = "about:blank";
protected WebViewConfig mWebViewConfig; protected WebViewConfig mWebViewConfig;
protected @Nullable WebView.PictureListener mPictureListener;
protected static class RNCWebViewClient extends WebViewClient { protected static class RNCWebViewClient extends WebViewClient {
@ -227,6 +225,11 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
protected @Nullable String injectedJS; protected @Nullable String injectedJS;
protected boolean messagingEnabled = false; protected boolean messagingEnabled = false;
protected @Nullable RNCWebViewClient mRNCWebViewClient; protected @Nullable RNCWebViewClient mRNCWebViewClient;
protected boolean sendContentSizeChangeEvents = false;
public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
}
protected class RNCWebViewBridge { protected class RNCWebViewBridge {
RNCWebView mContext; RNCWebView mContext;
@ -267,6 +270,20 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
cleanupCallbacksAndDestroy(); cleanupCallbacksAndDestroy();
} }
@Override
protected void onSizeChanged(int w, int h, int ow, int oh) {
if (sendContentSizeChangeEvents) {
dispatchEvent(
this,
new ContentSizeChangeEvent(
this.getId(),
w,
h
)
);
}
}
@Override @Override
public void setWebViewClient(WebViewClient client) { public void setWebViewClient(WebViewClient client) {
super.setWebViewClient(client); super.setWebViewClient(client);
@ -641,11 +658,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@ReactProp(name = "onContentSizeChange") @ReactProp(name = "onContentSizeChange")
public void setOnContentSizeChange(WebView view, boolean sendContentSizeChangeEvents) { public void setOnContentSizeChange(WebView view, boolean sendContentSizeChangeEvents) {
if (sendContentSizeChangeEvents) { ((RNCWebView) view).setSendContentSizeChangeEvents(sendContentSizeChangeEvents);
view.setPictureListener(getPictureListener());
} else {
view.setPictureListener(null);
}
} }
@ReactProp(name = "mixedContentMode") @ReactProp(name = "mixedContentMode")
@ -770,23 +783,6 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
((RNCWebView) webView).cleanupCallbacksAndDestroy(); ((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) { protected static void dispatchEvent(WebView webView, Event event) {
ReactContext reactContext = (ReactContext) webView.getContext(); ReactContext reactContext = (ReactContext) webView.getContext();
EventDispatcher eventDispatcher = EventDispatcher eventDispatcher =