Browse Source

fix(android): Handle IllegalArgumentException when Webview starts to download something but http/https URIs

aliaksei.dzeiko@pandadoc.com 4 years ago
parent
commit
a1b4e0290f

+ 33
- 28
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

@@ -76,6 +76,7 @@ import java.util.ArrayList;
76 76
 import java.util.HashMap;
77 77
 import java.util.Locale;
78 78
 import java.util.Map;
79
+import java.lang.IllegalArgumentException;
79 80
 
80 81
 import javax.annotation.Nullable;
81 82
 
@@ -196,36 +197,40 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
196 197
       public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
197 198
         RNCWebViewModule module = getModule(reactContext);
198 199
 
199
-        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
200
-
201
-        String fileName = URLUtil.guessFileName(url, contentDisposition, mimetype);
202
-        String downloadMessage = "Downloading " + fileName;
203
-
204
-        //Attempt to add cookie, if it exists
205
-        URL urlObj = null;
206 200
         try {
207
-          urlObj = new URL(url);
208
-          String baseUrl = urlObj.getProtocol() + "://" + urlObj.getHost();
209
-          String cookie = CookieManager.getInstance().getCookie(baseUrl);
210
-          request.addRequestHeader("Cookie", cookie);
211
-          System.out.println("Got cookie for DownloadManager: " + cookie);
212
-        } catch (MalformedURLException e) {
213
-          System.out.println("Error getting cookie for DownloadManager: " + e.toString());
214
-          e.printStackTrace();
215
-        }
201
+          DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
202
+
203
+          String fileName = URLUtil.guessFileName(url, contentDisposition, mimetype);
204
+          String downloadMessage = "Downloading " + fileName;
205
+
206
+          //Attempt to add cookie, if it exists
207
+          URL urlObj = null;
208
+          try {
209
+            urlObj = new URL(url);
210
+            String baseUrl = urlObj.getProtocol() + "://" + urlObj.getHost();
211
+            String cookie = CookieManager.getInstance().getCookie(baseUrl);
212
+            request.addRequestHeader("Cookie", cookie);
213
+            System.out.println("Got cookie for DownloadManager: " + cookie);
214
+          } catch (MalformedURLException e) {
215
+            System.out.println("Error getting cookie for DownloadManager: " + e.toString());
216
+            e.printStackTrace();
217
+          }
216 218
 
217
-        //Finish setting up request
218
-        request.addRequestHeader("User-Agent", userAgent);
219
-        request.setTitle(fileName);
220
-        request.setDescription(downloadMessage);
221
-        request.allowScanningByMediaScanner();
222
-        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
223
-        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
219
+          //Finish setting up request
220
+          request.addRequestHeader("User-Agent", userAgent);
221
+          request.setTitle(fileName);
222
+          request.setDescription(downloadMessage);
223
+          request.allowScanningByMediaScanner();
224
+          request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
225
+          request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
224 226
 
225
-        module.setDownloadRequest(request);
227
+          module.setDownloadRequest(request);
226 228
 
227
-        if (module.grantFileDownloaderPermissions()) {
228
-          module.downloadFile();
229
+          if (module.grantFileDownloaderPermissions()) {
230
+            module.downloadFile();
231
+          }
232
+        } catch (IllegalArgumentException e) {
233
+          System.out.println("IllegalArgumentException: " + e.toString());
229 234
         }
230 235
       }
231 236
     });
@@ -396,7 +401,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
396 401
   public void setMessagingEnabled(WebView view, boolean enabled) {
397 402
     ((RNCWebView) view).setMessagingEnabled(enabled);
398 403
   }
399
-   
404
+
400 405
   @ReactProp(name = "incognito")
401 406
   public void setIncognito(WebView view, boolean enabled) {
402 407
     // Remove all previous cookies
@@ -646,7 +651,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
646 651
         public Bitmap getDefaultVideoPoster() {
647 652
           return Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
648 653
         }
649
-        
654
+
650 655
         @Override
651 656
         public void onShowCustomView(View view, CustomViewCallback callback) {
652 657
           if (mVideoView != null) {