forked from eatvio/eatvio_app
feat(video_calls): ios mic fix, android call fix. feat(dev_tools): base url caching
This commit is contained in:
@@ -69,6 +69,7 @@ android {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
implementation platform('com.google.firebase:firebase-bom:34.4.0')
|
||||
implementation 'com.google.firebase:firebase-messaging'
|
||||
implementation 'com.huawei.agconnect:agconnect-core:1.9.5.300'
|
||||
|
||||
@@ -8,6 +8,10 @@ import com.hiennv.flutter_callkit_incoming.CallkitIncomingBroadcastReceiver
|
||||
|
||||
object CallKitIntentBridge {
|
||||
fun dispatchCallKitAction(context: Context, action: String, data: Bundle?) {
|
||||
if (IncomingCallRingtonePolicy.actionStopsRingtone(action)) {
|
||||
VideoCallPushBridge.silenceIncomingCall(context)
|
||||
}
|
||||
|
||||
if (data != null) {
|
||||
CallKitAcceptIntentMapper.payloadFromBundle(data, action)?.let { payload ->
|
||||
PendingCallKitAcceptBridge.store(payload)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.eatvioapp.app
|
||||
|
||||
/**
|
||||
* Call actions that must stop the incoming ringtone.
|
||||
* Accept is included: answering the call is not the same as ending it,
|
||||
* but the ringing player is a separate object from the in-call audio.
|
||||
*/
|
||||
internal object IncomingCallRingtonePolicy {
|
||||
fun actionStopsRingtone(action: String): Boolean {
|
||||
return action.contains("ACTION_CALL_ACCEPT") ||
|
||||
action.contains("ACTION_CALL_DECLINE") ||
|
||||
action.contains("ACTION_CALL_ENDED") ||
|
||||
action.contains("ACTION_CALL_TIMEOUT")
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import android.util.Log
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import com.hiennv.flutter_callkit_incoming.CallkitConstants
|
||||
import com.hiennv.flutter_callkit_incoming.CallkitEventCallback
|
||||
import com.hiennv.flutter_callkit_incoming.CallkitNotificationManager
|
||||
import com.hiennv.flutter_callkit_incoming.CallkitNotificationService
|
||||
import com.hiennv.flutter_callkit_incoming.CallkitSoundPlayerManager
|
||||
@@ -17,14 +18,36 @@ import com.hiennv.flutter_callkit_incoming.addCall
|
||||
import com.hiennv.flutter_callkit_incoming.getDataActiveCalls
|
||||
import com.hiennv.flutter_callkit_incoming.removeAllCalls
|
||||
import java.util.HashMap
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
object VideoCallPushBridge {
|
||||
private const val TAG = "VideoCallPushBridge"
|
||||
private const val ANDROID_ACCENT_COLOR = "#2787F5"
|
||||
private const val MISSED_CALL_CLEANUP_DELAY_MS = 250L
|
||||
|
||||
private val mainHandler = Handler(Looper.getMainLooper())
|
||||
private val playbackLock = Any()
|
||||
private val playbackGeneration = AtomicInteger(0)
|
||||
|
||||
private var activeSoundManager: CallkitSoundPlayerManager? = null
|
||||
private var activeNotificationManager: CallkitNotificationManager? = null
|
||||
private var activeCallBundle: Bundle? = null
|
||||
private var storedAppContext: Context? = null
|
||||
private var ringtoneTimeout: Runnable? = null
|
||||
private var callbackRegistered = false
|
||||
|
||||
/**
|
||||
* The plugin stops only the sound player owned by [FlutterCallkitIncomingPlugin].
|
||||
* Incoming pushes start a different player, so accept/decline never reached it
|
||||
* and the ringtone kept looping until the process died.
|
||||
*/
|
||||
private val incomingCallEventCallback = object : CallkitEventCallback {
|
||||
override fun onCallEvent(event: CallkitEventCallback.CallEvent, callData: Bundle) {
|
||||
val context = storedAppContext ?: return
|
||||
Log.i(TAG, "incoming call event=$event, stopping ringtone")
|
||||
silenceIncomingCall(context)
|
||||
}
|
||||
}
|
||||
|
||||
fun isIncomingVideoCall(data: Map<String, String>): Boolean {
|
||||
val rawType = data["type"]
|
||||
@@ -89,18 +112,114 @@ object VideoCallPushBridge {
|
||||
|
||||
fun showIncomingCall(context: Context, data: Map<String, String>) {
|
||||
val appContext = context.applicationContext
|
||||
storedAppContext = appContext
|
||||
ensureIncomingCallCallbackRegistered()
|
||||
|
||||
val callData = buildCallDataBundle(data)
|
||||
if (callData == null) {
|
||||
Log.w(TAG, "showIncomingCall: missing conversation uuid, data=$data")
|
||||
return
|
||||
}
|
||||
|
||||
val generation = playbackGeneration.incrementAndGet()
|
||||
Log.i(TAG, "showIncomingCall: conversation=${callData.getString(CallkitConstants.EXTRA_CALLKIT_ID)}")
|
||||
|
||||
// Ringtone.play() and stop() must run on the same thread. FCM delivers
|
||||
// pushes on a background thread, while Answer runs on the main thread.
|
||||
mainHandler.post {
|
||||
synchronized(playbackLock) {
|
||||
if (generation != playbackGeneration.get()) {
|
||||
Log.i(TAG, "showIncomingCall: skipped stale playback generation=$generation")
|
||||
return@post
|
||||
}
|
||||
startIncomingCallLocked(appContext, callData, generation)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the ringtone that [showIncomingCall] started and removes the incoming
|
||||
* notification. Safe to call more than once.
|
||||
*/
|
||||
fun silenceIncomingCall(context: Context) {
|
||||
val appContext = context.applicationContext
|
||||
storedAppContext = appContext
|
||||
Log.i(TAG, "silenceIncomingCall")
|
||||
playbackGeneration.incrementAndGet()
|
||||
val stop = Runnable {
|
||||
synchronized(playbackLock) {
|
||||
stopPlaybackLocked(appContext)
|
||||
}
|
||||
}
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
stop.run()
|
||||
} else {
|
||||
mainHandler.post(stop)
|
||||
}
|
||||
}
|
||||
|
||||
fun dismissAllIncomingCalls(context: Context) {
|
||||
val appContext = context.applicationContext
|
||||
val calls = getDataActiveCalls(appContext)
|
||||
Log.i(TAG, "dismissAllIncomingCalls: active calls=${calls.size}")
|
||||
playbackGeneration.incrementAndGet()
|
||||
|
||||
val dismiss = Runnable {
|
||||
synchronized(playbackLock) {
|
||||
val notificationManager = resolveNotificationManager(appContext)
|
||||
val bundles = calls.map { it.toBundle() }
|
||||
stopPlaybackLocked(appContext)
|
||||
|
||||
for (bundle in bundles) {
|
||||
try {
|
||||
dismissIncomingCallNotification(appContext, notificationManager, bundle)
|
||||
} catch (error: Exception) {
|
||||
Log.w(
|
||||
TAG,
|
||||
"dismissAllIncomingCalls: failed for call ${bundle.getString(CallkitConstants.EXTRA_CALLKIT_ID)}",
|
||||
error,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
CallkitNotificationService.stopService(appContext)
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "dismissAllIncomingCalls: failed to stop callkit service", error)
|
||||
}
|
||||
|
||||
try {
|
||||
removeAllCalls(appContext)
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "dismissAllIncomingCalls: failed to remove persisted calls", error)
|
||||
}
|
||||
|
||||
stopIncomingRingtone()
|
||||
scheduleMissedCallCleanup(notificationManager, bundles)
|
||||
}
|
||||
}
|
||||
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
dismiss.run()
|
||||
} else {
|
||||
mainHandler.post(dismiss)
|
||||
}
|
||||
}
|
||||
|
||||
private fun ensureIncomingCallCallbackRegistered() {
|
||||
if (callbackRegistered) return
|
||||
FlutterCallkitIncomingPlugin.registerEventCallback(incomingCallEventCallback)
|
||||
callbackRegistered = true
|
||||
}
|
||||
|
||||
private fun startIncomingCallLocked(appContext: Context, callData: Bundle, generation: Int) {
|
||||
stopPlaybackLocked(appContext)
|
||||
|
||||
val soundManager = CallkitSoundPlayerManager(appContext)
|
||||
val notificationManager = CallkitNotificationManager(appContext, soundManager)
|
||||
activeSoundManager = soundManager
|
||||
activeNotificationManager = notificationManager
|
||||
activeCallBundle = callData
|
||||
notificationManager.createNotificationChanel(callData)
|
||||
notificationManager.showIncomingNotification(callData)
|
||||
|
||||
@@ -109,46 +228,41 @@ object VideoCallPushBridge {
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "showIncomingCall: failed to persist active call", error)
|
||||
}
|
||||
|
||||
val durationMs = callData.getLong(CallkitConstants.EXTRA_CALLKIT_DURATION, 45_000L)
|
||||
if (durationMs > 0L) {
|
||||
val timeout = Runnable {
|
||||
synchronized(playbackLock) {
|
||||
if (generation != playbackGeneration.get()) return@Runnable
|
||||
Log.i(TAG, "incoming ringtone timed out")
|
||||
stopPlaybackLocked(appContext)
|
||||
}
|
||||
}
|
||||
ringtoneTimeout = timeout
|
||||
mainHandler.postDelayed(timeout, durationMs)
|
||||
}
|
||||
}
|
||||
|
||||
fun dismissAllIncomingCalls(context: Context) {
|
||||
val appContext = context.applicationContext
|
||||
val calls = getDataActiveCalls(appContext)
|
||||
Log.i(TAG, "dismissAllIncomingCalls: active calls=${calls.size}")
|
||||
|
||||
stopIncomingRingtone(appContext)
|
||||
|
||||
val notificationManager = resolveNotificationManager(appContext)
|
||||
val bundles = calls.map { it.toBundle() }
|
||||
|
||||
for (bundle in bundles) {
|
||||
private fun stopPlaybackLocked(appContext: Context) {
|
||||
cancelRingtoneTimeoutLocked()
|
||||
val notificationManager = activeNotificationManager
|
||||
val bundle = activeCallBundle
|
||||
stopIncomingRingtone()
|
||||
if (notificationManager != null && bundle != null) {
|
||||
try {
|
||||
dismissIncomingCallNotification(appContext, notificationManager, bundle)
|
||||
} catch (error: Exception) {
|
||||
Log.w(
|
||||
TAG,
|
||||
"dismissAllIncomingCalls: failed for call ${bundle.getString(CallkitConstants.EXTRA_CALLKIT_ID)}",
|
||||
error,
|
||||
)
|
||||
Log.w(TAG, "stopPlayback: failed to dismiss incoming notification", error)
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
CallkitNotificationService.stopService(appContext)
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "dismissAllIncomingCalls: failed to stop callkit service", error)
|
||||
}
|
||||
|
||||
try {
|
||||
removeAllCalls(appContext)
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "dismissAllIncomingCalls: failed to remove persisted calls", error)
|
||||
}
|
||||
|
||||
stopIncomingRingtone(appContext)
|
||||
scheduleMissedCallCleanup(notificationManager, bundles)
|
||||
activeSoundManager = null
|
||||
activeNotificationManager = null
|
||||
activeCallBundle = null
|
||||
}
|
||||
|
||||
private fun cancelRingtoneTimeoutLocked() {
|
||||
ringtoneTimeout?.let { mainHandler.removeCallbacks(it) }
|
||||
ringtoneTimeout = null
|
||||
}
|
||||
|
||||
private fun resolveNotificationManager(context: Context): CallkitNotificationManager {
|
||||
@@ -161,11 +275,19 @@ object VideoCallPushBridge {
|
||||
return CallkitNotificationManager(context, soundManager)
|
||||
}
|
||||
|
||||
private fun stopIncomingRingtone(context: Context) {
|
||||
activeSoundManager?.stop()
|
||||
FlutterCallkitIncomingPlugin.getInstance()
|
||||
?.getCallkitSoundPlayerManager()
|
||||
?.stop()
|
||||
private fun stopIncomingRingtone() {
|
||||
try {
|
||||
activeSoundManager?.stop()
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "stopIncomingRingtone: active player", error)
|
||||
}
|
||||
try {
|
||||
FlutterCallkitIncomingPlugin.getInstance()
|
||||
?.getCallkitSoundPlayerManager()
|
||||
?.stop()
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "stopIncomingRingtone: plugin player", error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.eatvioapp.app
|
||||
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class IncomingCallRingtonePolicyTest {
|
||||
@Test
|
||||
fun answerDeclineEndAndTimeoutStopTheRingtone() {
|
||||
assertTrue(
|
||||
IncomingCallRingtonePolicy.actionStopsRingtone(
|
||||
"com.hiennv.flutter_callkit_incoming.ACTION_CALL_ACCEPT",
|
||||
),
|
||||
)
|
||||
assertTrue(
|
||||
IncomingCallRingtonePolicy.actionStopsRingtone(
|
||||
"com.hiennv.flutter_callkit_incoming.ACTION_CALL_DECLINE",
|
||||
),
|
||||
)
|
||||
assertTrue(
|
||||
IncomingCallRingtonePolicy.actionStopsRingtone(
|
||||
"com.hiennv.flutter_callkit_incoming.ACTION_CALL_ENDED",
|
||||
),
|
||||
)
|
||||
assertTrue(
|
||||
IncomingCallRingtonePolicy.actionStopsRingtone(
|
||||
"com.hiennv.flutter_callkit_incoming.ACTION_CALL_TIMEOUT",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun incomingAndStartActionsKeepTheRingtonePlaying() {
|
||||
assertFalse(
|
||||
IncomingCallRingtonePolicy.actionStopsRingtone(
|
||||
"com.hiennv.flutter_callkit_incoming.ACTION_CALL_INCOMING",
|
||||
),
|
||||
)
|
||||
assertFalse(
|
||||
IncomingCallRingtonePolicy.actionStopsRingtone(
|
||||
"com.hiennv.flutter_callkit_incoming.ACTION_CALL_START",
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user