added GCM support to android

merge-requests/1/head
harshanl 9 years ago
parent 5a797d4de1
commit b401d7f61e

@ -61,28 +61,28 @@
javax.xml.bind.*,
javax.naming,
javax.sql,
javax.xml.bind.annotation.*,
javax.xml.parsers,
org.w3c.dom,
org.wso2.carbon.core,
org.wso2.carbon.context,
org.wso2.carbon.utils.*,
org.wso2.carbon.device.mgt.common.*,
org.wso2.carbon.ndatasource.core,
org.wso2.carbon.policy.mgt.common.*,
org.wso2.carbon.policy.mgt.core.*,
org.wso2.carbon.registry.core,
org.wso2.carbon.registry.core.exceptions,
org.wso2.carbon.registry.core.service,
org.wso2.carbon.registry.core.session,
org.wso2.carbon.registry.api,
org.wso2.carbon.device.mgt.extensions.license.mgt.registry,
com.google.gson.*
com.google.android.gcm.*,
com.google.gson,
org.json.simple,
org.json.simple.parser
</Import-Package>
<Export-Package>
!org.wso2.carbon.device.mgt.mobile.internal,
!org.wso2.carbon.device.mgt.mobile.impl,
org.wso2.carbon.device.mgt.mobile.*,
com.google.android.gcm.*,
</Export-Package>
</instructions>
</configuration>
@ -170,5 +170,9 @@
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>com.google.android.gcm</groupId>
<artifactId>gcm-server</artifactId>
</dependency>
</dependencies>
</project>

@ -28,6 +28,8 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.mobile.impl.android.gcm.GCMService;
import org.wso2.carbon.device.mgt.mobile.internal.MobileDeviceManagementDataHolder;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData;
import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature;
@ -43,7 +45,10 @@ public class AndroidPolicyMonitoringService implements PolicyMonitoringService {
@Override
public void notifyDevices(List<Device> list) throws PolicyComplianceException {
GCMService gcmService = MobileDeviceManagementDataHolder.getInstance().getGCMService();
if (gcmService.isGCMEnabled()) {
gcmService.sendNotification("POLICY_BUNDLE", list);
}
}
@Override

@ -0,0 +1,140 @@
/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.mobile.impl.android.gcm;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Sender;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry;
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidPluginConstants;
import org.wso2.carbon.device.mgt.mobile.internal.MobileDeviceManagementDataHolder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* GCM notification service implementation for Android platform.
*/
public class GCMService {
private static final Log log = LogFactory.getLog(GCMService.class);
public static final String GCM_APIKEY = "gcmAPIKey";
public static final String NOTIFIER_TYPE = "notifierType";
public static final String GCM_NOTIFIER_CODE = "2";
private static HashMap<Integer,TenantConfiguration> tenantConfigurationCache = new HashMap<>();
public boolean isGCMEnabled() {
String notifierType = getConfigurationProperty(NOTIFIER_TYPE);
if (GCM_NOTIFIER_CODE.equals(notifierType)) {
return true;
}
return false;
}
public void sendNotification(String messageData, Device device) {
int seconds = 60;
Sender sender = new Sender(getConfigurationProperty(GCM_APIKEY));
Message message =
new Message.Builder().timeToLive(seconds).delayWhileIdle(false).addData("data", messageData).build();
try {
sender.send(message, getGCMToken(device.getProperties()), 5);
} catch (IOException e) {
log.error("Exception occurred while sending the GCM notification.",e);
}
}
public void sendNotification(String messageData, List<Device> devices) {
int seconds = 60;
Sender sender = new Sender(getConfigurationProperty(GCM_APIKEY));
Message message =
new Message.Builder().timeToLive(seconds).delayWhileIdle(false).addData("data", messageData).build();
try {
sender.send(message, getGCMTokens(devices), 5);
} catch (IOException e) {
log.error("Exception occurred while sending the GCM notification.",e);
}
}
private static List<String> getGCMTokens(List<Device> devices) {
List<String> tokens = new ArrayList<>();
for (Device device : devices) {
tokens.add(getGCMToken(device.getProperties()));
}
return tokens;
}
private static String getGCMToken(List<Device.Property> properties) {
String gcmToken = null;
for (Device.Property property : properties) {
if (AndroidPluginConstants.GCM_TOKEN.equals(property.getName())) {
gcmToken = property.getValue();
break;
}
}
return gcmToken;
}
private static String getConfigurationProperty(String property) {
DeviceManagementService androidDMService = MobileDeviceManagementDataHolder.getInstance().
getAndroidDeviceManagementService();
try {
//Get the TenantConfiguration from cache if not we'll get it from DM service
TenantConfiguration tenantConfiguration = getTenantConfigurationFromCache();
if (tenantConfiguration == null) {
tenantConfiguration = androidDMService.getDeviceManager().getConfiguration();
if (tenantConfiguration != null) {
addTenantConfigurationToCache(tenantConfiguration);
}
}
if (tenantConfiguration != null) {
List<ConfigurationEntry> configs = tenantConfiguration.getConfiguration();
for (ConfigurationEntry entry : configs) {
if (property.equals(entry.getName())) {
return (String) entry.getValue();
}
}
}
return "";
} catch (DeviceManagementException e) {
log.error("Exception occurred while fetching the tenant-config.",e);
}
return null;
}
private static void addTenantConfigurationToCache(TenantConfiguration tenantConfiguration) {
tenantConfigurationCache.put(getTenantId(), tenantConfiguration);
}
private static TenantConfiguration getTenantConfigurationFromCache() {
return tenantConfigurationCache.get(getTenantId());
}
private static int getTenantId() {
return CarbonContext.getThreadLocalCarbonContext().getTenantId();
}
}

@ -18,6 +18,8 @@
package org.wso2.carbon.device.mgt.mobile.internal;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.mobile.impl.android.gcm.GCMService;
import org.wso2.carbon.registry.core.service.RegistryService;
/**
@ -26,6 +28,8 @@ import org.wso2.carbon.registry.core.service.RegistryService;
public class MobileDeviceManagementDataHolder {
private RegistryService registryService;
private DeviceManagementService androidDeviceManagementService;
private GCMService gcmService;
private static MobileDeviceManagementDataHolder thisInstance = new MobileDeviceManagementDataHolder();
@ -44,4 +48,20 @@ public class MobileDeviceManagementDataHolder {
this.registryService = registryService;
}
public DeviceManagementService getAndroidDeviceManagementService() {
return androidDeviceManagementService;
}
public void setAndroidDeviceManagementService(
DeviceManagementService androidDeviceManagementService) {
this.androidDeviceManagementService = androidDeviceManagementService;
}
public GCMService getGCMService() {
return gcmService;
}
public void setGCMService(GCMService gcmService) {
this.gcmService = gcmService;
}
}

@ -32,6 +32,7 @@ import org.wso2.carbon.device.mgt.mobile.dao.AbstractMobileDeviceManagementDAOFa
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidDeviceManagementService;
import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidPolicyMonitoringService;
import org.wso2.carbon.device.mgt.mobile.impl.android.gcm.GCMService;
import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManagementService;
import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsPolicyMonitoringService;
import org.wso2.carbon.ndatasource.core.DataSourceService;
@ -61,6 +62,7 @@ public class MobileDeviceManagementServiceComponent {
private ServiceRegistration androidServiceRegRef;
private ServiceRegistration windowsServiceRegRef;
private ServiceRegistration gcmServiceRegRef;
private static final Log log = LogFactory.getLog(MobileDeviceManagementServiceComponent.class);
@ -98,14 +100,20 @@ public class MobileDeviceManagementServiceComponent {
log.error("Exception occurred while initializing mobile device management database schema", e);
}
}
DeviceManagementService androidDeviceManagementService = new AndroidDeviceManagementService();
GCMService gcmService = new GCMService();
androidServiceRegRef =
bundleContext.registerService(DeviceManagementService.class.getName(),
new AndroidDeviceManagementService(), null);
androidDeviceManagementService, null);
windowsServiceRegRef =
bundleContext.registerService(DeviceManagementService.class.getName(),
new WindowsDeviceManagementService(), null);
gcmServiceRegRef =
bundleContext.registerService(GCMService.class.getName(), gcmService, null);
// Policy management service
bundleContext.registerService(PolicyMonitoringService.class,
@ -113,6 +121,9 @@ public class MobileDeviceManagementServiceComponent {
bundleContext.registerService(PolicyMonitoringService.class,
new WindowsPolicyMonitoringService(), null);
MobileDeviceManagementDataHolder.getInstance().setAndroidDeviceManagementService(
androidDeviceManagementService);
MobileDeviceManagementDataHolder.getInstance().setGCMService(gcmService);
if (log.isDebugEnabled()) {
log.debug("Mobile Device Management Service Component has been successfully activated");
}
@ -132,6 +143,9 @@ public class MobileDeviceManagementServiceComponent {
if (windowsServiceRegRef != null) {
windowsServiceRegRef.unregister();
}
if (gcmServiceRegRef != null) {
gcmServiceRegRef.unregister();
}
if (log.isDebugEnabled()) {
log.debug(
"Mobile Device Management Service Component has been successfully de-activated");

@ -4,7 +4,7 @@
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `AD_DEVICE` (
`DEVICE_ID` VARCHAR(45) NOT NULL,
`GCM_TOKEN` VARCHAR(45) NULL DEFAULT NULL,
`GCM_TOKEN` VARCHAR(1000) NULL DEFAULT NULL,
`DEVICE_INFO` VARCHAR(8000) NULL DEFAULT NULL,
`IMEI` VARCHAR(45) NULL DEFAULT NULL,
`IMSI` VARCHAR(45) NULL DEFAULT NULL,

@ -3,7 +3,7 @@
-- -----------------------------------------------------
CREATE TABLE AD_DEVICE (
DEVICE_ID VARCHAR(45) NOT NULL,
GCM_TOKEN VARCHAR(45) NULL DEFAULT NULL,
GCM_TOKEN VARCHAR(1000) NULL DEFAULT NULL,
DEVICE_INFO VARCHAR(8000) NULL DEFAULT NULL,
IMEI VARCHAR(45) NULL DEFAULT NULL,
IMSI VARCHAR(45) NULL DEFAULT NULL,

@ -3,7 +3,7 @@
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `AD_DEVICE` (
`DEVICE_ID` VARCHAR(45) NOT NULL,
`GCM_TOKEN` VARCHAR(45) NULL DEFAULT NULL,
`GCM_TOKEN` VARCHAR(1000) NULL DEFAULT NULL,
`DEVICE_INFO` VARCHAR(8000) NULL DEFAULT NULL,
`IMEI` VARCHAR(45) NULL DEFAULT NULL,
`IMSI` VARCHAR(45) NULL DEFAULT NULL,

@ -3,8 +3,8 @@
-- -----------------------------------------------------
CREATE TABLE AD_DEVICE (
DEVICE_ID VARCHAR(45) NOT NULL ,
DEVICE_INFO VARCHAR(1000) DEFAULT NULL,
GCM_TOKEN VARCHAR(45) DEFAULT NULL,
DEVICE_INFO VARCHAR(8000) DEFAULT NULL,
GCM_TOKEN VARCHAR(1000) DEFAULT NULL,
IMEI VARCHAR(45) DEFAULT NULL,
IMSI VARCHAR(45) DEFAULT NULL,
OS_VERSION VARCHAR(45) DEFAULT NULL,

@ -4,7 +4,7 @@
CREATE TABLE IF NOT EXISTS AD_DEVICE (
DEVICE_ID VARCHAR(45) NOT NULL ,
DEVICE_INFO TEXT NULL DEFAULT NULL,
GCM_TOKEN VARCHAR(45) NULL DEFAULT NULL,
GCM_TOKEN VARCHAR(1000) NULL DEFAULT NULL,
IMEI VARCHAR(45) NULL DEFAULT NULL,
IMSI VARCHAR(45) NULL DEFAULT NULL,
OS_VERSION VARCHAR(45) NULL DEFAULT NULL,

@ -528,6 +528,11 @@
<artifactId>gson</artifactId>
<version>${google.gson.version}</version>
</dependency>
<dependency>
<groupId>com.google.android.gcm</groupId>
<artifactId>gcm-server</artifactId>
<version>${gcm.server.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
@ -595,7 +600,7 @@
<apache.wss4j.version>2.0.0</apache.wss4j.version>
<codehaus.plexus.version>3.0.21</codehaus.plexus.version>
<google.gson.version>2.2.4</google.gson.version>
<gcm.server.version>1.0.2</gcm.server.version>
</properties>
<scm>
@ -785,5 +790,10 @@
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>gcm-server-repository</id>
<name>GCM Server repository - GitHub</name>
<url>https://github.com/slorber/gcm-server-repository/raw/master/releases/</url>
</repository>
</repositories>
</project>

Loading…
Cancel
Save