From ab418dbde37f4569963ac86cf7d718d7e6ed4843 Mon Sep 17 00:00:00 2001 From: Gobinath Date: Wed, 30 Nov 2016 14:55:53 +0530 Subject: [PATCH] Add new sensors to the android sense plugin Incoming and outgoing calls tracker Screen on off detector SMS tracker Audio playing and headset tracker Application install and uninstall detector User activity detector --- .../app/build.gradle | 1 + .../app/src/main/AndroidManifest.xml | 18 +- .../data/publisher/DataPublisherService.java | 106 ++++++++- .../android/sense/data/publisher/Event.java | 212 +++++++++++++++++- .../iot/android/sense/event/SenseService.java | 21 +- .../event/streams/SenseDataCollector.java | 6 +- .../event/streams/activity/ActivityData.java | 67 ++++++ .../streams/activity/ActivityReceiver.java | 54 +++++ .../streams/application/ApplicationData.java | 61 +++++ .../application/ApplicationDataReceiver.java | 45 ++++ .../sense/event/streams/audio/AudioData.java | 58 +++++ .../event/streams/audio/AudioDataReader.java | 50 +++++ .../event/streams/battery/BatteryData.java | 18 ++ .../streams/battery/BatteryDataReceiver.java | 8 +- .../sense/event/streams/call/CallData.java | 70 ++++++ .../event/streams/call/CallDataReceiver.java | 87 +++++++ .../event/streams/screen/ScreenData.java | 55 +++++ .../streams/screen/ScreenDataReceiver.java | 32 +++ .../sense/event/streams/sms/SmsData.java | 58 +++++ .../event/streams/sms/SmsDataReceiver.java | 49 ++++ .../sensorlisting/SupportedSensors.java | 3 + .../android/sense/util/SenseDataHolder.java | 111 +++++++-- .../sense/util/SenseDataReceiverManager.java | 175 +++++++++++++++ 23 files changed, 1319 insertions(+), 46 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/event/streams/activity/ActivityData.java 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/event/streams/activity/ActivityReceiver.java 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/event/streams/application/ApplicationData.java 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/event/streams/application/ApplicationDataReceiver.java 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/event/streams/audio/AudioData.java 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/event/streams/audio/AudioDataReader.java 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/event/streams/call/CallData.java 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/event/streams/call/CallDataReceiver.java 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/event/streams/screen/ScreenData.java 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/event/streams/screen/ScreenDataReceiver.java 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/event/streams/sms/SmsData.java 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/event/streams/sms/SmsDataReceiver.java 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/SenseDataReceiverManager.java diff --git a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/build.gradle b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/build.gradle index 0c360c9b12..9f6b11adf4 100755 --- a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/build.gradle +++ b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/build.gradle @@ -61,4 +61,5 @@ dependencies { compile 'org.altbeacon:android-beacon-library:2.8.1' compile 'uk.co.alt236:easycursor-android:1.0.0' compile 'uk.co.alt236:bluetooth-le-library-android:1.0.0' + compile 'com.google.android.gms:play-services-location:9.4.0' } diff --git a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/AndroidManifest.xml b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/AndroidManifest.xml index 7ba9b334cc..2b0f926dad 100755 --- a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/AndroidManifest.xml +++ b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/AndroidManifest.xml @@ -11,11 +11,16 @@ - - + + + + + + + + + @@ -53,13 +60,6 @@ - - - - - batteryDataMap = SenseDataHolder.getBatteryDataHolder(); if (!batteryDataMap.isEmpty()) { for (BatteryData batteryData : batteryDataMap) { Event event = new Event(); event.setTimestamp(batteryData.getTimestamp()); event.setBattery(batteryData.getLevel()); + event.setBatteryTemperature(batteryData.getTemperature()); + event.setBatteryStatus(batteryData.getStatus()); + event.setBatteryState(batteryData.getState().toString()); events.add(event); } } SenseDataHolder.resetBatteryDataHolder(); + //retrieve location data. List locationDataMap = SenseDataHolder.getLocationDataHolder(); @@ -132,7 +140,7 @@ public class DataPublisherService extends Service { event.setGps(new double[]{locationData.getLatitude(), locationData.getLongitude()}); events.add(event); } - } + } SenseDataHolder.resetLocationDataHolder(); //retrieve speed data. @@ -186,6 +194,90 @@ public class DataPublisherService extends Service { } } SenseDataHolder.resetWordDataHolder(); + + // retrieve call data. + List callDataList = SenseDataHolder.getCallDataHolder(); + if (!callDataList.isEmpty()) { + for (CallData callData : callDataList) { + Event event = new Event(); + event.setCallNumber(callData.getPhoneNumber()); + event.setCallType(callData.getType().toString().toLowerCase()); + event.setCallStartTime(callData.getStartTime()); + event.setCallEndTime(callData.getEndTime()); + event.setTimestamp(callData.getStartTime()); + + events.add(event); + } + } + SenseDataHolder.resetCallDataHolder(); + + // retrieve screen data. + List screenDataList = SenseDataHolder.getScreenDataHolder(); + if (!screenDataList.isEmpty()) { + for (ScreenData screenData : screenDataList) { + Event event = new Event(); + event.setScreenState(screenData.getAction()); + event.setTimestamp(screenData.getTimestamp()); + + events.add(event); + } + } + SenseDataHolder.resetScreenDataHolder(); + + // retrieve audio data. + List audioDataList = SenseDataHolder.getAudioDataHolder(); + if (!audioDataList.isEmpty()) { + for (AudioData audioData : audioDataList) { + Event event = new Event(); + event.setTimestamp(audioData.getTimestamp()); + event.setAudioPlaying(audioData.isPlaying()); + event.setHeadsetOn(audioData.isHeadsetOn()); + event.setMusicVolume(audioData.getMusicVolume()); + events.add(event); + } + } + SenseDataHolder.resetAudioDataHolder(); + + // retrieve activity data. + List activityDataList = SenseDataHolder.getActivityDataHolder(); + if (!activityDataList.isEmpty()) { + for (ActivityData activityData : activityDataList) { + Event event = new Event(); + event.setTimestamp(activityData.getTimestamp()); + event.setActivityType(activityData.getActivity()); + event.setConfidence(activityData.getConfidence()); + events.add(event); + } + } + SenseDataHolder.resetActivityDataHolder(); + + // retrieve sms data. + List smsDataList = SenseDataHolder.getSmsDataHolder(); + if (!smsDataList.isEmpty()) { + for (SmsData smsData : smsDataList) { + Event event = new Event(); + event.setTimestamp(smsData.getTimestamp()); + event.setSmsNumber(smsData.getPhoneNumber()); + events.add(event); + } + } + SenseDataHolder.resetSmsDataHolder(); + + + // retrieve application data. + List appDataList = SenseDataHolder.getApplicationDataHolder(); + if (!appDataList.isEmpty()) { + for (ApplicationData appData : appDataList) { + Event event = new Event(); + event.setTimestamp(appData.getTimestamp()); + event.setPackageName(appData.getPackageName()); + event.setAction(appData.getAction().toString()); + events.add(event); + } + } + SenseDataHolder.resetApplicationDataHolder(); + + //publish the data if (events.size() > 0 && LocalRegistry.isEnrolled(context)) { String user = LocalRegistry.getUsername(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 5b5446d707..1bc91c3a32 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 @@ -16,6 +16,12 @@ public class Event { private float accelerometer[] = new float[]{0, 0, 0}; //x,y,z private float magnetic[] = new float[]{0, 0, 0};; //x,y,z private float gyroscope[] = new float[]{0, 0, 0};; //x,y,z + private int batteryTemperature; + private int batteryStatus; + /** + * LOW or OK + */ + private String batteryState; private float light; private float pressure; private float proximity; @@ -32,8 +38,28 @@ public class Event { private int beaconMinor; private int beaconUuid; private String beaconProximity; - - + private String callNumber; + private String callType; + private long callStartTime; + private long callEndTime; + + /** + * State of the phone display. + * Possible values are: + * on + * off + * unknown + */ + private String screenState; + + private boolean audioPlaying; + private boolean headsetOn; + private int musicVolume; + private int activityType; + private int confidence; + private String smsNumber; + private String packageName; + private String action; private int getBattery() { return battery; @@ -44,6 +70,33 @@ public class Event { this.battery = battery; } + public int getBatteryTemperature() { + return batteryTemperature; + } + + public void setBatteryTemperature(int batteryTemperature) { + this.type = "battery"; // Type is battery + this.batteryTemperature = batteryTemperature; + } + + public int getBatteryStatus() { + return batteryStatus; + } + + public void setBatteryStatus(int batteryStatus) { + this.type = "battery"; // Type is battery + this.batteryStatus = batteryStatus; + } + + public String getBatteryState() { + return batteryState != null ? batteryState : ""; + } + + public void setBatteryState(String batteryState) { + this.type = "battery"; // Type is battery + this.batteryState = batteryState; + } + private double[] getGps() { return gps; } @@ -125,6 +178,15 @@ public class Event { this.rotation = rotation; } + public void setSmsNumber(String smsNumber) { + this.type = "sms"; + this.smsNumber = smsNumber; + } + + public String getSmsNumber() { + return smsNumber; + } + private String getWordSessionId() { return wordSessionId != null ? wordSessionId : ""; } @@ -142,6 +204,24 @@ public class Event { this.word = word; } + public String getPackageName() { + return packageName; + } + + public void setPackageName(String packageName) { + this.type = "application"; + this.packageName = packageName; + } + + public String getAction() { + return action; + } + + public void setAction(String action) { + this.type = "application"; + this.action = action; + } + private long getTimestamp() { return timestamp; } @@ -181,6 +261,9 @@ public class Event { public float getSpeed() { this.type = "speed"; + if (Float.isInfinite(speed) || Float.isNaN(speed)) { + return -1.0f; + } return speed; } @@ -192,7 +275,7 @@ public class Event { public String getTurns() { - if (turn == null || turn.isEmpty() || turn.equals("null")){ + if (turn == null || turn.isEmpty() || turn.equals("null")) { turn = "No Turns"; } return turn; @@ -234,8 +317,98 @@ public class Event { } public String getBeaconProximity() { - this.type = "beaconProximity"; - return beaconProximity; + return beaconProximity != null ? beaconProximity : ""; + } + + public String getCallNumber() { + return callNumber != null ? callNumber : ""; + } + + public void setCallNumber(String callNumber) { + this.type = "call"; + this.callNumber = callNumber; + } + + public String getCallType() { + return callType != null ? callType : ""; + } + + public void setCallType(String callType) { + this.type = "call"; + this.callType = callType; + } + + public long getCallStartTime() { + return callStartTime; + } + + public void setCallStartTime(long callStartTime) { + this.type = "call"; + this.callStartTime = callStartTime; + } + + public long getCallEndTime() { + return callEndTime; + } + + public void setCallEndTime(long callEndTime) { + this.type = "call"; + this.callEndTime = callEndTime; + } + + public String getScreenState() { + return screenState != null ? screenState : ""; + } + + public void setScreenState(String screenState) { + this.type = "screen"; + this.screenState = screenState; + } + + public boolean isAudioPlaying() { + return audioPlaying; + } + + public void setAudioPlaying(boolean audioPlaying) { + this.type = "audio"; + this.audioPlaying = audioPlaying; + } + + public boolean isHeadsetOn() { + return headsetOn; + } + + public void setHeadsetOn(boolean headsetOn) { + this.type = "audio"; + this.headsetOn = headsetOn; + } + + public int getMusicVolume() { + return musicVolume; + } + + public void setMusicVolume(int musicVolume) { + this.type = "audio"; + this.musicVolume = musicVolume; + } + + public int getActivityType() { + return activityType; + } + + public void setActivityType(int activityType) { + this.type = "activity"; + this.activityType = activityType; + } + + public int getConfidence() { + return confidence; + } + + public void setConfidence(int confidence) { + // Do not set type here since it can be used for various types. + // However, now it is being used by activity only + this.confidence = confidence; } public JSONObject getEvent() throws JSONException { @@ -248,7 +421,13 @@ public class Event { jsonEvent.put("metaData", jsonMetaData); JSONObject jsonPayloadData = new JSONObject(); + // battery jsonPayloadData.put("battery", getBattery()); + jsonPayloadData.put("battery_state", getBatteryState()); + jsonPayloadData.put("battery_status", getBatteryStatus()); + jsonPayloadData.put("battery_temperature", getBatteryTemperature()); + + //gps double gpsEvents[] = getGps(); jsonPayloadData.put("gps_lat", gpsEvents[0]); @@ -301,6 +480,29 @@ public class Event { jsonPayloadData.put("word_sessionId", getWordSessionId()); jsonPayloadData.put("word_status", getWordStatus()); + // call + jsonPayloadData.put("call_number", getCallNumber()); + jsonPayloadData.put("call_type", getCallType()); + jsonPayloadData.put("call_start_time", getCallStartTime()); + jsonPayloadData.put("call_end_time", getCallEndTime()); + + // screen + jsonPayloadData.put("screen_state", getScreenState()); + + // headset + jsonPayloadData.put("audio_playing", isAudioPlaying()); + jsonPayloadData.put("headset_on", isHeadsetOn()); + jsonPayloadData.put("music_volume", getMusicVolume()); + + jsonPayloadData.put("activity_type", getActivityType()); + jsonPayloadData.put("confidence", getConfidence()); + + jsonPayloadData.put("sms_number", getSmsNumber()); + + jsonPayloadData.put("application_name", getPackageName()); + jsonPayloadData.put("action", getAction()); + + jsonEvent.put("payloadData", jsonPayloadData); return jsonEvent; 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 0aac980762..37e3356c68 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 @@ -16,11 +16,11 @@ package org.wso2.carbon.iot.android.sense.event; import android.app.Service; import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; import android.os.IBinder; + import org.wso2.carbon.iot.android.sense.event.streams.SenseDataCollector; -import org.wso2.carbon.iot.android.sense.event.streams.battery.BatteryDataReceiver; import org.wso2.carbon.iot.android.sense.util.LocalRegistry; +import org.wso2.carbon.iot.android.sense.util.SenseDataReceiverManager; import org.wso2.carbon.iot.android.sense.util.SenseWakeLock; /** @@ -48,10 +48,14 @@ public class SenseService extends Service { //Below triggers the data collection for sensors,location and battery. SenseDataCollector Sensor = new SenseDataCollector(this, SenseDataCollector.DataType.SENSOR); SenseDataCollector Location = new SenseDataCollector(this, SenseDataCollector.DataType.LOCATION); - registerReceiver(new BatteryDataReceiver(), new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); SenseDataCollector speed = new SenseDataCollector(this, SenseDataCollector.DataType.SPEED); - - + SenseDataCollector audio = new SenseDataCollector(this, SenseDataCollector.DataType.AUDIO); + SenseDataReceiverManager.registerBatteryDataReceiver(this); + SenseDataReceiverManager.registerScreenDataReceiver(this); + SenseDataReceiverManager.registerCallDataReceiver(this); + SenseDataReceiverManager.registerActivityDataReceiver(this); + SenseDataReceiverManager.registerSmsDataReceiver(this); + SenseDataReceiverManager.registerAppDataReceiver(this); //service will not be stopped until we manually stop the service return Service.START_NOT_STICKY; @@ -59,6 +63,13 @@ public class SenseService extends Service { @Override public void onDestroy() { + SenseDataReceiverManager.unregisterBatteryDataReceiver(this); + SenseDataReceiverManager.unregisterScreenDataReceiver(this); + SenseDataReceiverManager.unregisterCallDataReceiver(this); + SenseDataReceiverManager.unregisterActivityDataReceiver(this); + SenseDataReceiverManager.unregisterSmsDataReceiver(this); + SenseDataReceiverManager.unregisterAppDataReceiver(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/SenseDataCollector.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/SenseDataCollector.java index 4899b29b8c..726e55fd41 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/SenseDataCollector.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/SenseDataCollector.java @@ -19,13 +19,14 @@ import org.wso2.carbon.iot.android.sense.event.streams.Location.LocationDataRead import org.wso2.carbon.iot.android.sense.event.streams.Sensor.SensorDataReader; import org.wso2.carbon.iot.android.sense.event.streams.Speed.SpeedDataReader; import org.wso2.carbon.iot.android.sense.beacon.MonitoringActivity; +import org.wso2.carbon.iot.android.sense.event.streams.audio.AudioDataReader; /** * This class triggered by service to collect the sensor data. */ public class SenseDataCollector { public enum DataType { - SENSOR, LOCATION,SPEED + SENSOR, LOCATION,SPEED, AUDIO } public SenseDataCollector(Context ctx, DataType dt) { @@ -40,6 +41,9 @@ public class SenseDataCollector { case SPEED: dr = new SpeedDataReader(ctx); break; + case AUDIO: + dr = new AudioDataReader(ctx); + break; } if (dr != 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/event/streams/activity/ActivityData.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/activity/ActivityData.java new file mode 100644 index 0000000000..772351c5bd --- /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/event/streams/activity/ActivityData.java @@ -0,0 +1,67 @@ +/* + * 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.event.streams.activity; + +import java.util.Date; + +public class ActivityData { + /** + * 0 - In vehicle + * 1 - Bicycle + * 2 - Foot + * 3 - Still + * 4 - Unknown + * 5 - Tilting + * 7 - Walking + * 8 - Running + */ + private int activity; + private int confidence; + private long timestamp; + + ActivityData(int activity, int confidence) { + this.activity = activity; + this.confidence = confidence; + this.timestamp = new Date().getTime(); + } + + public int getActivity() { + return activity; + } + + public void setActivity(int activity) { + this.activity = activity; + } + + public int getConfidence() { + return confidence; + } + + public void setConfidence(int confidence) { + this.confidence = confidence; + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } +} 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/activity/ActivityReceiver.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/activity/ActivityReceiver.java new file mode 100644 index 0000000000..3220491983 --- /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/event/streams/activity/ActivityReceiver.java @@ -0,0 +1,54 @@ +/* + * 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.event.streams.activity; + +import android.app.IntentService; +import android.content.Intent; + +import com.google.android.gms.location.ActivityRecognitionResult; +import com.google.android.gms.location.DetectedActivity; + +import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; + +public class ActivityReceiver extends IntentService { + public static final int MINIMUM_CONFIDENCE = 75; + + /** + * Retrieve the information for every 10 seconds. + */ + public static final long UPDATE_INTERVAL = 10000; + + public ActivityReceiver() { + super(ActivityReceiver.class.getName()); + } + + @Override + protected void onHandleIntent(Intent intent) { + if (ActivityRecognitionResult.hasResult(intent)) { + ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent); + + for (DetectedActivity activity : result.getProbableActivities()) { + if (activity.getConfidence() >= MINIMUM_CONFIDENCE) { + ActivityData data = new ActivityData(activity.getType(), activity.getConfidence()); + SenseDataHolder.getActivityDataHolder().add(data); + } + } + } + } +} 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/application/ApplicationData.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/application/ApplicationData.java new file mode 100644 index 0000000000..1443e36b94 --- /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/event/streams/application/ApplicationData.java @@ -0,0 +1,61 @@ +/* + * 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.event.streams.application; + +import java.util.Date; + +public class ApplicationData { + public enum Action { + INSTALL, REMOVE + } + + private long timestamp; + private String packageName; + private Action action; + + ApplicationData(String packageName, Action action) { + this.packageName = packageName; + this.action = action; + this.timestamp = new Date().getTime(); + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + public String getPackageName() { + return packageName; + } + + public void setPackageName(String packageName) { + this.packageName = packageName; + } + + public Action getAction() { + return action; + } + + public void setAction(Action action) { + this.action = action; + } +} 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/application/ApplicationDataReceiver.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/application/ApplicationDataReceiver.java new file mode 100644 index 0000000000..8cb52550fe --- /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/event/streams/application/ApplicationDataReceiver.java @@ -0,0 +1,45 @@ +/* + * 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.event.streams.application; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; + +/** + * Currently interested on package add and remove only. Can be extended for modification. + */ +public class ApplicationDataReceiver extends BroadcastReceiver { + + + @Override + public void onReceive(Context context, Intent intent) { + String packageName = intent.getData().toString().substring(8); + ApplicationData appData; + if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { + appData = new ApplicationData(packageName, ApplicationData.Action.INSTALL); + } else { + // Removed + appData = new ApplicationData(packageName, ApplicationData.Action.REMOVE); + } + SenseDataHolder.getApplicationDataHolder().add(appData); + } +} 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/audio/AudioData.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/audio/AudioData.java new file mode 100644 index 0000000000..0449d32919 --- /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/event/streams/audio/AudioData.java @@ -0,0 +1,58 @@ +/* + * 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.event.streams.audio; + +public class AudioData { + private long timestamp; + private boolean headsetOn; + private int musicVolume; + private boolean playing; + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + public boolean isHeadsetOn() { + return headsetOn; + } + + public void setHeadsetOn(boolean headsetOn) { + this.headsetOn = headsetOn; + } + + public int getMusicVolume() { + return musicVolume; + } + + public void setMusicVolume(int musicVolume) { + this.musicVolume = musicVolume; + } + + public boolean isPlaying() { + return playing; + } + + public void setPlaying(boolean playing) { + this.playing = playing; + } +} 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/audio/AudioDataReader.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/audio/AudioDataReader.java new file mode 100644 index 0000000000..1a5f85c092 --- /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/event/streams/audio/AudioDataReader.java @@ -0,0 +1,50 @@ +/* + * 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.event.streams.audio; + +import android.content.Context; +import android.media.AudioManager; +import android.util.Log; + +import org.wso2.carbon.iot.android.sense.event.streams.DataReader; +import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; + +import java.util.Date; + +public class AudioDataReader extends DataReader { + + private static final String TAG = AudioDataReader.class.getName(); + private Context context; + + public AudioDataReader(Context context) { + this.context = context; + } + + @Override + public void run() { + Log.d(TAG, "Running AudioDataReader"); + AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + AudioData audioData = new AudioData(); + audioData.setTimestamp(new Date().getTime()); + audioData.setPlaying(manager.isMusicActive()); + audioData.setHeadsetOn(manager.isWiredHeadsetOn()); + audioData.setMusicVolume(manager.getStreamVolume(AudioManager.STREAM_MUSIC)); + SenseDataHolder.getAudioDataHolder().add(audioData); + } +} 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/battery/BatteryData.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/battery/BatteryData.java index 666e02a85f..7a4d7413aa 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/battery/BatteryData.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/battery/BatteryData.java @@ -25,6 +25,10 @@ import java.util.Date; */ public class BatteryData { + public enum State { + LOW, OK + } + private int health; private int level; private int plugged; @@ -34,6 +38,7 @@ public class BatteryData { private int temperature; private int voltage; private long timestamp; + private State state; BatteryData(Intent intent) { timestamp = new Date().getTime(); @@ -46,7 +51,12 @@ public class BatteryData { String technology = intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY); temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0); voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0); + state = State.OK; + } + BatteryData(State state) { + this.timestamp = new Date().getTime(); + this.state = state; } public int getHealth() { @@ -121,4 +131,12 @@ public class BatteryData { public void setTimestamp(long timestamp) { this.timestamp = timestamp; } + + public State getState() { + return state; + } + + public void setState(State state) { + this.state = state; + } } 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/battery/BatteryDataReceiver.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/battery/BatteryDataReceiver.java index 9fffa1c157..794f69b3db 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/battery/BatteryDataReceiver.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/battery/BatteryDataReceiver.java @@ -32,7 +32,13 @@ public class BatteryDataReceiver extends BroadcastReceiver { */ @Override public void onReceive(Context context, Intent intent) { - SenseDataHolder.getBatteryDataHolder().add(new BatteryData(intent)); + if (Intent.ACTION_BATTERY_OKAY.equals(intent.getAction())) { + SenseDataHolder.getBatteryDataHolder().add(new BatteryData(BatteryData.State.OK)); + } else if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction())) { + SenseDataHolder.getBatteryDataHolder().add(new BatteryData(BatteryData.State.LOW)); + } else { + SenseDataHolder.getBatteryDataHolder().add(new BatteryData(intent)); + } } } 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/call/CallData.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/call/CallData.java new file mode 100644 index 0000000000..9f617cdb17 --- /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/event/streams/call/CallData.java @@ -0,0 +1,70 @@ +/* + * 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.event.streams.call; + +public class CallData { + + public enum Type { + INCOMING, OUTGOING, MISSED; + } + + private Type type; + private String phoneNumber; + private long startTime; + private long endTime; + + public CallData(Type type, String phoneNumber, long startTime, long endTime) { + this.type = type; + this.phoneNumber = phoneNumber; + this.startTime = startTime; + this.endTime = endTime; + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public long getStartTime() { + return startTime; + } + + public void setStartTime(long startTime) { + this.startTime = startTime; + } + + public long getEndTime() { + return endTime; + } + + public void setEndTime(long endTime) { + this.endTime = endTime; + } +} \ No newline at end of file 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/call/CallDataReceiver.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/call/CallDataReceiver.java new file mode 100644 index 0000000000..1988914e81 --- /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/event/streams/call/CallDataReceiver.java @@ -0,0 +1,87 @@ +/* + * 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.event.streams.call; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.telephony.TelephonyManager; + +import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; + +public class CallDataReceiver extends BroadcastReceiver { + private static int lastState = TelephonyManager.CALL_STATE_IDLE; + private static long startTime; + private static boolean isIncoming; + private static String lastNotifiedNumber; + + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) { + lastNotifiedNumber = intent.getExtras().getString(Intent.EXTRA_PHONE_NUMBER); + } else { + String extraState = intent.getExtras().getString(TelephonyManager.EXTRA_STATE); + int state = 0; + if (extraState.equals(TelephonyManager.EXTRA_STATE_IDLE)) { + state = TelephonyManager.CALL_STATE_IDLE; + } else if (extraState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { + state = TelephonyManager.CALL_STATE_OFFHOOK; + } else if (extraState.equals(TelephonyManager.EXTRA_STATE_RINGING)) { + state = TelephonyManager.CALL_STATE_RINGING; + } + + if (lastState == state) { + return; // Nothing has been changed + } + switch (state) { + case TelephonyManager.CALL_STATE_RINGING: + // Receiving a call + isIncoming = true; + startTime = System.currentTimeMillis(); + // If incoming call, get the incoming number + lastNotifiedNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER); + break; + case TelephonyManager.CALL_STATE_OFFHOOK: + if (lastState == TelephonyManager.CALL_STATE_RINGING) { + // RINGING -> OFFHOOK = ANSWERED + isIncoming = true; + startTime = System.currentTimeMillis(); + } else { + // NOT RINGING -> OFFHOOK = OUTGOING + isIncoming = false; + startTime = System.currentTimeMillis(); + } + break; + case TelephonyManager.CALL_STATE_IDLE: + if (lastState == TelephonyManager.CALL_STATE_RINGING) { + // RINGING -> IDLE = MISSED + SenseDataHolder.getCallDataHolder().add(new CallData(CallData.Type.MISSED, lastNotifiedNumber, startTime, System.currentTimeMillis())); + } else if (isIncoming) { + // Incoming (OFFHOOK) -> IDLE = INCOMING CALL ENDED + SenseDataHolder.getCallDataHolder().add(new CallData(CallData.Type.INCOMING, lastNotifiedNumber, startTime, System.currentTimeMillis())); + } else { + // Not Incoming -> IDLE = OUTGOING CALL ENDED + SenseDataHolder.getCallDataHolder().add(new CallData(CallData.Type.OUTGOING, lastNotifiedNumber, startTime, System.currentTimeMillis())); + } + break; + } + lastState = state; + } + } +} 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/screen/ScreenData.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/screen/ScreenData.java new file mode 100644 index 0000000000..0c124611f7 --- /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/event/streams/screen/ScreenData.java @@ -0,0 +1,55 @@ +/* + * 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.event.streams.screen; + +import android.content.Intent; + +import java.util.Date; + +public class ScreenData { + private String action; + private long timestamp; + + ScreenData(Intent intent) { + if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) { + this.action = "on"; + } else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) { + this.action = "off"; + } else { + this.action = "unknown"; + } + this.timestamp = new Date().getTime(); + } + + public String getAction() { + return action; + } + + public void setAction(String action) { + this.action = action; + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } +} 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/screen/ScreenDataReceiver.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/screen/ScreenDataReceiver.java new file mode 100644 index 0000000000..ace9d98faa --- /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/event/streams/screen/ScreenDataReceiver.java @@ -0,0 +1,32 @@ +/* + * 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.event.streams.screen; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; + +public class ScreenDataReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + SenseDataHolder.getScreenDataHolder().add(new ScreenData(intent)); + } +} 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/sms/SmsData.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/sms/SmsData.java new file mode 100644 index 0000000000..607ca2a857 --- /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/event/streams/sms/SmsData.java @@ -0,0 +1,58 @@ +/* + * 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.event.streams.sms; + +import java.util.Date; + +public class SmsData { + + private String phoneNumber; + private long timestamp; + private String message; + + SmsData(String phoneNumber, String message) { + this.phoneNumber = phoneNumber; + this.message = message; + this.timestamp = new Date().getTime(); + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} 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/sms/SmsDataReceiver.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/sms/SmsDataReceiver.java new file mode 100644 index 0000000000..2ada981fb3 --- /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/event/streams/sms/SmsDataReceiver.java @@ -0,0 +1,49 @@ +/* + * 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.event.streams.sms; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.telephony.SmsMessage; + +import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; + +public class SmsDataReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + final Bundle bundle = intent.getExtras(); + + if (bundle != null) { + + final Object[] pdusObj = (Object[]) bundle.get("pdus"); + + for (int i = 0; i < pdusObj.length; i++) { + SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]); + String phoneNumber = currentMessage.getDisplayOriginatingAddress(); + String message = currentMessage.getDisplayMessageBody(); + + SmsData smsData = new SmsData(phoneNumber, message); + SenseDataHolder.getSmsDataHolder().add(smsData); + } + } + } +} 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/sensorlisting/SupportedSensors.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/sensorlisting/SupportedSensors.java index 6d57e6d704..2b0f5351be 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/sensorlisting/SupportedSensors.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/sensorlisting/SupportedSensors.java @@ -60,6 +60,7 @@ public class SupportedSensors { sensorList.add("Light"); sensorList.add("Gyroscope"); sensorList.add("Proximity"); + sensorList.add("Pedometer"); } /** @@ -74,6 +75,7 @@ public class SupportedSensors { sensorTypeMap.put("gyroscope", Sensor.TYPE_GYROSCOPE); sensorTypeMap.put("light", Sensor.TYPE_LIGHT); sensorTypeMap.put("proximity", Sensor.TYPE_PROXIMITY); + sensorTypeMap.put("pedometer", Sensor.TYPE_STEP_COUNTER); } /** @@ -88,6 +90,7 @@ public class SupportedSensors { typeSensorMap.put(Sensor.TYPE_GYROSCOPE, "gyroscope"); typeSensorMap.put(Sensor.TYPE_LIGHT, "light"); typeSensorMap.put(Sensor.TYPE_PROXIMITY, "proximity"); + typeSensorMap.put(Sensor.TYPE_STEP_COUNTER, "pedometer"); } /** 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/SenseDataHolder.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/SenseDataHolder.java index 1d33225b9a..c3d12cafb8 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/SenseDataHolder.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/SenseDataHolder.java @@ -17,11 +17,17 @@ import org.wso2.carbon.iot.android.sense.beacon.BeaconScanedData; import org.wso2.carbon.iot.android.sense.event.streams.Location.LocationData; import org.wso2.carbon.iot.android.sense.event.streams.Sensor.SensorData; import org.wso2.carbon.iot.android.sense.event.streams.Speed.SpeedData; +import org.wso2.carbon.iot.android.sense.event.streams.activity.ActivityData; +import org.wso2.carbon.iot.android.sense.event.streams.application.ApplicationData; +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.screen.ScreenData; +import org.wso2.carbon.iot.android.sense.event.streams.sms.SmsData; import org.wso2.carbon.iot.android.sense.speech.detector.util.WordData; + import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; -import android.util.Log; /** @@ -31,72 +37,123 @@ public class SenseDataHolder { private static List sensorDataHolder; private static List batteryDataHolder; + private static List callDataHolder; private static List locationDataHolder; private static List wordDataHolder; private static List speedDataHolder; private static List beaconScanedDataHolder; - + private static List screenDataHolder; + private static List audioDataHolder; + private static List activityDataHolder; + private static List smsDataHolder; + private static List applicationDataHolder; //LocationData gps; private static final String TAG = SenseDataHolder.class.getName(); - public static List getSensorDataHolder(){ - if(sensorDataHolder == null){ + public static List getSensorDataHolder() { + if (sensorDataHolder == null) { sensorDataHolder = new CopyOnWriteArrayList<>(); } return sensorDataHolder; } - public static List getBatteryDataHolder(){ - if(batteryDataHolder == null){ + public static List getBatteryDataHolder() { + if (batteryDataHolder == null) { batteryDataHolder = new CopyOnWriteArrayList<>(); } return batteryDataHolder; } - public static List getLocationDataHolder(){ + public static List getCallDataHolder() { + if (callDataHolder == null) { + callDataHolder = new CopyOnWriteArrayList<>(); + } + return callDataHolder; + } + + public static List getLocationDataHolder() { - if(locationDataHolder == null){ + if (locationDataHolder == null) { locationDataHolder = new CopyOnWriteArrayList<>(); } - return locationDataHolder; + return locationDataHolder; } - public static List getWordDataHolder(){ - if(wordDataHolder == null){ + public static List getWordDataHolder() { + if (wordDataHolder == null) { wordDataHolder = new CopyOnWriteArrayList<>(); } return wordDataHolder; } - public static List getSpeedDataHolder(){ - if(speedDataHolder == null){ + public static List getSpeedDataHolder() { + if (speedDataHolder == null) { speedDataHolder = new CopyOnWriteArrayList<>(); } return speedDataHolder; } - public static List getBeaconScanedDataHolder(){ - if(beaconScanedDataHolder == null){ + public static List getBeaconScanedDataHolder() { + if (beaconScanedDataHolder == null) { beaconScanedDataHolder = new CopyOnWriteArrayList<>(); } return beaconScanedDataHolder; } - public static void resetSensorDataHolder(){ + public static List getScreenDataHolder() { + if (screenDataHolder == null) { + screenDataHolder = new CopyOnWriteArrayList<>(); + } + return screenDataHolder; + } + + public static List getAudioDataHolder() { + if (audioDataHolder == null) { + audioDataHolder = new CopyOnWriteArrayList<>(); + } + return audioDataHolder; + } + + public static List getActivityDataHolder() { + if (activityDataHolder == null) { + activityDataHolder = new CopyOnWriteArrayList<>(); + } + return activityDataHolder; + } + + public static List getSmsDataHolder() { + if (smsDataHolder == null) { + smsDataHolder = new CopyOnWriteArrayList<>(); + } + return smsDataHolder; + } + + public static List getApplicationDataHolder() { + if (applicationDataHolder == null) { + applicationDataHolder = new CopyOnWriteArrayList<>(); + } + return applicationDataHolder; + } + + public static void resetSensorDataHolder() { sensorDataHolder = null; } - public static void resetBatteryDataHolder(){ + public static void resetBatteryDataHolder() { batteryDataHolder = null; } - public static void resetLocationDataHolder(){ + public static void resetCallDataHolder() { + callDataHolder = null; + } + + public static void resetLocationDataHolder() { locationDataHolder = null; } @@ -112,5 +169,23 @@ public class SenseDataHolder { beaconScanedDataHolder = null; } + public static void resetScreenDataHolder() { + screenDataHolder = null; + } + public static void resetAudioDataHolder() { + audioDataHolder = null; + } + + public static void resetActivityDataHolder() { + activityDataHolder = null; + } + + public static void resetSmsDataHolder() { + smsDataHolder = null; + } + + public static void resetApplicationDataHolder() { + applicationDataHolder = 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 new file mode 100644 index 0000000000..a72532464a --- /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/SenseDataReceiverManager.java @@ -0,0 +1,175 @@ +/* + * 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; + +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.Bundle; +import android.provider.Telephony; +import android.support.annotation.Nullable; +import android.telephony.TelephonyManager; + +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.location.ActivityRecognition; + +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.screen.ScreenDataReceiver; +import org.wso2.carbon.iot.android.sense.event.streams.sms.SmsDataReceiver; + +public class SenseDataReceiverManager { + private static BroadcastReceiver batteryDataReceiver; + + private static BroadcastReceiver screenDataReceiver; + + private static BroadcastReceiver callDataReceiver; + + private static GoogleApiClient apiClient; + + private static SmsDataReceiver smsDataReceiver; + + private static ApplicationDataReceiver appDataReceiver; + + private SenseDataReceiverManager() { + + } + + public static void registerBatteryDataReceiver(Context context) { + if (batteryDataReceiver == null) { + batteryDataReceiver = new BatteryDataReceiver(); + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(Intent.ACTION_BATTERY_LOW); + intentFilter.addAction(Intent.ACTION_BATTERY_OKAY); + intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); + + context.registerReceiver(batteryDataReceiver, intentFilter); + } + } + + public static void unregisterBatteryDataReceiver(Context context) { + if (batteryDataReceiver != null) { + context.unregisterReceiver(batteryDataReceiver); + batteryDataReceiver = null; + } + } + + public static void registerScreenDataReceiver(Context context) { + if (screenDataReceiver == null) { + screenDataReceiver = new ScreenDataReceiver(); + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(Intent.ACTION_SCREEN_ON); + intentFilter.addAction(Intent.ACTION_SCREEN_OFF); + + context.registerReceiver(screenDataReceiver, intentFilter); + } + } + + public static void unregisterScreenDataReceiver(Context context) { + if (screenDataReceiver != null) { + context.unregisterReceiver(screenDataReceiver); + screenDataReceiver = null; + } + } + + public static void registerCallDataReceiver(Context context) { + if (callDataReceiver == null) { + callDataReceiver = new CallDataReceiver(); + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); + intentFilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL); + + context.registerReceiver(callDataReceiver, intentFilter); + } + } + + public static void unregisterCallDataReceiver(Context context) { + if (callDataReceiver != null) { + context.unregisterReceiver(callDataReceiver); + callDataReceiver = null; + } + } + + public static void registerActivityDataReceiver(Context context) { + if (apiClient == null) { + Intent intent = new Intent(context, ActivityReceiver.class); + final PendingIntent pendingIntent = PendingIntent.getService(context, 888971, intent, PendingIntent.FLAG_UPDATE_CURRENT); + apiClient = new GoogleApiClient.Builder(context) + .addApi(ActivityRecognition.API) + .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { + @Override + public void onConnected(@Nullable Bundle bundle) { + ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(apiClient, ActivityReceiver.UPDATE_INTERVAL, pendingIntent); + } + + @Override + public void onConnectionSuspended(int i) { + ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(apiClient, pendingIntent); + } + }) + .build(); + + apiClient.connect(); + } + } + + public static void unregisterActivityDataReceiver(Context context) { + if (apiClient != null) { + apiClient.disconnect(); + apiClient = null; + } + } + + public static void registerSmsDataReceiver(Context context) { + if (smsDataReceiver == null) { + smsDataReceiver = new SmsDataReceiver(); + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(Telephony.Sms.Intents.SMS_RECEIVED_ACTION); + context.registerReceiver(smsDataReceiver, intentFilter); + } + } + + public static void unregisterSmsDataReceiver(Context context) { + if (smsDataReceiver != null) { + context.unregisterReceiver(smsDataReceiver); + smsDataReceiver = null; + } + } + + public static void registerAppDataReceiver(Context context) { + if (appDataReceiver == null) { + appDataReceiver = new ApplicationDataReceiver(); + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); + intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); + context.registerReceiver(appDataReceiver, intentFilter); + } + } + + public static void unregisterAppDataReceiver(Context context) { + if (appDataReceiver != null) { + context.unregisterReceiver(appDataReceiver); + appDataReceiver = null; + } + } +}