From d8762a2f851bbaba62d02afabf99b9971269ee98 Mon Sep 17 00:00:00 2001 From: Dallas Gutauckis Date: Fri, 13 May 2016 11:09:24 -0400 Subject: [PATCH] Move MParticle-specific messaging logic into a router for reusability --- .../messaging/MPMessagingRouter.java | 40 +++++++++++++ .../messaging/PushAnalyticsReceiver.java | 59 +++---------------- .../PushAnalyticsReceiverCallback.java | 22 +++++++ 3 files changed, 69 insertions(+), 52 deletions(-) create mode 100644 android-core/src/main/java/com/mparticle/messaging/MPMessagingRouter.java create mode 100644 android-core/src/main/java/com/mparticle/messaging/PushAnalyticsReceiverCallback.java diff --git a/android-core/src/main/java/com/mparticle/messaging/MPMessagingRouter.java b/android-core/src/main/java/com/mparticle/messaging/MPMessagingRouter.java new file mode 100644 index 000000000..d77ec8568 --- /dev/null +++ b/android-core/src/main/java/com/mparticle/messaging/MPMessagingRouter.java @@ -0,0 +1,40 @@ +package com.mparticle.messaging; + +import android.content.Context; +import android.content.Intent; + +import com.mparticle.MPService; + +public class MPMessagingRouter { + /** + * Parses the incoming intent and delegates functionality to the given {@code callback} if appropriate. This implementation checks for + * MParticle-specific actions and will not handle messages outside of that scope. MParticle actions can be found in + * {@link MPMessagingAPI} {@code BROADCAST_*} constants + * + * @param context + * @param intent + * @param callback + * @return {@code true} if the {@link Intent} was handled by MParticle + */ + public static boolean onReceive(Context context, Intent intent, PushAnalyticsReceiverCallback callback) { + if (MPMessagingAPI.BROADCAST_NOTIFICATION_TAPPED.equalsIgnoreCase(intent.getAction())) { + AbstractCloudMessage message = intent.getParcelableExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA); + CloudAction action = intent.getParcelableExtra(MPMessagingAPI.CLOUD_ACTION_EXTRA); + if (!callback.onNotificationTapped(message, action)) { + intent.putExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA, message); + intent.putExtra(MPMessagingAPI.CLOUD_ACTION_EXTRA, action); + MPService.runIntentInService(context, intent); + } + return true; + } else if (MPMessagingAPI.BROADCAST_NOTIFICATION_RECEIVED.equalsIgnoreCase(intent.getAction())) { + AbstractCloudMessage message = intent.getParcelableExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA); + if (!callback.onNotificationReceived(message)) { + intent.putExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA, message); + MPService.runIntentInService(context, intent); + } + return true; + } + + return false; + } +} diff --git a/android-core/src/main/java/com/mparticle/messaging/PushAnalyticsReceiver.java b/android-core/src/main/java/com/mparticle/messaging/PushAnalyticsReceiver.java index e215c46cc..1ca722f07 100644 --- a/android-core/src/main/java/com/mparticle/messaging/PushAnalyticsReceiver.java +++ b/android-core/src/main/java/com/mparticle/messaging/PushAnalyticsReceiver.java @@ -4,8 +4,6 @@ import android.content.Context; import android.content.Intent; -import com.mparticle.MPService; - /** * BroadcastReceiver to be used to listen for, manipulate, and react to GCM notifications. * @@ -26,63 +24,20 @@ * @see #onNotificationTapped(AbstractCloudMessage, CloudAction) * */ -public class PushAnalyticsReceiver extends BroadcastReceiver { - private Context mContext = null; - +public class PushAnalyticsReceiver extends BroadcastReceiver implements PushAnalyticsReceiverCallback { @Override public final void onReceive(Context context, Intent intent) { - mContext = context; - if (MPMessagingAPI.BROADCAST_NOTIFICATION_TAPPED.equalsIgnoreCase(intent.getAction())){ - AbstractCloudMessage message = intent.getParcelableExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA); - CloudAction action = intent.getParcelableExtra(MPMessagingAPI.CLOUD_ACTION_EXTRA); - if (!onNotificationTapped(message, action)){ - intent.putExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA, message); - intent.putExtra(MPMessagingAPI.CLOUD_ACTION_EXTRA, action); - MPService.runIntentInService(context, intent); - } - return; - } else if (MPMessagingAPI.BROADCAST_NOTIFICATION_RECEIVED.equalsIgnoreCase(intent.getAction())){ - AbstractCloudMessage message = intent.getParcelableExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA); - if (!onNotificationReceived(message)){ - intent.putExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA, message); - MPService.runIntentInService(context, intent); - } - return; - } - - } - - /** - * Helper method to retrieve the Context that was passed into this BroadcastReceiver - * - * @return Context object - */ - protected final Context getContext(){ - return mContext; + MPMessagingRouter.onReceive(context, intent, this); } - /** - * Override this method to listen for when a notification has been received. - * - * - * @param message The message that was received. Depending on the push provider, could be either a {@link com.mparticle.messaging.MPCloudNotificationMessage} or a {@link com.mparticle.messaging.ProviderCloudMessage} - * @return True if you would like to handle this notification, False if you would like the mParticle to generate and show a {@link android.app.Notification}. - */ - protected boolean onNotificationReceived(AbstractCloudMessage message){ + @Override + public boolean onNotificationReceived(AbstractCloudMessage message) { return false; } - /** - * Override this method to listen for when a notification has been tapped or acted on. - * - * - * @param message The message that was tapped. Depending on the push provider, could be either a {@link com.mparticle.messaging.MPCloudNotificationMessage} or a {@link com.mparticle.messaging.ProviderCloudMessage} - * @param action The action that the user acted on. - * @return True if you would like to consume this tap/action, False if the mParticle SDK should attempt to handle it. - */ - protected boolean onNotificationTapped(AbstractCloudMessage message, CloudAction action){ + @Override + public boolean onNotificationTapped(AbstractCloudMessage message, CloudAction action) { return false; } - -} +} \ No newline at end of file diff --git a/android-core/src/main/java/com/mparticle/messaging/PushAnalyticsReceiverCallback.java b/android-core/src/main/java/com/mparticle/messaging/PushAnalyticsReceiverCallback.java new file mode 100644 index 000000000..b15f6d0fc --- /dev/null +++ b/android-core/src/main/java/com/mparticle/messaging/PushAnalyticsReceiverCallback.java @@ -0,0 +1,22 @@ +package com.mparticle.messaging; + +public interface PushAnalyticsReceiverCallback { + /** + * Override this method to listen for when a notification has been received. + * + * + * @param message The message that was received. Depending on the push provider, could be either a {@link com.mparticle.messaging.MPCloudNotificationMessage} or a {@link com.mparticle.messaging.ProviderCloudMessage} + * @return True if you would like to handle this notification, False if you would like the mParticle to generate and show a {@link android.app.Notification}. + */ + boolean onNotificationReceived(AbstractCloudMessage message); + + /** + * Override this method to listen for when a notification has been tapped or acted on. + * + * + * @param message The message that was tapped. Depending on the push provider, could be either a {@link com.mparticle.messaging.MPCloudNotificationMessage} or a {@link com.mparticle.messaging.ProviderCloudMessage} + * @param action The action that the user acted on. + * @return True if you would like to consume this tap/action, False if the mParticle SDK should attempt to handle it. + */ + boolean onNotificationTapped(AbstractCloudMessage message, CloudAction action); +}