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 2b0f926dad..9922eac4be 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 @@ -50,6 +50,12 @@ android:label="@string/app_name" > + + + 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/DataPublisherReceiver.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/DataPublisherReceiver.java index b09d15a889..4ece0fe950 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/DataPublisherReceiver.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/DataPublisherReceiver.java @@ -18,16 +18,18 @@ import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.util.Log; /** * This creates and AlarmManagerService that triggers the data uploader service with a 30 seconds interval. */ public class DataPublisherReceiver extends BroadcastReceiver { - private static int ALARM_INTERVAL = 30000; + private static int ALARM_INTERVAL = 1000; @Override public void onReceive(Context context, Intent intent) { AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + Log.i("Data Publisher", "triggered"); Intent i = new Intent(context, DataPublisherService.class); PendingIntent pending = PendingIntent.getService(context, 0, i, 0); service.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), ALARM_INTERVAL, pending); 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/SenseScheduleReceiver.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/SenseScheduleReceiver.java index 41ff9feffa..4b76b5cca2 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/SenseScheduleReceiver.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/SenseScheduleReceiver.java @@ -25,7 +25,7 @@ import java.util.Calendar; * This is a service which triggers to collect */ public class SenseScheduleReceiver extends BroadcastReceiver { - private static final int ALARM_INTERVAL = 5000; + private static final int ALARM_INTERVAL = 1000; @Override public void onReceive(Context context, Intent intent) { @@ -35,7 +35,7 @@ public class SenseScheduleReceiver extends BroadcastReceiver { Calendar cal = Calendar.getInstance(); - cal.add(Calendar.SECOND, 30); + cal.add(Calendar.SECOND, 10); service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), ALARM_INTERVAL, pending); } 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 ec62d8f4f4..7554f65244 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,7 +56,7 @@ public class SenseService extends Service { SenseDataReceiverManager.registerActivityDataReceiver(this); SenseDataReceiverManager.registerSmsDataReceiver(this); SenseDataReceiverManager.registerAppDataReceiver(this); - SenseDataReceiverManager.registerNetworkDataReceiver(this); + SenseDataReceiverManager.registerNetworkDataReader(this); //service will not be stopped until we manually stop the service return Service.START_NOT_STICKY; @@ -70,7 +70,7 @@ public class SenseService extends Service { SenseDataReceiverManager.unregisterActivityDataReceiver(this); SenseDataReceiverManager.unregisterSmsDataReceiver(this); SenseDataReceiverManager.unregisterAppDataReceiver(this); - SenseDataReceiverManager.unregisterNetworkDataReceiver(this); + SenseDataReceiverManager.unregisterNetworkDataReader(); 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/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 794f69b3db..0a971ab611 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 @@ -13,10 +13,15 @@ */ package org.wso2.carbon.iot.android.sense.event.streams.battery; +import android.app.AlarmManager; +import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.os.BatteryManager; +import android.util.Log; +import org.wso2.carbon.iot.android.sense.data.publisher.DataPublisherService; import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; /** @@ -24,21 +29,20 @@ import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; */ public class BatteryDataReceiver extends BroadcastReceiver { + private final long ALARM_INTERVAL = 1000; /** - * when the data is retreived then its added to a inmemory map. + * When the data is retrieved then its added to a in memory map. * - * @param context of the reciever. - * @param intent of the reciver + * @param context of the receiver. + * @param intent of the receiver */ @Override public void onReceive(Context context, Intent 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)); - } + AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + Log.i("Battery Data Receiver", "Triggered"); + Intent i = new Intent(context, BatteryReaderService.class); + PendingIntent pending = PendingIntent.getService(context, 0, i, 0); + service.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), ALARM_INTERVAL, pending); } } 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/BatteryReaderService.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/BatteryReaderService.java new file mode 100644 index 0000000000..c2ea5db9fd --- /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/battery/BatteryReaderService.java @@ -0,0 +1,63 @@ +/* + * 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.battery; + +import android.app.IntentService; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.BatteryManager; +import android.os.IBinder; +import android.support.annotation.Nullable; +import android.util.Log; + +import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; + +public class BatteryReaderService extends IntentService { + + private Context context; + + public BatteryReaderService() { + super("BatteryReaderService"); + } + + @Nullable + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + protected void onHandleIntent(Intent intent) { + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(Intent.ACTION_BATTERY_LOW); + intentFilter.addAction(Intent.ACTION_BATTERY_OKAY); + intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); + Intent intent1 = registerReceiver(null, intentFilter); + + Log.i("Battery Data", String.valueOf(intent1.getIntExtra(BatteryManager.EXTRA_LEVEL, 0))); + 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(intent1)); + } + } +} 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/data/NetworkData.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/data/NetworkData.java index 47b32325c7..c8fcfac4a6 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/event/streams/data/NetworkData.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/data/NetworkData.java @@ -23,19 +23,16 @@ import java.util.Date; public class NetworkData { //Mobile or Wifi - private String DATA_TYPE; + private String type; private long dataReceived; private long dataSent; private long timeStamp; - public NetworkData(long received, long sent) { - this.dataReceived = received; - this.dataSent = sent; - this.timeStamp = new Date().getTime(); + public NetworkData() { } public String getDataType() { - return DATA_TYPE; + return type; } public long getDataReceived() { @@ -57,4 +54,16 @@ public class NetworkData { public long getTimeStamp() { return timeStamp; } + + public void setTimeStamp(long timeStamp) { + this.timeStamp = timeStamp; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } } 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/data/NetworkDataReader.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/data/NetworkDataReader.java new file mode 100644 index 0000000000..6c32318037 --- /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/data/NetworkDataReader.java @@ -0,0 +1,96 @@ +/* + * 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.data; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.net.TrafficStats; +import android.os.AsyncTask; +import android.os.Handler; +import android.util.Log; + +import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; + +import java.util.Date; + +/** + * Class to read data sent and received by the device. + */ +public class NetworkDataReader extends AsyncTask { + + private NetworkData networkData; + private Context context; + private Handler mHandler = new Handler(); + private long mStartRX = 0; + private long mStartTX = 0; + private final String WIFI = "WIFI"; + private final String MOBILE = "MOBILE"; + private String connectionType; + + public NetworkDataReader(Context context) { + this.context = context; + } + + @Override + protected Long doInBackground(Void... voids) { + + ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + networkData = new NetworkData(); + + if (getConnectionType(connectivityManager, ConnectivityManager.TYPE_WIFI)) { + connectionType = WIFI; + } else if (getConnectionType(connectivityManager, ConnectivityManager.TYPE_MOBILE)) { + connectionType = MOBILE; + } + + mStartRX = TrafficStats.getTotalRxBytes(); + mStartTX = TrafficStats.getTotalTxBytes(); + if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) { + Log.e("ERROR", "Not connected."); + } else { + mHandler.postDelayed(mRunnable, 10000); + } + return null; + } + + /** + * Collect data sent and received with in 10 second time frames. + */ + private final Runnable mRunnable = new Runnable() { + public void run() { + long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX; + long txBytes = TrafficStats.getTotalTxBytes()- mStartTX; + Log.i("Usage: ", String.valueOf(rxBytes) + " " + String.valueOf(txBytes) + " " + System.currentTimeMillis()); + networkData.setType(connectionType); + networkData.setTimeStamp(new Date().getTime()); + networkData.setDataSent(txBytes); + networkData.setDataReceived(rxBytes); + SenseDataHolder.getNetworkDataHolder().add(networkData); + mHandler.postDelayed(mRunnable, 10000); + } + }; + + /** + * Get the type of the connection currently have. + */ + private boolean getConnectionType(ConnectivityManager manager, Integer type) { + NetworkInfo networkInfo = manager.getNetworkInfo(type); + return networkInfo.isConnected(); + } 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/data/NetworkDataReceiver.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/data/NetworkDataReceiver.java deleted file mode 100644 index bcf353ffee..0000000000 --- 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/data/NetworkDataReceiver.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.data; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.util.Log; - -import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; - -public class NetworkDataReceiver extends BroadcastReceiver{ - - @Override - public void onReceive(Context context, Intent intent) { - long sent = android.net.TrafficStats.getTotalTxBytes(); - long received = android.net.TrafficStats.getTotalRxBytes(); - Log.d("NetworkData :", "Received: " + sent + " Received : " + received); - - NetworkData networkData = new NetworkData(received, sent); - - SenseDataHolder.getNetworkDataHolder().add(networkData); - - } - -} 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 6514fbb513..8a31fdf9c7 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 @@ -56,6 +56,7 @@ public class SenseClientAsyncExecutor extends AsyncTask