From 4ec44ab3e1c74a1aab7fc101bf876feeea0832c1 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Wed, 20 Sep 2023 13:55:03 +0200 Subject: [PATCH] fix(android): Send notification events again --- .../org/moxxy/moxxy_native/Constants.kt | 3 +++ .../moxxy/moxxy_native/MoxxyNativePlugin.kt | 22 +++++-------------- .../notifications/NotificationReceiver.kt | 7 +++--- .../NotificationStreamHandler.kt | 22 +++++++++++++++++++ .../platform/KeyboardStreamHandler.kt | 3 +-- 5 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 android/src/main/kotlin/org/moxxy/moxxy_native/notifications/NotificationStreamHandler.kt diff --git a/android/src/main/kotlin/org/moxxy/moxxy_native/Constants.kt b/android/src/main/kotlin/org/moxxy/moxxy_native/Constants.kt index dfed36d..3257962 100644 --- a/android/src/main/kotlin/org/moxxy/moxxy_native/Constants.kt +++ b/android/src/main/kotlin/org/moxxy/moxxy_native/Constants.kt @@ -5,6 +5,9 @@ const val TAG = "moxxy_native" // The event channel name for the keyboard height const val KEYBOARD_HEIGHT_EVENT_CHANNEL_NAME = "org.moxxy.moxxyv2/keyboard_stream" +// The event channel name for notification events +const val NOTIFICATION_EVENT_CHANNEL_NAME = "org.moxxy.moxxyv2/notification_stream" + // The size of buffers to use for various operations const val BUFFER_SIZE = 4096 diff --git a/android/src/main/kotlin/org/moxxy/moxxy_native/MoxxyNativePlugin.kt b/android/src/main/kotlin/org/moxxy/moxxy_native/MoxxyNativePlugin.kt index 0065eef..5b365f4 100644 --- a/android/src/main/kotlin/org/moxxy/moxxy_native/MoxxyNativePlugin.kt +++ b/android/src/main/kotlin/org/moxxy/moxxy_native/MoxxyNativePlugin.kt @@ -25,6 +25,7 @@ import org.moxxy.moxxy_native.media.MediaImplementation import org.moxxy.moxxy_native.media.MoxxyMediaApi import org.moxxy.moxxy_native.notifications.MoxxyNotificationsApi import org.moxxy.moxxy_native.notifications.NotificationEvent +import org.moxxy.moxxy_native.notifications.NotificationStreamHandler import org.moxxy.moxxy_native.notifications.NotificationsImplementation import org.moxxy.moxxy_native.picker.FilePickerType import org.moxxy.moxxy_native.picker.MoxxyPickerApi @@ -37,23 +38,6 @@ import org.moxxy.moxxy_native.service.MoxxyServiceApi import org.moxxy.moxxy_native.service.PluginTracker import org.moxxy.moxxy_native.service.ServiceImplementation -object MoxxyEventChannels { - var notificationChannel: EventChannel? = null - var notificationEventSink: EventChannel.EventSink? = null -} - -object NotificationStreamHandler : EventChannel.StreamHandler { - override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { - Log.d(TAG, "NotificationStreamHandler: Attached stream") - MoxxyEventChannels.notificationEventSink = events - } - - override fun onCancel(arguments: Any?) { - Log.d(TAG, "NotificationStreamHandler: Detached stream") - MoxxyEventChannels.notificationEventSink = null - } -} - /* * Hold the last notification event in case we did a cold start. */ @@ -107,6 +91,10 @@ class MoxxyNativePlugin : FlutterPlugin, ActivityAware, ServiceAware, BroadcastR val keyboardChannel = EventChannel(flutterPluginBinding.getBinaryMessenger(), KEYBOARD_HEIGHT_EVENT_CHANNEL_NAME) keyboardChannel?.setStreamHandler(KeyboardStreamHandler) + // Special handling from notification events + val notificationChannel = EventChannel(flutterPluginBinding.getBinaryMessenger(), NOTIFICATION_EVENT_CHANNEL_NAME) + notificationChannel?.setStreamHandler(NotificationStreamHandler) + // Register the picker handler pickerListener = PickerResultListener(context!!) Log.d(TAG, "Attached to engine") diff --git a/android/src/main/kotlin/org/moxxy/moxxy_native/notifications/NotificationReceiver.kt b/android/src/main/kotlin/org/moxxy/moxxy_native/notifications/NotificationReceiver.kt index 9f8a47c..5b98d39 100644 --- a/android/src/main/kotlin/org/moxxy/moxxy_native/notifications/NotificationReceiver.kt +++ b/android/src/main/kotlin/org/moxxy/moxxy_native/notifications/NotificationReceiver.kt @@ -15,7 +15,6 @@ import androidx.core.app.RemoteInput import androidx.core.content.FileProvider import org.moxxy.moxxy_native.MARK_AS_READ_ACTION import org.moxxy.moxxy_native.MOXXY_FILEPROVIDER_ID -import org.moxxy.moxxy_native.MoxxyEventChannels import org.moxxy.moxxy_native.NOTIFICATION_EXTRA_ID_KEY import org.moxxy.moxxy_native.NOTIFICATION_EXTRA_JID_KEY import org.moxxy.moxxy_native.NOTIFICATION_MESSAGE_EXTRA_MIME @@ -50,7 +49,7 @@ class NotificationReceiver : BroadcastReceiver() { } private fun handleMarkAsRead(context: Context, intent: Intent) { - MoxxyEventChannels.notificationEventSink?.success( + NotificationStreamHandler.sink?.success( NotificationEvent( intent.getLongExtra(NOTIFICATION_EXTRA_ID_KEY, -1), intent.getStringExtra(NOTIFICATION_EXTRA_JID_KEY)!!, @@ -65,7 +64,7 @@ class NotificationReceiver : BroadcastReceiver() { private fun handleReply(context: Context, intent: Intent) { val remoteInput = RemoteInput.getResultsFromIntent(intent) ?: return val replyPayload = remoteInput.getCharSequence(REPLY_TEXT_KEY) - MoxxyEventChannels.notificationEventSink?.success( + NotificationStreamHandler.sink?.success( NotificationEvent( intent.getLongExtra(NOTIFICATION_EXTRA_ID_KEY, -1), intent.getStringExtra(NOTIFICATION_EXTRA_JID_KEY)!!, @@ -165,7 +164,7 @@ class NotificationReceiver : BroadcastReceiver() { } private fun handleTap(context: Context, intent: Intent) { - MoxxyEventChannels.notificationEventSink?.success( + NotificationStreamHandler.sink?.success( NotificationEvent( intent.getLongExtra(NOTIFICATION_EXTRA_ID_KEY, -1), intent.getStringExtra(NOTIFICATION_EXTRA_JID_KEY)!!, diff --git a/android/src/main/kotlin/org/moxxy/moxxy_native/notifications/NotificationStreamHandler.kt b/android/src/main/kotlin/org/moxxy/moxxy_native/notifications/NotificationStreamHandler.kt new file mode 100644 index 0000000..8516e0f --- /dev/null +++ b/android/src/main/kotlin/org/moxxy/moxxy_native/notifications/NotificationStreamHandler.kt @@ -0,0 +1,22 @@ +package org.moxxy.moxxy_native.notifications + +import android.util.Log +import io.flutter.plugin.common.EventChannel +import org.moxxy.moxxy_native.TAG + +object NotificationStreamHandler : EventChannel.StreamHandler { + // The event sink to use for sending notification events to the service. + var sink: EventChannel.EventSink? = null + + override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { + // "register" the event sink + sink = events + + Log.d(TAG, "NotificationStreamHandler: Attached stream") + } + + override fun onCancel(arguments: Any?) { + sink = null + Log.d(TAG, "NotificationStreamHandler: Detached stream") + } +} diff --git a/android/src/main/kotlin/org/moxxy/moxxy_native/platform/KeyboardStreamHandler.kt b/android/src/main/kotlin/org/moxxy/moxxy_native/platform/KeyboardStreamHandler.kt index 1a28221..da488bc 100644 --- a/android/src/main/kotlin/org/moxxy/moxxy_native/platform/KeyboardStreamHandler.kt +++ b/android/src/main/kotlin/org/moxxy/moxxy_native/platform/KeyboardStreamHandler.kt @@ -8,7 +8,6 @@ import android.view.ViewTreeObserver import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import io.flutter.plugin.common.EventChannel -import org.moxxy.moxxy_native.MoxxyEventChannels import org.moxxy.moxxy_native.TAG object KeyboardStreamHandler : EventChannel.StreamHandler { @@ -67,7 +66,7 @@ object KeyboardStreamHandler : EventChannel.StreamHandler { } override fun onCancel(arguments: Any?) { - MoxxyEventChannels.notificationEventSink = null + sink = null Log.d(TAG, "KeyboardStreamHandler: Detached stream") } }