Browse Source

feat(Android): webrtc permission request - conflicts fixed (#719)

* fix(Android): WebRTC permission request (#231)

* fix(Android):  Avoid unintentionally granting requests for new permissions (#231)

* ContextCompat import migrated to androidx new artifact

* RNCWebViewManager.java original format restored
Eze 5 years ago
parent
commit
bb79ae80ee

+ 41
- 1
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

@@ -5,12 +5,15 @@ import android.annotation.TargetApi;
5 5
 import android.app.DownloadManager;
6 6
 import android.content.Context;
7 7
 import android.content.Intent;
8
+import android.content.pm.PackageManager;
8 9
 import android.graphics.Bitmap;
9 10
 import android.graphics.Color;
11
+import android.Manifest;
10 12
 import android.net.Uri;
11 13
 import android.os.Build;
12 14
 import android.os.Environment;
13 15
 import androidx.annotation.RequiresApi;
16
+import androidx.core.content.ContextCompat;
14 17
 import android.text.TextUtils;
15 18
 import android.view.Gravity;
16 19
 import android.view.View;
@@ -22,6 +25,7 @@ import android.webkit.CookieManager;
22 25
 import android.webkit.DownloadListener;
23 26
 import android.webkit.GeolocationPermissions;
24 27
 import android.webkit.JavascriptInterface;
28
+import android.webkit.PermissionRequest;
25 29
 import android.webkit.URLUtil;
26 30
 import android.webkit.ValueCallback;
27 31
 import android.webkit.WebChromeClient;
@@ -65,6 +69,7 @@ import java.io.UnsupportedEncodingException;
65 69
 import java.net.MalformedURLException;
66 70
 import java.net.URL;
67 71
 import java.net.URLEncoder;
72
+import java.util.ArrayList;
68 73
 import java.util.HashMap;
69 74
 import java.util.Locale;
70 75
 import java.util.Map;
@@ -763,6 +768,41 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
763 768
       return true;
764 769
     }
765 770
 
771
+    // Fix WebRTC permission request error.
772
+    @Override
773
+    public void onPermissionRequest(final PermissionRequest request) {
774
+      String[] requestedResources = request.getResources();
775
+      ArrayList<String> permissions = new ArrayList<>();
776
+      ArrayList<String> grantedPermissions = new ArrayList<String>();
777
+      for (int i = 0; i < requestedResources.length; i++) {
778
+        if (requestedResources[i].equals(PermissionRequest.RESOURCE_AUDIO_CAPTURE)) {
779
+          permissions.add(Manifest.permission.RECORD_AUDIO);
780
+        } else if (requestedResources[i].equals(PermissionRequest.RESOURCE_VIDEO_CAPTURE)) {
781
+          permissions.add(Manifest.permission.CAMERA);
782
+        }
783
+        // TODO: RESOURCE_MIDI_SYSEX, RESOURCE_PROTECTED_MEDIA_ID.
784
+      }
785
+
786
+      for (int i = 0; i < permissions.size(); i++) {
787
+        if (ContextCompat.checkSelfPermission(mReactContext, permissions.get(i)) != PackageManager.PERMISSION_GRANTED) {
788
+          continue;
789
+        }
790
+        if (permissions.get(i).equals(Manifest.permission.RECORD_AUDIO)) {
791
+          grantedPermissions.add(PermissionRequest.RESOURCE_AUDIO_CAPTURE);
792
+        } else if (permissions.get(i).equals(Manifest.permission.CAMERA)) {
793
+          grantedPermissions.add(PermissionRequest.RESOURCE_VIDEO_CAPTURE);
794
+        }
795
+      }
796
+
797
+      if (grantedPermissions.isEmpty()) {
798
+        request.deny();
799
+      } else {
800
+        String[] grantedPermissionsArray = new String[grantedPermissions.size()];
801
+        grantedPermissionsArray = grantedPermissions.toArray(grantedPermissionsArray);
802
+        request.grant(grantedPermissionsArray);
803
+      }
804
+    }
805
+
766 806
     @Override
767 807
     public void onProgressChanged(WebView webView, int newProgress) {
768 808
       super.onProgressChanged(webView, newProgress);
@@ -1010,4 +1050,4 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1010 1050
       }
1011 1051
     }
1012 1052
   }
1013
-}
1053
+}