Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VoIP Screen Sharing Permission #5811

Merged
merged 9 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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 changelog.d/5811.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VoIP Screen Sharing Permission
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class DebugVectorFeatures(
override fun isLiveLocationEnabled(): Boolean = read(DebugFeatureKeys.liveLocationSharing)
?: vectorFeatures.isLiveLocationEnabled()

override fun isScreenSharingEnabled(): Boolean = read(DebugFeatureKeys.screenSharing)
?: vectorFeatures.isScreenSharingEnabled()

fun <T> override(value: T?, key: Preferences.Key<T>) = updatePreferences {
if (value == null) {
it.remove(key)
Expand Down Expand Up @@ -114,4 +117,5 @@ object DebugFeatureKeys {
val onboardingPersonalize = booleanPreferencesKey("onboarding-personalize")
val onboardingCombinedRegister = booleanPreferencesKey("onboarding-combined-register")
val liveLocationSharing = booleanPreferencesKey("live-location-sharing")
val screenSharing = booleanPreferencesKey("screen-sharing")
}
6 changes: 6 additions & 0 deletions vector/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@
android:exported="false"
android:foregroundServiceType="location" />

<service
android:name=".features.call.webrtc.ScreenCaptureService"
android:exported="false"
android:foregroundServiceType="mediaProjection"
tools:targetApi="Q" />

<!-- Receivers -->

<receiver
Expand Down
2 changes: 2 additions & 0 deletions vector/src/main/java/im/vector/app/features/VectorFeatures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface VectorFeatures {
fun isOnboardingPersonalizeEnabled(): Boolean
fun isOnboardingCombinedRegisterEnabled(): Boolean
fun isLiveLocationEnabled(): Boolean
fun isScreenSharingEnabled(): Boolean

enum class OnboardingVariant {
LEGACY,
Expand All @@ -43,4 +44,5 @@ class DefaultVectorFeatures : VectorFeatures {
override fun isOnboardingPersonalizeEnabled() = false
override fun isOnboardingCombinedRegisterEnabled() = false
override fun isLiveLocationEnabled(): Boolean = false
override fun isScreenSharingEnabled(): Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.platform.VectorBaseBottomSheetDialogFragment
import im.vector.app.databinding.BottomSheetCallControlsBinding
import im.vector.app.features.VectorFeatures
import javax.inject.Inject

@AndroidEntryPoint
class CallControlsBottomSheet : VectorBaseBottomSheetDialogFragment<BottomSheetCallControlsBinding>() {
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): BottomSheetCallControlsBinding {
return BottomSheetCallControlsBinding.inflate(inflater, container, false)
}

@Inject lateinit var vectorFeatures: VectorFeatures

private val callViewModel: VectorCallViewModel by activityViewModel()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -66,6 +70,12 @@ class CallControlsBottomSheet : VectorBaseBottomSheetDialogFragment<BottomSheetC
callViewModel.handle(VectorCallViewActions.InitiateCallTransfer)
dismiss()
}

views.callControlsShareScreen.isVisible = vectorFeatures.isScreenSharingEnabled()
views.callControlsShareScreen.views.bottomSheetActionClickableZone.debouncedClicks {
callViewModel.handle(VectorCallViewActions.ToggleScreenSharing)
dismiss()
}
}

private fun renderState(state: VectorCallViewState) {
Expand Down Expand Up @@ -95,5 +105,6 @@ class CallControlsBottomSheet : VectorBaseBottomSheetDialogFragment<BottomSheetC
views.callControlsToggleHoldResume.leftIcon = ContextCompat.getDrawable(requireContext(), R.drawable.ic_call_hold_action)
}
views.callControlsTransfer.isVisible = state.canOpponentBeTransferred
views.callControlsShareScreen.title = getString(if (state.isSharingScreen) R.string.call_stop_screen_sharing else R.string.call_start_screen_sharing)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP
import android.content.res.Configuration
import android.graphics.Color
import android.media.projection.MediaProjectionManager
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
Expand Down Expand Up @@ -56,6 +57,8 @@ 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.utils.EglUtils
import im.vector.app.features.call.webrtc.ScreenCaptureService
import im.vector.app.features.call.webrtc.ScreenCaptureServiceConnection
import im.vector.app.features.call.webrtc.WebRtcCall
import im.vector.app.features.call.webrtc.WebRtcCallManager
import im.vector.app.features.displayname.getBestName
Expand Down Expand Up @@ -94,6 +97,7 @@ class VectorCallActivity : VectorBaseActivity<ActivityCallBinding>(), CallContro

@Inject lateinit var callManager: WebRtcCallManager
@Inject lateinit var avatarRenderer: AvatarRenderer
@Inject lateinit var screenCaptureServiceConnection: ScreenCaptureServiceConnection

private val callViewModel: VectorCallViewModel by viewModel()

Expand Down Expand Up @@ -512,20 +516,22 @@ class VectorCallActivity : VectorBaseActivity<ActivityCallBinding>(), CallContro
private fun handleViewEvents(event: VectorCallViewEvents?) {
Timber.tag(loggerTag.value).v("handleViewEvents $event")
when (event) {
is VectorCallViewEvents.ConnectionTimeout -> {
is VectorCallViewEvents.ConnectionTimeout -> {
onErrorTimoutConnect(event.turn)
}
is VectorCallViewEvents.ShowDialPad -> {
is VectorCallViewEvents.ShowDialPad -> {
CallDialPadBottomSheet.newInstance(false).apply {
callback = dialPadCallback
}.show(supportFragmentManager, FRAGMENT_DIAL_PAD_TAG)
}
is VectorCallViewEvents.ShowCallTransferScreen -> {
is VectorCallViewEvents.ShowCallTransferScreen -> {
val callId = withState(callViewModel) { it.callId }
navigator.openCallTransfer(this, callTransferActivityResultLauncher, callId)
}
is VectorCallViewEvents.FailToTransfer -> showSnackbar(getString(R.string.call_transfer_failure))
else -> Unit
is VectorCallViewEvents.FailToTransfer -> showSnackbar(getString(R.string.call_transfer_failure))
is VectorCallViewEvents.ShowScreenSharingPermissionDialog -> handleShowScreenSharingPermissionDialog()
is VectorCallViewEvents.StopScreenSharingService -> handleStopScreenSharingService()
else -> Unit
}
}

Expand Down Expand Up @@ -628,6 +634,32 @@ class VectorCallActivity : VectorBaseActivity<ActivityCallBinding>(), CallContro
}
}

private val screenSharingPermissionActivityResultLauncher = registerStartForActivityResult { activityResult ->
if (activityResult.resultCode == Activity.RESULT_OK) {
callViewModel.handle(VectorCallViewActions.StartScreenSharing)
// We need to start a foreground service with a sticky notification during screen sharing
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContextCompat.startForegroundService(
this,
Intent(this, ScreenCaptureService::class.java)
)
screenCaptureServiceConnection.bind()
}
}
}

private fun handleShowScreenSharingPermissionDialog() {
getSystemService<MediaProjectionManager>()?.let {
navigator.openScreenSharingPermissionDialog(it.createScreenCaptureIntent(), screenSharingPermissionActivityResultLauncher)
}
}

private fun handleStopScreenSharingService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
screenCaptureServiceConnection.stopScreenCapturing()
}
}

companion object {
private const val EXTRA_MODE = "EXTRA_MODE"
private const val FRAGMENT_DIAL_PAD_TAG = "FRAGMENT_DIAL_PAD_TAG"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ sealed class VectorCallViewActions : VectorViewModelAction {
object CallTransferSelectionCancelled : VectorCallViewActions()
data class CallTransferSelectionResult(val callTransferResult: CallTransferResult) : VectorCallViewActions()
object TransferCall : VectorCallViewActions()
object ToggleScreenSharing : VectorCallViewActions()
object StartScreenSharing : VectorCallViewActions()
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ sealed class VectorCallViewEvents : VectorViewEvents {
object ShowDialPad : VectorCallViewEvents()
object ShowCallTransferScreen : VectorCallViewEvents()
object FailToTransfer : VectorCallViewEvents()
object ShowScreenSharingPermissionDialog : VectorCallViewEvents()
object StopScreenSharingService : VectorCallViewEvents()
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: This is a good opportunity to get rid of the commented code below. They're nearly 2 years old, I don't think we'll need it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right :) done.

// data class CallAnswered(val content: CallAnswerContent) : VectorCallViewEvents()
// data class CallHangup(val content: CallHangupContent) : VectorCallViewEvents()
// object CallAccepted : VectorCallViewEvents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,27 +256,30 @@ class VectorCallViewModel @AssistedInject constructor(

override fun handle(action: VectorCallViewActions) = withState { state ->
when (action) {
VectorCallViewActions.EndCall -> call?.endCall()
VectorCallViewActions.AcceptCall -> {
VectorCallViewActions.EndCall -> {
call?.endCall()
_viewEvents.post(VectorCallViewEvents.StopScreenSharingService)
}
VectorCallViewActions.AcceptCall -> {
setState {
copy(callState = Loading())
}
call?.acceptIncomingCall()
}
VectorCallViewActions.DeclineCall -> {
VectorCallViewActions.DeclineCall -> {
setState {
copy(callState = Loading())
}
call?.endCall()
}
VectorCallViewActions.ToggleMute -> {
VectorCallViewActions.ToggleMute -> {
val muted = state.isAudioMuted
call?.muteCall(!muted)
setState {
copy(isAudioMuted = !muted)
}
}
VectorCallViewActions.ToggleVideo -> {
VectorCallViewActions.ToggleVideo -> {
if (state.isVideoCall) {
val videoEnabled = state.isVideoEnabled
call?.enableVideo(!videoEnabled)
Expand All @@ -286,19 +289,19 @@ class VectorCallViewModel @AssistedInject constructor(
}
Unit
}
VectorCallViewActions.ToggleHoldResume -> {
VectorCallViewActions.ToggleHoldResume -> {
val isRemoteOnHold = state.isRemoteOnHold
call?.updateRemoteOnHold(!isRemoteOnHold)
}
is VectorCallViewActions.ChangeAudioDevice -> {
is VectorCallViewActions.ChangeAudioDevice -> {
callManager.audioManager.setAudioDevice(action.device)
}
VectorCallViewActions.SwitchSoundDevice -> {
VectorCallViewActions.SwitchSoundDevice -> {
_viewEvents.post(
VectorCallViewEvents.ShowSoundDeviceChooser(state.availableDevices, state.device)
)
}
VectorCallViewActions.HeadSetButtonPressed -> {
VectorCallViewActions.HeadSetButtonPressed -> {
if (state.callState.invoke() is CallState.LocalRinging) {
// accept call
call?.acceptIncomingCall()
Expand All @@ -309,20 +312,20 @@ class VectorCallViewModel @AssistedInject constructor(
}
Unit
}
VectorCallViewActions.ToggleCamera -> {
VectorCallViewActions.ToggleCamera -> {
call?.switchCamera()
}
VectorCallViewActions.ToggleHDSD -> {
VectorCallViewActions.ToggleHDSD -> {
if (!state.isVideoCall) return@withState
call?.setCaptureFormat(if (state.isHD) CaptureFormat.SD else CaptureFormat.HD)
}
VectorCallViewActions.OpenDialPad -> {
VectorCallViewActions.OpenDialPad -> {
_viewEvents.post(VectorCallViewEvents.ShowDialPad)
}
is VectorCallViewActions.SendDtmfDigit -> {
is VectorCallViewActions.SendDtmfDigit -> {
call?.sendDtmfDigit(action.digit)
}
VectorCallViewActions.InitiateCallTransfer -> {
VectorCallViewActions.InitiateCallTransfer -> {
call?.updateRemoteOnHold(true)
_viewEvents.post(
VectorCallViewEvents.ShowCallTransferScreen
Expand All @@ -334,13 +337,38 @@ class VectorCallViewModel @AssistedInject constructor(
is VectorCallViewActions.CallTransferSelectionResult -> {
handleCallTransferSelectionResult(action.callTransferResult)
}
VectorCallViewActions.TransferCall -> {
VectorCallViewActions.TransferCall -> {
handleCallTransfer()
}
is VectorCallViewActions.SwitchCall -> {
is VectorCallViewActions.SwitchCall -> {
setState { VectorCallViewState(action.callArgs) }
setupCallWithCurrentState()
}
is VectorCallViewActions.ToggleScreenSharing -> {
handleToggleScreenSharing(state.isSharingScreen)
}
is VectorCallViewActions.StartScreenSharing -> {
call?.startSharingScreen()
setState {
copy(isSharingScreen = true)
}
}
}
}

private fun handleToggleScreenSharing(isSharingScreen: Boolean) {
if (isSharingScreen) {
call?.stopSharingScreen()
setState {
copy(isSharingScreen = false)
}
_viewEvents.post(
VectorCallViewEvents.StopScreenSharingService
)
} else {
_viewEvents.post(
VectorCallViewEvents.ShowScreenSharingPermissionDialog
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ data class VectorCallViewState(
val callInfo: CallInfo? = null,
val formattedDuration: String = "",
val canOpponentBeTransferred: Boolean = false,
val transferee: TransfereeState = TransfereeState.NoTransferee
val transferee: TransfereeState = TransfereeState.NoTransferee,
val isSharingScreen: Boolean = false
) : MavericksState {

sealed class TransfereeState {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.vector.app.features.call.webrtc

import android.content.Intent
import android.os.Binder
import android.os.IBinder
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.core.services.VectorService
import im.vector.app.features.notifications.NotificationUtils
import javax.inject.Inject

@AndroidEntryPoint
class ScreenCaptureService : VectorService() {

@Inject lateinit var notificationUtils: NotificationUtils
private val binder = LocalBinder()

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
// Show a sticky notification
val notificationId = System.currentTimeMillis().toInt()
val notification = notificationUtils.buildScreenSharingNotification()
startForeground(notificationId, notification)
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: extracting this code into a private fun showStickyNotification() could get rid of the need for a comment ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice idea, done.


return START_STICKY
}

override fun onBind(intent: Intent?): IBinder {
return binder
}

fun stopService() {
stopSelf()
}

inner class LocalBinder : Binder() {
fun getService(): ScreenCaptureService = this@ScreenCaptureService
}
}
Loading