Skip to content

Commit d4bf3b0

Browse files
committed
Adjust try catch block for URI parsing
1 parent 7e306a4 commit d4bf3b0

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

android/src/main/java/com/reactnativecommunity/webview/RNCWebChromeClient.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -246,33 +246,32 @@ public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermiss
246246

247247
} else {
248248
String uri = "";
249-
URI parsedUri;
250249
try {
251250
// get current URL for webview
252251
uri = mWebView.getUrl();
253252
// get parsed URL
254-
parsedUri = new URI(uri);
253+
URI parsedUri = new URI(uri);
254+
// create URL string of origin and path
255+
String formattedUrl = parsedUri.getHost() + parsedUri.getPath();
256+
String alertMessage = String.format("Allow this %s to use your location?", formattedUrl);
257+
AlertDialog.Builder builder = new AlertDialog.Builder(this.mWebView.getContext());
258+
builder.setMessage(alertMessage);
259+
builder.setCancelable(false);
260+
builder.setPositiveButton("Allow", (dialog, which) -> {
261+
callback.invoke(origin, true, false);
262+
});
263+
builder.setNegativeButton("Don't allow", (dialog, which) -> {
264+
callback.invoke(origin, false, false);
265+
});
266+
AlertDialog alertDialog = builder.create();
267+
alertDialog.show();
268+
//Delay making `allow` clickable for 500ms to avoid unwanted presses.
269+
Button posButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
270+
posButton.setEnabled(false);
271+
this.runDelayed(() -> posButton.setEnabled(true), 500);
255272
} catch (Exception e) {
256273
System.out.println("URI " + uri + " is a malformed URL");
257274
}
258-
// create URL string of origin and path
259-
String formattedUrl = parsedUri.getHost() + parsedUri.getPath();
260-
String alertMessage = String.format("Allow %s to use your location?", formattedUrl);
261-
AlertDialog.Builder builder = new AlertDialog.Builder(this.mWebView.getContext());
262-
builder.setMessage(alertMessage);
263-
builder.setCancelable(false);
264-
builder.setPositiveButton("Allow", (dialog, which) -> {
265-
callback.invoke(origin, true, false);
266-
});
267-
builder.setNegativeButton("Don't allow", (dialog, which) -> {
268-
callback.invoke(origin, false, false);
269-
});
270-
AlertDialog alertDialog = builder.create();
271-
alertDialog.show();
272-
//Delay making `allow` clickable for 500ms to avoid unwanted presses.
273-
Button posButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
274-
posButton.setEnabled(false);
275-
this.runDelayed(() -> posButton.setEnabled(true), 500);
276275
}
277276
}
278277

0 commit comments

Comments
 (0)