From 811b2e8c75746878b651ee046ee31f1312eed745 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Fri, 1 Jul 2022 14:51:16 +0100 Subject: [PATCH] accessing the notification action ids via a dedicated class which builds upon the application id --- .../core/pushers/VectorMessagingReceiver.kt | 4 +- .../core/utils/ExternalApplicationsUtil.kt | 6 +-- .../NotificationBroadcastReceiver.kt | 13 ++--- .../notifications/NotificationUtils.kt | 51 ++++++++++--------- ...ttingsNotificationsTroubleshootFragment.kt | 8 +-- 5 files changed, 44 insertions(+), 38 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt b/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt index 0d6828f569..9764347ec1 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt @@ -31,6 +31,7 @@ import im.vector.app.core.network.WifiDetector import im.vector.app.core.pushers.model.PushData import im.vector.app.core.services.GuardServiceStarter import im.vector.app.features.notifications.NotifiableEventResolver +import im.vector.app.features.notifications.NotificationActionIds import im.vector.app.features.notifications.NotificationDrawerManager import im.vector.app.features.notifications.NotificationUtils import im.vector.app.features.settings.BackgroundSyncMode @@ -68,6 +69,7 @@ class VectorMessagingReceiver : MessagingReceiver() { @Inject lateinit var unifiedPushHelper: UnifiedPushHelper @Inject lateinit var unifiedPushStore: UnifiedPushStore @Inject lateinit var pushParser: PushParser + @Inject lateinit var actionIds: NotificationActionIds private val coroutineScope = CoroutineScope(SupervisorJob()) @@ -100,7 +102,7 @@ class VectorMessagingReceiver : MessagingReceiver() { // Diagnostic Push if (pushData.eventId == PushersManager.TEST_EVENT_ID) { - val intent = Intent(NotificationUtils.PUSH_ACTION) + val intent = Intent(actionIds.PUSH_ACTION) LocalBroadcastManager.getInstance(context).sendBroadcast(intent) return } diff --git a/vector/src/main/java/im/vector/app/core/utils/ExternalApplicationsUtil.kt b/vector/src/main/java/im/vector/app/core/utils/ExternalApplicationsUtil.kt index 3a8ffac543..9f7f0a70a6 100644 --- a/vector/src/main/java/im/vector/app/core/utils/ExternalApplicationsUtil.kt +++ b/vector/src/main/java/im/vector/app/core/utils/ExternalApplicationsUtil.kt @@ -182,7 +182,7 @@ fun openUri(activity: Activity, uri: String) { */ fun openMedia(activity: Activity, savedMediaPath: String, mimeType: String) { val file = File(savedMediaPath) - val uri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".fileProvider", file) + val uri = FileProvider.getUriForFile(activity, activity.packageName + ".fileProvider", file) val intent = Intent(Intent.ACTION_VIEW).apply { setDataAndType(uri, mimeType) @@ -214,7 +214,7 @@ fun openLocation(activity: Activity, latitude: Double, longitude: Double) { fun shareMedia(context: Context, file: File, mediaMimeType: String?) { val mediaUri = try { - FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", file) + FileProvider.getUriForFile(context, context.packageName + ".fileProvider", file) } catch (e: Exception) { Timber.e(e, "onMediaAction Selected File cannot be shared") return @@ -376,7 +376,7 @@ private fun addToGallery(savedFile: File, mediaMimeType: String?, context: Conte /** * Open the play store to the provided application Id, default to this app. */ -fun openPlayStore(activity: Activity, appId: String = BuildConfig.APPLICATION_ID) { +fun openPlayStore(activity: Activity, appId: String) { try { activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appId"))) } catch (activityNotFoundException: ActivityNotFoundException) { diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationBroadcastReceiver.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationBroadcastReceiver.kt index c01be9a44f..9e87b4d325 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotificationBroadcastReceiver.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationBroadcastReceiver.kt @@ -48,31 +48,32 @@ class NotificationBroadcastReceiver : BroadcastReceiver() { @Inject lateinit var activeSessionHolder: ActiveSessionHolder @Inject lateinit var analyticsTracker: AnalyticsTracker @Inject lateinit var clock: Clock + @Inject lateinit var actionIds: NotificationActionIds override fun onReceive(context: Context?, intent: Intent?) { if (intent == null || context == null) return Timber.v("NotificationBroadcastReceiver received : $intent") when (intent.action) { - NotificationUtils.SMART_REPLY_ACTION -> + actionIds.SMART_REPLY_ACTION -> handleSmartReply(intent, context) - NotificationUtils.DISMISS_ROOM_NOTIF_ACTION -> + actionIds.DISMISS_ROOM_NOTIF_ACTION -> intent.getStringExtra(KEY_ROOM_ID)?.let { roomId -> notificationDrawerManager.updateEvents { it.clearMessagesForRoom(roomId) } } - NotificationUtils.DISMISS_SUMMARY_ACTION -> + actionIds.DISMISS_SUMMARY_ACTION -> notificationDrawerManager.clearAllEvents() - NotificationUtils.MARK_ROOM_READ_ACTION -> + actionIds.MARK_ROOM_READ_ACTION -> intent.getStringExtra(KEY_ROOM_ID)?.let { roomId -> notificationDrawerManager.updateEvents { it.clearMessagesForRoom(roomId) } handleMarkAsRead(roomId) } - NotificationUtils.JOIN_ACTION -> { + actionIds.JOIN_ACTION -> { intent.getStringExtra(KEY_ROOM_ID)?.let { roomId -> notificationDrawerManager.updateEvents { it.clearMemberShipNotificationForRoom(roomId) } handleJoinRoom(roomId) } } - NotificationUtils.REJECT_ACTION -> { + actionIds.REJECT_ACTION -> { intent.getStringExtra(KEY_ROOM_ID)?.let { roomId -> notificationDrawerManager.updateEvents { it.clearMemberShipNotificationForRoom(roomId) } handleRejectRoom(roomId) 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 0985823000..02667cee7f 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 @@ -45,7 +45,6 @@ import androidx.core.content.getSystemService import androidx.core.content.res.ResourcesCompat import androidx.core.graphics.drawable.IconCompat import androidx.fragment.app.Fragment -import im.vector.app.BuildConfig import im.vector.app.R import im.vector.app.core.extensions.createIgnoredUri import im.vector.app.core.platform.PendingIntentCompat @@ -74,13 +73,30 @@ import kotlin.random.Random * Util class for creating notifications. * Note: Cannot inject ColorProvider in the constructor, because it requires an Activity */ + +data class NotificationActionIds @Inject constructor( + private val buildMeta: BuildMeta, +) { + + val JOIN_ACTION = "${buildMeta.applicationId}.NotificationActions.JOIN_ACTION" + val REJECT_ACTION = "${buildMeta.applicationId}.NotificationActions.REJECT_ACTION" + val QUICK_LAUNCH_ACTION = "${buildMeta.applicationId}.NotificationActions.QUICK_LAUNCH_ACTION" + val MARK_ROOM_READ_ACTION = "${buildMeta.applicationId}.NotificationActions.MARK_ROOM_READ_ACTION" + val SMART_REPLY_ACTION = "${buildMeta.applicationId}.NotificationActions.SMART_REPLY_ACTION" + val DISMISS_SUMMARY_ACTION = "${buildMeta.applicationId}.NotificationActions.DISMISS_SUMMARY_ACTION" + val DISMISS_ROOM_NOTIF_ACTION = "${buildMeta.applicationId}.NotificationActions.DISMISS_ROOM_NOTIF_ACTION" + val TAP_TO_VIEW_ACTION = "${buildMeta.applicationId}.NotificationActions.TAP_TO_VIEW_ACTION" + val DIAGNOSTIC_ACTION = "${buildMeta.applicationId}.NotificationActions.DIAGNOSTIC" + val PUSH_ACTION = "${buildMeta.applicationId}.PUSH" +} + @Singleton class NotificationUtils @Inject constructor( private val context: Context, private val stringProvider: StringProvider, private val vectorPreferences: VectorPreferences, private val clock: Clock, - private val buildMeta: BuildMeta, + private val actionIds: NotificationActionIds, ) { companion object { @@ -96,21 +112,6 @@ class NotificationUtils @Inject constructor( */ const val NOTIFICATION_ID_FOREGROUND_SERVICE = 61 - /* ========================================================================================== - * IDs for actions - * ========================================================================================== */ - - const val JOIN_ACTION = "${BuildConfig.APPLICATION_ID}.NotificationActions.JOIN_ACTION" - const val REJECT_ACTION = "${BuildConfig.APPLICATION_ID}.NotificationActions.REJECT_ACTION" - private const val QUICK_LAUNCH_ACTION = "${BuildConfig.APPLICATION_ID}.NotificationActions.QUICK_LAUNCH_ACTION" - const val MARK_ROOM_READ_ACTION = "${BuildConfig.APPLICATION_ID}.NotificationActions.MARK_ROOM_READ_ACTION" - const val SMART_REPLY_ACTION = "${BuildConfig.APPLICATION_ID}.NotificationActions.SMART_REPLY_ACTION" - const val DISMISS_SUMMARY_ACTION = "${BuildConfig.APPLICATION_ID}.NotificationActions.DISMISS_SUMMARY_ACTION" - const val DISMISS_ROOM_NOTIF_ACTION = "${BuildConfig.APPLICATION_ID}.NotificationActions.DISMISS_ROOM_NOTIF_ACTION" - const val TAP_TO_VIEW_ACTION = "${BuildConfig.APPLICATION_ID}.NotificationActions.TAP_TO_VIEW_ACTION" - const val DIAGNOSTIC_ACTION = "${BuildConfig.APPLICATION_ID}.NotificationActions.DIAGNOSTIC" - const val PUSH_ACTION = "${BuildConfig.APPLICATION_ID}.PUSH" - /* ========================================================================================== * IDs for channels * ========================================================================================== */ @@ -653,7 +654,7 @@ class NotificationUtils @Inject constructor( // Add actions and notification intents // Mark room as read val markRoomReadIntent = Intent(context, NotificationBroadcastReceiver::class.java) - markRoomReadIntent.action = MARK_ROOM_READ_ACTION + markRoomReadIntent.action = actionIds.MARK_ROOM_READ_ACTION markRoomReadIntent.data = createIgnoredUri(roomInfo.roomId) markRoomReadIntent.putExtra(NotificationBroadcastReceiver.KEY_ROOM_ID, roomInfo.roomId) val markRoomReadPendingIntent = PendingIntent.getBroadcast( @@ -700,7 +701,7 @@ class NotificationUtils @Inject constructor( val intent = Intent(context, NotificationBroadcastReceiver::class.java) intent.putExtra(NotificationBroadcastReceiver.KEY_ROOM_ID, roomInfo.roomId) - intent.action = DISMISS_ROOM_NOTIF_ACTION + intent.action = actionIds.DISMISS_ROOM_NOTIF_ACTION val pendingIntent = PendingIntent.getBroadcast( context.applicationContext, clock.epochMillis().toInt(), @@ -735,7 +736,7 @@ class NotificationUtils @Inject constructor( val roomId = inviteNotifiableEvent.roomId // offer to type a quick reject button val rejectIntent = Intent(context, NotificationBroadcastReceiver::class.java) - rejectIntent.action = REJECT_ACTION + rejectIntent.action = actionIds.REJECT_ACTION rejectIntent.data = createIgnoredUri("$roomId&$matrixId") rejectIntent.putExtra(NotificationBroadcastReceiver.KEY_ROOM_ID, roomId) val rejectIntentPendingIntent = PendingIntent.getBroadcast( @@ -753,7 +754,7 @@ class NotificationUtils @Inject constructor( // offer to type a quick accept button val joinIntent = Intent(context, NotificationBroadcastReceiver::class.java) - joinIntent.action = JOIN_ACTION + joinIntent.action = actionIds.JOIN_ACTION joinIntent.data = createIgnoredUri("$roomId&$matrixId") joinIntent.putExtra(NotificationBroadcastReceiver.KEY_ROOM_ID, roomId) val joinIntentPendingIntent = PendingIntent.getBroadcast( @@ -836,7 +837,7 @@ class NotificationUtils @Inject constructor( private fun buildOpenRoomIntent(roomId: String): PendingIntent? { val roomIntentTap = RoomDetailActivity.newIntent(context, TimelineArgs(roomId = roomId, switchToParentSpace = true), true) - roomIntentTap.action = TAP_TO_VIEW_ACTION + roomIntentTap.action = actionIds.TAP_TO_VIEW_ACTION // pending intent get reused by system, this will mess up the extra params, so put unique info to avoid that roomIntentTap.data = createIgnoredUri("openRoom?$roomId") @@ -874,7 +875,7 @@ class NotificationUtils @Inject constructor( val intent: Intent if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent = Intent(context, NotificationBroadcastReceiver::class.java) - intent.action = SMART_REPLY_ACTION + intent.action = actionIds.SMART_REPLY_ACTION intent.data = createIgnoredUri(roomId) intent.putExtra(NotificationBroadcastReceiver.KEY_ROOM_ID, roomId) return PendingIntent.getBroadcast( @@ -950,7 +951,7 @@ class NotificationUtils @Inject constructor( private fun getDismissSummaryPendingIntent(): PendingIntent { val intent = Intent(context, NotificationBroadcastReceiver::class.java) - intent.action = DISMISS_SUMMARY_ACTION + intent.action = actionIds.DISMISS_SUMMARY_ACTION intent.data = createIgnoredUri("deleteSummary") return PendingIntent.getBroadcast( context.applicationContext, @@ -989,7 +990,7 @@ class NotificationUtils @Inject constructor( fun displayDiagnosticNotification() { val testActionIntent = Intent(context, TestNotificationReceiver::class.java) - testActionIntent.action = DIAGNOSTIC_ACTION + testActionIntent.action = actionIds.DIAGNOSTIC_ACTION val testPendingIntent = PendingIntent.getBroadcast( context, 0, diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationsTroubleshootFragment.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationsTroubleshootFragment.kt index 0eb22bf24d..635021cc54 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationsTroubleshootFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationsTroubleshootFragment.kt @@ -34,6 +34,7 @@ import im.vector.app.core.extensions.cleanup import im.vector.app.core.extensions.registerStartForActivityResult import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.databinding.FragmentSettingsNotificationsTroubleshootBinding +import im.vector.app.features.notifications.NotificationActionIds import im.vector.app.features.notifications.NotificationUtils import im.vector.app.features.rageshake.BugReporter import im.vector.app.features.settings.VectorSettingsFragmentInteractionListener @@ -46,7 +47,8 @@ import javax.inject.Inject class VectorSettingsNotificationsTroubleshootFragment @Inject constructor( private val bugReporter: BugReporter, - private val testManagerFactory: NotificationTroubleshootTestManagerFactory + private val testManagerFactory: NotificationTroubleshootTestManagerFactory, + private val actionIds: NotificationActionIds, ) : VectorBaseFragment() { private var testManager: NotificationTroubleshootTestManager? = null @@ -151,11 +153,11 @@ class VectorSettingsNotificationsTroubleshootFragment @Inject constructor( tryOrNull("Unable to register the receiver") { LocalBroadcastManager.getInstance(requireContext()) - .registerReceiver(broadcastReceiverPush, IntentFilter(NotificationUtils.PUSH_ACTION)) + .registerReceiver(broadcastReceiverPush, IntentFilter(actionIds.PUSH_ACTION)) } tryOrNull("Unable to register the receiver") { LocalBroadcastManager.getInstance(requireContext()) - .registerReceiver(broadcastReceiverNotification, IntentFilter(NotificationUtils.DIAGNOSTIC_ACTION)) + .registerReceiver(broadcastReceiverNotification, IntentFilter(actionIds.DIAGNOSTIC_ACTION)) } }