From 479deb8a493dd2aca0b37b493d2b05feb8c84dd0 Mon Sep 17 00:00:00 2001 From: harshanl Date: Mon, 5 Sep 2016 17:46:08 +0530 Subject: [PATCH] Added logic to switch between GCM & LOCAL notification provider in Android --- .../impl/AndroidDeviceManagementService.java | 45 +++++++++++++++++-- .../impl/util/AndroidPluginConstants.java | 12 ++++- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/AndroidDeviceManagementService.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/AndroidDeviceManagementService.java index aadd3783f..bed25a980 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/AndroidDeviceManagementService.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/AndroidDeviceManagementService.java @@ -18,17 +18,20 @@ package org.wso2.carbon.device.mgt.mobile.android.impl; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManager; import org.wso2.carbon.device.mgt.common.ProvisioningConfig; -import org.wso2.carbon.device.mgt.common.app.mgt.Application; -import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; -import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; +import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.mobile.android.impl.util.AndroidPluginConstants; +import org.wso2.carbon.device.mgt.mobile.android.internal.AndroidDeviceManagementDataHolder; +import java.util.HashMap; import java.util.List; /** @@ -36,9 +39,13 @@ import java.util.List; */ public class AndroidDeviceManagementService implements DeviceManagementService { + private static final Log log = LogFactory.getLog(AndroidDeviceManagementService.class); private DeviceManager deviceManager; public static final String DEVICE_TYPE_ANDROID = "android"; private static final String SUPER_TENANT_DOMAIN = "carbon.super"; + private static final String NOTIFIER_PROPERTY = "notifierType"; + private static final String GCM_API_KEY = "gcmAPIKey"; + private static final String GCM_SENDER_ID = "gcmSenderId"; @Override public String getType() { @@ -67,7 +74,37 @@ public class AndroidDeviceManagementService implements DeviceManagementService { @Override public PushNotificationConfig getPushNotificationConfig() { + try { + DeviceManagementService deviceManagementService = AndroidDeviceManagementDataHolder.getInstance(). + getAndroidDeviceManagementService(); + if (deviceManagementService != null && deviceManagementService.getDeviceManager() != null) { + PlatformConfiguration androidConfig = deviceManagementService.getDeviceManager().getConfiguration(); + if (androidConfig != null) { + List configuration = androidConfig.getConfiguration(); + String notifierValue = this.getConfigProperty(configuration, NOTIFIER_PROPERTY); + if (notifierValue != null && !notifierValue.isEmpty()) { + int notifierType = Integer.parseInt(notifierValue); + if (notifierType == 2) { + HashMap config = new HashMap<>(); + config.put(GCM_API_KEY, this.getConfigProperty(configuration, GCM_API_KEY)); + config.put(GCM_SENDER_ID, this.getConfigProperty(configuration, GCM_SENDER_ID)); + return new PushNotificationConfig(AndroidPluginConstants.NotifierType.GCM, config); + } + } + } + } + } catch (DeviceManagementException e) { + log.error("Unable to get the Android platform configuration from registry."); + } return null; } + private String getConfigProperty(List configs, String propertyName) { + for (ConfigurationEntry entry : configs) { + if (propertyName.equals(entry.getName())) { + return entry.getValue().toString(); + } + } + return null; + } } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/util/AndroidPluginConstants.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/util/AndroidPluginConstants.java index 31fea9d5d..376932449 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/util/AndroidPluginConstants.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/util/AndroidPluginConstants.java @@ -21,7 +21,7 @@ package org.wso2.carbon.device.mgt.mobile.android.impl.util; /** * Defines constants used by android plugin. */ -public class AndroidPluginConstants { +public final class AndroidPluginConstants { //Properties related to AD_DEVICE table public static final String DEVICE_ID = "DEVICE_ID"; @@ -45,4 +45,12 @@ public class AndroidPluginConstants { public static final String ANDROID_FEATURE_NAME = "NAME"; public static final String ANDROID_FEATURE_DESCRIPTION = "DESCRIPTION"; -} + public static final class NotifierType { + private NotifierType() { + throw new AssertionError(); + } + + public static final String GCM = "GCM"; + public static final String LOCAL = "LOCAL"; + } +} \ No newline at end of file