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 4 years ago
parent
commit
bb79ae80ee

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

5
 import android.app.DownloadManager;
5
 import android.app.DownloadManager;
6
 import android.content.Context;
6
 import android.content.Context;
7
 import android.content.Intent;
7
 import android.content.Intent;
8
+import android.content.pm.PackageManager;
8
 import android.graphics.Bitmap;
9
 import android.graphics.Bitmap;
9
 import android.graphics.Color;
10
 import android.graphics.Color;
11
+import android.Manifest;
10
 import android.net.Uri;
12
 import android.net.Uri;
11
 import android.os.Build;
13
 import android.os.Build;
12
 import android.os.Environment;
14
 import android.os.Environment;
13
 import androidx.annotation.RequiresApi;
15
 import androidx.annotation.RequiresApi;
16
+import androidx.core.content.ContextCompat;
14
 import android.text.TextUtils;
17
 import android.text.TextUtils;
15
 import android.view.Gravity;
18
 import android.view.Gravity;
16
 import android.view.View;
19
 import android.view.View;
22
 import android.webkit.DownloadListener;
25
 import android.webkit.DownloadListener;
23
 import android.webkit.GeolocationPermissions;
26
 import android.webkit.GeolocationPermissions;
24
 import android.webkit.JavascriptInterface;
27
 import android.webkit.JavascriptInterface;
28
+import android.webkit.PermissionRequest;
25
 import android.webkit.URLUtil;
29
 import android.webkit.URLUtil;
26
 import android.webkit.ValueCallback;
30
 import android.webkit.ValueCallback;
27
 import android.webkit.WebChromeClient;
31
 import android.webkit.WebChromeClient;
65
 import java.net.MalformedURLException;
69
 import java.net.MalformedURLException;
66
 import java.net.URL;
70
 import java.net.URL;
67
 import java.net.URLEncoder;
71
 import java.net.URLEncoder;
72
+import java.util.ArrayList;
68
 import java.util.HashMap;
73
 import java.util.HashMap;
69
 import java.util.Locale;
74
 import java.util.Locale;
70
 import java.util.Map;
75
 import java.util.Map;
763
       return true;
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
     @Override
806
     @Override
767
     public void onProgressChanged(WebView webView, int newProgress) {
807
     public void onProgressChanged(WebView webView, int newProgress) {
768
       super.onProgressChanged(webView, newProgress);
808
       super.onProgressChanged(webView, newProgress);
1010
       }
1050
       }
1011
     }
1051
     }
1012
   }
1052
   }
1013
-}
1053
+}