Skip to content

Commit

Permalink
fix(android): replace deprecated WebView.PictureListener->`onConten…
Browse files Browse the repository at this point in the history
…tSizeChange` impl (#164)

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

* [android] use pre-provided w/h values
  • Loading branch information
Salakar authored and Titozzz committed Jan 10, 2019
1 parent 028e3f8 commit 9014c4c
Showing 1 changed file with 20 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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;
Expand Down Expand Up @@ -126,7 +125,6 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
protected static final String BLANK_URL = "about:blank";

protected WebViewConfig mWebViewConfig;
protected @Nullable WebView.PictureListener mPictureListener;

protected static class RNCWebViewClient extends WebViewClient {

Expand Down Expand Up @@ -227,6 +225,11 @@ protected static class RNCWebView extends WebView implements LifecycleEventListe
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;
Expand Down Expand Up @@ -267,6 +270,20 @@ public void onHostDestroy() {
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);
Expand Down Expand Up @@ -641,11 +658,7 @@ public void setSource(WebView view, @Nullable ReadableMap source) {

@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")
Expand Down Expand Up @@ -770,23 +783,6 @@ public void onDropViewInstance(WebView webView) {
((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 =
Expand Down

0 comments on commit 9014c4c

Please sign in to comment.