fix(android): Do not hold a strong reference to native module in the package (#434)

This commit is contained in:
Stanisław Chmiela 2019-03-20 14:04:47 +01:00 committed by Thibault Malbranche
parent 15ae93428f
commit 566a6292b4
3 changed files with 9 additions and 45 deletions

View File

@ -105,7 +105,6 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
// state and release page resources (including any running JavaScript).
protected static final String BLANK_URL = "about:blank";
protected WebViewConfig mWebViewConfig;
private RNCWebViewPackage aPackage;
public RNCWebViewManager() {
mWebViewConfig = new WebViewConfig() {
@ -171,15 +170,15 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
}
protected void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType) {
getModule().startPhotoPickerIntent(filePathCallback, acceptType);
getModule(reactContext).startPhotoPickerIntent(filePathCallback, acceptType);
}
protected void openFileChooser(ValueCallback<Uri> filePathCallback) {
getModule().startPhotoPickerIntent(filePathCallback, "");
getModule(reactContext).startPhotoPickerIntent(filePathCallback, "");
}
protected void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) {
getModule().startPhotoPickerIntent(filePathCallback, acceptType);
getModule(reactContext).startPhotoPickerIntent(filePathCallback, acceptType);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@ -188,7 +187,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
String[] acceptTypes = fileChooserParams.getAcceptTypes();
boolean allowMultiple = fileChooserParams.getMode() == WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE;
Intent intent = fileChooserParams.createIntent();
return getModule().startPhotoPickerIntent(filePathCallback, intent, acceptTypes, allowMultiple);
return getModule(reactContext).startPhotoPickerIntent(filePathCallback, intent, acceptTypes, allowMultiple);
}
});
reactContext.addLifecycleEventListener(webView);
@ -218,7 +217,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
RNCWebViewModule module = getModule();
RNCWebViewModule module = getModule(reactContext);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
@ -552,16 +551,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
((RNCWebView) webView).cleanupCallbacksAndDestroy();
}
public RNCWebViewPackage getPackage() {
return this.aPackage;
}
public void setPackage(RNCWebViewPackage aPackage) {
this.aPackage = aPackage;
}
public RNCWebViewModule getModule() {
return this.aPackage.getModule();
public RNCWebViewModule getModule(ReactContext reactContext) {
return reactContext.getNativeModule(RNCWebViewModule.class);
}
protected static class RNCWebViewClient extends WebViewClient {

View File

@ -40,8 +40,6 @@ public class RNCWebViewModule extends ReactContextBaseJavaModule implements Acti
private static final int PICKER_LEGACY = 3;
private static final int FILE_DOWNLOAD_PERMISSION_REQUEST = 1;
final String DEFAULT_MIME_TYPES = "*/*";
private final ReactApplicationContext reactContext;
private RNCWebViewPackage aPackage;
private ValueCallback<Uri> filePathCallbackLegacy;
private ValueCallback<Uri[]> filePathCallback;
private Uri outputFileUri;
@ -68,7 +66,6 @@ public class RNCWebViewModule extends ReactContextBaseJavaModule implements Acti
public RNCWebViewModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
reactContext.addActivityEventListener(this);
}
@ -234,14 +231,6 @@ public class RNCWebViewModule extends ReactContextBaseJavaModule implements Acti
return result;
}
public RNCWebViewPackage getPackage() {
return this.aPackage;
}
public void setPackage(RNCWebViewPackage aPackage) {
this.aPackage = aPackage;
}
private Intent getPhotoIntent() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
outputFileUri = getOutputUri(MediaStore.ACTION_IMAGE_CAPTURE);

View File

@ -6,23 +6,13 @@ import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class RNCWebViewPackage implements ReactPackage {
private RNCWebViewManager manager;
private RNCWebViewModule module;
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modulesList = new ArrayList<>();
module = new RNCWebViewModule(reactContext);
module.setPackage(this);
modulesList.add(module);
return modulesList;
return Collections.singletonList(new RNCWebViewModule(reactContext));
}
// Deprecated from RN 0.47
@ -32,12 +22,6 @@ public class RNCWebViewPackage implements ReactPackage {
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
manager = new RNCWebViewManager();
manager.setPackage(this);
return Arrays.<ViewManager>asList(manager);
}
public RNCWebViewModule getModule() {
return module;
return Collections.singletonList(new RNCWebViewManager());
}
}