Skip to content

show prompt before file download, mic and camera access #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
Expand Down Expand Up @@ -150,11 +152,14 @@ public void onPermissionRequest(final PermissionRequest request) {
ArrayList<String> requestedAndroidPermissions = new ArrayList<>();
for (String requestedResource : request.getResources()) {
String androidPermission = null;
String requestPermissionIdentifier = null;

if (requestedResource.equals(PermissionRequest.RESOURCE_AUDIO_CAPTURE)) {
androidPermission = Manifest.permission.RECORD_AUDIO;
requestPermissionIdentifier = "microphone";
} else if (requestedResource.equals(PermissionRequest.RESOURCE_VIDEO_CAPTURE)) {
androidPermission = Manifest.permission.CAMERA;
requestPermissionIdentifier = "camera";
} else if(requestedResource.equals(PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID)) {
if (mAllowsProtectedMedia) {
grantedPermissions.add(requestedResource);
Expand All @@ -169,9 +174,23 @@ public void onPermissionRequest(final PermissionRequest request) {
androidPermission = PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID;
} }
// TODO: RESOURCE_MIDI_SYSEX, RESOURCE_PROTECTED_MEDIA_ID.
String alertMessage = String.format("Allow this app to use your " + requestPermissionIdentifier + "?");
if (androidPermission != null) {
if (ContextCompat.checkSelfPermission(this.mWebView.getThemedReactContext(), androidPermission) == PackageManager.PERMISSION_GRANTED) {
grantedPermissions.add(requestedResource);
AlertDialog.Builder builder = new AlertDialog.Builder(this.mWebView.getContext());
builder.setMessage(alertMessage);
builder.setCancelable(false);
String finalAndroidPermission = androidPermission;
builder.setPositiveButton("Allow", (dialog, which) -> {
permissionRequest = request;
grantedPermissions.add(finalAndroidPermission);
requestPermissions(grantedPermissions);
});
builder.setNegativeButton("Don't allow", (dialog, which) -> {
request.deny();
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
} else {
requestedAndroidPermissions.add(androidPermission);
}
Expand All @@ -180,8 +199,10 @@ public void onPermissionRequest(final PermissionRequest request) {

// If all the permissions are already granted, send the response to the WebView synchronously
if (requestedAndroidPermissions.isEmpty()) {
request.grant(grantedPermissions.toArray(new String[0]));
grantedPermissions = null;
if (!grantedPermissions.isEmpty()) {
request.grant(grantedPermissions.toArray(new String[0]));
grantedPermissions = null;
}
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.reactnativecommunity.webview

import android.app.AlertDialog
import android.app.DownloadManager
import android.content.DialogInterface
import android.content.pm.ActivityInfo
import android.graphics.Bitmap
import android.graphics.Color
Expand Down Expand Up @@ -106,34 +108,42 @@ class RNCWebViewManagerImpl {

val downloadMessage = "Downloading $fileName"

//Attempt to add cookie, if it exists
var urlObj: URL? = null
try {
urlObj = URL(url)
val baseUrl = urlObj.protocol + "://" + urlObj.host
val cookie = CookieManager.getInstance().getCookie(baseUrl)
request.addRequestHeader("Cookie", cookie)
} catch (e: MalformedURLException) {
Log.w(TAG, "Error getting cookie for DownloadManager", e)
}
val builder = AlertDialog.Builder(webView.context)
builder.setMessage("Do you want to download \n$fileName?")
builder.setCancelable(false)
builder.setPositiveButton("Download") { _, _ ->
//Attempt to add cookie, if it exists
var urlObj: URL? = null
try {
urlObj = URL(url)
val baseUrl = urlObj.protocol + "://" + urlObj.host
val cookie = CookieManager.getInstance().getCookie(baseUrl)
request.addRequestHeader("Cookie", cookie)
} catch (e: MalformedURLException) {
Log.w(TAG, "Error getting cookie for DownloadManager", e)
}

//Finish setting up request
request.addRequestHeader("User-Agent", userAgent)
request.setTitle(fileName)
request.setDescription(downloadMessage)
request.allowScanningByMediaScanner()
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
module.setDownloadRequest(request)
if (module.grantFileDownloaderPermissions(
getDownloadingMessageOrDefault(),
getLackPermissionToDownloadMessageOrDefault()
)
) {
module.downloadFile(
getDownloadingMessageOrDefault()
)
//Finish setting up request
request.addRequestHeader("User-Agent", userAgent)
request.setTitle(fileName)
request.setDescription(downloadMessage)
request.allowScanningByMediaScanner()
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
module.setDownloadRequest(request)
if (module.grantFileDownloaderPermissions(
getDownloadingMessageOrDefault(),
getLackPermissionToDownloadMessageOrDefault()
)
) {
module.downloadFile(
getDownloadingMessageOrDefault()
)
}
}
builder.setNegativeButton("Cancel") { _: DialogInterface?, _: Int -> }
val alertDialog = builder.create()
alertDialog.show()
})
return RNCWebViewWrapper(context, webView)
}
Expand Down