From 88cc7471a8827fc9b807480ce44f0e0334c81441 Mon Sep 17 00:00:00 2001 From: ganfra Date: Tue, 20 Jul 2021 12:28:07 +0200 Subject: [PATCH] Call notification: add missed call icons. Also set tint. --- .../vector/app/core/services/CallService.kt | 28 ++++++----- .../notifications/NotificationUtils.kt | 50 ++++++++++++++----- .../res/drawable/ic_missed_video_call.xml | 10 ++++ .../drawable/ic_missed_video_call_small.xml | 4 ++ .../res/drawable/ic_missed_voice_call.xml | 12 +++++ .../drawable/ic_missed_voice_call_small.xml | 5 ++ vector/src/main/res/values/strings.xml | 3 +- 7 files changed, 85 insertions(+), 27 deletions(-) create mode 100644 vector/src/main/res/drawable/ic_missed_video_call.xml create mode 100644 vector/src/main/res/drawable/ic_missed_video_call_small.xml create mode 100644 vector/src/main/res/drawable/ic_missed_voice_call.xml create mode 100644 vector/src/main/res/drawable/ic_missed_voice_call_small.xml diff --git a/vector/src/main/java/im/vector/app/core/services/CallService.kt b/vector/src/main/java/im/vector/app/core/services/CallService.kt index 7434f8a277..b0766d1cdb 100644 --- a/vector/src/main/java/im/vector/app/core/services/CallService.kt +++ b/vector/src/main/java/im/vector/app/core/services/CallService.kt @@ -37,6 +37,7 @@ import im.vector.app.features.home.AvatarRenderer import im.vector.app.features.notifications.NotificationUtils import im.vector.app.features.popup.IncomingCallAlert import im.vector.app.features.popup.PopupAlertManager +import org.matrix.android.sdk.api.session.content.ContentUrlResolver import org.matrix.android.sdk.api.util.MatrixItem import timber.log.Timber @@ -47,7 +48,7 @@ class CallService : VectorService() { private val connections = mutableMapOf() private val knownCalls = mutableSetOf() - private val ongoingCallIds = mutableSetOf() + private val connectedCallIds = mutableSetOf() private lateinit var notificationManager: NotificationManagerCompat private lateinit var notificationUtils: NotificationUtils @@ -204,15 +205,12 @@ class CallService : VectorService() { mediaSession?.isActive = false myStopSelf() } - val wasOngoing = ongoingCallIds.remove(callId) - if (wasOngoing || isRejected) { - val notification = notificationUtils.buildCallEndedNotification() + val wasConnected = connectedCallIds.remove(callId) + if (wasConnected || terminatedCall.isOutgoing || isRejected) { + val notification = notificationUtils.buildCallEndedNotification(terminatedCall.isVideoCall) notificationManager.notify(callId.hashCode(), notification) } else { - val notification = notificationUtils.buildCallMissedNotification( - roomId = terminatedCall.nativeRoomId, - title = terminatedCall.matrixItem?.getBestName() ?: terminatedCall.opponentUserId - ) + val notification = notificationUtils.buildCallMissedNotification(terminatedCall) notificationManager.cancel(callId.hashCode()) notificationManager.notify(MISSED_CALL_TAG, terminatedCall.nativeRoomId.hashCode(), notification) } @@ -252,7 +250,7 @@ class CallService : VectorService() { private fun displayCallInProgressNotification(intent: Intent) { Timber.v("## VOIP displayCallInProgressNotification") val callId = intent.getStringExtra(EXTRA_CALL_ID) ?: "" - ongoingCallIds.add(callId) + connectedCallIds.add(callId) val call = callManager.getCallById(callId) ?: return Unit.also { handleUnexpectedState(callId) } @@ -277,7 +275,7 @@ class CallService : VectorService() { if (callId != null) { notificationManager.cancel(callId.hashCode()) } - val notification = notificationUtils.buildCallEndedNotification() + val notification = notificationUtils.buildCallEndedNotification(false) startForeground(DEFAULT_NOTIFICATION_ID, notification) if (knownCalls.isEmpty()) { mediaSession?.isActive = false @@ -296,15 +294,19 @@ class CallService : VectorService() { opponentUserId = this.mxCall.opponentUserId, matrixItem = vectorComponent().activeSessionHolder().getSafeActiveSession()?.let { this.getOpponentAsMatrixItem(it) - } + }, + isVideoCall = this.mxCall.isVideoCall, + isOutgoing = this.mxCall.isOutgoing ) } - private data class CallInformation( + data class CallInformation( val callId: String, val nativeRoomId: String, val opponentUserId: String, - val matrixItem: MatrixItem? + val matrixItem: MatrixItem?, + val isVideoCall: Boolean, + val isOutgoing: Boolean, ) companion object { diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt index 27771ddb2b..3c4af56b37 100755 --- a/vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt @@ -48,6 +48,7 @@ import androidx.fragment.app.Fragment import im.vector.app.BuildConfig import im.vector.app.R import im.vector.app.core.resources.StringProvider +import im.vector.app.core.services.CallService import im.vector.app.core.utils.startNotificationChannelSettingsIntent import im.vector.app.features.call.VectorCallActivity import im.vector.app.features.call.service.CallHeadsUpActionReceiver @@ -298,12 +299,14 @@ class NotificationUtils @Inject constructor(private val context: Context, .apply { if (call.mxCall.isVideoCall) { setContentText(stringProvider.getString(R.string.incoming_video_call)) + setSmallIcon(R.drawable.ic_call_answer_video) } else { setContentText(stringProvider.getString(R.string.incoming_voice_call)) + setSmallIcon(R.drawable.ic_call_answer) } } - .setSmallIcon(R.drawable.incoming_call_notification_transparent) .setCategory(NotificationCompat.CATEGORY_CALL) + .setColor(ThemeUtils.getColor(context, android.R.attr.colorPrimary)) .setLights(accentColor, 500, 500) .setOngoing(true) @@ -339,8 +342,6 @@ class NotificationUtils @Inject constructor(private val context: Context, builder.addAction( NotificationCompat.Action( R.drawable.ic_call_answer, - // IconCompat.createWithResource(applicationContext, R.drawable.ic_call) - // .setTint(ContextCompat.getColor(applicationContext, R.color.vctr_positive_accent)), getActionText(R.string.call_notification_answer, R.attr.colorPrimary), answerCallPendingIntent ) @@ -360,10 +361,15 @@ class NotificationUtils @Inject constructor(private val context: Context, .setContentTitle(ensureTitleNotEmpty(title)) .apply { setContentText(stringProvider.getString(R.string.call_ring)) + if (call.mxCall.isVideoCall) { + setSmallIcon(R.drawable.ic_call_answer_video) + } else { + setSmallIcon(R.drawable.ic_call_answer) + } } - .setSmallIcon(R.drawable.incoming_call_notification_transparent) .setCategory(NotificationCompat.CATEGORY_CALL) .setLights(accentColor, 500, 500) + .setColor(ThemeUtils.getColor(context, android.R.attr.colorPrimary)) .setOngoing(true) val contentIntent = VectorCallActivity.newIntent( @@ -407,11 +413,13 @@ class NotificationUtils @Inject constructor(private val context: Context, .apply { if (call.mxCall.isVideoCall) { setContentText(stringProvider.getString(R.string.video_call_in_progress)) + setSmallIcon(R.drawable.ic_call_answer_video) } else { setContentText(stringProvider.getString(R.string.call_in_progress)) + setSmallIcon(R.drawable.ic_call_answer) } } - .setSmallIcon(R.drawable.incoming_call_notification_transparent) + .setColor(ThemeUtils.getColor(context, android.R.attr.colorPrimary)) .setCategory(NotificationCompat.CATEGORY_CALL) val rejectCallPendingIntent = buildRejectCallPendingIntent(call.callId) @@ -450,11 +458,18 @@ class NotificationUtils @Inject constructor(private val context: Context, /** * Build a temporary (because service will be stopped just after) notification for the CallService, when a call is ended */ - fun buildCallEndedNotification(): Notification { + fun buildCallEndedNotification(isVideoCall: Boolean): Notification { return NotificationCompat.Builder(context, SILENT_NOTIFICATION_CHANNEL_ID) .setContentTitle(stringProvider.getString(R.string.call_ended)) + .apply { + if (isVideoCall) { + setSmallIcon(R.drawable.ic_call_answer_video) + } else { + setSmallIcon(R.drawable.ic_call_answer) + } + } .setTimeoutAfter(2000) - .setSmallIcon(R.drawable.ic_material_call_end_grey) + .setColor(ThemeUtils.getColor(context, android.R.attr.colorPrimary)) .setCategory(NotificationCompat.CATEGORY_CALL) .build() } @@ -462,17 +477,26 @@ class NotificationUtils @Inject constructor(private val context: Context, /** * Build notification for the CallService, when a call is missed */ - fun buildCallMissedNotification(roomId: String, title: String): Notification { - val builder = NotificationCompat.Builder(context, NOISY_NOTIFICATION_CHANNEL_ID) - .setContentTitle(title) - .setContentText(stringProvider.getString(R.string.call_missed)) - .setSmallIcon(R.drawable.ic_material_call_end_grey) + fun buildCallMissedNotification(callInformation: CallService.CallInformation): Notification { + val builder = NotificationCompat.Builder(context, SILENT_NOTIFICATION_CHANNEL_ID) + .setContentTitle(callInformation.matrixItem?.getBestName() ?: callInformation.opponentUserId) + .apply { + if (callInformation.isVideoCall) { + setContentText(stringProvider.getString(R.string.missed_video_call)) + setSmallIcon(R.drawable.ic_missed_video_call) + } else { + setContentText(stringProvider.getString(R.string.missed_audio_call)) + setSmallIcon(R.drawable.ic_missed_voice_call) + } + } + .setShowWhen(true) + .setColor(ThemeUtils.getColor(context, android.R.attr.colorPrimary)) .setAutoCancel(true) .setCategory(NotificationCompat.CATEGORY_CALL) val contentPendingIntent = TaskStackBuilder.create(context) .addNextIntentWithParentStack(HomeActivity.newIntent(context)) - .addNextIntent(RoomDetailActivity.newIntent(context, RoomDetailArgs(roomId))) + .addNextIntent(RoomDetailActivity.newIntent(context, RoomDetailArgs(callInformation.nativeRoomId))) .getPendingIntent(System.currentTimeMillis().toInt(), PendingIntent.FLAG_UPDATE_CURRENT) builder.setContentIntent(contentPendingIntent) diff --git a/vector/src/main/res/drawable/ic_missed_video_call.xml b/vector/src/main/res/drawable/ic_missed_video_call.xml new file mode 100644 index 0000000000..555e15a371 --- /dev/null +++ b/vector/src/main/res/drawable/ic_missed_video_call.xml @@ -0,0 +1,10 @@ + + + diff --git a/vector/src/main/res/drawable/ic_missed_video_call_small.xml b/vector/src/main/res/drawable/ic_missed_video_call_small.xml new file mode 100644 index 0000000000..c703f0cfca --- /dev/null +++ b/vector/src/main/res/drawable/ic_missed_video_call_small.xml @@ -0,0 +1,4 @@ + + + diff --git a/vector/src/main/res/drawable/ic_missed_voice_call.xml b/vector/src/main/res/drawable/ic_missed_voice_call.xml new file mode 100644 index 0000000000..dc869fa8bc --- /dev/null +++ b/vector/src/main/res/drawable/ic_missed_voice_call.xml @@ -0,0 +1,12 @@ + + + + diff --git a/vector/src/main/res/drawable/ic_missed_voice_call_small.xml b/vector/src/main/res/drawable/ic_missed_voice_call_small.xml new file mode 100644 index 0000000000..21d8b309c6 --- /dev/null +++ b/vector/src/main/res/drawable/ic_missed_voice_call_small.xml @@ -0,0 +1,5 @@ + + + + diff --git a/vector/src/main/res/values/strings.xml b/vector/src/main/res/values/strings.xml index e9c8e5b43e..f77c940cf9 100644 --- a/vector/src/main/res/values/strings.xml +++ b/vector/src/main/res/values/strings.xml @@ -727,7 +727,8 @@ Call connected Call connecting… Call ended - Missed call + Missed audio call + Missed video call Calling… Incoming Call Incoming Video Call