Skip to content

Commit d55c77e

Browse files
committed
Add URL information about geolocation user info request
1 parent 64ffaa0 commit d55c77e

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ private void runDelayed(Runnable function, long delayMillis) {
232232

233233
@Override
234234
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
235-
236235
if (ContextCompat.checkSelfPermission(this.mWebView.getThemedReactContext(), Manifest.permission.ACCESS_FINE_LOCATION)
237236
!= PackageManager.PERMISSION_GRANTED) {
238237

@@ -245,23 +244,33 @@ public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermiss
245244
requestPermissions(Collections.singletonList(Manifest.permission.ACCESS_FINE_LOCATION));
246245

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

0 commit comments

Comments
 (0)