Refactoring Android Sense agent.

Increased the battery data retrieving sequence.
Completed network data reading implementation.
The data publishing interval is changed to 1000ms.
merge-requests/1/head
Menaka Jayawardena 8 years ago
parent 49ee7c2986
commit e0afb8d205

@ -50,6 +50,12 @@
android:label="@string/app_name" >
</service>
<service
android:name="org.wso2.carbon.iot.android.sense.event.streams.battery.BatteryReaderService"
android:enabled="true"
android:label="@string/app_name" >
</service>
<service android:name="org.wso2.carbon.iot.android.sense.event.streams.activity.ActivityReceiver"/>
<receiver android:name="org.wso2.carbon.iot.android.sense.event.SenseScheduleReceiver" >

@ -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);

@ -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);
}

@ -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();

@ -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);
}
}

@ -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));
}
}
}

@ -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;
}
}

@ -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<Void, Void, Long> {
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();
}

@ -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);
}
}

@ -56,6 +56,7 @@ public class SenseClientAsyncExecutor extends AsyncTask<String, Void, Map<String
private final static String TAG = "SenseService Client";
private static final String STATUS = "status";
private final String PASSWORD_GRANT_TYPE = "password";
private final static String DEVICE_NAME = Build.MANUFACTURER + " " + Build.MODEL;
private Context context;
@ -116,7 +117,7 @@ public class SenseClientAsyncExecutor extends AsyncTask<String, Void, Map<String
new BasicAuthRequestInterceptor(apiApplicationKey.getConsumerKey(), apiApplicationKey.getConsumerSecret()))
.contract(new JAXRSContract()).encoder(new JacksonEncoder()).decoder(new JacksonDecoder())
.target(TokenIssuerService.class, endpoint + SenseConstants.TOKEN_ISSUER_CONTEXT);
accessTokenInfo = tokenIssuerService.getToken("password", username, password, "device_" + deviceId);
accessTokenInfo = tokenIssuerService.getToken(PASSWORD_GRANT_TYPE, username, password, "device_" + deviceId);
//DeviceRegister
AndroidSenseManagerService androidSenseManagerService = Feign.builder().client(disableHostnameVerification)

@ -35,7 +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.data.NetworkDataReader;
import org.wso2.carbon.iot.android.sense.event.streams.screen.ScreenDataReceiver;
import org.wso2.carbon.iot.android.sense.event.streams.sms.SmsDataReceiver;
@ -52,7 +52,7 @@ public class SenseDataReceiverManager {
private static ApplicationDataReceiver appDataReceiver;
private static NetworkDataReceiver networkDataReceiver;
private static NetworkDataReader networkDataReader;
private SenseDataReceiverManager() {
@ -174,20 +174,20 @@ 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 registerNetworkDataReader(Context context) {
if (networkDataReader == null) {
networkDataReader = new NetworkDataReader(context);
networkDataReader.execute();
}
}
public static void unregisterNetworkDataReceiver(Context context) {
if (networkDataReceiver != null) {
context.unregisterReceiver(networkDataReceiver);
public static void unregisterNetworkDataReader() {
if (networkDataReader != null) {
networkDataReader.cancel(true);
}
networkDataReceiver = null;
networkDataReader = null;
}

@ -21,7 +21,7 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
/**
* This hold the api defintion that is used as a contract with netflix feign.
* This hold the api definition that is used as a contract with netflix feign.
*/
@Path("/token")
public interface TokenIssuerService {
@ -30,4 +30,10 @@ public interface TokenIssuerService {
@Produces(MediaType.APPLICATION_JSON)
AccessTokenInfo getToken(@QueryParam("grant_type") String grant, @QueryParam("username") String username,
@QueryParam("password") String password, @QueryParam("deviceId") String deviceId);
@POST
@Produces(MediaType.APPLICATION_JSON)
AccessTokenInfo getRefreshToken(@QueryParam("grant_type") String grantType, @QueryParam("refreshToken") String refreshToken);
}

Loading…
Cancel
Save