Create a foreground service during screen sharing.

This commit is contained in:
Onuray Sahin 2022-04-20 13:33:35 +03:00
parent 99cab794c4
commit fa3476d6cd
5 changed files with 85 additions and 0 deletions

View File

@ -375,6 +375,12 @@
android:exported="false" android:exported="false"
android:foregroundServiceType="location" /> android:foregroundServiceType="location" />
<service
android:name=".features.call.webrtc.ScreenCaptureService"
android:exported="false"
android:foregroundServiceType="mediaProjection"
tools:targetApi="Q" />
<!-- Receivers --> <!-- Receivers -->
<receiver <receiver

View File

@ -57,6 +57,7 @@ import im.vector.app.features.call.dialpad.CallDialPadBottomSheet
import im.vector.app.features.call.dialpad.DialPadFragment import im.vector.app.features.call.dialpad.DialPadFragment
import im.vector.app.features.call.transfer.CallTransferActivity import im.vector.app.features.call.transfer.CallTransferActivity
import im.vector.app.features.call.utils.EglUtils import im.vector.app.features.call.utils.EglUtils
import im.vector.app.features.call.webrtc.ScreenCaptureService
import im.vector.app.features.call.webrtc.WebRtcCall import im.vector.app.features.call.webrtc.WebRtcCall
import im.vector.app.features.call.webrtc.WebRtcCallManager import im.vector.app.features.call.webrtc.WebRtcCallManager
import im.vector.app.features.displayname.getBestName import im.vector.app.features.displayname.getBestName
@ -633,6 +634,13 @@ class VectorCallActivity : VectorBaseActivity<ActivityCallBinding>(), CallContro
private val screenSharingPermissionActivityResultLauncher = registerStartForActivityResult { activityResult -> private val screenSharingPermissionActivityResultLauncher = registerStartForActivityResult { activityResult ->
if (activityResult.resultCode == Activity.RESULT_OK) { if (activityResult.resultCode == Activity.RESULT_OK) {
callViewModel.handle(VectorCallViewActions.StartScreenSharing) 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)
)
}
} }
} }

View File

@ -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)
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder {
return binder
}
fun stopService() {
stopSelf()
}
inner class LocalBinder : Binder() {
fun getService(): ScreenCaptureService = this@ScreenCaptureService
}
}

View File

@ -535,6 +535,20 @@ class NotificationUtils @Inject constructor(private val context: Context,
.build() .build()
} }
/**
* Creates a notification that indicates the application is capturing the screen.
*/
fun buildScreenSharingNotification(): Notification {
return NotificationCompat.Builder(context, SILENT_NOTIFICATION_CHANNEL_ID)
.setContentTitle(stringProvider.getString(R.string.screen_sharing_notification_title))
.setContentText(stringProvider.getString(R.string.screen_sharing_notification_description))
.setSmallIcon(R.drawable.ic_share_screen)
.setColor(ThemeUtils.getColor(context, android.R.attr.colorPrimary))
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setContentIntent(buildOpenHomePendingIntentForSummary())
.build()
}
fun buildDownloadFileNotification(uri: Uri, fileName: String, mimeType: String): Notification { fun buildDownloadFileNotification(uri: Uri, fileName: String, mimeType: String): Notification {
return NotificationCompat.Builder(context, SILENT_NOTIFICATION_CHANNEL_ID) return NotificationCompat.Builder(context, SILENT_NOTIFICATION_CHANNEL_ID)
.setGroup(stringProvider.getString(R.string.app_name)) .setGroup(stringProvider.getString(R.string.app_name))

View File

@ -3024,4 +3024,8 @@
<string name="room_message_notify_everyone">Notify the whole room</string> <string name="room_message_notify_everyone">Notify the whole room</string>
<string name="room_message_autocomplete_users">Users</string> <string name="room_message_autocomplete_users">Users</string>
<string name="room_message_autocomplete_notification">Room notification</string> <string name="room_message_autocomplete_notification">Room notification</string>
<!-- Screen sharing -->
<string name="screen_sharing_notification_title">${app_name} Screen Sharing</string>
<string name="screen_sharing_notification_description">Screen sharing is in progress</string>
</resources> </resources>