Skip to content
Open
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,7 +3,9 @@ package com.nativephp.mobile.network
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.core.content.ContextCompat
import android.util.Log
import android.webkit.*
import android.widget.Toast
Expand Down Expand Up @@ -121,6 +123,44 @@ class WebViewManager(
)
return true
}

override fun onPermissionRequest(request: PermissionRequest) {
Log.d(TAG, "onPermissionRequest: ${request.resources.joinToString()}")

val allowed = mutableListOf<String>()
for (resource in request.resources) {
when (resource) {
PermissionRequest.RESOURCE_AUDIO_CAPTURE -> {
if (ContextCompat.checkSelfPermission(
context,
android.Manifest.permission.RECORD_AUDIO,
) == PackageManager.PERMISSION_GRANTED
) {
allowed.add(resource)
}
}
PermissionRequest.RESOURCE_VIDEO_CAPTURE -> {
if (ContextCompat.checkSelfPermission(
context,
android.Manifest.permission.CAMERA,
) == PackageManager.PERMISSION_GRANTED
) {
allowed.add(resource)
}
}
PermissionRequest.RESOURCE_MIDI_SYSEX,
PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID -> allowed.add(resource)
}
}

if (allowed.isNotEmpty()) {
Log.d(TAG, "Granting WebView permission: ${allowed.joinToString()}")
request.grant(allowed.toTypedArray())
} else {
Log.d(TAG, "Denying WebView permission: ${request.resources.joinToString()}")
request.deny()
}
}
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/Plugins/Compilers/AndroidPluginCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,20 @@ protected function mergeManifestEntries(Collection $plugins): void
}
}

$permissionsToAdd = array_unique($permissionsToAdd);

// Chromium's WebView audio stack needs MODIFY_AUDIO_SETTINGS whenever microphone
// capture is declared (e.g. for getUserMedia / communication device routing).
if (
in_array('android.permission.RECORD_AUDIO', $permissionsToAdd, true)
|| str_contains($mainManifest, 'android.permission.RECORD_AUDIO')
) {
$permissionsToAdd[] = 'android.permission.MODIFY_AUDIO_SETTINGS';
$permissionsToAdd = array_unique($permissionsToAdd);
}

// Add permissions that don't already exist
$mainManifest = $this->injectPermissions($mainManifest, array_unique($permissionsToAdd));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we have left the array_unique here so we still only need to call it once when RECORD_AUDIO is present?

$mainManifest = $this->injectPermissions($mainManifest, $permissionsToAdd);

// Add features that don't already exist
$mainManifest = $this->injectFeatures($mainManifest, $featuresToAdd);
Expand Down
Loading