Skip to content
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
1 change: 1 addition & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE"/>

<application>
<service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.DialogInterface
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.telecom.CallAudioState
import android.telecom.PhoneAccountHandle
Expand Down Expand Up @@ -95,6 +96,7 @@ class TwilioVoicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamH
private val REQUEST_CODE_CALL_PHONE = 3
private val REQUEST_CODE_READ_PHONE_NUMBERS = 4
private val REQUEST_CODE_READ_PHONE_STATE = 5
private val REQUEST_CODE_MICROPHONE_FOREGROUND = 6

private var isSpeakerOn: Boolean = false
private var isBluetoothOn: Boolean = false
Expand Down Expand Up @@ -244,6 +246,9 @@ class TwilioVoicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamH
if (requestCode == REQUEST_CODE_MICROPHONE) {
if (permissions.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Microphone permission granted")
requestPermissionForMicrophoneForeground(onPermissionResult = { granted ->
Log.d(TAG, "onRequestPermissionsResult: Microphone foreground permission granted: $granted");
});
logEventPermission("Microphone", true)
} else {
Log.d(TAG, "Microphone permission not granted")
Expand Down Expand Up @@ -281,6 +286,14 @@ class TwilioVoicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamH
Log.d(TAG, "Call Phone permission not granted")
logEventPermission("Call Phone State", false)
}
} else if (requestCode == REQUEST_CODE_MICROPHONE_FOREGROUND) {
if (permissions.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Microphone foreground permission granted")
logEventPermission("Microphone", true)
} else {
Log.d(TAG, "Microphone foreground permission not granted")
logEventPermission("Microphone", false)
}
}
return true
}
Expand Down Expand Up @@ -1435,6 +1448,23 @@ class TwilioVoicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamH
)
}

/// Request permission for microphone on Android 14 and higher.
/// Source: https://developer.android.com/reference/android/Manifest.permission#FOREGROUND_SERVICE_MICROPHONE
private fun requestPermissionForMicrophoneForeground(onPermissionResult: (Boolean) -> Unit) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
Log.d(TAG, "requestPermissionForMicrophoneForeground: Microphone foreground permission automatically requested.");
return requestPermissionOrShowRationale(
"Microphone Foreground",
"Microphone Foreground permission is required to make or receive phone calls on Android 14 and higher.",
Manifest.permission.FOREGROUND_SERVICE_MICROPHONE,
REQUEST_CODE_MICROPHONE_FOREGROUND,
onPermissionResult
)
} else {
Log.d(TAG, "requestPermissionForMicrophoneForeground: Microphone foreground permission skipped.");
}
}

private fun requestPermissionForPhoneState(onPermissionResult: (Boolean) -> Unit) {
return requestPermissionOrShowRationale(
"Read Phone State",
Expand Down