Merge pull request #448 from menakaj/android-sense-agent-fixes

Modifications for Android Sense app.
revert-dabc3590
sinthuja 8 years ago committed by GitHub
commit 4e9cea4b00

@ -37,6 +37,7 @@ import org.wso2.carbon.iot.android.sense.event.streams.application.ApplicationDa
import org.wso2.carbon.iot.android.sense.event.streams.audio.AudioData; import org.wso2.carbon.iot.android.sense.event.streams.audio.AudioData;
import org.wso2.carbon.iot.android.sense.event.streams.battery.BatteryData; 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.call.CallData;
import org.wso2.carbon.iot.android.sense.event.streams.data.NetworkData;
import org.wso2.carbon.iot.android.sense.event.streams.screen.ScreenData; import org.wso2.carbon.iot.android.sense.event.streams.screen.ScreenData;
import org.wso2.carbon.iot.android.sense.event.streams.sms.SmsData; import org.wso2.carbon.iot.android.sense.event.streams.sms.SmsData;
import org.wso2.carbon.iot.android.sense.speech.detector.util.ProcessWords; import org.wso2.carbon.iot.android.sense.speech.detector.util.ProcessWords;
@ -277,6 +278,19 @@ public class DataPublisherService extends Service {
} }
SenseDataHolder.resetApplicationDataHolder(); SenseDataHolder.resetApplicationDataHolder();
//Retrieve network data
List<NetworkData> networkDataList = SenseDataHolder.getNetworkDataHolder();
if (!networkDataList.isEmpty()) {
for (NetworkData networkData : networkDataList) {
Event event = new Event();
event.setTimestamp(networkData.getTimeStamp());
event.setDataType(networkData.getDataType());
event.setDataReceived(networkData.getDataReceived());
event.setDataSent(networkData.getDataSent());
events.add(event);
}
}
SenseDataHolder.resetNetworkDataHolder();
//publish the data //publish the data
if (events.size() > 0 && LocalRegistry.isEnrolled(context)) { if (events.size() > 0 && LocalRegistry.isEnrolled(context)) {

@ -61,6 +61,17 @@ public class Event {
private String packageName; private String packageName;
private String action; private String action;
/**
* Network data
* Time stamp
* Data type
* Data sent
* Data received.
* */
private String dataType;
private long dataSent;
private long dataReceived;
private int getBattery() { private int getBattery() {
return battery; return battery;
} }
@ -411,6 +422,36 @@ public class Event {
this.confidence = confidence; this.confidence = confidence;
} }
public String getDataType() {
this.type = "data";
return dataType;
}
public void setDataType(String dataType) {
this.type = "data";
this.dataType = dataType;
}
public long getDataSent() {
this.type = "data";
return dataSent;
}
public void setDataSent(long dataSent) {
this.type = "data";
this.dataSent = dataSent;
}
public long getDataReceived() {
this.type = "data";
return dataReceived;
}
public void setDataReceived(long dataReceived) {
this.type = "data";
this.dataReceived = dataReceived;
}
public JSONObject getEvent() throws JSONException { public JSONObject getEvent() throws JSONException {
JSONObject jsonEvent = new JSONObject(); JSONObject jsonEvent = new JSONObject();
JSONObject jsonMetaData = new JSONObject(); JSONObject jsonMetaData = new JSONObject();
@ -502,6 +543,10 @@ public class Event {
jsonPayloadData.put("application_name", getPackageName()); jsonPayloadData.put("application_name", getPackageName());
jsonPayloadData.put("action", getAction()); jsonPayloadData.put("action", getAction());
//Network data
jsonPayloadData.put("data_type", getDataType());
jsonPayloadData.put("data_received", getDataReceived());
jsonPayloadData.put("data_sent", getDataSent());
jsonEvent.put("payloadData", jsonPayloadData); jsonEvent.put("payloadData", jsonPayloadData);

@ -56,6 +56,7 @@ public class SenseService extends Service {
SenseDataReceiverManager.registerActivityDataReceiver(this); SenseDataReceiverManager.registerActivityDataReceiver(this);
SenseDataReceiverManager.registerSmsDataReceiver(this); SenseDataReceiverManager.registerSmsDataReceiver(this);
SenseDataReceiverManager.registerAppDataReceiver(this); SenseDataReceiverManager.registerAppDataReceiver(this);
SenseDataReceiverManager.registerNetworkDataReceiver(this);
//service will not be stopped until we manually stop the service //service will not be stopped until we manually stop the service
return Service.START_NOT_STICKY; return Service.START_NOT_STICKY;
@ -69,6 +70,7 @@ public class SenseService extends Service {
SenseDataReceiverManager.unregisterActivityDataReceiver(this); SenseDataReceiverManager.unregisterActivityDataReceiver(this);
SenseDataReceiverManager.unregisterSmsDataReceiver(this); SenseDataReceiverManager.unregisterSmsDataReceiver(this);
SenseDataReceiverManager.unregisterAppDataReceiver(this); SenseDataReceiverManager.unregisterAppDataReceiver(this);
SenseDataReceiverManager.unregisterNetworkDataReceiver(this);
SenseWakeLock.releaseCPUWakeLock(); SenseWakeLock.releaseCPUWakeLock();
super.onDestroy(); super.onDestroy();

@ -26,6 +26,7 @@ import org.wso2.carbon.iot.android.sense.realtimeviewer.sensorlisting.SupportedS
import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; import org.wso2.carbon.iot.android.sense.util.SenseDataHolder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -65,7 +66,7 @@ public class SensorDataReader extends DataReader implements SensorEventListener
SensorData sensorInfo = senseDataStruct.get(sensor.getName()); SensorData sensorInfo = senseDataStruct.get(sensor.getName());
sensorVector.add(sensorInfo); sensorVector.add(sensorInfo);
Log.d(TAG, "Sensor Name " + sensor.getName() + ", Type " + sensor.getType() + " " + Log.d(TAG, "Sensor Name " + sensor.getName() + ", Type " + sensor.getType() + " " +
", sensorValue :" + sensorInfo.getSensorValues()); ", sensorValue :" + Arrays.toString(sensorInfo.getSensorValues()));
} }
} catch (Throwable e) { } catch (Throwable e) {
Log.d(TAG, "error on sensors"); Log.d(TAG, "error on sensors");

@ -42,9 +42,9 @@ public class RealTimeSensorReader implements SensorEventListener {
RealTimeSensor realTimeSensor = new RealTimeSensor(); RealTimeSensor realTimeSensor = new RealTimeSensor();
realTimeSensor.setName(supportedSensors.getType(event.sensor.getType()).toUpperCase()); realTimeSensor.setName(supportedSensors.getType(event.sensor.getType()).toUpperCase());
realTimeSensor.setValueX(event.values[0] + ""); realTimeSensor.setValueX(event.values == null ? "" : event.values[0] + "");
realTimeSensor.setValueY(event.values[1] + ""); realTimeSensor.setValueY(event.values == null || event.values.length <= 1 ? "" : event.values[1] + "");
realTimeSensor.setValueZ(event.values[2] + ""); realTimeSensor.setValueZ(event.values == null || event.values.length <= 1 ? "" : event.values[2] + "");
TempStore.sensorDataMap.put(supportedSensors.getType(event.sensor.getType()), realTimeSensor); TempStore.sensorDataMap.put(supportedSensors.getType(event.sensor.getType()), realTimeSensor);

@ -23,6 +23,7 @@ import org.wso2.carbon.iot.android.sense.constants.SenseConstants;
import org.wso2.carbon.iot.android.sense.util.dto.AccessTokenInfo; import org.wso2.carbon.iot.android.sense.util.dto.AccessTokenInfo;
import org.wso2.carbon.iot.android.sense.util.dto.AndroidConfiguration; import org.wso2.carbon.iot.android.sense.util.dto.AndroidConfiguration;
import org.wso2.carbon.iot.android.sense.util.dto.AndroidSenseManagerService; import org.wso2.carbon.iot.android.sense.util.dto.AndroidSenseManagerService;
import org.wso2.carbon.iot.android.sense.util.dto.ApiApplicationKey;
import org.wso2.carbon.iot.android.sense.util.dto.ApiApplicationRegistrationService; import org.wso2.carbon.iot.android.sense.util.dto.ApiApplicationRegistrationService;
import org.wso2.carbon.iot.android.sense.util.dto.ApiRegistrationProfile; import org.wso2.carbon.iot.android.sense.util.dto.ApiRegistrationProfile;
import org.wso2.carbon.iot.android.sense.util.dto.DynamicClientRegistrationService; import org.wso2.carbon.iot.android.sense.util.dto.DynamicClientRegistrationService;
@ -98,41 +99,24 @@ public class SenseClientAsyncExecutor extends AsyncTask<String, Void, Map<String
responseMap.put(STATUS, "200"); responseMap.put(STATUS, "200");
AccessTokenInfo accessTokenInfo = null; AccessTokenInfo accessTokenInfo = null;
try { try {
//DynamicClientRegistraiton.
DynamicClientRegistrationService dynamicClientRegistrationService = Feign.builder()
.client(disableHostnameVerification).contract(new
JAXRSContract()).encoder(new JacksonEncoder()).decoder(new JacksonDecoder())
.target(DynamicClientRegistrationService.class, endpoint + SenseConstants.DCR_CONTEXT);
RegistrationProfile registrationProfile = new RegistrationProfile();
String applicationName = "android-sense:" + deviceId;
registrationProfile.setOwner(username);
registrationProfile.setClientName(applicationName);
registrationProfile.setCallbackUrl("");
registrationProfile.setGrantType("password refresh_token client_credentials");
registrationProfile.setApplicationType("device");
registrationProfile.setTokenScope("production");
OAuthApplicationInfo oAuthApplicationInfo = dynamicClientRegistrationService.register(registrationProfile);
//PasswordGrantType
TokenIssuerService tokenIssuerService = Feign.builder().client(disableHostnameVerification).requestInterceptor(
new BasicAuthRequestInterceptor(oAuthApplicationInfo.getClient_id(), oAuthApplicationInfo.getClient_secret()))
.contract(new JAXRSContract()).encoder(new JacksonEncoder()).decoder(new JacksonDecoder())
.target(TokenIssuerService.class, endpoint + SenseConstants.TOKEN_ISSUER_CONTEXT);
accessTokenInfo = tokenIssuerService.getToken("password", username, password);
//ApiApplicationRegistration //ApiApplicationRegistration
ApiApplicationRegistrationService apiApplicationRegistrationService = Feign.builder().client(disableHostnameVerification) ApiApplicationRegistrationService apiApplicationRegistrationService = Feign.builder().client(disableHostnameVerification)
.requestInterceptor(new OAuthRequestInterceptor(accessTokenInfo.getAccess_token())) .requestInterceptor(new BasicAuthRequestInterceptor(username, password))
.contract(new JAXRSContract()).encoder(new JacksonEncoder()).decoder(new JacksonDecoder()) .contract(new JAXRSContract()).encoder(new JacksonEncoder()).decoder(new JacksonDecoder())
.target(ApiApplicationRegistrationService.class, endpoint + SenseConstants.API_APPLICATION_REGISTRATION_CONTEXT); .target(ApiApplicationRegistrationService.class, endpoint + SenseConstants.API_APPLICATION_REGISTRATION_CONTEXT);
ApiRegistrationProfile apiRegistrationProfile = new ApiRegistrationProfile(); ApiRegistrationProfile apiRegistrationProfile = new ApiRegistrationProfile();
apiRegistrationProfile.setApplicationName(applicationName); apiRegistrationProfile.setApplicationName("android_sense_" + deviceId);
apiRegistrationProfile.setConsumerKey(oAuthApplicationInfo.getClient_id());
apiRegistrationProfile.setConsumerSecret(oAuthApplicationInfo.getClient_secret());
apiRegistrationProfile.setIsAllowedToAllDomains(false); apiRegistrationProfile.setIsAllowedToAllDomains(false);
apiRegistrationProfile.setIsMappingAnExistingOAuthApp(true); apiRegistrationProfile.setIsMappingAnExistingOAuthApp(false);
apiRegistrationProfile.setTags(new String[]{SenseConstants.DEVICE_TYPE}); apiRegistrationProfile.setTags(new String[]{SenseConstants.DEVICE_TYPE});
String replyMsg = apiApplicationRegistrationService.register(apiRegistrationProfile); ApiApplicationKey apiApplicationKey = apiApplicationRegistrationService.register(apiRegistrationProfile);
//PasswordGrantType
TokenIssuerService tokenIssuerService = Feign.builder().client(disableHostnameVerification).requestInterceptor(
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);
//DeviceRegister //DeviceRegister
AndroidSenseManagerService androidSenseManagerService = Feign.builder().client(disableHostnameVerification) AndroidSenseManagerService androidSenseManagerService = Feign.builder().client(disableHostnameVerification)

@ -22,6 +22,7 @@ import org.wso2.carbon.iot.android.sense.event.streams.application.ApplicationDa
import org.wso2.carbon.iot.android.sense.event.streams.audio.AudioData; import org.wso2.carbon.iot.android.sense.event.streams.audio.AudioData;
import org.wso2.carbon.iot.android.sense.event.streams.battery.BatteryData; 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.call.CallData;
import org.wso2.carbon.iot.android.sense.event.streams.data.NetworkData;
import org.wso2.carbon.iot.android.sense.event.streams.screen.ScreenData; import org.wso2.carbon.iot.android.sense.event.streams.screen.ScreenData;
import org.wso2.carbon.iot.android.sense.event.streams.sms.SmsData; import org.wso2.carbon.iot.android.sense.event.streams.sms.SmsData;
import org.wso2.carbon.iot.android.sense.speech.detector.util.WordData; import org.wso2.carbon.iot.android.sense.speech.detector.util.WordData;
@ -47,6 +48,7 @@ public class SenseDataHolder {
private static List<ActivityData> activityDataHolder; private static List<ActivityData> activityDataHolder;
private static List<SmsData> smsDataHolder; private static List<SmsData> smsDataHolder;
private static List<ApplicationData> applicationDataHolder; private static List<ApplicationData> applicationDataHolder;
private static List<NetworkData> networkDataHolder;
//LocationData gps; //LocationData gps;
@ -141,6 +143,13 @@ public class SenseDataHolder {
return applicationDataHolder; return applicationDataHolder;
} }
public static List<NetworkData> getNetworkDataHolder() {
if (networkDataHolder == null) {
networkDataHolder = new CopyOnWriteArrayList<>();
}
return networkDataHolder;
}
public static void resetSensorDataHolder() { public static void resetSensorDataHolder() {
sensorDataHolder = null; sensorDataHolder = null;
} }
@ -188,4 +197,8 @@ public class SenseDataHolder {
public static void resetApplicationDataHolder() { public static void resetApplicationDataHolder() {
applicationDataHolder = null; applicationDataHolder = null;
} }
public static void resetNetworkDataHolder() {
networkDataHolder = null;
}
} }

@ -35,6 +35,7 @@ import org.wso2.carbon.iot.android.sense.event.streams.activity.ActivityReceiver
import org.wso2.carbon.iot.android.sense.event.streams.application.ApplicationDataReceiver; import org.wso2.carbon.iot.android.sense.event.streams.application.ApplicationDataReceiver;
import org.wso2.carbon.iot.android.sense.event.streams.battery.BatteryDataReceiver; 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.call.CallDataReceiver;
import org.wso2.carbon.iot.android.sense.event.streams.data.NetworkDataReceiver;
import org.wso2.carbon.iot.android.sense.event.streams.screen.ScreenDataReceiver; import org.wso2.carbon.iot.android.sense.event.streams.screen.ScreenDataReceiver;
import org.wso2.carbon.iot.android.sense.event.streams.sms.SmsDataReceiver; import org.wso2.carbon.iot.android.sense.event.streams.sms.SmsDataReceiver;
@ -51,6 +52,8 @@ public class SenseDataReceiverManager {
private static ApplicationDataReceiver appDataReceiver; private static ApplicationDataReceiver appDataReceiver;
private static NetworkDataReceiver networkDataReceiver;
private SenseDataReceiverManager() { private SenseDataReceiverManager() {
} }
@ -171,5 +174,21 @@ public class SenseDataReceiverManager {
context.unregisterReceiver(appDataReceiver); context.unregisterReceiver(appDataReceiver);
appDataReceiver = null; appDataReceiver = null;
} }
} public static void registerNetworkDataReceiver(Context context) {
if (networkDataReceiver == null) {
networkDataReceiver = new NetworkDataReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MANAGE_NETWORK_USAGE);
context.registerReceiver(networkDataReceiver, intentFilter);
}
}
public static void unregisterNetworkDataReceiver(Context context) {
if (networkDataReceiver != null) {
context.unregisterReceiver(networkDataReceiver);
}
networkDataReceiver = null;
} }
} }

@ -0,0 +1,43 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.iot.android.sense.util.dto;
/**
* This holds api application consumer key and secret.
*/
public class ApiApplicationKey {
private String client_id;
private String client_secret;
public String getConsumerKey() {
return this.client_id;
}
public void setClient_id(String consumerKey) {
this.client_id = consumerKey;
}
public String getConsumerSecret() {
return this.client_secret;
}
public void setClient_secret(String consumerSecret) {
this.client_secret = consumerSecret;
}
}

@ -21,5 +21,5 @@ public interface ApiApplicationRegistrationService {
@POST @POST
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
String register(ApiRegistrationProfile registrationProfile); ApiApplicationKey register(ApiRegistrationProfile registrationProfile);
} }

@ -29,5 +29,5 @@ public interface TokenIssuerService {
@POST @POST
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
AccessTokenInfo getToken(@QueryParam("grant_type") String grant, @QueryParam("username") String username, AccessTokenInfo getToken(@QueryParam("grant_type") String grant, @QueryParam("username") String username,
@QueryParam("password") String password); @QueryParam("password") String password, @QueryParam("deviceId") String deviceId);
} }

Loading…
Cancel
Save