|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2026 Stream.io Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Stream License; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://github.com/GetStream/stream-video-android/blob/main/LICENSE |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.getstream.video.android.core.notifications.internal.service.incomingcallcoordinator |
| 18 | + |
| 19 | +import android.annotation.SuppressLint |
| 20 | +import android.content.Context |
| 21 | +import androidx.core.app.NotificationManagerCompat |
| 22 | +import androidx.core.net.toUri |
| 23 | +import io.getstream.log.taggedLogger |
| 24 | +import io.getstream.video.android.core.StreamVideoClient |
| 25 | +import io.getstream.video.android.core.notifications.NotificationType |
| 26 | +import io.getstream.video.android.core.notifications.internal.VideoPushDelegate.Companion.DEFAULT_CALL_TEXT |
| 27 | +import io.getstream.video.android.core.notifications.internal.service.CallService.Companion.TRIGGER_REMOVE_INCOMING_CALL |
| 28 | +import io.getstream.video.android.core.notifications.internal.service.CallServiceConfig |
| 29 | +import io.getstream.video.android.core.notifications.internal.service.IncomingCallPresenter |
| 30 | +import io.getstream.video.android.core.notifications.internal.service.IncomingCallRequest |
| 31 | +import io.getstream.video.android.core.notifications.internal.service.JetpackTelecomRepositoryProvider |
| 32 | +import io.getstream.video.android.core.notifications.internal.service.ServiceIntentBuilder |
| 33 | +import io.getstream.video.android.core.notifications.internal.service.ShowIncomingCallResult |
| 34 | +import io.getstream.video.android.core.notifications.internal.service.StartServiceParam |
| 35 | +import io.getstream.video.android.core.notifications.internal.telecom.TelecomHelper |
| 36 | +import io.getstream.video.android.core.notifications.internal.telecom.TelecomPermissions |
| 37 | +import io.getstream.video.android.core.utils.safeCallWithResult |
| 38 | +import io.getstream.video.android.model.StreamCallId |
| 39 | +import kotlinx.coroutines.launch |
| 40 | + |
| 41 | +/** Coordinates the existing incoming-call service and optional Telecom registration path. */ |
| 42 | +internal class PreAndroid17IncomingCallCoordinator( |
| 43 | + private val context: Context, |
| 44 | + private val client: StreamVideoClient, |
| 45 | + private val incomingCallPresenter: IncomingCallPresenter, |
| 46 | + private val serviceIntentBuilder: ServiceIntentBuilder, |
| 47 | + private val telecomPermissions: TelecomPermissions, |
| 48 | + private val telecomHelper: TelecomHelper, |
| 49 | + private val jetpackTelecomRepositoryProvider: JetpackTelecomRepositoryProvider, |
| 50 | +) : IncomingCallCoordinator { |
| 51 | + |
| 52 | + private val logger by taggedLogger("PreAndroid17IncomingCallCoordinator") |
| 53 | + |
| 54 | + @SuppressLint("MissingPermission", "NewApi") |
| 55 | + override fun showIncomingCall(request: IncomingCallRequest) { |
| 56 | + val result = incomingCallPresenter.showIncomingCall( |
| 57 | + context = context, |
| 58 | + callId = request.callId, |
| 59 | + callDisplayName = request.callDisplayName, |
| 60 | + callServiceConfiguration = request.callServiceConfiguration, |
| 61 | + notification = request.notification, |
| 62 | + ) |
| 63 | + logger.d { "[showIncomingCall] service start result: $result" } |
| 64 | + |
| 65 | + if (result != ShowIncomingCallResult.FG_SERVICE || |
| 66 | + !telecomPermissions.canUseTelecom(request.callServiceConfiguration, context) || |
| 67 | + !telecomHelper.canUseJetpackTelecom() |
| 68 | + ) { |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + updateIncomingCallNotification(request, client) |
| 73 | + val jetpackTelecomRepository = jetpackTelecomRepositoryProvider.get(request.callId) |
| 74 | + val addressUri = "${client.telecomConfig?.schema}:${request.callId.id}".toUri() |
| 75 | + val formattedCallDisplayName = request.callDisplayName |
| 76 | + ?.takeIf { it.isNotBlank() } |
| 77 | + ?: DEFAULT_CALL_TEXT |
| 78 | + val call = client.call(request.callId.type, request.callId.id) |
| 79 | + |
| 80 | + call.state.jetpackTelecomRepository = jetpackTelecomRepository |
| 81 | + call.scope.launch { |
| 82 | + jetpackTelecomRepository.registerCall( |
| 83 | + displayName = formattedCallDisplayName, |
| 84 | + address = addressUri, |
| 85 | + isIncoming = true, |
| 86 | + isVideoCall = request.isVideo, |
| 87 | + ) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + override fun dismissIncomingCall( |
| 92 | + callId: StreamCallId, |
| 93 | + config: CallServiceConfig, |
| 94 | + ) { |
| 95 | + safeCallWithResult { |
| 96 | + context.startService( |
| 97 | + serviceIntentBuilder.buildStartIntent( |
| 98 | + context, |
| 99 | + StartServiceParam( |
| 100 | + callId = callId, |
| 101 | + trigger = TRIGGER_REMOVE_INCOMING_CALL, |
| 102 | + callServiceConfiguration = config, |
| 103 | + ), |
| 104 | + ), |
| 105 | + )!! |
| 106 | + }.onError { |
| 107 | + val notificationId = callId.getNotificationId(NotificationType.Incoming) |
| 108 | + logger.d { "[dismissIncomingCall] notificationId: $notificationId" } |
| 109 | + NotificationManagerCompat.from(context).cancel(notificationId) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private fun updateIncomingCallNotification( |
| 114 | + request: IncomingCallRequest, |
| 115 | + client: StreamVideoClient, |
| 116 | + ) { |
| 117 | + request.notification?.let { notification -> |
| 118 | + val notificationId = request.callId.getNotificationId(NotificationType.Incoming) |
| 119 | + client.call(request.callId.type, request.callId.id) |
| 120 | + .state.updateNotification(notificationId, notification) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments