Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions client/client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ android {
//Publish location changes to server
buildConfigField "boolean", "LOCATION_PUBLISHING_ENABLED", "false"
//Collect WiFi scan results
buildConfigField "boolean", "WIFI_SCANNING_ENABLED", "false"
buildConfigField "boolean", "WIFI_SCANNING_ENABLED", "true"
//Is user consent required for file upload from device to server
buildConfigField "boolean", "REQUIRE_CONSENT_FOR_FILE_UPLOAD", "true"
}
Expand Down Expand Up @@ -307,7 +307,7 @@ android {
//Publish location changes to server
buildConfigField "boolean", "LOCATION_PUBLISHING_ENABLED", "false"
//Collect WiFi scan results
buildConfigField "boolean", "WIFI_SCANNING_ENABLED", "false"
buildConfigField "boolean", "WIFI_SCANNING_ENABLED", "true"
//Is user consent required for file upload from device to server
buildConfigField "boolean", "REQUIRE_CONSENT_FOR_FILE_UPLOAD", "true"
debuggable true
Expand Down Expand Up @@ -576,7 +576,7 @@ android {
//Publish location changes to server
buildConfigField "boolean", "LOCATION_PUBLISHING_ENABLED", "true"
//Collect WiFi scan results
buildConfigField "boolean", "WIFI_SCANNING_ENABLED", "false"
buildConfigField "boolean", "WIFI_SCANNING_ENABLED", "true"
//Is user consent required for file upload from device to server
buildConfigField "boolean", "REQUIRE_CONSENT_FOR_FILE_UPLOAD", "true"
}
Expand Down Expand Up @@ -709,7 +709,7 @@ android {
//Publish location changes to server
buildConfigField "boolean", "LOCATION_PUBLISHING_ENABLED", "true"
//Collect WiFi scan results
buildConfigField "boolean", "WIFI_SCANNING_ENABLED", "false"
buildConfigField "boolean", "WIFI_SCANNING_ENABLED", "true"
//Is user consent required for file upload from device to server
buildConfigField "boolean", "REQUIRE_CONSENT_FOR_FILE_UPLOAD", "true"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.wso2.iot.agent.beans;

import com.fasterxml.jackson.annotation.JsonInclude;

import org.wso2.iot.agent.AndroidAgentException;
import org.wso2.iot.agent.utils.CommonUtils;

Expand All @@ -26,6 +28,7 @@
* This class represents the basic information of
* the carbon-device-mgt supported device
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Device {

private String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ public void onReceive(final Context context, Intent intent) {
LocalNotification.startPolling(context);
}

/**
* Clear the device info and wifi info payloads stored in shared preference
* when the device restarts.
*/
if(Preference.getString(context, Constants.LAST_DEVICE_INFO_SHARED_PREF) != null) {
Preference.putString(context, Constants.LAST_DEVICE_INFO_SHARED_PREF,null);
}

if(Preference.getString(context, Constants.LAST_WIFI_SCAN_RESULT_SHARED_PREF) != null) {
Preference.putString(context, Constants.LAST_WIFI_SCAN_RESULT_SHARED_PREF,null);
}

if(Preference.getString(context, Constants.LAST_APP_LIST_SHARED_PREF) != null) {
Preference.putString(context, Constants.LAST_APP_LIST_SHARED_PREF,null);
}

if (Intent.ACTION_BOOT_COMPLETED.equals(action) || Constants.AGENT_UPDATED_BROADCAST_ACTION.equals(action)) {
if (Constants.OWNERSHIP_COSU.equals(Constants.DEFAULT_OWNERSHIP)) {
Preference.putBoolean(context.getApplicationContext(), Constants.AGENT_FRESH_START, true);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Base64;
import android.util.Log;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -44,9 +45,18 @@
import org.wso2.iot.agent.AndroidAgentException;
import org.wso2.iot.agent.beans.Device;
import org.wso2.iot.agent.utils.Constants;
import org.wso2.iot.agent.utils.Preference;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

public class NetworkInfoService extends Service {

Expand All @@ -61,6 +71,7 @@ public class NetworkInfoService extends Service {
private TelephonyManager telephonyManager;

private static final int DEFAULT_AGE = 0;
private static final String SSID = "ssid";
private static final String MAC_ADDRESS = "macAddress";
private static final String SIGNAL_STRENGTH = "signalStrength";
private static final String AGE = "age";
Expand Down Expand Up @@ -175,25 +186,23 @@ public static String getNetworkStatus() throws AndroidAgentException {
property.setName(Constants.Device.MOBILE_CONNECTION_TYPE);
property.setValue(info.getSubtypeName());
properties.add(property);
property = new Device.Property();
property.setName(Constants.Device.MOBILE_SIGNAL_STRENGTH);
property.setValue(String.valueOf(getCellSignalStrength()));
properties.add(property);
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
property = new Device.Property();
property.setName(Constants.Device.WIFI_SSID);
// NetworkInfo API of Android seem to add extra "" to SSID, therefore escaping it.
property.setValue(String.valueOf(thisInstance.getWifiSSID()).replaceAll("\"", ""));
properties.add(property);

property = new Device.Property();
property.setName(Constants.Device.WIFI_SIGNAL_STRENGTH);
property.setValue(String.valueOf(thisInstance.getWifiSignalStrength()));
properties.add(property);
}
}

property = new Device.Property();
property.setName(Constants.Device.MOBILE_SIGNAL_STRENGTH);
property.setValue(String.valueOf(getCellSignalStrength()));
properties.add(property);

try {
payload = mapper.writeValueAsString(properties);
} catch (JsonProcessingException e) {
Expand All @@ -214,25 +223,66 @@ public static void setCellSignalStrength(int cellSignalStrength) {
NetworkInfoService.cellSignalStrength = cellSignalStrength;
}

public static String getWifiScanResult() throws AndroidAgentException {
private static double compareSignalStrength(int previousSignalStrength, int newSignalStrength) {
int val = ((newSignalStrength - previousSignalStrength) / newSignalStrength) * 100;
if (val >= 0) {
return val;
} else {
return -1 * val;
}
}

public static String getWifiScanResult(Context context) throws AndroidAgentException {
if (wifiScanResults != null) {
Map<String, Integer> wifiScanResultsMap = null;
if (Preference.getString(context, Constants.LAST_WIFI_SCAN_RESULT_SHARED_PREF) != null) {
String lastUpdatedInfoString = Preference.getString(context, Constants.LAST_WIFI_SCAN_RESULT_SHARED_PREF);
byte[] lastUpdatedInfoByteArray = Base64.decode(lastUpdatedInfoString, Base64.DEFAULT);
ByteArrayInputStream bais = new ByteArrayInputStream(lastUpdatedInfoByteArray);
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(bais);
wifiScanResultsMap = (HashMap<String, Integer>) ois.readObject();
} catch (IOException e) {
throw new AndroidAgentException("Error occurred while deserialize lastWifiScanResultsMap object", e);
} catch (ClassNotFoundException e) {
throw new AndroidAgentException("Error occurred while casting to lastWifiScanResultsMap object", e);
}
} else {
wifiScanResultsMap = new HashMap<>();
}
try {
JSONArray scanResults = new JSONArray();
JSONObject scanResult;
for (ScanResult result : wifiScanResults) {
scanResult = new JSONObject();
scanResult.put(MAC_ADDRESS, result.BSSID);
scanResult.put(SIGNAL_STRENGTH, result.level);
scanResult.put(AGE, DEFAULT_AGE);
scanResult.put(CHANNEL, result.frequency);
scanResult.put(SNR, result.level); // temporarily added
scanResults.put(scanResult);
if (!wifiScanResultsMap.containsKey(result.wifiSsid.toString()) || compareSignalStrength(
Integer.valueOf(wifiScanResultsMap.get(result.wifiSsid.toString())), result.level) >= 0.2) {
scanResult = new JSONObject();
scanResult.put(SSID, result.wifiSsid);
scanResult.put(MAC_ADDRESS, result.BSSID);
scanResult.put(SIGNAL_STRENGTH, result.level);
scanResult.put(AGE, DEFAULT_AGE);
scanResult.put(CHANNEL, result.frequency);
scanResult.put(SNR, result.level); // temporarily added
scanResults.put(scanResult);
wifiScanResultsMap.put(result.wifiSsid.toString(), result.level);
}
}
if (Constants.DEBUG_MODE_ENABLED) {
Log.d(TAG, "Wifi scan result: " + scanResults.toString());
}
// scanning for next round
thisInstance.startWifiScan();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(bao);
oos.writeObject(wifiScanResultsMap);
String stringObject = null;
stringObject = Base64.encodeToString(bao.toByteArray(), Base64.DEFAULT);
Preference.putString(context, Constants.LAST_WIFI_SCAN_RESULT_SHARED_PREF, stringObject);
} catch (IOException e) {
throw new AndroidAgentException("Error occurred while serializing lastWifiScanResultsMap object", e);
}
return scanResults.toString();
} catch (JSONException e) {
String msg = "Error occurred while retrieving wifi scan results";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

import org.apache.commons.io.input.ReversedLinesFileReader;
import org.json.JSONArray;
Expand Down Expand Up @@ -90,8 +94,10 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public abstract class OperationManager implements APIResultCallBack, VersionBasedOperations {

Expand Down Expand Up @@ -284,6 +290,34 @@ public void getApplicationList(org.wso2.iot.agent.beans.Operation operation) thr
JSONArray result = new JSONArray();
RuntimeInfo runtimeInfo = new RuntimeInfo(context);
Map<String, Application> applications = runtimeInfo.getAppMemory();
Gson gson = new Gson();
JsonElement element = null;
if (Preference.getString(context, Constants.LAST_APP_LIST_SHARED_PREF) == null) {
element = gson.toJsonTree(apps, new TypeToken<ArrayList<DeviceAppInfo>>() {
}.getType());
if (!element.isJsonArray()) {
throw new AndroidAgentException("Invalid JSON format. Not JSON array.");
}
String applicationList = element.getAsJsonArray().toString().replace(" ", "");
Preference.putString(context, Constants.LAST_APP_LIST_SHARED_PREF, applicationList);
} else {
String previousAppList = Preference.getString(context, Constants.LAST_APP_LIST_SHARED_PREF);
element = gson.toJsonTree(apps, new TypeToken<ArrayList<DeviceAppInfo>>() {
}.getType());
if (!element.isJsonArray()) {
throw new AndroidAgentException("Invalid JSON format. Not JSON array.");
}
String newApplicationList = element.getAsJsonArray().toString().replace(" ", "");
if (previousAppList.equals(newApplicationList)) {
operation.setOperationResponse(Constants.NO_APPLIST_CHANGE);
operation.setStatus(resources.getString(R.string.operation_value_completed));
resultBuilder.build(operation);
return;
} else {
Preference.putString(context, Constants.LAST_APP_LIST_SHARED_PREF, newApplicationList);
}
}

for (DeviceAppInfo infoApp : apps) {
JSONObject app = new JSONObject();
try {
Expand Down Expand Up @@ -1164,7 +1198,7 @@ public void connectToRemoteSession(org.wso2.iot.agent.beans.Operation operation)
Object serverUrl = payload.get("serverUrl");
Object uuidToValidateDevice = payload.get("uuidToValidateDevice");
if (serverUrl != null) {
if(uuidToValidateDevice != null) {
if (uuidToValidateDevice != null) {
// Initialize web socket session
WebSocketSessionHandler.getInstance(context).initializeSession(serverUrl.toString(),
operation.getId(), uuidToValidateDevice.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import android.util.Log;
import android.widget.Toast;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
Expand Down Expand Up @@ -259,6 +260,7 @@ public static boolean isNetworkAvailable(Context context) {
public static String toJSON (Object obj) throws AndroidAgentException {
try {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper.writeValueAsString(obj);
} catch (JsonMappingException e) {
throw new AndroidAgentException("Error occurred while mapping class to json", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ public class Constants {
public static final int DEFAULT_START_INTERVAL = BuildConfig.DEFAULT_START_TIME;
public static final boolean FCM_FALLBACK_PULL_ENABLED = BuildConfig.FCM_FALLBACK_PULL_ENABLED;

/**
* Device info payload.
*/
public static final String LAST_DEVICE_INFO_SHARED_PREF = "lastDeviceObject";
public static final String LAST_WIFI_SCAN_RESULT_SHARED_PREF = "lastWifiScanResultsMap";
public static final String LAST_APP_LIST_SHARED_PREF = "lastApplicationList";
public static final String NO_APPLIST_CHANGE = "SAME_APPLICATION_LIST";

/**
* HTTP clients
*/
Expand Down Expand Up @@ -484,6 +492,8 @@ public final class Device {
public static final String PID = "PID";
public static final String SHARED_DIRTY = "SHARED_DIRTY";
public static final String PHONE_NUMBER = "PHONE_NUMBER";
public static final String DEVICE_IDENTIFIER = "DEVICE_IDENTIFIER";
public static final String DEVICE_NAME = "DEVICE_NAME";

private Device() {
throw new AssertionError();
Expand Down