Преглед изворни кода

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

Stanisław Chmiela пре 5 година
родитељ
комит
566a6292b4

+ 7
- 16
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java Прегледај датотеку

105
   // state and release page resources (including any running JavaScript).
105
   // state and release page resources (including any running JavaScript).
106
   protected static final String BLANK_URL = "about:blank";
106
   protected static final String BLANK_URL = "about:blank";
107
   protected WebViewConfig mWebViewConfig;
107
   protected WebViewConfig mWebViewConfig;
108
-  private RNCWebViewPackage aPackage;
109
 
108
 
110
   public RNCWebViewManager() {
109
   public RNCWebViewManager() {
111
     mWebViewConfig = new WebViewConfig() {
110
     mWebViewConfig = new WebViewConfig() {
171
       }
170
       }
172
 
171
 
173
       protected void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType) {
172
       protected void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType) {
174
-        getModule().startPhotoPickerIntent(filePathCallback, acceptType);
173
+        getModule(reactContext).startPhotoPickerIntent(filePathCallback, acceptType);
175
       }
174
       }
176
 
175
 
177
       protected void openFileChooser(ValueCallback<Uri> filePathCallback) {
176
       protected void openFileChooser(ValueCallback<Uri> filePathCallback) {
178
-        getModule().startPhotoPickerIntent(filePathCallback, "");
177
+        getModule(reactContext).startPhotoPickerIntent(filePathCallback, "");
179
       }
178
       }
180
 
179
 
181
       protected void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) {
180
       protected void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) {
182
-        getModule().startPhotoPickerIntent(filePathCallback, acceptType);
181
+        getModule(reactContext).startPhotoPickerIntent(filePathCallback, acceptType);
183
       }
182
       }
184
 
183
 
185
       @TargetApi(Build.VERSION_CODES.LOLLIPOP)
184
       @TargetApi(Build.VERSION_CODES.LOLLIPOP)
188
         String[] acceptTypes = fileChooserParams.getAcceptTypes();
187
         String[] acceptTypes = fileChooserParams.getAcceptTypes();
189
         boolean allowMultiple = fileChooserParams.getMode() == WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE;
188
         boolean allowMultiple = fileChooserParams.getMode() == WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE;
190
         Intent intent = fileChooserParams.createIntent();
189
         Intent intent = fileChooserParams.createIntent();
191
-        return getModule().startPhotoPickerIntent(filePathCallback, intent, acceptTypes, allowMultiple);
190
+        return getModule(reactContext).startPhotoPickerIntent(filePathCallback, intent, acceptTypes, allowMultiple);
192
       }
191
       }
193
     });
192
     });
194
     reactContext.addLifecycleEventListener(webView);
193
     reactContext.addLifecycleEventListener(webView);
218
 
217
 
219
     webView.setDownloadListener(new DownloadListener() {
218
     webView.setDownloadListener(new DownloadListener() {
220
       public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
219
       public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
221
-        RNCWebViewModule module = getModule();
220
+        RNCWebViewModule module = getModule(reactContext);
222
 
221
 
223
         DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
222
         DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
224
 
223
 
552
     ((RNCWebView) webView).cleanupCallbacksAndDestroy();
551
     ((RNCWebView) webView).cleanupCallbacksAndDestroy();
553
   }
552
   }
554
 
553
 
555
-  public RNCWebViewPackage getPackage() {
556
-    return this.aPackage;
557
-  }
558
-
559
-  public void setPackage(RNCWebViewPackage aPackage) {
560
-    this.aPackage = aPackage;
561
-  }
562
-
563
-  public RNCWebViewModule getModule() {
564
-    return this.aPackage.getModule();
554
+  public RNCWebViewModule getModule(ReactContext reactContext) {
555
+    return reactContext.getNativeModule(RNCWebViewModule.class);
565
   }
556
   }
566
 
557
 
567
   protected static class RNCWebViewClient extends WebViewClient {
558
   protected static class RNCWebViewClient extends WebViewClient {

+ 0
- 11
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewModule.java Прегледај датотеку

40
   private static final int PICKER_LEGACY = 3;
40
   private static final int PICKER_LEGACY = 3;
41
   private static final int FILE_DOWNLOAD_PERMISSION_REQUEST = 1;
41
   private static final int FILE_DOWNLOAD_PERMISSION_REQUEST = 1;
42
   final String DEFAULT_MIME_TYPES = "*/*";
42
   final String DEFAULT_MIME_TYPES = "*/*";
43
-  private final ReactApplicationContext reactContext;
44
-  private RNCWebViewPackage aPackage;
45
   private ValueCallback<Uri> filePathCallbackLegacy;
43
   private ValueCallback<Uri> filePathCallbackLegacy;
46
   private ValueCallback<Uri[]> filePathCallback;
44
   private ValueCallback<Uri[]> filePathCallback;
47
   private Uri outputFileUri;
45
   private Uri outputFileUri;
68
 
66
 
69
   public RNCWebViewModule(ReactApplicationContext reactContext) {
67
   public RNCWebViewModule(ReactApplicationContext reactContext) {
70
     super(reactContext);
68
     super(reactContext);
71
-    this.reactContext = reactContext;
72
     reactContext.addActivityEventListener(this);
69
     reactContext.addActivityEventListener(this);
73
   }
70
   }
74
 
71
 
234
     return result;
231
     return result;
235
   }
232
   }
236
 
233
 
237
-  public RNCWebViewPackage getPackage() {
238
-    return this.aPackage;
239
-  }
240
-
241
-  public void setPackage(RNCWebViewPackage aPackage) {
242
-    this.aPackage = aPackage;
243
-  }
244
-
245
   private Intent getPhotoIntent() {
234
   private Intent getPhotoIntent() {
246
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
235
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
247
     outputFileUri = getOutputUri(MediaStore.ACTION_IMAGE_CAPTURE);
236
     outputFileUri = getOutputUri(MediaStore.ACTION_IMAGE_CAPTURE);

+ 2
- 18
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewPackage.java Прегледај датотеку

6
 import com.facebook.react.bridge.ReactApplicationContext;
6
 import com.facebook.react.bridge.ReactApplicationContext;
7
 import com.facebook.react.uimanager.ViewManager;
7
 import com.facebook.react.uimanager.ViewManager;
8
 
8
 
9
-import java.util.ArrayList;
10
-import java.util.Arrays;
11
 import java.util.Collections;
9
 import java.util.Collections;
12
 import java.util.List;
10
 import java.util.List;
13
 
11
 
14
 public class RNCWebViewPackage implements ReactPackage {
12
 public class RNCWebViewPackage implements ReactPackage {
15
-
16
-  private RNCWebViewManager manager;
17
-  private RNCWebViewModule module;
18
-
19
   @Override
13
   @Override
20
   public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
14
   public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
21
-    List<NativeModule> modulesList = new ArrayList<>();
22
-    module = new RNCWebViewModule(reactContext);
23
-    module.setPackage(this);
24
-    modulesList.add(module);
25
-    return modulesList;
15
+    return Collections.singletonList(new RNCWebViewModule(reactContext));
26
   }
16
   }
27
 
17
 
28
   // Deprecated from RN 0.47
18
   // Deprecated from RN 0.47
32
 
22
 
33
   @Override
23
   @Override
34
   public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
24
   public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
35
-    manager = new RNCWebViewManager();
36
-    manager.setPackage(this);
37
-    return Arrays.<ViewManager>asList(manager);
38
-  }
39
-
40
-  public RNCWebViewModule getModule() {
41
-    return module;
25
+    return Collections.singletonList(new RNCWebViewManager());
42
   }
26
   }
43
 }
27
 }