diff --git a/vector/src/main/java/im/vector/app/features/call/VectorCallActivity.kt b/vector/src/main/java/im/vector/app/features/call/VectorCallActivity.kt index abba36f2a0..e21aaf8880 100644 --- a/vector/src/main/java/im/vector/app/features/call/VectorCallActivity.kt +++ b/vector/src/main/java/im/vector/app/features/call/VectorCallActivity.kt @@ -54,6 +54,9 @@ import im.vector.app.core.utils.registerForPermissionsResult import im.vector.app.databinding.ActivityCallBinding import im.vector.app.features.call.dialpad.CallDialPadBottomSheet import im.vector.app.features.call.dialpad.DialPadFragment +import im.vector.app.features.call.transfer.CallTransferActivity +import im.vector.app.features.call.transfer.CallTransferResult +import im.vector.app.features.call.transfer.CallTransferViewEvents import im.vector.app.features.call.utils.EglUtils import im.vector.app.features.call.webrtc.WebRtcCall import im.vector.app.features.call.webrtc.WebRtcCallManager @@ -523,14 +526,23 @@ class VectorCallActivity : VectorBaseActivity(), CallContro val callId = withState(callViewModel) { it.callId } navigator.openCallTransfer(this, callTransferActivityResultLauncher, callId) } + is VectorCallViewEvents.FailToTransfer -> showSnackbar(getString(R.string.call_transfer_failure)) null -> { } } } private val callTransferActivityResultLauncher = registerStartForActivityResult { activityResult -> - if (activityResult.resultCode == Activity.RESULT_CANCELED) { - callViewModel.handle(VectorCallViewActions.CallTransferSelectionCancelled) + + when(activityResult.resultCode) { + Activity.RESULT_CANCELED -> { + callViewModel.handle(VectorCallViewActions.CallTransferSelectionCancelled) + } + Activity.RESULT_OK -> { + activityResult.data?.extras?.getParcelable(CallTransferActivity.EXTRA_TRANSFER_RESULT)?.also { + callViewModel.handle(VectorCallViewActions.CallTransferSelectionResult(it)) + } + } } } diff --git a/vector/src/main/java/im/vector/app/features/call/VectorCallViewActions.kt b/vector/src/main/java/im/vector/app/features/call/VectorCallViewActions.kt index fb39660282..d1ed961814 100644 --- a/vector/src/main/java/im/vector/app/features/call/VectorCallViewActions.kt +++ b/vector/src/main/java/im/vector/app/features/call/VectorCallViewActions.kt @@ -18,6 +18,7 @@ package im.vector.app.features.call import im.vector.app.core.platform.VectorViewModelAction import im.vector.app.features.call.audio.CallAudioManager +import im.vector.app.features.call.transfer.CallTransferResult sealed class VectorCallViewActions : VectorViewModelAction { object EndCall : VectorCallViewActions() @@ -37,5 +38,6 @@ sealed class VectorCallViewActions : VectorViewModelAction { object ToggleHDSD : VectorCallViewActions() object InitiateCallTransfer : VectorCallViewActions() object CallTransferSelectionCancelled : VectorCallViewActions() + data class CallTransferSelectionResult(val callTransferResult: CallTransferResult) : VectorCallViewActions() object TransferCall : VectorCallViewActions() } diff --git a/vector/src/main/java/im/vector/app/features/call/VectorCallViewEvents.kt b/vector/src/main/java/im/vector/app/features/call/VectorCallViewEvents.kt index 5a0a2f127c..7c29d7eea3 100644 --- a/vector/src/main/java/im/vector/app/features/call/VectorCallViewEvents.kt +++ b/vector/src/main/java/im/vector/app/features/call/VectorCallViewEvents.kt @@ -29,6 +29,7 @@ sealed class VectorCallViewEvents : VectorViewEvents { ) : VectorCallViewEvents() object ShowDialPad : VectorCallViewEvents() object ShowCallTransferScreen : VectorCallViewEvents() + object FailToTransfer : VectorCallViewEvents() // data class CallAnswered(val content: CallAnswerContent) : VectorCallViewEvents() // data class CallHangup(val content: CallHangupContent) : VectorCallViewEvents() // object CallAccepted : VectorCallViewEvents() diff --git a/vector/src/main/java/im/vector/app/features/call/VectorCallViewModel.kt b/vector/src/main/java/im/vector/app/features/call/VectorCallViewModel.kt index 4aca0ea499..a26eec04f3 100644 --- a/vector/src/main/java/im/vector/app/features/call/VectorCallViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/call/VectorCallViewModel.kt @@ -29,13 +29,17 @@ import im.vector.app.core.di.hiltMavericksViewModelFactory import im.vector.app.core.extensions.exhaustive import im.vector.app.core.platform.VectorViewModel import im.vector.app.features.call.audio.CallAudioManager +import im.vector.app.features.call.dialpad.DialPadLookup +import im.vector.app.features.call.transfer.CallTransferResult import im.vector.app.features.call.webrtc.WebRtcCall import im.vector.app.features.call.webrtc.WebRtcCallManager import im.vector.app.features.call.webrtc.getOpponentAsMatrixItem +import im.vector.app.features.createdirect.DirectRoomHelper import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.launch import org.matrix.android.sdk.api.MatrixPatterns +import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.call.CallState import org.matrix.android.sdk.api.session.call.MxCall @@ -47,7 +51,9 @@ class VectorCallViewModel @AssistedInject constructor( @Assisted initialState: VectorCallViewState, val session: Session, val callManager: WebRtcCallManager, - val proximityManager: CallProximityManager + val proximityManager: CallProximityManager, + private val dialPadLookup: DialPadLookup, + private val directRoomHelper: DirectRoomHelper, ) : VectorViewModel(initialState) { private var call: WebRtcCall? = null @@ -327,6 +333,9 @@ class VectorCallViewModel @AssistedInject constructor( VectorCallViewActions.CallTransferSelectionCancelled -> { call?.updateRemoteOnHold(false) } + is VectorCallViewActions.CallTransferSelectionResult -> { + handleCallTransferSelectionResult(action.callTransferResult) + } VectorCallViewActions.TransferCall -> { handleCallTransfer() } @@ -345,6 +354,53 @@ class VectorCallViewModel @AssistedInject constructor( } } + private fun handleCallTransferSelectionResult(result: CallTransferResult) { + when (result) { + is CallTransferResult.ConnectWithUserId -> connectWithUserId(result) + is CallTransferResult.ConnectWithPhoneNumber -> connectWithPhoneNumber(result) + }.exhaustive + } + + private fun connectWithUserId(result: CallTransferResult.ConnectWithUserId) { + viewModelScope.launch { + try { + if (result.consultFirst) { + val dmRoomId = directRoomHelper.ensureDMExists(result.selectedUserId) + callManager.startOutgoingCall( + nativeRoomId = dmRoomId, + otherUserId = result.selectedUserId, + isVideoCall = call?.mxCall?.isVideoCall.orFalse(), + transferee = call + ) + } else { + call?.transferToUser(result.selectedUserId, null) + } + } catch (failure: Throwable) { + _viewEvents.post(VectorCallViewEvents.FailToTransfer) + } + } + } + + private fun connectWithPhoneNumber(action: CallTransferResult.ConnectWithPhoneNumber) { + viewModelScope.launch { + try { + val result = dialPadLookup.lookupPhoneNumber(action.phoneNumber) + if (action.consultFirst) { + callManager.startOutgoingCall( + nativeRoomId = result.roomId, + otherUserId = result.userId, + isVideoCall = call?.mxCall?.isVideoCall.orFalse(), + transferee = call + ) + } else { + call?.transferToUser(result.userId, result.roomId) + } + } catch (failure: Throwable) { + _viewEvents.post(VectorCallViewEvents.FailToTransfer) + } + } + } + @AssistedFactory interface Factory : MavericksAssistedViewModelFactory { override fun create(initialState: VectorCallViewState): VectorCallViewModel diff --git a/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferActivity.kt b/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferActivity.kt index 0e63316bbe..4c0f092e3b 100644 --- a/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferActivity.kt +++ b/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferActivity.kt @@ -57,8 +57,6 @@ class CallTransferActivity : VectorBaseActivity() { callTransferViewModel.observeViewEvents { when (it) { is CallTransferViewEvents.Complete -> handleComplete() - CallTransferViewEvents.Loading -> showWaitingView() - is CallTransferViewEvents.FailToTransfer -> showSnackbar(getString(R.string.call_transfer_failure)) } } @@ -82,25 +80,32 @@ class CallTransferActivity : VectorBaseActivity() { when (views.callTransferTabLayout.selectedTabPosition) { CallTransferPagerAdapter.USER_LIST_INDEX -> { val selectedUser = sectionsPagerAdapter.userListFragment?.getCurrentState()?.getSelectedMatrixId()?.firstOrNull() ?: return@debouncedClicks - val action = CallTransferAction.ConnectWithUserId(views.callTransferConsultCheckBox.isChecked, selectedUser) - callTransferViewModel.handle(action) + val result = CallTransferResult.ConnectWithUserId(views.callTransferConsultCheckBox.isChecked, selectedUser) + handleComplete(result) } CallTransferPagerAdapter.DIAL_PAD_INDEX -> { val phoneNumber = sectionsPagerAdapter.dialPadFragment?.getRawInput() ?: return@debouncedClicks - val action = CallTransferAction.ConnectWithPhoneNumber(views.callTransferConsultCheckBox.isChecked, phoneNumber) - callTransferViewModel.handle(action) + val result = CallTransferResult.ConnectWithPhoneNumber(views.callTransferConsultCheckBox.isChecked, phoneNumber) + handleComplete(result) } } } } - private fun handleComplete() { - setResult(Activity.RESULT_OK) + private fun handleComplete(callTransferResult: CallTransferResult? = null) { + if (callTransferResult != null) { + val intent = Intent().apply { + putExtra(EXTRA_TRANSFER_RESULT, callTransferResult) + } + setResult(RESULT_OK, intent) + } else { + setResult(RESULT_OK) + } finish() } companion object { - + const val EXTRA_TRANSFER_RESULT = "EXTRA_TRANSFER_RESULT" fun newIntent(context: Context, callId: String): Intent { return Intent(context, CallTransferActivity::class.java).also { it.putExtra(Mavericks.KEY_ARG, CallTransferArgs(callId)) diff --git a/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferAction.kt b/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferResult.kt similarity index 64% rename from vector/src/main/java/im/vector/app/features/call/transfer/CallTransferAction.kt rename to vector/src/main/java/im/vector/app/features/call/transfer/CallTransferResult.kt index bd694ad14e..238c916634 100644 --- a/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferAction.kt +++ b/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferResult.kt @@ -16,9 +16,11 @@ package im.vector.app.features.call.transfer -import im.vector.app.core.platform.VectorViewModelAction +import android.os.Parcelable +import kotlinx.parcelize.Parcelize -sealed class CallTransferAction : VectorViewModelAction { - data class ConnectWithUserId(val consultFirst: Boolean, val selectedUserId: String) : CallTransferAction() - data class ConnectWithPhoneNumber(val consultFirst: Boolean, val phoneNumber: String) : CallTransferAction() + +sealed class CallTransferResult : Parcelable{ + @Parcelize data class ConnectWithUserId(val consultFirst: Boolean, val selectedUserId: String) : CallTransferResult() + @Parcelize data class ConnectWithPhoneNumber(val consultFirst: Boolean, val phoneNumber: String) : CallTransferResult() } diff --git a/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferViewEvents.kt b/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferViewEvents.kt index a8451e4fb5..4202506d23 100644 --- a/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferViewEvents.kt +++ b/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferViewEvents.kt @@ -20,6 +20,4 @@ import im.vector.app.core.platform.VectorViewEvents sealed class CallTransferViewEvents : VectorViewEvents { object Complete : CallTransferViewEvents() - object Loading : CallTransferViewEvents() - object FailToTransfer : CallTransferViewEvents() } diff --git a/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferViewModel.kt b/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferViewModel.kt index de6a5de539..64140eacf0 100644 --- a/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/call/transfer/CallTransferViewModel.kt @@ -23,6 +23,7 @@ import dagger.assisted.AssistedInject import im.vector.app.core.di.MavericksAssistedViewModelFactory import im.vector.app.core.di.hiltMavericksViewModelFactory import im.vector.app.core.extensions.exhaustive +import im.vector.app.core.platform.EmptyAction import im.vector.app.core.platform.VectorViewModel import im.vector.app.features.call.dialpad.DialPadLookup import im.vector.app.features.call.webrtc.WebRtcCall @@ -34,10 +35,8 @@ import org.matrix.android.sdk.api.session.call.CallState import org.matrix.android.sdk.api.session.call.MxCall class CallTransferViewModel @AssistedInject constructor(@Assisted initialState: CallTransferViewState, - private val dialPadLookup: DialPadLookup, - private val directRoomHelper: DirectRoomHelper, private val callManager: WebRtcCallManager) : - VectorViewModel(initialState) { + VectorViewModel(initialState) { @AssistedFactory interface Factory : MavericksAssistedViewModelFactory { @@ -68,53 +67,6 @@ class CallTransferViewModel @AssistedInject constructor(@Assisted initialState: call?.removeListener(callListener) } - override fun handle(action: CallTransferAction) { - when (action) { - is CallTransferAction.ConnectWithUserId -> connectWithUserId(action) - is CallTransferAction.ConnectWithPhoneNumber -> connectWithPhoneNumber(action) - }.exhaustive - } + override fun handle(action: EmptyAction) { } - private fun connectWithUserId(action: CallTransferAction.ConnectWithUserId) { - viewModelScope.launch { - try { - if (action.consultFirst) { - val dmRoomId = directRoomHelper.ensureDMExists(action.selectedUserId) - callManager.startOutgoingCall( - nativeRoomId = dmRoomId, - otherUserId = action.selectedUserId, - isVideoCall = call?.mxCall?.isVideoCall.orFalse(), - transferee = call - ) - } else { - call?.transferToUser(action.selectedUserId, null) - } - _viewEvents.post(CallTransferViewEvents.Complete) - } catch (failure: Throwable) { - _viewEvents.post(CallTransferViewEvents.FailToTransfer) - } - } - } - - private fun connectWithPhoneNumber(action: CallTransferAction.ConnectWithPhoneNumber) { - viewModelScope.launch { - try { - _viewEvents.post(CallTransferViewEvents.Loading) - val result = dialPadLookup.lookupPhoneNumber(action.phoneNumber) - if (action.consultFirst) { - callManager.startOutgoingCall( - nativeRoomId = result.roomId, - otherUserId = result.userId, - isVideoCall = call?.mxCall?.isVideoCall.orFalse(), - transferee = call - ) - } else { - call?.transferToUser(result.userId, result.roomId) - } - _viewEvents.post(CallTransferViewEvents.Complete) - } catch (failure: Throwable) { - _viewEvents.post(CallTransferViewEvents.FailToTransfer) - } - } - } }