Browse Source

Add try-catch for DownloadManager.Request

Prevents a crash when attempting to download a non HTTP/HTTPS link (like a url beginning with `blob://`)
Jason Gaare 4 years ago
parent
commit
8679057f00
No account linked to committer's email address

+ 31
- 26
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

@@ -9,6 +9,7 @@ import android.content.pm.ActivityInfo;
9 9
 import android.content.pm.PackageManager;
10 10
 import android.graphics.Bitmap;
11 11
 import android.graphics.Color;
12
+import java.lang.IllegalArgumentException;
12 13
 import android.Manifest;
13 14
 import android.net.http.SslError;
14 15
 import android.net.Uri;
@@ -202,36 +203,40 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
202 203
 
203 204
         RNCWebViewModule module = getModule(reactContext);
204 205
 
205
-        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
206
-
207
-        String fileName = URLUtil.guessFileName(url, contentDisposition, mimetype);
208
-        String downloadMessage = "Downloading " + fileName;
209
-
210
-        //Attempt to add cookie, if it exists
211
-        URL urlObj = null;
212 206
         try {
213
-          urlObj = new URL(url);
214
-          String baseUrl = urlObj.getProtocol() + "://" + urlObj.getHost();
215
-          String cookie = CookieManager.getInstance().getCookie(baseUrl);
216
-          request.addRequestHeader("Cookie", cookie);
217
-        } catch (MalformedURLException e) {
218
-          System.out.println("Error getting cookie for DownloadManager: " + e.toString());
219
-          e.printStackTrace();
220
-        }
207
+          DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
208
+
209
+          String fileName = URLUtil.guessFileName(url, contentDisposition, mimetype);
210
+          String downloadMessage = "Downloading " + fileName;
211
+
212
+          //Attempt to add cookie, if it exists
213
+          URL urlObj = null;
214
+          try {
215
+            urlObj = new URL(url);
216
+            String baseUrl = urlObj.getProtocol() + "://" + urlObj.getHost();
217
+            String cookie = CookieManager.getInstance().getCookie(baseUrl);
218
+            request.addRequestHeader("Cookie", cookie);
219
+          } catch (MalformedURLException e) {
220
+            System.out.println("Error getting cookie for DownloadManager: " + e.toString());
221
+            e.printStackTrace();
222
+          }
221 223
 
222
-        //Finish setting up request
223
-        request.addRequestHeader("User-Agent", userAgent);
224
-        request.setTitle(fileName);
225
-        request.setDescription(downloadMessage);
226
-        request.allowScanningByMediaScanner();
227
-        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
228
-        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
224
+          //Finish setting up request
225
+          request.addRequestHeader("User-Agent", userAgent);
226
+          request.setTitle(fileName);
227
+          request.setDescription(downloadMessage);
228
+          request.allowScanningByMediaScanner();
229
+          request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
230
+          request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
229 231
 
230
-        module.setDownloadRequest(request);
232
+          module.setDownloadRequest(request);
231 233
 
232
-        if (module.grantFileDownloaderPermissions()) {
233
-          module.downloadFile();
234
-        }
234
+          if (module.grantFileDownloaderPermissions()) {
235
+            module.downloadFile();
236
+          }
237
+        } catch (IllegalArgumentException e) {
238
+          System.out.println("IllegalArgumentException: " + e.toString());
239
+        }		         
235 240
       }
236 241
     });
237 242