From 60cc08417063619b087db455389688b28c9d4ddb Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Mon, 5 Dec 2016 15:10:44 +0530 Subject: [PATCH] Modifications for Android Sense app. 1. Updated agent enrollment process. 2. Fixed ArrayIndexOutOfBound exception when enrolling. 3. Added Network data to the event stream. --- .../data/publisher/DataPublisherService.java | 14 ++++++ .../android/sense/data/publisher/Event.java | 45 +++++++++++++++++++ .../iot/android/sense/event/SenseService.java | 2 + .../streams/Sensor/SensorDataReader.java | 3 +- .../realtimesensor/RealTimeSensorReader.java | 6 +-- .../sense/util/SenseClientAsyncExecutor.java | 40 +++++------------ .../android/sense/util/SenseDataHolder.java | 13 ++++++ .../sense/util/SenseDataReceiverManager.java | 19 ++++++++ .../sense/util/dto/ApiApplicationKey.java | 43 ++++++++++++++++++ .../ApiApplicationRegistrationService.java | 2 +- .../sense/util/dto/TokenIssuerService.java | 2 +- 11 files changed, 155 insertions(+), 34 deletions(-) create mode 100644 components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/ApiApplicationKey.java diff --git a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/DataPublisherService.java b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/DataPublisherService.java index 28b515e98a..f0bdffae23 100755 --- a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/DataPublisherService.java +++ b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/DataPublisherService.java @@ -37,6 +37,7 @@ import org.wso2.carbon.iot.android.sense.event.streams.application.ApplicationDa import org.wso2.carbon.iot.android.sense.event.streams.audio.AudioData; import org.wso2.carbon.iot.android.sense.event.streams.battery.BatteryData; import org.wso2.carbon.iot.android.sense.event.streams.call.CallData; +import org.wso2.carbon.iot.android.sense.event.streams.data.NetworkData; import org.wso2.carbon.iot.android.sense.event.streams.screen.ScreenData; import org.wso2.carbon.iot.android.sense.event.streams.sms.SmsData; import org.wso2.carbon.iot.android.sense.speech.detector.util.ProcessWords; @@ -277,6 +278,19 @@ public class DataPublisherService extends Service { } SenseDataHolder.resetApplicationDataHolder(); + //Retrieve network data + List networkDataList = SenseDataHolder.getNetworkDataHolder(); + if (!networkDataList.isEmpty()) { + for (NetworkData networkData : networkDataList) { + Event event = new Event(); + event.setTimestamp(networkData.getTimeStamp()); + event.setDataType(networkData.getDataType()); + event.setDataReceived(networkData.getDataReceived()); + event.setDataSent(networkData.getDataSent()); + events.add(event); + } + } + SenseDataHolder.resetNetworkDataHolder(); //publish the data if (events.size() > 0 && LocalRegistry.isEnrolled(context)) { diff --git a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/Event.java b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/Event.java index 1bc91c3a32..62a3659761 100755 --- a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/Event.java +++ b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/Event.java @@ -61,6 +61,17 @@ public class Event { private String packageName; private String action; + /** + * Network data + * Time stamp + * Data type + * Data sent + * Data received. + * */ + private String dataType; + private long dataSent; + private long dataReceived; + private int getBattery() { return battery; } @@ -411,6 +422,36 @@ public class Event { this.confidence = confidence; } + public String getDataType() { + this.type = "data"; + return dataType; + } + + public void setDataType(String dataType) { + this.type = "data"; + this.dataType = dataType; + } + + public long getDataSent() { + this.type = "data"; + return dataSent; + } + + public void setDataSent(long dataSent) { + this.type = "data"; + this.dataSent = dataSent; + } + + public long getDataReceived() { + this.type = "data"; + return dataReceived; + } + + public void setDataReceived(long dataReceived) { + this.type = "data"; + this.dataReceived = dataReceived; + } + public JSONObject getEvent() throws JSONException { JSONObject jsonEvent = new JSONObject(); JSONObject jsonMetaData = new JSONObject(); @@ -502,6 +543,10 @@ public class Event { jsonPayloadData.put("application_name", getPackageName()); jsonPayloadData.put("action", getAction()); + //Network data + jsonPayloadData.put("data_type", getDataType()); + jsonPayloadData.put("data_received", getDataReceived()); + jsonPayloadData.put("data_sent", getDataSent()); jsonEvent.put("payloadData", jsonPayloadData); diff --git a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/SenseService.java b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/SenseService.java index 37e3356c68..ec62d8f4f4 100755 --- a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/SenseService.java +++ b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/SenseService.java @@ -56,6 +56,7 @@ public class SenseService extends Service { SenseDataReceiverManager.registerActivityDataReceiver(this); SenseDataReceiverManager.registerSmsDataReceiver(this); SenseDataReceiverManager.registerAppDataReceiver(this); + SenseDataReceiverManager.registerNetworkDataReceiver(this); //service will not be stopped until we manually stop the service return Service.START_NOT_STICKY; @@ -69,6 +70,7 @@ public class SenseService extends Service { SenseDataReceiverManager.unregisterActivityDataReceiver(this); SenseDataReceiverManager.unregisterSmsDataReceiver(this); SenseDataReceiverManager.unregisterAppDataReceiver(this); + SenseDataReceiverManager.unregisterNetworkDataReceiver(this); SenseWakeLock.releaseCPUWakeLock(); super.onDestroy(); diff --git a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/Sensor/SensorDataReader.java b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/Sensor/SensorDataReader.java index 3a57d4d48f..9df0447045 100755 --- a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/Sensor/SensorDataReader.java +++ b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/Sensor/SensorDataReader.java @@ -26,6 +26,7 @@ import org.wso2.carbon.iot.android.sense.realtimeviewer.sensorlisting.SupportedS import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -65,7 +66,7 @@ public class SensorDataReader extends DataReader implements SensorEventListener SensorData sensorInfo = senseDataStruct.get(sensor.getName()); sensorVector.add(sensorInfo); Log.d(TAG, "Sensor Name " + sensor.getName() + ", Type " + sensor.getType() + " " + - ", sensorValue :" + sensorInfo.getSensorValues()); + ", sensorValue :" + Arrays.toString(sensorInfo.getSensorValues())); } } catch (Throwable e) { Log.d(TAG, "error on sensors"); diff --git a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/realtimeviewer/event/realtimesensor/RealTimeSensorReader.java b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/realtimeviewer/event/realtimesensor/RealTimeSensorReader.java index fc4b474e65..6504e65ab4 100755 --- a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/realtimeviewer/event/realtimesensor/RealTimeSensorReader.java +++ b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/realtimeviewer/event/realtimesensor/RealTimeSensorReader.java @@ -42,9 +42,9 @@ public class RealTimeSensorReader implements SensorEventListener { RealTimeSensor realTimeSensor = new RealTimeSensor(); realTimeSensor.setName(supportedSensors.getType(event.sensor.getType()).toUpperCase()); - realTimeSensor.setValueX(event.values[0] + ""); - realTimeSensor.setValueY(event.values[1] + ""); - realTimeSensor.setValueZ(event.values[2] + ""); + realTimeSensor.setValueX(event.values == null ? "" : event.values[0] + ""); + realTimeSensor.setValueY(event.values == null || event.values.length <= 1 ? "" : event.values[1] + ""); + realTimeSensor.setValueZ(event.values == null || event.values.length <= 1 ? "" : event.values[2] + ""); TempStore.sensorDataMap.put(supportedSensors.getType(event.sensor.getType()), realTimeSensor); diff --git a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/SenseClientAsyncExecutor.java b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/SenseClientAsyncExecutor.java index 1d0e809cf5..6514fbb513 100755 --- a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/SenseClientAsyncExecutor.java +++ b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/SenseClientAsyncExecutor.java @@ -23,6 +23,7 @@ import org.wso2.carbon.iot.android.sense.constants.SenseConstants; import org.wso2.carbon.iot.android.sense.util.dto.AccessTokenInfo; import org.wso2.carbon.iot.android.sense.util.dto.AndroidConfiguration; import org.wso2.carbon.iot.android.sense.util.dto.AndroidSenseManagerService; +import org.wso2.carbon.iot.android.sense.util.dto.ApiApplicationKey; import org.wso2.carbon.iot.android.sense.util.dto.ApiApplicationRegistrationService; import org.wso2.carbon.iot.android.sense.util.dto.ApiRegistrationProfile; import org.wso2.carbon.iot.android.sense.util.dto.DynamicClientRegistrationService; @@ -98,41 +99,24 @@ public class SenseClientAsyncExecutor extends AsyncTask activityDataHolder; private static List smsDataHolder; private static List applicationDataHolder; + private static List networkDataHolder; //LocationData gps; @@ -141,6 +143,13 @@ public class SenseDataHolder { return applicationDataHolder; } + public static List getNetworkDataHolder() { + if (networkDataHolder == null) { + networkDataHolder = new CopyOnWriteArrayList<>(); + } + return networkDataHolder; + } + public static void resetSensorDataHolder() { sensorDataHolder = null; } @@ -188,4 +197,8 @@ public class SenseDataHolder { public static void resetApplicationDataHolder() { applicationDataHolder = null; } + + public static void resetNetworkDataHolder() { + networkDataHolder = null; + } } diff --git a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/SenseDataReceiverManager.java b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/SenseDataReceiverManager.java index a72532464a..ed3abacd75 100644 --- a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/SenseDataReceiverManager.java +++ b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/SenseDataReceiverManager.java @@ -35,6 +35,7 @@ import org.wso2.carbon.iot.android.sense.event.streams.activity.ActivityReceiver import org.wso2.carbon.iot.android.sense.event.streams.application.ApplicationDataReceiver; import org.wso2.carbon.iot.android.sense.event.streams.battery.BatteryDataReceiver; import org.wso2.carbon.iot.android.sense.event.streams.call.CallDataReceiver; +import org.wso2.carbon.iot.android.sense.event.streams.data.NetworkDataReceiver; import org.wso2.carbon.iot.android.sense.event.streams.screen.ScreenDataReceiver; import org.wso2.carbon.iot.android.sense.event.streams.sms.SmsDataReceiver; @@ -51,6 +52,8 @@ public class SenseDataReceiverManager { private static ApplicationDataReceiver appDataReceiver; + private static NetworkDataReceiver networkDataReceiver; + private SenseDataReceiverManager() { } @@ -171,5 +174,21 @@ public class SenseDataReceiverManager { context.unregisterReceiver(appDataReceiver); appDataReceiver = null; } + } public static void registerNetworkDataReceiver(Context context) { + if (networkDataReceiver == null) { + networkDataReceiver = new NetworkDataReceiver(); + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(Intent.ACTION_MANAGE_NETWORK_USAGE); + context.registerReceiver(networkDataReceiver, intentFilter); + } + } + + public static void unregisterNetworkDataReceiver(Context context) { + if (networkDataReceiver != null) { + context.unregisterReceiver(networkDataReceiver); + } + networkDataReceiver = null; } + + } diff --git a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/ApiApplicationKey.java b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/ApiApplicationKey.java new file mode 100644 index 0000000000..e4130755e4 --- /dev/null +++ b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/ApiApplicationKey.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2016, 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.iot.android.sense.util.dto; + +/** + * This holds api application consumer key and secret. + */ +public class ApiApplicationKey { + private String client_id; + private String client_secret; + + public String getConsumerKey() { + return this.client_id; + } + + public void setClient_id(String consumerKey) { + this.client_id = consumerKey; + } + + public String getConsumerSecret() { + return this.client_secret; + } + + public void setClient_secret(String consumerSecret) { + this.client_secret = consumerSecret; + } +} diff --git a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/ApiApplicationRegistrationService.java b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/ApiApplicationRegistrationService.java index e8cd26148b..64d4aa66f9 100755 --- a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/ApiApplicationRegistrationService.java +++ b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/ApiApplicationRegistrationService.java @@ -21,5 +21,5 @@ public interface ApiApplicationRegistrationService { @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) - String register(ApiRegistrationProfile registrationProfile); + ApiApplicationKey register(ApiRegistrationProfile registrationProfile); } diff --git a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/TokenIssuerService.java b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/TokenIssuerService.java index 161f07f45c..a23ccb8eb9 100755 --- a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/TokenIssuerService.java +++ b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/TokenIssuerService.java @@ -29,5 +29,5 @@ public interface TokenIssuerService { @POST @Produces(MediaType.APPLICATION_JSON) AccessTokenInfo getToken(@QueryParam("grant_type") String grant, @QueryParam("username") String username, - @QueryParam("password") String password); + @QueryParam("password") String password, @QueryParam("deviceId") String deviceId); }