Skip to content

Commit 1629a52

Browse files
authored
show prompt before file download, mic and camera access (#8)
* show prompt before download starts * prompt user before allowing mic and camera access
1 parent a233350 commit 1629a52

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import android.Manifest;
44
import android.annotation.TargetApi;
55
import android.app.Activity;
6+
import android.app.AlertDialog;
7+
import android.content.DialogInterface;
68
import android.content.pm.PackageManager;
79
import android.net.Uri;
810
import android.os.Build;
@@ -150,11 +152,14 @@ public void onPermissionRequest(final PermissionRequest request) {
150152
ArrayList<String> requestedAndroidPermissions = new ArrayList<>();
151153
for (String requestedResource : request.getResources()) {
152154
String androidPermission = null;
155+
String requestPermissionIdentifier = null;
153156

154157
if (requestedResource.equals(PermissionRequest.RESOURCE_AUDIO_CAPTURE)) {
155158
androidPermission = Manifest.permission.RECORD_AUDIO;
159+
requestPermissionIdentifier = "microphone";
156160
} else if (requestedResource.equals(PermissionRequest.RESOURCE_VIDEO_CAPTURE)) {
157161
androidPermission = Manifest.permission.CAMERA;
162+
requestPermissionIdentifier = "camera";
158163
} else if(requestedResource.equals(PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID)) {
159164
if (mAllowsProtectedMedia) {
160165
grantedPermissions.add(requestedResource);
@@ -169,9 +174,23 @@ public void onPermissionRequest(final PermissionRequest request) {
169174
androidPermission = PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID;
170175
} }
171176
// TODO: RESOURCE_MIDI_SYSEX, RESOURCE_PROTECTED_MEDIA_ID.
177+
String alertMessage = String.format("Allow this app to use your " + requestPermissionIdentifier + "?");
172178
if (androidPermission != null) {
173179
if (ContextCompat.checkSelfPermission(this.mWebView.getThemedReactContext(), androidPermission) == PackageManager.PERMISSION_GRANTED) {
174-
grantedPermissions.add(requestedResource);
180+
AlertDialog.Builder builder = new AlertDialog.Builder(this.mWebView.getContext());
181+
builder.setMessage(alertMessage);
182+
builder.setCancelable(false);
183+
String finalAndroidPermission = androidPermission;
184+
builder.setPositiveButton("Allow", (dialog, which) -> {
185+
permissionRequest = request;
186+
grantedPermissions.add(finalAndroidPermission);
187+
requestPermissions(grantedPermissions);
188+
});
189+
builder.setNegativeButton("Don't allow", (dialog, which) -> {
190+
request.deny();
191+
});
192+
AlertDialog alertDialog = builder.create();
193+
alertDialog.show();
175194
} else {
176195
requestedAndroidPermissions.add(androidPermission);
177196
}
@@ -180,8 +199,10 @@ public void onPermissionRequest(final PermissionRequest request) {
180199

181200
// If all the permissions are already granted, send the response to the WebView synchronously
182201
if (requestedAndroidPermissions.isEmpty()) {
183-
request.grant(grantedPermissions.toArray(new String[0]));
184-
grantedPermissions = null;
202+
if (!grantedPermissions.isEmpty()) {
203+
request.grant(grantedPermissions.toArray(new String[0]));
204+
grantedPermissions = null;
205+
}
185206
return;
186207
}
187208

android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.reactnativecommunity.webview
22

3+
import android.app.AlertDialog
34
import android.app.DownloadManager
5+
import android.content.DialogInterface
46
import android.content.pm.ActivityInfo
57
import android.graphics.Bitmap
68
import android.graphics.Color
@@ -106,34 +108,42 @@ class RNCWebViewManagerImpl {
106108

107109
val downloadMessage = "Downloading $fileName"
108110

109-
//Attempt to add cookie, if it exists
110-
var urlObj: URL? = null
111-
try {
112-
urlObj = URL(url)
113-
val baseUrl = urlObj.protocol + "://" + urlObj.host
114-
val cookie = CookieManager.getInstance().getCookie(baseUrl)
115-
request.addRequestHeader("Cookie", cookie)
116-
} catch (e: MalformedURLException) {
117-
Log.w(TAG, "Error getting cookie for DownloadManager", e)
118-
}
111+
val builder = AlertDialog.Builder(webView.context)
112+
builder.setMessage("Do you want to download \n$fileName?")
113+
builder.setCancelable(false)
114+
builder.setPositiveButton("Download") { _, _ ->
115+
//Attempt to add cookie, if it exists
116+
var urlObj: URL? = null
117+
try {
118+
urlObj = URL(url)
119+
val baseUrl = urlObj.protocol + "://" + urlObj.host
120+
val cookie = CookieManager.getInstance().getCookie(baseUrl)
121+
request.addRequestHeader("Cookie", cookie)
122+
} catch (e: MalformedURLException) {
123+
Log.w(TAG, "Error getting cookie for DownloadManager", e)
124+
}
119125

120-
//Finish setting up request
121-
request.addRequestHeader("User-Agent", userAgent)
122-
request.setTitle(fileName)
123-
request.setDescription(downloadMessage)
124-
request.allowScanningByMediaScanner()
125-
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
126-
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
127-
module.setDownloadRequest(request)
128-
if (module.grantFileDownloaderPermissions(
129-
getDownloadingMessageOrDefault(),
130-
getLackPermissionToDownloadMessageOrDefault()
131-
)
132-
) {
133-
module.downloadFile(
134-
getDownloadingMessageOrDefault()
135-
)
126+
//Finish setting up request
127+
request.addRequestHeader("User-Agent", userAgent)
128+
request.setTitle(fileName)
129+
request.setDescription(downloadMessage)
130+
request.allowScanningByMediaScanner()
131+
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
132+
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
133+
module.setDownloadRequest(request)
134+
if (module.grantFileDownloaderPermissions(
135+
getDownloadingMessageOrDefault(),
136+
getLackPermissionToDownloadMessageOrDefault()
137+
)
138+
) {
139+
module.downloadFile(
140+
getDownloadingMessageOrDefault()
141+
)
142+
}
136143
}
144+
builder.setNegativeButton("Cancel") { _: DialogInterface?, _: Int -> }
145+
val alertDialog = builder.create()
146+
alertDialog.show()
137147
})
138148
return RNCWebViewWrapper(context, webView)
139149
}

0 commit comments

Comments
 (0)